eclipsejface

Does org.eclipse.jface.preference.FileFieldEditor.setFileExtensions(String[]) work only in file explorer?


I am allowing only *.exe files in my FileFieldEditor. It does work well in file explorer, but if I type in text field path with other file type, I get no error message (file exists). Is this correct behavior? If so, I have probably to check if string ends with ".exe", or is there some implemented functionality?

Thank you for any help!

package test.preferences;

//imports

public class RootPP extends FieldEditorPP implements IWorkbenchPP
{
  //code

  @Override
  protected void createFieldEditors()
  {
    // code
    
    // Web browser
    FileFieldEditor browserFE = new CustomFileFieldEditor(
        PConstants.P_BROWSER_INSTALL_PATH, 
        "Web browser", 
        getFieldEditorParent());
    browserFE.setChangeButtonText("Browse...");
    browserFE.setEmptyStringAllowed(true);
    browserFE.setFileExtensions(new String[]{"*.exe"});//allow only *.exe files
    
    addField(browserFE);
    
    //code
  }
  //code
  
}

and:

package test.preferences;

//imports

public class CustomFileFieldEditor extends FileFieldEditor
{
  private boolean isPathValid;
  
  public CustomFileFieldEditor(String name, String labelText, Composite parent)
  {
    //this(name, labelText, parent, true);
    init(name, labelText);
    //this.enforceAbsolute = enforceAbsolute;
    setErrorMessage(JFaceResources.getString("FileFieldEditor.errorMessage"));
    //$NON-NLS-1$
    //setChangeButtonText(JFaceResources.getString("openBrowse"));//$NON-NLS-1$
    //setValidateStrategy(validationStrategy);
    setValidateStrategy(VALIDATE_ON_KEY_STROKE);
    createControl(parent);
  }
  
  @Override
  protected boolean doCheckState() {
    //code
  }
  
  @Override
  protected boolean checkState()
  {
    //code
    
    if (isPathValid)
      clearErrorMessage();
    else
    {
      Display.getCurrent().timerExec(2000, () -> {
        if (!isPathValid && !getTextControl().isDisposed()) 
          showErrorMessage(errorMessage);
      });
    }
    return isPathValid;
  }
}

Solution

  • The setFileExtensions (and setFilterPath) methods are only used to set the parameters for any FileDialog that might be shown. They are not used to do any validation on the text. So you need to do validation.