/* BBBrowserManager.m				 
 *
 * This is my subclass of the IKSuitcase to allow browsing suitcases like
 * the folder objects.
 *
 * For interface-info see the header file. The comments in this file mostly
 * cover only the real implementation details.
 *
 * Written by: 		Thomas Engel
 * Created:    		14.05.1994 (Copyleft)
 * Last modified: 	14.05.1994
 */

#import "BBBrowserManager.h"
#import "BBAppManager.h"
#import "Inspector.subproj/BBMasterInspector.h"
#import "BBBeaker.h"
#import "BBCameraWindow.h"
#import "BBRegion.h"
#import "BBAtom.h"
#import "BBBond.h"

@implementation BBBrowserManager

- initBrowser:aBrowser root:anObject
{
	// Here we setup all the messages for the different mouse clicks and
	// we ensure that selecting multiple branches is allowed.
	
	[super initBrowser:aBrowser root:anObject];
	[browser setTarget:self];
	[browser setAction:@selector(updateSelection:)];
	[browser setDoubleAction:@selector(openSelection:)];
	[browser setBranchSelectionEnabled:YES];
	
	[shelf setTarget:self];
	[shelf setDoubleAction:@selector(openSelection:)];
		
	return self;
}

- free
{
	if( selectionList ) [selectionList free];
	return [super free];
}

- browser:sender setColumnIcon:cell for:(int)column
{
	[super browser:sender setColumnIcon:cell for:(int)column];
	[self updateSelection:self];
	return self;
}

- updateSelection:sender
{
	// We will take the selection that is inside the 
	int	col;
	
	if( selectionList ) [selectionList free];
	
	col = [browser selectedColumn];
	
	if( col == -1 )
	{
		selectionList = [List new];
		[selectionList addObject:root];
	}
	else
		selectionList = [browser getSelectionInColumn:col];
	
	// According to the kind of selection we have different results:
	// Here we set the images, inspector etc.
	
	[[[NXApp delegate] inspector] inspectList:selectionList];
	
	return self;
}

- openSelection:sender
{
	// Here we will try to open the selection the sender does provide.
	// It might be the shelf or the browser itself. We have to create a new
	// list here because we don't know what will happen to it!
	
	id	aList;
	id	anObject;
	id	inspector;
	
	if( sender == shelf )
	{
		anObject = [[shelf selectedCell] delegate];
		if( [anObject respondsTo:@selector(count)] &&
			[anObject respondsTo:@selector(objectAt:)] )
			aList = [anObject copy];
		
		else
		{
			aList = [List new];
			[aList addObject:anObject];	
		}
	}
	else aList = [selectionList copy];

	// Depending on the type of selection we will use different approaches.
	// If it is one object we will try to use its open: method and exit from
	// here.

	if( [aList count] == 0 )
	{
		[aList free];
		return self;
	}
	
	anObject = [aList objectAt:0];
	
	if( [aList count] == 1 )
	{
		if( [anObject respondsTo:@selector(open:)] )
		{
			[aList free];
			[anObject open:self];
			return self;
		}
	}
	
	// In every other case we will try if the first object does know its
	// displayClass. This should work with a single or multiple selection.
	// We will use this class to open the whole selection.
	
	if( [anObject respondsTo:@selector(displayClass)] )
	{
		[[[anObject displayClass] alloc] initFrom:aList 
									     asPartOf:root];
		return self;
	}
	
	// In every other case we would just ask the inspector to inspect the
	// selection. 
	// This is currently the browsers active selection. Double clicks from
	// the shelf do NOT work! (see the bugs)

	else
	{
		inspector = [[NXApp delegate] inspector];
		[inspector makeKeyAndOrderFront:self];
		[inspector inspectList:selectionList];
	}
	return self;
}

- group:sender
{
	// We will try to group the selection inside the borwser.
	
	id	newGroup;
	int	i;
	
	if( [selectionList count] < 1 ) return self;
	
	// So it sames there is a valid selection. Depending on the type of a 
	// selection we have to make different grouping effords.
	// Atoms or Bond will lead to a new region inside the relating molecule.
	
	if( [[selectionList objectAt:0] isKindOf:[BBAtom class]] )
	{
		newGroup = [BBRegion new];
		
		for( i=0; i<[selectionList count]; i++ )
			[newGroup addAtom:[selectionList objectAt:i]];
		
		[[[selectionList objectAt:0] molecule] addRegion:newGroup];
	}
	// Grouping Bonds is almost the same as grouping Atoms
	
	else if( [[selectionList objectAt:0] isKindOf:[BBBond class]] )
	{
		newGroup = [BBRegion new];
		
		for( i=0; i<[selectionList count]; i++ )
			[newGroup addBond:[selectionList objectAt:i]]; 

		[[[selectionList objectAt:0] molecule] addRegion:newGroup];
	}
	
	return self;
}

/*
 * Here come the methods we will implement to satisfy our window.
 * They are useful to change the inspector and handle oher tasks.
 */
 
- windowWillClose:sender
{
	return [root close:sender];
}

- windowDidClose:sender
{
	// OK we are closed...so lets make the root everything.

	return [root free];
}

- windowDidBecomeMain:sender
{
	// Here we get informed when we become the main window. This is the point
	// to change the inspector to inspect our selection.
	
	[[[NXApp delegate] inspector] inspectList:selectionList];
	return self;
}

- windowDidResignMain:sender
{
	return self;
}

/*
 * History: 16.05.94 Now we do control the window and browsing stuff.
 *
 *
 * Bugs: - Double clicking from the shelf will no show the inspector.
 *		   This feature will be added once the iconPath can be set via a tree.
 */

@end
