From 32b7c4df28e2eb3a4a8bcb71b3faae4f173f016c Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Sat, 5 Mar 2011 00:14:43 +0000 Subject: Add support for auto saving expanded items. --- Source/SPConnectionController.m | 7 ++- Source/SPConnectionControllerDataSource.h | 37 ++++++++++++ Source/SPConnectionControllerDataSource.m | 93 +++++++++++++++++++++++++++++++ Source/SPConnectionControllerDelegate.h | 2 +- Source/SPConnectionControllerDelegate.m | 56 ------------------- Source/SPFavoriteNode.h | 2 +- Source/SPFavoriteNode.m | 21 +++++++ Source/SPGroupNode.h | 2 +- Source/SPGroupNode.m | 22 ++++++++ Source/SPTreeNode.h | 2 +- Source/SPTreeNode.m | 18 ++++++ 11 files changed, 201 insertions(+), 61 deletions(-) create mode 100644 Source/SPConnectionControllerDataSource.h create mode 100644 Source/SPConnectionControllerDataSource.m (limited to 'Source') diff --git a/Source/SPConnectionController.m b/Source/SPConnectionController.m index 8424b427..49dcb542 100644 --- a/Source/SPConnectionController.m +++ b/Source/SPConnectionController.m @@ -167,9 +167,14 @@ static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, v // Register double click action for the favorites outline view (double click favorite to connect) [favoritesOutlineView setTarget:self]; [favoritesOutlineView setDoubleAction:@selector(nodeDoubleClicked:)]; + + // Register drag types for the favorites outline view [favoritesOutlineView registerForDraggedTypes:[NSArray arrayWithObject:SPFavoritesPasteboardDragType]]; [favoritesOutlineView setDraggingSourceOperationMask:NSDragOperationMove forLocal:YES]; + // Preserve expanded group nodes + [favoritesOutlineView setAutosaveExpandedItems:YES]; + // Registered to be notified of changes to connection information [self addObserver:self forKeyPath:SPFavoriteNameKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; [self addObserver:self forKeyPath:SPFavoriteHostKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; @@ -1283,7 +1288,7 @@ static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, v - (void)_reloadFavoritesViewData { [favoritesOutlineView reloadData]; - [favoritesOutlineView expandItem:[[favoritesRoot childNodes] objectAtIndex:0] expandChildren:YES]; + [favoritesOutlineView expandItem:[[favoritesRoot childNodes] objectAtIndex:0] expandChildren:NO]; [favoritesOutlineView scrollRowToVisible:[favoritesOutlineView selectedRow]]; } diff --git a/Source/SPConnectionControllerDataSource.h b/Source/SPConnectionControllerDataSource.h new file mode 100644 index 00000000..3f3aa7c0 --- /dev/null +++ b/Source/SPConnectionControllerDataSource.h @@ -0,0 +1,37 @@ +// +// $Id$ +// +// SPConnectionControllerDataSource.h +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on February 20, 2011 +// Copyright (c) 2011 Stuart Connolly. All rights reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at + +#import "SPConnectionController.h" + +/** + * @category SPConnectionControllerDelegate SPConnectionControllerDelegate.h + * + * @author Stuart Connolly http://stuconnolly.com/ + * + * Connection controller data source category. + */ +@interface SPConnectionController (SPConnectionControllerDataSource) + +@end diff --git a/Source/SPConnectionControllerDataSource.m b/Source/SPConnectionControllerDataSource.m new file mode 100644 index 00000000..9249d65d --- /dev/null +++ b/Source/SPConnectionControllerDataSource.m @@ -0,0 +1,93 @@ +// +// $Id$ +// +// SPConnectionControllerDataSource.m +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on February 20, 2011 +// Copyright (c) 2011 Stuart Connolly. All rights reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at + +#import "SPConnectionControllerDataSource.h" + +@implementation SPConnectionController (SPConnectionControllerDataSource) + +- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item +{ + SPTreeNode *node = (item == nil ? favoritesRoot : (SPTreeNode *)item); + + return [[node childNodes] count]; +} + +- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item +{ + SPTreeNode *node = (item == nil ? favoritesRoot : (SPTreeNode *)item); + + return NSArrayObjectAtIndex([node childNodes], index); +} + +- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item +{ + return [(SPTreeNode *)item isGroup]; +} + +- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item +{ + SPTreeNode *node = (SPTreeNode *)item; + + return (![node isGroup]) ? [[[node representedObject] nodeFavorite] objectForKey:SPFavoriteNameKey] : [[node representedObject] nodeName]; +} + +- (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item +{ + // Trim whitespace + NSString *newName = [object stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + + if ([newName length]) { + + // Get the node that was renamed + SPTreeNode *node = [self selectedFavoriteNode]; + + if (![node isGroup]) { + // Updating the name triggers a KVO update + [self setName:newName]; + + // Update associated Keychain items + [self _updateFavoritePasswordsFromField:nil]; + } + else { + [[node representedObject] setNodeName:newName]; + + [favoritesController saveFavorites]; + + [self _reloadFavoritesViewData]; + } + } +} + +- (id)outlineView:(NSOutlineView *)outlineView itemForPersistentObject:(id)object +{ + return [NSKeyedUnarchiver unarchiveObjectWithData:object]; +} + +- (id)outlineView:(NSOutlineView *)outlineView persistentObjectForItem:(id)item +{ + return [NSKeyedArchiver archivedDataWithRootObject:item]; +} + +@end diff --git a/Source/SPConnectionControllerDelegate.h b/Source/SPConnectionControllerDelegate.h index 03f13950..4936ee19 100644 --- a/Source/SPConnectionControllerDelegate.h +++ b/Source/SPConnectionControllerDelegate.h @@ -30,7 +30,7 @@ * * @author Stuart Connolly http://stuconnolly.com/ * - * Connection controller delegate/data source category. + * Connection controller delegate category. */ @interface SPConnectionController (SPConnectionControllerDelegate) diff --git a/Source/SPConnectionControllerDelegate.m b/Source/SPConnectionControllerDelegate.m index 3318958e..3aeb2da6 100644 --- a/Source/SPConnectionControllerDelegate.m +++ b/Source/SPConnectionControllerDelegate.m @@ -49,62 +49,6 @@ [databaseConnectionView setPosition:[[[connectionSplitView subviews] objectAtIndex:0] frame].size.width ofDividerAtIndex:0]; } -#pragma mark - -#pragma mark Outline view datasource methods - -- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item -{ - SPTreeNode *node = (item == nil ? favoritesRoot : (SPTreeNode *)item); - - return [[node childNodes] count]; -} - -- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item -{ - SPTreeNode *node = (item == nil ? favoritesRoot : (SPTreeNode *)item); - - return NSArrayObjectAtIndex([node childNodes], index); -} - -- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item -{ - return [(SPTreeNode *)item isGroup]; -} - -- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item -{ - SPTreeNode *node = (SPTreeNode *)item; - - return (![node isGroup]) ? [[[node representedObject] nodeFavorite] objectForKey:SPFavoriteNameKey] : [[node representedObject] nodeName]; -} - -- (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item -{ - // Trim whitespace - NSString *newName = [object stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; - - if ([newName length]) { - - // Get the node that was renamed - SPTreeNode *node = [self selectedFavoriteNode]; - - if (![node isGroup]) { - // Updating the name triggers a KVO update - [self setName:newName]; - - // Update associated Keychain items - [self _updateFavoritePasswordsFromField:nil]; - } - else { - [[node representedObject] setNodeName:newName]; - - [favoritesController saveFavorites]; - - [self _reloadFavoritesViewData]; - } - } -} - #pragma mark - #pragma mark Outline view delegate methods diff --git a/Source/SPFavoriteNode.h b/Source/SPFavoriteNode.h index 5387c137..a77e7dc0 100644 --- a/Source/SPFavoriteNode.h +++ b/Source/SPFavoriteNode.h @@ -30,7 +30,7 @@ * * Tree node the represents a connection favorite. */ -@interface SPFavoriteNode : NSObject +@interface SPFavoriteNode : NSObject { NSDictionary *nodeFavorite; } diff --git a/Source/SPFavoriteNode.m b/Source/SPFavoriteNode.m index 55a44653..ee40f61f 100644 --- a/Source/SPFavoriteNode.m +++ b/Source/SPFavoriteNode.m @@ -25,6 +25,9 @@ #import "SPFavoriteNode.h" +// Constants +static const NSString *SPFavoriteNodeKey = @"SPFavoriteNode"; + @implementation SPFavoriteNode @synthesize nodeFavorite; @@ -67,6 +70,24 @@ return node; } +#pragma mark - +#pragma mark Coding protocol methods + +- (id)initWithCoder:(NSCoder *)coder +{ + if ((self = [super initWithCoder:coder])) { + [self setNodeFavorite:[coder decodeObjectForKey:SPFavoriteNodeKey]]; + } + + return self; +} + +- (void)encodeWithCoder:(NSCoder *)coder +{ + [super encodeWithCoder:coder]; + + [coder encodeObject:[self nodeFavorite] forKey:SPFavoriteNodeKey]; +} #pragma mark - #pragma mark Other diff --git a/Source/SPGroupNode.h b/Source/SPGroupNode.h index e5dad641..8c4f2e84 100644 --- a/Source/SPGroupNode.h +++ b/Source/SPGroupNode.h @@ -30,7 +30,7 @@ * * Tree node that represents a group. */ -@interface SPGroupNode : NSObject +@interface SPGroupNode : NSObject { NSString *nodeName; } diff --git a/Source/SPGroupNode.m b/Source/SPGroupNode.m index 6687b29e..182a96f7 100644 --- a/Source/SPGroupNode.m +++ b/Source/SPGroupNode.m @@ -25,6 +25,9 @@ #import "SPGroupNode.h" +// Constants +static const NSString *SPGroupNodeNameKey = @"SPGroupNodeName"; + @implementation SPGroupNode @synthesize nodeName; @@ -67,6 +70,25 @@ return node; } +#pragma mark - +#pragma mark Coding protocol methods + +- (id)initWithCoder:(NSCoder *)coder +{ + if ((self = [super initWithCoder:coder])) { + [self setNodeName:[coder decodeObjectForKey:SPGroupNodeNameKey]]; + } + + return self; +} + +- (void)encodeWithCoder:(NSCoder *)coder +{ + [super encodeWithCoder:coder]; + + [coder encodeObject:[self nodeName] forKey:SPGroupNodeNameKey]; +} + #pragma mark - #pragma mark Other diff --git a/Source/SPTreeNode.h b/Source/SPTreeNode.h index 62682af8..7e4290b4 100644 --- a/Source/SPTreeNode.h +++ b/Source/SPTreeNode.h @@ -30,7 +30,7 @@ * * NSTreeNode subclass which adds some convenience methods. */ -@interface SPTreeNode : NSTreeNode +@interface SPTreeNode : NSTreeNode { BOOL isGroup; } diff --git a/Source/SPTreeNode.m b/Source/SPTreeNode.m index 7be4f09a..3d5f2ebc 100644 --- a/Source/SPTreeNode.m +++ b/Source/SPTreeNode.m @@ -27,6 +27,9 @@ #import "SPFavoriteNode.h" #import "SPGroupNode.h" +// Constants +static const NSString *SPTreeNodeIsGroupKey = @"SPTreeNodeIsGroup"; + @implementation SPTreeNode @synthesize isGroup; @@ -247,6 +250,21 @@ return dictionary; } +#pragma mark - +#pragma mark Coding protocol methods + +- (id)initWithCoder:(NSCoder *)coder +{ + [self setIsGroup:[[coder decodeObjectForKey:SPTreeNodeIsGroupKey] boolValue]]; + + return self; +} + +- (void)encodeWithCoder:(NSCoder *)coder +{ + [coder encodeObject:[NSNumber numberWithBool:[self isGroup]] forKey:SPTreeNodeIsGroupKey]; +} + #pragma mark - #pragma mark Other -- cgit v1.2.3