javajfacetreeviewer

Set selection on collapsed nodes in tree viewer


I've the following problem: I created a tree viewer and bind a data model. The tree is comlete collapsed.
Now I want to select a specific node in the tree:

treeViewer.setSelection(new StructuredSelection(person), true);

Person is one of my custom objects in the data model. The node will be found and selected if the tree is expanded.
Because the node is a child of another node (3. level), nothing happens if the tree is collapsed.
Is it possible to select/focus the node, expand the parent items etc.?
I know I can recursively walk over all nodes and try to find the correct one but is there a method which do this work for me? Or maybe there's a different call of setSelection which let's me expand the tree path?


Solution

  • You can use TreeSelection for this. This takes aTreePath as an argument - which lists all the nodes in the path.

    TreePath path = new TreePath(... array of nodes from root to person ...);
    
    treeViewer.setSelection(new TreeSelection(path), true);