Time for action – obtaining the selection

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 are no add/removePostSelection methods).

  1. Create a class called Rainbow in the com.packtpub.e4.application.parts package. Add a private static final array of strings with colors of the rainbow.
  2. Add a 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);
      }
    }
  3. Open the Application.e4xmi and go to Application | Windows and Dialogs | Trimmed Window | Controls | Perspective Stack | Perspective | Controls | Part Sash 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.packtpub.e4.application.parts.Rainbow.
  4. Run the application, and two tabs should be shown. Next to the Hello tab, the Rainbow tab will be shown, containing the rainbow colors in a list.
  5. To synchronize the selection between the two tabs, add a field of type 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(event -> {
          selectionService.setSelection(event.getSelection()));
      ...
    }
  6. When a selection is made on the 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 to the Hello class as follows:
    @Inject
    @Optional
    public void setSelection(
      @Named(IServiceConstants.ACTIVE_SELECTION)
      Object selection) {
      if (selection != null) {
        label.setText(selection.toString());
      }
    }
  7. Run the application, switch to the Rainbow part, and select the Red color from the list. Switch back to the Hello part and it should show [Red] in the label.

What just happened?

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 may not 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 (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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.141.44