/* Copyright (C) 2002-2003, Jeffrey D. Argast. The authors make NO WARRANTY or representation, either express or implied, with respect to this software, its quality, accuracy, merchantability, or fitness for a particular purpose. This software is provided "AS IS", and you, its user, assume the entire risk as to its quality and accuracy. Permission is hereby granted to use, copy, modify, and distribute this software or portions thereof for any purpose, without fee, subject to these conditions: (1) If any part of the source code is distributed, then this statement must be included, with this copyright and no-warranty notice unaltered. (2) Permission for use of this software is granted only if the user accepts full responsibility for any undesirable consequences; the authors accept NO LIABILITY for damages of any kind. */ #import "EditConfigurationsController.h" #import "AppDefaults.h" #import "AppUtils.h" #import "PerforceConfigurationsList.h" #import "PerforceConfiguration.h" @implementation EditConfigurationsController - (id) init { self = [super initWithWindowNibName:@"EditConfigurations"]; return self; } - (void) dealloc { [fConfigs release]; [fConfigsForEdit release]; [super dealloc]; } - (void) fillForm { PerforceConfiguration* activeConfig = [fConfigs activeConfiguration]; [fPortField setStringValue:[activeConfig perforcePort]]; [fHostField setStringValue:[activeConfig perforceHost]]; [fUserField setStringValue:[activeConfig perforceUser]]; [fClientField setStringValue:[activeConfig perforceClient]]; [fPasswordField setStringValue:[activeConfig perforcePasswd]]; [fUnicodeServerCheckBox setState: ([activeConfig unicodeServer] ? NSOnState : NSOffState)]; } - (void) fillActiveConfiguration { PerforceConfiguration* activeConfig = [fConfigs activeConfiguration]; [activeConfig setPerforcePort:[fPortField stringValue]]; [activeConfig setPerforceHost:[fHostField stringValue]]; [activeConfig setPerforceUser:[fUserField stringValue]]; [activeConfig setPerforceClient:[fClientField stringValue]]; [activeConfig setPerforcePasswd:[fPasswordField stringValue]]; [activeConfig setUnicodeServer:([fUnicodeServerCheckBox state] == NSOnState ? YES : NO)]; } - (void) updateMainWindowButtons { if ( !fModified ) { [fRevert setEnabled:NO]; [fApply setEnabled:NO]; } else { [fRevert setEnabled:YES]; [fApply setEnabled:YES]; } } - (void) updateNewWindowButtons { NSString* newName = [fNewConfigField stringValue]; int index = [fConfigs indexOfConfigurationNamed:newName]; if ( (index == -1) && ([newName length] > 0) ) { [fNewOK setEnabled:YES]; } else { [fNewOK setEnabled:NO]; } } - (void) updateEditWindowButtons { int selectedRow = [fEditTable selectedRow]; int numConfigs = [fConfigsForEdit count]; if ( selectedRow < 0 ) { [fEditRename setEnabled:NO]; [fEditDelete setEnabled:NO]; [fEditDuplicate setEnabled:NO]; } else if ( numConfigs < 2 ) { [fEditRename setEnabled:YES]; [fEditDelete setEnabled:NO]; [fEditDuplicate setEnabled:YES]; } else { [fEditRename setEnabled:YES]; [fEditDelete setEnabled:YES]; [fEditDuplicate setEnabled:YES]; } } - (void) setModified:(BOOL)modified { BOOL updateButtons = (fModified != modified); fModified = modified; if ( updateButtons ) { [self updateMainWindowButtons]; } } - (void) initializePopup { int activeConfig = [fConfigs indexOfActiveConfiguration]; NSMenu* newMenu = [[[NSMenu alloc] initWithTitle:@""] autorelease]; id newItem = nil; int numItems = [fConfigs count]; int n; for ( n = 0; n < numItems; n++ ) { PerforceConfiguration* config = [fConfigs configurationAtIndex:n]; newItem = [newMenu addItemWithTitle:[config configurationName] action:@selector(switchConfiguration:) keyEquivalent:@""]; [newItem setTarget:self]; } [newMenu addItem:[NSMenuItem separatorItem]]; newItem = [newMenu addItemWithTitle:@"New Configuration..." action:@selector(newConfiguration:) keyEquivalent:@""]; [newItem setTarget:self]; newItem = [newMenu addItemWithTitle:@"Edit Configurations..." action:@selector(editConfigurations:) keyEquivalent:@""]; [newItem setTarget:self]; [fConfigPopup setMenu:newMenu]; [fConfigPopup selectItemAtIndex:activeConfig]; } - (void) windowDidLoad { [self aboutToShowWindow]; CenterWindowInScreen ([self window]); } - (void) aboutToShowWindow { AppDefaults* appDefaults = [AppDefaults defaultAppDefaults]; [fConfigs release]; fConfigs = [[appDefaults getConfigurations] copy]; fModified = NO; [self initializePopup]; [self fillForm]; [self updateMainWindowButtons]; } - (IBAction) chooseRevert:(id)sender { [self aboutToShowWindow]; } - (IBAction) chooseApply:(id)sender { [self fillActiveConfiguration]; AppDefaults* appDefaults = [AppDefaults defaultAppDefaults]; [appDefaults setConfigurations:fConfigs]; [self setModified:NO]; } - (IBAction) switchConfiguration:(NSMenuItem*)sender { PerforceConfiguration* activeConfig = [fConfigs activeConfiguration]; if ( [[sender title] isEqualToString:[activeConfig configurationName]] ) return; [self fillActiveConfiguration]; [fConfigPopup selectItem:sender]; int index = [fConfigPopup indexOfSelectedItem]; [fConfigs setActiveConfiguration:index]; [self fillForm]; [self setModified:YES]; } - (IBAction) newConfiguration:(id)sender { int index = [fConfigs indexOfActiveConfiguration]; [fConfigPopup selectItemAtIndex:index]; [fNewOK setEnabled:NO]; [fNewConfigField setStringValue:@""]; [NSApp beginSheet:fNewConfig modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(newConfigDidEnd:returnCode:contextInfo:) contextInfo:nil]; } - (void) newConfigDidEnd:(NSWindow*)sheet returnCode:(int)returnCode contextInfo:(void*)contextInfo { if ( returnCode == 1 ) { NSString* newConfigName = [fNewConfigField stringValue]; if ( [newConfigName length] > 0 ) { PerforceConfiguration* newConfig = [[[PerforceConfiguration alloc] initWithName:newConfigName perforcePort:@"" perforceHost:@"" perforceUser:@"" perforceClient:@"" perforcePasswd:@"" unicodeServer:NO] autorelease]; [fConfigs addConfiguration:newConfig]; int newLoc = [fConfigs count] - 1; [fConfigPopup insertItemWithTitle:newConfigName atIndex:newLoc]; NSMenuItem* newMenuItem = [fConfigPopup itemAtIndex:newLoc]; [newMenuItem setTarget:self]; [newMenuItem setAction:@selector(switchConfiguration:)]; [self switchConfiguration:newMenuItem]; } } [[self window] makeKeyAndOrderFront:self]; } - (IBAction) newOkay:(id)sender { [fNewConfig orderOut:sender]; [NSApp endSheet:fNewConfig returnCode:1]; } - (IBAction) newCancel:(id)sender { [fNewConfig orderOut:sender]; [NSApp endSheet:fNewConfig returnCode:0]; } - (IBAction) editConfigurations:(id) sender { int index = [fConfigs indexOfActiveConfiguration]; [fConfigPopup selectItemAtIndex:index]; fConfigsForEdit = [fConfigs copy]; [fEditRename setEnabled:YES]; [fEditDuplicate setEnabled:YES]; [fEditDelete setEnabled:NO]; [fEditTable reloadData]; [fEditTable selectRow:0 byExtendingSelection:NO]; [NSApp beginSheet:fEditConfigs modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(editConfigDidEnd:returnCode:contextInfo:) contextInfo:nil]; } - (void) editConfigDidEnd:(NSWindow*)sheet returnCode:(int)returnCode contextInfo:(void*)contextInfo { if ( returnCode == 1 ) { [fConfigs release]; fConfigs = [fConfigsForEdit retain]; [self initializePopup]; [self fillForm]; [self setModified:YES]; } [fConfigsForEdit release]; fConfigsForEdit = nil; [[self window] makeKeyAndOrderFront:self]; } - (IBAction) editDuplicateConfig:(id)sender { int selectedRow = [fEditTable selectedRow]; PerforceConfiguration* newConfig = [[fConfigsForEdit configurationAtIndex:selectedRow] copy]; NSMutableString* newName = [NSMutableString stringWithString:[newConfig configurationName]]; [newName appendString:@" copy"]; int index = 0; while ((index = [fConfigsForEdit indexOfConfigurationNamed:newName]) != -1) { [newName appendString:@" copy"]; } [newConfig setConfigurationName:newName]; [fConfigsForEdit addConfiguration:newConfig]; [fEditTable reloadData]; [self updateEditWindowButtons]; } - (IBAction) editRenameConfig:(id)sender { int selectedRow = [fEditTable selectedRow]; [fEditTable editColumn:0 row:selectedRow withEvent:nil select:YES]; } - (IBAction) editDeleteConfig:(id)sender { int selectedRow = [fEditTable selectedRow]; [fConfigsForEdit removeConfigurationAtIndex:selectedRow]; [fEditTable reloadData]; [self updateEditWindowButtons]; } - (IBAction) editDone:(id)sender { [fEditConfigs orderOut:sender]; [NSApp endSheet:fEditConfigs returnCode:1]; } - (IBAction) editCancel:(id)sender { [fEditConfigs orderOut:sender]; [NSApp endSheet:fEditConfigs returnCode:0]; } - (IBAction) editUnicodeServer:(id)sender { [self setModified:YES]; } // // delegates // - (void)controlTextDidChange:(NSNotification *)aNotification { id anObject = [aNotification object]; if ( anObject == fNewConfigField ) { [self updateNewWindowButtons]; } else { [self setModified:YES]; } } - (void)controlTextDidEndEditing:(NSNotification *)aNotification { id anObject = [aNotification object]; if ( anObject == fEditTable ) { NSDictionary* dict = [aNotification userInfo]; NSText* fieldEditor = [dict objectForKey:@"NSFieldEditor"]; NSString* newName = [fieldEditor string]; if ( [fConfigsForEdit indexOfConfigurationNamed:newName] == -1 ) { int selectedRow = [fEditTable selectedRow]; PerforceConfiguration* config = [fConfigsForEdit configurationAtIndex:selectedRow]; [config setConfigurationName:newName]; } [fEditTable reloadData]; } } - (void)tableViewSelectionDidChange:(NSNotification *)aNotification { [self updateEditWindowButtons]; } - (BOOL)windowShouldClose:(id)sender { if ( fModified ) { NSBeginAlertSheet( @"Apply configurtion changes?", @"Apply", // default button label @"Don't Apply", // alternate button label @"Cancel", // third button [self window], // window sheet is attached to self, // no delegate @selector(alertDidEnd:returnCode:contextInfo:), // did-end selector nil, // no need for did-dismiss selector nil, // context info @"", // additional text nil); // no parameters in message return NO; } return YES; } - (void) alertDidEnd:(NSWindow*)sheet returnCode:(int)returnCode contextInfo:(void*)contextInfo { if ( (returnCode == 0) || (returnCode == 1) ) { if ( returnCode == 1 ) { [self chooseApply:self]; } [self close]; } } // // datasource // - (int)numberOfRowsInTableView:(NSTableView *)tableView { if ( fConfigsForEdit ) { return [fConfigsForEdit count]; } return 0; } - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row { return [[fConfigsForEdit configurationAtIndex:row] configurationName]; } @end