The current selection can be obtained through the selection service with a listener, similar to Eclipse 3.x. However, the ISelectionService
in Eclipse 3.x has been replaced with an almost identical ESelectionService
in Eclipse 4.x. (Other than the minor lack of JavaDoc and change of package name, the only significant difference between the two is that there is no add/removePostSelection
methods.)
Rainbow
in the com.packtpub.e4.application.parts
package. Add a static final
array of strings with colors of the rainbow.create()
method, along with a @PostConstruct
annotation, that takes a Composite
parent. Inside, create a ListViewer
and set the input to the array of rainbow colors. The class will look like:public class Rainbow { private static final Object[] rainbow = { "Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet" }; @PostConstruct public void create(Composite parent) { ListViewer lv = new ListViewer(parent, SWT.NONE); lv.setContentProvider(new ArrayContentProvider()); lv.setInput(rainbow); } }
Application.e4xmi
and go to Application | Windows | Trimmed Window | Controls | Perspective Stack | Perspective | Controls | Part Stash Container | Part Stack. Right-click on the Part Stack and choose Add child followed by Part. Set the label as Rainbow
and find the Rainbow
class with the Find button, or by using the bundleclass
URI bundleclass://com.packtpub.e4.application/com.pactpub.e4.app.parts.Rainbow
.ESelectionService
called selectionService
, along with an @Inject
annotation. In the create()
method, add an anonymous ISelectionChangedListener
to the ListViewer
, such that if a selection event is received, the selection is reflected in the selectionService
. The implementation will look like:@Inject private ESelectionService selectionService; @PostConstruct public void create(Composite parent) { ListViewer lv = new ListViewer(parent, SWT.NONE); lv.setContentProvider(new ArrayContentProvider()); lv.addSelectionChangedListener(new ISelectionChangedListener() { @Override public void selectionChanged(SelectionChangedEvent event) { selectionService.setSelection(event.getSelection()); } }); ... }
ListViewer
, it will send a selection event to the platform's selection service. To determine what objects are selected, the Hello
part can register for changes in the selection, and use that to display the text in the label. Add a setSelection()
method on the Hello
class as follows:@Inject @Optional public void setSelection( @Named(IServiceConstants.ACTIVE_SELECTION) Object selection) { if (selection != null) { label.setText(selection.toString()); } }
[Red]
in the label.The list viewer is similar to the previous example. Here, a simple array of String
values is being used, backed with an ArrayContentProvider
. By creating a new part, it is possible to switch between the tabs to see the effect of changing the selection.
In order to hook up the viewer's selection with the platform's selection, the viewer's selectionChanged
event needs to be delegated to the platform. To do that, the ESelectionService
needs to be injected.
The E4 context contains a set of name/value pairs (like a HashMap
), and one of these is used to track the current selection. When the selection changes, the new value is set into the context, and this triggers the method calls on the corresponding parts.
Because there may not be a selection when the part is created, it is necessary to annotate it with @Optional
. If a method is marked with @Optional
then it won't be called at all; if a parameter is marked as @Optional
then the method will be called, but with a null
parameter.
In general, for methods that receive events, mark them as @Optional
so that they are not called at creation time.
E4 can automatically inject the active selection as it changes. The context contains an object with a key of IServiceConstants.ACTIVE_SELECTION
(which has the value org.eclipse.ui.selection
), which can be injected as a @Named
parameter in a method call. Since there may not be a selection, the @Optional
annotation must be used, as otherwise exceptions will be reported when the part is created.