If you want to define your preferences in Eclipse RCP plug-ins, you need to extend the extension point:
Org.eclipse.core.runtime.preferences//This extension point is used to initialize the values in the preferences
org.eclipse.ui.preferencepages//This extension point is used to define your own preferences page
The contents of plugin.xml such as:
Database preferences is hung under workflowbase, you need to fill in the category Workflowbase ID
Workflowpreferenceinitializer class, used to initialize values in preferences
Import Org.eclipse.core.runtime.preferences.abstractpreferenceinitializer;import Org.eclipse.jface.preference.ipreferencestore;import Mydesigner. workflowactivator;/** * Class used to initialize default preference values. * Initialization of Preferences */public class Workflowpreferenceinitializer extends Abstractpreferenceinitializer {/* * (NON-JAVADOC) * * @see Org.eclipse.core.runtime.preferences.abstractpreferenceinitializer#initializedefaultpreferences () */public void Initializedefaultpreferences () {Ipreferencestore store = Workflowactivator.getdefault (). Getpreferencestore (); Store.setdefault (Workflowpreferenceconstants.p_boolean, True); Store.setdefault (workflowpreferenceconstants.p_ CHOICE, "Choice2"); Store.setdefault (workflowpreferenceconstants.p_string, "Default value"); Store.setdefault ( Workflowpreferenceconstants.user_name, "admin"); Store.setdefault (Workflowpreferenceconstants.password, "123456") ;//The initial value on the page}}
Workflowpreferenceconstants The class defines a constant in the preference item
public class Workflowpreferenceconstants {public static final string p_path = "Pathpreference";p ublic static final string P_boolean = "Booleanpreference";p ublic static final String p_choice = "Choicepreference";p ublic static final string P_stri NG = "Stringpreference";p ublic static final String user_name = "UserName"; public static final String PASSWORD = "PASSWORD";}
Workflowbasepreferencepage This class defines the Workflow page in the preferences
Import Mydesigner. Workflowactivator;import Org.eclipse.jface.preference.ipreferencestore;import Org.eclipse.jface.preference.preferencepage;import Org.eclipse.swt.swt;import Org.eclipse.swt.layout.GridData; Import Org.eclipse.swt.layout.gridlayout;import Org.eclipse.swt.widgets.button;import Org.eclipse.swt.widgets.composite;import Org.eclipse.swt.widgets.control;import Org.eclipse.swt.widgets.Label; Import Org.eclipse.swt.widgets.text;import Org.eclipse.ui.iworkbench;import Org.eclipse.ui.iworkbenchpreferencepage;import com.workflow.preferences.workflowpreferenceconstants;/** * Workflow page in Preferences * @author LWW * */public class Workflowbasepreferencepage extends Preferencepage implements Iworkbenchpre Ferencepage{private text userName;//user name private Text password;//Password Box public workflowbasepreferencepage () {super (); Setpreferencestore (Workflowactivator.getdefault (). Getpreferencestore ()); SetDescription ("This is a workflowBase Preferencepage! ");} @Overridepublic void Init (Iworkbench Workbench) {}//This method is the method that must be implemented to create various controls on the page in this method @Overrideprotected control createcontents (Composite parent) {Composite Composite = new Comp Osite (parent, SWT.) NONE); Composite.setlayout (New GridLayout (2, false)); Gets the Preferencestore object that holds this page Ipreferencestore Preferencestore = Getpreferencestore (); New Label (composite, SWT. left). SetText ("Login user name:"); UserName = new Text (composite, SWT. BORDER); Username.setlayoutdata (New Griddata (griddata.fill_horizontal)); Set the user name to the value saved in the file Username.settext (preferencestore.getstring (workflowpreferenceconstants.user_name)); New Label (composite, SWT. left). SetText ("Login password:"); Password = new Text (composite, SWT. BORDER); Password.setechochar (' * '); Set Password with * Display Password.setlayoutdata (new Griddata (griddata.fill_horizontal)); Set the password to the value saved in the file Password.settext (preferencestore.getstring (Workflowpreferenceconstants.password)); return composite; }/* * Overrides a method in the parent class, but calls the method when the Restore Defaults button is clicked */protected void Performdefaults () {Ipreferencestore preferencestorE = Getpreferencestore (); Username.settext (preferencestore.getdefaultstring (workflowpreferenceconstants.user_name)); Password.settext (preferencestore.getdefaultstring (Workflowpreferenceconstants.password)); }/* Overrides the method in the parent class, but the method is called when the Apply button is clicked */public Boolean Performok () {Ipreferencestore preferencestore = Getpreferencestore () ; if (userName! = null) Preferencestore.setvalue (Workflowpreferenceconstants.user_name, Username.gettext ()); if (password! = null) Preferencestore.setvalue (Workflowpreferenceconstants.password, Password.gettext ()); return true; } @Override//used to extend its own button protected void Contributebuttons (Composite parent) {//super.contributebuttons (parent); Button bt1 = New button (parent, SWT. NONE); Bt1.settext ("button One"); ((GridLayout) parent.getlayout ()). numcolumns++; Button BT2 = New button (parent, SWT. NONE); Bt2.settext ("button II"); ((GridLayout) parent.getlayout ()). numcolumns++; } }
Dbpreferencepage This class defines the preference page for DB
Import Org.eclipse.jface.preference.*;import Org.eclipse.ui.iworkbenchpreferencepage;import Org.eclipse.ui.iworkbench;import Com.workflow.preferences.workflowpreferenceconstants;import Mydesigner. Workflowactivator;public class Dbpreferencepageextends Fieldeditorpreferencepageimplements iworkbenchpreferencepage {public dbpreferencepage () {super (GRID); Setpreferencestore (Workflowactivator.getdefault ( ). Getpreferencestore ()); SetDescription ("a demonstration of a preference page implementation");} /** * creates the field editors. Field editors is abstractions of * the common GUI blocks needed to manipulate various types * of preferences. Each field editor knows what to save and * restore itself. */public void Createfieldeditors () {AddField (new Directoryfieldeditor (Workflowpreferenceconstants.p_path, & Directory preference: ", getfieldeditorparent ())); AddField (New Booleanfieldeditor (workflowpreferenceconstants.p_ Boolean, "&an example of a Boolean preference", Getfieldeditorparent ())); AddfiEld (New Radiogroupfieldeditor (Workflowpreferenceconstants.p_choice, "an example of a multiple-choice preference", 1, New string[][] {{"&choice 1", "Choice1"}, {"C&hoice 2", "Choice2"}}, Getfieldeditorparent ())); AddField (New Str Ingfieldeditor (workflowpreferenceconstants.p_string, "A &text preference:", getfieldeditorparent ()));} /* (non-javadoc) * @see org.eclipse.ui.iworkbenchpreferencepage#init (org.eclipse.ui.IWorkbench) */public void Init ( Iworkbench Workbench) {}}
Execution results
The value you set is saved to the
File is generated in runtime-mydesigner.product\.metadata\.plugins\org.eclipse.core.runtime\.settings
Mydesigner.prefs (Mydesigner is the current plug-in name)
To read the values in the file:
Gets the value in the preference Ipreferencestore store = Workflowactivator.getdefault (). Getpreferencestore (); System.out.println ("User name:" + store.getstring (workflowpreferenceconstants.user_name)); System.out.println ("Password:" + store.getstring (Workflowpreferenceconstants.password));//initial value on the page