java插件获取首选项_Eclipse RCP 中创建自定义首选项,并能读取首选项中的值

Eclipse RCP的插件中若想自己定义首选项需要扩展扩展点:

org.eclipse.core.runtime.preferences //该扩展点用于初始化首选项中的值

org.eclipse.ui.preferencePages//该扩展点用于定义自己的首选项页面

dbb02af2458dae29b5f413ecab0c6fd2.bmp

plugin.xml中内容如:

7da6aa5cfc4bb05ecf00baa19462ed64.bmp

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!");

}

@Override

public void init(IWorkbench workbench) {

}

//该方法为必须实现的方法,在此方法中创建页面上的各种控件

@Override

protected 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 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) {

}

}执行结果如图:

9b5901a3a43c56680d78378956e0fcc3.bmp

设置的值会保存到

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));//页面上的初始值

原文:http://blog.youkuaiyun.com/luoww1/article/details/34425305

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值