From af4fdcda594c392eb417dc6291fbb98fa6474a90 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Mon, 25 Mar 2013 21:53:25 +0200 Subject: Convert value if locale changes and field has converter (#11419) Change-Id: Icb33ee2db9e36d4282c19b46203054a2da4abdbd --- server/src/com/vaadin/ui/AbstractField.java | 36 +++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'server/src') diff --git a/server/src/com/vaadin/ui/AbstractField.java b/server/src/com/vaadin/ui/AbstractField.java index 619d717d97..4ec6d7bac2 100644 --- a/server/src/com/vaadin/ui/AbstractField.java +++ b/server/src/com/vaadin/ui/AbstractField.java @@ -1321,15 +1321,33 @@ public abstract class AbstractField extends AbstractComponent implements } private void localeMightHaveChanged() { - if (!equals(valueLocale, getLocale()) && dataSource != null - && !isModified()) { - // When we have a data source and the internal value is directly - // read from that we want to update the value - T newInternalValue = convertFromModel(getPropertyDataSource() - .getValue()); - if (!equals(newInternalValue, getInternalValue())) { - setInternalValue(newInternalValue); - fireValueChange(false); + if (!equals(valueLocale, getLocale())) { + // The locale HAS actually changed + + if (dataSource != null && !isModified()) { + // When we have a data source and the internal value is directly + // read from that we want to update the value + T newInternalValue = convertFromModel(getPropertyDataSource() + .getValue()); + if (!equals(newInternalValue, getInternalValue())) { + setInternalValue(newInternalValue); + fireValueChange(false); + } + } else if (dataSource == null && converter != null) { + /* + * No data source but a converter has been set. The same issues + * as above but we cannot use propertyDataSource. Convert the + * current value back to a model value using the old locale and + * then convert back using the new locale. If this does not + * match the field value we need to set the converted value + * again. + */ + Object convertedValue = convertToModel(getInternalValue(), + valueLocale); + T newinternalValue = convertFromModel(convertedValue); + if (!equals(getInternalValue(), newinternalValue)) { + setConvertedValue(convertedValue); + } } } } -- cgit v1.2.3