diff options
Diffstat (limited to 'Source')
-rw-r--r-- | Source/TablesList.h | 8 | ||||
-rw-r--r-- | Source/TablesList.m | 78 |
2 files changed, 76 insertions, 10 deletions
diff --git a/Source/TablesList.h b/Source/TablesList.h index 9eb632ab..a1268ee7 100644 --- a/Source/TablesList.h +++ b/Source/TablesList.h @@ -57,9 +57,14 @@ enum sp_table_types IBOutlet id tableNameField; IBOutlet id tableEncodingButton; IBOutlet id addTableButton; + IBOutlet id tableRenameSheet; + IBOutlet id tableRenameField; + IBOutlet id tableRenameText; + IBOutlet id renameTableButton; IBOutlet NSMenuItem *removeTableMenuItem; IBOutlet NSMenuItem *duplicateTableMenuItem; + IBOutlet NSMenuItem *renameTableMenuItem; CMMCPConnection *mySQLConnection; @@ -72,9 +77,10 @@ enum sp_table_types // IBAction methods - (IBAction)updateTables:(id)sender; - (IBAction)addTable:(id)sender; -- (IBAction)closeTableSheet:(id)sender; +- (IBAction)closeSheet:(id)sender; - (IBAction)removeTable:(id)sender; - (IBAction)copyTable:(id)sender; +- (IBAction)renameTable:(id)sender; // copyTableSheet methods - (IBAction)closeCopyTableSheet:(id)sender; diff --git a/Source/TablesList.m b/Source/TablesList.m index ce635c9b..18fd1d9c 100644 --- a/Source/TablesList.m +++ b/Source/TablesList.m @@ -287,9 +287,9 @@ } /** - * Closes the add table sheet and stops the modal session + * Closes the current sheet and stops the modal session */ -- (IBAction)closeTableSheet:(id)sender +- (IBAction)closeSheet:(id)sender { [NSApp stopModalWithCode:[sender tag]]; } @@ -500,6 +500,52 @@ } } +/** + * Renames the currently selected table. + */ +- (IBAction)renameTable:(id)sender +{ + if ((![tableSourceInstance saveRowOnDeselect]) || (![tableContentInstance saveRowOnDeselect]) || (![tableDocumentInstance database])) { + return; + } + + [tableWindow endEditingFor:nil]; + + [tableRenameText setStringValue:[NSString stringWithFormat:@"Rename table %@ to:", [self tableName]]]; + + [NSApp beginSheet:tableRenameSheet + modalForWindow:tableWindow + modalDelegate:self + didEndSelector:nil + contextInfo:nil]; + + NSInteger returnCode = [NSApp runModalForWindow:tableRenameSheet]; + + [NSApp endSheet:tableRenameSheet]; + [tableRenameSheet orderOut:nil]; + + if (!returnCode) { + // Clear table name + [tableRenameField setStringValue:@""]; + + return; + } + + [mySQLConnection queryString:[NSString stringWithFormat:@"RENAME TABLE %@ TO %@", [[self tableName] backtickQuotedString], [[tableRenameField stringValue] backtickQuotedString]]]; + + if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""]) { + NSBeginAlertSheet(NSLocalizedString(@"Unable to rename table", @"rename table error message"), + NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, + [NSString stringWithFormat:NSLocalizedString(@"The table '%@' was unable to be renamed because an error occurred.\n\nMySQL said: %@", @"rename table error informative message"), [self tableName], [mySQLConnection getLastErrorMessage]]); + } + else { + // If there was no error, rename the table in our list and reload the table view's data + [tables replaceObjectAtIndex:[tablesListView selectedRow] withObject:[tableRenameField stringValue]]; + + [tablesListView reloadData]; + } +} + #pragma mark Alert sheet methods /** @@ -622,9 +668,15 @@ */ - (void)controlTextDidChange:(NSNotification *)notification { - if ([notification object] == tableNameField) { + id object = [notification object]; + + if (object == tableNameField) { [addTableButton setEnabled:([[tableNameField stringValue] length] > 0)]; } + + if (object == tableRenameField) { + [renameTableButton setEnabled:([[tableRenameField stringValue] length] > 0)]; + } } #pragma mark Getter methods @@ -1108,17 +1160,25 @@ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem { // popup button below table list - if ([menuItem action] == @selector(copyTable:)) - { - if( [self tableType] == SP_TABLETYPE_FUNC || [self tableType] == SP_TABLETYPE_PROC ) + if ([menuItem action] == @selector(copyTable:)) { + if ([self tableType] == SP_TABLETYPE_FUNC || [self tableType] == SP_TABLETYPE_PROC) return NO; - return [tablesListView numberOfSelectedRows] == 1 && [[self tableName] length] && [tablesListView numberOfSelectedRows] > 0; + + return ([tablesListView numberOfSelectedRows] == 1) && [[self tableName] length] && [tablesListView numberOfSelectedRows] > 0; } - if ([menuItem action] == @selector(removeTable:)) - { + + if ([menuItem action] == @selector(removeTable:) ) { return [tablesListView numberOfSelectedRows] > 0; } + if ([menuItem action] == @selector(renameTable:)) { + if ([self tableType] == SP_TABLETYPE_VIEW) { + return NO; + } + + return ([tablesListView numberOfSelectedRows] == 1) && [[self tableName] length]; + } + return [super validateMenuItem:menuItem]; } |