From f65e0fe8e01e81ec282347088f4cc8f43e12dc2e Mon Sep 17 00:00:00 2001 From: mltownsend Date: Tue, 1 Jun 2010 22:09:47 +0000 Subject: Added sorting of the favorites in the connection panel. Will use the same sort order as what is set in preferences as well. Fix for Issue#664 --- Interfaces/English.lproj/ConnectionView.xib | 400 +++++++++++++++++++++++++--- Source/SPConnectionController.h | 9 + Source/SPConnectionController.m | 161 ++++++++++- 3 files changed, 536 insertions(+), 34 deletions(-) diff --git a/Interfaces/English.lproj/ConnectionView.xib b/Interfaces/English.lproj/ConnectionView.xib index 6930435e..8a291a5d 100644 --- a/Interfaces/English.lproj/ConnectionView.xib +++ b/Interfaces/English.lproj/ConnectionView.xib @@ -2,10 +2,10 @@ 1050 - 10C540 - 740 - 1038.25 - 458.00 + 10D573 + 762 + 1038.29 + 460.00 YES @@ -15,19 +15,19 @@ YES - 740 - 1.2.2 + 762 + 1.2.5 YES - + YES - com.brandonwalkin.BWToolkit com.apple.InterfaceBuilder.CocoaPlugin + com.brandonwalkin.BWToolkit YES @@ -239,6 +239,135 @@ 25 + + + 268 + {{30, -1}, {32, 24}} + + YES + + -2076049856 + 134350848 + + LucidaGrande + 13 + 16 + + + -2038284033 + 163 + + + 400 + 75 + + + YES + + + 1048576 + 2147483647 + 1 + + NSImage + NSActionTemplate + + + NSImage + NSMenuCheckmark + + + NSImage + NSMenuMixedState + + _popUpItemAction: + + + YES + + OtherViews + + YES + + + + Sort By + + 1048576 + 2147483647 + + + _popUpItemAction: + + + Sort By + + YES + + + Name + + 2147483647 + + + _popUpItemAction: + + + + + Host + + 2147483647 + + + _popUpItemAction: + + + + + Type + + 2147483647 + + + _popUpItemAction: + + + + + YES + YES + + + 2147483647 + + + _popUpItemAction: + + + + + Reverse Sort Order + + 2147483647 + + + _popUpItemAction: + + + + + + + + 1 + YES + 1 + YES + YES + 2 + + {202, 23} @@ -279,7 +408,7 @@ controlColor 3 - MC42NjY2NjY2ODY1AA + MC42NjY2NjY2NjY3AA @@ -1236,7 +1365,7 @@ Error Detail NSPanel - {3.40282e+38, 3.40282e+38} + {1.79769e+308, 1.79769e+308} {100, 100} @@ -1276,9 +1405,9 @@ YES - + 1 - MC45MDE5NjA3OTAyIDAuOTAxOTYwNzkwMiAwLjkwMTk2MDc5MDIAA + MC45MDE5NjA3OSAwLjkwMTk2MDc5IDAuOTAxOTYwNzkAA LucidaGrande @@ -1399,7 +1528,7 @@ YES - + LucidaGrande-Bold 10 @@ -1562,7 +1691,7 @@ {{0, 0}, {1440, 878}} {100, 119} - {3.40282e+38, 3.40282e+38} + {1.79769e+308, 1.79769e+308} @@ -1577,14 +1706,8 @@ Edit Favorite... 2147483647 - - NSImage - NSMenuCheckmark - - - NSImage - NSMenuMixedState - + + @@ -2616,6 +2739,38 @@ 5474 + + + sortFavorites: + + + + 5487 + + + + sortFavorites: + + + + 5488 + + + + sortFavorites: + + + + 5489 + + + + reverseSortFavorites: + + + + 5490 + @@ -2748,6 +2903,7 @@ YES + @@ -3642,6 +3798,86 @@ + + 5475 + + + YES + + + + + + 5476 + + + YES + + + + + + 5477 + + + YES + + + + + + + 5478 + + + + + 5479 + + + YES + + + + + + 5481 + + + YES + + + + + + + + + + 5482 + + + + + 5483 + + + + + 5484 + + + + + 5485 + + + + + 5486 + + + @@ -3821,12 +4057,25 @@ 5468.IBEditorWindowLastContentRect 5468.IBPluginDependency 5469.IBPluginDependency + 5475.IBPluginDependency + 5476.IBPluginDependency + 5477.IBEditorWindowLastContentRect + 5477.IBPluginDependency + 5478.IBPluginDependency + 5479.IBPluginDependency + 5481.IBEditorWindowLastContentRect + 5481.IBPluginDependency + 5482.IBPluginDependency + 5483.IBPluginDependency + 5484.IBPluginDependency + 5485.IBPluginDependency + 5486.IBPluginDependency YES com.apple.InterfaceBuilder.CocoaPlugin - {{73, 173}, {882, 563}} + {{77, 293}, {882, 563}} com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -4168,7 +4417,20 @@ com.brandonwalkin.BWToolkit com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{754, 342}, {150, 23}} + {{639, 342}, {150, 23}} + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.brandonwalkin.BWToolkit + com.brandonwalkin.BWToolkit + {{96, 279}, {97, 37}} + com.brandonwalkin.BWToolkit + com.brandonwalkin.BWToolkit + com.brandonwalkin.BWToolkit + {{193, 206}, {180, 93}} + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -4189,7 +4451,7 @@ - 5474 + 5490 @@ -4218,23 +4480,23 @@ NSObject - + IBProjectSource - Source/CMImageView.h + Source/SPConnectionController.h NSObject - + IBProjectSource - Source/SPConnectionController.h + Source/SPContentFilterManager.h NSObject IBProjectSource - Source/SPContentFilterManager.h + Source/SPImageView.h @@ -4266,7 +4528,7 @@ IBProjectSource - Source/TablesList.h + Source/SPTablesList.h @@ -4328,7 +4590,9 @@ addFavorite: editFavorites: initiateConnection: + reverseSortFavorites: showHelp: + sortFavorites: updateFavoriteSelection: @@ -4338,6 +4602,8 @@ id id id + id + id @@ -4427,6 +4693,22 @@ BWToolkitFramework.framework/Headers/BWAnchoredButtonCell.h + + BWAnchoredPopUpButton + NSPopUpButton + + IBFrameworkSource + BWToolkitFramework.framework/Headers/BWAnchoredPopUpButton.h + + + + BWAnchoredPopUpButtonCell + NSPopUpButtonCell + + IBFrameworkSource + BWToolkitFramework.framework/Headers/BWAnchoredPopUpButtonCell.h + + BWTransparentScrollView NSScrollView @@ -4565,6 +4847,14 @@ AppKit.framework/Headers/NSMenuItem.h + + NSMenuItemCell + NSButtonCell + + IBFrameworkSource + AppKit.framework/Headers/NSMenuItemCell.h + + NSObject @@ -4816,6 +5106,20 @@ Growl.framework/Headers/GrowlApplicationBridge.h + + NSObject + + IBFrameworkSource + PSMTabBar.framework/Headers/PSMTabBarCell.h + + + + NSObject + + IBFrameworkSource + PSMTabBar.framework/Headers/PSMTabBarControl.h + + NSObject @@ -4908,6 +5212,22 @@ AppKit.framework/Headers/NSPanel.h + + NSPopUpButton + NSButton + + IBFrameworkSource + AppKit.framework/Headers/NSPopUpButton.h + + + + NSPopUpButtonCell + NSMenuItemCell + + IBFrameworkSource + AppKit.framework/Headers/NSPopUpButtonCell.h + + NSProgressIndicator NSView @@ -5094,6 +5414,7 @@ 0 + IBCocoaFramework com.apple.InterfaceBuilder.CocoaPlugin.macosx @@ -5109,5 +5430,22 @@ YES ../../sequel-pro.xcodeproj 3 + + YES + + YES + NSActionTemplate + NSMenuCheckmark + NSMenuMixedState + button_edit + + + YES + {10, 10} + {9, 8} + {7, 2} + {32, 23} + + diff --git a/Source/SPConnectionController.h b/Source/SPConnectionController.h index 6a47f9ac..aa45b74d 100644 --- a/Source/SPConnectionController.h +++ b/Source/SPConnectionController.h @@ -29,6 +29,7 @@ #import "SPDatabaseDocument.h" #import "SPKeychain.h" #import "SPSSHTunnel.h" +#import "SPConstants.h" @class BWAnchoredButtonBar; @@ -73,6 +74,7 @@ NSString *sshUser; NSString *sshPassword; NSString *sshPort; +@private NSString *favoritesPBoardType; NSString *connectionKeychainItemName; NSString *connectionKeychainItemAccount; @@ -106,6 +108,10 @@ IBOutlet NSButton *helpButton; IBOutlet NSProgressIndicator *progressIndicator; IBOutlet NSTextField *progressIndicatorText; + + BOOL reverseFavoritesSort; + SPFavoritesSortItem previousSortItem, currentSortItem; + } @property (readwrite, assign) id delegate; @@ -126,6 +132,7 @@ @property (readwrite, retain) NSString *connectionKeychainItemAccount; @property (readwrite, retain) NSString *connectionSSHKeychainItemName; @property (readwrite, retain) NSString *connectionSSHKeychainItemAccount; +@property (readonly, assign) NSString *favoritesPBoardType; - (id)initWithDocument:(SPDatabaseDocument *)theTableDocument; @@ -142,6 +149,8 @@ - (IBAction)editFavorites:(id)sender; - (IBAction)showHelp:(id)sender; - (void)resizeTabViewToConnectionType:(NSUInteger)theType animating:(BOOL)animate; +- (IBAction)sortFavorites:(id)sender; +- (IBAction)reverseSortFavorites:(id)sender; // Connection details interaction - (BOOL)checkHost; diff --git a/Source/SPConnectionController.m b/Source/SPConnectionController.m index 631cb2ed..7e9844e3 100644 --- a/Source/SPConnectionController.m +++ b/Source/SPConnectionController.m @@ -31,6 +31,12 @@ #import "SPConstants.h" #import "SPAlertSheets.h" +@interface SPConnectionController (PrivateAPI) + +- (void)_sortFavorites; + +@end + @implementation SPConnectionController @synthesize delegate; @@ -69,6 +75,7 @@ mySQLConnection = nil; sshTunnel = nil; cancellingConnection = NO; + favoritesPBoardType = @"FavoritesPBoardType"; // Load the connection nib, keeping references to the top-level objects for later release nibObjectsToRelease = [[NSMutableArray alloc] init]; @@ -90,14 +97,19 @@ prefs = [[NSUserDefaults standardUserDefaults] retain]; favorites = nil; [self updateFavorites]; - + // Register an observer for changes within the favorites [prefs addObserver:self forKeyPath:SPFavorites options:NSKeyValueObservingOptionNew context:NULL]; + // Set sort items + currentSortItem = [prefs integerForKey:SPFavoritesSortedBy]; + reverseFavoritesSort = [prefs boolForKey:SPFavoritesSortedInReverse]; + // Register double click for the favorites view (double click favorite to connect) [favoritesTable setTarget:self]; [favoritesTable setDoubleAction:@selector(initiateConnection:)]; - + [favoritesTable registerForDraggedTypes:[NSArray arrayWithObject:favoritesPBoardType]]; + [favoritesTable setDraggingSourceOperationMask:NSDragOperationMove forLocal:YES]; // Set the focus to the favorites table and select the appropriate row [[tableDocument parentWindow] setInitialFirstResponder:favoritesTable]; NSInteger tableRow; @@ -116,7 +128,7 @@ [self resizeTabViewToConnectionType:SPTCPIPConnection animating:NO]; } } - + [self _sortFavorites]; return self; } @@ -655,6 +667,69 @@ #pragma mark - #pragma mark Favorites interaction +- (void)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]; + +} + +- (void)reverseSortFavorites:(id)sender +{ + reverseFavoritesSort = (![sender state]); + + [prefs setBool:reverseFavoritesSort forKey:SPFavoritesSortedInReverse]; + + // Perform re-sorting + [self _sortFavorites]; + + [sender setState:reverseFavoritesSort]; +} + +- (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]; + } + NSDictionary *first = [[favorites objectAtIndex:0] retain]; + [favorites removeObjectAtIndex:0]; + [favorites sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; + [favorites insertObject:first atIndex:0]; + [favoritesTable reloadData]; + [first release]; + +} /** * Updates the local favorites array from the user defaults */ @@ -865,6 +940,62 @@ return [[favorites objectAtIndex:rowIndex] objectForKey:@"name"]; } +- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id < NSDraggingInfo >)info + row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation +{ + BOOL acceptedDrop = NO; + if ((row == 0) || ([info draggingSource] != aTableView)) return acceptedDrop; + NSPasteboard* pboard = [info draggingPasteboard]; + NSData* rowData = [pboard dataForType:favoritesPBoardType]; + NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData]; + NSInteger dragRow = [rowIndexes firstIndex]; + NSInteger defaultConnectionRow = [prefs integerForKey:SPLastFavoriteIndex]; + if (defaultConnectionRow == dragRow) + { + [prefs setInteger:row forKey:SPLastFavoriteIndex]; + } + NSMutableDictionary *draggedFavorite = [favorites objectAtIndex:dragRow]; + [favorites removeObjectAtIndex:dragRow]; + if (row > [favorites count]) + { + row--; + } + [favorites insertObject:draggedFavorite atIndex:row]; + [aTableView reloadData]; + [aTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO]; + // reset the prefs with the new order + NSMutableArray *reorderedFavorites = [[NSMutableArray alloc] initWithArray:favorites]; + [reorderedFavorites removeObjectAtIndex:0]; + [prefs setObject:reorderedFavorites forKey:SPFavorites]; + [[[NSApp delegate] preferenceController] updateDefaultFavoritePopup]; + [reorderedFavorites release]; + [self updateFavorites]; + acceptedDrop = YES; + return acceptedDrop; + +} + +- (BOOL)tableView:(NSTableView *)aTableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes + toPasteboard:(NSPasteboard *)pboard +{ + NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes]; + [pboard declareTypes:[NSArray arrayWithObject:favoritesPBoardType] owner:self]; + [pboard setData:archivedData forType:favoritesPBoardType]; + return YES; +} + +- (NSDragOperation)tableView:(NSTableView *)aTableView validateDrop:(id < NSDraggingInfo >)info + proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)operation +{ + if (row == 0) return NSDragOperationNone; + if ([info draggingSource] == aTableView) + { + [aTableView setDropRow:row dropOperation:NSTableViewDropAbove]; + return NSDragOperationMove; + } + return NSDragOperationNone; +} + /** * Loads a favorite, if any are selected. */ @@ -919,6 +1050,7 @@ [(ImageAndTextCell *)aCell setTextColor:[NSColor grayColor]]; } + #pragma mark - #pragma mark NSSplitView delegate methods @@ -950,6 +1082,27 @@ return (proposedMin + 80); } +#pragma mark - +#pragma mark Menu Validation +-(BOOL)validateMenuItem:(NSMenuItem *)menuItem +{ + SEL action = [menuItem action]; + if ((action == @selector(sortFavorites:)) || (action == @selector(reverseSortFavorites:))) { + + // 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 YES; + +} @end #pragma mark - @@ -965,4 +1118,6 @@ return YES; } + + @end -- cgit v1.2.3