[英]propertyChange not called when restoring default values
我正在构建一个首选项页面,扩展了FieldEditorPreferencePage类。 这是代码(一些明显的代码未显示):
public class PreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
public static final String PREF_KEY_1 = "checkBoxPref";
public static final String PREF_KEY_2 = "filePref";
private FileFieldEditor pathField;
private BooleanFieldEditor yesOrNoField;
private Composite pathFieldParent;
@Override
public void init(IWorkbench workbench) {
setPreferenceStore(new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID));
}
@Override
protected void createFieldEditors() {
this.yesOrNoField = new BooleanFieldEditor(PREF_KEY_1, "Check this box!", getFieldEditorParent());
this.pathFieldParent = getFieldEditorParent();
this.pathField = new FileFieldEditor(PREF_KEY_2, "Path:", this.pathFieldParent);
addField(this.yesOrNoField);
addField(this.pathField);
boolean isChecked = getPreferenceStore().getBoolean(PREF_KEY_1);
updatePathFieldEnablement(! isChecked);
}
/**
* Updates the fields according to entered values
*/
private void updatePathFieldEnablement(boolean enabled) {
this.pathField.setEnabled(enabled, this.pathFieldParent);
}
@SuppressWarnings("boxing")
@Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getProperty().equals(FieldEditor.VALUE) && event.getSource() == this.yesOrNoField) {
updatePathFieldEnablement(! (boolean) event.getNewValue());
}
super.propertyChange(event);
}
}
根据BooleanFieldEditor的值,可以使用propertyChange方法来启用/禁用FileFieldEditor。
如果我通过选中或取消选中它来更改BooleanFieldEditor值,则可以正常工作,但是当我单击“恢复默认值”按钮时,不会调用propertyChange。
有人看到原因吗?
好吧,我想我已经得到了回应。
我在调查中走得更远,得到了这段代码,这对我来说似乎是可疑的:
在BooleanFieldEditor类中:
@Override
protected void doLoadDefault() {
if (checkBox != null) {
boolean value = getPreferenceStore().getDefaultBoolean(getPreferenceName());
checkBox.setSelection(value);
wasSelected = value;
}
}
在类StringFieldEditor中
@Override
protected void doLoadDefault() {
if (textField != null) {
String value = getPreferenceStore().getDefaultString(
getPreferenceName());
textField.setText(value);
}
valueChanged();
}
我们可以看到FileFieldEditor(继承自StringFieldEditor)向其侦听器( valueChanged();
)启动了PropertyChangeEvent,但没有启动BooleanFieldEditor。 我没有找到任何代码表明BooleanFieldEditor正在使用另一种机制。 我认为这是jFace中的错误。
要解决此问题,我只需要重写FieldEditorPreferencePage#performDefaults
方法,结果就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.