/* 
 * ToDoInspector - controller for the inspector panel
 *
 * You may freely copy, distribute and reuse the code in this example.
 * This code is provided AS IS without warranty of any kind, expressed 
 * or implied, as to its fitness for any particular use.
 *
 * Copyright 1995 Ralph Zazula (rzazula@next.com).  All Rights Reserved.
 *
 */

#import "ToDoInspector.h"
#import "ToDoController.h"
#import "ToDoItem.h"
#import "objectLoadNib.h"
#import <appkit/Window.h>

#define _controller _reservedToDoInspector1

/* 
 * the class for the InspectorPanel - a Panel that
 * refuses to become key or main
 */
@interface NoKeyWindow : Panel
@end

@implementation NoKeyWindow
- (BOOL)canBecomeKeyWindow { return NO; }
- (BOOL)canBecomeMainWindow { return NO; }
@end

@implementation ToDoInspector

- awakeFromNib
{
	[window setFrameUsingName:"ToDoInspector"];
	[window setFrameAutosaveName:"ToDoInspector"];
	[window setHideOnDeactivate:YES];
	[showPendingSwitch setIntValue:[_controller showPending]];
	[showPrivateSwitch setIntValue:[_controller showPrivate]];
	[showCompletedSwitch setIntValue:[_controller showCompleted]];
	return self;
}

- window
{
	if(!window) {
		[self loadNib:"Inspector"];
	}
	return window;
}

- (int)type
{
	return [[typeMatrix selectedCell] tag];
}

- switchType:sender
{
	id itemList = [_controller selectedItems];
	
	[itemList makeObjectsPerform:@selector(setType:) with:(id)[self type]];
	
	[_controller dirty:self];
	[_controller update];
		
   return self;
}

- completed:sender
{
	id itemList = [_controller selectedItems];
	
	[itemList makeObjectsPerform:@selector(setCompleted:) 
		with:(id)[completedSwitch intValue]];
		
	[_controller dirty:self];
	[_controller update];
	return self;
}

- private:sender
{
	id itemList = [_controller selectedItems];
	
	[itemList makeObjectsPerform:@selector(setPrivate:) 
		with:(id)[privateSwitch intValue]];
		
	[_controller dirty:self];
	[_controller update];
	return self;
}

- toggleShowPending:sender
{
	NXWriteDefault([NXApp appName], "ShowPending", [sender intValue] == 0 ? "NO" : "YES");
	
	[_controller setShowPending:[sender intValue]];
	[_controller update];
	return self;
}

- toggleShowCompleted:sender
{
	NXWriteDefault([NXApp appName], "ShowCompleted", [sender intValue] == 0 ? "NO" : "YES");
	
	[_controller setShowCompleted:[sender intValue]];
	[_controller update];
	return self;
}

- toggleShowPrivate:sender
{
	NXWriteDefault([NXApp appName], "ShowPrivate", [sender intValue] == 0 ? "NO" : "YES");
	
	[_controller setShowPrivate:[sender intValue]];
	[_controller update];
	return self;
}

- windowDidUpdate:sender
{
	id itemList = [_controller selectedItems];
	
	[sender disableFlushWindow];
	
	if(!itemList || ([itemList count] > 1)) {
		[typeMatrix setEnabled:NO];
		if(!itemList) {
			[statusMatrix setEnabled:NO];
		} else {
			[statusMatrix setEnabled:YES];
		}
		[createdText setStringValue:""];
		[completedText setStringValue:""];
		[priorityText setStringValue:""];
	} else {
		ToDoItem *item = [itemList lastObject];
		
		[typeMatrix setEnabled:YES];
		[typeMatrix selectCellWithTag:[item type]];
		[statusMatrix setEnabled:YES];
		[completedSwitch setIntValue:[item isCompleted]];
		[privateSwitch setIntValue:[item isPrivate]];
		[createdText setStringValue:[item asciiStartDate]];
		if([item isCompleted]) {
			[completedText setStringValue:[item asciiCompletedDate]];
		} else {
			[completedText setStringValue:""];
		}
		[priorityText setIntValue:[item priority]];
	}
	
	[[sender reenableFlushWindow] flushWindow];
	
	return self;
}

@end

@implementation ToDoInspector(Private)

- _setController:anObject
{
	_controller = anObject;
	return self;
}

- _controller
{
	return _controller;
}

@end
