Dalewn
Dalewn

Reputation: 13

SWT TreeViewer expand and collaps at the same time when SWT.FULL_SELECTION is set

I have implemented a TreeViewer as followed and added a double click listener to it. This double click listener is supposed to expand/collapse a node.

treeViewer = new TreeViewer(component, SWT.BORDER | SWT.MULTI );
treeViewer.addDoubleClickListener(new IDoubleClickListener() {
            public void doubleClick(DoubleClickEvent e) {
                final IStructuredSelection selection = (IStructuredSelection) e
                        .getSelection();
                if (selection == null || selection.isEmpty())
                    return;

                final Object sel = selection.getFirstElement();
                TreeNode selNode = (TreeNode) sel;

                if (treeViewer.getExpandedState(sel)) {
                    treeViewer.collapseToLevel(sel, 1);
                } else {
                    treeViewer.expandToLevel(sel, 1);
                }
            }
        });

Now if I change the TreeViewer to be FULL_SELECTION like...

treeViewer = new TreeViewer(component, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);

this behavoiur changes to something weird.

When double clicked on the node where the lable is displayed, everything works as previous. BUT when clicked on the row, a little further away from the lable the node get's expanded/collapsed both at the same time, making no change at all.

Debugging it I found, that before the double click event I receive there is another event fired (or sth is listening to it before I get to deal with it), expanding/collapsing the specific node. Disabling the listener I was able to get rid of that behaviour. But that again disabled the expansion of the node when double clicked on the lable.

I have seen similar behavior in eclipse while debugging and wondered whether this is a know bug or even worse intended behaviour?

Any suggestions are appreciated and thanks in advance!

Upvotes: 1

Views: 1418

Answers (1)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 20985

I can observe the issue that you describe on Windows 7. This something that expands/collapses an item is most likely native code.

While I think this is a bug in SWT or the Win32 tree control that should be reported and fixed or worked around in SWT, I found a workaround that works on first sight. If you use an SWT listener on the Tree widget instead of the IDoubleClikListener you can determine if the label itself was clicked or just the row and expand/collapse the item yourself or leave it to that something.

treeViewer.getTree().addListener( SWT.DefaultSelection, new Listener() {
  @Override
  public void handleEvent( Event event ) {
    TreeItem treeItem = ( TreeItem )event.item;
    Point cursorLocation = event.display.getCursorLocation();
    Point localCursorLocation = treeItem.getParent().toControl( cursorLocation );
    if( treeItem.getBounds().contains( localCursorLocation ) ) {
      // put your JFace expand/collapse code here
    }
  }
} );

Since this issue is likely platform dependant you should test on all targeted platforms.

Upvotes: 1

Related Questions