Time for action – creating a part

Having created a sample application, the next step is to create a view, known as a part in E4. Parts are the generic name for views, editors, and other grouping components in an E4 application. Unlike views in Eclipse 3, the view class doesn't have to have any references to the Eclipse APIs. This makes it particularly easy to build and test in isolation.

  1. Open the plugin.xml file of the com.packtpub.e4.application project, and go to the Dependencies tab. Add a dependency to the org.eclipse.ui.di bundle, if it's not already added.
  2. Create a new class called Hello in the com.packtpub.e4.application.parts package.
  3. Add a field called label of type Label.
  4. Add a create() method, annotated with @PostConstruct that instantiates the Label and sets its text to Hello.

    Note

    If @PostConstruct appears not to work, ensure javax.annotation is added as a package import to the bundle (see http://www.vogella.com/articles/EclipseRCP/article.html#tutorial_api2).

  5. Add a focus() method, annotated with @Focus.

    Note

    The focus() method is required in Eclipse 4.2 but optional in 4.3 onwards.

  6. The class will look like:
    package com.packtpub.e4.application.parts;
    import javax.annotation.PostConstruct;
    import org.eclipse.e4.ui.di.Focus;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Label;
    public class Hello {
      private Label label;
      @PostConstruct
      public void create(Composite parent) {
        label = new Label(parent, SWT.NONE);
        label.setText("Hello");
      }
      @Focus
      public void focus() {
        label.setFocus();
      }
    }
  7. Double-click on the Application.e4xmi file and it should open up in the application editor (if it doesn't, install the E4 tools from the update site earlier in this chapter).
  8. Navigate to Application | Windows | Trimmed Window | Controls | Perspective Stack | Perspective | Controls | PartSashContainer | Part Stack.
  9. Delete the Sample Part, if present, by right-clicking on it and choosing Remove.
  10. Right-click on the Part Stack, and select Add child followed by Part. The part should be created as follows:
    • ID: com.packtpub.e4.application.part.0
    • Label: Hello
    • Class URI: Click on Find and enter Hello as the class name, and it will add bundleclass://com.packtpub.e4.application/ com.packtpub.e4.application.parts.Hello
    • Closeable: Ensure Closeable is not selected
    • To Be Rendered: Ensure To Be Rendered is selected
    • Visible: Keep Visible selected
    Time for action – creating a part
  11. Finally, save the e4xmi file and then launch the product. If all has gone well, Hello should be displayed in the generated window:
    Time for action – creating a part
  12. If nothing is shown, the first debug point is to delete the workspace of the runtime application. The launch configuration can be set to clear the contents of the workspace each time it launches, which is a good idea for E4 development as often the new files aren't copied over or stale files can be left behind.

    Go to the Run | Run Configurations... menu, select the com.packtpub.e4.application configuration. On the Main tab there is an option to Clear:

    Time for action – creating a part
  13. Leave the option to Ask for confirmation before clearing selected to be prompted with a dialog each time the product is run, or deselected to delete the contents of the runtime workspace each time.
  14. If still not shown, put a breakpoint in the @PostConstruct method to verify that it is being called. If not, check @javax.annotation.PostConstruct annotation is on the method, that the right class name in the e4xmi file, and that everything is saved before launching. Another approach is to delete the launch configuration and launch a new one via the product launch as before.

What just happened?

Eclipse 4 applications are modeled with an e4xmi file, which defines both visual and non-visual contents. At runtime, an Eclipse 4 application is booted with org.eclipse.e4.ui.workbench.swt.E4Application, which reads the file specified in the product's applicationXMI property. The default name created by the wizard is Application.e4xmi, but this can be replaced if necessary.

Note

Other renderers exist, such as e(fx)clipse's implementation built on JavaFX.

Parts in E4 are the equivalent of views/editors in Eclipse 3.x. The structure of viewable content in the default E4 application is:

  1. Application, which contains
  2. (Trimmed) windows, which contains
  3. Perspective stacks, which contains
  4. Perspectives, which contains
  5. PartSashContainer, which contains
  6. Part stacks, which contains
  7. Parts

In addition, Trimmed Windows can also contain Menus and Menu Items. Menu Items are covered later in this chapter.

In the default application, the window uses a Trimmed Window, which means it can have toolbars such as the save and open tools. (These are shown as Handled Tool Item or Direct in the application editor.) Each element can have a control which allows parts to be mixed and matched; for example, it's not mandatory for a Window to use any perspectives at all; they can just contain controls if desired.

The Hello part was created and added into the application by adding it to the model. At runtime, the class is instantiated, followed by an invocation of the method annotated with @PostConstruct. Any required arguments are injected in automatically, which are obtained from the runtime context which is akin to a HashMap of services by class name. Finally, the @Focus call is invoked when the part gets the focus; it should delegate that call to the key widget within the part.

Note, that since the hook between the application model and the code is the pointer in the Class URI reference to the fully qualified class name, when renaming Java class names or packages, it's important to select the option that allows the fully qualified name to be replaced in other files, as otherwise links between the application model and the parts may be broken.

It may be necessary to clear the workspace when starting. This is because the application model is not only used for an initial starting point of the application; it's also used to model the runtime of the application as well. Any changes to the model (creating new views, resizing the parts) updates the runtime model. When the application shuts down normally, the updated state of the model is saved and used for subsequent launches. As a result, a newly launched application with the same workspace will show the state of the workspace at the last time it shut down, not any new state that may have been added at development time.

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

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