Could you try this?
Basically adding filter on selection events. In case of Viewer implementation,
org.eclipse.jface.util.OpenStrategy class is responsible for firing selection events what Viewer's understand. If filter is added on SWT selection event, Viewer will never know about underlying selection.
public class SWTSimpleTree {
Display display = new Display();
Shell shell = new Shell(display);
Tree tree;
private Listener filter = new Listener() {
@Override
public void handleEvent(Event event) {
event.type=SWT.None;
event.doit=false;
event.item = null;
tree.deselectAll();
}
};
public SWTSimpleTree() {
shell.setLayout(new GridLayout());
tree = new Tree(shell, SWT.BORDER);
tree.setLayoutData(new GridData(GridData.FILL_BOTH));
TreeItem item = new TreeItem(tree, SWT.NULL);
item.setText("ITEM");
TreeItem item2 = new TreeItem(item, SWT.NULL);
item2.setText("ITEM2");
TreeItem item3 = new TreeItem(item2, SWT.NULL);
item3.setText("ITEM3");
System.out.println("item: " + item.getParent() + ", " + item.getParentItem());
System.out.println("item2: " + item2.getParent() + ", " + item2.getParentItem());
System.out.println(tree.getItemCount());
System.out.println(tree.getItems().length);
tree.getDisplay().addFilter(SWT.Selection, filter);
tree.getDisplay().addFilter(SWT.DefaultSelection, filter);
shell.setSize(300, 200);
shell.open();
//textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private void init() {
}
public static void main(String[] args) {
new SWTSimpleTree();
}
}