在Nodeclipse preference Page 我想在expressPath更改时更新expressVersion。
我在下面添加代码,但它不会触发PropertyChangeEvent,因此这个逻辑不会被执行 这有什么不对?
expressPath = new FileFieldEditor(PreferenceConstants.EXPRESS_PATH, "Express path:", getFieldEditorParent());
//TODO update expressVersion on expressPath selection
IPropertyChangeListener listener = new IPropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getProperty().equals(FieldEditor.IS_VALID)) {
String newValue = (String) event.getNewValue();
expressVersion.setStringValue( ProcessUtils.getCurrentVersionOf(newValue) );
}
}
};
expressPath.setPropertyChangeListener(listener);
addField(expressPath);
expressVersion = new StringFieldEditor(PreferenceConstants.EXPRESS_VERSION, "Selected Express version:", getFieldEditorParent());
expressVersion.setEnabled(false, getFieldEditorParent());
addField(expressVersion);
答案 0 :(得分:1)
查看FieldEditorPreferencePage的来源,我发现initialize()
正在重置所有文件的PropertyChangeListeners
/**
* Initializes all field editors.
*/
protected void initialize() {
if (fields != null) {
Iterator<FieldEditor> e = fields.iterator();
while (e.hasNext()) {
FieldEditor pe = e.next();
pe.setPage(this);
pe.setPropertyChangeListener(this);
pe.setPreferenceStore(getPreferenceStore());
pe.load();
}
}
}
在createFieldEditors();
之后调用initialize()protected Control createContents(Composite parent) {
fieldEditorParent = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
layout.numColumns = 1;
layout.marginHeight = 0;
layout.marginWidth = 0;
fieldEditorParent.setLayout(layout);
fieldEditorParent.setFont(parent.getFont());
createFieldEditors();
if (style == GRID) {
adjustGridLayout();
}
initialize();
checkState();
return fieldEditorParent;
}
所以用下面的代码解决了
@Override
protected void initialize() {
super.initialize();
expressPath.setPropertyChangeListener(new MyPropertyChangeListener(this));
}
private class MyPropertyChangeListener implements IPropertyChangeListener{
private FieldEditorPreferencePage page;
public MyPropertyChangeListener(FieldEditorPreferencePage nodePreferencePage) {
page = nodePreferencePage;
}
@Override
public void propertyChange(PropertyChangeEvent event) {
page.propertyChange(event);
// The IS_VALID property is only fired when the valid state changes.
if (event.getProperty().equals(FieldEditor.IS_VALID)) {
isValidPath = ((Boolean) event.getNewValue()).booleanValue();
if (!isValidPath) {
expressVersion.setStringValue( "Selected path is not valid" );
}
return;
}
if (!isValidPath) { // if it is still not valid file, we don't need to check again
return;
}
if (event.getProperty().equals(FieldEditor.VALUE)) {
String newExecutablePath = (String) event.getNewValue();
expressVersion.setStringValue( ProcessUtils.getCurrentVersionOf(newExecutablePath));
}
}
}