#2112 fwt::Tree.setExpanded

SlimerDude Sun 17 Mar 2013

On and off, this has been driving me nuts for months - I can't get fwt::Tree.setExpanded to work.

I know it was a pain to use in SWT so I'm wondering if it's me who's doing something blatantly wrong, or if maybe it's the Tree implementation.

Here's a simple example:

using gfx
using fwt

class Bannana : Window {
  Tree tree := Tree()

  new make() : super(null) {
    size = Size(320, 240)
    content = tree
    tree.model = BannanaModel()

    onOpen.add |->| {
      tree.show("tea")
      tree.setExpanded("tea", true)
      tree.repaint
      Env.cur.err.printLine("tea is expanded : " + tree.isExpanded("tea"))
    }
  }

  static Void main(Str[] args := [,]) {
    Bannana().open
  }
}

class BannanaModel : TreeModel {
  override Obj[] roots() {
    return ["dog"]
  }

  override Str text(Obj node) {
    (Str)(node as Str)
  }

  override Image? image(Obj node) {
    null
  }

  override Obj[] children(Obj node) {
    (node == "dog") ? ["tea", "coffee"] : [,]
  }
}

SlimerDude Sun 17 Mar 2013

Got it! Me so happy!

setExpanded doesn't expanded any parent nodes, so you need to work your way from the root to your leaf, expanding each node on the way.

So given a tree of:

+ dog
  + tea
    +coffee 
override Obj[] children(Obj node) {
  if (node == "dog")
    return ["tea"]
  if (node == "tea")
    return ["coffee"]
  return [,]
}

to show coffee it's:

tree.setExpanded("dog", true)
tree.setExpanded("tea", true)

Playing with tiny examples definitely helps!

Login or Signup to reply.