| Free tutorials for Java, Eclipse and Web programming |
Of course you can create, store and retrieve preference values directly via your coding. The following will create three commands . The first command will set the preference values. The next will display the values and the last will clear the preference values.
Create a new RCP project "de.vogella.preferences.test" using the "Hello RCP" as a template.
Create three command which the following handlers and add them to the menu.
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;
// Saves some preferences
public class SetPreferences extends AbstractHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
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;
// Removes the preferences settings
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;
// Shows the current 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 via the handler "SetPreferences" and re-start the program the values should still be stored in the application. The handler "ShowPreferences" displays the current values while "DeletePreferences" removes the current values.