A more useful alternative to the Memento pattern is DialogSettings
, which provides a properties-like interface for storing strings and other basic primitive values. This stores its information in an XML file, and can be acquired as a standard extension to the UI plug-in or created from a file location. The settings store is used to store values persistently, and is saved automatically when the plug-in shuts down. At startup, it is loaded automatically. Perform the following steps:
DialogSettings
, remove the init()
and save()
methods from the TimeZoneView
and replace them with the following in the createPartControl()
:final IDialogSettings settings =Activator.getDefault().getDialogSettings(); lastTabSelected = settings.get("lastTabSelected");
getDialogSettings()
comes from the UIPlugin
class. Once the DialogSettings
have been acquired, it can be used to store and retrieve values. Update the selection listener to store this in the settings instead:tabs.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (e.item instanceof CTabItem) { lastTabSelected = ((CTabItem) e.item).getText(); settings.put("lastTabSelected", lastTabSelected); } } });
The calls to IMemento
were replaced with DialogSettings
, a much more useful mechanism for storing values. In addition, we created a separate group of settings to create a nested namespace with settings.addNewSection("name")
and with settings.getSection("name")
.
The name DialogSettings
comes from the fact that was initially used by dialogs with warnings such as "Do not show this message again". In fact, this is so common that a Dialog with a "Do not show this message again" can be created with a MessageDialogWithToggle
as follows:
if (settings.getBoolean("spamalot")) { MessageDialogWithToggle dialog = MessageDialogWithToggle. openInformation(Display.getCurrent().getActiveShell(), "Spam", "Keep being spammed?", "Do not show this spam again", false, null, null); boolean spamalot = !dialog.getToggleState(); settings.put("spamalot",spamalot); }
The MessageDialogWithToggle
also has an option to write this to a preference store using the store and the key value in the last two values (null
in the example).
The DialogSettings
can also be used to store items such as the last-value-used or to restore selection.
The key difference between using an IPreferenceStore
and using DialogSettings
is that the former can be used for importing/exporting preferences between Eclipse instances, using the File | Import/Export | Preferences menu. The DialogSettings
, on the other hand, are supposed to be transient and recreatable if they are lost.
Q1. What is the default style used for the FieldEditorPreferencePage
, and how can it be changed to something more aesthetically pleasing?
Q2. What kinds of primitive values can be edited with a FieldEditorPreferencePage
?
Q3. How can a preference value be searched for in the preference page?
Q4. Which is the preferred API for storing view-specific information; IMemento
or IEclipsePreferences
?
Q5. Which class provides the "Do not show this message again" support?