I've implemented a selection listener for my treeviewer to expand or collapse a node on selection. This impementation works fine for collapsing, but does not expand a node.
this.getTree().addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
TreeItem treeItem = (TreeItem) event.item;
if (treeItem.getItems().length > 0) {
if (MyTreeViewer.this.getExpandedState(treeItem)) {
MyTreeViewer.this.collapseToLevel(treeItem, MyTreeViewer.this.ALL_LEVELS);
} else {
MyTreeViewer.this.expandToLevel(treeItem, 1);
}
MyTreeViewer.this.refresh();
}
}
});
Do you have any suggestions how to fix this?
For a JFace TreeViewer you should use a
ISelectionChangedListener
or aIDoubleClickListener
- do not use the underlying Tree listeners as they may not interact correctly with the viewer.This is what I use for double click:
The key thing here is to use the selection as the argument to
collapseToLevel
/expandToLevel
.Just change to implement
ISelectionChangedListener
to work on selection.Add the listener with TreeViewer
addDoubleClickListener
oraddSelectionChangedListener