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. Create a new class called Hello in the com.packtpub.e4.application.parts package.
  2. Add a private field called label of type Label.
  3. Add a create method annotated with @PostConstruct that instantiates the Label and sets its text to "Hello".
  4. Optionally, add an onFocus method annotated with @Focus that sets the focus on the Label.
  5. 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 onFocus() {
        label.setFocus();
      }
    }
  6. Double-click on the Application.e4xmi file and it should open up in the application editor (if it doesn't, ensure that the E4 Tools are installed from the update site as described earlier in this chapter).
  7. Navigate to Application | Windows and Dialogs | Trimmed Window | Controls | Perspective Stack | Perspective | Controls | Part Sash Container | Part Stack.
  8. Delete the Sample Part, if present, by right-clicking on it and choosing Remove.
  9. Right-click on the Part Stack, and select Add child followed by Part. The part should be created as follows:
    1. ID: com.packtpub.e4.application.part.hello
    2. Label: Hello
    3. 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
    4. Ensure that the Closeable checkbox is not selected
    5. Ensure that the To Be Rendered checkbox is selected
    6. Ensure that the Visible checkbox is selected
    Time for action – creating a part
  10. 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
  11. If nothing is shown, the first debug point is to delete the workspace of the run-time 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.
  12. Go to the Run | Run Configurations… menu, and 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 run-time workspace each time.
  14. If still not shown, put a breakpoint in the @PostConstruct method to verify that it is being called. If not, verify that the @javax.annotation.PostConstruct annotation is on the method, that the right class name is specified 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 changed if necessary.

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

  • Application, which contains
  • (Trimmed) Windows, which contain
  • Perspective stacks, which contain
  • Perspectives, which contain
  • Part Sash Container, which contain
  • Part Stacks, which contain
  • Parts

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

The default application uses a Trimmed Window, which means it can have toolbars such as the Save and Open tools (these are shown as a 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 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.

During development, 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. Any changes to the model (creating new views, resizing the parts) update 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. Workspaces can be cleaned by running Eclipse with the -clean and -clearPersistedState flags.

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

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