Dialog settings

The org.eclipse.jface.dialogs package provides a utility class, DialogSettings, for storing and retrieving keyed values. You can use this class to save and retrieve primitive data types and string values that you associate with key names. The settings are loaded and saved using an XML file.

AbstractUIPlugin provides support for plug-in wide dialog settings stored in an XML file in your plug-in's directory. If a dialog settings file is not found in your plug-in directory, an empty DialogSettings will be created for you. When the plug-in is shut down, any settings that were added to it will be saved in an XML file and retrieved the next time the plug-in is started up.

You can access your dialog settings anywhere in your plug-in code. The following snippet shows how you could obtain the dialog settings for the readme tool.

   IDialogSettings settings = ReadmePlugin.getDefault().getDialogSettings();

Values are stored and retrieved using get and put methods. The get methods are named after the type of primitive that is being accessed. You can store and retrieve boolean, long, double, float, int, array, and string values. The following snippet shows how we could use dialog settings to initialize control values in a dialog.

   protected Control createDialogArea(Composite parent) {
      IDialogSettings settings = ReadmePlugin.getDefault().getDialogSettings();
      checkbox = new Button(parent,SWT.CHECK);
      checkbox.setText("Generate sample section titles");
      // initialize the checkbox according to the dialog settings

The value of the setting can be stored later when the ok button is pressed.

   protected void okPressed() {
      IDialogSettings settings = ReadmePlugin.getDefault().getDialogSettings();
      // store the value of the generate sections checkbox
      settings.put("GenSections", checkbox.getSelection());

Dialog bounds settings

In general, the definition and interpretation of dialog settings are the responsibility of your plug-in. However, there are some specific dialog settings keys defined inside the JFace dialog framework that are used to remember the last size and position of a dialog, so that the dialog can be opened to that size and position on its next invocation. The framework will do the work to query and store the dialog's size and position, but you must implement a method that supplies the IDialogSettings instance that should be used to store the dialog bounds information. The following snippet shows how the SectionsDialog could take advantage of this feature.

   protected IDialogSettings getDialogBoundsSettings() {
	   return ReadmePlugin.getDefault().getDialogSettings();

By implementing this method, the size and position of the SectionsDialog will be stored in predefined keys within the plug-in's dialog settings, causing the dialog to open at its previous location.