summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2013-03-25 21:53:25 +0200
committerVaadin Code Review <review@vaadin.com>2013-03-31 11:01:47 +0000
commitaf4fdcda594c392eb417dc6291fbb98fa6474a90 (patch)
tree0fc8cfb665a8836bf2baad65297a559321a537f5 /server
parent833b117b967e3466b37bf95d3f801e927e0e5c45 (diff)
downloadvaadin-framework-af4fdcda594c392eb417dc6291fbb98fa6474a90.tar.gz
vaadin-framework-af4fdcda594c392eb417dc6291fbb98fa6474a90.zip
Convert value if locale changes and field has converter (#11419)
Change-Id: Icb33ee2db9e36d4282c19b46203054a2da4abdbd
Diffstat (limited to 'server')
-rw-r--r--server/src/com/vaadin/ui/AbstractField.java36
-rw-r--r--server/tests/src/com/vaadin/ui/AbstractFieldDataSourceLocaleChange.java63
2 files changed, 90 insertions, 9 deletions
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<T> 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);
+ }
}
}
}
diff --git a/server/tests/src/com/vaadin/ui/AbstractFieldDataSourceLocaleChange.java b/server/tests/src/com/vaadin/ui/AbstractFieldDataSourceLocaleChange.java
new file mode 100644
index 0000000000..9810873f0b
--- /dev/null
+++ b/server/tests/src/com/vaadin/ui/AbstractFieldDataSourceLocaleChange.java
@@ -0,0 +1,63 @@
+package com.vaadin.ui;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+import junit.framework.Assert;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.vaadin.data.util.converter.StringToIntegerConverter;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.server.VaadinSession;
+
+public class AbstractFieldDataSourceLocaleChange {
+
+ private VaadinSession vaadinSession;
+ private UI ui;
+
+ @Before
+ public void setup() {
+ vaadinSession = new VaadinSession(null);
+ VaadinSession.setCurrent(vaadinSession);
+ ui = new UI() {
+
+ @Override
+ protected void init(VaadinRequest request) {
+
+ }
+ };
+ ui.setSession(vaadinSession);
+ UI.setCurrent(ui);
+ }
+
+ @Test
+ public void localeChangesOnAttach() {
+ TextField tf = new TextField();
+
+ ;
+ tf.setConverter(new StringToIntegerConverter() {
+ @Override
+ protected NumberFormat getFormat(Locale locale) {
+ if (locale == null) {
+ NumberFormat format = super.getFormat(locale);
+ format.setGroupingUsed(false);
+ format.setMinimumIntegerDigits(10);
+ return format;
+ }
+ return super.getFormat(locale);
+ }
+ });
+ tf.setImmediate(true);
+ tf.setConvertedValue(10000);
+ Assert.assertEquals("0000010000", tf.getValue());
+
+ VerticalLayout vl = new VerticalLayout();
+ ui.setContent(vl);
+ ui.setLocale(new Locale("en", "US"));
+
+ vl.addComponent(tf);
+ Assert.assertEquals("10,000", tf.getValue());
+ }
+}