From b2a0af91eae18b6490ddeafd2708d256b0d53cb3 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Thu, 17 Mar 2011 19:25:56 +0000 Subject: Bind the default favorites drop down menu pref to the new favorites data file, with sub-menus representing groups (still a bit buggy). --- Source/SPAppController.h | 2 -- Source/SPGeneralPreferencePane.h | 2 -- Source/SPGeneralPreferencePane.m | 68 +++++++++++++++++++++++++++++++++++----- Source/SPTreeNode.m | 14 ++++----- 4 files changed, 68 insertions(+), 18 deletions(-) (limited to 'Source') diff --git a/Source/SPAppController.h b/Source/SPAppController.h index fe80e2d9..5d12be48 100644 --- a/Source/SPAppController.h +++ b/Source/SPAppController.h @@ -30,7 +30,6 @@ @interface SPAppController : NSObject { - IBOutlet NSWindow* bundleEditorWindow; BOOL isNewFavorite; @@ -55,7 +54,6 @@ NSMutableArray *runningActivitiesArray; NSString *lastBundleBlobFilesDirectory; - } @property (readwrite, retain) NSString *lastBundleBlobFilesDirectory; diff --git a/Source/SPGeneralPreferencePane.h b/Source/SPGeneralPreferencePane.h index ab7ef949..7d14492e 100644 --- a/Source/SPGeneralPreferencePane.h +++ b/Source/SPGeneralPreferencePane.h @@ -35,8 +35,6 @@ @interface SPGeneralPreferencePane : SPPreferencePane { IBOutlet NSPopUpButton *defaultFavoritePopup; - - IBOutlet NSArrayController *favoritesController; } - (IBAction)updateDefaultFavorite:(id)sender; diff --git a/Source/SPGeneralPreferencePane.m b/Source/SPGeneralPreferencePane.m index 36cbed9f..ecc3172a 100644 --- a/Source/SPGeneralPreferencePane.m +++ b/Source/SPGeneralPreferencePane.m @@ -24,6 +24,16 @@ // More info at #import "SPGeneralPreferencePane.h" +#import "SPFavoritesController.h" +#import "SPTreeNode.h" +#import "SPFavoriteNode.h" +#import "SPGroupNode.h" + +@interface SPGeneralPreferencePane () + +- (NSMenuItem *)_constructMenuItemForNode:(SPTreeNode *)node; + +@end @implementation SPGeneralPreferencePane @@ -37,7 +47,7 @@ { [prefs setBool:([defaultFavoritePopup indexOfSelectedItem] == 0) forKey:SPSelectLastFavoriteUsed]; - [prefs setInteger:[[sender selectedItem] tag] forKey:SPDefaultFavorite]; + [prefs setInteger:[sender tag] forKey:SPDefaultFavorite]; } #pragma mark - @@ -55,15 +65,13 @@ [[defaultFavoritePopup menu] addItem:[NSMenuItem separatorItem]]; // Add all favorites to the menu - for (NSDictionary *favorite in [favoritesController arrangedObjects]) + for (SPTreeNode *node in [[[[[SPFavoritesController sharedFavoritesController] favoritesTree] childNodes] objectAtIndex:0] childNodes]) { - NSMenuItem *favoriteMenuItem = [[NSMenuItem alloc] initWithTitle:[favorite objectForKey:SPFavoriteNameKey] action:NULL keyEquivalent:@""]; - - [favoriteMenuItem setTag:[[favorite objectForKey:SPFavoriteIDKey] integerValue]]; + NSMenuItem *menuItem = [self _constructMenuItemForNode:node]; - [[defaultFavoritePopup menu] addItem:favoriteMenuItem]; + [[defaultFavoritePopup menu] addItem:menuItem]; - [favoriteMenuItem release]; + [menuItem release]; } // Select the default favorite from prefs @@ -75,6 +83,52 @@ } } +#pragma mark - +#pragma mark Private API + +/** + * Builds a menu item and sub-menu (if required) of the supplied tree node. + * + * @param node The node to build the menu item for + * + * @return The menu item + */ +- (NSMenuItem *)_constructMenuItemForNode:(SPTreeNode *)node +{ + NSMenuItem *menuItem = nil; + + if ([node isGroup]) { + + SPGroupNode *groupNode = (SPGroupNode *)[node representedObject]; + + menuItem = [[NSMenuItem alloc] initWithTitle:[groupNode nodeName] action:NULL keyEquivalent:@""]; + + NSMenu *subMenu = [[NSMenu alloc] initWithTitle:[groupNode nodeName]]; + + for (SPTreeNode *childNode in [node childNodes]) + { + NSMenuItem *innerItem = [self _constructMenuItemForNode:childNode]; + + [subMenu addItem:innerItem]; + + [innerItem release]; + } + + [menuItem setSubmenu:subMenu]; + } + else { + NSDictionary *favorite = [(SPFavoriteNode *)[node representedObject] nodeFavorite]; + + menuItem = [[NSMenuItem alloc] initWithTitle:[favorite objectForKey:SPFavoriteNameKey] action:@selector(updateDefaultFavorite:) keyEquivalent:@""]; + + [menuItem setTag:[[favorite objectForKey:SPFavoriteIDKey] integerValue]]; + + [menuItem setTarget:self]; + } + + return menuItem; +} + #pragma mark - #pragma mark Preference pane protocol methods diff --git a/Source/SPTreeNode.m b/Source/SPTreeNode.m index e197d180..d93db01b 100644 --- a/Source/SPTreeNode.m +++ b/Source/SPTreeNode.m @@ -69,7 +69,7 @@ static NSString *SPTreeNodeIsGroupKey = @"SPTreeNodeIsGroup"; return; } - if (![node isLeaf]) { + if ([node isGroup]) { [node removeObjectFromChildren:object]; } } @@ -88,7 +88,7 @@ static NSString *SPTreeNodeIsGroupKey = @"SPTreeNodeIsGroup"; { [descendants addObject:node]; - if (![node isLeaf]) { + if ([node isGroup]) { [descendants addObjectsFromArray:[node descendants]]; } } @@ -107,7 +107,7 @@ static NSString *SPTreeNodeIsGroupKey = @"SPTreeNodeIsGroup"; for (SPTreeNode *node in [self childNodes]) { - if ([node isLeaf]) { + if (![node isGroup]) { [childLeafs addObject:node]; } } @@ -127,7 +127,7 @@ static NSString *SPTreeNodeIsGroupKey = @"SPTreeNodeIsGroup"; for (SPTreeNode *node in [self childNodes]) { - if ([node isLeaf]) { + if (![node isGroup]) { [childLeafs addObject:node]; } else { @@ -149,7 +149,7 @@ static NSString *SPTreeNodeIsGroupKey = @"SPTreeNodeIsGroup"; for (SPTreeNode *node in [self childNodes]) { - if (![node isLeaf]) { + if ([node isGroup]) { [groupChildren addObject:node]; } } @@ -177,7 +177,7 @@ static NSString *SPTreeNodeIsGroupKey = @"SPTreeNodeIsGroup"; break; } - if (![node isLeaf]) { + if ([node isGroup]) { SPTreeNode *innerNode = [self parentFromArray:[node childNodes]]; if (innerNode) { @@ -205,7 +205,7 @@ static NSString *SPTreeNodeIsGroupKey = @"SPTreeNodeIsGroup"; if (node == self) return YES; // Check all the sub-nodes - if (![node isLeaf]) { + if ([node isGroup]) { if ([self isDescendantOfOrOneOfNodes:[node childNodes]]) { return YES; } -- cgit v1.2.3