-1

I have implemented a double click listener on a tree viewer and my example code snippet is below.

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);
    }
}

I have an issue now. I pass the tree viewer's object from different classes which needs double click functionality. So instead of passing tree viewer's object from different classes is there a way I could use event.getSource() which returns the object on which the event occurred? I tried implementing it but in vain. Can someone please suggest on how to proceed? I do not want to use tree viewer's object everywhere because event.getSource() returns the respective tree viewer's object.

UPDATE

Yes I agree to your point but I do not want to pass tree viewer as a parameter. So I have implemented it this way:

public class doubleClickListener implements IDoubleClickListener
{
    public void doubleClick(DoubleClickEvent event) 
    {
        Object obj = event.getSource();
        if (obj instanceof TreeViewer) 
        {
            TreeViewer eventSrc = (TreeViewer) obj;
            ITreeSelection selec = (ITreeSelection) eventSrc.getSelection();
            if (selec != null && !selec.isEmpty() && selec instanceof IStructuredSelection) 
            {
                IStructuredSelection selection = selec;
                Object item = selection.getFirstElement();              
                if (eventSrc.getExpandedState(item)) 
                {
                    eventSrc.collapseToLevel(item, AbstractTreeViewer.ALL_LEVELS);
                } 
                else 
                {
                    eventSrc.expandToLevel(item, 1);
                }
            }
        }
    }
}

Is there an enhancement to my above code? I mean is creating an object of ITreeSelection and checking that object is an instaceof IStructuredSelection the right way to do? If not what could be the right way to check instanceof IStructuredSelection?

flavio.donze
  • 7,432
  • 9
  • 58
  • 91
Sangli
  • 373
  • 4
  • 20

1 Answers1

0

Just pass the tree viewer as a parameter to the double click constructor:

public class DoubleClickListener implements IDoubleClickListener
{
   // The tree viewer to work with
   private final TreeViewer treeViewer;


   public DoubleClickListener(TreeViewer viewer)
   {
     treeViewer = viwer;
   }

   ... no change needed to doubleClick code
}

To use:

treeViewer.addDoubleClickListener(new DoubleClickListener(treeViewer));

Update:

Alternatively the DoubleClickEvent getSource() method will be the TreeViewer (as long as the the listener is installed on a tree viewer!).

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • As I could not answer my question before 8 hours of posting it, I have edited my question and added my code there. Can you please look into it at the earliest? – Sangli Apr 03 '14 at 08:45
  • Ok I will add my answer after 8 hours. Thanks for your help. – Sangli Apr 03 '14 at 09:02
  • Do I need this check selec!=null && !selec.isEmpty() ? If not then if null is passed how will it be handled? – Sangli Apr 03 '14 at 09:04
  • You would have to read the code of the tree viewer to be sure if it every passes in null or an empty selection. – greg-449 Apr 03 '14 at 09:06