Time for action – switching between themes

The themes can be switched at runtime by interacting with the IThemeEngine, which can be acquired from the Eclipse 4 runtime service through injection. This provides a list of all installed themes, which can be used to populate the list viewer, and to allow the style to be changed.

  1. Create a class called ThemePart in the com.packtpub.e4.application.parts package. Add an injected field IThemeEngine, and a create method annotated with @PostConstruct that takes a Composite parameter:
    public class ThemePart {
      @Inject
      private IThemeEngine themeEngine;
      @PostConstruct
      public void create(Composite parent) {
      }
     }
  2. In the create method, add a ListViewer that is associated with an ArrayContentProvider:
    ListViewer lv = new ListViewer(parent, SWT.NONE);
    lv.setContentProvider(new ArrayContentProvider());
  3. Add a selection listener to the list viewer so that when an item is selected, the element is compared with the list of themes; if one is found, set it through the theme engine:
    lv.addSelectionChangedListener(e -> {
      ISelection sel = e.getSelection();
      if (sel instanceof IStructuredSelection && !sel.isEmpty()) {
        Object selectedElement =
         ((IStructuredSelection) sel).getFirstElement();
        for (ITheme theme : themeEngine.getThemes()) {
          if (selectedElement.equals(theme.getLabel())) {
            themeEngine.setTheme(theme, false);
          }
        }
      }
    });
  4. Finally, set the list viewer's input to a list of theme engines:
    lv.setInput(themeEngine.getThemes().stream() //
     .map(ITheme::getLabel).collect(Collectors.toList()));
  5. Now that the part is complete, it can be added to the Application.e4xmi. Add a new Part under the Application | Windows and Dialogs | Trimmed Window | Controls | Perspective Stack | Perspective | Controls | Part Sash Container | Part Stack entry in the model.
  6. Run the application, and the viewer should show a list of available themes. Clicking on the themes should switch the user interface style:
    Time for action – switching between themes

What just happened?

The theme engine is responsible for configuring the user interface when the theme changes, and will dynamically reconfigure the components as they are created. One of the benefits of having themes is that the user can switch between them, or even add new themes after the application has been launched and have them dynamically displayed in the list. Each theme is associated with an id, and it is this identifier (or the ITheme object) that is used to set the theme on the user interface.

The example shows building a list of themes by label and then searching for them when the theme changes. For a small set of themes this isn't a problem; but a better approach would be to add the ITheme objects into the viewer (or a dynamically populated menu) and then have them rendered as the label. That way, when the theme manager's setTheme method is called, the object can be used directly.

Tip

A LabelProvider could be used to translate the ITheme into a more suitable textual representation suitable for a viewer, which would allow the viewer to store the theme objects directly.

Have a go hero

Instead of having a set of themes displayed in the main user interface, it would be better if this was presented as a menu item. This can be achieved using a DynamicMenuContribution type in the user interface, and to use the @AboutToShow annotation to populate the available menu items, one per available theme. The same handler can be used to perform the style change, provided that the theme identifier is passed as part of the handler invocation.

Pop quiz – styling Eclipse 4

Q1. How can Eclipse 4 parts be styled?

Q2. What is an Eclipse 4 theme?

Q3. How are elements referenced in CSS files?

Q4. What CSS would be necessary to change all labels to italics?

Q5. How can the CSS Spy be opened?

Q6. How can the theme be changed at runtime?

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

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