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 by using a template. A clock.ui plug-in will be created to host the clock widgets and views.

  1. Open the plug-in wizard by navigating to File | New | Other | Plug-in Project. Enter the details as follows:
    • Project name: com.packtpub.e4.clock.ui
    • Select the checkbox for Use default location
    • Select the checkbox for Create a Java project
    • Target Eclipse Version: 3.5 or greater
  2. Click on Next again, and fill in the plug-in properties:
    • ID: com.packtpub.e4.clock.ui
    • Version: 1.0.0.qualifier
    • Name: Clock
    • Vendor: PacktPub
    • Select the checkbox for Generate an Activator
    • Activator: com.packtpub.e4.clock.ui.Activator
    • Select the checkbox for This plug-in will make contributions to the UI
    • Rich client application: No
  3. Click on Next to choose from a set of templates:
    • Select the checkbox for Create a plug-in using one of the templates
    • Choose the Plug-in with a view template
  4. Click on Next to customize a few aspects of the sample, including:
    • Java package name: com.packtpub.e4.clock.ui.views
    • View class name: ClockView
    • View name: Clock View
    • View category ID: com.packtpub.e4.clock.ui
    • View category name: Timekeeping
    • Viewer type: Table Viewer
  5. Deselect the Add checkboxes as these are not required.
  6. Click on Finish to create the project.
  7. Run the Eclipse application via the Run toolbar icon.
  8. Go to Window | Show View | Other | Timekeeping | Clock View to show the Clock View, which has a simple list view with One, Two, Three listed:
    Time for action – creating a view

What just happened?

It's not usual to create a plug-in project per view (or per action) but often plug-ins are encapsulated and provide functionality specific to that particular plug-in. In this case, clocks aren't related to the Hello World one created before, so a new plug-in was created to host them.

The plug-in wizard created an empty plug-in project, as well as two key files.

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 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.

The clause ;singleton:=true is an OSGi directive, which means that only one version of this plug-in can be installed in Eclipse at one time. For plug-ins which 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 classpath. Any additional plug-in dependencies need to be added 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 devices 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:

<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 Eclipse 3.x.

Note

plugin.xml E4: The Eclipse 4 model defines views in a different way, which is covered in more detail in Chapter 7, Understanding the Eclipse 4 Model. 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.

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

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