//
//  OxPathController.m
//  AdventureEngine
//
//  Created by Nicholas Matsakis on 1/30/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "OxPathController.h"

@implementation OxPathController

@synthesize canCreateDirectories;
@synthesize canChooseFiles;	
@synthesize canChooseDirectories;
@synthesize allowsMultipleSelection;
@synthesize directory;
@synthesize fileTypes;
@synthesize file;
@synthesize files;	

+ (void) initialize
{
	[self exposeBinding:@"canCreateDirectories"];
	[self exposeBinding:@"canChooseFiles"];
	[self exposeBinding:@"canChooseDirectories"];
	[self exposeBinding:@"allowsMultipleSelection"];
	[self exposeBinding:@"directory"];
	[self exposeBinding:@"fileTypes"];
	[self exposeBinding:@"file"];
	[self exposeBinding:@"files"];
}

- init
{
	if ((self = [super init])) {
		self.canCreateDirectories = NO;
		self.canChooseFiles = YES;
		self.canChooseDirectories = NO;
		self.allowsMultipleSelection = NO;
		self.directory = nil;
		self.fileTypes = nil;
		self.file = @"";
		self.files = [[NSArray array] retain];
	}
	return self;
}

- initWithCoder:(NSCoder *)coder
{
	if ((self = [super initWithCoder:coder])) {
		self.canCreateDirectories = [coder decodeBoolForKey:@"OxCanCreateDirectories"];
		self.canChooseFiles = [coder decodeBoolForKey:@"OxCanChooseFiles"];
		self.canChooseDirectories = [coder decodeBoolForKey:@"OxCanChooseDirectories"];
		self.allowsMultipleSelection = [coder decodeBoolForKey:@"OxAllowsMultipleSelection"];
		self.directory = [[coder decodeObjectForKey:@"OxDirectory"] retain];
		self.fileTypes = [[coder decodeObjectForKey:@"OxFileTypes"] retain];
		self.file = [[coder decodeObjectForKey:@"OxFile"] retain];
		self.files = [[coder decodeObjectForKey:@"OxFiles"] retain];
	}
	return self;
}

- (void) dealloc
{
	[m_binder release];
	[directory release];
	[fileTypes release];
	[file release];
	[files release];
	[super dealloc];
}

- (void) encodeWithCoder:(NSCoder *)coder
{
	[coder encodeBool:canCreateDirectories forKey:@"OxCanCreateDirectories"];
	[coder encodeBool:canChooseFiles forKey:@"OxCanChooseFiles"];
	[coder encodeBool:canChooseDirectories forKey:@"OxCanChooseDirectories"];
	[coder encodeBool:allowsMultipleSelection forKey:@"OxAllowsMultipleSelection"];
	[coder encodeObject:directory forKey:@"OxDirectory"];
	[coder encodeObject:fileTypes forKey:@"OxFileTypes"];
	[coder encodeObject:file forKey:@"OxFile"];
	[coder encodeObject:files forKey:@"OxFiles"];
}

- (IBAction) browse:(id)sender
{
	NSOpenPanel *panel = [NSOpenPanel openPanel];
	[panel setCanCreateDirectories:self.canCreateDirectories];
	[panel setCanChooseFiles:self.canChooseFiles];
	[panel setCanChooseDirectories:self.canChooseDirectories];
	[panel setAllowsMultipleSelection:self.allowsMultipleSelection];
	NSInteger res = [panel runModalForDirectory:self.directory file:self.file types:self.fileTypes];
	if (res == NSCancelButton)
		return;
	
	self.files = [panel filenames];
	self.file = [self.files objectAtIndex:0];
}

@end
