From fa7cff57548edc51420693e6909fe2adb3c18951 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Wed, 26 Jan 2011 21:44:29 +0000 Subject: Fix and enable drag and drop in the connection favorites outline view. --- Source/SPConnectionController.m | 67 +++++++++++++++++++++++++++++---- Source/SPConnectionControllerDelegate.m | 49 +++++++++++------------- 2 files changed, 80 insertions(+), 36 deletions(-) (limited to 'Source') diff --git a/Source/SPConnectionController.m b/Source/SPConnectionController.m index 2bad361f..c12f1ac4 100644 --- a/Source/SPConnectionController.m +++ b/Source/SPConnectionController.m @@ -46,6 +46,7 @@ static const NSString *SPExportFavoritesFilename = @"SequelProFavorites.plist"; - (BOOL)_checkHost; - (void)_sortFavorites; +- (void)_sortTreeNode:(SPTreeNode *)node usingKey:(NSString *)key; - (void)_favoriteTypeDidChange; - (void)_reloadFavoritesViewData; - (void)_restoreConnectionInterface; @@ -56,6 +57,8 @@ static const NSString *SPExportFavoritesFilename = @"SequelProFavorites.plist"; - (void)_updateFavoritePasswordsFromField:(NSControl *)control; +static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, void *key); + @end @implementation SPConnectionController @@ -1177,19 +1180,67 @@ static const NSString *SPExportFavoritesFilename = @"SequelProFavorites.plist"; sortKey = SPFavoriteTypeKey; break; } + + [self _sortTreeNode:[[favoritesRoot childNodes] objectAtIndex:0] usingKey:sortKey]; + + [favoritesController saveFavorites]; - // First sort the contents of all groups - for (SPTreeNode *node in [[[favoritesRoot childNodes] objectAtIndex:0] groupChildren]) + [self _reloadFavoritesViewData]; +} + +/** + * Sorts the supplied tree node using the supplied sort key. + * + * @param node The tree node to sort + * @param key The sort key to sort by + */ +- (void)_sortTreeNode:(SPTreeNode *)node usingKey:(NSString *)key +{ + NSMutableArray *nodes = [[node mutableChildNodes] mutableCopy]; + + for (SPTreeNode *node in nodes) { - [[node mutableChildNodes] sortUsingSelector:@selector(compare:)]; + if ([node isGroup]) { + [self _sortTreeNode:node usingKey:key]; + } } - // Secondly sort the root's leaf nodes - [[[[favoritesRoot childNodes] objectAtIndex:0] childLeafs] sortUsingSelector:@selector(compare:)]; + NSMutableArray *groupNodes = [[NSMutableArray alloc] init]; - [favoritesController saveFavorites]; - - [self _reloadFavoritesViewData]; + for (node in nodes) + { + if ([node isGroup]) { + [groupNodes addObject:node]; + [nodes removeObject:node]; + } + } + + [nodes sortUsingFunction:compareFavoritesUsingKey context:key]; + + [nodes addObjectsFromArray:groupNodes]; + + [[node mutableChildNodes] setArray:nodes]; + + [groupNodes release]; +} + +/** + * Sort function used by NSMutableArray's sortUsingFunction: + * + * @param favorite1 The first of the favorites to compare (and determine sort order) + * @param favorite2 The second of the favorites to compare + * @param key The sort key to perform the comparison by + * + * @return An integer (NSComparisonResult) indicating the order of the comparison + */ +static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, void *key) +{ + NSString *dictKey = (NSString *)key; + + id value1 = [[(SPFavoriteNode *)[(SPTreeNode *)favorite1 representedObject] nodeFavorite] objectForKey:dictKey]; + id value2 = [[(SPFavoriteNode *)[(SPTreeNode *)favorite2 representedObject] nodeFavorite] objectForKey:dictKey]; + + return [value1 compare:value2]; } /** diff --git a/Source/SPConnectionControllerDelegate.m b/Source/SPConnectionControllerDelegate.m index 449a2cb2..3318958e 100644 --- a/Source/SPConnectionControllerDelegate.m +++ b/Source/SPConnectionControllerDelegate.m @@ -109,7 +109,7 @@ #pragma mark Outline view delegate methods - (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item -{ +{ return ([[(SPTreeNode *)item parentNode] parentNode] == nil); } @@ -160,7 +160,7 @@ - (CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id)item { - return ((SPTreeNode *)[[item parentNode] parentNode] == nil) ? 22 : 17; + return (![[item parentNode] parentNode]) ? 22 : 17; } - (NSString *)outlineView:(NSOutlineView *)outlineView toolTipForCell:(NSCell *)cell rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)tableColumn item:(id)item mouseLocation:(NSPoint)mouseLocation @@ -185,7 +185,7 @@ #pragma mark - #pragma mark Outline view drag & drop -/*- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard +- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard { [pboard declareTypes:[NSArray arrayWithObject:SPFavoritesPasteboardDragType] owner:self]; [pboard setData:[NSData data] forType:SPFavoritesPasteboardDragType]; @@ -197,6 +197,9 @@ { NSDragOperation result = NSDragOperationNone; + // Prevent dropping favorites on other favorites (non-groups) + if ((index == NSOutlineViewDropOnItemIndex) && (![item isGroup])) return result; + if ([info draggingSource] == outlineView) { [outlineView setDropItem:item dropChildIndex:index]; @@ -211,46 +214,39 @@ BOOL acceptedDrop = NO; if ((!item) || ([info draggingSource] != outlineView)) return acceptedDrop; - + SPTreeNode *node = (item) ? item : [[[[favoritesRoot childNodes] objectAtIndex:0] childNodes] objectAtIndex:0]; - - // TODO: Fix me - + // Disable all automatic sorting - //currentSortItem = -1; - //reverseFavoritesSort = NO; - - //[prefs setInteger:currentSortItem forKey:SPFavoritesSortedBy]; - //[prefs setBool:NO forKey:SPFavoritesSortedInReverse]; + currentSortItem = -1; + reverseFavoritesSort = NO; - // Remove sort descriptors - //[favorites sortUsingDescriptors:[NSArray array]]; + [prefs setInteger:currentSortItem forKey:SPFavoritesSortedBy]; + [prefs setBool:NO forKey:SPFavoritesSortedInReverse]; // Uncheck sort by menu items for (NSMenuItem *menuItem in [[favoritesSortByMenuItem submenu] itemArray]) { [menuItem setState:NSOffState]; } - + NSArray *nodes = [self selectedFavoriteNodes]; if ([node isGroup]) { if (index == NSOutlineViewDropOnItemIndex) { index = 0; } - else { - SPTreeNode *oldNode = node; - - node = [node parentNode]; - index = ([[node childNodes] indexOfObject:oldNode] + 1); - } } else { if (index == NSOutlineViewDropOnItemIndex) { index = 0; } } - + + if (![[node representedObject] nodeName]) { + node = [[favoritesRoot childNodes] objectAtIndex:0]; + } + NSMutableArray *childNodeArray = [node mutableChildNodes]; for (SPTreeNode *treeNode in nodes) @@ -270,12 +266,12 @@ else { [[[treeNode parentNode] mutableChildNodes] removeObject:treeNode]; } - + [childNodeArray insertObject:treeNode atIndex:newIndex]; newIndex++; } - + [favoritesController saveFavorites]; [self _reloadFavoritesViewData]; @@ -285,7 +281,7 @@ acceptedDrop = YES; return acceptedDrop; -}*/ +} #pragma mark - #pragma mark Textfield delegate methods @@ -466,9 +462,6 @@ if ((action == @selector(sortFavorites:)) || (action == @selector(reverseSortFavorites:))) { - // TODO: Fix me - return NO; - // Loop all the items in the sort by menu only checking the currently selected one for (NSMenuItem *item in [[menuItem menu] itemArray]) { -- cgit v1.2.3