#1900 Tree.setExpanded Best Use

yliu Mon 4 Jun 2012

Consider the following code:

#! /usr/bin/env fan

using fwt
using gfx

class ExampleTree
{
  Void main()
  {
    Node[] roots := 
   [
     Node{name="a"; children=[,]},
     Node{name="b"; children=[,]},
     Node{name="c"; children=[,]}
   ]

   Tree t := Tree{model=ExampleTreeModel(roots)}
   Text mod := Text{
     onAction.add |e|{
  roots.last.children.push(Node{name = e.widget->text; children = [,]})
  e.widget->text = ""
  t.refreshAll
  t.setExpanded(roots.last, true)
}
   }

   exampleWrapper := SashPane{
   orientation = Orientation.vertical
weights = [491,40]
t,
mod,
   }
   w := Window
   {
   size = Size(643,572)
content = exampleWrapper
   }
   w.open
  }
}
class Node{
  Str name
  Node[] children
  new make(|This| f)
  {
   f(this)
  }
}

class ExampleTreeModel : TreeModel
{
  List theroots
  new make(Obj?[] theroots)
  {
    this.theroots = theroots
  }
  override Obj[] roots() {
    return theroots
  }
  override Obj[] children(Obj node){
    return node->children
  }
  override Str text(Obj node)
  {
    return node->name
  }
}

From the onAction function for the text widget, I expect that setExpanded would set the last node, "c", to be expanded when given new children, however if you run this script you will notice this is not the case. Tree.refreshAll is working fine so I'm assuming Object referencing is not the issue here. How is setExpanded supposed to be used? or how can I get this behavior from Tree.

andy Tue 5 Jun 2012

Just update the node in question instead of the whole tree:

onAction.add |e| {
  roots.last.children.push(Node { name=e.widget->text; children=[,] })
  e.widget->text = ""
  t.refreshNode(roots.last)
  t.setExpanded(roots.last, true)
}

yliu Tue 5 Jun 2012

Awesome works great. Thanks Andy

Login or Signup to reply.