Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

m
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Each plug-in has a local workspace preference store for saving
+
== Current Practice ==
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:
+
As of 3.1 the technique to modify preference values goes something like this:
  
<pre>
+
private void savePluginSettings() {
   Plugin plugin = ExamplesPlugin.getDefault();
+
   // saves plugin preferences at the workspace level
   Preferences prefs = plugin.getPluginPreferences();
+
   Preferences prefs =
  int value = prefs.getInt("some.key");
+
    InstanceScope.INSTANCE.getNode(MY_PLUGIN_ID); // does all the above behind the scenes
</pre>
+
 
+
   prefs.put(KEY1, this.someStr);
When preferences are changed, the preference store must be
+
   prefs.put(KEY2, this.someBool);
explicitly saved to disk:
+
   
 
+
  try {
<pre>
+
    // prefs are automatically flushed during a plugin's "super.stop()".
   prefs.setValue("some.key", 5);
+
    prefs.flush();
   plugin.savePluginPreferences();
+
  } catch(BackingStoreException e) {
</pre>
+
    //TODO write a real exception handler.
 
+
    e.printStackTrace();
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, <tt>false</tt> 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 <tt>initializeDefaultPluginPreferences</tt>
+
method in your <tt>Plugin</tt> subclass:
+
 
+
<pre>
+
  protected void initializeDefaultPluginPreferences() {
+
      prefs = plugin.getPluginPreferences();
+
      prefs.setDefault("some,key", 1);
+
 
   }
 
   }
</pre>
+
}
 
+
Although you can change default values at any time, it is generally advised
+
private void loadPluginSettings() {
to keep them consistent so that users know what to expect when they
+
  Preferences prefs = new InstanceScope().getNode(MY_PLUGIN_ID);
revert preferences to their default values. Programmatically reverting
+
  // you might want to call prefs.sync() if you're worried about others changing your settings
a preference to its default value is accomplished with the <tt>setToDefault</tt>
+
  this.someStr = prefs.get(KEY1);
method.
+
  this.someBool= prefs.getBoolean(KEY2);
 +
}
  
== See Also: ==
+
== See Also: ==
*[[FAQ_How_do_I_use_the_preference_service%3F]]
+
  
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
+
*[[FAQ How do I use the preference service?]]
 +
*[[FAQ What is a preference scope?]]
 +
{{Template:FAQ_Tagline}}

Latest revision as of 05:53, 12 November 2014

Current Practice

As of 3.1 the technique to modify preference values goes something like this:

private void savePluginSettings() {
  // saves plugin preferences at the workspace level
  Preferences prefs =
    InstanceScope.INSTANCE.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.

Back to the top