Re: NSTreeController - please help, can't see the forest for the tree controller
Re: NSTreeController - please help, can't see the forest for the tree controller
- Subject: Re: NSTreeController - please help, can't see the forest for the tree controller
- From: Scott Ahten <email@hidden>
- Date: Wed, 22 Jun 2005 17:57:02 -0400
On Jun 22, 2005, at 2:11 PM, Keith Blount wrote:
Hmm, having played with this I've made a miniscule
amount of headway, but it's been more headache than
headway mostly... If anyone has any ideas on how to
address the following issues when using
NSTreeController bound to an NSOutlineView, I would be
very grateful:
1) There is no addLeaf: method in NSTreeController, so
how can I set it up so that I have two buttons, an Add
Folder button that initiates child objects with their
leaf key path (an isLeaf BOOL in my model) set to NO,
and an Add Leaf button that initiates child objects
with the leaf key path set to YES?
This depends on how your model object is interpreted as a folder.
If you're using the presence of a Core Data relationship to define if
an model object "is a folder", then you simply call add child on the
Tree Controller. The tree controller will create automatically the
relationship if a node is selected.
If your model object defines it's "is a folder" identity by some
other means, you'll need to create a new managed object, set the
correct property, then add the object it to the Tree Controller.
In my application, I do this with a NSTableView by subclassing
NSArrayController and adding a IB action which creates a new managed
object, creates the relationship based on the current selection, then
calls [self addObject:newObject] to add the object.
- (IBAction)addAndSetObjectProperties:(id)sender {
// Check that we have all the data we need to initalize a
new object
//...
// Create a new object, set it's property and add it to the
array controller
newObject = [NSEntityDescription
insertNewObjectForEntityForName:@"Object"
inManagedObjectContext:[self managedObjectContext]];
[newObject setValue: YES forKey:@"YourProperty"];
[self addObject:newObject];
}
Since you're using NSTreeController instead, you'll most likely need
to add the object using insertObject:atArrangedObjectIndexPath:
instead. Unlike the addChild message, I'm not sure if this method
automatically creates a relationship with the selected node, so you
may need to assign the relationship manually based on the current
selection.
[newObject setValue: selectedParentObject forKey:@"parent"];
2) How do I save the state of the expanded items in my
outline view, given that the outline view's datasource
methods are rendered useless by NSTreeController
sending proxy objects to them? Obviously, I don't want
to save the state of the expanded items into my actual
file - I want exactly the same behaviour I would get
if I could use -outlineView:persistentObjectForItem:
and
-outlineView:itemForPersistentObject:.
If you're trying to read or restore the expanded or unexpanded state
of an outline view's nodes, you can walk each node in the outline
view, get the corresponding persistent object using observedObject,
then query or set the nodes state by calling the appropriate outline
view methods with the proxy object, such as isItemExpanded:item,
collapseItem:item, expandItem:item, etc.
Since NSOutlineView is a subclass of NSTableView, you can use
numberOfRows and itemAtRow:index methods to walk the outline view and
access it's nodes. You can either create an attribute to store this
state in your persistent object or store it externally.
To read expanded state and store it in your persistent object (code
not tested and typed in mail) ....
id proxyNode = [outlineView itemAtRow: x]
id persistentObject = [proxyNode observedObject];
[persistentObject setValue:[proxyNode isItemExpanded]
forKey:@"expanded"];
To restore...
id proxyNode = [outlineView itemAtRow: x]
id persistentObject = [proxyNode observedObject];
if ([persistentObject getValueForKey:@"expanded"]) {
[proxyNode expandItem];
}
You may need to disable undo while setting the expanded property of
many managed objects programmatically.
If saving state externally, each Core Data persistent object has an
built-in read-only "objectID" attribute that can be use to uniquely
identify each object. You can use this ID as a key to externally
store the state of each object node in your outline view.
Many thanks,
Keith
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40pixelfreak.net
This email sent to email@hidden
- - -
:: email@hidden
:: http://www.pixelfreak.net
- - -
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden