java插件获取首选项_Eclipse RCP 中创建自定义首选项,并能读取首选项中的值
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!");
}
@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) {
}
}执行结果如图:
设置的值会保存到
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.csdn.net/luoww1/article/details/34425305
java插件获取首选项_Eclipse RCP 中创建自定义首选项,并能读取首选项中的值相关推荐
- NI Multisim元件库:在Multisim中创建自定义元器件
转载于: http://www.ni.com/tutorial/3173/zhs/ 概览 「在Multisim中创建自定义元器件」与「在 NI Ultiboard中创建自定义元器件」为您提供了关于如何 ...
- hive 元数据 自定义_如何在Hive中创建自定义函数UDF及如何直接通过Impala的同步元数据重用UDF的jar文件-阿里云开发者社区...
如何在Hive中创建自定义函数UDF及使用 如何在Impala中使用Hive的自定义函数 UDF函数开发 使用Intellij工具开发Hive的UDF函数,进行编译: 1.使用Intellij工具通过 ...
- 如何在WordPress中创建自定义主页
Often users ask us if it's possible to create a custom homepage in WordPress. 用户经常问我们是否可以在WordPress中 ...
- 如何在ASP.NET Core中创建自定义AuthorizeAttribute?
本文翻译自:How do you create a custom AuthorizeAttribute in ASP.NET Core? I'm trying to make a custom aut ...
- 在OpenCart中创建自定义送货方式:第一部分
尽管OpenCart核心本身提供了许多有用的运送方法,但始终有机会需要创建自己的运送方法. 另一方面,作为一名Web开发人员,您将始终尝试探索自己选择的框架,以了解如何创建自己的自定义内容! 在本系列 ...
- 在Fritzing中创建自定义元件
topic 参考地址: 第1步:收集所有信息 图1. 数据表中的元件尺寸 图2. ML8511分线板 图3. 所需尺寸 第2步:面包板视图 图4. 主窗口 图5. 文件属性窗口 图6. 新矩形 图8. ...
- 在Fritzing中创建自定义元件(最终修改)
topic 在Fritzing中创建自定义元件 **软件** **第1步:收集所有信息** **第2步:面包板视图** **第3步:原理图视图** **第4步:PCB视图** **第5步:创建Frit ...
- Github上如何在组织中创建代码仓库,并如何授予该组织中某个小组权限?
如何在组织中创建代码仓库,并如何授予该组织中某个小组权限? 比如:在google这样的组织中,代码仓库应该属于组织所有的. 现在在组织中创建代码仓库,并且让开发小组能够访问到该组织新创建的代码仓库. ...
- 如何在android中创建自定义对话框?
本文翻译自:How to create a Custom Dialog box in android? I want to create a custom dialog box like below ...
- 微软word开机自启动_如何在Microsoft Word中创建自定义封面
微软word开机自启动 A great cover page draws in readers. If you use Microsoft Word, you're in luck, because ...
最新文章
- 程序员崩溃的40多个瞬间!!!太形象了,你遇到过几个?
- SQL Server查询备份日期和备份设备名
- Python语言pip升级python-augeas失败之解决办法
- Kettle使用_8 存储过程结合获取系统信息
- Screen Painter 程序设计
- Python学习笔记——Python的下载与安装
- angular复习笔记4-模板
- 安装 opencv ubuntu_Ubuntu18.04安装 OpenCV4.3.0
- 循环智能杨植麟:“人机耦合”将是对话语义应用的新趋势!
- 挂载、卸载、free查看内存情况、创建交换分区、回环设备、dd命令、自动挂载、fuser...
- php微信支付接口开发程序(一)
- 数独终盘生成器(调试成果)
- Mybatis 拦截器简述
- Matlab OpenEXR 打开exr格式图片
- XSS 存储漏洞修复
- R3Det: Refined Single-Stage Detector with Feature Refinement for Rotating Object
- Python实现对主要城市及其周边地区天气数据的爬取
- html5 画布绘制时钟
- 论文阅读:FASTEMIT: LOW-LATENCY STREAMING ASR WITH SEQUENCE-LEVEL EMISSION REGULARIZATION
- C#编程学习49:将数据写入到excel中
热门文章
- 计算机造句英语怎么说,计算的英文翻译是什么及如何造句
- Android基础——四大组件之Activity
- cadshx字体怎么安装_福利 | 关于PPT字体,你应该知道的几件事...字体包福利见文末...
- U-Boot 之三 U-Boot 源码文件解析及移植过程详解
- Linux 之四 Ubuntu 20.04 WiFi 无法使用、设置无法显示、远程桌面、SSH、Git、PPA、FFmpeg 等各问题记录
- 算法练习day10——190328(根据指定值划分单链表、复制含有rand指针节点的链表、两个单链表相交)
- C/Cpp / 条件编译
- 提示计算机未安装flash,大师应对安装了flash,但浏览器提示没安装,如何解决...
- java窗体设置最小宽度_flex web Application设置最小高度和宽度。
- mysql启动选项只读_MySQL的启动选项和系统变量该如何配置?