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

xfyts7mz  于 2023-08-04  发布在  Eclipse
关注(0)|答案(1)|浏览(104)

在我的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.如果地面纹理路径错误-出现错误消息>正确
1.设置焦点(鼠标光标)到背景纹理场和和地面纹理-错误消息消失,因为只有背景纹理场是有效的(从StringFieldEditor.getTextControl焦点丢失事件触发)>错误!!!
另一个场景:
1.如果地面纹理路径错误-出现错误消息>正确
1.设置错误的值到背景纹理路径-错误消息出现>正确
1.删除背景纹理路径-允许空字符串-错误信息消失,但没有地面路径> WRONG的错误信息!!!
注意:整个页面由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();
  }
    
}

m1m5dgzv

m1m5dgzv1#

作为一个热修复,我禁用了FileFieldEditor触发的所有错误消息,只有FieldEditorPreferencePage处理这些错误消息。
CustomFileFieldEditor中:

@Override
  protected void clearErrorMessage()
  {
    // only FieldEditorPreferencePage should handle error messages
    //if (page != null)
    //  page.setErrorMessage(null);
  }
  
  @Override
  public void setErrorMessage(String message)
  {
    // only FieldEditorPreferencePage should handle error messages
    //errorMessage = message;
  }

字符串
PrefPage中:

@Override
  public void propertyChange(PropertyChangeEvent event)
  {

    if (event.getProperty().equals(FieldEditor.IS_VALID))
    {
      boolean validSuffix = false;
      boolean newValue = ((Boolean) event.getNewValue()).booleanValue();
      
      if (newValue)
      {
        checkState();

        if (this.isValid())
        {
          // check correct file extension
          // improve, do not rely on extension
          if (event.getSource() instanceof FileFieldEditor)
          {
            FileFieldEditor ffe = (FileFieldEditor) event.getSource();
            if (!ffe.getStringValue().isEmpty())
            {
              for (String suffix : Arrays.asList(this.pictureTypesAllowed))
              {
                if (ffe.getStringValue().endsWith(suffix))
                {
                  validSuffix = true;
                  break;
                }
              }
            }
            else
              validSuffix = true;//improve code, ternary op?
          }
          
          if (validSuffix)
            super.setErrorMessage(null);
          else
            super.setErrorMessage("test_suffix");
        }
      } else
      {
        invalidFieldEditor = (FieldEditor) event.getSource();
        this.setErrorMessage("test");
        setValid(newValue);
      }
    }
  }


正如@greg-449在评论中提到的,我将考虑用SWT控件重新制作整个首选项页面。

相关问题