2

How to expand TreeViewer programmatically?

If I use underlying Tree.setExpanded(true),

    action1 = new Action() {
        public void run() {
            viewer.getTree().getItems()[0].setExpanded(true);
        }
    };

element does not appear:

enter image description here

If I use mouse clicks, branch expands ok.

Baz
  • 36,440
  • 11
  • 68
  • 94
Dims
  • 47,675
  • 117
  • 331
  • 600
  • I guess that `setExpdanded` is just a state and not an action. You could have a method that expand all the children elements. – Marc-Andre Feb 14 '14 at 15:02

1 Answers1

4

Have a look at AbsractTreeViewer#expandToLevel(Object, int):

Expands all ancestors of the given element or tree path so that the given element becomes visible in this viewer's tree control, and then expands the subtree rooted at the given element to the given level.

Baz
  • 36,440
  • 11
  • 68
  • 94
  • I came to the question while was debugging why `AbsractTreeViewer#expandToLevel(Object, int)` was not working. – Dims Feb 14 '14 at 15:05
  • @Dims Could you elaborate on that? I don't really understand. Please clarify what you mean with _"was not working"_. – Baz Feb 14 '14 at 15:07
  • Ok, finally I am starting to understand what was the problem (in my another qstn) – Dims Feb 14 '14 at 15:17
  • No, still not. If some nodes are identical (by means of equals/hashCode), I still don't know how to expand special node... – Dims Feb 14 '14 at 18:31
  • @Dims I'm assuming that making them unique is not an option? – Baz Feb 14 '14 at 18:39
  • Yes. They are lazy and the path is generated elsewhere. I can't know `identityHashCode` of them. Even I can't find required node by enumeration, because there are no enumeration methods. – Dims Feb 14 '14 at 19:18
  • @Dims Even if you could enumerate, how would you differentiate between two "equal" nodes? – Baz Feb 14 '14 at 19:24
  • By path. Paths are unique. But now I am seeing that it is working. Some things I was understanding incorrectly, and some things are seem to work different in API. I seems to me that some API methods use comparer, added to the viewer, and others do ignore comparer while using equal method defined in model element. Also I probably had some bugs in my code. Finally I got it working. – Dims Feb 16 '14 at 13:11