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.
Hello
in the com.packtpub.e4.application.parts
package.private
field called label
of type Label
.create
method annotated with @PostConstruct
that instantiates the Label
and sets its text to "Hello"
.onFocus
method annotated with @Focus
that sets the focus on the Label
.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(); } }
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).com.packtpub.e4.application.part.hello
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: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, 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.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:
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.
3.131.13.37