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.
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.Hello
in the com.packtpub.e4.application.parts
package.label
of type Label
.create()
method, annotated with @PostConstruct
that instantiates the Label
and sets its text to Hello
.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).
focus()
method, annotated with @Focus
.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(); } }
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).com.packtpub.e4.application.part.0
Hello
Hello
as the class name, and it will add bundleclass://com.packtpub.e4.application/ com.packtpub.e4.application.parts.Hello
e4xmi
file and then launch the product. If all has gone well, Hello should be displayed in the generated window:Go to the Run | Run Configurations... menu, select the com.packtpub.e4.application
configuration. On the Main tab there is an option to Clear:
@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.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.
Parts in E4 are the equivalent of views/editors in Eclipse 3.x. The structure of viewable content in the default E4 application is:
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.