20.7 加入首选项(preferencePages)
选择主菜单“窗口→首选项”命令打开“首选项”窗口,如图20.8所示。此窗口是Eclipse设置项的集中营,第三方插件也应将自己的设置加入到此窗口。图20.8中左边可以看到本节将要实现的myplugin插件的设置树。

图20.8 “首选项”窗口
20.7.1 修改plugin.xml文件,设置首选项的扩展点
打开plugin.xml文件的编辑框,将如下代码块插入到最后一行的</plugin>项之前:
<extension point="org.eclipse.ui.preferencePages">
<page
name="myplugin插件设置"
class="cn.com.chengang.myplugin.RootPreferencePage"
id="cn.com.chengang.myplugin.RootPreferencePage">
</page>
<page
name="DB数据库"
category="cn.com.chengang.myplugin.RootPreferencePage"
class="cn.com.chengang.myplugin.DBPreferencePage"
id="cn.com.chengang.myplugin.DBPreferencePage">
</page>
</extension>
代码说明:
● org.eclipse.ui.preferencePages是首选项的扩展点。
● name是首选项的树结点显示的名称。
● class是首选项的树结点所对应的类(还没编写,下一步将完成此类)。
● id是首选项的树结点标识,建议设置成和class一样的名称。
● category是父结点的id标识。当然,父结点要存在才行。
20.7.2 建立首选项各结点对应的类
在plugin.xml中提前设置了首选项结点对应的RootPreferencePage和DBPreferencePage类后,本小节就来在包cn.com.chengang.myplugin中创建这两个类(如下代码所示)。首选项的类必须继承PreferencePage抽象类并实现IWorkbenchPreferencePage接口,该接口只有一个init方法,抽象类则有一些“首选项”窗口固有按钮的处理方法需要被实现。
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;
}
}
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 = "glchengang";
public static final String PASSWORD_DEFAULT = "12345678";
// 定义三个文本框
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接口的方法,当3个文本框中发生修改时将执行此方法
// 方法中对输入值进行了验证并将“确定”、“应用”两按钮使能
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());
}
}
20.7.3 运行插件
运行插件后,打开新Eclipse环境的“首选项”窗口,选择“DB数据库”选项,将其中的密码删除后可得到如图20.9所示的出错效果。

图20.9 首选项出错的效果图
20.7.4 总结
本实例的核心是IPreferenceStore对象的使用,用它的getString方法来取值、setValue方法来存值。其次和以前的事件代码写法有所不同的是:本类实现了ModifyListener接口,也成为了一个监听器,这样在各文本框的加入监听器的代码就会简洁很多,不过其事件代码必须保证3个文本框可以共用才行。






