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.
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>
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) { } }
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())); }
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")); }
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.
18.222.116.146