Package org.eclipse.ui

# Class SelectionListenerFactory

java.lang.Object
org.eclipse.ui.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

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static interface`
`SelectionListenerFactory.ISelectionModel`
A model containing selection values.
`static class`
`SelectionListenerFactory.Predicates`
Static class to hold the predicates for this factory.

## Constructor Summary

Constructors
Constructor
Description
`SelectionListenerFactory()`

## Method Summary

Modifier and Type
Method
Description
`static 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.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## 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 the `ISelectionListener` 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 implements `ISelectionChangedListener`.
  `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 implements `ISelectionChangedListener`.
Returns:
  the listener. Never null.
See Also:
  - `SelectionListenerFactory.Predicates.alreadyDelivered`
  - `SelectionListenerFactory.Predicates.targetPartVisible`

### 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 implements `ISelectionChangedListener`.
  `additionalPredicate` - the additional predicate which must test true before the selection is delivered. May not be null.
Returns:
  the listener, never null.
See Also:
  - `SelectionListenerFactory.Predicates.alreadyDelivered`
  - `SelectionListenerFactory.Predicates.targetPartVisible`

### 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 implements `ISelectionChangedListener`. May not be null.
Returns:
  the listener, never null.
See Also:
  - `SelectionListenerFactory.Predicates.alreadyDeliveredAnyPart`
  - `SelectionListenerFactory.Predicates.targetPartVisible`
  - `SelectionListenerFactory.Predicates.selfMute`

### 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 implements `ISelectionChangedListener`.
  `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:
  - `createVisibleListener(IWorkbenchPart, ISelectionListener)`
  - `SelectionListenerFactory.Predicates.alreadyDeliveredAnyPart`
  - `SelectionListenerFactory.Predicates.targetPartVisible`
  - `SelectionListenerFactory.Predicates.selfMute`

### 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