本文学习参照文章“Preferences in the Eclipse Workbench UI”,除了实现流程外,更重要的是对例子中代码的学习。
在Eclipse中,选择window->preferences后打开preferences窗口,这个窗口是Eclipse所有设置项的集中地,同时也是我们自定义的插件进行设置的窗口。
第一章:Preference概述
Preference是一种数据,它存在于工作空间中,存储的是Eclipse工作的状态,使之保持一致性。Eclipse2.0提供了两种preference:UI preferences (the same as in 1.0) and Core Preferences。Preference并不涉及到当前工作空间中的任何资源,它应该被编辑器,视图或者其它的操作资源的对象所使用。一种资源实例对应的数据最好存储成一个属性。Preference对任何插件来说都是可用的,当然,先决条件是存在插件。通常的方法是你的插件上为你觉得可能被其他插件用到的Preference提供API, Preference的值存储在工作空间的.metadata/.plugins目录下面。
每个插件都有自己的preference store(存储区),这是工作空间提供的。在下面的例子中(这个例子是在文件中查找错误的单词),我们将定义一个插件,使用它的preference store存储preference。在UI中当要使用插件的时候我们定义AbustractUIPlugin的子类。构造函数将在工作台中创建一个独立的允许容易访问的插件实例。我们实现方法initializeDefaultPrefere
所有的preference应该都有默认值来保证在任何时候都有值。默认值也保证了UI能够提供一种方式重置preference的值回到初始设定,这是通过“Restore”button实现的。Preference的默认值在插件中也应该进行初始化,以便它在任何UI创建之前设置。
第二章:示例实现
public class BadWordCheckerPlugin extends AbstractUIPlugin {
} |
在xml中配置
<extension point="org.eclipse.ui.preferencePages"> </extension> |
是在页面中显示的名字, 是参数配置页面类,这个类必须实现IWorkbenchPreferencePage
第二个类:reference Page
这个页面只是一个用JFace实现的简单页面,Eclipse中有个PreferencePage类—它实现了Preference 页面需要的大多数API,当点下确定键后,参数对话框的信息就会被保存。代码如下:
package org.eclipse.ui.articles.badwordchecker; import org.eclipse.jface.preference.ColorFieldEditor; 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.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPreferencePage public class BadWordsColorPreferenceP } |
运行效果如下图:
第三个类:ad Words Preference Page
下面将要用一个更复杂些的组件作为preference,并使它存储在preference store且在preference页面中可编辑。例子是:添加String数组的bad words preference。
由于PreferenceConverter没有String数组的API,我们在BadWordCheckerPlugin中来实现。添加如下代码:
public String[] getDefaultBadWordsPrefer
} public String[] getBadWordsPreference() {
} private String[] convert(String preferenceValue) {
} public void setBadWordsPreference(String[] elements) {
} |
BadWordsPreferencePage主要是实现界面和两个按键的响应。代码如下(可以仔细研究):
package org.eclipse.ui.articles.badwordchecker; import org.eclipse.jface.preference.PreferencePage; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.*; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPreferencePage public class BadWordsPreferencePage (badWordList.getItems()); } |
运行结果如下:
第四:对参数值的应用
通常,一个preference是向一个打开的editor或者view进行传值。这时,就需要通过IPropertyChangeListener对变化添加监听。IPropertyChangeListener类是对IProperyStore添加监听,当用户点击OK或者Apply时,监听会被触发。
在这个例子中,我们实现了一个视图,用选定的颜色显示存在的bad words,这个视图通过IPropertyChangeListener更新显示的颜色。
}; public void init(IViewSite site) throws PartInitException {
public void dispose() {
} |
实现效果如下:
具体类实现代码如下(好多代码还不懂,有待于学习):
package org.eclipse.ui.articles.badwordchecker; import java.io.IOException; import java.io.InputStream; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.jface.preference.PreferenceConverter; import org.eclipse.jface.util.IPropertyChangeListener; import org.eclipse.jface.util.PropertyChangeEvent; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyleRange; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.ISelectionListener; import org.eclipse.ui.IViewSite; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.PartInitException; import org.eclipse.ui.part.ViewPart; public class BadWordsView extends ViewPart { } |