/*--------------------------------------------------------------------------
 *
 * 	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.
 *
 *
 *	SortController
 *
 *	Inherits From:		NSObject
 *
 *	Conforms To:		None
 *
 *	Declared In:		SortController.h
 *
 *
 *------------------------------------------------------------------------*/
#import "SortController.h"
#import "SlidingMatrix.h"
#import <appkit/appkit.h>
#import <eointerface/eointerface.h>
#import <eoaccess/eoaccess.h>
#import <foundation/foundation.h>




@implementation SortController

- appDidInit:sender
{
	int 			count, iterator;
   	NSEnumerator 	*enumerator;
	id 				object, cell;
	NXRect			frameRect;
	NXSize			cellSize;
	id				attributes;

	[window disableDisplay];
	entity = [(EODatabaseDataSource *)[(EOController *)controller
						dataSource] entity];

	attributes = [entity attributes];
	[attributeOrder getFrame:&frameRect];
	[attributeOrder getCellSize:&cellSize];
	cellSize.width = frameRect.size.width;
	[attributeOrder setCellSize:&cellSize];

	count = [attributes count];
	[attributeOrder renewRows:count cols:1];
	[attributeOrder sizeToCells];

	enumerator = [attributes objectEnumerator];
	iterator = 0;

	while (object = [enumerator nextObject]) {
		cell = [attributeOrder cellAt:iterator:0];
		[cell setTitle: [[(EOAttribute *)object name] cString]];
		[cell setTag: 0];
		iterator++;
	}

	[scrollView setDocView:attributeOrder];
	[controller fetch];
	[window reenableDisplay];
	[window display];
	[hints orderFront:nil];
	[window makeKeyAndOrderFront:nil];
	return self;
}


- _orderArrayFromList
{
	id	attributeDetails;
	id	orderArray = [[[NSMutableArray allocWithZone:[self zone]]
							initWithCapacity:0] autorelease];
	int	iterator, count;
	id	ordering, current;

	//  List containing cells with sorting details
	attributeDetails = [attributeOrder cellList];

	// get ordering from attribute list
	count = [attributeDetails count];

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

		current = [attributeDetails objectAt:iterator];
		if ([current tag]) {
			//  there is a sorting order defined
			ordering = [[EOAttributeOrdering allocWithZone:[self zone]]
				initWithAttribute:[entity attributeNamed:
					[NSString stringWithCString:[current title]]]
				ordering: [current tag]];
			[(NSMutableArray *)orderArray addObject: ordering];
		}
	}
	return orderArray;
}


- _orderArrayFromTable
{
	id	orderArray = [[[NSMutableArray allocWithZone:[self zone]]
							initWithCapacity:0] autorelease];
	int	iterator, iterator2, orderCount, attributeCount;
	id	ordering=nil, current, detail=nil;
	id	orderList = nil, attributeDetails;

	//  List containing cells with sorting details
	attributeDetails = [attributeOrder cellList];

	// get ordering from table view columns
	orderList = [tableView columnList];
	orderCount = [orderList count];
	attributeCount = [attributeDetails count];


	for (iterator = 0; iterator < orderCount; iterator++) {
		current = [[[orderList objectAt: iterator] identifier] key];
		detail = nil;				

		for (iterator2 = 0; (detail == nil) && (iterator2 < attributeCount);
							 iterator2++) {
			detail = [attributeDetails objectAt:iterator2];
				
			if (detail) {
				if (NXOrderStrings([detail title], [current cString],
					YES, -1, NULL))
					detail = nil;
			}
		}

		if (detail) {

			if ([detail tag]) {				
			// if there is a sort order imposed...

				ordering = [[EOAttributeOrdering allocWithZone:[self zone]]
					initWithAttribute:[entity attributeNamed:current]
					ordering:[detail tag]];

				[(NSMutableArray *)orderArray addObject: ordering];
			}
		}
	}

	return orderArray;		
}		


- _sortedFetch
{
	NSArray *order;

	if ([priority selectedRow] == 0)
 		order = [[self _orderArrayFromList] retain];
	else
 		order = [[self _orderArrayFromTable] retain];

	[window disableDisplay];
	[(EODatabaseDataSource *)[controller dataSource]
			setFetchOrder: order];
	[controller fetch];
	[order release];
	[window reenableDisplay];
	[window display];
	return self;
}
	

- changeOrder:sender
{
	id current;

	current = [attributeOrder selectedCell];

	switch ([[sender selectedCell] tag]) {
	case 0:
		[current setIcon: "low"];
		[current setTag:1];
		break;
	case 1:
		[current setIcon: "high"];
		[current setTag:2];
		break;
	case 2:
		[current setIcon: "empty"];
		[current setTag:0];
		break;
	}

	[attributeOrder display];
    return self;
}


- doIt: sender
{
	[self _sortedFetch];
	return self;
}


- ofSorts:sender
{
	[window makeKeyAndOrderFront:nil];	
    return self;
}


@end
