In an Eclipse 3.x application, the main window is typically accessed via a static accessor like Display.getDisplay()
or workbench.getWorkbenchWindows()
. Both of these assume there is a way of getting to this global list in the first place, often through tightly coupled code references. In addition to 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
.
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 Applications | Windows | Trimmed Window which is where the label is defined. Modify the label to Hello E4
and save the file.Hello E4
.Application.e4xmi
file, select the Trimmed Window node and do Edit | Copy. Select the Windows node and do Edit | Paste. This will create a duplicate node for the Trimmed Window. Modify the label to Other E4
and save the file.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
, the 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.