user2090321
user2090321

Reputation: 25

SWT: prevent Tree from expanding by doubleclick?

I have a problem with SWT Tree. My situation is like this: I have a SWT Tree, which contains many TreeItems (Log entries), which contain TreeItems too. Those log entries have really long messages, which could not be shown in the TreeColumns at all. So my idea was: adding a Listener to the tree, which opens a new Dialog by DoubleClick, which shows the entries' details. So far so good. If I do a double click on a item, it works. BUT: If I do a double click on a parent Item, it will expand (and thats good), but my double click Listener is active then as well and the Dialog will open. That's not, what I want.

So, there are two solutions to the problem: 1) prevent the Tree from expanding/collapsing by double click automatically and implement the method by myself or 2) recognize, that the item was expanded and the event has to be aborted.

I do not really know how to do 1 or 2. Do u guys know that?

Thanks in advance.

Upvotes: 2

Views: 1902

Answers (3)

Mike Nakis
Mike Nakis

Reputation: 61969

Other answers did not work for me. This worked:

treeViewer.getControl().addListener(SWT.MeasureItem, new Listener(){
    @Override
    public void handleEvent(Event event) {
    }});

I found this in a discussion in the Eclipse Community Forums: Disabling Treeviewer doubleclick expand/collapse.

When you look at this code you might be tempted to believe that it pretends to the tree control that your tree items have a size of zero, and as a result the tree control fails to detect that the double-click happened within the item, so it does not perform the double-click action. However, this is not what is actually happening. Instead, what this snippet does is that it leverages some weird code in the implementation of the tree control, which checks whether a listener has been added for SWT.MeasureItem, and if so, it deliberately avoids handling a double-click. This piece of code is even prefixed with a lengthy comment which a) fails to make sense and b) does not agree with what the code does. (Whatever.) So, bottom line is that by simply adding a handler for SWT.MeasureItem, and regardless of what the handler does, we are preventing the tree control from handling double-clicks. This is a prime example of Programming by Coincidence1.


1 The term "Programming by coincidence" was coined in the book The Pragmatic Programmer by Andy Hunt and Dave Thomas. It refers to relying on luck and accidental successes rather than programming deliberately.

Upvotes: 2

user2090321
user2090321

Reputation: 25

There is another solution which works much better.

The problem with the solution from 'sambi reddy' was, that the tree was prevented from expanding by doubleclick, but it was prevented from expanding by clickling on the left handside cross as well.

My solution (that works well), was easy: I added a TreeListener, which listens to expanding/collapsing the tree and removed the expanding/collpasing implementation from the MouseDoubleClick-Listener.

No JFace TreeViewer - it works fine.

Upvotes: 0

sambi reddy
sambi reddy

Reputation: 3085

If you are using TreeViewer, you could make use of IOpenListener

treeViewer.addOpenListener(new IOpenListener() {

      @Override
      public void open(OpenEvent event) {
}
}

Upvotes: 1

Related Questions