Version 5.6
Copyright © 2009, 2010 , 2011, 2012 Lars Vogel
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 | ||
Table of Contents
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
org.osgi.service.prefs.Preferences
class. 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 plugins and product definitions. |
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 also 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("note1"); 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("note1"); Preferences sub2 = preferences.node("node2"); sub1.get("h1", "default"); sub1.get("h2", "default"); sub2.get("h1", "default");
You can use the file plugin_customization.ini to set preference values. For format to use is <plugin id> / <setting> = <value></para>.e.g. de.vogella.test.proference/check=true.
To find the right values, just start your Eclipse application, switch to the .metadata directory in your workspace directory (per default the directory your application is starting in) and search for files ending with "".pref".
Eclipse 4 provides dependency injection to simplify the handling of preferences. It allows to access Preferences via dependency injection and to track changes.
Eclipse will track changes for Preferences in the
InstanceScope
and
ConfigurationScope. Preferences in the
DefaultScope
will not be tracked.
// Instance Scope is tracked Preferences node = new InstanceScope().getNode("de.vogella.e4.todo"); node.put("key", "2012"); // Injection will be called @Inject @Preference(value = "key", nodePath = "de.vogella.e4.todo") String year; // @Inject public TestView() { Preferences node = new DefaultScope().getNode("test.project"); node.put("key", "2010"); } // Setting the preference via DI @Inject public void createControl(final Composite composite, @Preference final IEclipsePreferences node) { Button store = new Button(composite, SWT.PUSH); store.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { node.put("key", "myValue"); } }); }
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.
http://wiki.eclipse.org/E4 Eclipse E4 - Wiki