关于Eclipse插件开发-----加入首选项(preferencePages)
选择主菜单"窗口---->首选项"命令打开"首选项"窗口.此窗口是Eclipse设置项的集中营,

修改plugin.xml文件,设置首选项的扩展点:
plug.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension point="org.eclipse.ui.perspectives">
<perspective
name="myplugin 透视图"
icon="icons/selectall.gif"
class="cn.com.kxh.myplugin.SamplePerspective"
id="cn.com.kxh.myplugin.SamplePerspective">
</perspective>
</extension>
<extension point="org.eclipse.ui.views">
<view
name="视图1"
icon="icons/prev.gif"
category="com.glkxh.myplugin.view"
class="cn.com.kxh.myplugin.View1"
id="cn.com.kxh.myplugin.View1">
</view>
<view
name="视图2"
icon="icons/project.gif"
category="com.glkxh.myplugin.view"
class="cn.com.kxh.myplugin.View2"
id="cn.com.kxh.myplugin.View2">
</view>
</extension>
<extension point="org.eclipse.ui.editors">
<editor
name="中国Editor"
icon="icons/project.gif"
class="cn.com.kxh.myplugin.ChinaEditor"
id="cn.com.kxh.myplugin.ChinaEditor">
</editor>
<editor
name="美国Editor"
icon="icons/prev.gif"
class="cn.com.kxh.myplugin.UsaEditor"
id="cn.com.kxh.myplugin.UsaEditor">
</editor>
<editor
name="法国Editor"
icon="icons/remove.gif"
class="cn.com.kxh.myplugin.FranceEditor"
id="cn.com.kxh.myplugin.FranceEditor">
</editor>
</extension>
<extension point="org.eclipse.ui.preferencePages">
<page
name="myplugin插件设置"
class="cn.com.kxh.myplugin.RootPreferencePage"
id="cn.com.kxh.myplugin.RootPreferencePage">
</page>
<page
name="DB数据库"
category="cn.com.kxh.myplugin.RootPreferencePage"
class="cn.com.kxh.myplugin.DBPreferencePage"
id="cn.com.kxh.myplugin.DBPreferencePage">
</page>
</extension>
</plugin>
代码说明:
1.org.eclipse.ui.preferencePages 是首选项(Preference)的扩展点
2.name是首选项的树节点显示的名称.
3.class是首选项的树节点所对应的类(还没编写,下一步将完成此类)
4.id是首选项的树节点标识.建议设置成和class一样的名称.
5.category是父节点的id标识,当然,父节点要存在才行.
建立首选项对应的类
在上面的plugin.xml文件中已经定义的两个类.
cn.com.kxh.myplugin.RootPreferencePage和cn.com.kxh.myplugin.DBPreferencePage
首选项的类必须继承PreferencePage抽象类并实现IWorkbenchPreferencepage接口.该接口只有一个init方法,抽象类中则有一些"首选项"窗口固有按钮的处理方法需要被实现.
RootPreferencePage.java
public class RootPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
public void init(IWorkbench workbench) {}
protected Control createContents(Composite parent) {
Composite topComp = new Composite(parent, SWT.NONE);
topComp.setLayout(new RowLayout());
new Label(topComp, SWT.NONE).setText("欢迎使用myplugin插件");
return topComp;
}
}
DBPreferencePage.java
public class DBPreferencePage extends PreferencePage implements IWorkbenchPreferencePage, ModifyListener {
// 为文本框定义三个键值
public static final String URL_KEY = "$URL_KEY";
public static final String USERNAME_KEY = "$USERNAME_KEY";
public static final String PASSWORD_KEY = "$PASSWORD_KEY";
// 为文本框值定义三个默认值
public static final String URL_DEFAULT = "jdbc:db2://127.0.0.1/mydb";
public static final String USERNAME_DEFAULT = "kongxiaohan";
public static final String PASSWORD_DEFAULT = "kxhkxhkxhkxh";
// 定义三个文本框
private Text urlText, usernameText, passwordText;
// 定义一个IPreferenceStore对象
private IPreferenceStore ps;
// 接口IWorkbenchPreferencePage的方法,它负责初始化。在此方法中设置一个
// PreferenceStore对象,由此对象提供文本框值的读入/写出方法
public void init(IWorkbench workbench) {
setPreferenceStore(Activator.getDefault().getPreferenceStore());
}
// 父类的界面创建方法
protected Control createContents(Composite parent) {
Composite topComp = new Composite(parent, SWT.NONE);
topComp.setLayout(new GridLayout(2, false));
// 创建三个文本框及其标签
new Label(topComp, SWT.NONE).setText("URL:");
urlText = new Text(topComp, SWT.BORDER);
urlText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
new Label(topComp, SWT.NONE).setText("用户名:");
usernameText = new Text(topComp, SWT.BORDER);
usernameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
new Label(topComp, SWT.NONE).setText("密码:");
passwordText = new Text(topComp, SWT.BORDER | SWT.PASSWORD);
passwordText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
// 取出以前保存的值,并设置到文本框中。如果取出值为空值或空字串,则填入默认值。
ps = getPreferenceStore();// 取得一个IPreferenceStore对象
String url = ps.getString(URL_KEY);
if (url == null || url.trim().equals(""))
urlText.setText(URL_DEFAULT);
else
urlText.setText(url);
String username = ps.getString(USERNAME_KEY);
if (username == null || username.trim().equals(""))
usernameText.setText(USERNAME_DEFAULT);
else
usernameText.setText(username);
String password = ps.getString(PASSWORD_KEY);
if (password == null || password.trim().equals(""))
passwordText.setText(PASSWORD_DEFAULT);
else
passwordText.setText(password);
// 添加事件监听器。this代表本类,因为本类实现了ModifyListener接口成了监听器
usernameText.addModifyListener(this);
passwordText.addModifyListener(this);
urlText.addModifyListener(this);
return topComp;
}
// 实现自ModifyListener接口的方法,当三个文本框中发生修改时将执行此方法。
// 方法中对输入值进行了验证并将“确定”、“应用”两按钮使能
public void modifyText(ModifyEvent e) {
String errorStr = null;// 将原错误信息清空
if (urlText.getText().trim().length() == 0) {
errorStr = "URL不能为空!";
} else if (usernameText.getText().trim().length() == 0) {
errorStr = "用户名不能为空!";
} else if (passwordText.getText().trim().length() == 0) {
errorStr = "密码不能为空!";
}
setErrorMessage(errorStr);// errorStr=null时复原为正常的提示文字
setValid(errorStr == null);// “确定”按钮
getApplyButton().setEnabled(errorStr == null);// “应用”按钮
}
// 父类方法。单击“复原默认值”按钮时将执行此方法,取出默认值设置到文本框中
protected void performDefaults() {
urlText.setText(URL_DEFAULT);
usernameText.setText(USERNAME_DEFAULT);
passwordText.setText(PASSWORD_DEFAULT);
}
// 父类方法。单击“应用”按钮时执行此方法,将文本框值保存并弹出成功的提示信息
protected void performApply() {
doSave(); // 自定义方法,保存设置
MessageDialog.openInformation(getShell(), "信息", "成功保存修改!");
}
// 父类方法。单击“确定”按钮时执行此方法,将文本框值保存并弹出成功的提示信息
public boolean performOk() {
doSave();
MessageDialog.openInformation(getShell(), "信息", "修改在下次启动生效");
return true; // true表示成功退出
}
// 自定义方法。保存文本框的值
private void doSave() {
ps.setValue(URL_KEY, urlText.getText());
ps.setValue(USERNAME_KEY, usernameText.getText());
ps.setValue(PASSWORD_KEY, passwordText.getText());
}
}
运行结果:
将其中的密码删除之后得到下面的提示效果.

这个例子中的核心是IPreferenceStroe对象的使用,用它的getString方法来取值,setValue方法来存值.其次和以前的事件代码写法有所不同的是:本类实现了ModifyListener接口,也成为了一个监听器,这样在各文本框的加入监听器的代码就会简洁很多,不过其事件代码必须保证3个文本框可以共用才行.
此外还用的其他的程序文件.
Activator.java
/**
* The activator class controls the plug-in life cycle
*/
public class Activator extends AbstractUIPlugin { // The plug-in ID
public static final String PLUGIN_ID = "cn.com.kxh.myplugin"; //$NON-NLS-1$ // The shared instance
private static Activator plugin; /**
* The constructor
*/
public Activator() {
plugin = this;
} /**
* Returns the shared instance
*
* @return the shared instance
*/
public static Activator getDefault() {
return plugin;
} /*
* (non-Javadoc)
* @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
*/
@Override
public void start(BundleContext context) throws Exception {
super.start(context);
} /*
* (non-Javadoc)
* @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
*/
@Override
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
} public static ImageDescriptor getImageDescriptor(String path) {
return imageDescriptorFromPlugin(PLUGIN_ID, path);
} }
Messages.java
public class Messages {
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle ("cn.com.kxh.myplugin.messages");
public static String getString(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
}
messages.properties
LanguageDialog.ok=OK
LanguageDialog.remove=Remove
另外,我在调试程序的时候有一个地方老是报空指针NPE的错误.
最后查到其实是Activator.java这个类要在MANIFEST.MF这个文件中注册正确才行.
MANIFEST.MF
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Myplugin插件
Bundle-SymbolicName: cn.com.kxh.myplugin;singleton:=true
Bundle-Version: 1.0.1
Bundle-Activator: cn.com.kxh.myplugin.Activator
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Bundle-Vendor: Eclipse从入门到精通
关于Eclipse插件开发-----加入首选项(preferencePages)的更多相关文章
- Eclipse RCP 中创建自己定义首选项,并能读取首选项中的值
Eclipse RCP的插件中若想自定义首选项须要扩展扩展点: org.eclipse.core.runtime.preferences //该扩展点用于初始化首选项中的值 org.eclipse.u ...
- Eclipse 首选项(Preferences)
Eclipse 首选项(Preferences) 设置首选项 该对话框可通过框架管理但是其他插件可以设置其他页面来管理首选项的配置. 我们可以通过 Window 菜单选择 Preferences 菜单 ...
- Eclipse 中的 parameter参数,property属性,preference首选项 区别
parameter参数 1.配置框架 web.xml <init-param> <param-name>contextConfigLocation</param-name ...
- 【插件开发】—— 1 Eclipse插件开发导盲
[插件开发]—— 1 Eclipse插件开发导盲 在真正接触eclipse插件开发一个月后,对插件的开发过程以及技术要求,也有了一定的了解.遥想之前像无头苍蝇一样乱撞乱学,真心觉得浪费了不少时间. ...
- Eclipse 插件开发 -- 深入理解菜单(Menu)功能及其扩展点( FROM IBM)
Eclipse 插件开发 -- 深入理解菜单(Menu)功能及其扩展点 菜单是各种软件及开发平台会提供的必备功能,Eclipse 也不例外,提供了丰富的菜单,包括主菜单(Main Menu),视图 / ...
- 【eclipse插件开发实战】Eclipse插件开发1——eclipse内核结构、扩展点机制
Eclipse插件开发实战1--eclipse内核结构.扩展点机制 一.前言 本系列总体介绍eclipse插件开发基本理论.插件项目结构及开发步骤,最后再给出两个插件开发实例. 总体安排结构如下: 1 ...
- eclipse中没有server选项无法配置Tomcat
eclipse集成Tomcat: 打开eclipse - 窗口 - 首选项 - 服务器 - 运行时环境 找到Tomcat然后添加. eclipse添加插件: 开发WEB项目时要集成Tomcat可以并不 ...
- Eclipse插件开发中对于Jar包和类文件引用的处理(彻底解决插件开发中的NoClassDefFoundError问题)(转)
目的:Eclipse插件开发中,经常要引用第三方包或者是引用其他插件中的类,由于插件开发环境引用类路径的设置和运行平台引用类路径的设置不同,经常导致开发过程OK,一旦运行则出现NoClassDefFo ...
- mysql数据库管理工具sqlyog在首选项里可以设置默认查询分页条数和字体,改写关键字大小写
sqlyog设置一直习惯用sqlyog来管理mysql数据库,但有三个地方用得不是很爽:1.默认查询条数只有1000条经常需要勾选掉重新查询.2.自动替换关键字大小写,有时候字段名为关键字的搞成大写的 ...
随机推荐
- 反编译c#的相关问题
最近硬盘坏掉了,有一个项目没有备份,只好用Exe 文件反编译出来用,查了一下相关的文章用到的工具如下: ILSpy_Master_2.1.0.1603_RTW_Binaries 直接生成时,有些奇怪, ...
- Ajax解决缓存的5种方法
原文:http://www.ido321.com/129.html 1.在ajax发送请求前加上 anyAjaxObj.setRequestHeader(“If-Modified-Since”,”0″ ...
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
- uvalive 4513 Stammering Aliens
题意:给你一个串,问期中至少出现m次的最长子串及其起始位置的坐标. 思路:hash+LCP+二分答案 #include<cstdio> #include<cstring> #i ...
- 【转载】strlen与sizeof区别
自己小结: sizeof使用时,若是数组变量,则是数组变量占的大小 char a[10]; sizeof(a)=10 若是指针,则为指针大小,数组变量作为函数参数传递时,会退化成指针,且函数内是不知道 ...
- Python读取文件内容的三种方式并比较
本次实验的文件是一个60M的文件,共计392660行内容. 程序一: def one(): start = time.clock() fo = open(file,'r') fc = fo.readl ...
- BNUOJ-26580 Software Bugs KMP匹配,维护
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26580 题意:给一个模式串,然后m个匹配串,要求删掉匹配串中的所有存在的模式串,使得余下的 ...
- HDU-4669 Mutiples on a circle 环形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4669 题意:给一串数字连乘一个环,求连续的子串中组成的新的数字能被K整除的个数. 首先容易想到用DP来 ...
- .NET ORM框架(一)
最近做项目自己整理了一个ORM框架,分享给大家看看,有很多不足望大家指出. 下面是使用方法 BLL 主要方法 逻辑层:子类继承父类, 直接用BASE调用 ManagerBLL 中的方法. public ...
- fastjson把对象转化成json避免$ref
转自http://blog.csdn.net/wxwzy738/article/details/30244993 DisableCircularReferenceDetect来禁止循环引用检测: JS ...