简体   繁体   中英

SWT double-click expansion

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 ?

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!).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM