Class SelectionListenerFactory


  • public class SelectionListenerFactory
    extends Object
    Selection listeners are notified of all selections in the workbench. This means that the listener is always required to filter unwanted selections. In addition, the listener has to make sure not to waste cycles in the UI thread, for instance, not update the UI while it is invisible, but make sure to repaint if becoming visible.

    This filtering generally requires a lot of boilerplate code while, ideally, you only want to receive selections that are of interest.

    This factory takes care of many practical filtering scenarios by allowing the creation of an intermediate selection service that only calls you back with selections you can work with.

    Usage: (assumes the part implements ISelectionListener)

    Only visit the listener if our part is visible:

     getSite().getPage().addSelectionListener(SelectionListenerFactory.createVisibleListener(this, this));
     

    Only visit the listener if our part is visible and the selection did not come from us:

     getSite().getPage().addSelectionListener(SelectionListenerFactory.createVisibleSelfMutedListener(this, this));
     

    Chaining predicates:

     import static org.eclipse.ui.SelectionListenerFactory.Predicates.adaptsTo;
     import static org.eclipse.ui.SelectionListenerFactory.Predicates.selectionPartVisible;
     import static org.eclipse.ui.SelectionListenerFactory.Predicates.selectionSize;
     import static org.eclipse.ui.SelectionListenerFactory.Predicates.selfMute;
     import static org.eclipse.ui.SelectionListenerFactory.Predicates.targetPartVisible;
    
     Predicate<ISelectionModel> predicate = adaptsTo(PlatformObject.class))
                    .and(selectionSize(1))
                    .and(selfMute)
                    .and(selectionPartVisible)
                    .and(targetPartVisible));
    
     getSite().getPage().addSelectionListener(SelectionListenerFactory.createListener(this, predicate));
     

    Creating your own predicate in combination with the visible part predicate:

     Predicate<ISelectionModel> predicate = new Predicate<>() {
    
            public boolean test(ISelectionModel model) {
                    if (model.getCurrentSelectionPart() == SampleView4.this) {
                            return false;
                    }
                    if (!(model.getCurrentSelectionPart() instanceof SampleView)) {
                            return false;
                    }
                    return true;
            }
     };
    
     GetSite().getPage().addSelectionListener(SelectionListenerFactory.createVisibleListener(this, this, predicate));
     
    Since:
    3.117