Time for action – creating a preference page

Although preferences can be stored programmatically, users will typically change them through the user interface. The canonical way of presenting preferences is through a preference page in Eclipse, which can be accessed through the Preferences menu. A preference page implements the IPreferencePage interface, but the easiest way is to use the FieldEditorPreferencePage as a superclass, which provides most of the standard plug-in behavior needed, along with the IWorkbenchPreferencePage interface.

  1. Open the plugin.xml of the com.packtpub.e4.clock.ui plug-in. To declare a new preference page, use the org.eclipse.ui.preferencePages extension point. Add the following:
    <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>

    Note

    The same effect can be achieved by editing plugin.xml in the editor, clicking on Add in the Extensions tab, and selecting the preferencePages extension point.

  2. Create a class called ClockPreferencePage that extends FieldEditorPreferencePage and implements IWorkbenchPreferencePage in the com.packtpub.e4.clock.ui package, as follows:
    public class ClockPreferencePage extends FieldEditorPreferencePage
     implements IWorkbenchPreferencePage {
      protected void createFieldEditors() {
      }
      public void init(IWorkbench workbench) {
      }
    }
  3. Run the target Eclipse application, and go to the preferences window by navigating to Eclipse | Preferences on macOS or Window | Preferences on Windows/Linux. In the preferences list, a Clock preference page should be displayed, although at present there will be no content.
  4. Add a new IntegerFieldEditor for storing the launch count, by adding it to the createFieldEditors method:
    protected void createFieldEditors() {
      addField(new IntegerFieldEditor("launchCount", 
      "Number of times it has been launched",getFieldEditorParent()));
    }
  5. Run the target Eclipse application, go to Preferences, and look at the Clock preference page. To get it to display the correct value, FieldEditorPreferencePage needs to be connected to the plug-in's PreferenceStore:
    public void init(IWorkbench workbench) {
      setPreferenceStore(new ScopedPreferenceStore(
       InstanceScope.INSTANCE, "com.packtpub.e4.clock.ui"));
    }
  6. Now run the target Eclipse application, go to Preferences, and look at the Clock preference page. The number will update based on the number of times the Eclipse application has launched:
    Time for action – creating a preference page

What just happened?

The Preferences page was created and connected to the preferences window and the correct preference store. 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
18.222.116.146