Question

When creating a JFace TreeViewer, one can use the SWT constants to set single or multiple selections allowed for the user to the tree items (SWT.SINGLE or SWT.MULTI).

Can anyone help me please as I want a tree that its items can not be selected at all? I want it as for preview purpose and user should not be able to select an item there.

Many thanks in advance,

Était-ce utile?

La solution

I don't really understand why do you want to suppress the selection, but this is how it may work:

viewer.addSelectionChangedListener(new ISelectionChangedListener() {
    @Override
    public void selectionChanged(final SelectionChangedEvent event) {
        if (!event.getSelection().isEmpty()) {
             v.setSelection(StructuredSelection.EMPTY);
        }
    }
});

In addition to this I would make sure that the viewer:

  1. is not registered as the selection provider in the part's site
  2. accepts no other ISelectionChangedListeners

Autres conseils

Another easy solution is to put the viewer into a separate composite and set the enabled state of it to false:

Composite c = new Composite(parent, SWT.NONE);
TreeViewer viewer = new TreeViewer(c, SWT.Border);
c.setEnabled(false);

Your viewer will now NOT appear grey (setting viewer.setEnabled will make it appear grey) but the user is unable to select anything in the viewer. Updating and refreshing the viewer works just fine. But keep in mind that no SelectionEvents will be thrown when clicking into the viewer now.

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();
  }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top