/* MiscInspectorManager.m				 
 *
 * This is a general class that should make it easy to implement Inspectors
 * as we know them from the WM or IB...but more general purpose.
 * Anyway. It's not a real inspector. It's the class the manages the selection
 * and all the different inspectors.
 *
 * For interface-info see the header file. The comments in this file mostly
 * cover only the real implementation details.
 *
 * Written by: 		Thomas Engel
 * Created:    		08.02.1994 (Copyleft)
 * Last modified: 	25.02.1994
 */

//#import "MiscInspectorManager.h"
//#import "MiscInspector.h"
//#import "MiscInspectorWrapper.h"
//#import "MiscSwapView.h"
#import <misckit/misckit.h>

@implementation MiscInspectorManager

- init
{
	self = [super init];
	if( !self ) return self;

	// OK. We really are an object...just load the NIB and get the
	// default inspectors.
	// And we'll initialize the swap Controllers to have some kind of startup
	// config.
	
	[[self loadNibSection] addDefaultInspectors];
	[self updateInspectors];
	return self;
}

- loadNibSection
{
	if( [NXApp loadNibSection:"Inspector.nib" owner:self] == nil )
		NXRunAlertPanel( NULL, "Couldn't load Inspector.nib",
						 "OK" ,NULL, NULL );
						 
	return self;
}

- makeKeyAndOrderFront:sender
{
	// If the window is visible we will make it key. We don't have to take 
	// care about the selection because [inspect] does it if the window IS
	// visible.
	// Otherwise we will updateInspectors and cause the swapView to swap.
		
	if( [window isVisible] ) 
		[window makeKeyAndOrderFront:sender];
	else
	{
		[self updateInspectors];
		[swapView swapContentView:self];
	}
	return self;
}

- inspect:anObject
{
	selection = anObject;
	if( selection == nil ) 
			selectionCount = 0;
	else	selectionCount = 1;

	if( [window isVisible] )
	{
		[self updateInspectors];
		[swapView swapContentView:self];
	}
	return self;
}

- inspectList:aList
{
	// If there is not really a List of object to inspect then lets handle it
	// as a simple inspection.
	
	if( [aList count] < 2 )
		return [self inspect:[aList objectAt:0]];
	
	// ...ok its a true list.
	
	selection = aList;
	selectionCount = [aList count];
	if( [window isVisible] )
	{
		[self updateInspectors];
		[swapView swapContentView:self];
	}
	return self;
}

- selection
{
	return selection;
}

- (unsigned)selectionCount
{
	return selectionCount;
}

- addInspector:anInspector
{
	// Here we add inspectors to our list. We do NOT check if they are unique.
	// Anyway.It will not make any problems.
	
	if( !inspectors ) inspectors = [List new];
	[inspectors addObject:anInspector];
	return self;
}

- addDefaultInspectors
{
	// Here we will add the 3 default inspectors for our default NIB.
	// We don't add them by setting the direct connection to the manager (self)
	// becuase we couln't tell in which order they will be added. And that's
	// a problem because order is very importat here.
	
	[notApplInspector setManager:self];
	[noSelInspector setManager:self];
	[multiSelInspector setManager:self];
	
	return self;
}

- updateInspectors
{
	// Let's find the inspector that want to deal with our current selection.
	// We will add everyone to the swapViews controller list.
	// If there are many different inspectors for the same trigger only the
	// last will remain in the controller list!
	//
	// Attention: This does not cause the swapView to update its display!!!!
	// 		  	  You might need to trigger a swap on your own.
	//			  Anyway. You should use this method directly. Use [inspect:].
	//
	// Note: Remember. If you change the controllers of a swapView you should
	//		 be sure that there will be a new controller 
	
	id		anInspector;
	int		i;
	
	for( i=0; i<[inspectors count]; i++ )
	{
		anInspector = [inspectors objectAt:i];
		if( [anInspector doesHandleSelection] )
		{
			if( [anInspector respondsTo:@selector(addWrappedControllers)] )
					[anInspector addWrappedControllers];
			else	[swapView addController:anInspector];
		}
	}
	return self;
}

- setSwapView:anObject
{
	swapView = anObject;
	[swapView setDelegate:self];
	
	return self;
}

- swapView
{
	return swapView;
}

- window
{
	return window;
}

- setOkButton:aButton
{
	// To keep up with the state and superview stuff.

	okButton = aButton;
	if( buttonsSuperview == nil ) buttonsSuperview = [okButton superview];
	buttonsVisible = YES;
		
	return self;
}

- okButton
{
	return okButton;
}

- setRevertButton:aButton
{
	// To keep up with the state and superview stuff.
	
	revertButton = aButton;
	if( buttonsSuperview == nil ) buttonsSuperview = [revertButton superview];
	buttonsVisible = YES;
	
	return self;
}

- revertButton
{
	return revertButton;
}

- setShowButtons:(BOOL)flag
{
	// Here we will hide/undhide ok/revert buttons.
	// By default they are disabled.

	[[okButton cell] setEnabled:NO];
	[[revertButton cell] setEnabled:NO];	

	if( flag == YES )
	{
		if( !buttonsVisible )
		{
			[buttonsSuperview addSubview:okButton];
			[buttonsSuperview addSubview:revertButton];
			buttonsVisible = YES;
		}
	}
	else
	{
		if( buttonsVisible )
		{
			[okButton removeFromSuperview];
			[revertButton removeFromSuperview];
			buttonsVisible = NO;
		}
	}

	return self;
}

- ok:sender
{
	return [[swapView contentsController] ok:sender];
}

- revert:sender
{
	return [[swapView contentsController] revert:sender];
}

- touch:sender
{
	// This methode should be invoked when the contens of the inspector
	// has changed and should be confirmed. We just change some Buttons here.

	[[okButton cell] setEnabled:YES];
	[[revertButton cell] setEnabled:YES];	
	[window setDocEdited:YES];
	
	return self;
}

- textDidChange:sender
{
	// If we are a text-objects delegate we get an easy touch-handling.
	// Well normally we will never be e texts delegate but the single
	// inspectors use this method when they are delegates.

	[self touch:self];
	return self;
}

/*
 * These are the delegate-methods we provide for our swapView. This is were
 * we make our view come to front after command-key actions. We do them after
 * the swap to make it nicer. Only if the window was not visible up to then.
 */
 
- viewWillSwap:sender
{
	// Befor the swap we have to ensure that the controllerList is up to date.
	// Otherwise the swapView would choose a wrong controller and display
	// the wrong one.
	
	if( ![window isVisible] ) [self updateInspectors];

	return self;
}

- viewDidSwap:sender
{
	if( ![window isVisible] ) [window makeKeyAndOrderFront:self];

	return self;
}

@end

/*
 * History: 25.02.94 Seems like I forgot the ok,revert here ... hmm
 *
 *			22.02.94 Made it new-swapView-conform.
 *
 *			11.02.94 Remove some little 'feature-bugs'
 *
 *			08.02.94 A complete redesign for the MiscKit. Only the basic idea
 *					 remains the same as in earlier versins.
 *
 *			15.01.94 Added automatich window title setting to -revert.
 *
 *			11.01.94 Did make the ok and revert method more firendly.
 *					 They know let the viewController know whats going on. 
 *
 *			08.01.94 Updated to our new swapController.
 *
 *			18.12.93 Many methods get their first code. It all should end up
 *					 with easy new-inspectors just a click away...
 *
 *			14.12.93 Just added some code for object inspection. Well some
 *					 pieces of the code should be moved to the MultipagePanel
 *
 *			25.11.93 Created a copy of the pref-manager.
 *
 *
 * Bugs: - inspect(List):
 *		   does check only [window isVisilbe] this is not what I really want.
 *		   I should aviod updating inside miniwindows ! Saves some drawing time
 *		   But Inspectors are seldom minimizable so this might not be too 
 *         serious.
 */