//--------------------------------------------------------------------------
// Copyright 1996 Running Start, Inc.  All Rights Reserved.
//
// Ralph Zazula (zazula@running-start.com) http://www.running-start.com
//
// You may freely copy, distribute and reuse the code in this example.
// Running Start, Inc. disclaims any warranty of any kind, expressed or 
// implied, as to its fitness for any particular use.
//
//		This notice may not be removed from this source code.
//
//	This object is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"License.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//--------------------------------------------------------------------------

#import "ListConnectionInspector.h"

@implementation ListConnectionInspector

- init
/*
 * Load the NIB file for this inspector.
 */
{
	char path[MAXPATHLEN+1];
	id bundle;
	
	bundle = [NXBundle bundleForClass:[self class]];
	[bundle getPath:path forResource:"ListConnectionInspector" ofType:"nib"];
	[NXApp loadNibFile:path owner:self withNames:NO fromZone:[self zone]];
	return self;
}

- ok:sender
/*
 * We make the OK button "Connect".  Use this method to add the
 * connect destination to the List object.
 */
{
	if([NXApp isConnecting]) {
		[object addObjectIfAbsent:[NXApp connectDestination]];
		[self revert:nil];
	}
	return [super ok:sender];
}

- revert:sender
/*
 * this method is called in two instances:
 *		sender == nil : used to tell us to update the display
 *		sender != nil : when the REVERT ("Disconnect") button is pressed
 *
 *	use this to enable/disable the buttons and select entries in the
 * list if the user draws out a connection that we have already stored.
 */
{
	if(!sender) {
		/* just updating the screen */
		[okButton setTitle:"Connect"];
		[okButton setEnabled:([NXApp isConnecting] && 
			[object indexOf:[NXApp connectDestination]] == NX_NOT_IN_LIST)];
		
		[revertButton setTitle:"Disconnect"];
		[revertButton setEnabled:([NXApp isConnecting] && 
			[object indexOf:[NXApp connectDestination]] != NX_NOT_IN_LIST)];
			
	} else {
		/* the "Disconnect" button was pressed */
		[object removeObjectAt:[[browser matrixInColumn:0] selectedRow]];
		[NXApp stopConnecting];
	}
	
	/* update the browser */
	[browser loadColumnZero];

	/*
	 * This next bit checks to see if the user has dragged out a connection
	 * that duplicates one we've already stored in the list.  If so, reflect
	 * this by selecting the appropriate cell in the browser.
	 */
	if([NXApp isConnecting] && 
		([object indexOf:[NXApp connectDestination]] != NX_NOT_IN_LIST)) {
		[[browser matrixInColumn:0] 
			selectCellAt:[object indexOf:[NXApp connectDestination]]:0];
	}
		
	return [super revert:sender];
}

- (BOOL)wantsButtons
{
	return YES;
}

- displayConnection:sender
/*
 * The target of the NXBrowser.  Displays the connection for the selected entry.
 */
{
	[NXApp displayConnectionBetween:object 
		and:[object objectAt:[[sender matrixInColumn:0] selectedRow]]];
	
	return self;
}

/*** as the NXBrowser's delegate ***/

- (int)browser:sender fillMatrix:matrix inColumn:(int)column
{
	int	i;
	id		cellList, theCell;
	char buf[128];
	id	item;
	
	/* Set the matrix to have the right number of cells. */
	[matrix renewRows:[object count] cols:1];

	/* Get list of cells from the matrix. */
	cellList = [matrix cellList];

	/*
	 * For each cell set its value, set whether it is a leaf
	 * or not and mark it loaded.
	 */
	for (i=0; i<[cellList count]; i++) {
		item = [object objectAt:i];
		theCell = [cellList objectAt:i];
		if([item respondsTo:@selector(title)]) {
			sprintf(buf, "%s (%s)", [[item class] name], [item title]);
		} else {
			sprintf(buf, "%s", [[item class] name]);
		}
		[theCell setStringValue:buf];
		[theCell setLeaf:YES];
		[theCell setLoaded:YES];
	}
	
	/* Return the number of rows. */
	return [object count];
}

@end
