EditableGridExample [message #1778401] |
Wed, 13 December 2017 15:43  |
Eclipse User |
|
|
|
I was trying to following the example for registering my own data validator and have a Error Decoration when the data is invalid. I ran the EditableGridExample but the ISIN validator is not showing the correct Error. It is showing "Error whilst validating cell value" instead of "Security Id must be 3 alpha characters optionally followed by numbers". Does someone have an example of working Error Decoration? The code below doesn't work properly in the example.
private static void registerISINValidator(IConfigRegistry configRegistry) {
TextCellEditor textCellEditor = new TextCellEditor();
textCellEditor.setErrorDecorationEnabled(true);
textCellEditor.setErrorDecorationText(
"Security Id must be 3 alpha characters optionally followed by numbers");
textCellEditor.setDecorationPositionOverride(SWT.LEFT | SWT.TOP);
configRegistry.registerConfigAttribute(
EditConfigAttributes.CELL_EDITOR, textCellEditor,
DisplayMode.NORMAL, SECURITY_ID_EDITOR);
configRegistry.registerConfigAttribute(
EditConfigAttributes.DATA_VALIDATOR, getSecurtityIdValidator(),
DisplayMode.EDIT, SECURITY_ID_CONFIG_LABEL);
}
|
|
|
Re: EditableGridExample [message #1778419 is a reply to message #1778401] |
Thu, 14 December 2017 01:27  |
Eclipse User |
|
|
|
The validation concept in combination with the decoration provider was overhauled some years ago. Looks like we missed to at least deprecate the method setErrorDecorationText().
In the old concept it was not possible to be more specific in the error text. It was not possible to distinguish whether it was a conversion error or a validation error, or what concrete validation failed. There was only one general error decoration for all cases.
The new concept allows you to be more specific by throwing a ValidationFailedException or a ConversionFailedException with a specific message instead of returning only false. Returning false only results in a general message, and without a decoration or an error dialog this is sufficient.
If you change the IDataValidator to something like the following snippet, it works again as expected and can be customized to become even more specific:
private static IDataValidator getSecurtityIdValidator() {
return new DataValidator() {
@Override
public boolean validate(int columnIndex, int rowIndex, Object newValue) {
boolean valid = false;
if (newValue != null) {
String value = (String) newValue;
if (value.length() > 3) {
String alphabeticPart = value.substring(0, 2);
String numericPart = value.substring(3, value.length());
valid = isAlpha(alphabeticPart)
&& isNumeric(numericPart);
} else {
String alphabeticPart = value.substring(0, value.length());
valid = isAlpha(alphabeticPart);
}
}
if (!valid) {
throw new ValidationFailedException("Security Id must be 3 alpha characters optionally followed by numbers");
}
return valid;
}
private boolean isAlpha(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isLetter(str.charAt(i)))
return false;
}
return true;
}
private boolean isNumeric(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i)))
return false;
}
return true;
}
};
}
|
|
|
Powered by
FUDForum. Page generated in 0.03174 seconds