RSS

Search Engine

Tuesday, July 6, 2010

Preference Page

3.1. Project

The following will create an example for preference pages.

Create a new RCP project "de.vogella.preferences.page" . Use the "Hello RCP" as a template.

Tip

The template which we are going to use requires an activator. Make sure you flag the "Generate an activator..." during the project creation.

3.2. Extension Point and Preference Page

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

Maintain the following code for your class "MyPreferencePage1".

   
package de.vogella.preferences.page.perferencepage;

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");
}
}

3.3. Commands

To display the preference page you can re-use an existing Eclipse command.

Add the standard Eclipse command "org.eclipse.ui.window.preferences" to your menu. This will show the preference dialog. See Eclipse Command for how to add a standard command to your menu.

Run the application. Now you can select your preference page via the menu.

Create a command "showPreferenceValues" with a default handler. Add this command also to your menu. Maintain the following coding for the default handler.

   
package de.vogella.preferences.page.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

import de.vogella.preferences.page.Activator;

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");
MessageDialog.openInformation(shell, "Info", myPrefBoolean.toString());
// I assume you get the rest by yourself
return null;
}

}

This command demonstrates how to access preferences values from the preferencePage. Try to change values in the preference page, restart the application. The values should still be the changed ones.

0 comments:

Post a Comment