From a1c2b661639babea1d54ce71dec1a9cd91263bda Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Sun, 4 Oct 2009 16:35:48 +0000 Subject: Add the ability to copy the selected server variable(s), either as name = value pairs, just the variable or just the variable value. --- Source/TableDocument.h | 5 ++- Source/TableDocument.m | 120 ++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 107 insertions(+), 18 deletions(-) (limited to 'Source') diff --git a/Source/TableDocument.h b/Source/TableDocument.h index 87daf69a..7ef7616d 100644 --- a/Source/TableDocument.h +++ b/Source/TableDocument.h @@ -148,7 +148,6 @@ enum sp_current_query_mode - (NSString *)getHTMLforPrint; -- (IBAction)showUserManager:(id)sender; - (void)initQueryEditorWithString:(NSString *)query; - (void)initWithConnectionFile:(NSString *)path; // Connection callback and methods @@ -208,6 +207,10 @@ enum sp_current_query_mode - (BOOL)saveDocumentWithFilePath:(NSString *)fileName inBackground:(BOOL)saveInBackground onlyPreferences:(BOOL)saveOnlyPreferences; - (IBAction)closePasswordSheet:(id)sender; - (IBAction)backForwardInHistory:(id)sender; +- (IBAction)copy:(id)sender; +- (IBAction)copyServerVariableName:(id)sender; +- (IBAction)copyServerVariableValue:(id)sender; +- (IBAction)showUserManager:(id)sender; // Getter methods - (NSString *)name; diff --git a/Source/TableDocument.m b/Source/TableDocument.m index bd57a7ab..b98d7d51 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -59,6 +59,7 @@ @interface TableDocument (PrivateAPI) - (void)_updateServerVariablesFilterForFilterString:(NSString *)filterString; +- (void)_copyServerVariablesToPasteboardIncludingName:(BOOL)name andValue:(BOOL)value; @end @@ -1800,7 +1801,7 @@ } /** - * + * Closes either the server variables or create syntax sheets. */ - (IBAction)closePanelSheet:(id)sender { @@ -1809,6 +1810,7 @@ // If it was the server variables sheet that was closed release the relevant arrays if necessary if ([sender window] == variablesSheet) { + // If the filtered array is allocated and its not a reference to the variables array get rid of it if ((variablesFiltered) && (variablesFiltered != variables)) { [variablesFiltered release], variablesFiltered = nil; @@ -1818,6 +1820,38 @@ } } +/** + * Copy implementation for server variables table view. + */ +- (IBAction)copy:(id)sender +{ + [self _copyServerVariablesToPasteboardIncludingName:YES andValue:YES]; +} + +/** + * Copies the name(s) of the selected server variables. + */ +- (IBAction)copyServerVariableName:(id)sender +{ + [self _copyServerVariablesToPasteboardIncludingName:YES andValue:NO]; +} + +/** + * Copies the value(s) of the selected server variables. + */ +- (IBAction)copyServerVariableValue:(id)sender +{ + [self _copyServerVariablesToPasteboardIncludingName:NO andValue:YES]; +} + +/** + * Displays the user account manager. + */ +- (IBAction)showUserManager:(id)sender +{ + [userManagerInstance show]; +} + /** * Passes query to tablesListInstance */ @@ -2540,19 +2574,14 @@ } /** - * Menu validation + * Menu item validation. */ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem { if (!_isConnected) { - if ([menuItem action] == @selector(newDocument:) || - [menuItem action] == @selector(terminate:)) - { - return YES; - } else { - return NO; - } + return ([menuItem action] == @selector(newDocument:) || [menuItem action] == @selector(terminate:)); } + if ([menuItem action] == @selector(import:) || [menuItem action] == @selector(export:) || [menuItem action] == @selector(exportMultipleTables:) || @@ -2576,9 +2605,7 @@ return YES; } - - if ([menuItem action] == @selector(exportTable:)) - { + if ([menuItem action] == @selector(exportTable:)) { return ([self database] != nil && [self table] != nil); } @@ -2613,6 +2640,25 @@ return (([[spHistoryControllerInstance history] count]) && (([spHistoryControllerInstance historyPosition] + 1) < [[spHistoryControllerInstance history] count])); } + // Copy selected server variable(s) + if ([menuItem action] == @selector(copy:)) { + return ([variablesTableView numberOfSelectedRows] > 0); + } + + // Copy selected server variable name(s) + if ([menuItem action] == @selector(copyServerVariableName:)) { + [menuItem setTitle:([variablesTableView numberOfSelectedRows] > 1) ? NSLocalizedString(@"Copy Variable Names", @"copy server variable names menu item") : NSLocalizedString(@"Copy Variable Name", @"copy server variable name menu item")]; + + return ([variablesTableView numberOfSelectedRows] > 0); + } + + // Copy selected server variable value(s) + if ([menuItem action] == @selector(copyServerVariableValue:)) { + [menuItem setTitle:([variablesTableView numberOfSelectedRows] > 1) ? NSLocalizedString(@"Copy Variable Values", @"copy server variable values menu item") : NSLocalizedString(@"Copy Variable Value", @"copy server variable value menu item")]; + + return ([variablesTableView numberOfSelectedRows] > 0); + } + return [super validateMenuItem:menuItem]; } @@ -3365,11 +3411,6 @@ if(spfDocData) [spfDocData release]; [super dealloc]; } - -- (void)showUserManager:(id)sender -{ - [userManagerInstance show]; -} @end @@ -3424,4 +3465,49 @@ [saveVariablesButton setTitle:@"Save View As..."]; } +/** + * Copies either the name or value or both (as name = value pairs) of the currently selected server variables. + */ +- (void)_copyServerVariablesToPasteboardIncludingName:(BOOL)name andValue:(BOOL)value +{ + // At least one of either name or value must be true + if ((!name) && (!value)) return; + + NSResponder *firstResponder = [variablesSheet firstResponder]; + + if ((firstResponder == variablesTableView) && ([variablesTableView numberOfSelectedRows] > 0)) { + + NSString *string = @""; + NSIndexSet *rows = [variablesTableView selectedRowIndexes]; + + NSUInteger i = [rows firstIndex]; + + while (i != NSNotFound) + { + if (i < [variablesFiltered count]) { + NSDictionary *variable = NSArrayObjectAtIndex(variablesFiltered, i); + + NSString *variableName = [variable objectForKey:@"Variable_name"]; + NSString *variableValue = [variable objectForKey:@"Value"]; + + // Decide what to include in the string + if (name && value) { + string = [string stringByAppendingFormat:@"%@ = %@\n", variableName, variableValue]; + } + else { + string = [string stringByAppendingFormat:@"%@\n", (name) ? variableName : variableValue]; + } + } + + i = [rows indexGreaterThanIndex:i]; + } + + NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard]; + + // Copy the string to the pasteboard + [pasteBoard declareTypes:[NSArray arrayWithObjects:NSStringPboardType, nil] owner:nil]; + [pasteBoard setString:string forType:NSStringPboardType]; + } +} + @end -- cgit v1.2.3