/* CellSelector.m
 * Written By:  Thomas Burkholder
 *
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 */

#import <objc/objc-runtime.h>
#import "../Utilities.subproj/SortedList.h"
#import "../Utilities.subproj/ClassAgent.h"
#import "CellSelector.h"

@implementation CellSelector

static CellSelector *cellSelector;

+ (id)getCellClass
{
	return [CellSelector getCellClassRespondsTo:(SEL)0];
}

+ (id)getCellClassRespondsTo:(SEL)aSel
{
	if (!cellSelector) // set up a new one
		cellSelector = [[CellSelector alloc] init];
	[cellSelector setFilterSel:aSel];
	return [cellSelector choose];
}

- init
{
	char buf[MAXPATHLEN+1];
	NXBundle *bundle;

	[super init];

	// load the nib file
	bundle = [NXBundle bundleForClass:[CellSelector class]];
	[bundle getPath:buf forResource:"CellSelector" ofType:"nib"];
	[NXApp loadNibFile:buf owner:self withNames:NO
							fromZone:[self zone]];
	list = [[SortedList alloc] init];
	[list setAgent:[[ClassAgent alloc] init]];
	[list addObject:[ActionCell class]];
	[browser setDelegate:list];
	[browser setTarget:self];
	[browser setAction:@selector(check:)];
	[okButton setEnabled:NO];
	[browser loadColumnZero];
	return self;
}

- choose
{
	[panel makeFirstResponder:browser];
	[panel orderFront:cellSelector];
	[NXApp runModalFor:panel];
	[panel orderOut:self];
	return objc_lookUpClass([[browser selectedCell] stringValue]);
}

- check:sender
{
	id p;

	p = objc_lookUpClass([[browser selectedCell] stringValue]);
	if ((p && !filterSel) || (p && [p respondsTo:filterSel])) {
		[okButton setEnabled:YES];
		[browser setDoubleAction:@selector(ok:)];
	} else {
		[okButton setEnabled:NO];
		[browser setDoubleAction:(SEL)0];
	}
	return self;
}

- ok:sender
{
	[NXApp stopModal];
	return self;
}

- setFilterSel:(SEL)aSel
{
	filterSel = aSel;
	return self;
}

@end
