/*
**  Node.m
**
**  Copyright (c) 2004
**
**  Author: Yen-Ju Chen <yjchenx@hotmail.com>
**
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation; either version 2 of the License, or
**  (at your option) any later version.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "Node.h"
#include <Foundation/Foundation.h>
#include "GNUstep.h"
#include "Utilities.h"

@implementation Node

- (void) resetOrderOfChildren
{
  unsigned int i, count = [[self children] count];
  for(i = 0; i < count; i++)
    {
      [[[self children] objectAtIndex: i] setOrder: i];
    }
}

- (BOOL) nameExistsInChildren: (NSString *) string
{
  unsigned int i, count = [[self children] count];
  for(i = 0; i < count; i++)
    {
      if ([string isEqualToString: [(Node *)[[self children] objectAtIndex: i] name]])
        {
	  return YES;
        }
    }
  return NO;
}

- (NSString *) keyOfItem
{
  return keyOfAttributes([self order], [self category], [self name], [self type]);
}

- (NSComparisonResult) compareOrder: (Node *) other
{
  if ([self order] < [other order])
    return NSOrderedAscending;
  else if ([self order] > [other order])
    return NSOrderedDescending;
  else
    return [[self name] compare: [other name]];
}

- (void) sortChildren
{
  [children sortUsingSelector: @selector(compareOrder:)];
}

- (Node *) childWithName: (NSString *) string
{
  unsigned int i, count = [children count];
  Node *object;
  for(i = 0; i < count; i++)
    {
      object = [children objectAtIndex: i];
      if ([[object name] isEqualToString: string])
        return object;
    }
  return nil;
}

- (id) init
{
  self = [super init];

  children = [[NSMutableArray alloc] init];

  return self;
}

- (void) setOrder: (int) number
{
  order = number;
}

- (int) order
{
  return order;
}

- (void) setCategory: (NSString *) string
{
  ASSIGN(category, AUTORELEASE([string copy]));
}

- (NSString *) category
{
  return AUTORELEASE([category copy]);
}

- (void) setName: (NSString *) string
{
  ASSIGN(name, AUTORELEASE([string copy]));
}

- (NSString *) name
{
  return AUTORELEASE([name copy]);
}

- (void) setValue: (id) string
{
  ASSIGN(value, AUTORELEASE([string copy]));
}

- (id) value
{
  return AUTORELEASE([value copy]);
}

- (void) setType: (NSString *) string
{
  ASSIGN(type, AUTORELEASE([string copy]));
}

- (NSString *) type
{
  return AUTORELEASE([type copy]);
}

- (void) addChild: (Node *) child
{
  [children addObject: child];
}

- (void) removeChildAtIndex: (unsigned int) index
{
  if ((index == NSNotFound) || (index > [children count]))
    return;

  [children removeObjectAtIndex: index];
}

- (NSMutableArray *) children
{
  return children;
}

- (void) dealloc
{
  RELEASE(name);
  RELEASE(value);
  RELEASE(children);
  [super dealloc];
}

@end

