Hi,
I have a TextField with a customized StringToDoubleConverter. It is basically the same as the original one but returns “0.0” as Double value when null is set in the TextField. Also I tried to set a custom failure message when the convesion fails. This failure message is shown to the user as the validation message on mouse-over.
My problem is that i can’t get my custom message to show. It only shows the standard message “Could not convert value to Double”.
How do I get it to show my custom message?
I use a standard DoubleRangeValidator on the TextField and it works properly and displays my custom message I set like this:
numberOfHoursField.setConverter(new CustomStringToDoubleConverter());
numberOfHoursField.addValidator(new DoubleRangeValidator("Value have to be between 0.0 and 24.0", 0.0, 24.0));
ConverterCode:
@SuppressWarnings("serial")
public class CustomStringToDoubleConverter implements Converter<String, Double> {
@Override
public Double convertToModel(String value, Locale locale) {
if (value == null) {
return 0.0;
}
// Remove leading and trailing white space
value = value.trim();
ParsePosition parsePosition = new ParsePosition(0);
Number parsedValue = getFormat(locale).parse(value, parsePosition);
if (parsePosition.getIndex() != value.length()) {
//throw new ConversionException("Could not convert '" + value + "' to " + getModelType().getName());
throw new ConversionException("MY CUSTOM CONVERSION FAILURE MESSAGE TO USER'");
}
if (parsedValue == null) {
// Convert "" to null
return 0.0;
}
return parsedValue.doubleValue();
}
@Override
public String convertToPresentation(Double value, Locale locale) {
if (value == null) {
return "0";
}
return getFormat(locale).format(value);
}
protected NumberFormat getFormat(Locale locale) {
if (locale == null) {
locale = Locale.getDefault();
}
return NumberFormat.getNumberInstance(locale);
}
@Override
public Class<Double> getModelType() {
return Double.class;
}
/*
* (non-Javadoc)
*
* @see com.vaadin.data.util.converter.Converter#getPresentationType()
*/
@Override
public Class<String> getPresentationType() {
return String.class;
}
}