Eclipse RCP 中建立自訂喜好設定,並能讀取喜好設定中的值

來源:互聯網
上載者:User

標籤:eclipse rcp   preference   喜好設定設定儲存   

Eclipse RCP的外掛程式中若想自己定義喜好設定需要擴充擴充點:

org.eclipse.core.runtime.preferences //該擴充點用於初始化喜好設定中的值

org.eclipse.ui.preferencePages//該擴充點用於定義自己的喜好設定頁面


plugin.xml中內容如:


Database Preferences掛在WorkFlowBase下,需要在category中填寫workFlowBase的ID


WorkFlowPreferenceInitializer類,用於初始化喜好設定中的值

import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;import org.eclipse.jface.preference.IPreferenceStore;import mydesigner.WorkFlowActivator;/** * Class used to initialize default preference values. * 喜好設定的初始化 */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");//頁面上的初始值}}
WorkFlowPreferenceConstants 該類定義了喜好設定中的常量

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該類定義了喜好設定中的workFlow頁面

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頁面 * @author lww * */public class WorkFlowBasePreferencePage extends PreferencePage implements IWorkbenchPreferencePage{private Text userName; //使用者名稱private Text password; //密碼框public WorkFlowBasePreferencePage() {super();setPreferenceStore(WorkFlowActivator.getDefault().getPreferenceStore());setDescription("This is a workflowBase PreferencePage!");}@Overridepublic void init(IWorkbench workbench) {}//該方法為必須實現的方法,在此方法中建立頁面上的各種控制項 @Overrideprotected Control createContents(Composite parent) {   Composite composite = new Composite(parent, SWT.NONE);    composite.setLayout(new GridLayout(2, false));    //擷取儲存此頁面的PreferenceStore對象    IPreferenceStore preferenceStore = getPreferenceStore();    new Label(composite, SWT.LEFT).setText("登入使用者名稱:");    userName = new Text(composite, SWT.BORDER);    userName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));    //設定使用者名稱為儲存在檔案中的值    userName.setText(preferenceStore.getString(WorkFlowPreferenceConstants.USER_NAME));    new Label(composite, SWT.LEFT).setText("登入密碼:");    password = new Text(composite, SWT.BORDER);    password.setEchoChar('*'); //設定密碼用*顯示   password.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));    //設定密碼為儲存在檔案中的值    password.setText(preferenceStore.getString(WorkFlowPreferenceConstants.PASSWORD));    return composite; }/* * 覆蓋父類中的方法,但單擊“恢複預設值”按鈕時調用該方法 */ protected void performDefaults() {    IPreferenceStore preferenceStore = getPreferenceStore();    userName.setText( preferenceStore.getDefaultString(WorkFlowPreferenceConstants.USER_NAME));    password.setText( preferenceStore.getDefaultString(WorkFlowPreferenceConstants.PASSWORD)); } /* * 覆蓋父類中的方法,但單擊“應用”按鈕時調用該方法 */ 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//用於擴充自己的按鈕protected void contributeButtons(Composite parent) {    // super.contributeButtons(parent);    Button bt1 = new Button(parent, SWT.NONE);    bt1.setText("按鈕一");    ((GridLayout) parent.getLayout()).numColumns++;    Button bt2 = new Button(parent, SWT.NONE);    bt2.setText("按鈕二");    ((GridLayout) parent.getLayout()).numColumns++; } }
DBPreferencePage該類定義了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 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) {}}
執行結果


設定的值會儲存到
runtime-myDesigner.product\.metadata\.plugins\org.eclipse.core.runtime\.settings中會組建檔案

MyDesigner.prefs(MyDesigner是當前的外掛程式名)

若要讀取該檔案中的值:

//擷取喜好設定中的值IPreferenceStore store = WorkFlowActivator.getDefault().getPreferenceStore();System.out.println("使用者名稱:" + store.getString(WorkFlowPreferenceConstants.USER_NAME));System.out.println("密碼:" + store.getString(WorkFlowPreferenceConstants.PASSWORD));//頁面上的初始值



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.