Search code examples
cappuccino

CPTreeController (Cappuccino)


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)

Solution

  • 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"];
    }