在插件应用程序中,经常遇到这样的情况:当用户第一次应用该软件时需要作一些配置操作,以后用户再次使用该应用程序时,则按照以前的配置启动程序。这种配置操作一般发生在Preference(首选项)页面中,但也可在程序的其他地方,下面我们先看看在Preference页面中是怎样操作的。
1) 首先看一下该Preference页面的实际效果图:
2)实现首选项页面的主要代码SamplePreferencePage.java
package test.preference.preferences;
import org.eclipse.jface.preference.*;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.IWorkbench;
import test.preference.PreferencePlugin;
/**
* This class represents a preference page that
* is contributed to the Preferences dialog. By
* subclassing <samp>FieldEditorPreferencePage</samp>, we
* can use the field support built into JFace that allows
* us to create a page that is small and knows how to
* save, restore and apply itself.
* <p>
* This page is used to modify preferences only. They
* are stored in the preference store that belongs to
* the main plug-in class. That way, preferences can
* be accessed directly via the preference store.
*/
public class SamplePreferencePage
extends FieldEditorPreferencePage //继承FieldEditorPreferencePage类
implements IWorkbenchPreferencePage {//实现IWorkbenchPreferencePage接口
public SamplePreferencePage() {
super(GRID);
setPreferenceStore(PreferencePlugin.getDefault().getPreferenceStore());//设置该插件的PreferenceStore()
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(PreferenceConstants.P_PATH, //增加一个目录选择Filed
"&Directory preference:", getFieldEditorParent()));
addField(//增加一个check Filed
new BooleanFieldEditor(
PreferenceConstants.P_BOOLEAN,
"&An example of a boolean preference",
getFieldEditorParent()));
addField(new RadioGroupFieldEditor(//增加一个RadioGroupField
PreferenceConstants.P_CHOICE,
"An example of a multiple-choice preference",
1,
new String[][] { { "&Choice 1", "choice1" }, {
"C&hoice 2", "choice2" }
}, getFieldEditorParent()));
addField(//增加一个StringField
new StringFieldEditor(PreferenceConstants.P_STRING, "A &text preference:", getFieldEditorParent()));
}
/* (non-Javadoc)
* @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
*/
public void init(IWorkbench workbench) {
}
}
说明:在构造函数中的setPreferenceStore(PreferencePlugin.getDefault().getPreferenceStore())只是设置该插件的PreferenceStore,如果PreferenceStore为null则创建一个新的PreferenceStore。真正load以前的配置信息在FieldEditorPreferencePage的initialize()函数中
protected void initialize() {
if (fields != null) {
Iterator e = fields.iterator();
while (e.hasNext()) {
FieldEditor pe = (FieldEditor) e.next();
pe.setPage(this);
pe.setPropertyChangeListener(this);
pe.setPreferenceStore(getPreferenceStore());
pe.load();//加载以前的配置信息
}
}
}
Load()函数是每个FieldEditor对象都有的存储函数。
3)在具体分析之前,我们先看一下类的层次关系图:
4)页面中的其他四个按钮Restore Default,Apply,OK, Cancel为程序自动添加的。
其中Restore Default,Apply为 SamplePreferencePage的父类FieldEditorPreferencePage的父类PreferencePage所添加。其响应函数performDefaults() 和performApply() 也在 PreferencePage里有定义
OK, Cancel两个按钮则为整个PreferencePage所共有。
以下是摘自PreferencePage的部分源码:
//增加了defaultsButton及其响应函数performDefaults()
defaultsButton = new Button(buttonBar, SWT.PUSH);
defaultsButton.setText(labels[0]);
Dialog.applyDialogFont(defaultsButton);
GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
Point minButtonSize = defaultsButton.computeSize(SWT.DEFAULT,
SWT.DEFAULT, true);
data.widthHint = Math.max(widthHint, minButtonSize.x);
defaultsButton.setLayoutData(data);
defaultsButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
performDefaults();
}
});
//增加了applyButton及其响应函数performApply()
applyButton = new Button(buttonBar, SWT.PUSH);
applyButton.setText(labels[1]);
Dialog.applyDialogFont(applyButton);
data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
minButtonSize = applyButton.computeSize(SWT.DEFAULT, SWT.DEFAULT,
true);
data.widthHint = Math.max(widthHint, minButtonSize.x);
applyButton.setLayoutData(data);
applyButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
performApply();
}
});
applyButton.setEnabled(isValid());
说明:
1. 用户可以重写 performDefaults()函数,以实现自己特定的功能,但必须调用super.performDefaults()
2. performApply()函数的实现只是简单地调用performOk(),用户同样可以重写performOk(),但无需调用super. performOk();
3. 我们看到在FieldEditorPreferencePage中performOk()被重写
public boolean performOk() {
if (fields != null) {
Iterator e = fields.iterator();
while (e.hasNext()) {
FieldEditor pe = (FieldEditor) e.next();
pe.store();//存储
pe.setPresentsDefaultValue(false);
}
}
return true;
}
注意调用各个FieldEditor的store函数来存储配置。
5)如何在插件的其他部分访问该配置信息
新建一个org.eclipse.ui.popupMenus的扩展点,实现功能为:右键一个plugin.xml会弹出一个菜单项New Submenu,单击会弹出一个消息框,显示用户在Preference页面中所设置的目录值
效果图:
关键性的代码是:
Shell shell = new Shell();
IPreferenceStore pstore= PreferencePlugin.getDefault().getPreferenceStore();//取得PreferencePlugin的PreferenceStore引用
String path = "The path is "+pstore.getString(PreferenceConstants.P_PATH);//取得键为PreferenceConstants.P_PATH所对应的value
MessageDialog.openInformation(
shell,
"Preference Plug-in",
path);
或者
Shell shell = new Shell();
Preferences preferences = PreferencePlugin.getDefault().getPluginPreferences();
String path = preferences.getString(PreferenceConstants.P_PATH);
MessageDialog.openInformation(
shell,
"Preference Plug-in",
path);
补充:
1. 如果用户的首选项页面继承自PreferencePage而不是FieldEditorPreferencePage则需要重写performOk()函数
public boolean performOk()
{
Preferences preferences = TauPlugin.getDefault().getPluginPreferences();
preferences.setValue("numProc", spin.getSelection());
preferences.setValue("TAUCDTArchPath",tauArch.getText());
TauPlugin.getDefault().savePluginPreferences();//保存配置
return true;
}
自己写一个加载配置的函数,
private void loadSaved()
{
Preferences preferences = TauPlugin.getDefault().getPluginPreferences();
int numMachines = preferences.getInt("numProc");
if(numMachines < 1) numMachines = 1;
spin.setSelection(numMachines);
tauArch.setText(preferences.getString("TAUCDTArchPath"));
}
1) 首先看一下该Preference页面的实际效果图:
2)实现首选项页面的主要代码SamplePreferencePage.java
package test.preference.preferences;
import org.eclipse.jface.preference.*;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.IWorkbench;
import test.preference.PreferencePlugin;
/**
* This class represents a preference page that
* is contributed to the Preferences dialog. By
* subclassing <samp>FieldEditorPreferencePage</samp>, we
* can use the field support built into JFace that allows
* us to create a page that is small and knows how to
* save, restore and apply itself.
* <p>
* This page is used to modify preferences only. They
* are stored in the preference store that belongs to
* the main plug-in class. That way, preferences can
* be accessed directly via the preference store.
*/
public class SamplePreferencePage
extends FieldEditorPreferencePage //继承FieldEditorPreferencePage类
implements IWorkbenchPreferencePage {//实现IWorkbenchPreferencePage接口
public SamplePreferencePage() {
super(GRID);
setPreferenceStore(PreferencePlugin.getDefault().getPreferenceStore());//设置该插件的PreferenceStore()
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(PreferenceConstants.P_PATH, //增加一个目录选择Filed
"&Directory preference:", getFieldEditorParent()));
addField(//增加一个check Filed
new BooleanFieldEditor(
PreferenceConstants.P_BOOLEAN,
"&An example of a boolean preference",
getFieldEditorParent()));
addField(new RadioGroupFieldEditor(//增加一个RadioGroupField
PreferenceConstants.P_CHOICE,
"An example of a multiple-choice preference",
1,
new String[][] { { "&Choice 1", "choice1" }, {
"C&hoice 2", "choice2" }
}, getFieldEditorParent()));
addField(//增加一个StringField
new StringFieldEditor(PreferenceConstants.P_STRING, "A &text preference:", getFieldEditorParent()));
}
/* (non-Javadoc)
* @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
*/
public void init(IWorkbench workbench) {
}
}
说明:在构造函数中的setPreferenceStore(PreferencePlugin.getDefault().getPreferenceStore())只是设置该插件的PreferenceStore,如果PreferenceStore为null则创建一个新的PreferenceStore。真正load以前的配置信息在FieldEditorPreferencePage的initialize()函数中
protected void initialize() {
if (fields != null) {
Iterator e = fields.iterator();
while (e.hasNext()) {
FieldEditor pe = (FieldEditor) e.next();
pe.setPage(this);
pe.setPropertyChangeListener(this);
pe.setPreferenceStore(getPreferenceStore());
pe.load();//加载以前的配置信息
}
}
}
Load()函数是每个FieldEditor对象都有的存储函数。
3)在具体分析之前,我们先看一下类的层次关系图:
4)页面中的其他四个按钮Restore Default,Apply,OK, Cancel为程序自动添加的。
其中Restore Default,Apply为 SamplePreferencePage的父类FieldEditorPreferencePage的父类PreferencePage所添加。其响应函数performDefaults() 和performApply() 也在 PreferencePage里有定义
OK, Cancel两个按钮则为整个PreferencePage所共有。
以下是摘自PreferencePage的部分源码:
//增加了defaultsButton及其响应函数performDefaults()
defaultsButton = new Button(buttonBar, SWT.PUSH);
defaultsButton.setText(labels[0]);
Dialog.applyDialogFont(defaultsButton);
GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
Point minButtonSize = defaultsButton.computeSize(SWT.DEFAULT,
SWT.DEFAULT, true);
data.widthHint = Math.max(widthHint, minButtonSize.x);
defaultsButton.setLayoutData(data);
defaultsButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
performDefaults();
}
});
//增加了applyButton及其响应函数performApply()
applyButton = new Button(buttonBar, SWT.PUSH);
applyButton.setText(labels[1]);
Dialog.applyDialogFont(applyButton);
data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
minButtonSize = applyButton.computeSize(SWT.DEFAULT, SWT.DEFAULT,
true);
data.widthHint = Math.max(widthHint, minButtonSize.x);
applyButton.setLayoutData(data);
applyButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
performApply();
}
});
applyButton.setEnabled(isValid());
说明:
1. 用户可以重写 performDefaults()函数,以实现自己特定的功能,但必须调用super.performDefaults()
2. performApply()函数的实现只是简单地调用performOk(),用户同样可以重写performOk(),但无需调用super. performOk();
3. 我们看到在FieldEditorPreferencePage中performOk()被重写
public boolean performOk() {
if (fields != null) {
Iterator e = fields.iterator();
while (e.hasNext()) {
FieldEditor pe = (FieldEditor) e.next();
pe.store();//存储
pe.setPresentsDefaultValue(false);
}
}
return true;
}
注意调用各个FieldEditor的store函数来存储配置。
5)如何在插件的其他部分访问该配置信息
新建一个org.eclipse.ui.popupMenus的扩展点,实现功能为:右键一个plugin.xml会弹出一个菜单项New Submenu,单击会弹出一个消息框,显示用户在Preference页面中所设置的目录值
效果图:
关键性的代码是:
Shell shell = new Shell();
IPreferenceStore pstore= PreferencePlugin.getDefault().getPreferenceStore();//取得PreferencePlugin的PreferenceStore引用
String path = "The path is "+pstore.getString(PreferenceConstants.P_PATH);//取得键为PreferenceConstants.P_PATH所对应的value
MessageDialog.openInformation(
shell,
"Preference Plug-in",
path);
或者
Shell shell = new Shell();
Preferences preferences = PreferencePlugin.getDefault().getPluginPreferences();
String path = preferences.getString(PreferenceConstants.P_PATH);
MessageDialog.openInformation(
shell,
"Preference Plug-in",
path);
补充:
1. 如果用户的首选项页面继承自PreferencePage而不是FieldEditorPreferencePage则需要重写performOk()函数
public boolean performOk()
{
Preferences preferences = TauPlugin.getDefault().getPluginPreferences();
preferences.setValue("numProc", spin.getSelection());
preferences.setValue("TAUCDTArchPath",tauArch.getText());
TauPlugin.getDefault().savePluginPreferences();//保存配置
return true;
}
自己写一个加载配置的函数,
private void loadSaved()
{
Preferences preferences = TauPlugin.getDefault().getPluginPreferences();
int numMachines = preferences.getInt("numProc");
if(numMachines < 1) numMachines = 1;
spin.setSelection(numMachines);
tauArch.setText(preferences.getString("TAUCDTArchPath"));
}