您的位置:首页 > 编程语言 > Java开发

关于Eclipse插件开发-----加入首选项(preferencePages)

2014-12-20 19:31 369 查看
选择主菜单"窗口---->首选项"命令打开"首选项"窗口.此窗口是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从入门到精通
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐