Time for action – creating a view
The Eclipse UI consists of multiple views, which are the rectangular areas that display content, such as the Outline, Console, or Package Explorer. In Eclipse 3.x, views are created by adding an extension point to an existing plug-in, or using a template. A clock.ui
plug-in will be created to host the clock widgets and views.
- Open the plug-in wizard by navigating to File | New | Other | Plug-in Project. Enter the details as follows:
- Set Project name to
com.packtpub.e4.clock.ui
. - Ensure that Use default location is selected.
- Ensure that Create a Java project is selected.
- The Eclipse Version should be targeted to 3.5 or greater.
- Set Project name to
- Click on Next again, and fill in the plug-in properties:
- Set ID to
com.packtpub.e4.clock.ui
. - Set Version to
1.0.0.qualifier
. - Set Name to Clock.
- Set Vendor to PacktPub.
- Ensure that Generate an Activator is selected.
- Set the Activator to
com.packtpub.e4.clock.ui.Activator
. - Ensure that This plug-in will make contributions to the UI is selected.
- Rich client application should be No.
- Set ID to
- Click on Next to choose from a set of templates:
- Ensure that Create a plug-in using one of the templates is selected.
- Choose the Plug-in with a view template.
- Click on Next to customize the aspects of the sample; set:
- Java package name to
com.packtpub.e4.clock.ui.views
. - View class name to
ClockView
. - View name to Clock View.
- View category ID to
com.packtpub.e4.clock.ui
. - View category name to
Timekeeping
. - Viewer type to Table Viewer.
- Java package name to
- Deselect the Add checkboxes as these are not required.
- Click on Finish to create the project.
- Run the target Eclipse application via the run toolbar icon.
- Navigate to Window | Show View | Other | Timekeeping | Clock View to show the Clock View, which has a simple list view with One, Two, and Three listed:
What just happened?
Functionality in Eclipse will typically be implemented in multiple plug-ins. Since the clock functionality developed in this chapter is unrelated to that of the hello
plug-in, a new plug-in was created to host the code. Plug-ins typically will have more than just one view or extension, grouped logically.
The plug-in wizard created an empty plug-in project as well as two key files: MANIFEST.MF
and plugin.xml
.
Manifest.mf
The manifest contains references to dependent plug-ins and interfaces, and includes the following:
Bundle-SymbolicName: com.packtpub.e4.clock.ui;singleton:=true Bundle-Version: 1.0.0.qualifier Bundle-Activator: com.packtpub.e4.clock.ui.Activator Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime
Plug-ins that contribute to the user interface need to do two things:
- Depend on
org.eclipse.ui
- Have
;singleton:=true
after the bundle's symbolic name
The dependency on the org.eclipse.ui
bundle gives access to the Standard Widget Toolkit and other key parts of the Eclipse framework.
Note
The clause ;singleton:=true
is an OSGi directive, which means that only one version of this plug-in can be installed in Eclipse at a time. For plug-ins that add dependencies to the UI, there is a restriction that they must be singletons (this constraint is one of the main reasons why installing a new plug-requires the IDE to restart).
The manifest sets up the project's class path. Any additional plug-in dependencies need to be to the manifest.
plugin.xml
The plugin.xml
file defines a list of extensions that this plug-in provides. Extension points are how Eclipse advertises the plug-in extensions, much like a USB hub provides a generic connector that allows many other types of device to be plugged in.
The Eclipse extension points are documented in the help system, and each has a point identifier, with optional children that are point-specific. In this case, the extension is defined using the org.eclipse.ui.views
point, which expects a combination of category
and view
elements. In this case, it will look like the following:
<plugin> <extension point="org.eclipse.ui.views"> <category name="Timekeeping" id="com.packtpub.e4.clock.ui"/> <view name="Clock View" icon="icons/sample.gif" category="com.packtpub.e4.clock.ui" class="com.packtpub.e4.clock.ui.views.ClockView" id="com.packtpub.e4.clock.ui.views.ClockView"/> </extension> </plugin>
The class in this case extends the ViewPart
abstract class, which is used for all views in the Eclipse 3.x model.
Note
The Eclipse 4 (E4) model defines views in a different way, which is covered in more detail in Chapter 7, Creating Eclipse 4 Applications. The Eclipse 4.x SDK includes a 3.x compatibility layer, so these examples will work in Eclipse 4.x SDKs.
The viewer component is a default table view, which will be replaced in the next section.