Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

3. Preference Page

The following will create a project with with a preference pages which allows the user to maintain certain settings. Create a new RCP project "de.vogella.preferences.page". Make sure you flag the "Generate an activator..." during the project creation. Use the "RCP application with a view" as a template.

Go to plugin.xml and add the extension "org.eclipse.ui.preferencePages" with the following settings.

Maintain the following code for your class "MyPreferencePage1". Method init() sets the preference store and the method createFieldEditors() registers pre-defined editors for values. checkState() allows to perform a validations. To get notified about value changes you to override the propertyChange method.

			
package de.vogella.preferences.page.preferencepage;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import de.vogella.preferences.page.Activator;

public class MyPreferencePage1 extends FieldEditorPreferencePage implements
		IWorkbenchPreferencePage {

	public MyPreferencePage1() {
		super(GRID);

	}

	public void createFieldEditors() {
		addField(new DirectoryFieldEditor("PATH", "&Directory preference:",
				getFieldEditorParent()));
		addField(new BooleanFieldEditor("BOOLEAN_VALUE",
				"&An example of a boolean preference", getFieldEditorParent()));

		addField(new RadioGroupFieldEditor("CHOICE",
				"An example of a multiple-choice preference", 1,
				new String[][] { { "&Choice 1", "choice1" },
						{ "C&hoice 2", "choice2" } }, getFieldEditorParent()));
		addField(new StringFieldEditor("MySTRING1", "A &text preference:",
				getFieldEditorParent()));
		addField(new StringFieldEditor("MySTRING2", "A &text preference:",
				getFieldEditorParent()));
	}

	@Override
	public void init(IWorkbench workbench) {
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription("A demonstration of a preference page implementation");
	}
}

		

To display the preference page you can use the standard Eclipse command "org.eclipse.ui.window.preferences". Add this to your menu, it will show the preference dialog.

Run the application and check that you can open your preference page via the menu. Validate that maintained values are stored even if you re-start your application.

Add a command "showPreferenceValues" with the following handler to the menu. This command demonstrates how to access preferences values from the preferencePage.

			
package de.vogella.preferences.page.handler;

import org.eclipse.core.commands.AbstractHandler;

public class ShowPreferenceValues extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event)
				.getShell();
		String myPrefString = Activator.getDefault().getPreferenceStore()
				.getString("MySTRING1");
		MessageDialog.openInformation(shell, "Info", myPrefString);
		Boolean myPrefBoolean = Activator.getDefault().getPreferenceStore()
				.getBoolean("BOOLEAN_VALUE");
		// RadioGroupFieldEditor can get access
		String choice = Activator.getDefault().getPreferenceStore().getString("CHOICE");
		System.out.println(choice);
		MessageDialog.openInformation(shell, "Info", myPrefBoolean.toString());
		// I assume you get the rest by yourself
		return null;
	}

}

		

To set the default values for preferences use the extension point "org.eclipse.core.runtime.preferences". Create a new initializer with the following class "de.vogella.preferences.page.preferencepage.MyInitializer".

			
package de.vogella.preferences.page.preferencepage;

import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.jface.preference.IPreferenceStore;

import de.vogella.preferences.page.Activator;

public class MyInitializer extends AbstractPreferenceInitializer {

	public MyInitializer() {
	}

	@Override
	public void initializeDefaultPreferences() {
		IPreferenceStore store = Activator.getDefault().getPreferenceStore();
		store.setDefault("MySTRING1", "http://www.vogella.de");
	}

}

		

Finally change the class "View" to the following to show one of the preference values. We also register a PropertyChangeListener to the preference store to get informed in case the preference settings change.

			
package de.vogella.preferences.page;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;

public class View extends ViewPart {

	private Label label;

	public void createPartControl(Composite parent) {
		IPreferenceStore preferenceStore = Activator.getDefault()
				.getPreferenceStore();
		String string = preferenceStore.getString("MySTRING1");

		label = new Label(parent, SWT.NONE);
		label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false,
				false));
		label.setText(string);
		// Add change listener to the preference store so that we are notified
		// in case of changes
		Activator.getDefault().getPreferenceStore()
				.addPropertyChangeListener(new IPropertyChangeListener() {
					@Override
					public void propertyChange(PropertyChangeEvent event) {
						if (event.getProperty() == "MySTRING1") {
							label.setText(event.getNewValue().toString());
						}
					}
				});
	}

	public void setFocus() {
	}
}