Why propertyChangeListener does not react to change in PathEditor

31 views Asked by At

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

There are 1 answers

0
know On

As @greg-449 mentioned, I have to fire fireStateChanged myself. For adding paths I use getNewInputObject() to chatch event:

@Override
  public String getNewInputObject()
  {
    oldState = isValid;
    oldValue = getList().getItems();
    String dirPath = super.getNewInputObject();
    
    // other code

    if (isValid != oldState)
      fireStateChanged(IS_VALID, oldState, isValid);
    
    return dirPath;
  }

Validation while removing paths from editor - I add listener in CustomPathEditor constructor:

if (enableValidation)
    {
      getRemoveButton().addListener(SWT.Selection, new Listener()
      {
        // handle validation after path was removed
        @Override
        public void handleEvent(Event event)
        {
          oldState = isValid;
          oldValue = getList().getItems();
          valueChanged();
        }
      });
    }