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.
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) { } }
create
method, add a ListViewer
that is associated with an ArrayContentProvider
:ListViewer lv = new ListViewer(parent, SWT.NONE); lv.setContentProvider(new ArrayContentProvider());
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); } } } });
lv.setInput(themeEngine.getThemes().stream() // .map(ITheme::getLabel).collect(Collectors.toList()));
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.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.
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.
3.129.13.201