/* SwitchViewConnectInspector.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 "SwitchViewConnectInspector.h"
#import "SVConnector.h"
#import "../Utilities.subproj/SortedList.h"
#import "../Utilities.subproj/ConnectorAgent.h"

static BOOL selecting;

@implementation SwitchView (ConnectionsInspector)

-(const char *)getConnectInspectorClassName
{
	NXEvent *ev = [NXApp currentEvent];

	// NOTE:  This doesn't work on PC's and most NextStations, due
	// to the fact that control-alt is mapped to be a Help key!!!
	// expect this to go away later with the more complete version of
	// superclass-inspector access.
	if (ev->flags & NX_ALTERNATEMASK)
		return [super getConnectInspectorClassName];
	else
		return "SwitchViewConnectInspector";
}

@end

@implementation SwitchViewConnectInspector

- init
{
	char buf[MAXPATHLEN + 1];
	id bundle;
	
	[super init];
	
	bundle = [NXBundle bundleForClass:[SwitchView class]];
	[bundle getPath:buf forResource:"SwitchViewConnectInspector" ofType:"nib"];
	[NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];
	[connectorList setAgent:[[ConnectorAgent alloc] init]];
	[browser setEmptySelectionEnabled:YES];
	[browser loadColumnZero];
	
	selecting = NO;
	
	return self;
}

- free
{
	// should also be freeing the window, views.
	[[connectorList agent] free];
	[connectorList free];
	return [self free];
}

- selectConnection:sender
{
	id conn = [connectorList objectAt:[[sender matrixInColumn:0] selectedRow]];

	if (!conn) return self;

	selecting = YES;
	// draw the connection
	[NXApp displayConnectionBetween:[conn source] and:[conn destination]];
	[connectButton setTitle:"Disconnect"];
	[connectButton setTag:1];
	[connectButton setEnabled:YES];
	return self;
}

- ok:sender
{
	int i;
	id aConnector, b;

	if ([sender tag] == 0) {
		aConnector = [[SVConnector alloc] init];
		[aConnector setDestination:[NXApp connectDestination]];
		[aConnector setSource:object];
		[[NXApp activeDocument] addConnector:aConnector];
		[connectorList addObject:aConnector];
		[browser loadColumnZero];
		b = [browser matrixInColumn:0];
		[b selectCellAt:[b cellCount]-1 :0];
		[connectButton setTitle:"Disconnect"];
		[connectButton setTag:1];
	} else if ([sender tag] ==1) {
		i = [[browser matrixInColumn:0] selectedRow];
		aConnector = [connectorList removeObjectAt:i];
		
		[[NXApp activeDocument] removeConnector:aConnector];
		[aConnector free];
		[browser loadColumnZero];
		[[browser matrixInColumn:0] selectCellAt:-1 :-1];
		[connectButton setTitle:"Connect"];
		[connectButton setTag:0];
	}
	return [super ok:sender];
}

- revert:sender
{
	int s;

	if (selecting) {
		selecting = NO;
		return self;
	}

	[connectorList empty];
	[[NXApp activeDocument] listConnectors:connectorList forSource:object];
	[browser loadColumnZero];

	if ([NXApp isConnecting]) {
		s = [connectorList indexOf:[NXApp connectDestination]];
		if (s == NX_NOT_IN_LIST) {
			[[browser matrixInColumn:0] selectCellAt:-1 :-1];
			[connectButton setTitle:"Connect"];
			[connectButton setTag:0];
			[connectButton setEnabled:YES];
		} else {
			[[browser matrixInColumn:0] selectCellAt:s :0];
			[connectButton setTitle:"Disconnect"];
			[connectButton setTag:1];
			[connectButton setEnabled:YES];
		}
	} else {
		[[browser matrixInColumn:0] selectCellAt:-1 :-1];
		[connectButton setTitle:"No Selection"];
		[connectButton setEnabled:NO];
		[connectButton setTag:2];
	}

	return [super revert:sender];
}

- (BOOL)wantsButtons
{
	return NO;
}

@end
