propertyChange not called when restoring default values

129 views Asked by At

I am building a preference page extending the FieldEditorPreferencePage class. This is the code (some obvious code not displayed):

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);
    }
}

The propertyChange method is there to enable/disable the FileFieldEditor depending on the BooleanFieldEditor value.

It works OK if I change the BooleanFieldEditor valeu by checking or unchecking it, but the propertyChange is not called when I hit the "Restore default values" button.

Do someone see a reason for that?

1

There are 1 answers

3
moudug On BEST ANSWER

OK, I think I've got my response.

I went further in my investigation and I got to this code which seems suspect to me:

In class BooleanFieldEditor :

@Override
protected void doLoadDefault() {
    if (checkBox != null) {
        boolean value = getPreferenceStore().getDefaultBoolean(getPreferenceName());
        checkBox.setSelection(value);
        wasSelected = value;
    }
}

and in class StringFieldEditor

@Override
protected void doLoadDefault() {
    if (textField != null) {
        String value = getPreferenceStore().getDefaultString(
                getPreferenceName());
        textField.setText(value);
    }
    valueChanged();
}

We can see that the FileFieldEditor (that inherits from StringFieldEditor) launches an PropertyChangeEvent to its listeners (valueChanged();) but not the BooleanFieldEditor. I did not find any code indicating that BooleanFieldEditor are using another mechanism. I think this is a bug in jFace.

To get around this problem, I just had to override the FieldEditorPreferencePage#performDefaults method and the result's fine.