Time for action – creating a preference page

Although preferences can be stored, providing a user interface is necessary for end users. A preference page implements the IPreferencePage interface. The easiest way is to use FieldEditorPreferencePage as a super class, which provides most of the standard plug-in behavior needed. Perform the following steps:

  1. Open the plugin.xml of the clock.ui plug-in. In order to declare a new preference page, use the org.eclipse.ui.preferencePages extension point. Add the following code:
    <extension point="org.eclipse.ui.preferencePages"><page name="Clock"class="com.packtpub.e4.clock.ui.ClockPreferencePage"id="com.packtpub.e4.clock.ui.preference.page"/>
    </extension>
  2. The same effect can be achieved by editing the plugin.xml in the editor and clicking on Add in the Extensions tab and selecting the preferencePages extension point.
  3. Create a class ClockPreferencePage that extends FieldEditorPreferencePage in the com.packtpub.e4.clock.ui package as follows:
    public class ClockPreferencePage extends FieldEditorPreferencePageimplements IWorkbenchPreferencePage {
      protected void createFieldEditors() {
      }
      public void init(IWorkbench workbench) {
      }
    }
  4. Run the Eclipse application. Go to the preferences window by clicking on Eclipse | Preferences for OS X, or Window | Preferences for Windows/Linux. In the preferences list a Clock page should be displayed, although at present there will be no content.
  5. Add a new IntegerFieldEditor for storing the launch count by adding it to the createFieldEditors() method as follows:
    protected void createFieldEditors() {
      addField(new IntegerFieldEditor("launchCount", "Number of times it has been launched",getFieldEditorParent()));
    }
  6. Run the Eclipse application, go to Preferences and look at the Clock page. To get it to display the correct value, the FieldEditorPreferencePage needs to be connected to the plug-in's PreferenceStore as follows:
    public void init(IWorkbench workbench){
      setPreferenceStore(
       Activator.getDefault().getPreferenceStore());
    }
  7. Now run the Eclipse application, go to the Preferences and look at the Clock page. The number will update based on the number of times the Eclipse application has launched.

What just happened?

The preferences page was created and connected to the preferences window and the correct preference store (obtained from the plug-in's Activator). Instances of FieldEditor were added to display properties on a per-property basis.

The implementation of getFieldEditorParent() is something that needs to be called each time a field editor is created. It might be tempting to refactor this into a common variable, but the JavaDoc says that the value cannot be cached. If the FLAT style is used, then it will create a new instance of the parent each time it is called.

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

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