From e6497aafda5ecb92e1fe18f5e23999c2babec296 Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Thu, 16 Sep 2010 01:00:12 +0000 Subject: - Consolidate structure and content row editing cancellation code into a single function in each class - Tweak NSTableView subclasses to catch presses of the Escape key and ask the class to abort row editing. This makes Esc key behaviour much more consistent, and allows cancelling/reverting edits made via the mouse (eg checkboxes, dropdowns, enums), or after the cell editing has finished but the row is still selected. - Remove debug - Update localisable strings --- Source/SPCopyTable.m | 7 ++++ Source/SPTableContent.m | 90 ++++++++++++++++++----------------------------- Source/SPTableStructure.h | 1 + Source/SPTableStructure.m | 47 ++++++++++++------------- Source/SPTableView.m | 8 ++++- 5 files changed, 73 insertions(+), 80 deletions(-) (limited to 'Source') diff --git a/Source/SPCopyTable.m b/Source/SPCopyTable.m index 962557f1..faf1b231 100644 --- a/Source/SPCopyTable.m +++ b/Source/SPCopyTable.m @@ -739,6 +739,13 @@ NSInteger MENU_EDIT_COPY_AS_SQL = 2003; return; } + // Check if ESCAPE is hit and use it to cancel row editing if supported + else if ([theEvent keyCode] == 53 && [[self delegate] respondsToSelector:@selector(cancelRowEditing)]) + { + if ([[self delegate] cancelRowEditing]) return; + } + + [super keyDown:theEvent]; } diff --git a/Source/SPTableContent.m b/Source/SPTableContent.m index 79afe0a8..99e34af1 100644 --- a/Source/SPTableContent.m +++ b/Source/SPTableContent.m @@ -1623,14 +1623,10 @@ if ( [contextInfo isEqualToString:@"removeallrows"] ) { if ( returnCode == NSAlertDefaultReturn ) { - //check if the user is currently editing a row - if (isEditingRow) { - //cancel the edit - isEditingRow = NO; - // in case the delete fails, make sure we at least stay in a somewhat consistent state - [tableValues replaceRowAtIndex:currentlyEditingRow withRowContents:oldRow]; - currentlyEditingRow = -1; - } + + // Check if the user is currently editing a row, and revert to ensure a somewhat + // consistent state if deletion fails. + if (isEditingRow) [self cancelRowEditing]; [mySQLConnection queryString:[NSString stringWithFormat:@"DELETE FROM %@", [selectedTable backtickQuotedString]]]; if ( ![mySQLConnection queryErrored] ) { @@ -1668,31 +1664,17 @@ if ([selectedRows count]!=1) { NSLog(@"Expected only one selected row, but found %d",[selectedRows count]); } - // this code is pretty much taken from the escape key handler + + // Always cancel the edit; if the user is currently editing a new row, we can just discard it; + // if editing an old row, restore it to the original to ensure consistent state if deletion fails. + // If editing a new row, deselect the row and return - as no table reload is required. if ( isEditingNewRow ) { - // since the user is currently editing a new row, we don't actually have to delete any rows from the database - // we just have to remove the row from the view (and the store) - isEditingRow = NO; - isEditingNewRow = NO; - tableRowsCount--; - [tableValues removeRowAtIndex:currentlyEditingRow]; - currentlyEditingRow = -1; - [self updateCountText]; - [tableContentView reloadData]; - - //deselect the row + [self cancelRowEditing]; // Resets isEditingNewRow! [tableContentView selectRowIndexes:[NSIndexSet indexSet] byExtendingSelection:NO]; - - // we also don't have to reload the table, since no query went to the database return; } else { - //cancel the edit - isEditingRow = NO; - // in case the delete fails, make sure we at least stay in a somewhat consistent state - [tableValues replaceRowAtIndex:currentlyEditingRow withRowContents:oldRow]; - currentlyEditingRow = -1; + [self cancelRowEditing]; } - } [tableContentView selectRowIndexes:[NSIndexSet indexSet] byExtendingSelection:NO]; @@ -2430,16 +2412,7 @@ // Discard changes selected } else { - if ( !isEditingNewRow ) { - [tableValues replaceRowAtIndex:currentlyEditingRow withRowContents:oldRow]; - isEditingRow = NO; - } else { - tableRowsCount--; - [tableValues removeRowAtIndex:currentlyEditingRow]; - isEditingRow = NO; - isEditingNewRow = NO; - } - currentlyEditingRow = -1; + [self cancelRowEditing]; } [tableContentView reloadData]; } @@ -2476,6 +2449,29 @@ return NO; } +/** + * Cancel active row editing, replacing the previous row if there was one + * and resetting state. + * Returns whether row editing was cancelled. + */ +- (BOOL)cancelRowEditing +{ + if (!isEditingRow) return NO; + if (isEditingNewRow) { + tableRowsCount--; + [tableValues removeRowAtIndex:currentlyEditingRow]; + [self updateCountText]; + isEditingNewRow = NO; + } else { + [tableValues replaceRowAtIndex:currentlyEditingRow withRowContents:oldRow]; + } + isEditingRow = NO; + currentlyEditingRow = -1; + [tableContentView reloadData]; + [tableContentView makeFirstResponder]; + return YES; +} + /** * Returns the WHERE argument to identify a row. * If "row" is -2, it uses the oldRow. @@ -3295,7 +3291,6 @@ } - // Catch editing events in the row and if the row isn't currently being edited, // start an edit. This allows edits including enum changes to save correctly. if ( !isEditingRow ) { @@ -3835,22 +3830,7 @@ // Abort editing [control abortEditing]; - if ( isEditingRow && !isEditingNewRow ) { - isEditingRow = NO; - [tableValues replaceRowAtIndex:row withRowContents:oldRow]; - } else if ( isEditingNewRow ) { - isEditingRow = NO; - isEditingNewRow = NO; - tableRowsCount--; - [tableValues removeRowAtIndex:row]; - [self updateCountText]; - [tableContentView reloadData]; - } - currentlyEditingRow = -1; - - // Preserve the focus - [tableContentView makeFirstResponder]; - + [self cancelRowEditing]; return TRUE; } diff --git a/Source/SPTableStructure.h b/Source/SPTableStructure.h index 8a1fa32a..d5229153 100644 --- a/Source/SPTableStructure.h +++ b/Source/SPTableStructure.h @@ -85,6 +85,7 @@ - (IBAction)removeField:(id)sender; - (IBAction)resetAutoIncrement:(id)sender; - (IBAction)showOptimizedFieldType:(id)sender; +- (BOOL)cancelRowEditing; // Index sheet methods - (IBAction)closeSheet:(id)sender; diff --git a/Source/SPTableStructure.m b/Source/SPTableStructure.m index c6e4d7f7..4d9a496f 100644 --- a/Source/SPTableStructure.m +++ b/Source/SPTableStructure.m @@ -609,6 +609,27 @@ } } +/** + * Cancel active row editing, replacing the previous row if there was one + * and resetting state. + * Returns whether row editing was cancelled. + */ +- (BOOL)cancelRowEditing +{ + if (!isEditingRow) return NO; + if (isEditingNewRow) { + isEditingNewRow = NO; + [tableFields removeObjectAtIndex:currentlyEditingRow]; + } else { + [tableFields replaceObjectAtIndex:currentlyEditingRow withObject:[NSMutableDictionary dictionaryWithDictionary:oldRow]]; + } + isEditingRow = NO; + [tableSourceView reloadData]; + currentlyEditingRow = -1; + [tableSourceView makeFirstResponder]; + return YES; +} + #pragma mark - #pragma mark Index sheet methods @@ -1065,18 +1086,7 @@ closes the keySheet // Discard changes and cancel editing else { - if (!isEditingNewRow) { - [tableFields replaceObjectAtIndex:currentlyEditingRow - withObject:[NSMutableDictionary dictionaryWithDictionary:oldRow]]; - isEditingRow = NO; - } - else { - [tableFields removeObjectAtIndex:currentlyEditingRow]; - isEditingRow = NO; - isEditingNewRow = NO; - } - - currentlyEditingRow = -1; + [self cancelRowEditing]; } [tableSourceView reloadData]; @@ -1706,18 +1716,7 @@ would result in a position change. else if ( [[control window] methodForSelector:command] == [[control window] methodForSelector:@selector(cancelOperation:)] ) { [control abortEditing]; - if ( isEditingRow && !isEditingNewRow ) { - isEditingRow = NO; - [tableFields replaceObjectAtIndex:row withObject:[NSMutableDictionary dictionaryWithDictionary:oldRow]]; - } else if ( isEditingNewRow ) { - isEditingRow = NO; - isEditingNewRow = NO; - [tableFields removeObjectAtIndex:row]; - [tableSourceView reloadData]; - } - currentlyEditingRow = -1; - [tableSourceView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO]; - [tableSourceView makeFirstResponder]; + [self cancelRowEditing]; return YES; } else { return NO; diff --git a/Source/SPTableView.m b/Source/SPTableView.m index c3f76baf..aa0b5067 100644 --- a/Source/SPTableView.m +++ b/Source/SPTableView.m @@ -89,7 +89,7 @@ { // Check if ENTER or RETURN is hit and edit the column. - if([self numberOfSelectedRows] == 1 && ([theEvent keyCode] == 36 || [theEvent keyCode] == 76)) + if ([self numberOfSelectedRows] == 1 && ([theEvent keyCode] == 36 || [theEvent keyCode] == 76)) { if([[[[self delegate] class] description] isEqualToString:@"SPFieldMapperController"]) { @@ -126,6 +126,12 @@ } } + + // Check if ESCAPE is hit and use it to cancel row editing if supported + else if ([theEvent keyCode] == 53 && [[self delegate] respondsToSelector:@selector(cancelRowEditing)]) + { + if ([[self delegate] cancelRowEditing]) return; + } [super keyDown:theEvent]; -- cgit v1.2.3