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.

  1. Open the plug-in wizard by navigating to File | New | Other | Plug-in Project. Enter the details as follows:
    1. Set Project name to com.packtpub.e4.clock.ui.
    2. Ensure that Use default location is selected.
    3. Ensure that Create a Java project is selected.
    4. The Eclipse Version should be targeted to 3.5 or greater.
  2. Click on Next again, and fill in the plug-in properties:
    1. Set ID to com.packtpub.e4.clock.ui.
    2. Set Version to 1.0.0.qualifier.
    3. Set Name to Clock.
    4. Set Vendor to PacktPub.
    5. Ensure that Generate an Activator is selected.
    6. Set the Activator to com.packtpub.e4.clock.ui.Activator.
    7. Ensure that This plug-in will make contributions to the UI is selected.
    8. Rich client application should be No.
  3. Click on Next to choose from a set of templates:
    1. Ensure that Create a plug-in using one of the templates is selected.
    2. Choose the Plug-in with a view template.
  4. Click on Next to customize the aspects of the sample; set:
    1. Java package name to com.packtpub.e4.clock.ui.views.
    2. View class name to ClockView.
    3. View name to Clock View.
    4. View category ID to com.packtpub.e4.clock.ui.
    5. View category name to Timekeeping.
    6. Viewer type to Table Viewer.
  5. Deselect the Add checkboxes as these are not required.
  6. Click on Finish to create the project.
  7. Run the target Eclipse application via the run toolbar icon.
  8. 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:
    Time for action – creating a view

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.

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

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