2002-04-14 14:02:56 +00:00
# import "NovaWindowController.h"
# import "BoomWindowController.h"
# import "CharWindowController.h"
# import "ColrWindowController.h"
# import "CronWindowController.h"
# import "DescWindowController.h"
@ implementation NovaWindowController
2002-04-29 00:05:34 +00:00
- ( id ) initWithResource : ( id < ResKnifeResourceProtocol > ) newResource
2002-04-14 14:02:56 +00:00
{
2002-04-27 18:17:47 +00:00
id oldSelf = self ;
NSData * classData = [ [ ( id < ResKnifeResourceProtocol > ) newResource type ] dataUsingEncoding : NSASCIIStringEncoding allowLossyConversion : YES ] ;
NSString * className = [ [ [ NSString stringWithCString : [ classData bytes ] length : [ classData length ] ] capitalizedString ] stringByAppendingString : @ "WindowController" ] ;
2002-10-04 19:54:05 +00:00
if ( [ className isEqualToString : @ "Yea(R)WindowController" ] ) className = @ "YearWindowController" ; // lossy conversion turns <EFBFBD> into ( R ) , so i have to special - case ؑ <EFBFBD> <EFBFBD>
2002-04-27 18:17:47 +00:00
self = [ [ NSClassFromString ( className ) alloc ] initWithResource : newResource ] ;
[ oldSelf release ] ;
2002-04-14 14:02:56 +00:00
if ( ! self ) return nil ;
// do global stuff here
2002-04-29 00:05:34 +00:00
resource = [ ( id ) newResource retain ] ;
undoManager = [ [ NSUndoManager alloc ] init ] ;
2002-10-04 19:54:05 +00:00
// localCenter = [ [ NSNotificationCenter alloc ] init ] ;
plugBundle = [ NSBundle bundleForClass : [ self class ] ] ;
// plugBundle = [ NSBundle bundleWithIdentifier : @ "au.com.sutherland-studios.resknife.novatools" ] ;
2002-04-14 14:02:56 +00:00
2002-04-30 23:44:23 +00:00
// load the window from the nib file and set it ' s title
[ self window ] ; // implicitly loads nib
if ( [ newResource name ] && ! [ [ newResource name ] isEqualToString : @ "" ] )
[ [ self window ] setTitle : [ NSString stringWithFormat : @ "%@: %@" , [ [ self window ] title ] , [ newResource name ] ] ] ;
2002-04-14 14:02:56 +00:00
return self ;
}
2002-04-29 00:05:34 +00:00
- ( id ) initWithResources : ( id < ResKnifeResourceProtocol > ) newResource , . . .
2002-04-14 14:02:56 +00:00
{
return nil ;
}
2002-04-29 00:05:34 +00:00
- ( void ) dealloc
{
2002-10-04 19:54:05 +00:00
// [ localCenter release ] ;
2002-04-29 00:05:34 +00:00
[ [ NSNotificationCenter defaultCenter ] removeObserver : self ] ;
[ ( id ) resource autorelease ] ;
[ undoManager release ] ;
2002-12-31 19:06:40 +00:00
[ shipDataSource release ] ;
[ soundDataSource release ] ;
2002-04-29 00:05:34 +00:00
[ super dealloc ] ;
}
2002-04-27 18:17:47 +00:00
- ( void ) windowDidLoad
{
[ super windowDidLoad ] ;
2002-04-29 00:05:34 +00:00
// create the data sources ( here because this is called just before they are applied to the combo boxes )
2002-10-04 19:54:05 +00:00
descriptionDataSource = [ [ DataSource alloc ] initForType : [ plugBundle localizedStringForKey : @ "desc" value : @ "" table : @ "Resource Types" ] ] ;
2002-04-29 00:05:34 +00:00
governmentDataSource = [ [ DataSource alloc ] initForType : [ plugBundle localizedStringForKey : @ "govt" value : @ "" table : @ "Resource Types" ] ] ;
2002-10-04 19:54:05 +00:00
pictureDataSource = [ [ DataSource alloc ] initForType : [ plugBundle localizedStringForKey : @ "PICT" value : @ "" table : @ "Resource Types" ] ] ;
2002-04-29 00:05:34 +00:00
planetDataSource = [ [ DataSource alloc ] initForType : [ plugBundle localizedStringForKey : @ "spob" value : @ "" table : @ "Resource Types" ] ] ;
shipDataSource = [ [ DataSource alloc ] initForType : [ plugBundle localizedStringForKey : @ "ship" value : @ "" table : @ "Resource Types" ] ] ;
2002-10-04 19:54:05 +00:00
soundDataSource = [ [ DataSource alloc ] initForType : [ plugBundle localizedStringForKey : @ "snd" value : @ "" table : @ "Resource Types" ] ] ;
spinDataSource = [ [ DataSource alloc ] initForType : [ plugBundle localizedStringForKey : @ "spin" value : @ "" table : @ "Resource Types" ] ] ;
2002-04-27 18:17:47 +00:00
// we don ' t want this notification until we have a window !
2002-04-30 23:44:23 +00:00
[ [ NSNotificationCenter defaultCenter ] addObserver : self selector : @ selector ( resourceNameDidChange : ) name : ResourceNameDidChangeNotification object : resource ] ;
2002-04-27 18:17:47 +00:00
// bug : only registers for notifications on the resource we ' re editing , need dependant resources too ( pass nil for object ? )
// [ [ NSNotificationCenter defaultCenter ] addObserver : self selector : @ selector ( resourceDataDidChange : ) name : ResourceDataDidChangeNotification object : resource ] ;
2002-04-29 00:05:34 +00:00
}
- ( NSUndoManager * ) windowWillReturnUndoManager : ( NSWindow * ) sender
{
return undoManager ;
}
2002-10-04 19:54:05 +00:00
- ( BOOL ) windowShouldClose : ( id ) sender
{
if ( [ [ self window ] isDocumentEdited ] )
{
NSDictionary * errorValues = [ self validateValues ] ;
2002-12-31 19:06:40 +00:00
NSArray * fields = [ errorValues allKeys ] ; // bug : order of items in array is not guaranteed
2002-10-04 19:54:05 +00:00
NSArray * descriptions = [ errorValues allValues ] ;
switch ( [ errorValues count ] )
{
case 0 :
NSBeginAlertSheet ( @ "Do you want to save the changes you made to this resource?" , @ "Save" , @ "Don<6F> t Save" , @ "Cancel" , sender , self , @ selector ( saveSheetDidClose : returnCode : contextInfo : ) , nil , nil , @ "Your changes will be lost if you don't save them." ) ;
break ;
case 1 :
NSBeginAlertSheet ( @ "Invalid values, changes cannot be saved." , @ "Cancel" , @ "Discard Changes" , nil , sender , self , @ selector ( invalidValuesSheetDidClose : returnCode : contextInfo : ) , nil , nil , @ "An invalid value has been given for one of the resource's items. The following field has it's value set incorrectly:\n\n%@: %@" , [ fields objectAtIndex : 0 ] , [ descriptions objectAtIndex : 0 ] ) ;
break ;
case 2 :
NSBeginAlertSheet ( @ "Invalid values, changes cannot be saved." , @ "Cancel" , @ "Discard Changes" , nil , sender , self , @ selector ( invalidValuesSheetDidClose : returnCode : contextInfo : ) , nil , nil , @ "There are invalid values given for a couple of the resource's items. The following fields have their values set incorrectly:\n\n%@: %@\n%@: %@" , [ fields objectAtIndex : 0 ] , [ descriptions objectAtIndex : 0 ] , [ fields objectAtIndex : 1 ] , [ descriptions objectAtIndex : 1 ] ) ;
break ;
case 3 :
NSBeginAlertSheet ( @ "Invalid values, changes cannot be saved." , @ "Cancel" , @ "Discard Changes" , nil , sender , self , @ selector ( invalidValuesSheetDidClose : returnCode : contextInfo : ) , nil , nil , @ "There are invalid values given for three of the resource's items. The following fields have their values set incorrectly:\n\n%@: %@\n%@: %@\n%@: %@" , [ fields objectAtIndex : 0 ] , [ descriptions objectAtIndex : 0 ] , [ fields objectAtIndex : 1 ] , [ descriptions objectAtIndex : 1 ] , [ fields objectAtIndex : 2 ] , [ descriptions objectAtIndex : 2 ] ) ;
break ;
default :
NSBeginAlertSheet ( @ "Invalid values, changes cannot be saved." , @ "Cancel" , @ "Discard Changes" , nil , sender , self , @ selector ( invalidValuesSheetDidClose : returnCode : contextInfo : ) , nil , nil , @ "There are invalid values given for many of the resource's items. The following fields have their values set incorrectly:\n\n%@: %@\n%@: %@\n%@: %@\nplus others." , [ fields objectAtIndex : 0 ] , [ descriptions objectAtIndex : 0 ] , [ fields objectAtIndex : 1 ] , [ descriptions objectAtIndex : 1 ] , [ fields objectAtIndex : 2 ] , [ descriptions objectAtIndex : 2 ] ) ;
break ;
}
return NO ;
}
else return YES ;
}
2002-04-29 00:05:34 +00:00
- ( void ) setResource : ( id < ResKnifeResourceProtocol > ) newResource
{
id old = resource ;
resource = [ ( id ) newResource retain ] ;
[ old release ] ;
}
- ( void ) setUndoManager : ( NSUndoManager * ) newUndoManager
{
id old = undoManager ;
undoManager = [ newUndoManager retain ] ;
[ old release ] ;
2002-04-27 18:17:47 +00:00
}
- ( IBAction ) toggleResID : ( id ) sender
{
// toggles between resource IDs and index numbers
NSLog ( @ "%@" , [ resource type ] ) ;
}
2002-04-30 23:44:23 +00:00
- ( void ) resourceNameDidChange : ( NSNotification * ) notification
{
NSString * prefix ;
NSScanner * scanner = [ NSScanner scannerWithString : [ [ self window ] title ] ] ;
if ( ! [ scanner scanUpToString : @ ":" intoString : & prefix ] )
prefix = [ [ self window ] title ] ;
if ( ! [ [ ( id < ResKnifeResourceProtocol > ) [ notification object ] name ] isEqualToString : @ "" ] )
[ [ self window ] setTitle : [ NSString stringWithFormat : @ "%@: %@" , prefix , [ ( id < ResKnifeResourceProtocol > ) [ notification object ] name ] ] ] ;
else [ [ self window ] setTitle : prefix ] ;
}
2002-10-04 19:54:05 +00:00
- ( void ) saveSheetDidClose : ( NSWindow * ) sheet returnCode : ( int ) returnCode contextInfo : ( void * ) contextInfo
{
switch ( returnCode )
{
case NSAlertDefaultReturn : // save
[ self saveResource ] ;
[ [ self window ] close ] ;
break ;
case NSAlertAlternateReturn : // don ' t save
[ [ self window ] close ] ;
break ;
case NSAlertOtherReturn : // cancel
break ;
}
}
- ( void ) invalidValuesSheetDidClose : ( NSWindow * ) sheet returnCode : ( int ) returnCode contextInfo : ( void * ) contextInfo
{
switch ( returnCode )
{
case NSAlertDefaultReturn : // cancel
break ;
case NSAlertAlternateReturn : // discard changes
[ [ self window ] close ] ;
break ;
}
}
2002-04-14 14:02:56 +00:00
@ end