In an Eclipse 3.x application, the main window is typically accessed via a static accessor such as Display.getDisplay()
or workbench.getWorkbenchWindows()
. Both of these assume that there is a way of getting to this global list in the first place, often through tightly coupled code references. As well as OSGi services, E4 can also be used to inject references to GUI components. However, rather than accessing the GUI components directly, models are used instead. As a result, components in E4 tend to start with M (for Model)—such as MPart
, MWindow
, and MPerspective
.
private
field MWindow window
to the Hello
class, along with an @Inject
annotation.create
method so that the label of the text is taken from the window's title (label). The class will look like:import org.eclipse.e4.ui.model.application.ui.basic.MWindow; public class Hello { @Inject private MWindow window; @PostConstruct public void create(Composite parent) { Label label = new Label(parent, SWT.NONE); label.setText(window.getLabel()); } ... }
com.packtpub.e4.application
. Application.e4xmi
file, and go to Application | Windows and Dialogs | Trimmed Window which is where the label is defined. Change the label to Hello E4
and save the file.Hello E4
.Application.e4xmi
file, select the Trimmed Window node and perform a copy with Edit | Copy. Select the Windows and Dialogs node and paste with Edit | Paste. This will create a duplicate node for the Trimmed Window. Change the label to Other E4
and save the file.Hello E4
while the other will contain Other E4
.Other E4
part to be invisible by unchecking the Visible checkbox on the Trimmed Window.A reference to the enclosing MWindow
was acquired, using the same mechanism as for the OSGi LogService
. However, while the LogService
is a global instance, the MWindow
is local to the currently selected part.
The lookup strategy for objects follows a hierarchical set of contexts, which are hash-table-like structures that contain named objects. These are represented with the MContext
interface, which is implemented by:
MApplication
MWindow
MPerspective
MPart
MPopupMenu
The context search starts at the most specific element, and works its way up to the top level. If they aren't found in the MApplication
, OSGi services are consulted.
Each of these are organized into a hierarchy with IEclipseContext
, which maintains parent/child relationships between them. The lookup automatically follows the chain if an object cannot be located in the current context.
In the last example, having two separate windows means that two contexts are used, and so although the code is identical between the two, one has the Hello E4 window model injected, while the other has the Other E4 window model injected.
18.119.136.235