Saving DragNDropOutlineView data - SOLVED (was: Data source management in NSOutlineView)
Saving DragNDropOutlineView data - SOLVED (was: Data source management in NSOutlineView)
- Subject: Saving DragNDropOutlineView data - SOLVED (was: Data source management in NSOutlineView)
- From: Jeremy Dronfield <email@hidden>
- Date: Fri, 25 Jun 2004 17:18:42 +0100
On 25 Jun 2004, at 9:57 am, Jeremy Dronfield wrote:
Since posting, I've looked again at the TreeNode class, and I think I
can see a way to use its instance methods to parse it and build my own
convenience method. Haven't tried yet, though...
I've now coded and tested a solution to this, which works.
DragNDropOutlineView now saves its data when quitting and reloads the
saved data at launch. For the benefit of the archive, this is what I've
done. To get the DragNDropOutlineView example to save its data to its
own data resource file (the same file it comes supplied with) only
requires the addition of one method to the AppController class. The
method I've come up with is reasonably clean and efficient:
// On the first run through, the array passed in consists of
// the entire top-level contents of the outline view data
- (NSArray *)arrayFromItems:(NSArray *)items
{
id item;
NSMutableArray *itemsArray = [[NSMutableArray alloc] init];
NSEnumerator *itemEnum = [items objectEnumerator];
while (item = [itemEnum nextObject]) {
if ([NODE_DATA(item) isExpandable]) {
// The item is a group, so we need to get its children
NSMutableDictionary *subDict = [[NSMutableDictionary alloc] init];
// Get all the group's child items and pass the array
// on again
NSArray *children = [SAFENODE(item) children];
NSArray *subArray = [NSArray arrayWithArray:[self
arrayFromItems:children]];
// Place the results in a dictionary and add it to the array
// which is returned by this method
[subDict setObject:[NODE_DATA(item) name] forKey:@"Group"];
[subDict setObject:subArray forKey:@"Entries"];
[itemsArray addObject:subDict];
[subDict release];
} else {
// The item is a leaf (a child item), so just add it to the array
[itemsArray addObject:[NODE_DATA(item) name]];
}
}
return [itemsArray autorelease];
}
The method is called from -windowWillClose: (AppController needs to be
set as the window's delegate first), like so:
- (void)windowWillClose:(NSNotification *)notification
{
// Get all our data source items and pass them to -arrayFromItems:
NSArray *items = [self arrayFromItems:[SAFENODE(treeData) children]];
// Place the resulting array in a dictionary, along with the name
// the example uses for its root object
NSDictionary *saveDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:
items,
@"OVRoot", nil]
forKeys:
[NSArray arrayWithObjects:
@"Entries",
@"Group", nil]];
// Write the dictionary to the resource file in the app bundle, where
it will be
// accessed next time the app launches
[saveDict writeToFile:[[NSBundle mainBundle]
pathForResource:INITIAL_INFODICT ofType: @"dict"] atomically:YES];
}
In spite of the difficulty of getting to grips with it (there have been
moments when I've cursed the name of Chuck Pisula), I reckon
DragNDropOutlineView is one of the best examples (of any kind) Apple
supplies. It could do with a little modernising (it uses one or two
now-deprecated methods), but it comes with just about everything you
could conceivably need from an NSOutlineView.
Now I'm off to begin adapting it for use in my app...
Regards,
-Jeremy
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.