Class SelectionListenerFactory
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
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
A model containing selection values.static class
Static class to hold the predicates for this factory. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic ISelectionListener
createListener
(IWorkbenchPart part, Predicate<SelectionListenerFactory.ISelectionModel> predicate) Create a listener for a part that also acts as the selection listener.static ISelectionListener
createListener
(IWorkbenchPart part, ISelectionListener listener, Predicate<SelectionListenerFactory.ISelectionModel> predicate) Create a listener for a part with a separate selection listener.static ISelectionListener
createVisibleListener
(IWorkbenchPart part, ISelectionListener listener) Convenience method to create a listener that only gets notified when: the selection has changed; the part is visible.static ISelectionListener
createVisibleListener
(IWorkbenchPart part, ISelectionListener listener, Predicate<SelectionListenerFactory.ISelectionModel> additionalPredicate) Convenience method to create a listener that only gets notified when: the selection has changed; the part is visible.static ISelectionListener
createVisibleSelfMutedListener
(IWorkbenchPart part, ISelectionListener listener) Provides a listener that only gets notified of selection events when: the selection was not already delivered from any part; the part is visible; the selection does not originate from the part.static ISelectionListener
createVisibleSelfMutedListener
(IWorkbenchPart part, ISelectionListener listener, Predicate<SelectionListenerFactory.ISelectionModel> additionalPredicate) Provides a listener that only gets notified of selection events when: the selection has changed; the part is visible; the selection does not originate from the part.static ISelectionListener
decorate
(ISelectionListener listener, Predicate<SelectionListenerFactory.ISelectionModel> additionalPredicate) Decorates the passed listener with the passed predicate.
-
Constructor Details
-
SelectionListenerFactory
public SelectionListenerFactory()
-
-
Method Details
-
createListener
public static ISelectionListener createListener(IWorkbenchPart part, Predicate<SelectionListenerFactory.ISelectionModel> predicate) Create a listener for a part that also acts as the selection listener.The listener will be automatically removed when the part is closed.
- Parameters:
part
- the part which must implements theISelectionListener
to be notified. May not be null.predicate
- the predicate must test true before the selection is delivered. May not be null.- Returns:
- the listener. Never null.
-
createListener
public static ISelectionListener createListener(IWorkbenchPart part, ISelectionListener listener, Predicate<SelectionListenerFactory.ISelectionModel> predicate) Create a listener for a part with a separate selection listener.The listener will be automatically removed when the part is closed.
- Parameters:
part
- the part. May not be null.listener
- the selection listener to be notified. May not be null. It can be the part itself if it implementsISelectionChangedListener
.predicate
- the predicate that must test true before the selection is delivered. May not be null.- Returns:
- the listener
-
createVisibleListener
public static ISelectionListener createVisibleListener(IWorkbenchPart part, ISelectionListener listener) Convenience method to create a listener that only gets notified when:- the selection has changed;
- the part is visible.
The listener will be automatically removed when the part is closed.
- Parameters:
part
- the part. May not be null.listener
- the selection listener to be notified. May not be null. It can be the part itself if it implementsISelectionChangedListener
.- Returns:
- the listener. Never null.
- See Also:
-
createVisibleListener
public static ISelectionListener createVisibleListener(IWorkbenchPart part, ISelectionListener listener, Predicate<SelectionListenerFactory.ISelectionModel> additionalPredicate) Convenience method to create a listener that only gets notified when:- the selection has changed;
- the part is visible.
The listener will be automatically removed when the part is closed.
- Parameters:
part
- the part. May not be null.listener
- the selection listener to be notified. May not be null. It can be the part itself if it implementsISelectionChangedListener
.additionalPredicate
- the additional predicate which must test true before the selection is delivered. May not be null.- Returns:
- the listener, never null.
- See Also:
-
createVisibleSelfMutedListener
public static ISelectionListener createVisibleSelfMutedListener(IWorkbenchPart part, ISelectionListener listener) Provides a listener that only gets notified of selection events when:- the selection was not already delivered from any part;
- the part is visible;
- the selection does not originate from the part.
The listener will be automatically removed when the part is closed.
- Parameters:
part
- the part. May not be null.listener
- the selection listener to be notified. It can be the part itself if it implementsISelectionChangedListener
. May not be null.- Returns:
- the listener, never null.
- See Also:
-
createVisibleSelfMutedListener
public static ISelectionListener createVisibleSelfMutedListener(IWorkbenchPart part, ISelectionListener listener, Predicate<SelectionListenerFactory.ISelectionModel> additionalPredicate) Provides a listener that only gets notified of selection events when:- the selection has changed;
- the part is visible;
- the selection does not originate from the part.
The listener will be automatically removed if the part is closed.
- Parameters:
part
- the part. May not be null.listener
- the selection listener to be notified. May not be null. It can be the part itself if it implementsISelectionChangedListener
.additionalPredicate
- the predicate to and-add on top of the chain that this method already adds. May not be null- Returns:
- the listener, never null.
- See Also:
-
decorate
public static ISelectionListener decorate(ISelectionListener listener, Predicate<SelectionListenerFactory.ISelectionModel> additionalPredicate) Decorates the passed listener with the passed predicate. The listener must be created by this factory otherwise nothing happens.- Parameters:
listener
- the listener that was created by this factory.additionalPredicate
- a predicate to and-add on top of the chain that the listener already has. May not be null.- Returns:
- the passed listener
-