Time for action – getting the window

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.

  1. To obtain the reference to the window, add a field MWindow window to the Hello class, along with an @Inject annotation.
  2. Modify the 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());
      }
    }
  3. Run the application, and the name of the label should be the name of the window, which is com.packtpub.e4.application.
  4. Open the 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.
  5. Run the application, and the name of the label and window should be Hello E4.
  6. Go back to the 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.
  7. Run the application, and two windows should be displayed. The first will contain a label "Hello E4" while the other will contain Other E4.
  8. Finally, modify the Other E4 part to be invisible by unchecking the Visible checkbox on the Trimmed Window.

What just happened?

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.

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

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