站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Eclipse 专题Eclipse编程Tips(3)-实现Preference

Eclipse编程Tips(3)-实现Preference

摘要: Eclipse编程Tips(3)-实现Preference
Preference可以用来设置系统的参数,方便用户定义自己的设置。例如Eclipse本身就提供了很多可供设置的内容,但就编辑器来说,就可以设置字体、颜色等。那么怎样实现一个Preference扩展点呢?
原来很简单,Eclipse在JFace中已经提供了大量的类可供使用,只要扩展FieldEditorPreferencePage就可以了。例如下例:

/**
*
*/
public class IMChatPreference extends FieldEditorPreferencePage implements
IWorkbenchPreferencePage {
/**
* @param style
*/
public IMChatPreference() {
super(GRID);
setPreferenceStore(IMPlugin.getDefault().getPreferenceStore()); //此处设置要保存设置的位置,通过Plugin获得
}

/**
* @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
*/
public void init(IWorkbench workbench) {
}

private void createColorControl(Composite parent, String name,
String labelText) {
ColorFieldEditor field = new ColorFieldEditor(name, labelText, parent); //这是一个设置颜色的组件
field.setPage(this);
field.setPreferenceStore(getPreferenceStore());
addField(field);
}

private void createFontControl(Composite parent, String name,
String labelText) {
FontFieldEditor field = new FontFieldEditor(name, labelText,
parent); //这是一个设置字体的组件
field.setPage(this);
field.setPreferenceStore(getPreferenceStore());
addField(field);
}

public boolean performOk() {
IMPlugin.getDefault().savePluginPreferences(); //当按确定按钮时的动作,自然就是保存配置了
return super.performOk();
}

public IPreferenceStore doGetPreferenceStore() {
return IMPlugin.getDefault().getPreferenceStore(); //获取配置保存的store
}

/**
* @see org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors()
*/
@Override
public void createFieldEditors() { //此方法是用来创建界面的。自己定义界面,如何摆放控件等等
final Composite parent = getFieldEditorParent();
final Composite container = new Composite(parent,SWT.NULL);
GridLayout layout = new GridLayout(1,false);
layout.marginWidth = layout.marginHeight = 0;
container.setLayout(layout);
container.setLayoutData(new GridData(GridData.FILL_BOTH));

Group group=new Group(container,SWT.NULL);
group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
group.setText(IMPlugin.getResourceString("msgbox.title"));
group.setToolTipText(group.getText());
createColorControl(group, IMConsts.KEY_CHAT_BACKGROUND_COLOR, IMPlugin
.getResourceString(IMConsts.KEY_CHAT_BACKGROUND_COLOR));
createColorControl(group, IMConsts.KEY_CHAT_FOREGROUND_COLOR, IMPlugin
.getResourceString(IMConsts.KEY_CHAT_FOREGROUND_COLOR));
createFontControl(group, IMConsts.KEY_CHAT_FONT, IMPlugin
.getResourceString(IMConsts.KEY_CHAT_FONT));

group=new Group(container,SWT.NULL);
group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
group.setText(IMPlugin.getResourceString("msgsend.title"));
group.setToolTipText(group.getText());
createColorControl(group, IMConsts.KEY_SEND_BACKGROUND_COLOR, IMPlugin
.getResourceString(IMConsts.KEY_SEND_BACKGROUND_COLOR));
createColorControl(group, IMConsts.KEY_SEND_FOREGROUND_COLOR, IMPlugin
.getResourceString(IMConsts.KEY_SEND_FOREGROUND_COLOR));
createFontControl(group, IMConsts.KEY_SEND_FONT, IMPlugin
.getResourceString(IMConsts.KEY_SEND_FONT));
}
}

Eclipse还提供了StringFieldEditor , IntegerFieldEditor 等,方便使用。
接下来的工作自然就是设置扩展点了。不再赘述。

上述代码是通过阅读Eclipse本身的Preference实现得到的。最开始的时候,我自己处理各种输入,结果对于颜色的设置,在重新打开界面的时候,由于不能刷新,导致外观表现错误。后来反复研究Eclipse,终于发现原来早就处理好了。哎,浪费了好多时间:(
希望能对大家有所帮助。



↑返回目录
前一篇: 安裝Eclipse SVN Plugin
后一篇: 关于Eclipse draw2d的FanRouter