//************************************************************************
//
//	OICNSTableColumnInspector.m
//
//	by Juergen Moellenhoff <jm@oic.de>		
//
//
//	This code is supplied "as is" the author makes no warranty as to its 
//	suitability for any purpose.  This code is free and may be distributed 
//	in accordance with the terms of the:
//		
//			GNU GENERAL PUBLIC LICENSE
//			Version 2, June 1991
//			copyright (C) 1989, 1991 Free Software Foundation, Inc.
// 			675 Mass Ave, Cambridge, MA 02139, USA
//
//************************************************************************

#import "OICNSTableColumnInspector.h"
#import <OICInterface/OICNSTableHeaderView.h>

@implementation OICNSTableColumnInspector

- init
{
	NSString 	*nibPath;
	
	[super init];
	
	nibPath = [[NSBundle bundleForClass:[self class]] 
				pathForResource:@"OICNSTableColumnInspector" ofType:@"nib"];

	[NSBundle loadNibFile:nibPath 
				externalNameTable:[NSDictionary dictionaryWithObjectsAndKeys:self, @"NSOwner", nil]
				withZone:[self zone]];
	
	 return self;
}

- (void)ok:sender
{
	if (sender == orderingMatrix)
	{
        switch([[orderingMatrix selectedCell] tag])
        {
        case 0:
            [[self object] setOrdering:NSOrderedAscending];
            break;

        case 1:
            [[self object] setOrdering:NSOrderedDescending];
            break;

        case 2:
        default:
            [[self object] setOrdering:NSOrderedSame];
            break;
        }

        [[NSApp activeDocument] drawObject:[self object]];
	}

	if (sender == dataCellMatrix)
	{
		id	cell = nil;

        switch([[dataCellMatrix selectedCell] tag])
        {
        case 1: // NSImageCell
            cell = [[NSImageCell alloc] init];
            break;

		case 2: // NSComboBoxCell
            cell = [[NSComboBoxCell alloc] init];
            break;

        case 0: // NSTextFieldCell
        default:
            cell = [[NSTextFieldCell alloc] init];
            break;
        }

		[[NSApp activeDocument] detachObject:[[self object] dataCell]];

		[[self object] setDataCell:cell];
		[cell release];
	}

	if (sender == caseInsensitiveSwitch)
		[[self object] setCaseInsensitive:([caseInsensitiveSwitch state] != 0)];

	[super ok:sender];
}


- (void)revert:sender
{
	int	selectedCell = 0;
	id	dataCell;

	[caseInsensitiveSwitch setState:[[self object] isCaseInsensitive]];

	switch([(OICNSTableColumn *)[self object] ordering])
	{
	case NSOrderedAscending:
		selectedCell = 0;
		break;

	case NSOrderedDescending:
		selectedCell = 1;
		break;

	case NSOrderedSame:
	default:
		selectedCell = 2;
		break;
	}
	[orderingMatrix selectCellWithTag:selectedCell];


	dataCell = [[self object] dataCell];
	if ([dataCell isKindOfClass:[NSTextFieldCell class]])
		selectedCell = 0;

	if ([dataCell isKindOfClass:[NSImageCell class]])
		selectedCell = 1;

	if ([dataCell isKindOfClass:[NSComboBoxCell class]])
		selectedCell = 2;

	[dataCellMatrix selectCellWithTag:selectedCell];
	[super revert:sender];
}


- (BOOL)wantsButtons
{
	return NO;
}

@end

@implementation OICNSTableColumn(C_OICNSTableColumnIBAdds)

- (void)setTableView:(NSTableView *)aTableView
{
	[super setTableView:aTableView];

	// Create the right headerView when someone copy, paste the OICNSTableColumn
	// into a "normal" NSTableView.
	if (![[aTableView headerView] isKindOfClass:[OICNSTableHeaderView class]])
	{
		OICNSTableHeaderView	*newTableHeaderView;

		newTableHeaderView = [[[OICNSTableHeaderView alloc] initWithFrame:[[aTableView headerView] frame]] autorelease];

		NSLog(@"%@:-%@, Replace HeaderView [%@] with HeaderView [%@]", 
			NSStringFromClass([self class]), NSStringFromSelector(_cmd), 
			[aTableView headerView], newTableHeaderView);

		[aTableView setHeaderView:newTableHeaderView];
	}
}

- awakeAfterUsingCoder:(NSCoder *)aDecoder
{
 	// ...set the fetch order in IB test mode...
	[self performSelector:@selector(awakeFromNib) withObject:self afterDelay:250.0 / 1000.0];

	return self;
}

@end
