Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

Eclipse Preferences - Tutorial

Lars Vogel

Version 1.1

29.03.2011

Revision History
Revision 0.125.06.2009Lars Vogel
Created
Revision 0.2 - 1.0115.09.2010 - 29.03.2011Lars Vogel
bug fixes and enhancements

Eclipse Preferences

This article describes the usage of Eclipse preferences and preference pages. Within this tutorial Eclipse 3.6 (Helios) is used.


Table of Contents

1. Eclipse Preferences
1.1. Overview
1.2. Preference Page
1.3. Assumptions
2. Preferences via code
3. Preference Page
4. Access Preferences in different plugins
5. Thank you
6. Questions and Discussion
7. Links and Literature
7.1. Source Code
7.2. vogella Resources

1. Eclipse Preferences

1.1. Overview

Preferences are key / values pairs where the key is an arbitrary name for the preference. The value can be a boolean, string, int of another primitive type. Eclipse Preferences (org.osgi.service.prefs.Preferences) allow to save plugin specific configuration values.

Eclipse Preferences are very similar to Java Preferences with some support of additional features. They use the Eclipse framework to save and retrieve the configuration. Preferences are received and saved by get and put methods while the get methods also supply a default value in case the preferences is not yet set.

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

  • 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.

1.2. Preference Page

While you can create and manipulate preferences directly in your coding Eclipse provides an standard way for the user to maintain these values. Preference Pages are the default for doing this. Preference pages are declared via the extension point "org.eclipse.ui.preferencePages". Each preference page defines are class which is responsible for maintaining the preference values. This class must implement IWorkbenchPreferencePage and must have a non-parameter constructor. The class PreferencePage or one of its subclasses can get extended, a good template is usually FieldEditor PreferencePage.

To show the Preference dialog you can use the standard command "org.eclipse.ui.window.preferences".

1.3. Assumptions

The following assumes that you know how to create simple Eclipse RCP applications and that you know how to create commands and add them to the menu. Please see the Eclipse RCP and the Eclipse Commands tutorials .