Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does org.eclipse.jface.preference.FileFieldEditor.setFileExtensions(String[]) works 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
  
  @Override
  protected void performApply() {
    super.performApply();
    Viewer.showReloadRestartMessage(this.getShell());
  }
  
  @Override
  public boolean performOk() {
    boolean bol = super.performOk();
    Viewer.showReloadRestartMessage(Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell());
    return bol;
  }
}

and:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading