RSS

Search Engine

Tuesday, July 6, 2010

Using preferences

The following will create three commands. One of set the preference values. The next will display the values and the last will clear the preference values.

Create a new project "de.vogella.preferences.test" (see Eclipse RCP Tutorials for details). Use the "Hello RCP" as a template.

Create the following commands with the following default handler. Add these commands to the menu.

Table 1. Commands

CommandDefault HandlerDescription
de.vogella.preferences.test.setPreferencesde.vogella.preferences.test.handler.SetPreferencesSets the initial values of Preferences
de.vogella.preferences.test.deletePreferencesde.vogella.preferences.test.handler.DeletePreferencesDeletes the preference values
de.vogella.preferences.test.showPreferencesde.vogella.preferences.test.handler.ShowPreferencesDisplays the preference values

Create the following coding in the default handler.

   
package de.vogella.preferences.test.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

public class SetPreferences extends AbstractHandler {

public Object execute(ExecutionEvent event) throws ExecutionException {
// This would be using instance scope
// Preferences preferences = new InstanceScope()
// .getNode("de.vogella.preferences.test");
// This is using configuration scope
Preferences preferences = new ConfigurationScope()
.getNode("de.vogella.preferences.test");
// This would be using default n scope
// Preferences preferences = new DefaultScope()
// .getNode(Application.PLUGIN_ID);
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();
}
return null;
}

}

   
package de.vogella.preferences.test.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

public class DeletePreferences extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) {
Preferences preferences = new ConfigurationScope()
.getNode("de.vogella.preferences.test");
Preferences sub1 = preferences.node("note1");
Preferences sub2 = preferences.node("node2");
// Delete the existing settings
try {
sub1.clear();
sub2.clear();
} catch (BackingStoreException e) {
e.printStackTrace();
}
// Forces the application to save the preferences
try {
preferences.flush();
} catch (BackingStoreException e) {
e.printStackTrace();
}
return null;
}
}

   
package de.vogella.preferences.test.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;
import org.osgi.service.prefs.Preferences;

public class ShowPreferences extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event)
.getShell();
Preferences preferences = new ConfigurationScope()
.getNode("de.vogella.preferences.test");
Preferences sub1 = preferences.node("note1");
Preferences sub2 = preferences.node("node2");
MessageDialog.openInformation(shell, "Info", sub1.get("h1", "default"));
MessageDialog.openInformation(shell, "Info", sub1.get("h2", "default"));
MessageDialog.openInformation(shell, "Info", sub2.get("h1", "default"));
return null;
}
}

Run and test your program. If you set the preferences and re-start the program the values should still be stored in the application.

0 comments:

Post a Comment