In Eclipse RCP plug-ins, you need to extend the extension point if you want to customize your preferences:
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:
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbhvvd3cx/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70 /gravity/southeast "/>
Database preferences is hung under Workflowbase. Need to fill in the category Workflowbase ID
Workflowpreferenceinitializer class. Used to initialize a value in a preference item
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"); / / initial value on page
}
}
Workflowpreferenceconstants The class defines a constant in the preference item
public class WorkFlowPreferenceConstants {
public static final String P_PATH = "pathPreference";
public static final String P_BOOLEAN = "booleanPreference";
public static final String P_CHOICE = "choicePreference";
public static final String P_STRING = "stringPreference";
public 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 IWorkbenchPreferencePage{
private Text userName; //username
Private text password; / / password box
public WorkFlowBasePreferencePage() {
Super ();
setPreferenceStore(WorkFlowActivator.getDefault().getPreferenceStore());
setDescription("This is a workflowBase PreferencePage!");
}
@Override
public void init(IWorkbench workbench) {
}
//This is a method that must be implemented to create various controls on the page
@Override
protected Control createContents(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(2, false));
//Get the preferencestore object that holds this page
IPreferenceStore preferenceStore = getPreferenceStore();
New label (composite, SWT. Left). Settext ("login Username:");
userName = new Text(composite, SWT.BORDER);
userName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
//Set username 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 to display with *
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;
}
/ *
*Override methods in the parent class. But this method is called when you click the restore defaults button
* /
protected void performDefaults() {
IPreferenceStore preferenceStore = getPreferenceStore();
userName.setText( preferenceStore.getDefaultString(WorkFlowPreferenceConstants.USER_NAME));
password.setText( preferenceStore.getDefaultString(WorkFlowPreferenceConstants.PASSWORD));
}
/ *
*Override the method in the parent class, but call it when you click the Apply button
* /
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 / / to extend your own button
protected void contributeButtons(Composite parent) {
// super.contributeButtons(parent);
Button bt1 = new Button(parent, SWT.NONE);
BT1. Settext ("button I");
((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 DBPreferencePage
extends FieldEditorPreferencePage
implements IWorkbenchPreferencePage {
public DBPreferencePage() {
super(GRID);
setPreferenceStore(WorkFlowActivator.getDefault().getPreferenceStore());
setDescription("A demonstration of a preference page implementation");
}
/**
* Creates the field editors. Field editors are abstractions of
* the common GUI blocks needed to manipulate various types
* of preferences. Each field editor knows how 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 StringFieldEditor(WorkFlowPreferenceConstants.P_STRING, "A &text preference:", getFieldEditorParent()));
}
/* (non-Javadoc)
* @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
*/
public void init(IWorkbench workbench) {
}
}
Run 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 value in the file:
//Get values in preferences
IPreferenceStore store = WorkFlowActivator.getDefault().getPreferenceStore();
System.out.println("username:" + store.getString(WorkFlowPreferenceConstants.USER_NAME));
System. Out. Println ("password:" + store. GetString (workflowpreferenceconstants. Password)); / / initial value on the page
The corresponding Preferences dialog box pops up:
//Guide the user to the preferences configuration page
PreferenceManager manager = PlatformUI.getWorkbench().getPreferenceManager();
Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
PreferenceDialog pd = new PreferenceDialog(parentShell, manager);
PD. Setselectednode ("com. Workflow. Preferences. Page. Dbpreferencepage"); / / set the selected page (ID of page in org. Eclipse. UI. Preferencepages extension point)
Pd.open ();
There is no search filter bar in the dialog box.
There is also an implementation:
PreferencesUtil.createPreferenceDialogOn(new Shell(), "com.workflow.preferences.page.DBPreferencePage",
new String[]{"com.workflow.preferences.page.WorkFlowBasePreferencePage","com.workflow.preferences.page.DBPreferencePage"}, null).open();
The interface looks like the following:
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbhvvd3cx/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70 /gravity/southeast "/>
The above new string array is the main definition of which first page item node bar is displayed on the left, and if NULL, all the selected items are displayed.
Create your own definition preferences in Eclipse RCP and be able to read the values in preferences