在我的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();
}
}
型
1条答案
按热度按时间m1m5dgzv1#
作为一个热修复,我禁用了
FileFieldEditor
触发的所有错误消息,只有FieldEditorPreferencePage
处理这些错误消息。在
CustomFileFieldEditor
中:字符串
在
PrefPage
中:型
正如@greg-449在评论中提到的,我将考虑用SWT控件重新制作整个首选项页面。