#import "UHInspector.h"
#import "uhInspection.h"	//inspection protocols

@implementation UHInspector

- init
{
	id bundle;
	char path[MAXPATHLEN+1];
	
	[super init];
	/* Assumption is that the nib file is in the same bundle from which
		the inspector class was loaded.  This could be the main bundle,
		or it could be a dynamically loaded bundle. */
	bundle=[NXBundle bundleForClass:[self class]];
	[bundle getPath:path forResource:[self name] ofType:"nib"];
	[NXApp loadNibFile:path owner:self];
	
	/* We want the panel to float forward, and not to stick around. */
	[[inspectorPanel setFloatingPanel:YES] setHideOnDeactivate:YES];
	return self;
}

/* To inspect a single object, without specifying the requestor. */
- inspect:anObject
{
	return [self inspect:anObject requestor:nil];
}

/* Inspect a single object, requestor gets notification of inspection. */
- inspect:anObject requestor:aRequestor
{
	object=anObject;	//save object to be inspected
	
	/* If object wants to know its inspector, tell it... */
	if(object && [object respondsTo:@selector(setInspector:)])
		[object setInspector:self];
		
	/* If the object instance has a label, use it as the title of the panel. */
	if(object && [object respondsTo:@selector(label)])
		[inspectorPanel setTitle:[object label]];
	/* ...else if item specifies a label for the inspector, use it. */
	else if([object respondsTo:@selector(getLabelForInspector)])
		[inspectorPanel setTitle:[object getLabelForInspector]];
		
	objectList=nil;
	requestor=aRequestor;	//save object to be notified of changes
	[self doRevert:self];	//init the panel to object's current values
	return self;
}

/* To inspect an object list, without specifying the requestor. */
- inspectList:aList
{
	return [self inspectList:aList requestor:nil];
}

/* Inspect an object list, requestor gets notification of inspection. */
- inspectList:aList requestor:aRequestor
{
	objectList=aList;
	object=[aList objectAt:0];
	requestor=aRequestor;	//save object to be notified of changes
	[self doRevert:self];	//init the panel to first object's values
	return self;
}

/* Simply returns the current object being inspected. */
- object
{
	return object;
}

/* Connect inspector panel's controls to this method.  It calls doOk:  */
- ok:sender
{
	/* If a list of objects, call doOk: in a loop for each object in the list. */
	if(objectList){
		int i;
		for(i=0; i<[objectList count]; i++){
			object=[objectList objectAt:i];
			[self doOk:sender];		//call the method implemented by the subclass
		}
		if(requestor && [requestor respondsTo:@selector(objectListWasInspected:)])
			[requestor objectListWasInspected:objectList];
	}
	/* If a single object, simply doOk: for it. */
	else{
		[self doOk:sender];		//call the method implemented by the subclass
		if(requestor && [requestor respondsTo:@selector(objectWasInspected:)])
			[requestor objectWasInspected:object];
	}
	return self;
}

/* Never call this method, but override to implement it in the subclass. */
- doOk:sender
{
	return self;
}

/* If you use a Revert button on the panel, connect it to this method. */
- revert:sender
{
	[self doRevert:sender];
	return self;
}

/* Never call this method, but override to implement it in the subclass. */
- doRevert:sender
{
	return self;
}

/* Returns the single view in which all of the panel's controls should be grouped.
	Connect to this view, usually a Box, in IB. */
- inspectorView
{
	return inspectorView;
}

/* Returns the inspector panel itself.  Connect in IB. */
- inspectorPanel
{
	return inspectorPanel;
}

/* Call this method to actually order the inspector panel forward. */
- showInspector:sender
{
	[inspectorPanel makeKeyAndOrderFront:self];
	return self;
}

/* Default is that we do not want Revert and OK buttons. */
- (BOOL)wantsButtons
{
	return NO;
}

/* Free the panel with this inspector. */
- free
{
	[inspectorPanel free];
	return [super free];
}

@end