From 0b4ee446f7052f964f704d8dfabe53103733325b Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Sat, 27 Mar 2010 15:34:31 +0000 Subject: Add the ability to sort the connection favorites table view in the preferences. Sort options are by name, host or type as well as the option to be sorted in reverse order. This implements issue #490. --- Interfaces/English.lproj/MainMenu.xib | 293 +++++++++++++++++++++++++++++++++- Resources/PreferenceDefaults.plist | 8 + Source/SPConstants.h | 10 ++ Source/SPConstants.m | 2 + Source/SPPreferenceController.h | 21 ++- Source/SPPreferenceController.m | 95 +++++++++++ Source/TableDocument.m | 6 + 7 files changed, 422 insertions(+), 13 deletions(-) diff --git a/Interfaces/English.lproj/MainMenu.xib b/Interfaces/English.lproj/MainMenu.xib index 0dcd0af4..a51aa337 100644 --- a/Interfaces/English.lproj/MainMenu.xib +++ b/Interfaces/English.lproj/MainMenu.xib @@ -12,7 +12,7 @@ YES - + YES @@ -1209,6 +1209,73 @@ + + + Favorites + + 2147483647 + + + submenuAction: + + Favorites + + YES + + + Sort By Name + + 2147483647 + + + + + + Sort By Host + + 2147483647 + + + + + + Sort By Type + + 2147483647 + + + + + + YES + YES + + + 2147483647 + + + + + + Reverse Sort Order + + 2147483647 + + + + + + + + + YES + YES + + + 2147483647 + + + Hide Toolbar @@ -2950,6 +3017,38 @@ 1051 + + + sortFavorites: + + + + 1066 + + + + sortFavorites: + + + + 1067 + + + + reverseFavoritesSortOrder: + + + + 1068 + + + + sortFavorites: + + + + 1070 + @@ -4274,6 +4373,8 @@ + + @@ -4357,6 +4458,58 @@ + + 1058 + + + YES + + + + + + 1059 + + + YES + + + + + + + + + + 1061 + + + + + 1062 + + + + + 1063 + + + + + 1064 + + + + + 1065 + + + + + 1069 + + + @@ -4385,10 +4538,19 @@ 1047.IBPluginDependency 1048.IBPluginDependency 1050.IBPluginDependency + 1058.IBPluginDependency + 1059.IBEditorWindowLastContentRect + 1059.IBPluginDependency 106.IBEditorWindowLastContentRect 106.IBPluginDependency 106.ImportedFromIB2 106.editorWindowContentRectSynchronizationRect + 1061.IBPluginDependency + 1062.IBPluginDependency + 1063.IBPluginDependency + 1064.IBPluginDependency + 1065.IBPluginDependency + 1069.IBPluginDependency 111.IBPluginDependency 111.ImportedFromIB2 129.IBPluginDependency @@ -4753,11 +4915,20 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + {{765, 481}, {180, 93}} + com.apple.InterfaceBuilder.CocoaPlugin {{824, 199}, {204, 153}} com.apple.InterfaceBuilder.CocoaPlugin {{561, 605}, {181, 43}} com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -4889,7 +5060,7 @@ com.apple.InterfaceBuilder.CocoaPlugin - {{514, 531}, {251, 293}} + {{514, 501}, {251, 323}} com.apple.InterfaceBuilder.CocoaPlugin {{698, 703}, {184, 133}} @@ -5163,7 +5334,7 @@ - 1057 + 1070 @@ -5346,7 +5517,7 @@ NSObject - + IBProjectSource Source/SPQueryFavoriteManager.h @@ -5527,6 +5698,118 @@ + + SPPreferenceController + NSWindowController + + YES + + YES + addFavorite: + changeFont: + displayAutoUpdatePreferences: + displayEditorPreferences: + displayFavoritePreferences: + displayGeneralPreferences: + displayNetworkPreferences: + displayNotificationPreferences: + displayTablePreferences: + duplicateFavorite: + favoriteTypeDidChange: + removeFavorite: + reverseFavoritesSortOrder: + setDefaultColors: + showCustomQueryFontPanel: + showGlobalResultTableFontPanel: + sortFavorites: + updateDefaultFavorite: + + + YES + id + id + id + id + id + id + id + id + id + id + id + id + id + id + id + id + id + id + + + + YES + + YES + autoUpdateView + defaultFavoritePopup + editorFontName + editorView + favoriteHostTextField + favoriteHostTextFieldSSH + favoriteNameTextField + favoriteUserTextField + favoriteUserTextFieldSSH + favoriteUserTextFieldSocket + favoritesController + favoritesTabView + favoritesTableView + favoritesView + generalView + globalResultTableFontName + networkView + notificationsView + preferencesWindow + socketPasswordField + sshPasswordField + sshSQLPasswordField + standardPasswordField + tableCell + tablesView + + + YES + NSView + NSPopUpButton + NSTextField + NSView + NSTextField + NSTextField + NSTextField + NSTextField + NSTextField + NSTextField + NSArrayController + NSTabView + NSTableView + NSView + NSView + NSTextField + NSView + NSView + NSWindow + NSSecureTextField + NSSecureTextField + NSSecureTextField + NSSecureTextField + id + NSView + + + + IBProjectSource + Source/SPPreferenceController.h + + SPProcessListController NSWindowController @@ -5696,7 +5979,7 @@ BWAnchoredButtonBar - + SPServerVariablesController diff --git a/Resources/PreferenceDefaults.plist b/Resources/PreferenceDefaults.plist index 281afa3a..1e71ad22 100644 --- a/Resources/PreferenceDefaults.plist +++ b/Resources/PreferenceDefaults.plist @@ -153,5 +153,13 @@ PrintWarningRowLimit 1000 + ProcessListShowProcessID + + ProcessListShowFullProcessList + + FavoritesSortedBy + 0 + FavoritesSortedInReverse + diff --git a/Source/SPConstants.h b/Source/SPConstants.h index 54bce4d1..c849c3ba 100644 --- a/Source/SPConstants.h +++ b/Source/SPConstants.h @@ -93,6 +93,14 @@ typedef enum SPSSHPasswordNone = 2 } SPSSHTunnelPasswordMode; +// Sort by constants +typedef enum +{ + SPFavoritesSortNameItem = 0, + SPFavoritesSortHostItem = 1, + SPFavoritesSortTypeItem = 2 +} SPFavoritesSortItem; + // Long running notification time for Growl messages extern const CGFloat SPLongRunningNotificationTime; @@ -200,6 +208,8 @@ extern NSString *SPTableInformationPanelCollapsed; extern NSString *SPTableColumnWidths; extern NSString *SPProcessListShowProcessID; extern NSString *SPProcessListShowFullProcessList; +extern NSString *SPFavoritesSortedBy; +extern NSString *SPFavoritesSortedInReverse; // Hidden Prefs extern NSString *SPPrintWarningRowLimit; diff --git a/Source/SPConstants.m b/Source/SPConstants.m index f0df9dac..8a2729e8 100644 --- a/Source/SPConstants.m +++ b/Source/SPConstants.m @@ -130,6 +130,8 @@ NSString *SPTableInformationPanelCollapsed = @"TableInformationPanelCollap NSString *SPTableColumnWidths = @"tableColumnWidths"; NSString *SPProcessListShowProcessID = @"ProcessListShowProcessID"; NSString *SPProcessListShowFullProcessList = @"ProcessListShowFullProcessList"; +NSString *SPFavoritesSortedBy = @"FavoritesSortedBy"; +NSString *SPFavoritesSortedInReverse = @"FavoritesSortedInReverse"; // Hidden Prefs NSString *SPPrintWarningRowLimit = @"PrintWarningRowLimit"; diff --git a/Source/SPPreferenceController.h b/Source/SPPreferenceController.h index e8e7a50d..029993d9 100644 --- a/Source/SPPreferenceController.h +++ b/Source/SPPreferenceController.h @@ -25,6 +25,8 @@ #import +#import "SPConstants.h" + @class SPKeychain; @interface SPPreferenceController : NSWindowController @@ -57,15 +59,13 @@ IBOutlet NSTextField *favoriteHostTextFieldSSH; IBOutlet id tableCell; - - SPKeychain *keychain; - NSDictionary *currentFavorite; - + IBOutlet NSTextField *editorFontName; IBOutlet NSTextField *globalResultTableFontName; - NSInteger fontChangeTarget; - + SPKeychain *keychain; + NSDictionary *currentFavorite; + NSToolbar *toolbar; NSToolbarItem *generalItem; @@ -80,7 +80,10 @@ NSUserDefaults *prefs; BOOL favoriteNameFieldWasTouched; - NSInteger favoriteType; + NSInteger favoriteType, fontChangeTarget; + + BOOL reverseFavoritesSort; + SPFavoritesSortItem previousSortItem, currentSortItem; } - (void)applyRevisionChanges; @@ -89,10 +92,12 @@ - (IBAction)addFavorite:(id)sender; - (IBAction)removeFavorite:(id)sender; - (IBAction)duplicateFavorite:(id)sender; - - (IBAction)updateDefaultFavorite:(id)sender; +- (IBAction)updateDefaultFavorite:(id)sender; - (IBAction)showCustomQueryFontPanel:(id)sender; - (IBAction)showGlobalResultTableFontPanel:(id)sender; - (IBAction)setDefaultColors:(id)sender; +- (IBAction)sortFavorites:(id)sender; +- (IBAction)reverseFavoritesSortOrder:(id)sender; // Toolbar item IBAction methods - (IBAction)displayGeneralPreferences:(id)sender; diff --git a/Source/SPPreferenceController.m b/Source/SPPreferenceController.m index 33cc9e81..760a48b8 100644 --- a/Source/SPPreferenceController.m +++ b/Source/SPPreferenceController.m @@ -34,6 +34,7 @@ @interface SPPreferenceController (PrivateAPI) - (void)_setupToolbar; +- (void)_sortFavorites; - (void)_resizeWindowForContentView:(NSView *)view; @end @@ -57,6 +58,9 @@ favoriteNameFieldWasTouched = YES; favoriteType = 0; fontChangeTarget = 0; + reverseFavoritesSort = NO; + + previousSortItem = SPFavoritesSortNameItem; } return self; @@ -71,6 +75,10 @@ keychain = [[SPKeychain alloc] init]; + // Set sort items + currentSortItem = [prefs integerForKey:SPFavoritesSortedBy]; + reverseFavoritesSort = [prefs boolForKey:SPFavoritesSortedInReverse]; + [tableCell setImage:[NSImage imageNamed:@"database"]]; // Replace column's NSTextFieldCell with custom SWProfileTextFieldCell @@ -87,6 +95,8 @@ [self updateDefaultFavoritePopup]; [prefs synchronize]; + + [self _sortFavorites]; } #pragma mark - @@ -425,6 +435,38 @@ } } +/** + * Sorts the favorites table view based on the selected sort by item + */ +- (IBAction)sortFavorites:(id)sender +{ + previousSortItem = currentSortItem; + currentSortItem = [[sender menu] indexOfItem:sender]; + + [prefs setInteger:currentSortItem forKey:SPFavoritesSortedBy]; + + // Perform sorting + [self _sortFavorites]; + + [[[sender menu] itemAtIndex:previousSortItem] setState:NSOffState]; + [[[sender menu] itemAtIndex:currentSortItem] setState:NSOnState]; +} + +/** + * Reverses the favorites table view sorting based on the selected criteria + */ +- (IBAction)reverseFavoritesSortOrder:(id)sender +{ + reverseFavoritesSort = (![sender state]); + + [prefs setBool:reverseFavoritesSort forKey:SPFavoritesSortedInReverse]; + + // Perform re-sorting + [self _sortFavorites]; + + [sender setState:reverseFavoritesSort]; +} + #pragma mark - #pragma mark Toolbar item IBAction methods @@ -1113,6 +1155,22 @@ return ([favoritesTableView numberOfSelectedRows] > 0); } + if ((action == @selector(sortFavorites:)) || (action == @selector(reverseFavoritesSortOrder:))) { + + // Loop all the items in the sort by menu only checking the currently selected one + for (NSMenuItem *item in [[menuItem menu] itemArray]) + { + [item setState:([[menuItem menu] indexOfItem:item] == currentSortItem) ? NSOnState : NSOffState]; + } + + // Check or uncheck the reverse sort item + if (action == @selector(reverseFavoritesSortOrder:)) { + [menuItem setState:reverseFavoritesSort]; + } + + return [[toolbar selectedItemIdentifier] isEqualToString:SPPreferenceToolbarFavorites]; + } + return YES; } @@ -1219,6 +1277,43 @@ [self displayGeneralPreferences:nil]; } +/** + * Sorts the connection favorites based on the selected criteria. + */ +- (void)_sortFavorites +{ + NSString *sortKey = @""; + + switch (currentSortItem) + { + case SPFavoritesSortNameItem: + sortKey = @"name"; + break; + case SPFavoritesSortHostItem: + sortKey = @"host"; + break; + case SPFavoritesSortTypeItem: + sortKey = @"type"; + break; + default: + sortKey = @"name"; + break; + } + + NSSortDescriptor *sortDescriptor = nil; + + if (currentSortItem == SPFavoritesSortTypeItem) { + sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:sortKey ascending:reverseFavoritesSort] autorelease]; + } + else { + sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:sortKey ascending:reverseFavoritesSort selector:@selector(caseInsensitiveCompare:)] autorelease]; + } + + [favoritesController setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; + + [favoritesTableView reloadData]; +} + // ------------------------------------------------------------------------------- // _resizeWindowForContentView: // diff --git a/Source/TableDocument.m b/Source/TableDocument.m index d73fa943..2a95c158 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -3148,6 +3148,12 @@ return ([[tablesListInstance valueForKeyPath:@"tables"] count] > 1); } + + // If validation for the sort favorites tableview items reaches here then the preferences window isn't + // open return NO. + if (([menuItem action] == @selector(sortFavorites:)) || ([menuItem action] == @selector(reverseFavoritesSortOrder:))) { + return NO; + } return [super validateMenuItem:menuItem]; } -- cgit v1.2.3