Time for action: choosing from a list

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.

  1. In the 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()));}
  2. Run the target Eclipse instance, and go to the Clock preference page. A new drop-down will allow the user to select their favorite ZoneId. Choose a value, and then close and reopen the target Eclipse instance; the previous value should be stored:
    Time for action: choosing from a list

What just happened?

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.91.37