/*--------------------------------------------------------------------------
 *
 * 	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 "ScrollViewExtensions.h"
#import <eointerface/EOController.h>

typedef enum {
    EOCONTROLLER, EODATABASE, EODATABASECONTEXT, EODATABASECHANNEL,
	EOADAPTOR, EOADAPTORCONTEXT, EOADAPTORCHANNEL
} EOFDelegates;




@implementation AppController

/*--------------------------------------------------------------------------
 *	Tracing
 *------------------------------------------------------------------------*/
- (BOOL) isTracing
{
	return trace;
}


- (BOOL) isVerbose
{
	return verbose;
}


- (BOOL) wantsAlertPanels
{
	return alert;
}


- setTracingOFF: sender
{
	trace = ( ! [[sender selectedCell] state]);
	return self;
}


- setIsVerbose: sender
{
	verbose = [[sender selectedCell] state];
	return self;
}


- setWantsAlertPanels: sender
{
	alert = [[sender selectedCell] state];
	return self;
}


- console:sender
{
	id	window = [console window];
	
	[window isVisible] ? [window orderOut:nil] : [window orderFront:nil];
	return self;
}


/*--------------------------------------------------------------------------
 *	Setting Delegates
 *------------------------------------------------------------------------*/
- _eofObjectForTag:(int)tag
{
	id	eofObject;
	
	switch (tag)
		{
		case EOCONTROLLER:
			eofObject = eoController;
			break;
			
		case EODATABASECHANNEL:	
			eofObject = [[eoController dataSource] databaseChannel]; 
			break;

		case EOADAPTOR:	
			eofObject = [[[[[eoController dataSource] databaseChannel] 
				adaptorChannel] adaptorContext] adaptor];
			break;

		case EOADAPTORCONTEXT:	
			eofObject = [[[[eoController dataSource] databaseChannel] 
				adaptorChannel] adaptorContext];
			break;

		case EOADAPTORCHANNEL:	
			eofObject = [[[eoController dataSource] databaseChannel] 
				adaptorChannel];
			break;

		case EODATABASE:
		case EODATABASECONTEXT:
		default:
			eofObject = nil;
			break;
		}

	return eofObject;
}


- setDelegates: sender
{
	int	rows, cols, iterator;
	
	[sender getNumRows:&rows numCols:&cols];
	for (iterator = 0; iterator < rows; iterator++)
		{
		id cell = [sender cellAt:iterator :0];
		[[self _eofObjectForTag:[cell tag]] 
			setDelegate:[cell state] ? eofDelegate : nil];
		}
		
	return self;
}
		

/*--------------------------------------------------------------------------
 *	Modifying EOController Characteristics
 *------------------------------------------------------------------------*/
- setEnableUndo: sender
{
	[eoController setUndoEnabled: [[sender selectedCell] state]];
	return self;
}


- setAutomaticUndoMarks: sender
{
	[eoController setMarksEveryOperation: [[sender selectedCell] state]];
	return self;
}


- setBufferEdits: sender
{
	[eoController setSavesToObjectsAutomatically: 
		( ! [[sender selectedCell] state])];
	return self;
}


- setBufferOperations: sender
{
	[eoController setSavesToDataSourceAutomatically: 
		( ! [[sender selectedCell] state])];
	return self;
}


- setMaximumUndoEntries: sender
{
	[eoController setMaximumUndoMarks: [sender intValue]];
	return self;
}


/*--------------------------------------------------------------------------
 *	Announcing Trace Info
 *------------------------------------------------------------------------*/
- (void) announce:sender selector:(SEL)selector
{
	return [self announce:sender selector:selector with:nil];
}


- (void) announce:sender selector:(SEL)selector with:array
{
	NSMutableString	*string;

	if (trace == NO) return;
	
	string = [NSMutableString stringWithFormat:@"BEGIN> {Sender = %s%s",
		[[[sender class] description] cString], verbose ? " " : "}"];
	
	if (verbose)
		{
		id verboseString = 
			[NSMutableString 
				stringWithFormat:@"}\n\t{Sender Description = %s}",
			[[sender description] cString]];
		[string appendString:verboseString];
		}
		
	[string appendFormat:@" \n\t{Delegate Method = %s} ", sel_getName(selector)];

	if (verbose && array)
		{
		id object;
		id verboseString = [NSMutableString stringWithCapacity:0];
		id enumerator = [array objectEnumerator];
		
		while (object = [enumerator nextObject])
			[verboseString 
				appendFormat:@"\n\t{Argument = %s} \n\t{Argument Description = %s} ", 
				[[[object class] description] cString], 
				[[object description] cString]];

		[string appendString:verboseString];
		}

		
	[string appendFormat:@"\n"];
	[console sprintf: "%s", [string cString]];
}


/*--------------------------------------------------------------------------
 *	Application Control
 *------------------------------------------------------------------------*/
- appDidInit: sender
{
	trace = YES;
	verbose = NO;
	alert = YES;
	
	[eoController setDelegate:eofDelegate];
	[[self _eofObjectForTag:EODATABASECHANNEL] setDelegate:eofDelegate];
	[[self _eofObjectForTag:EOADAPTORCHANNEL] setDelegate:eofDelegate];
	[[self _eofObjectForTag:EOADAPTORCONTEXT] setDelegate:eofDelegate];
	[[self _eofObjectForTag:EOADAPTOR] setDelegate:eofDelegate];
	[eoController setSelectsFirstObjectAfterFetch: YES];
	return self;
}


- windowWillClose:sender
{
	if (sender = [console window])
		[consoleButton setState:1];
		
	return self;
}


- windowDidBecomeKey:sender
{
	if (sender = [console window])
		[consoleButton setState:0];
		
	return self;
}


@end
