Although free text may be appropriate for some types of preferences, for others, choosing from a set of values may be more appropriate. ComboFieldEditor
can be used to present the user with a selection, which can be used to represent the user's favourite TimeZone
. The combo dropdown is built from an array of pairs of strings. The first string is the displayed label in the dropdown, while the second value is the string identifier that will be persisted to (and loaded from) the preferences store. Perform the following steps:
createFieldEditors()
method of the ClockPreferencePage class
, add the following code to populate a ComboFieldEditor
with the list of TimeZone
IDs:protected void createFieldEditors() {String[][] data;String[] ids = TimeZone.getAvailableIDs(); Arrays.sort(ids); data = new String[ids.length][]; for (int i = 0; i < ids.length; i++) { data[i] = new String[] { ids[i], ids[i] }; } addField(new ComboFieldEditor("favourite","Favourite time zone", data, getFieldEditorParent())); }
TimeZone
. Choose a value, then close and re-open the Eclipse instance; the previous value should be stored as seen in the following screenshot:Adding new types of field editor allows different types of data to be edited. Since all of the preferences are saved as a string, the ComboFieldEditor
takes a set of pairs of strings; one for the display label, and one for the persisted value.
The ComboFieldEditor
was initialized with a list of the TimeZone
IDs, using the ID for both the display label and the persisted value. The display could present more information such as the display name, the offset from GMT, or other metadata. However, the string value which is persisted to the preferences should be unique and not subject to parsing or loading errors. In this case, using the ID means that a later iteration of the preferences plug-in could render the display text in a different form while still persisting the same ID in the preference store.