diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-10-20 11:36:45 +0300 |
---|---|---|
committer | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-10-24 13:52:17 +0300 |
commit | c72beed9267bcf7d2693fbab87c31904319b1e84 (patch) | |
tree | e7dc9addacf86a16c6e6270adc6135b234452bb7 /server/src/main | |
parent | 6d75c88fcb8c37e4f588bdc09dc183f9d51e4a62 (diff) | |
download | vaadin-framework-c72beed9267bcf7d2693fbab87c31904319b1e84.tar.gz vaadin-framework-c72beed9267bcf7d2693fbab87c31904319b1e84.zip |
Add ValueContext parameter for Converters
Change-Id: I47179b06b9e345f5a454ac1806d0bc9bcac24bcf
Diffstat (limited to 'server/src/main')
24 files changed, 240 insertions, 85 deletions
diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java index 8356a03dda..5493712cec 100644 --- a/server/src/main/java/com/vaadin/data/Binder.java +++ b/server/src/main/java/com/vaadin/data/Binder.java @@ -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; } diff --git a/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java b/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java index c06a9baa61..e7f4476f66 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java @@ -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); } } diff --git a/server/src/main/java/com/vaadin/data/util/converter/Converter.java b/server/src/main/java/com/vaadin/data/util/converter/Converter.java index 97985af9ca..c25c567773 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/Converter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/Converter.java @@ -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); } }; } diff --git a/server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java b/server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java index 2fff4915ee..bd51181f5e 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java @@ -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; } diff --git a/server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java b/server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java index 5695e4bf9d..1fb7584e90 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java @@ -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; } diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java index af0a63fc7a..d30540d120 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java @@ -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); } diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java index d469db7055..186beb9d4b 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java @@ -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(); + } + }); } } diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java index 7b9e1850f0..38b27f8b50 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java @@ -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 { diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java index 2e3e8ab04a..d8a036c002 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java @@ -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); } } diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java index 463a7f6b36..52bf9509c9 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java @@ -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) { diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java index be592665cf..aeeeffcb8b 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java @@ -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) { diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java index ec05647a51..1c98e54c6e 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java @@ -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); diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java index 9b7bfa4fc6..444e7ede8e 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java @@ -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 index 0000000000..a8fdf296f5 --- /dev/null +++ b/server/src/main/java/com/vaadin/data/util/converter/ValueContext.java @@ -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); + } +} diff --git a/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java b/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java index 4290620051..4d655e0cbf 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java +++ b/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java @@ -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(); } diff --git a/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java b/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java index 64430470d4..78c3886dd6 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java @@ -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()); } } diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignDateConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignDateConverter.java index 9f97ea6ac9..9ccdea4ba3 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignDateConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignDateConverter.java @@ -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); } diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignEnumConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignEnumConverter.java index 5a2c3f340c..d5b3c9a9cd 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignEnumConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignEnumConverter.java @@ -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; } diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignLocalDateConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignLocalDateConverter.java index 188516b431..59301ef386 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignLocalDateConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignLocalDateConverter.java @@ -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); } } diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignObjectConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignObjectConverter.java index d8fcfbad71..cf1c1ef69a 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignObjectConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignObjectConverter.java @@ -15,10 +15,9 @@ */ 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; } diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignResourceConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignResourceConverter.java index 1efc382fcc..ef6e10ef5c 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignResourceConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignResourceConverter.java @@ -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) { diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java index 2468c29c8c..4856a0db8f 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java @@ -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) { diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java index d0b2622015..3fe034e5fd 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java @@ -15,11 +15,11 @@ */ 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 { diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignToStringConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignToStringConverter.java index d1564e0b8f..fa81b36bb7 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignToStringConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignToStringConverter.java @@ -16,10 +16,10 @@ 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 { |