Jump to: navigation, search

Difference between revisions of "FAQ How do I load and save plug-in preferences?"

(See Also:)
(Current Practice)
Line 27: Line 27:
 
The above technique has been deprecated since 3.1.
 
The above technique has been deprecated since 3.1.
  
The current technique (as far as the author can tell after finally getting it working) goes something like this:
+
The current technique goes something like this:
  
 
  private void savePluginSettings() {
 
  private void savePluginSettings() {
 
   // saves plugin preferences at the workspace level
 
   // saves plugin preferences at the workspace level
   Preferences prefs = Platform.getPreferencesService().getRootNode().node(Plugin.PLUGIN_PREFEERENCES_SCOPE).node(MY_PLUGIN_ID);
+
   Preferences prefs =
 +
    //Platform.getPreferencesService().getRootNode().node(Plugin.PLUGIN_PREFEERENCES_SCOPE).node(MY_PLUGIN_ID);
 +
    new InstanceScope().getNode(MY_PLUGIN_ID); // does all the above behind the scenes
 
   
 
   
 
   prefs.put(KEY1, this.someStr);
 
   prefs.put(KEY1, this.someStr);
Line 46: Line 48:
 
   
 
   
 
  private void loadPluginSettings() {
 
  private void loadPluginSettings() {
   Preferences prefs = Platform.getPreferencesService().getRootNode().node(Plugin.PLUGIN_PREFEERENCES_SCOPE).node(MY_PLUGIN_ID);
+
   Preferences prefs = new InstanceScope().getNode(MY_PLUGIN_ID);
 
   // you might want to call prefs.sync() if you're worried about others changing your settings
 
   // you might want to call prefs.sync() if you're worried about others changing your settings
 
   this.someStr = prefs.get(KEY1);
 
   this.someStr = prefs.get(KEY1);
 
   this.someBool= prefs.getBoolean(KEY2);
 
   this.someBool= prefs.getBoolean(KEY2);
 
  }
 
  }
 
Note: There are several different scopes hung off the root node, and it is THEIR children that can be saved.  Values set to ''root'' will not be saved.  Values set to ''root/PLUGIN_PREFERENCES_SCOPE'' will not be saved.  Also note that there is no particular place where all these scope names have been stored, though there are several in Plugin.
 
 
The actual children of the root node under the author's install of Eclipse 3.6:
 
*project (The author believes that this is implemented by ResourcesPlugin, but cannot find the constant to access it anywhere)
 
*bundle_defaults (probably loaded by "preferences.ini" in your plugin folder)
 
*default (?)
 
*configuration (?)
 
*instance (AKA PLUGIN_PREFERENCES_SCOPE)
 
*profile (?)
 
  
 
== See Also:  ==
 
== See Also:  ==

Revision as of 13:25, 19 July 2010

The Deprecated Way

Each plug-in has a local workspace preference store for saving arbitrary primitive data types and strings. Preferences displayed in the Workbench Preferences dialog are typically stored in these local plug-in preference stores. The Import and Export buttons in the Workbench Preferences page also operate on these plug-in preference stores.

Retrieving values from plug-in preferences is fairly straightforward:

   Plugin plugin = ExamplesPlugin.getDefault();
   Preferences prefs = plugin.getPluginPreferences();
   int value = prefs.getInt("some.key");

When preferences are changed, the preference store must be explicitly saved to disk:

   prefs.setValue("some.key", 5);
   plugin.savePluginPreferences();

The plug-in preference store can also store default values for any preference in that plug-in. If a value has not been explicitly set for a given key, the default value is returned. If the key has no default value set, a default default value is used: zero for numeric preferences, false for Boolean preferences, and the empty string for string preferences. If the default default is not appropriate for your preference keys, you should establish a default value for each key by overriding the initializeDefaultPluginPreferences method in your Plugin subclass:

   protected void initializeDefaultPluginPreferences() {
      prefs = plugin.<strike>getPluginPreferences</strike>();
      prefs.setDefault("some,key", 1);
   }

Although you can change default values at any time, it is generally advised to keep them consistent so that users know what to expect when they revert preferences to their default values. Programmatically reverting a preference to its default value is accomplished with the setToDefault method.


Current Practice

The above technique has been deprecated since 3.1.

The current technique goes something like this:

private void savePluginSettings() {
  // saves plugin preferences at the workspace level
  Preferences prefs =
    //Platform.getPreferencesService().getRootNode().node(Plugin.PLUGIN_PREFEERENCES_SCOPE).node(MY_PLUGIN_ID);
    new InstanceScope().getNode(MY_PLUGIN_ID); // does all the above behind the scenes

  prefs.put(KEY1, this.someStr);
  prefs.put(KEY2, this.someBool);

  try {
    // prefs are automatically flushed during a plugin's "super.stop()".
    prefs.flush();
  } catch(BackingStoreException e) {
    //TODO write a real exception handler.
    e.printStackTrace();
  }
}

private void loadPluginSettings() {
  Preferences prefs = new InstanceScope().getNode(MY_PLUGIN_ID);
  // you might want to call prefs.sync() if you're worried about others changing your settings
  this.someStr = prefs.get(KEY1);
  this.someBool= prefs.getBoolean(KEY2);
}

See Also:


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.