如何在一个首选项页面验证更多 `org.eclipse.jface.preference.StringFieldEditor` 实例?

问题描述 投票:0回答:0

在我的

FieldEditorPreferencePage
,我确实创建了两个
FileFieldEditor
实例,一个用于地面纹理设置,另一个用于背景纹理设置:

public class PrefPage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage
{
  final int MAX_NR_COLUMNS = 3;
  private List<FieldEditor> fields = null;
  
  public PrefPage() {
    super(GRID);
    setPreferenceStore(Activator.getDefault().getPreferenceStore());
  }

  @Override
  protected void createFieldEditors()
  {
    Composite fieldEditorParent = getFieldEditorParent();
    //types allowed for Ground and Background texture
    String[] pictureTypesAllowed = {"jpg","jpeg","png"};
    String[] pictureTypesAllowedC = new String[]{"*.jpg;*.jpeg;*.png"};
    
    // some settings
    
    // ground settings
    Group envGroup = new Group(fieldEditorParent, SWT.NULL);
    envGroup.setText("Ground");
    envGroup.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false, MAX_NR_COLUMNS, 1));
    ComboFieldEditor envControlFE = new ComboFieldEditor(PrefRayConstants.P_RT_GROUND, "Type") PrefRayConstants.GROUND_TYPES, envGroup);
    addField(envControlFE);
    
    CustomFileFieldEditor txtrControlFE = new CustomFileFieldEditor(PrefRayConstants.P_RT_GROUND_TEXTURE, 
        "Texture", 
        envGroup,
        pictureTypesAllowed); 
    txtrControlFE.setFileExtensions(pictureTypesAllowedC);//allow only those file types
    addField(txtrControlFE);
    
    // background settings
    Group bgGroup = new Group(fieldEditorParent, SWT.NULL);
    bgGroup.setText("Background");
    bgGroup.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false, MAX_NR_COLUMNS, 1));
    FileFieldEditor bgTxtrControlFE = new CustomFileFieldEditor(PrefRayConstants.P_RT_BG_TEXTURE, 
        "Texture", 
        bgGroup,
        pictureTypesAllowed); 
    bgTxtrControlFE.setFileExtensions(pictureTypesAllowedC);//allow only those file types
    addField(bgTxtrControlFE);
  }
  
  @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());
    if (Viewer.getActiveViewer()!=null)
      Viewer.getActiveViewer().setRayLocalSettings();*/
    return bol;
  }

}

我的问题是:

  1. 如果地面纹理路径错误 - 出现错误消息 > 正确
  2. 将焦点(鼠标光标)设置为背景纹理字段并返回地面纹理 - 错误消息消失,因为仅验证背景纹理字段(由
    StringFieldEditor.getTextControl
    的焦点丢失事件触发)> 错误!!!

另一个场景:

  1. 如果地面纹理路径错误 - 出现错误消息 > 正确
  2. 为背景纹理路径设置错误的值 - 出现错误消息 > 正确
  3. 删除背景纹理路径 - 允许空字符串 - 错误消息消失,但地面路径没有错误消息 > 错误!!!

注意:整个页面已通过

FieldEditorPreferencePage.checkState
正确验证。

我的问题是:Eclipse 中是否有内置的东西,我错过了什么?如果没有,更好的方法是什么:自定义 StringFieldEditor.checkState (为所有字段添加一些验证)或在首选项页面以某种方式进行验证?

我的定制

FileFieldEditor
:

public class CustomFileFieldEditor extends FileFieldEditor
{
  private List<String> allowedExtensions = null;
  private boolean enableValidation = true;

  public CustomFileFieldEditor(String name, String labelText, Composite parent)
  {
    //this(name, labelText, parent, true);
    init(name, labelText);
    setErrorMessage(JFaceResources.getString("FileFieldEditor.errorMessage"));
    setValidateStrategy(VALIDATE_ON_KEY_STROKE);
    createControl(parent);
  }
  
  public CustomFileFieldEditor(String name, String labelText, Composite parent, String[] allowedExtensions)
  {
    // this(name, labelText, parent, true);
    init(name, labelText);
    setErrorMessage(JFaceResources.getString("FileFieldEditor.errorMessage"));
    setValidateStrategy(VALIDATE_ON_KEY_STROKE);
    createControl(parent);
    this.allowedExtensions  = Arrays.asList(allowedExtensions);
  }

  
  public CustomFileFieldEditor(String name, String labelText, Composite parent, boolean enableValidation)
  {
    //super(name, labelText, parent);
    init(name, labelText);
    setErrorMessage(JFaceResources.getString("FileFieldEditor.errorMessage"));
    enableValidation(enableValidation);
    //setEmptyStringAllowed(!enableValidation);
    setValidateStrategy(VALIDATE_ON_KEY_STROKE);
    createControl(parent);
  }
  
  public void enableValidation(boolean enableValidation)
  {
    this.enableValidation = enableValidation;
  }

  @Override
  protected boolean doCheckState() {
    return super.doCheckState();
  }
  
  @Override
  protected boolean checkState()
  {
    return super.checkState();

  }
  
  public void valueChanged() {
    super.valueChanged();
  }
  
  // prepared method
  private boolean isSuffixAllowed(String txt)
  {
    if (allowedExtensions == null)
      return true;

      if (!allowedExtensions.isEmpty())
        for (String suffix : allowedExtensions)
          if (txt.endsWith(suffix))
            return true;

    return false;
  }
  
  @Override
  public boolean isValid()
  {
    return super.isValid();
  }
    
}
eclipse jface
© www.soinside.com 2019 - 2024. All rights reserved.