diff options
author | stuconnolly <stuart02@gmail.com> | 2010-03-27 15:34:31 +0000 |
---|---|---|
committer | stuconnolly <stuart02@gmail.com> | 2010-03-27 15:34:31 +0000 |
commit | 0b4ee446f7052f964f704d8dfabe53103733325b (patch) | |
tree | f09193b597cf3f9771d77a4694d8dd0b5b0a98ed /Source/SPPreferenceController.m | |
parent | 0559e08ff40bc2496c6af22beb4df99cf112e13b (diff) | |
download | sequelpro-0b4ee446f7052f964f704d8dfabe53103733325b.tar.gz sequelpro-0b4ee446f7052f964f704d8dfabe53103733325b.tar.bz2 sequelpro-0b4ee446f7052f964f704d8dfabe53103733325b.zip |
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.
Diffstat (limited to 'Source/SPPreferenceController.m')
-rw-r--r-- | Source/SPPreferenceController.m | 95 |
1 files changed, 95 insertions, 0 deletions
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: // |