/*--------------------------------------------------------------------------
 *
 * 	You may freely copy, distribute, and reuse the code in this example.
 * 	SHL Systemhouse disclaims any warranty of any kind, expressed or  
 *	implied, as to its fitness for any particular use.
 *
 *
 *	AppController
 *
 *	Inherits From:		NSObject
 *
 *	Conforms To:		None
 *
 *	Declared In:		AppController.h
 *
 *------------------------------------------------------------------------*/
#import "AppController.h"
#import "BetterRealMutableArray.h"



@implementation AppController

/*--------------------------------------------------------------------------
 *	App delegate methods
 *------------------------------------------------------------------------*/
- appDidInit:sender
{
	array = [[BetterRealMutableArray arrayWithCapacity:5] retain];
	[window makeKeyAndOrderFront:nil];
	return self;
}


- (void) dealloc
{
	[array release];
	return [super dealloc];
}


- _displayArray
{
	unsigned	iterator = 0;
	id			enumerator;
	id			nextObject;
	int			column;
	int			count = [array count];

	column = [visualArray selectedCol];
	enumerator = [array objectEnumerator];

	for (iterator = 0; iterator < count; iterator++) {

		nextObject = [enumerator nextObject];

		if (nextObject == @"blueBlock") {
			if (iterator == column)
				[[visualArray cellAt:0:iterator] setIcon: "blueBlockOn"];
			else [[visualArray cellAt:0:iterator] setIcon: "blueBlock"];
		}
		else if (nextObject == @"blackBlock") {
			if (iterator == column)
				[[visualArray cellAt:0:iterator] setIcon: "blackBlockOn"];
			else [[visualArray cellAt:0:iterator] setIcon: "blackBlock"];
		}
		else // object must be nil
			if (iterator == column)
				[[visualArray cellAt:0:iterator] setIcon: "on"];
			else [[visualArray cellAt:0:iterator] setIcon: "empty"];

	}
	return self;
}
			

/*--------------------------------------------------------------------------
 *	Target / action methods
 *------------------------------------------------------------------------*/
- delete:sender
{
	[array removeObjectAtIndex: [visualArray selectedCol]];

	[self _displayArray];
    return self;
}


- empty:sender
{
	[array removeAllObjects];
	[self _displayArray];
    return self;
}


- insert:sender
{
	int			col;
	NSString 	*imagename;

	if ([objectSelector selectedCol] == 0)
		imagename = @"blueBlock";
	else imagename = @"blackBlock";

	if ((col = [visualArray selectedCol]) >= 0)
		[array insertObject:imagename atIndex:col];
	else
		[(NSMutableArray *)array addObject:imagename];

	[self _displayArray];
    return self;
}


- selectCell:sender
{
	[self _displayArray];
	return self;
}


@end
