Although free text may be appropriate for some types of preference, choosing from a set of values may be more appropriate for other types. A ComboFieldEditor
instance can be used to present the user with a selection of time zones, from which the user can set their favorite ZoneId
. The combo drop-down is built from an array of pairs of strings; the first string in each pair is the display label, while the second value in each pair is the string identifier that will be persisted in the preferences store.
ClockPreferencePage
method createFieldEditors
, add the following code to populate a ComboFieldEditor
with the list of ZoneId
elements:protected void createFieldEditors() {// ... String[][] data = ZoneId.getAvailableZoneIds() // .stream().sorted().map(s -> new String[] { s, s }) // .collect(Collectors.toList()).toArray(new String[][] {});addField(new ComboFieldEditor("favorite","Favorite time zone", data, getFieldEditorParent()));}
ZoneId
. Choose a value, and then close and reopen the target Eclipse instance; the previous value should be stored:Field editors can be used to customize stored data. Since preference values 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 combo field editor was initialized with a ZoneId
list, 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 that 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.
3.145.91.37