2

what is the best way to build a binding compatible outline view datasouce in cappuccino ? i.e. a kind of CPTreeController

my source is currently a jSON object (containing objects and arrays) and I would like to display it into an outline view as well as being able to change its parameters / get notified for the changes. (Once loaded into the CPTreeController, I do not need to serialize it back to jSON, I will work directly with the datasource)

Then:

  • Is there a hidden CPTreeController somewhere or a similar lib ready to use ?
  • If I rewrite my own datasource, should I write it all from scratch or could I easily mix CPDictionaries and CPArrays to achieve this task ? (keeping in mind it should be bindings compliant)
Flavien Volken
  • 19,196
  • 12
  • 100
  • 133

1 Answers1

1

A search through sources says there is no hidden CPTreeController so you can either write your own implementation of CPTreeController and contribute it to community or you can implement data source protocol for a specific model, something like this:

- (int)outlineView:(CPOutlineView)theOutlineView numberOfChildrenOfItem:(id)theItem
{
    if (theItem == nil)
        theItem = rootNode;

    return [[theItem childNodes] count];
}

- (id)outlineView:(CPOutlineView)theOutlineView child:(int)theIndex ofItem:(id)theItem
{
    if (theItem == nil)
        theItem = rootNode;

    return [[theItem childNodes] objectAtIndex:theIndex];
}

- (BOOL)outlineView:(CPOutlineView)theOutlineView isItemExpandable:(id)theItem
{
    if (theItem == nil)
        theItem = rootNode;

    return [[theItem childNodes] count] > 0;
}

- (id)outlineView:(CPOutlineView)anOutlineView objectValueForTableColumn:(CPTableColumn)theColumn byItem:(id)theItem
{
    return [[theItem representedObject] valueForKey:"name"];
}
Valerii Hiora
  • 1,542
  • 13
  • 8
  • I wonder if there wouldn't be something to do with a [CPTree](http://cappuccino.org/learn/documentation/interface_c_p_tree_node.html) too. Which object would then be responsible for the key bindings ? my datasource or the objects I would add in it ? – Flavien Volken Dec 06 '11 at 14:32
  • 1
    `CPTreeNode` helps a lot as you don't have to reimplement tree data structure. In the sample above `rootNode` and `theItem` meant to be `CPTreeNode`s. As where is no explicit bindings it is up to you which object will be responsible for key bindings, but for me it seems that `CPTreeNode` also separates tree structure from the actual data so it is better to bind to added objects, like `[[theItem representedObject] valueForKey:"name"]`. – Valerii Hiora Dec 15 '11 at 08:53