1

Double-Clicking tree items works completely fine, but when I press CTRL + M on the keyboard then the tree items expand\collapse, can someone please tell me the reason behind this? Is this a bug in Eclipse or why does this double-click functionality get triggered when I press CTRL+M.

Thanks.

Sangli
  • 373
  • 4
  • 20

2 Answers2

7

Use TreeViewer.addDoubleClickListener to listen for tree double clicks not a mouse listener. You could use something like this:

private class DoubleClickListener implements IDoubleClickListener
{
  @Override
  public void doubleClick(final DoubleClickEvent event)
  {
    final IStructuredSelection selection = (IStructuredSelection)event.getSelection();
    if (selection == null || selection.isEmpty())
      return;

    final Object sel = selection.getFirstElement();

    final ITreeContentProvider provider = (ITreeContentProvider)treeViewer.getContentProvider();

    if (!provider.hasChildren(sel))
      return;

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

Update: Using TreeViewer.addDoubleClickListener is the preferred way to do double click handling for all classes derived from StructuredViewer.

Each double click listener is run using SafeRunnable which deals with any exceptions that the listener may throw, this safeguards the rest of the code for errors in the listeners.

The DoubleClickEvent provides direct access to the model object data so it is not necessary to deal with Tree or TreeItem objects to work out selections.

The double click code in the TreeViewer interfaces correctly with the OpenStrategy single / double click to open code.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • How do I associate the listener to my TreeViewer? – Sangli Mar 17 '14 at 11:56
  • treeViewer.addDoubleClickListener(new DoubleClickListener()); – greg-449 Mar 17 '14 at 12:07
  • Hello greg, your code worked absolutely fine and I'm going to use the same but I need to defend myself for using addDoubleClickListener() over addMouseListener(). Can you please tell me the reason behind using addDoubleClickListener() over addMouseListener() and the advantages of using addDoubleClickListener()? – Sangli Mar 17 '14 at 18:29
  • Added a bit more detail – greg-449 Mar 17 '14 at 18:43
  • I'm sorry to say this but I'm relatively new to JAVA. Can you please put it in layman terms? Also I would like to know the disadvantages of using MouseListener for double-clicking. I got a hint of what you are trying to convey as it is purely technical and you are an expert in JAVA. I request you to please explain and clear my doubts. – Sangli Mar 18 '14 at 04:45
  • Basically you could do this with the MouseListener but if you are using a TreeViewer you should use the methods provided by TreeViewer rather than accessing the underlying Tree. – greg-449 Mar 18 '14 at 07:42
  • What is the difference between StructuredViewer and StructuredSelection? – Sangli Mar 19 '14 at 08:56
  • StructuredViewer is the base class for TreeViewer, TableViewer and others. StructuredSelection is a tree or table selection containing zero, one or more selected items. – greg-449 Mar 19 '14 at 10:25
  • Do I have to check for the condition provider.hasChildren(null) as well? If not then can u elaborate the reason please? If I have to check for provider.hasChildren(null) then what is the reason? Please elaborate as I'm new to these concepts. – Sangli Mar 20 '14 at 18:55
  • The code has already checked `selection.isEmpty()` and returned if it was, so the `selection.getFirstElement()` call will not return null. – greg-449 Mar 20 '14 at 18:59
  • http://stackoverflow.com/questions/22634047/writing-junits-test-class-for-a-doubleclicklistener This is the link to my another question. Please look into this question and if possible please respond at the earliest. Sorry if it was the wrong way to imply. – Sangli Mar 25 '14 at 12:08
  • I have implemented the double click listener as per your suggestion. I have a doubt, is there a way I could use event.getSource() which returns an object on which the event initially occured? So that I do not have to use tree Viewer's object explicitly, because I pass the tree viewer's object from different classes which needs the double click function. I do not want to pass the object from other classes. I tried implementing it but I cannot use it on getSelection() and getContentProvider(). Could you please suggest on how to proceed? – Sangli Apr 02 '14 at 19:13
  • I have an issue. Double-click works fine on the tree items but even When I press CTRL + M the tree items expand\collapse but I don't want that to happen, What's the reason behind this CTRL+M functionality? – Sangli May 14 '14 at 10:05
0

I think the following code will be better , cause it will not cause the tree item to reload children and will keep the original state of other tree items.

_treeViewer.addDoubleClickListener( new IDoubleClickListener()
    {
        @Override
        public void doubleClick( DoubleClickEvent event )
        {
            ISelection selection = event.getSelection();

            if( selection instanceof ITreeSelection )
            {
                TreePath[] paths= ((ITreeSelection)selection).getPathsFor(selectedItem);

                for (int i= 0; i < paths.length; i++) 
                {
                    _treeViewer.setExpandedState(paths[i], !_treeViewer.getExpandedState(paths[i]));
                }
                }
            }
        }
    } );
Andy Wu
  • 1
  • 1