What is the best approach to do PreferencePage in eclipse?
Inspired from this thread:
There may be two classes of preferences:
those that are just data that the editor, view, or builder goes and gets when it needs it.
In the first case, it seems like the best approach is to use aFieldEditorPreferencePage
to display, input, validate, and store preferences.
To get the values, subclasses can provide convenience methods that get the preferences store and query and return the value given its id.those that affect an editor or view that is already running.
The opened editors and/or views need to be notified of changes that happen so they can update their views accordingly.
This could be done by iterating over the effected editors and views in theFieldEditorPreferencPage.performOk()
method, but this seems like it would create undesirable coupling: The preference page would have to know how to update the corresponding viewers.
Another possibility is to use the fact that aFieldEditorPreferencePage
is anIPropertyChangeListener
so editors could register interest in property changes that effect their views. The disadvantage of this approach is that the interest has to be removed when the editor or view is removed or exceptions will be raised if the preference page is changed after the viewer is closed.
Bug 143727 references the up-to-date (i.e. "Eclipse3.x") articles on preferences:
- How do I use the Preference service?
- Define your own global preferences
- package
org.eclipse.core.runtime.preferences
Its attached "badwordchecker" preference page is interesting to check out:
/*
* @see IWorkbenchPreferencePage#init(IWorkbench)
*/
public void init(IWorkbench workbench) {
//Initialize the preference store we wish to use
setPreferenceStore(BadWordCheckerPlugin.getDefault().getPreferenceStore());
}
/**
* Performs special processing when this page's Restore Defaults button has been pressed.
* Sets the contents of the nameEntry field to
* be the default
*/
protected void performDefaults() {
badWordList.setItems(BadWordCheckerPlugin.getDefault().getDefaultBadWordsPreference());
}
/**
* Method declared on IPreferencePage. Save the
* author name to the preference store.
*/
public boolean performOk() {
BadWordCheckerPlugin.getDefault().setBadWordsPreference(badWordList.getItems());
return super.performOk();
}