summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/ui/AbstractField.java
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2011-12-19 14:07:48 +0200
committerArtur Signell <artur@vaadin.com>2011-12-19 14:07:48 +0200
commiteecf11ad18484d5a3862aa6fedcc51fdffc8b4eb (patch)
tree4f2b1e20c6503892f2b5ee95c7c2881c18496bbe /src/com/vaadin/ui/AbstractField.java
parent68f1371a15d345971870cf3e67ae11b1cf491846 (diff)
downloadvaadin-framework-eecf11ad18484d5a3862aa6fedcc51fdffc8b4eb.tar.gz
vaadin-framework-eecf11ad18484d5a3862aa6fedcc51fdffc8b4eb.zip
#8102 Allow overriding the conversion error message for Fields
Diffstat (limited to 'src/com/vaadin/ui/AbstractField.java')
-rw-r--r--src/com/vaadin/ui/AbstractField.java63
1 files changed, 55 insertions, 8 deletions
diff --git a/src/com/vaadin/ui/AbstractField.java b/src/com/vaadin/ui/AbstractField.java
index 2a7f0e42d3..339741545d 100644
--- a/src/com/vaadin/ui/AbstractField.java
+++ b/src/com/vaadin/ui/AbstractField.java
@@ -136,6 +136,11 @@ public abstract class AbstractField<T> extends AbstractComponent implements
private String requiredError = "";
/**
+ * The error message that is shown when the field value cannot be converted.
+ */
+ private String valueConversionError = "Could not convert value to {0}";
+
+ /**
* Is automatic validation enabled.
*/
private boolean validationVisible = true;
@@ -848,8 +853,13 @@ public abstract class AbstractField<T> extends AbstractComponent implements
* If there is a value converter, always use it. It must convert or
* throw an exception.
*/
- return valueConverter.convertFromTargetToSource(fieldValue,
- getLocale());
+ try {
+ return valueConverter.convertFromTargetToSource(fieldValue,
+ getLocale());
+ } catch (com.vaadin.data.util.converter.Converter.ConversionException e) {
+ throw new ConversionException(
+ getValueConversionError(valueConverter.getSourceType()));
+ }
}
if (fieldValue == null) {
@@ -871,12 +881,24 @@ public abstract class AbstractField<T> extends AbstractComponent implements
return fieldValue;
} else {
throw new Converter.ConversionException(
- "Unable to convert value of type "
- + fieldValue.getClass().getName()
- + " to "
- + type.getName()
- + ". No value converter is set and the types are not compatible.");
+ getValueConversionError(type));
+ }
+ }
+ /**
+ * Returns the value conversion error with {0} replaced by the data source
+ * type.
+ *
+ * @param dataSourceType
+ * The type of the data source
+ * @return The value conversion error string with parameters replaced.
+ */
+ protected String getValueConversionError(Class<?> dataSourceType) {
+ if (dataSourceType == null) {
+ return getValueConversionError();
+ } else {
+ return getValueConversionError().replace("{0}",
+ dataSourceType.getSimpleName());
}
}
@@ -998,7 +1020,9 @@ public abstract class AbstractField<T> extends AbstractComponent implements
valueToValidate = getValueConverter()
.convertFromTargetToSource(fieldValue, getLocale());
} catch (Exception e) {
- throw new InvalidValueException(e.getMessage());
+ throw new InvalidValueException(
+ getValueConversionError(getValueConverter()
+ .getSourceType()));
}
}
@@ -1391,6 +1415,29 @@ public abstract class AbstractField<T> extends AbstractComponent implements
}
/**
+ * Gets the error that is shown if the field value cannot be converted to
+ * the data source type.
+ *
+ * @return The error that is shown if conversion of the field value fails
+ */
+ public String getValueConversionError() {
+ return valueConversionError;
+ }
+
+ /**
+ * Sets the error that is shown if the field value cannot be converted to
+ * the data source type. If {0} is present in the message, it will be
+ * replaced by the simple name of the data source type.
+ *
+ * @param valueConversionError
+ * Message to be shown when conversion of the value fails
+ */
+ public void setValueConversionError(String valueConversionError) {
+ this.valueConversionError = valueConversionError;
+ requestRepaint();
+ }
+
+ /**
* Is the field empty?
*
* In general, "empty" state is same as null. As an exception, TextField