I do use FieldEditorPreferencePage, apart other field editors there is PathEditor, which is only one not listening to PropertyChangeEvent. Does not matter if I add or remove a path, there is no reaction. org.eclipse.jface.preference.FieldEditor.fireValueChanged(String, Object, Object) is not fired. Do you have any idea why? Seems like org.eclipse.jface.preference.FieldEditor.setPropertyChangeListener(IPropertyChangeListener) sets it correctly for all editors at page. Note: all other editors I use are extended from StringFieldEditor (FileEditor, DirectoryEditor...)
public class PreferencePage extends ValidatingPreferencePage implements IWorkbenchPreferencePage
{
private String[] styleSheetTypesAllowed = {"xslt","xsl"};
private String[] configTypesAllowed = {"lcc","qml"};
private CustomFileFieldEditor styleSheet;
public PreferencePage()
{
super(GRID);
setPreferenceStore(Activator.getDefault().getPreferenceStore());
}
@Override
public void createFieldEditors()
{
Composite parent = getFieldEditorParent();
// some other fields added
PathEditor pathEditor = new PathEditor(
PreferenceConstants.P_CONFIGPATH,
"Configuration paths",
"Add directory",
parent);
}
@Override
protected void initialize()
{
super.initialize();
isLicense();
}
@Override
protected void performDefaults()
{
// license might be enabled from ini file. If so, keep fields enabled:
enableFieldEditors(isLicense());
super.performDefaults();
}
}
Inside ValidatingPreferencePage there is updated handling of error messages, enabling field editors according license status and updated checkState() method:
public class ValidatingPreferencePage extends FieldEditorPreferencePage implements FieldEditors
{
// fields, constructors
// super.fields is private - create own list
@Override
protected void addField(FieldEditor editor)
{
if (fields == null)
fields = new ArrayList<>();
fields.add(editor);
super.addField(editor);
}
@Override
public void propertyChange(PropertyChangeEvent event)
{
if (event.getProperty().equals(FieldEditor.IS_VALID))
{
boolean newValue = ((Boolean) event.getNewValue()).booleanValue();
// If the new value is true then we must check all field editors.
// If it is false, then the page is invalid in any case.
if (newValue)
{
checkState();
} else
{
validFileType = true;
invalidFieldEditor = (FieldEditor) event.getSource();
setValid(newValue);
}
}
// handle CTRL+V & delete whole string & focus lost
if (event.getProperty().equals(FieldEditor.VALUE))
{
validFileType = true;
checkState();
}
}
private void setErrorMessagePP()
{
// set error messages
}
@Override
protected void checkState()
{
//super.checkState();
// other code
}
public List<FieldEditor> getFieldEditorList()
{
return fields;
}
}
And interface:
public interface PreferencesValidation
{
public void enableValidation(boolean enableVal);
public void setEmptyStringAllowed(boolean emptyStringAllowed);
public void setEnabled(boolean newValue, Composite parent);
public void refreshValidState();
public boolean checkState();
public boolean handlesLicense();
public String getDependsOn();
}
As @greg-449 mentioned, I have to fire
fireStateChangedmyself. For adding paths I usegetNewInputObject()to chatch event:Validation while removing paths from editor - I add listener in
CustomPathEditorconstructor: