Back to top

vogella training Training Books

Eclipse 4 Preferences - Tutorial

Based on Eclipse 4.2

Lars Vogel

Version 5.6

03.11.2011

Revision History
Revision 0.1 14.02.2009 Lars
Vogel
created
Revision 0.2 - 5.6 16.02.2009 - 03.11.2011 Lars
Vogel
bug fixes and enhancements

Using Eclipse 4 Preferences

This tutorial gives an overview how preference values can be used in Eclipse 4.


Table of Contents

1. Eclipse Preferences
1.1. Preferences and Scopes
1.2. Eclipse Preference API
1.3. Setting preferences via plugin_customization.ini
2. Eclipse 4 Preferences
3. Thank you
4. Questions and Discussion
5. Links and Literature
5.1. Source Code

1. Eclipse Preferences

1.1. Preferences and Scopes

Preferences are key / value pairs, where the key is an arbitrary name for the preference. The value can be a boolean, string, int or another primitive type. For example a user name might be stored via a key called user . The actual value for this key might be "Lars Vogel".

Eclipse Preferences are based on the Preferences class from the org.osgi.service.prefs package. Eclipse Preferences are very similar to standard Java Preferences with some support of additional features. They use the Eclipse framework to save and retrieve the configuration.

The Eclipse runtime defines three so-called scopes. The scope defines how the preference data is stored and how it is changeable.

Table 1. Eclipse Preference scope

Scope Description
Instance scope If the user runs the same program twice, the settings between the two programs may be different.
Configuration scope If the user runs the same program twice then the settings between the two programs are the same.
Default scope Default values which can not be changed. Supplied via configuration files in plug-ins and product definitions.


1.2. Eclipse Preference API

You can create and manipulate preferences directly in your coding. Preferences are read and saved by get() and put() methods. In the get() method you specify a default value in case the key can not be found. The clear() method allows to delete a preference value.

// We access the Configuration Scope
Preferences preferences = ConfigurationScope.INSTANCE
  .getNode("de.vogella.preferences.test");

Preferences sub1 = preferences.node("node1");
Preferences sub2 = preferences.node("node2");
sub1.put("h1", "Hello");
sub1.put("h2", "Hello again");
sub2.put("h1", "Moin");
try {
  // Forces the application to save the preferences
  preferences.flush();
  } catch (BackingStoreException e) {
    e.printStackTrace();
  }
} 

// Read values from the configuration scope
Preferences preferences = ConfigurationScope.INSTANCE
  .getNode("de.vogella.preferences.test");
Preferences sub1 = preferences.node("node1");
Preferences sub2 = preferences.node("node2");
sub1.get("h1", "default");
sub1.get("h2", "default");
sub2.get("h1", "default"); 

1.3. Setting preferences via plugin_customization.ini

You can use a file to set certain preference values. This files needs to get registered via the preferenceCustomization property on your product extension point. This file is typically named plugin_customization.ini.

Referring to the plugin_customization.ini file in the product extension

The format to use is <plugin id>/<setting>=<value>, e.g. de.vogella.test.preference/check=true.

To find the right values, just start your Eclipse application, switch to the .metadata directory in your workspace directory (by default the directory your application is starting in) and search for files ending with .pref.

2. Eclipse 4 Preferences

Eclipse 4 provides dependency injection to simplify the handling of preferences. It allows you to access Preferences via dependency injection.

// Get IEclipsePreferences to change a value
@Execute
public void execute
  (@Preference(nodePath = "com.example.e4.rcp.todo") IEclipsePreferences prefs) {
  // More stuff...
  prefs.put("user", "TestUser");
  prefs.put("password", "Password");
  // Persists
  try {
  prefs.flush();
    } catch (BackingStoreException e) {
      e.printStackTrace();
    }
} 

Eclipse allows to track the values of preference settings. If these values change Eclipse will re-inject the values. Eclipse can track changes in the InstanceScope and ConfigurationScope. Preferences in the DefaultScope will not be tracked.

@Inject
@Optional
public void trackUserSettings
  (@Preference(nodePath = "com.example.e4.rcp.todo", 
  value = "user") 
  String user) {
  System.out.println("New user: " + user);
}
  
@Inject
@Optional
public void trackPasswordSettings
    (@Preference(nodePath = "com.example.e4.rcp.todo", 
  value = "password") 
  String password) {
  System.out.println("New password: " + password);
} 

3. Thank you

Please help me to support this article:

Flattr this

4. Questions and Discussion

Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.

5. Links and Literature

5.1. Source Code

Source Code of Examples

http://wiki.eclipse.org/E4 Eclipse E4 - Wiki

Eclipse RCP

Eclipse EMF

Dependency Injection