]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add ValueContext parameter for Converters
authorTeemu Suo-Anttila <teemusa@vaadin.com>
Thu, 20 Oct 2016 08:36:45 +0000 (11:36 +0300)
committerTeemu Suo-Anttila <teemusa@vaadin.com>
Mon, 24 Oct 2016 10:52:17 +0000 (13:52 +0300)
Change-Id: I47179b06b9e345f5a454ac1806d0bc9bcac24bcf

40 files changed:
server/src/main/java/com/vaadin/data/Binder.java
server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java
server/src/main/java/com/vaadin/data/util/converter/Converter.java
server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java
server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java
server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java
server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java
server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java
server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java
server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java
server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java
server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java
server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java
server/src/main/java/com/vaadin/data/util/converter/ValueContext.java [new file with mode: 0644]
server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java
server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java
server/src/main/java/com/vaadin/ui/declarative/converters/DesignDateConverter.java
server/src/main/java/com/vaadin/ui/declarative/converters/DesignEnumConverter.java
server/src/main/java/com/vaadin/ui/declarative/converters/DesignLocalDateConverter.java
server/src/main/java/com/vaadin/ui/declarative/converters/DesignObjectConverter.java
server/src/main/java/com/vaadin/ui/declarative/converters/DesignResourceConverter.java
server/src/main/java/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java
server/src/main/java/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java
server/src/main/java/com/vaadin/ui/declarative/converters/DesignToStringConverter.java
server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java
server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java
server/src/test/java/com/vaadin/data/ValueContextTest.java [new file with mode: 0644]
server/src/test/java/com/vaadin/tests/data/converter/AbstractConverterTest.java
server/src/test/java/com/vaadin/tests/data/converter/AbstractStringConverterTest.java
server/src/test/java/com/vaadin/tests/data/converter/ConverterTest.java
server/src/test/java/com/vaadin/tests/data/converter/DateToLongConverterTest.java
server/src/test/java/com/vaadin/tests/data/converter/DateToSqlDateConverterTest.java
server/src/test/java/com/vaadin/tests/data/converter/StringToBigDecimalConverterTest.java
server/src/test/java/com/vaadin/tests/data/converter/StringToBigIntegerConverterTest.java
server/src/test/java/com/vaadin/tests/data/converter/StringToBooleanConverterTest.java
server/src/test/java/com/vaadin/tests/data/converter/StringToDateConverterTest.java
server/src/test/java/com/vaadin/tests/data/converter/StringToDoubleConverterTest.java
server/src/test/java/com/vaadin/tests/data/converter/StringToFloatConverterTest.java
server/src/test/java/com/vaadin/tests/data/converter/StringToIntegerConverterTest.java
server/src/test/java/com/vaadin/tests/data/converter/StringToLongConverterTest.java

index 8356a03ddaa4e80b7217f878c0db806da8013814..5493712cec4c84971794588159a8254e1f8ffde3 100644 (file)
@@ -34,6 +34,7 @@ import java.util.stream.Collectors;
 
 import com.vaadin.data.util.converter.Converter;
 import com.vaadin.data.util.converter.StringToIntegerConverter;
+import com.vaadin.data.util.converter.ValueContext;
 import com.vaadin.event.EventRouter;
 import com.vaadin.server.ErrorMessage;
 import com.vaadin.server.UserError;
@@ -563,10 +564,23 @@ public class Binder<BEAN> implements Serializable {
         private ValidationStatus<TARGET> doValidation() {
             FIELDVALUE fieldValue = field.getValue();
             Result<TARGET> dataValue = converterValidatorChain
-                    .convertToModel(fieldValue, findLocale());
+                    .convertToModel(fieldValue, createValueContext());
             return new ValidationStatus<>(this, dataValue);
         }
 
+        /**
+         * Creates a value context from the current state of the binding and its
+         * field.
+         *
+         * @return the value context
+         */
+        protected ValueContext createValueContext() {
+            if (field instanceof Component) {
+                return new ValueContext((Component) field);
+            }
+            return new ValueContext(findLocale());
+        }
+
         private void unbind() {
             onValueChange.remove();
         }
@@ -584,8 +598,8 @@ public class Binder<BEAN> implements Serializable {
         }
 
         private FIELDVALUE convertDataToFieldType(BEAN bean) {
-            return converterValidatorChain
-                    .convertToPresentation(getter.apply(bean), findLocale());
+            return converterValidatorChain.convertToPresentation(
+                    getter.apply(bean), createValueContext());
         }
 
         /**
@@ -661,7 +675,7 @@ public class Binder<BEAN> implements Serializable {
         }
 
         @Override
-        public Result<T> convertToModel(T value, Locale locale) {
+        public Result<T> convertToModel(T value, ValueContext context) {
             Result<? super T> validationResult = validator.apply(value);
             if (validationResult.isError()) {
                 return Result.error(validationResult.getMessage().get());
@@ -671,7 +685,7 @@ public class Binder<BEAN> implements Serializable {
         }
 
         @Override
-        public T convertToPresentation(T value, Locale locale) {
+        public T convertToPresentation(T value, ValueContext context) {
             return value;
         }
 
index c06a9baa614c6aefeb95bd0bbe3c83996e67c64e..e7f4476f66a35dce742cb9a79e6676de87d85260 100644 (file)
@@ -109,12 +109,12 @@ public abstract class AbstractStringToNumberConverter<T>
     }
 
     @Override
-    public String convertToPresentation(T value, Locale locale) {
+    public String convertToPresentation(T value, ValueContext context) {
         if (value == null) {
             return null;
         }
 
-        return getFormat(locale).format(value);
+        return getFormat(context.getLocale().orElse(null)).format(value);
     }
 
 }
index 97985af9ca3068f798334b4a00b705d63bf80b55..c25c56777398755a26fe603a6b2ec274063e7b8c 100644 (file)
@@ -45,11 +45,17 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable {
      *
      * @param value
      *            The value to convert. Can be null
-     * @param locale
-     *            The locale to use for conversion. Can be null.
+     * @param context
+     *            The value context for the conversion.
      * @return The converted value compatible with the source type
      */
-    public Result<MODEL> convertToModel(PRESENTATION value, Locale locale);
+    public Result<MODEL> convertToModel(PRESENTATION value,
+            ValueContext context);
+
+    default public Result<MODEL> convertToModel(PRESENTATION value,
+            Locale locale) {
+        return convertToModel(value, new ValueContext(locale));
+    }
 
     /**
      * Converts the given value from presentation type to model type.
@@ -58,11 +64,17 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable {
      *
      * @param value
      *            The value to convert. Can be null
-     * @param locale
-     *            The locale to use for conversion. Can be null.
+     * @param context
+     *            The value context for the conversion.
      * @return The converted value compatible with the source type
      */
-    public PRESENTATION convertToPresentation(MODEL value, Locale locale);
+    public PRESENTATION convertToPresentation(MODEL value,
+            ValueContext context);
+
+    default public PRESENTATION convertToPresentation(MODEL value,
+            Locale locale) {
+        return convertToPresentation(value, new ValueContext());
+    }
 
     /**
      * Returns a converter that returns its input as-is in both directions.
@@ -124,12 +136,12 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable {
         return new Converter<P, M>() {
 
             @Override
-            public Result<M> convertToModel(P value, Locale locale) {
+            public Result<M> convertToModel(P value, ValueContext context) {
                 return toModel.apply(value);
             }
 
             @Override
-            public P convertToPresentation(M value, Locale locale) {
+            public P convertToPresentation(M value, ValueContext context) {
                 return toPresentation.apply(value);
             }
         };
@@ -157,16 +169,18 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable {
             Converter<MODEL, T> other) {
         return new Converter<PRESENTATION, T>() {
             @Override
-            public Result<T> convertToModel(PRESENTATION value, Locale locale) {
+            public Result<T> convertToModel(PRESENTATION value,
+                    ValueContext context) {
                 Result<MODEL> model = Converter.this.convertToModel(value,
-                        locale);
-                return model.flatMap(v -> other.convertToModel(v, locale));
+                        context);
+                return model.flatMap(v -> other.convertToModel(v, context));
             }
 
             @Override
-            public PRESENTATION convertToPresentation(T value, Locale locale) {
-                MODEL model = other.convertToPresentation(value, locale);
-                return Converter.this.convertToPresentation(model, locale);
+            public PRESENTATION convertToPresentation(T value,
+                    ValueContext context) {
+                MODEL model = other.convertToPresentation(value, context);
+                return Converter.this.convertToPresentation(model, context);
             }
         };
     }
index 2fff4915ee66cc7c9ece41021ba4fe391d683354..bd51181f5ebc6e71b3863a0965a3f4e50c1d78fc 100644 (file)
@@ -17,7 +17,6 @@
 package com.vaadin.data.util.converter;
 
 import java.util.Date;
-import java.util.Locale;
 
 import com.vaadin.data.Result;
 
@@ -30,7 +29,7 @@ import com.vaadin.data.Result;
 public class DateToLongConverter implements Converter<Date, Long> {
 
     @Override
-    public Result<Long> convertToModel(Date value, Locale locale) {
+    public Result<Long> convertToModel(Date value, ValueContext context) {
         if (value == null) {
             return Result.ok(null);
         }
@@ -39,7 +38,7 @@ public class DateToLongConverter implements Converter<Date, Long> {
     }
 
     @Override
-    public Date convertToPresentation(Long value, Locale locale) {
+    public Date convertToPresentation(Long value, ValueContext context) {
         if (value == null) {
             return null;
         }
index 5695e4bf9d314cde427bcc57a29248d2da6850ca..1fb7584e905fd10558afa0e3f96f85e7b23a8ed1 100644 (file)
@@ -20,7 +20,6 @@
 package com.vaadin.data.util.converter;
 
 import java.util.Date;
-import java.util.Locale;
 
 import com.vaadin.data.Result;
 
@@ -37,7 +36,8 @@ import com.vaadin.data.Result;
 public class DateToSqlDateConverter implements Converter<Date, java.sql.Date> {
 
     @Override
-    public Result<java.sql.Date> convertToModel(Date value, Locale locale) {
+    public Result<java.sql.Date> convertToModel(Date value,
+            ValueContext context) {
         if (value == null) {
             return Result.ok(null);
         }
@@ -46,7 +46,8 @@ public class DateToSqlDateConverter implements Converter<Date, java.sql.Date> {
     }
 
     @Override
-    public Date convertToPresentation(java.sql.Date value, Locale locale) {
+    public Date convertToPresentation(java.sql.Date value,
+            ValueContext context) {
         if (value == null) {
             return null;
         }
index af0a63fc7a7790e65908b2261a532d73cad845c3..d30540d120581451f85ab3a064b9144426ede820 100644 (file)
@@ -60,8 +60,9 @@ public class StringToBigDecimalConverter
     }
 
     @Override
-    public Result<BigDecimal> convertToModel(String value, Locale locale) {
-        return convertToNumber(value, locale)
+    public Result<BigDecimal> convertToModel(String value,
+            ValueContext context) {
+        return convertToNumber(value, context.getLocale().orElse(null))
                 .map(number -> (BigDecimal) number);
     }
 
index d469db70553475f25b8dec5825fd7eaa19644cd6..186beb9d4b8dcbf068421812fbf5e3e7cc4dbe2e 100644 (file)
@@ -60,14 +60,16 @@ public class StringToBigIntegerConverter
     }
 
     @Override
-    public Result<BigInteger> convertToModel(String value, Locale locale) {
-        return convertToNumber(value, locale).map(number -> {
-            if (number == null) {
-                return null;
-            } else {
-                return ((BigDecimal) number).toBigInteger();
-            }
-        });
+    public Result<BigInteger> convertToModel(String value,
+            ValueContext context) {
+        return convertToNumber(value, context.getLocale().orElse(null))
+                .map(number -> {
+                    if (number == null) {
+                        return null;
+                    } else {
+                        return ((BigDecimal) number).toBigInteger();
+                    }
+                });
     }
 
 }
index 7b9e1850f03076703e30df057c3cb79d99f25daf..38b27f8b50830cbd970138a9da7a99734b1f9a13 100644 (file)
@@ -72,7 +72,7 @@ public class StringToBooleanConverter implements Converter<String, Boolean> {
     }
 
     @Override
-    public Result<Boolean> convertToModel(String value, Locale locale) {
+    public Result<Boolean> convertToModel(String value, ValueContext context) {
         if (value == null) {
             return Result.ok(null);
         }
@@ -80,6 +80,7 @@ public class StringToBooleanConverter implements Converter<String, Boolean> {
         // Remove leading and trailing white space
         value = value.trim();
 
+        Locale locale = context.getLocale().orElse(null);
         if (getTrueString(locale).equals(value)) {
             return Result.ok(true);
         } else if (getFalseString(locale).equals(value)) {
@@ -92,10 +93,11 @@ public class StringToBooleanConverter implements Converter<String, Boolean> {
     }
 
     @Override
-    public String convertToPresentation(Boolean value, Locale locale) {
+    public String convertToPresentation(Boolean value, ValueContext context) {
         if (value == null) {
             return null;
         }
+        Locale locale = context.getLocale().orElse(null);
         if (value) {
             return getTrueString(locale);
         } else {
index 2e3e8ab04a004072b2641144778b4d3653337d6a..d8a036c002638b90e1490cfcb1268d2ea6acdb44 100644 (file)
@@ -58,7 +58,7 @@ public class StringToDateConverter implements Converter<String, Date> {
     }
 
     @Override
-    public Result<Date> convertToModel(String value, Locale locale) {
+    public Result<Date> convertToModel(String value, ValueContext context) {
         if (value == null) {
             return Result.ok(null);
         }
@@ -67,7 +67,8 @@ public class StringToDateConverter implements Converter<String, Date> {
         value = value.trim();
 
         ParsePosition parsePosition = new ParsePosition(0);
-        Date parsedValue = getFormat(locale).parse(value, parsePosition);
+        Date parsedValue = getFormat(context.getLocale().orElse(null))
+                .parse(value, parsePosition);
         if (parsePosition.getIndex() != value.length()) {
             return Result.error("Could not convert '" + value);
         }
@@ -76,12 +77,12 @@ public class StringToDateConverter implements Converter<String, Date> {
     }
 
     @Override
-    public String convertToPresentation(Date value, Locale locale) {
+    public String convertToPresentation(Date value, ValueContext context) {
         if (value == null) {
             return null;
         }
 
-        return getFormat(locale).format(value);
+        return getFormat(context.getLocale().orElse(null)).format(value);
     }
 
 }
index 463a7f6b366004cc10225aa44422050315c8a549..52bf9509c94f19efc50736e93ad64aa3e0d3af06 100644 (file)
@@ -49,8 +49,9 @@ public class StringToDoubleConverter
     }
 
     @Override
-    public Result<Double> convertToModel(String value, Locale locale) {
-        Result<Number> n = convertToNumber(value, locale);
+    public Result<Double> convertToModel(String value, ValueContext context) {
+        Result<Number> n = convertToNumber(value,
+                context.getLocale().orElse(null));
 
         return n.map(number -> {
             if (number == null) {
index be592665cfdb2b2c7d830ca24237fb820d099d9d..aeeeffcb8bb85f196b3d66cc915d28924e0ef67c 100644 (file)
@@ -47,8 +47,9 @@ public class StringToFloatConverter
     }
 
     @Override
-    public Result<Float> convertToModel(String value, Locale locale) {
-        Result<Number> n = convertToNumber(value, locale);
+    public Result<Float> convertToModel(String value, ValueContext context) {
+        Result<Number> n = convertToNumber(value,
+                context.getLocale().orElse(null));
 
         return n.map(number -> {
             if (number == null) {
index ec05647a514b8fe2db3b032a8977176fcdd1b2d9..1c98e54c6e18000aa4c10bc1add7a1a42b60d1eb 100644 (file)
@@ -63,8 +63,9 @@ public class StringToIntegerConverter
     }
 
     @Override
-    public Result<Integer> convertToModel(String value, Locale locale) {
-        Result<Number> n = convertToNumber(value, locale);
+    public Result<Integer> convertToModel(String value, ValueContext context) {
+        Result<Number> n = convertToNumber(value,
+                context.getLocale().orElse(null));
         return n.flatMap(number -> {
             if (number == null) {
                 return Result.ok(null);
index 9b7bfa4fc6202728c915306c5767f1e637754455..444e7ede8e3cea83dc95f01c76a74bfd3dc78645 100644 (file)
@@ -62,8 +62,9 @@ public class StringToLongConverter
     }
 
     @Override
-    public Result<Long> convertToModel(String value, Locale locale) {
-        Result<Number> n = convertToNumber(value, locale);
+    public Result<Long> convertToModel(String value, ValueContext context) {
+        Result<Number> n = convertToNumber(value,
+                context.getLocale().orElse(null));
         return n.map(number -> {
             if (number == null) {
                 return null;
diff --git a/server/src/main/java/com/vaadin/data/util/converter/ValueContext.java b/server/src/main/java/com/vaadin/data/util/converter/ValueContext.java
new file mode 100644 (file)
index 0000000..a8fdf29
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2000-2016 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.util.converter;
+
+import java.io.Serializable;
+import java.util.Locale;
+import java.util.Objects;
+import java.util.Optional;
+
+import com.vaadin.ui.Component;
+import com.vaadin.ui.UI;
+
+/**
+ * Value context for {@code Converter}s. Contains relevant information for
+ * converting values.
+ *
+ * @author Vaadin Ltd.
+ * @since
+ */
+public class ValueContext implements Serializable {
+
+    private final Component component;
+    private final Locale locale;
+
+    /**
+     * Constructor for {@code ValueContext} without a {@code Locale}.
+     */
+    public ValueContext() {
+        this.locale = null;
+        this.component = null;
+    }
+
+    /**
+     * Constructor for {@code ValueContext} without a {@code Component}.
+     *
+     * @param locale
+     *            The locale used with conversion. Can be null.
+     */
+    public ValueContext(Locale locale) {
+        this.component = null;
+        this.locale = locale;
+    }
+
+    /**
+     * Constructor for {@code ValueContext}.
+     *
+     * @param component
+     *            The component related to current value. Can be null.
+     */
+    public ValueContext(Component component) {
+        Objects.requireNonNull(component,
+                "Component can't be null in ValueContext construction");
+        this.component = component;
+        this.locale = findLocale();
+    }
+
+    private Locale findLocale() {
+        Locale l = null;
+        if (component != null) {
+            l = component.getLocale();
+        }
+        if (l == null && UI.getCurrent() != null) {
+            l = UI.getCurrent().getLocale();
+        }
+        if (l == null) {
+            l = Locale.getDefault();
+        }
+        return l;
+    }
+
+    /**
+     * Returns an {@code Optional} for the {@code Component} related to value
+     * conversion.
+     *
+     * @return the optional of component
+     */
+    public Optional<Component> getComponent() {
+        return Optional.ofNullable(component);
+    }
+
+    /**
+     * Returns an {@code Optional} for the {@code Locale} used in the value
+     * conversion.
+     *
+     * @return the optional of locale
+     */
+    public Optional<Locale> getLocale() {
+        return Optional.ofNullable(locale);
+    }
+}
index 4290620051ec1e1f59dfe745d7c42066e45367a2..4d655e0cbf74238726fe257b4bac1e42d440a3b3 100644 (file)
@@ -37,6 +37,7 @@ import org.jsoup.nodes.Element;
 import org.jsoup.nodes.Node;
 
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ValueContext;
 import com.vaadin.shared.ui.AlignmentInfo;
 import com.vaadin.shared.util.SharedUtil;
 import com.vaadin.ui.Alignment;
@@ -398,7 +399,7 @@ public class DesignAttributeHandler implements Serializable {
         Converter<String, Object> converter = (Converter<String, Object>) getFormatter()
                 .findConverterFor(sourceType);
         if (converter != null) {
-            return converter.convertToPresentation(value, null);
+            return converter.convertToPresentation(value, new ValueContext());
         } else {
             return value.toString();
         }
index 64430470d4f947c9b2a38fa35be3f2ef9870e590..78c3886dd693bc9f067a41d914e002678132edce 100644 (file)
@@ -36,6 +36,7 @@ import com.vaadin.data.util.converter.Converter;
 import com.vaadin.data.util.converter.StringToBigDecimalConverter;
 import com.vaadin.data.util.converter.StringToDoubleConverter;
 import com.vaadin.data.util.converter.StringToFloatConverter;
+import com.vaadin.data.util.converter.ValueContext;
 import com.vaadin.event.ShortcutAction;
 import com.vaadin.server.Resource;
 import com.vaadin.ui.declarative.converters.DesignDateConverter;
@@ -88,12 +89,14 @@ public class DesignFormatter implements Serializable {
         Converter<String, Boolean> booleanConverter = new Converter<String, Boolean>() {
 
             @Override
-            public Result<Boolean> convertToModel(String value, Locale locale) {
+            public Result<Boolean> convertToModel(String value,
+                    ValueContext context) {
                 return Result.ok(!value.equalsIgnoreCase("false"));
             }
 
             @Override
-            public String convertToPresentation(Boolean value, Locale locale) {
+            public String convertToPresentation(Boolean value,
+                    ValueContext context) {
                 if (value.booleanValue()) {
                     return "";
                 } else {
@@ -146,12 +149,14 @@ public class DesignFormatter implements Serializable {
         converterMap.put(String.class, new Converter<String, String>() {
 
             @Override
-            public Result<String> convertToModel(String value, Locale locale) {
+            public Result<String> convertToModel(String value,
+                    ValueContext context) {
                 return Result.ok(value);
             }
 
             @Override
-            public String convertToPresentation(String value, Locale locale) {
+            public String convertToPresentation(String value,
+                    ValueContext context) {
                 return value;
             }
 
@@ -163,7 +168,7 @@ public class DesignFormatter implements Serializable {
 
             @Override
             public Result<Character> convertToModel(String value,
-                    Locale locale) {
+                    ValueContext context) {
                 return Result.ok(value.charAt(0));
             }
 
@@ -226,7 +231,8 @@ public class DesignFormatter implements Serializable {
     public <T> T parse(String value, Class<? extends T> type) {
         Converter<String, T> converter = findConverterFor(type);
         if (converter != null) {
-            Result<T> result = converter.convertToModel(value, null);
+            Result<T> result = converter.convertToModel(value,
+                    new ValueContext());
             return result.getOrThrow(msg -> new IllegalArgumentException(msg));
         } else {
             return null;
@@ -262,7 +268,7 @@ public class DesignFormatter implements Serializable {
         } else {
             Converter<String, Object> converter = findConverterFor(
                     object.getClass());
-            return converter.convertToPresentation(object, null);
+            return converter.convertToPresentation(object, new ValueContext());
         }
     }
 
index 9f97ea6ac9dc602e7a70e44a3d6d72b23a2fe303..9ccdea4ba3e408433ad8da070b7656c16a83b1d7 100644 (file)
@@ -18,10 +18,10 @@ package com.vaadin.ui.declarative.converters;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
-import java.util.Locale;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ValueContext;
 import com.vaadin.ui.declarative.DesignAttributeHandler;
 
 /**
@@ -34,7 +34,7 @@ import com.vaadin.ui.declarative.DesignAttributeHandler;
 public class DesignDateConverter implements Converter<String, Date> {
 
     @Override
-    public Result<Date> convertToModel(String value, Locale locale) {
+    public Result<Date> convertToModel(String value, ValueContext context) {
         for (String pattern : new String[] { "yyyy-MM-dd HH:mm:ssZ",
                 "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH",
                 "yyyy-MM-dd", "yyyy-MM", "yyyy" }) {
@@ -48,7 +48,7 @@ public class DesignDateConverter implements Converter<String, Date> {
     }
 
     @Override
-    public String convertToPresentation(Date value, Locale locale) {
+    public String convertToPresentation(Date value, ValueContext context) {
         return new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ").format(value);
     }
 
index 5a2c3f340c676925b0793b2c119b541c09721957..d5b3c9a9cd3924e18f853b54affffc77195e3bdb 100644 (file)
@@ -19,6 +19,7 @@ import java.util.Locale;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ValueContext;
 import com.vaadin.ui.declarative.DesignAttributeHandler;
 
 /**
@@ -46,7 +47,7 @@ public class DesignEnumConverter<T extends Enum>
 
     @SuppressWarnings("unchecked")
     @Override
-    public Result<T> convertToModel(String value, Locale locale) {
+    public Result<T> convertToModel(String value, ValueContext context) {
         if (value == null || value.trim().equals("")) {
             return Result.ok(null);
         }
@@ -60,7 +61,7 @@ public class DesignEnumConverter<T extends Enum>
     }
 
     @Override
-    public String convertToPresentation(T value, Locale locale) {
+    public String convertToPresentation(T value, ValueContext context) {
         if (value == null) {
             return null;
         }
index 188516b431779ba7729414658320fcf2cd214844..59301ef3863869ff3fcbc82b4f96a8c20c3d6efc 100644 (file)
@@ -22,6 +22,7 @@ import java.util.Locale;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ValueContext;
 import com.vaadin.ui.declarative.DesignAttributeHandler;
 
 /**
@@ -34,12 +35,13 @@ import com.vaadin.ui.declarative.DesignAttributeHandler;
 public class DesignLocalDateConverter implements Converter<String, LocalDate> {
 
     @Override
-    public Result<LocalDate> convertToModel(String value, Locale locale) {
+    public Result<LocalDate> convertToModel(String value,
+            ValueContext context) {
         for (String pattern : new String[] { "yyyy-MM-dd", "yyyy-MM",
                 "yyyy" }) {
             try {
-                Locale effectiveLocale = locale == null ? Locale.ENGLISH
-                        : locale;
+                Locale effectiveLocale = context.getLocale()
+                        .orElse(Locale.ENGLISH);
                 LocalDate date = DateTimeFormatter
                         .ofPattern(pattern, effectiveLocale)
                         .parse(value, LocalDate::from);
@@ -52,9 +54,11 @@ public class DesignLocalDateConverter implements Converter<String, LocalDate> {
     }
 
     @Override
-    public String convertToPresentation(LocalDate value, Locale locale) {
-        return DateTimeFormatter.ofPattern("yyyy-MM-dd",
-                locale == null ? Locale.ENGLISH : locale).format(value);
+    public String convertToPresentation(LocalDate value, ValueContext context) {
+        return DateTimeFormatter
+                .ofPattern("yyyy-MM-dd",
+                        context.getLocale().orElse(Locale.ENGLISH))
+                .format(value);
     }
 
 }
index d8fcfbad718eb3cb529fbf358e0cf7abde3cb35b..cf1c1ef69a335d94af6d416355a86f1fa3087a7b 100644 (file)
  */
 package com.vaadin.ui.declarative.converters;
 
-import java.util.Locale;
-
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ValueContext;
 import com.vaadin.ui.declarative.DesignAttributeHandler;
 
 /**
@@ -31,12 +30,12 @@ import com.vaadin.ui.declarative.DesignAttributeHandler;
 public class DesignObjectConverter implements Converter<String, Object> {
 
     @Override
-    public Result<Object> convertToModel(String value, Locale locale) {
+    public Result<Object> convertToModel(String value, ValueContext context) {
         return Result.ok(value);
     }
 
     @Override
-    public String convertToPresentation(Object value, Locale locale) {
+    public String convertToPresentation(Object value, ValueContext context) {
         if (value == null) {
             return null;
         }
index 1efc382fccb8811659cb79f5bd0c0d009ba5ebe2..ef6e10ef5c57031f206b6f310a758d8ce2314ace 100644 (file)
@@ -23,6 +23,7 @@ import java.util.Map;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ValueContext;
 import com.vaadin.server.ExternalResource;
 import com.vaadin.server.FileResource;
 import com.vaadin.server.FontAwesome;
@@ -44,7 +45,7 @@ import com.vaadin.ui.declarative.DesignAttributeHandler;
 public class DesignResourceConverter implements Converter<String, Resource> {
 
     @Override
-    public Result<Resource> convertToModel(String value, Locale locale) {
+    public Result<Resource> convertToModel(String value, ValueContext context) {
         if (!value.contains("://")) {
             // assume it'is "file://" protocol, one that is used to access a
             // file on a given path on the server, this will later be striped
@@ -63,7 +64,7 @@ public class DesignResourceConverter implements Converter<String, Resource> {
     }
 
     @Override
-    public String convertToPresentation(Resource value, Locale locale) {
+    public String convertToPresentation(Resource value, ValueContext context) {
         ResourceConverterByProtocol byType = ResourceConverterByProtocol
                 .byType(value.getClass());
         if (byType != null) {
index 2468c29c8cc1b15dea8fa27e1a2f547bcc253550..4856a0db8fdeea6f56163cd229452cfafb1e9997 100644 (file)
@@ -17,12 +17,12 @@ package com.vaadin.ui.declarative.converters;
 
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.Locale;
 import java.util.Map;
 import java.util.Map.Entry;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ValueContext;
 import com.vaadin.event.ShortcutAction;
 import com.vaadin.event.ShortcutAction.KeyCode;
 import com.vaadin.event.ShortcutAction.ModifierKey;
@@ -121,7 +121,8 @@ public class DesignShortcutActionConverter
     }
 
     @Override
-    public Result<ShortcutAction> convertToModel(String value, Locale locale) {
+    public Result<ShortcutAction> convertToModel(String value,
+            ValueContext context) {
         if (value.length() == 0) {
             return Result.ok(null);
         }
@@ -159,7 +160,8 @@ public class DesignShortcutActionConverter
     }
 
     @Override
-    public String convertToPresentation(ShortcutAction value, Locale locale) {
+    public String convertToPresentation(ShortcutAction value,
+            ValueContext context) {
         StringBuilder sb = new StringBuilder();
         // handle modifiers
         if (value.getModifiers() != null) {
index d0b262201514e126b1e1956389b2209cd994b5f1..3fe034e5fd055de8609335299011263fd5bd52f3 100644 (file)
  */
 package com.vaadin.ui.declarative.converters;
 
-import java.util.Locale;
 import java.util.TimeZone;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ValueContext;
 import com.vaadin.ui.declarative.DesignAttributeHandler;
 
 /**
@@ -32,7 +32,7 @@ import com.vaadin.ui.declarative.DesignAttributeHandler;
 public class DesignTimeZoneConverter implements Converter<String, TimeZone> {
 
     @Override
-    public Result<TimeZone> convertToModel(String value, Locale locale) {
+    public Result<TimeZone> convertToModel(String value, ValueContext context) {
         if (value == null || value.isEmpty()) {
             return Result.ok(null);
         }
@@ -41,7 +41,7 @@ public class DesignTimeZoneConverter implements Converter<String, TimeZone> {
     }
 
     @Override
-    public String convertToPresentation(TimeZone value, Locale locale) {
+    public String convertToPresentation(TimeZone value, ValueContext context) {
         if (value == null) {
             return "";
         } else {
index d1564e0b8f2a4a6eca16cb72841ed24dfcc10d2f..fa81b36bb7869276d8586481e203035386b36d8a 100644 (file)
 package com.vaadin.ui.declarative.converters;
 
 import java.lang.reflect.InvocationTargetException;
-import java.util.Locale;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ValueContext;
 import com.vaadin.ui.declarative.DesignAttributeHandler;
 
 /**
@@ -72,7 +72,7 @@ public class DesignToStringConverter<TYPE> implements Converter<String, TYPE> {
     }
 
     @Override
-    public Result<TYPE> convertToModel(String value, Locale locale) {
+    public Result<TYPE> convertToModel(String value, ValueContext context) {
         try {
             return Result.ok(type
                     .cast(type.getMethod(this.staticMethodName, String.class)
@@ -85,7 +85,7 @@ public class DesignToStringConverter<TYPE> implements Converter<String, TYPE> {
     }
 
     @Override
-    public String convertToPresentation(TYPE value, Locale locale) {
+    public String convertToPresentation(TYPE value, ValueContext context) {
         if (value == null) {
             return NULL_VALUE_REPRESENTATION;
         } else {
index f80f0781c222c06f0fead655a049db164c90b68f..d9750b989a3eddc2c6550dd797c6e4c47486c7ad 100644 (file)
@@ -17,7 +17,6 @@ package com.vaadin.data;
 
 import java.time.LocalDate;
 import java.util.List;
-import java.util.Locale;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.Collectors;
@@ -31,6 +30,7 @@ import com.vaadin.data.Binder.Binding;
 import com.vaadin.data.ValidationStatus.Status;
 import com.vaadin.data.util.converter.Converter;
 import com.vaadin.data.util.converter.StringToIntegerConverter;
+import com.vaadin.data.util.converter.ValueContext;
 import com.vaadin.data.validator.EmailValidator;
 import com.vaadin.data.validator.StringLengthValidator;
 import com.vaadin.server.AbstractErrorMessage;
@@ -537,7 +537,7 @@ public class BinderBookOfVaadinTest {
     class MyConverter implements Converter<String, Integer> {
         @Override
         public Result<Integer> convertToModel(String fieldValue,
-                Locale locale) {
+                ValueContext context) {
             // Produces a converted value or an error
             try {
                 // ok is a static helper method that creates a Result
@@ -549,7 +549,8 @@ public class BinderBookOfVaadinTest {
         }
 
         @Override
-        public String convertToPresentation(Integer integer, Locale locale) {
+        public String convertToPresentation(Integer integer,
+                ValueContext context) {
             // Converting to the field type should always succeed,
             // so there is no support for returning an error Result.
             return String.valueOf(integer);
index 09e9b7e8ce494be68bed6d88d93770ba840deefd..318cb4e3d93c13e5de438c4fa2e2bbcf8c9f2f4c 100644 (file)
@@ -20,7 +20,6 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 import java.util.Collections;
-import java.util.Locale;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.Collectors;
@@ -30,6 +29,7 @@ import org.junit.Before;
 import org.junit.Test;
 
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ValueContext;
 import com.vaadin.tests.data.bean.BeanWithEnums;
 import com.vaadin.tests.data.bean.TestEnum;
 import com.vaadin.ui.CheckBoxGroup;
@@ -40,14 +40,14 @@ public class BinderMultiSelectTest
             implements Converter<Set<TestEnum>, String> {
         @Override
         public Result<String> convertToModel(Set<TestEnum> value,
-                Locale locale) {
+                ValueContext context) {
             return Result.ok(value.stream().map(TestEnum::name)
                     .collect(Collectors.joining(",")));
         }
 
         @Override
         public Set<TestEnum> convertToPresentation(String value,
-                Locale locale) {
+                ValueContext context) {
             return Stream.of(value.split(","))
                     .filter(string -> !string.isEmpty()).map(TestEnum::valueOf)
                     .collect(Collectors.toSet());
diff --git a/server/src/test/java/com/vaadin/data/ValueContextTest.java b/server/src/test/java/com/vaadin/data/ValueContextTest.java
new file mode 100644 (file)
index 0000000..9fce299
--- /dev/null
@@ -0,0 +1,60 @@
+package com.vaadin.data;
+
+import java.util.Locale;
+import java.util.Objects;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.vaadin.data.util.converter.ValueContext;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.UI;
+
+public class ValueContextTest extends UI {
+
+    private static final Locale UI_LOCALE = Locale.GERMAN;
+    private static final Locale COMPONENT_LOCALE = Locale.FRENCH;
+    private TextField textField;
+
+    @Test
+    public void locale_from_component() {
+        textField.setLocale(COMPONENT_LOCALE);
+        ValueContext fromComponent = new ValueContext(textField);
+        Locale locale = fromComponent.getLocale().orElse(null);
+        Objects.requireNonNull(locale);
+        Assert.assertEquals("Unexpected locale from component",
+                COMPONENT_LOCALE, locale);
+    }
+
+    @Test
+    public void locale_from_ui() {
+        ValueContext fromComponent = new ValueContext(textField);
+        Locale locale = fromComponent.getLocale().orElse(null);
+        Objects.requireNonNull(locale);
+        Assert.assertEquals("Unexpected locale from component", UI_LOCALE,
+                locale);
+    }
+
+    @Test
+    public void default_locale() {
+        setLocale(null);
+        ValueContext fromComponent = new ValueContext(textField);
+        Locale locale = fromComponent.getLocale().orElse(null);
+        Objects.requireNonNull(locale);
+        Assert.assertEquals("Unexpected locale from component",
+                Locale.getDefault(), locale);
+    }
+
+    @Before
+    public void setUp() {
+        setLocale(UI_LOCALE);
+        textField = new TextField();
+        setContent(textField);
+    }
+
+    @Override
+    public void init(VaadinRequest request) {
+    }
+}
index bc9a96b3112603b541cb8b1d762cba05fb317c85..6b25f0a4e7f2df8983c17b66b0f2b05085a1fbe1 100644 (file)
@@ -5,12 +5,14 @@ import org.junit.Test;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ValueContext;
 
 public abstract class AbstractConverterTest {
 
     @Test
     public void testNullConversion() {
-        assertValue(null, getConverter().convertToModel(null, null));
+        assertValue(null,
+                getConverter().convertToModel(null, new ValueContext()));
     }
 
     protected abstract Converter<?, ?> getConverter();
index bf2653da880d7de05702b3d4041c9ec87bf21e83..b4cffaf3255bc43ae68d70e6fbd27297946535eb 100644 (file)
@@ -5,6 +5,7 @@ import org.junit.Test;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ValueContext;
 
 public abstract class AbstractStringConverterTest
         extends AbstractConverterTest {
@@ -15,16 +16,18 @@ public abstract class AbstractStringConverterTest
     @Test
     public void testEmptyStringConversion() {
         assertValue("Null value was converted incorrectly", null,
-                getConverter().convertToModel("", null));
+                getConverter().convertToModel("", new ValueContext()));
     }
 
     @Test
     public void testErrorMessage() {
-        Result<?> result = getConverter().convertToModel("abc", null);
+        Result<?> result = getConverter().convertToModel("abc",
+                new ValueContext());
         Assert.assertTrue(result.isError());
         Assert.assertEquals(getErrorMessage(), result.getMessage().get());
     }
 
+    @Override
     protected String getErrorMessage() {
         return "conversion failed";
     }
index a3d52b052081793ecccc8b1322ea8049c1b4194c..95f200bdf66a0413c3c693ebf78b4bde55e6657d 100644 (file)
@@ -7,6 +7,7 @@ import org.junit.Test;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ValueContext;
 
 public class ConverterTest {
 
@@ -26,10 +27,9 @@ public class ConverterTest {
     @Test
     public void basicConversion() {
         Assert.assertEquals("presentation-123",
-                converter.convertToPresentation("123", null));
+                converter.convertToPresentation("123", new ValueContext()));
         Assert.assertEquals("123",
-                converter.convertToModel("presentation-123", null)
+                converter.convertToModel("presentation-123", new ValueContext())
                         .getOrThrow(msg -> new AssertionError(msg)));
     }
-
 }
index d0eb336616ac0040a1226e0db089b6d211b38d56..75fc42dbb739cbba66ec490c87ac45d6a640fc25 100644 (file)
@@ -5,6 +5,7 @@ import java.util.Date;
 import org.junit.Test;
 
 import com.vaadin.data.util.converter.DateToLongConverter;
+import com.vaadin.data.util.converter.ValueContext;
 
 public class DateToLongConverterTest extends AbstractConverterTest {
 
@@ -16,7 +17,8 @@ public class DateToLongConverterTest extends AbstractConverterTest {
     @Override
     @Test
     public void testNullConversion() {
-        assertValue(null, getConverter().convertToModel(null, null));
+        assertValue(null,
+                getConverter().convertToModel(null, new ValueContext()));
     }
 
     @Test
@@ -25,6 +27,6 @@ public class DateToLongConverterTest extends AbstractConverterTest {
         assertValue(
                 Long.valueOf(946677600000l
                         + (d.getTimezoneOffset() + 120) * 60 * 1000L),
-                getConverter().convertToModel(d, null));
+                getConverter().convertToModel(d, new ValueContext()));
     }
 }
index 6264b81da1403618390da0967cb9a7a3d1abaf78..c1ecf4657b1187cb0ae3a097e6bf54031fd1928b 100644 (file)
@@ -6,6 +6,7 @@ import java.util.Locale;
 import org.junit.Test;
 
 import com.vaadin.data.util.converter.DateToSqlDateConverter;
+import com.vaadin.data.util.converter.ValueContext;
 
 public class DateToSqlDateConverterTest extends AbstractConverterTest {
 
@@ -18,7 +19,7 @@ public class DateToSqlDateConverterTest extends AbstractConverterTest {
     public void testValueConversion() {
         Date testDate = new Date(100, 0, 1);
         long time = testDate.getTime();
-        assertValue(testDate, getConverter()
-                .convertToModel(new java.sql.Date(time), Locale.ENGLISH));
+        assertValue(testDate, getConverter().convertToModel(
+                new java.sql.Date(time), new ValueContext(Locale.ENGLISH)));
     }
 }
index 4481da2eadc7284f0ab12fca351d17dcd2a7daa0..2178cc561ed51cb90d674ec479ac6fef6ebdd69e 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.Test;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.StringToBigDecimalConverter;
+import com.vaadin.data.util.converter.ValueContext;
 
 public class StringToBigDecimalConverterTest
         extends AbstractStringConverterTest {
@@ -35,7 +36,7 @@ public class StringToBigDecimalConverterTest
     @Test
     public void testValueParsing() {
         Result<BigDecimal> converted = getConverter().convertToModel("10",
-                null);
+                new ValueContext());
         BigDecimal expected = new BigDecimal(10);
         assertValue(expected, converted);
     }
@@ -46,7 +47,7 @@ public class StringToBigDecimalConverterTest
         String expected = "12,5";
 
         String converted = getConverter().convertToPresentation(bd,
-                Locale.GERMAN);
+                new ValueContext(Locale.GERMAN));
         Assert.assertEquals(expected, converted);
     }
 }
index 56ed799bbcfe364937a2693fbe31b339c5483945..3e7afd4f550b2db8b9f7f4d20c650fe5e3262d58 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.Test;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.StringToBigIntegerConverter;
+import com.vaadin.data.util.converter.ValueContext;
 
 public class StringToBigIntegerConverterTest
         extends AbstractStringConverterTest {
@@ -36,7 +37,7 @@ public class StringToBigIntegerConverterTest
     public void testValueParsing() {
         String bigInt = "1180591620717411303424"; // 2^70 > 2^63 - 1
         Result<BigInteger> converted = getConverter().convertToModel(bigInt,
-                null);
+                new ValueContext());
         BigInteger expected = new BigInteger(bigInt);
         assertValue("Value bigger than max long was converted incorrectly",
                 expected, converted);
@@ -48,7 +49,7 @@ public class StringToBigIntegerConverterTest
         String expected = "1.000";
 
         String converted = getConverter().convertToPresentation(bd,
-                Locale.GERMAN);
+                new ValueContext(Locale.GERMAN));
         Assert.assertEquals(
                 "Value with specific locale was converted incorrectly",
                 expected, converted);
index 5ca24f64aaf6bd040cd7ae8c0bd10d0a9bbfea20..4de05b9f091a3aa3b9628880cea4781e3b02209d 100644 (file)
@@ -8,6 +8,7 @@ import org.junit.Assert;
 import org.junit.Test;
 
 import com.vaadin.data.util.converter.StringToBooleanConverter;
+import com.vaadin.data.util.converter.ValueContext;
 
 public class StringToBooleanConverterTest extends AbstractStringConverterTest {
 
@@ -43,42 +44,49 @@ public class StringToBooleanConverterTest extends AbstractStringConverterTest {
 
     @Test
     public void testValueConversion() {
-        assertValue(true, getConverter().convertToModel("true", null));
-        assertValue(false, getConverter().convertToModel("false", null));
+        assertValue(true,
+                getConverter().convertToModel("true", new ValueContext()));
+        assertValue(false,
+                getConverter().convertToModel("false", new ValueContext()));
     }
 
     @Test
     public void testYesNoValueConversion() {
-        assertValue(true, yesNoConverter.convertToModel("yes", null));
-        assertValue(false, yesNoConverter.convertToModel("no", null));
+        assertValue(true,
+                yesNoConverter.convertToModel("yes", new ValueContext()));
+        assertValue(false,
+                yesNoConverter.convertToModel("no", new ValueContext()));
 
         Assert.assertEquals("yes",
-                yesNoConverter.convertToPresentation(true, null));
-        Assert.assertEquals("no",
-                yesNoConverter.convertToPresentation(false, null));
+                yesNoConverter.convertToPresentation(true, new ValueContext()));
+        Assert.assertEquals("no", yesNoConverter.convertToPresentation(false,
+                new ValueContext()));
     }
 
     @Test
     public void testEmptyTrueValueConversion() {
-        assertValue(true, emptyTrueConverter.convertToModel("", null));
-        assertValue(false, emptyTrueConverter.convertToModel("ABSENT", null));
+        assertValue(true,
+                emptyTrueConverter.convertToModel("", new ValueContext()));
+        assertValue(false, emptyTrueConverter.convertToModel("ABSENT",
+                new ValueContext()));
 
-        Assert.assertEquals("",
-                emptyTrueConverter.convertToPresentation(true, null));
-        Assert.assertEquals("ABSENT",
-                emptyTrueConverter.convertToPresentation(false, null));
+        Assert.assertEquals("", emptyTrueConverter.convertToPresentation(true,
+                new ValueContext()));
+        Assert.assertEquals("ABSENT", emptyTrueConverter
+                .convertToPresentation(false, new ValueContext()));
     }
 
     @Test
     public void testLocale() {
-        Assert.assertEquals("May 18, 2033",
-                localeConverter.convertToPresentation(true, Locale.US));
-        Assert.assertEquals("January 24, 2065",
-                localeConverter.convertToPresentation(false, Locale.US));
+        Assert.assertEquals("May 18, 2033", localeConverter
+                .convertToPresentation(true, new ValueContext(Locale.US)));
+        Assert.assertEquals("January 24, 2065", localeConverter
+                .convertToPresentation(false, new ValueContext(Locale.US)));
 
-        Assert.assertEquals("18. Mai 2033",
-                localeConverter.convertToPresentation(true, Locale.GERMANY));
+        Assert.assertEquals("18. Mai 2033", localeConverter
+                .convertToPresentation(true, new ValueContext(Locale.GERMANY)));
         Assert.assertEquals("24. Januar 2065",
-                localeConverter.convertToPresentation(false, Locale.GERMANY));
+                localeConverter.convertToPresentation(false,
+                        new ValueContext(Locale.GERMANY)));
     }
 }
index 0fe20888470874294a5be490c9032f44f14a5f09..5319813c0018a1ad9ff512805532d67a1c304427 100644 (file)
@@ -6,6 +6,7 @@ import java.util.Locale;
 import org.junit.Test;
 
 import com.vaadin.data.util.converter.StringToDateConverter;
+import com.vaadin.data.util.converter.ValueContext;
 
 public class StringToDateConverterTest extends AbstractConverterTest {
 
@@ -16,12 +17,13 @@ public class StringToDateConverterTest extends AbstractConverterTest {
 
     @Test
     public void testEmptyStringConversion() {
-        assertValue(null, getConverter().convertToModel("", null));
+        assertValue(null,
+                getConverter().convertToModel("", new ValueContext()));
     }
 
     @Test
     public void testValueConversion() {
-        assertValue(new Date(100, 0, 1), getConverter()
-                .convertToModel("Jan 1, 2000 12:00:00 AM", Locale.ENGLISH));
+        assertValue(new Date(100, 0, 1), getConverter().convertToModel(
+                "Jan 1, 2000 12:00:00 AM", new ValueContext(Locale.ENGLISH)));
     }
 }
index 28a5a7f73f2ac4062ad74b6efe17625783a876b8..ba807e5782cca1fdcfef2d857b34e9906b0f7fa5 100644 (file)
@@ -5,6 +5,7 @@ import org.junit.Test;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.StringToDoubleConverter;
+import com.vaadin.data.util.converter.ValueContext;
 
 public class StringToDoubleConverterTest extends AbstractConverterTest {
 
@@ -15,18 +16,21 @@ public class StringToDoubleConverterTest extends AbstractConverterTest {
 
     @Test
     public void testEmptyStringConversion() {
-        assertValue(null, getConverter().convertToModel("", null));
+        assertValue(null,
+                getConverter().convertToModel("", new ValueContext()));
     }
 
     @Test
     public void testValueConversion() {
-        Result<Double> value = getConverter().convertToModel("10", null);
+        Result<Double> value = getConverter().convertToModel("10",
+                new ValueContext());
         assertValue(10.0d, value);
     }
 
     @Test
     public void testErrorMessage() {
-        Result<Double> result = getConverter().convertToModel("abc", null);
+        Result<Double> result = getConverter().convertToModel("abc",
+                new ValueContext());
         Assert.assertTrue(result.isError());
         Assert.assertEquals("Failed", result.getMessage().get());
     }
index 20b51deb5454a4f911b6a6c2568caf8c4a54f199..8bdf651edee58fe7f693db056a45334807c71ac6 100644 (file)
@@ -3,6 +3,7 @@ package com.vaadin.tests.data.converter;
 import org.junit.Test;
 
 import com.vaadin.data.util.converter.StringToFloatConverter;
+import com.vaadin.data.util.converter.ValueContext;
 
 public class StringToFloatConverterTest extends AbstractStringConverterTest {
 
@@ -14,18 +15,20 @@ public class StringToFloatConverterTest extends AbstractStringConverterTest {
     @Override
     @Test
     public void testNullConversion() {
-        assertValue(null, getConverter().convertToModel(null, null));
+        assertValue(null,
+                getConverter().convertToModel(null, new ValueContext()));
     }
 
     @Override
     @Test
     public void testEmptyStringConversion() {
-        assertValue(null, getConverter().convertToModel("", null));
+        assertValue(null,
+                getConverter().convertToModel("", new ValueContext()));
     }
 
     @Test
     public void testValueConversion() {
         assertValue(Float.valueOf(10),
-                getConverter().convertToModel("10", null));
+                getConverter().convertToModel("10", new ValueContext()));
     }
 }
index a215c2c78544cddd1193dc9ac465c5a94394187b..62fe699395adc62410ab64b460d4e499e88e01ef 100644 (file)
@@ -5,6 +5,7 @@ import org.junit.Test;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.StringToIntegerConverter;
+import com.vaadin.data.util.converter.ValueContext;
 
 public class StringToIntegerConverterTest extends AbstractConverterTest {
 
@@ -15,7 +16,8 @@ public class StringToIntegerConverterTest extends AbstractConverterTest {
 
     @Test
     public void testEmptyStringConversion() {
-        assertValue(null, getConverter().convertToModel("", null));
+        assertValue(null,
+                getConverter().convertToModel("", new ValueContext()));
     }
 
     @Test
@@ -28,7 +30,7 @@ public class StringToIntegerConverterTest extends AbstractConverterTest {
         for (Number value : values) {
             try {
                 getConverter().convertToModel(String.format("%.0f", value),
-                        null);
+                        new ValueContext());
             } catch (Exception e) {
                 accepted = true;
             }
@@ -39,12 +41,13 @@ public class StringToIntegerConverterTest extends AbstractConverterTest {
     @Test
     public void testValueConversion() {
         assertValue(Integer.valueOf(10),
-                getConverter().convertToModel("10", null));
+                getConverter().convertToModel("10", new ValueContext()));
     }
 
     @Test
     public void testErrorMessage() {
-        Result<Integer> result = getConverter().convertToModel("abc", null);
+        Result<Integer> result = getConverter().convertToModel("abc",
+                new ValueContext());
         Assert.assertTrue(result.isError());
         Assert.assertEquals("Failed", result.getMessage().get());
     }
index 818e0f2ce7e4bd178a038b4709565b81328d0b24..0acd463a26e8b3dbc42d65e0503077042f5ffaaa 100644 (file)
@@ -4,6 +4,7 @@ import org.junit.Test;
 
 import com.vaadin.data.Result;
 import com.vaadin.data.util.converter.StringToLongConverter;
+import com.vaadin.data.util.converter.ValueContext;
 
 public class StringToLongConverterTest extends AbstractStringConverterTest {
 
@@ -15,21 +16,23 @@ public class StringToLongConverterTest extends AbstractStringConverterTest {
     @Override
     @Test
     public void testEmptyStringConversion() {
-        assertValue(null, getConverter().convertToModel("", null));
+        assertValue(null,
+                getConverter().convertToModel("", new ValueContext()));
     }
 
     @Test
     public void testValueConversion() {
         assertValue(Long.valueOf(10),
-                getConverter().convertToModel("10", null));
+                getConverter().convertToModel("10", new ValueContext()));
     }
 
     @Test
     public void testExtremeLongValueConversion() {
         Result<Long> l = getConverter().convertToModel("9223372036854775807",
-                null);
+                new ValueContext());
         assertValue(Long.MAX_VALUE, l);
-        l = getConverter().convertToModel("-9223372036854775808", null);
+        l = getConverter().convertToModel("-9223372036854775808",
+                new ValueContext());
         assertValue(Long.MIN_VALUE, l);
     }
 
@@ -37,10 +40,11 @@ public class StringToLongConverterTest extends AbstractStringConverterTest {
     public void testOutOfBoundsValueConversion() {
         // Long.MAX_VALUE+1 is converted to Long.MAX_VALUE
         Result<Long> l = getConverter().convertToModel("9223372036854775808",
-                null);
+                new ValueContext());
         assertValue(Long.MAX_VALUE, l);
         // Long.MIN_VALUE-1 is converted to Long.MIN_VALUE
-        l = getConverter().convertToModel("-9223372036854775809", null);
+        l = getConverter().convertToModel("-9223372036854775809",
+                new ValueContext());
         assertValue(Long.MIN_VALUE, l);
 
     }