From 192e93a814f05dc8c5cc7a01b7264bc0a8a9dec7 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Wed, 10 Aug 2016 10:02:21 +0300 Subject: Move and rename converters into com.vaadin.legacy.data.util.converter.Legacy* Change-Id: I75fd33e66c8c5c265bc60cce58ff639a5d5642ab --- .../com/vaadin/data/util/PropertyFormatter.java | 4 +- .../converter/AbstractStringToNumberConverter.java | 123 ---------- .../com/vaadin/data/util/converter/Converter.java | 176 -------------- .../data/util/converter/ConverterFactory.java | 33 --- .../vaadin/data/util/converter/ConverterUtil.java | 261 --------------------- .../data/util/converter/DateToLongConverter.java | 89 ------- .../util/converter/DateToSqlDateConverter.java | 81 ------- .../util/converter/DefaultConverterFactory.java | 127 ---------- .../data/util/converter/ReverseConverter.java | 96 -------- .../converter/StringToBigDecimalConverter.java | 60 ----- .../converter/StringToBigIntegerConverter.java | 67 ------ .../util/converter/StringToBooleanConverter.java | 167 ------------- .../data/util/converter/StringToByteConverter.java | 89 ------- .../converter/StringToCollectionConverter.java | 243 ------------------- .../data/util/converter/StringToDateConverter.java | 131 ----------- .../util/converter/StringToDoubleConverter.java | 64 ----- .../data/util/converter/StringToEnumConverter.java | 160 ------------- .../util/converter/StringToFloatConverter.java | 64 ----- .../util/converter/StringToIntegerConverter.java | 94 -------- .../data/util/converter/StringToLongConverter.java | 78 ------ .../util/converter/StringToShortConverter.java | 90 ------- .../data/util/sqlcontainer/ColumnProperty.java | 2 +- .../LegacyAbstractStringToNumberConverter.java | 123 ++++++++++ .../data/util/converter/LegacyConverter.java | 176 ++++++++++++++ .../util/converter/LegacyConverterFactory.java | 33 +++ .../data/util/converter/LegacyConverterUtil.java | 261 +++++++++++++++++++++ .../util/converter/LegacyDateToLongConverter.java | 89 +++++++ .../converter/LegacyDateToSqlDateConverter.java | 81 +++++++ .../converter/LegacyDefaultConverterFactory.java | 127 ++++++++++ .../util/converter/LegacyReverseConverter.java | 96 ++++++++ .../LegacyStringToBigDecimalConverter.java | 60 +++++ .../LegacyStringToBigIntegerConverter.java | 67 ++++++ .../converter/LegacyStringToBooleanConverter.java | 167 +++++++++++++ .../converter/LegacyStringToByteConverter.java | 89 +++++++ .../LegacyStringToCollectionConverter.java | 243 +++++++++++++++++++ .../converter/LegacyStringToDateConverter.java | 131 +++++++++++ .../converter/LegacyStringToDoubleConverter.java | 64 +++++ .../converter/LegacyStringToEnumConverter.java | 160 +++++++++++++ .../converter/LegacyStringToFloatConverter.java | 64 +++++ .../converter/LegacyStringToIntegerConverter.java | 94 ++++++++ .../converter/LegacyStringToLongConverter.java | 78 ++++++ .../converter/LegacyStringToShortConverter.java | 90 +++++++ .../data/validator/LegacyDoubleValidator.java | 4 +- .../data/validator/LegacyIntegerValidator.java | 4 +- .../com/vaadin/legacy/ui/LegacyAbstractField.java | 42 ++-- .../main/java/com/vaadin/server/VaadinSession.java | 30 +-- .../main/java/com/vaadin/ui/AbstractSelect.java | 10 +- server/src/main/java/com/vaadin/ui/DateField.java | 10 +- server/src/main/java/com/vaadin/ui/Grid.java | 38 +-- server/src/main/java/com/vaadin/ui/Label.java | 18 +- server/src/main/java/com/vaadin/ui/Table.java | 18 +- .../ui/declarative/DesignAttributeHandler.java | 4 +- .../com/vaadin/ui/declarative/DesignFormatter.java | 56 ++--- .../converters/DesignDateConverter.java | 8 +- .../converters/DesignEnumConverter.java | 8 +- .../converters/DesignObjectConverter.java | 8 +- .../converters/DesignResourceConverter.java | 20 +- .../converters/DesignShortcutActionConverter.java | 8 +- .../converters/DesignTimeZoneConverter.java | 8 +- .../converters/DesignToStringConverter.java | 20 +- 60 files changed, 2453 insertions(+), 2453 deletions(-) delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/Converter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/ConverterFactory.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/ConverterUtil.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/DefaultConverterFactory.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/ReverseConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToByteConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToCollectionConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToEnumConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToShortConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyAbstractStringToNumberConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterFactory.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterUtil.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToLongConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToSqlDateConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDefaultConverterFactory.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyReverseConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigDecimalConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigIntegerConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBooleanConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToByteConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToCollectionConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDateConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDoubleConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToEnumConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToFloatConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToIntegerConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToLongConverter.java create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToShortConverter.java (limited to 'server/src/main') diff --git a/server/src/main/java/com/vaadin/data/util/PropertyFormatter.java b/server/src/main/java/com/vaadin/data/util/PropertyFormatter.java index e188ce8bc3..b3df70534b 100644 --- a/server/src/main/java/com/vaadin/data/util/PropertyFormatter.java +++ b/server/src/main/java/com/vaadin/data/util/PropertyFormatter.java @@ -16,7 +16,7 @@ package com.vaadin.data.util; import com.vaadin.data.Property; -import com.vaadin.data.util.converter.Converter; +import com.vaadin.legacy.data.util.converter.LegacyConverter; /** * Formatting proxy for a {@link Property}. @@ -46,7 +46,7 @@ import com.vaadin.data.util.converter.Converter; * type of the underlying property (a PropertyFormatter is always a * Property<String>) * - * @deprecated As of 7.0, replaced by {@link Converter} + * @deprecated As of 7.0, replaced by {@link LegacyConverter} * @author Vaadin Ltd. * @since 5.3.0 */ 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 deleted file mode 100644 index 81f1a85f89..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright 2000-2014 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.text.NumberFormat; -import java.text.ParsePosition; -import java.util.Locale; - -/** - * A converter that converts from the number type T to {@link String} and back. - * Uses the given locale and {@link NumberFormat} for formatting and parsing. - * Automatically trims the input string, removing any leading and trailing white - * space. - *

- * Override and overwrite {@link #getFormat(Locale)} to use a different format. - *

- * - * @author Vaadin Ltd - * @since 7.1 - */ -public abstract class AbstractStringToNumberConverter implements - Converter { - - /** - * Returns the format used by {@link #convertToPresentation(Object, Locale)} - * and {@link #convertToModel(Object, Locale)}. - * - * @param locale - * The locale to use - * @return A NumberFormat instance - * @since 7.1 - */ - protected NumberFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - - return NumberFormat.getNumberInstance(locale); - } - - /** - * Convert the value to a Number using the given locale and - * {@link #getFormat(Locale)}. - * - * @param value - * The value to convert - * @param locale - * The locale to use for conversion - * @return The converted value - * @throws ConversionException - * If there was a problem converting the value - * @since 7.1 - */ - protected Number convertToNumber(String value, - Class targetType, Locale locale) - throws ConversionException { - if (value == null) { - return null; - } - - // Remove leading and trailing white space - value = value.trim(); - - // Parse and detect errors. If the full string was not used, it is - // an error. - ParsePosition parsePosition = new ParsePosition(0); - Number parsedValue = getFormat(locale).parse(value, parsePosition); - if (parsePosition.getIndex() != value.length()) { - throw new ConversionException("Could not convert '" + value - + "' to " + getModelType().getName()); - } - - if (parsedValue == null) { - // Convert "" to null - return null; - } - - return parsedValue; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang - * .Object, java.util.Locale) - */ - @Override - public String convertToPresentation(T value, - Class targetType, Locale locale) - throws ConversionException { - if (value == null) { - return null; - } - - return getFormat(locale).format(value); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getPresentationType() - */ - @Override - public Class getPresentationType() { - return String.class; - } - -} 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 deleted file mode 100644 index a7188ebb33..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/Converter.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright 2000-2014 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; - -/** - * Interface that implements conversion between a model and a presentation type. - *

- * Typically {@link #convertToPresentation(Object, Class, Locale)} and - * {@link #convertToModel(Object, Class, Locale)} should be symmetric so that - * chaining these together returns the original result for all input but this is - * not a requirement. - *

- *

- * Converters must not have any side effects (never update UI from inside a - * converter). - *

- *

- * All Converters must be stateless and thread safe. - *

- *

- * If conversion of a value fails, a {@link ConversionException} is thrown. - *

- * - * @param - * The presentation type. Must be compatible with what - * {@link #getPresentationType()} returns. - * @param - * The model type. Must be compatible with what - * {@link #getModelType()} returns. - * @author Vaadin Ltd. - * @since 7.0 - */ -public interface Converter extends Serializable { - - /** - * Converts the given value from target type to source type. - *

- * A converter can optionally use locale to do the conversion. - *

- * A converter should in most cases be symmetric so chaining - * {@link #convertToPresentation(Object, Class, Locale)} and - * {@link #convertToModel(Object, Class, Locale)} should return the original - * value. - * - * @param value - * The value to convert, compatible with the target type. Can be - * null - * @param targetType - * The requested type of the return value - * @param locale - * The locale to use for conversion. Can be null. - * @return The converted value compatible with the source type - * @throws ConversionException - * If the value could not be converted - */ - public MODEL convertToModel(PRESENTATION value, - Class targetType, Locale locale) - throws ConversionException; - - /** - * Converts the given value from source type to target type. - *

- * A converter can optionally use locale to do the conversion. - *

- * A converter should in most cases be symmetric so chaining - * {@link #convertToPresentation(Object, Class, Locale)} and - * {@link #convertToModel(Object, Class, Locale)} should return the original - * value. - * - * @param value - * The value to convert, compatible with the target type. Can be - * null - * @param targetType - * The requested type of the return value - * @param locale - * The locale to use for conversion. Can be null. - * @return The converted value compatible with the source type - * @throws ConversionException - * If the value could not be converted - */ - public PRESENTATION convertToPresentation(MODEL value, - Class targetType, Locale locale) - throws ConversionException; - - /** - * The source type of the converter. - * - * Values of this type can be passed to - * {@link #convertToPresentation(Object, Class, Locale)}. - * - * @return The source type - */ - public Class getModelType(); - - /** - * The target type of the converter. - * - * Values of this type can be passed to - * {@link #convertToModel(Object, Class, Locale)}. - * - * @return The target type - */ - public Class getPresentationType(); - - /** - * An exception that signals that the value passed to - * {@link Converter#convertToPresentation(Object, Class, Locale)} or - * {@link Converter#convertToModel(Object, Class, Locale)} could not be - * converted. - * - * @author Vaadin Ltd - * @since 7.0 - */ - public static class ConversionException extends RuntimeException { - - /** - * Constructs a new ConversionException without a detail - * message. - */ - public ConversionException() { - } - - /** - * Constructs a new ConversionException with the specified - * detail message. - * - * @param msg - * the detail message - */ - public ConversionException(String msg) { - super(msg); - } - - /** - * Constructs a new {@code ConversionException} with the specified - * cause. - * - * @param cause - * The cause of the the exception - */ - public ConversionException(Throwable cause) { - super(cause); - } - - /** - * Constructs a new ConversionException with the specified - * detail message and cause. - * - * @param message - * the detail message - * @param cause - * The cause of the the exception - */ - public ConversionException(String message, Throwable cause) { - super(message, cause); - } - } - -} diff --git a/server/src/main/java/com/vaadin/data/util/converter/ConverterFactory.java b/server/src/main/java/com/vaadin/data/util/converter/ConverterFactory.java deleted file mode 100644 index 0280be4a73..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/ConverterFactory.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2000-2014 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; - -/** - * Factory interface for providing Converters based on a presentation type and a - * model type. - * - * @author Vaadin Ltd. - * @since 7.0 - * - */ -public interface ConverterFactory extends Serializable { - public Converter createConverter( - Class presentationType, Class modelType); - -} diff --git a/server/src/main/java/com/vaadin/data/util/converter/ConverterUtil.java b/server/src/main/java/com/vaadin/data/util/converter/ConverterUtil.java deleted file mode 100644 index 91e11a6222..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/ConverterUtil.java +++ /dev/null @@ -1,261 +0,0 @@ -/* - * Copyright 2000-2014 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 com.vaadin.server.VaadinSession; - -public class ConverterUtil implements Serializable { - - /** - * Finds a converter that can convert from the given presentation type to - * the given model type and back. Uses the given application to find a - * {@link ConverterFactory} or, if application is null, uses the - * {@link VaadinSession#getCurrent()}. - * - * @param - * the presentation type - * @param - * the model type - * @param presentationType - * the presentation type - * @param modelType - * the model type - * @param session - * the session to use to find a ConverterFactory or null to use - * the current session - * @return a Converter capable of converting between the given types or null - * if no converter was found - */ - public static Converter getConverter( - Class presentationType, - Class modelType, VaadinSession session) { - Converter converter = null; - if (session == null) { - session = VaadinSession.getCurrent(); - } - - if (session != null) { - ConverterFactory factory = session.getConverterFactory(); - converter = factory.createConverter(presentationType, modelType); - } - return converter; - - } - - /** - * Convert the given value from the data source type to the UI type. - * - * @param modelValue - * the model value to convert - * @param presentationType - * the type of the presentation value - * @param converter - * the converter to use - * @param locale - * the locale to use for conversion - * @param - * the presentation type - * @param - * the model type - * - * @return the converted value, compatible with the presentation type, or - * the original value if its type is compatible and no converter is - * set. - * @throws Converter.ConversionException - * if there was a problem converting the value - */ - @SuppressWarnings("unchecked") - public static PRESENTATIONTYPE convertFromModel( - MODELTYPE modelValue, - Class presentationType, - Converter converter, Locale locale) - throws Converter.ConversionException { - if (converter != null) { - /* - * If there is a converter, always use it. It must convert or throw - * an exception. - */ - PRESENTATIONTYPE presentation = converter.convertToPresentation( - modelValue, presentationType, locale); - if (presentation != null - && !presentationType.isInstance(presentation)) { - throw new Converter.ConversionException( - "Converter returned an object of type " - + presentation.getClass().getName() - + " when expecting " - + presentationType.getName()); - } - - return presentation; - } - if (modelValue == null) { - // Null should always be passed through the converter but if there - // is no converter we can safely return null - return null; - } - - if (presentationType.isAssignableFrom(modelValue.getClass())) { - return (PRESENTATIONTYPE) modelValue; - } else { - throw new Converter.ConversionException( - "Unable to convert value of type " - + modelValue.getClass().getName() - + " to presentation type " - + presentationType - + ". No converter is set and the types are not compatible."); - } - } - - /** - * Convert the given value from the presentation (UI) type to model (data - * source) type. - * - * @param presentationValue - * the presentation value to convert - * @param modelType - * the type of the model - * @param converter - * the converter to use - * @param locale - * the locale to use for conversion - * @param - * the presentation type - * @param - * the model type - * - * @return the converted value, compatible with the model type, or the - * original value if its type is compatible and no converter is set. - * @throws Converter.ConversionException - * if there was a problem converting the value - */ - public static MODELTYPE convertToModel( - PRESENTATIONTYPE presentationValue, Class modelType, - Converter converter, Locale locale) - throws Converter.ConversionException { - if (converter != null) { - /* - * If there is a converter, always use it. It must convert or throw - * an exception. - */ - MODELTYPE model = converter.convertToModel(presentationValue, - modelType, locale); - if (model != null && !modelType.isInstance(model)) { - throw new Converter.ConversionException( - "Converter returned an object of type " - + model.getClass().getName() - + " when expecting " + modelType.getName()); - } - - return model; - } - - if (presentationValue == null) { - // Null should always be passed through the converter but if there - // is no converter we can safely return null - return null; - } - - if (modelType == null) { - // No model type, return original value - return (MODELTYPE) presentationValue; - } else if (modelType.isAssignableFrom(presentationValue.getClass())) { - // presentation type directly compatible with model type - return modelType.cast(presentationValue); - } else { - throw new Converter.ConversionException( - "Unable to convert value of type " - + presentationValue.getClass().getName() - + " to model type " - + modelType - + ". No converter is set and the types are not compatible."); - } - - } - - /** - * Checks if the given converter can handle conversion between the given - * presentation and model type. Does strict type checking and only returns - * true if the converter claims it can handle exactly the given types. - * - * @see #canConverterPossiblyHandle(Converter, Class, Class) - * - * @param converter - * The converter to check. If this is null the result is always - * false. - * @param presentationType - * The presentation type - * @param modelType - * The model type - * @return true if the converter supports conversion between the given - * presentation and model type, false otherwise - */ - public static boolean canConverterHandle(Converter converter, - Class presentationType, Class modelType) { - if (converter == null) { - return false; - } - - if (modelType != converter.getModelType()) { - return false; - } - if (presentationType != converter.getPresentationType()) { - return false; - } - - return true; - } - - /** - * Checks if it possible that the given converter can handle conversion - * between the given presentation and model type somehow. - * - * @param converter - * The converter to check. If this is null the result is always - * false. - * @param presentationType - * The presentation type - * @param modelType - * The model type - * @return true if the converter possibly support conversion between the - * given presentation and model type, false otherwise - */ - public static boolean canConverterPossiblyHandle(Converter converter, - Class presentationType, Class modelType) { - if (converter == null) { - return false; - } - Class converterModelType = converter.getModelType(); - - if (!modelType.isAssignableFrom(converterModelType) - && !converterModelType.isAssignableFrom(modelType)) { - // model types are not compatible in any way - return false; - } - - Class converterPresentationType = converter.getPresentationType(); - if (!presentationType.isAssignableFrom(converterPresentationType) - && !converterPresentationType - .isAssignableFrom(presentationType)) { - // presentation types are not compatible in any way - return false; - } - - return true; - } -} 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 deleted file mode 100644 index fffe80352e..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2000-2014 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.util.Date; -import java.util.Locale; - -/** - * A converter that converts from {@link Long} to {@link Date} and back. - * - * @author Vaadin Ltd - * @since 7.0 - */ -public class DateToLongConverter implements Converter { - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.lang.Class, java.util.Locale) - */ - @Override - public Long convertToModel(Date value, Class targetType, - Locale locale) { - if (value == null) { - return null; - } - - return value.getTime(); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang - * .Object, java.lang.Class, java.util.Locale) - */ - @Override - public Date convertToPresentation(Long value, - Class targetType, Locale locale) { - if (targetType != getPresentationType()) { - throw new ConversionException("Converter only supports " - + getPresentationType().getName() + " (targetType was " - + targetType.getName() + ")"); - } - if (value == null) { - return null; - } - - return new Date(value); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class getModelType() { - return Long.class; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getPresentationType() - */ - @Override - public Class getPresentationType() { - return Date.class; - } - -} 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 deleted file mode 100644 index 6a7e8327a1..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2000-2014 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.util.Date; -import java.util.Locale; - -/** - * Converter for handling conversion between {@link java.util.Date} and - * {@link java.sql.Date}. This is used when a PopupDateField or InlineDateField - * is connected to a java.sql.Date property, typically through a JPAContainer or - * SQLContainer. Note that information (time information) is lost when - * converting from {@link java.util.Date} to {@link java.sql.Date}. - * - * @since 7.1 - * @author Vaadin Ltd - */ -public class DateToSqlDateConverter implements Converter { - - @Override - public java.sql.Date convertToModel(Date value, - Class targetType, Locale locale) - throws ConversionException { - if (targetType != getModelType()) { - throw new ConversionException("Converter only supports " - + getModelType().getName() + " (targetType was " - + targetType.getName() + ")"); - } - - if (value == null) { - return null; - } - - return new java.sql.Date(value.getTime()); - } - - @Override - public Date convertToPresentation(java.sql.Date value, - Class targetType, Locale locale) - throws ConversionException { - if (targetType != getPresentationType()) { - throw new ConversionException("Converter only supports " - + getPresentationType().getName() + " (targetType was " - + targetType.getName() + ")"); - } - - if (value == null) { - return null; - } - - return new Date(value.getTime()); - } - - @Override - public Class getModelType() { - return java.sql.Date.class; - } - - @Override - public Class getPresentationType() { - return Date.class; - } - -} diff --git a/server/src/main/java/com/vaadin/data/util/converter/DefaultConverterFactory.java b/server/src/main/java/com/vaadin/data/util/converter/DefaultConverterFactory.java deleted file mode 100644 index 3a1f1a4252..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/DefaultConverterFactory.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright 2000-2014 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.math.BigDecimal; -import java.math.BigInteger; -import java.util.Date; -import java.util.logging.Logger; - -import com.vaadin.server.VaadinSession; - -/** - * Default implementation of {@link ConverterFactory}. Provides converters for - * standard types like {@link String}, {@link Double} and {@link Date}.

- *

- * Custom converters can be provided by extending this class and using - * {@link VaadinSession#setConverterFactory(ConverterFactory)}. - *

- * - * @author Vaadin Ltd - * @since 7.0 - */ -public class DefaultConverterFactory implements ConverterFactory { - - private final static Logger log = Logger - .getLogger(DefaultConverterFactory.class.getName()); - - @Override - public Converter createConverter( - Class presentationType, Class modelType) { - Converter converter = findConverter( - presentationType, modelType); - if (converter != null) { - log.finest(getClass().getName() + " created a " - + converter.getClass()); - return converter; - } - - // Try to find a reverse converter - Converter reverseConverter = findConverter( - modelType, presentationType); - if (reverseConverter != null) { - log.finest(getClass().getName() + " created a reverse " - + reverseConverter.getClass()); - return new ReverseConverter(reverseConverter); - } - - log.finest(getClass().getName() + " could not find a converter for " - + presentationType.getName() + " to " + modelType.getName() - + " conversion"); - return null; - - } - - protected Converter findConverter( - Class presentationType, Class modelType) { - if (presentationType == String.class) { - // TextField converters and more - Converter converter = (Converter) createStringConverter(modelType); - if (converter != null) { - return converter; - } - } else if (presentationType == Date.class) { - // DateField converters and more - Converter converter = (Converter) createDateConverter(modelType); - if (converter != null) { - return converter; - } - } - - return null; - - } - - protected Converter createDateConverter(Class sourceType) { - if (Long.class.isAssignableFrom(sourceType)) { - return new DateToLongConverter(); - } else if (java.sql.Date.class.isAssignableFrom(sourceType)) { - return new DateToSqlDateConverter(); - } else { - return null; - } - } - - protected Converter createStringConverter(Class sourceType) { - if (Double.class.isAssignableFrom(sourceType)) { - return new StringToDoubleConverter(); - } else if (Float.class.isAssignableFrom(sourceType)) { - return new StringToFloatConverter(); - } else if (Integer.class.isAssignableFrom(sourceType)) { - return new StringToIntegerConverter(); - } else if (Long.class.isAssignableFrom(sourceType)) { - return new StringToLongConverter(); - } else if (BigDecimal.class.isAssignableFrom(sourceType)) { - return new StringToBigDecimalConverter(); - } else if (Boolean.class.isAssignableFrom(sourceType)) { - return new StringToBooleanConverter(); - } else if (Date.class.isAssignableFrom(sourceType)) { - return new StringToDateConverter(); - } else if (Enum.class.isAssignableFrom(sourceType)) { - return new StringToEnumConverter(); - } else if (BigInteger.class.isAssignableFrom(sourceType)) { - return new StringToBigIntegerConverter(); - } else if (Short.class.isAssignableFrom(sourceType)) { - return new StringToShortConverter(); - } else if (Byte.class.isAssignableFrom(sourceType)) { - return new StringToByteConverter(); - } else { - return null; - } - } - -} diff --git a/server/src/main/java/com/vaadin/data/util/converter/ReverseConverter.java b/server/src/main/java/com/vaadin/data/util/converter/ReverseConverter.java deleted file mode 100644 index 2495dc631a..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/ReverseConverter.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 2000-2014 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.util.Locale; - -/** - * A converter that wraps another {@link Converter} and reverses source and - * target types. - * - * @param - * The source type - * @param - * The target type - * - * @author Vaadin Ltd - * @since 7.0 - */ -public class ReverseConverter implements - Converter { - - private Converter realConverter; - - /** - * Creates a converter from source to target based on a converter that - * converts from target to source. - * - * @param converter - * The converter to use in a reverse fashion - */ - public ReverseConverter(Converter converter) { - this.realConverter = converter; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#convertToModel(java - * .lang.Object, java.util.Locale) - */ - @Override - public MODEL convertToModel(PRESENTATION value, - Class targetType, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { - return realConverter.convertToPresentation(value, targetType, locale); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang - * .Object, java.util.Locale) - */ - @Override - public PRESENTATION convertToPresentation(MODEL value, - Class targetType, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { - return realConverter.convertToModel(value, targetType, locale); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getSourceType() - */ - @Override - public Class getModelType() { - return realConverter.getPresentationType(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getTargetType() - */ - @Override - public Class getPresentationType() { - return realConverter.getModelType(); - } - -} 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 deleted file mode 100644 index 549f156f61..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2000-2014 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.math.BigDecimal; -import java.text.DecimalFormat; -import java.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link BigDecimal} and back. - * Uses the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - *

- * Leading and trailing white spaces are ignored when converting from a String. - *

- *

- * Override and overwrite {@link #getFormat(Locale)} to use a different format. - *

- * - * @author Vaadin Ltd - * @since 7.2 - */ -public class StringToBigDecimalConverter extends - AbstractStringToNumberConverter { - @Override - protected NumberFormat getFormat(Locale locale) { - NumberFormat numberFormat = super.getFormat(locale); - if (numberFormat instanceof DecimalFormat) { - ((DecimalFormat) numberFormat).setParseBigDecimal(true); - } - - return numberFormat; - } - - @Override - public BigDecimal convertToModel(String value, - Class targetType, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { - return (BigDecimal) convertToNumber(value, BigDecimal.class, locale); - } - - @Override - public Class getModelType() { - return BigDecimal.class; - } -} 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 deleted file mode 100644 index adaa8c6111..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2000-2014 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.math.BigDecimal; -import java.math.BigInteger; -import java.text.DecimalFormat; -import java.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link BigInteger} and back. - * Uses the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - *

- * Leading and trailing white spaces are ignored when converting from a String. - *

- *

- * Override and overwrite {@link #getFormat(Locale)} to use a different format. - *

- * - * @author Vaadin Ltd - * @since 7.4 - */ -public class StringToBigIntegerConverter extends - AbstractStringToNumberConverter { - - @Override - protected NumberFormat getFormat(Locale locale) { - NumberFormat numberFormat = super.getFormat(locale); - if (numberFormat instanceof DecimalFormat) { - ((DecimalFormat) numberFormat).setParseBigDecimal(true); - } - - return numberFormat; - } - - @Override - public BigInteger convertToModel(String value, - Class targetType, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { - - BigDecimal bigDecimalValue = (BigDecimal) convertToNumber(value, - BigDecimal.class, locale); - - return (bigDecimalValue != null) ? bigDecimalValue.toBigInteger() - : null; - } - - @Override - public Class getModelType() { - return BigInteger.class; - } -} 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 deleted file mode 100644 index f965cfcc6a..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright 2000-2014 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.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Boolean} and back. The String representation is given by - * {@link Boolean#toString()} or provided in constructor {@link #StringToBooleanConverter(String, String)}. - *

Leading and trailing white spaces are ignored when converting from a String.

- *

For language-dependent representation, subclasses should overwrite {@link #getFalseString(Locale)} and {@link #getTrueString(Locale)}

- * - * @author Vaadin Ltd - * @since 7.0 - */ -public class StringToBooleanConverter implements Converter { - - private final String trueString; - - private final String falseString; - - /** - * Creates converter with default string representations - "true" and "false" - * - */ - public StringToBooleanConverter() { - this(Boolean.TRUE.toString(), Boolean.FALSE.toString()); - } - - /** - * Creates converter with custom string representation. - * - * @since 7.5.4 - * @param falseString string representation for false - * @param trueString string representation for true - */ - public StringToBooleanConverter(String trueString, String falseString) { - this.trueString = trueString; - this.falseString = falseString; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.lang.Class, java.util.Locale) - */ - @Override - public Boolean convertToModel(String value, - Class targetType, Locale locale) - throws ConversionException { - if (value == null || value.isEmpty()) { - return null; - } - - // Remove leading and trailing white space - value = value.trim(); - - if (getTrueString().equals(value)) { - return true; - } else if (getFalseString().equals(value)) { - return false; - } else { - throw new ConversionException("Cannot convert " + value + " to " - + getModelType().getName()); - } - } - - /** - * Gets the string representation for true. Default is "true", if not set in constructor. - * - * @return the string representation for true - */ - protected String getTrueString() { - return trueString; - } - - /** - * Gets the string representation for false. Default is "false", if not set in constructor. - * - * @return the string representation for false - */ - protected String getFalseString() { - return falseString; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang - * .Object, java.lang.Class, java.util.Locale) - */ - @Override - public String convertToPresentation(Boolean value, - Class targetType, Locale locale) - throws ConversionException { - if (value == null) { - return null; - } - if (value) { - return getTrueString(locale); - } else { - return getFalseString(locale); - } - } - - /** - * Gets the locale-depended string representation for false. - * Default is locale-independent value provided by {@link #getFalseString()} - * - * @since 7.5.4 - * @param locale to be used - * @return the string representation for false - */ - protected String getFalseString(Locale locale) { - return getFalseString(); - } - - /** - * Gets the locale-depended string representation for true. - * Default is locale-independent value provided by {@link #getTrueString()} - * - * @since 7.5.4 - * @param locale to be used - * @return the string representation for true - */ - protected String getTrueString(Locale locale) { - return getTrueString(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class getModelType() { - return Boolean.class; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getPresentationType() - */ - @Override - public Class getPresentationType() { - return String.class; - } - -} diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToByteConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToByteConverter.java deleted file mode 100644 index 719fced0ed..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToByteConverter.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2000-2014 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.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Byte} and back. Uses - * the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - *

- * Override and overwrite {@link #getFormat(Locale)} to use a different format. - *

- * - * @author Vaadin Ltd - * @since 7.4 - */ -public class StringToByteConverter extends - AbstractStringToNumberConverter { - - /** - * Returns the format used by - * {@link #convertToPresentation(Byte, Class, Locale)} and - * {@link #convertToModel(String, Class, Locale)} - * - * @param locale - * The locale to use - * @return A NumberFormat instance - */ - @Override - protected NumberFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - return NumberFormat.getIntegerInstance(locale); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.lang.Class, java.util.Locale) - */ - @Override - public Byte convertToModel(String value, Class targetType, - Locale locale) throws ConversionException { - Number n = convertToNumber(value, targetType, locale); - - if (n == null) { - return null; - } - - byte byteValue = n.byteValue(); - if (byteValue == n.longValue()) { - return byteValue; - } - - throw new ConversionException("Could not convert '" + value + "' to " - + Byte.class.getName() + ": value out of range"); - - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class getModelType() { - return Byte.class; - } - -} diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToCollectionConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToCollectionConverter.java deleted file mode 100644 index 6930b6da06..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToCollectionConverter.java +++ /dev/null @@ -1,243 +0,0 @@ -/* - * Copyright 2000-2014 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.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Collection} of tokens - * and back. - *

- * Allows to break a string into tokens using delimiter. Each token can be - * converted to its own model using provided converter. - *

- * Default constructor uses ", " as delimiter string and - * {@link String} for token types. Other constructors allow to configure - * delimiter and token types. - * - * @since 7.5.0 - * - * @author Vaadin Ltd - */ -public class StringToCollectionConverter implements - Converter { - - private final String delimiter; - private final Converter tokenConverter; - private final Class tokenType; - private final CollectionFactory factory; - - /** - * Creates converter with ", " as delimiter and {@link String} - * as token model type in collection. - */ - public StringToCollectionConverter() { - this(", ", null, String.class); - } - - /** - * Creates converter with given {@code delimiter} and {@link String} as - * token model type in collection. - * - * @param delimiter - * custom delimiter - */ - public StringToCollectionConverter(String delimiter) { - this(delimiter, null, String.class); - } - - /** - * Creates converter with given {@code tokenConverter} for convert tokens - * and expected {@code tokenType}. - *

- * If {@code tokenConverter} is null then no conversation is done and - * {@link String} is used as token type in resulting model collection. - * - * @param tokenConverter - * converter for token - * @param tokenType - * expected token model type - */ - public StringToCollectionConverter(Converter tokenConverter, - Class tokenType) { - this(", ", tokenConverter, tokenType); - } - - /** - * Creates converter with given {@code tokenConverter} for convert tokens - * and expected {@code tokenType}. - *

- * If {@code tokenConverter} is null then no conversation is done and - * {@link String} is used as token type in resulting model collection. - * - * @param tokenConverter - * converter for token - * @param tokenType - * expected token model type - * @param delimiter - * delimiter in presentation string - */ - public StringToCollectionConverter(String delimiter, - Converter tokenConverter, Class tokenClass) { - this(delimiter, tokenConverter, tokenClass, - new DefaultCollectionFactory()); - } - - /** - * Creates converter with given {@code tokenConverter} for convert tokens - * and expected {@code tokenType}. - *

- * If {@code tokenConverter} is null then no conversation is done and - * {@link String} is used as token type in resulting model collection. - * - * @param tokenConverter - * converter for token - * @param tokenType - * expected token model type - * @param delimiter - * delimiter in presentation string - * @param factory - * factory to create resulting collection - */ - public StringToCollectionConverter(String delimiter, - Converter tokenConverter, Class tokenClass, - CollectionFactory factory) { - if (delimiter == null || delimiter.isEmpty()) { - throw new IllegalArgumentException( - "Delimiter should be non-empty string"); - } - this.delimiter = delimiter; - this.tokenConverter = tokenConverter; - tokenType = tokenClass; - this.factory = factory; - } - - @Override - public Class getModelType() { - return Collection.class; - } - - @Override - public Class getPresentationType() { - return String.class; - } - - @Override - public Collection convertToModel(String value, - Class targetType, Locale locale) - throws Converter.ConversionException { - if (value == null) { - return null; - } - - int index = value.indexOf(delimiter); - int previous = 0; - Collection result = factory.createCollection(targetType); - Converter converter = tokenConverter; - while (index != -1) { - collectToken(value.substring(previous, index), result, converter, - locale); - previous = index + delimiter.length(); - index = value.indexOf(delimiter, previous); - } - collectToken(value.substring(previous), result, converter, locale); - return result; - } - - @Override - public String convertToPresentation(Collection value, - Class targetType, Locale locale) - throws Converter.ConversionException { - if (value == null) { - return null; - } - StringBuilder builder = new StringBuilder(); - Converter converter = tokenConverter; - for (Iterator iterator = value.iterator(); iterator.hasNext();) { - if (converter == null) { - builder.append(iterator.next()); - } else { - builder.append(converter.convertToPresentation(iterator.next(), - targetType, locale)); - } - builder.append(delimiter); - } - if (builder.length() > 0) { - return builder.substring(0, builder.length() - delimiter.length()); - } else { - return builder.toString(); - } - } - - private void collectToken(String token, Collection collection, - Converter converter, Locale locale) { - if (converter == null) { - collection.add(token); - } else { - collection.add(converter.convertToModel(token, tokenType, locale)); - } - } - - /** - * Default collection factory implementation. - * - * @author Vaadin Ltd - */ - public static class DefaultCollectionFactory implements CollectionFactory { - - @Override - public Collection createCollection(Class type) { - if (type.isAssignableFrom(ArrayList.class)) { - return new ArrayList(); - } else if (type.isAssignableFrom(HashSet.class)) { - return new HashSet(); - } else if (!type.isInterface() - && !Modifier.isAbstract(type.getModifiers())) { - try { - return type.newInstance(); - } catch (InstantiationException ignore) { - } catch (IllegalAccessException ignore) { - } - } - return new ArrayList(); - } - - } - - /** - * Collection factory. Defines a strategy to create collection by collection - * class. - * - * @author Vaadin Ltd - */ - public interface CollectionFactory extends Serializable { - - /** - * Create collection by its {@code type}. - * - * @param type - * collection type - * @return instantiated collection with given {@code type} - */ - Collection createCollection(Class type); - } -} 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 deleted file mode 100644 index 8f0db24caf..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright 2000-2014 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.text.DateFormat; -import java.text.ParsePosition; -import java.util.Date; -import java.util.Locale; - -/** - * A converter that converts from {@link Date} to {@link String} and back. Uses - * the given locale and {@link DateFormat} for formatting and parsing. - *

- * Leading and trailing white spaces are ignored when converting from a String. - *

- *

- * Override and overwrite {@link #getFormat(Locale)} to use a different format. - *

- * - * @author Vaadin Ltd - * @since 7.0 - */ -public class StringToDateConverter implements Converter { - - /** - * Returns the format used by - * {@link #convertToPresentation(Date, Class,Locale)} and - * {@link #convertToModel(String, Class, Locale)}. - * - * @param locale - * The locale to use - * @return A DateFormat instance - */ - protected DateFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - - DateFormat f = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, - DateFormat.MEDIUM, locale); - f.setLenient(false); - return f; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.lang.Class, java.util.Locale) - */ - @Override - public Date convertToModel(String value, Class targetType, - Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { - if (targetType != getModelType()) { - throw new ConversionException("Converter only supports " - + getModelType().getName() + " (targetType was " - + targetType.getName() + ")"); - } - - if (value == null) { - return null; - } - - // Remove leading and trailing white space - value = value.trim(); - - ParsePosition parsePosition = new ParsePosition(0); - Date parsedValue = getFormat(locale).parse(value, parsePosition); - if (parsePosition.getIndex() != value.length()) { - throw new ConversionException("Could not convert '" + value - + "' to " + getModelType().getName()); - } - - return parsedValue; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang - * .Object, java.lang.Class, java.util.Locale) - */ - @Override - public String convertToPresentation(Date value, - Class targetType, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { - if (value == null) { - return null; - } - - return getFormat(locale).format(value); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class getModelType() { - return Date.class; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getPresentationType() - */ - @Override - public Class getPresentationType() { - return String.class; - } - -} 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 deleted file mode 100644 index f514eac648..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2000-2014 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.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Double} and back. - * Uses the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - *

- * Leading and trailing white spaces are ignored when converting from a String. - *

- *

- * Override and overwrite {@link #getFormat(Locale)} to use a different format. - *

- * - * @author Vaadin Ltd - * @since 7.0 - */ -public class StringToDoubleConverter extends - AbstractStringToNumberConverter { - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.util.Locale) - */ - @Override - public Double convertToModel(String value, - Class targetType, Locale locale) - throws ConversionException { - Number n = convertToNumber(value, targetType, locale); - return n == null ? null : n.doubleValue(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class getModelType() { - return Double.class; - } - -} diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToEnumConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToEnumConverter.java deleted file mode 100644 index 7b2f43f972..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToEnumConverter.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright 2000-2014 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.util.EnumSet; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to an {@link Enum} and back. - *

- * Designed to provide nice human readable strings for {@link Enum} classes - * conforming to one of these patterns: - *

    - *
  • The constants are named SOME_UPPERCASE_WORDS and there's no toString - * implementation.
  • - *
  • toString() always returns the same human readable string that is not the - * same as its name() value. Each constant in the enum type returns a distinct - * toString() value.
  • - *
- * Will not necessarily work correctly for other cases. - *

- * - * @author Vaadin Ltd - * @since 7.4 - */ -public class StringToEnumConverter implements Converter { - - @Override - public Enum convertToModel(String value, Class targetType, - Locale locale) throws ConversionException { - if (value == null || value.trim().equals("")) { - return null; - } - - return stringToEnum(value, targetType, locale); - } - - /** - * Converts the given string to the given enum type using the given locale - *

- * Compatible with {@link #enumToString(Enum, Locale)} - * - * @param value - * The string value to convert - * @param enumType - * The type of enum to create - * @param locale - * The locale to use for conversion. If null, the JVM default - * locale will be used - * @return The enum which matches the given string - * @throws ConversionException - * if the conversion fails - */ - public static > T stringToEnum(String value, - Class enumType, Locale locale) throws ConversionException { - if (locale == null) { - locale = Locale.getDefault(); - } - - if (!enumType.isEnum()) { - throw new ConversionException(enumType.getName() - + " is not an enum type"); - } - - // First test for the human-readable value since that's the more likely - // input - String upperCaseValue = value.toUpperCase(locale); - T match = null; - for (T e : EnumSet.allOf(enumType)) { - String upperCase = enumToString(e, locale).toUpperCase(locale); - if (upperCase.equals(upperCaseValue)) { - if (match != null) { - throw new ConversionException("Both " + match.name() - + " and " + e.name() - + " are matching the input string " + value); - } - match = e; - } - } - - if (match != null) { - return match; - } - - // Then fall back to using a strict match based on name() - try { - return Enum.valueOf(enumType, upperCaseValue); - } catch (Exception ee) { - throw new ConversionException(ee); - } - } - - /** - * Converts the given enum to a human readable string using the given locale - *

- * Compatible with {@link #stringToEnum(String, Class, Locale)} - * - * @param value - * The enum value to convert - * @param locale - * The locale to use for conversion. If null, the JVM default - * locale will be used - * @return A human readable string based on the enum - * @throws ConversionException - * if the conversion fails - */ - public static String enumToString(Enum value, Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - - String enumString = value.toString(); - if (enumString.equals(value.name())) { - // FOO -> Foo - // FOO_BAR -> Foo bar - // _FOO -> _foo - String result = enumString.substring(0, 1).toUpperCase(locale); - result += enumString.substring(1).toLowerCase(locale) - .replace('_', ' '); - return result; - } else { - return enumString; - } - } - - @Override - public String convertToPresentation(Enum value, - Class targetType, Locale locale) - throws ConversionException { - if (value == null) { - return null; - } - - return enumToString(value, locale); - } - - @Override - public Class getModelType() { - return Enum.class; - } - - @Override - public Class getPresentationType() { - return String.class; - } - -} 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 deleted file mode 100644 index f4d7f97853..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2000-2014 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.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Float} and back. Uses - * the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - *

- * Leading and trailing white spaces are ignored when converting from a String. - *

- *

- * Override and overwrite {@link #getFormat(Locale)} to use a different format. - *

- * - * @author Vaadin Ltd - * @since 7.0 - */ -public class StringToFloatConverter extends - AbstractStringToNumberConverter { - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.util.Locale) - */ - @Override - public Float convertToModel(String value, - Class targetType, Locale locale) - throws ConversionException { - Number n = convertToNumber(value, targetType, locale); - return n == null ? null : n.floatValue(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class getModelType() { - return Float.class; - } - -} 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 deleted file mode 100644 index 59a46babe1..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2000-2014 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.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Integer} and back. - * Uses the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - *

- * Override and overwrite {@link #getFormat(Locale)} to use a different format. - *

- * - * @author Vaadin Ltd - * @since 7.0 - */ -public class StringToIntegerConverter extends - AbstractStringToNumberConverter { - - /** - * Returns the format used by - * {@link #convertToPresentation(Integer, Class, Locale)} and - * {@link #convertToModel(String, Class, Locale)} - * - * @param locale - * The locale to use - * @return A NumberFormat instance - */ - @Override - protected NumberFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - return NumberFormat.getIntegerInstance(locale); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.lang.Class, java.util.Locale) - */ - @Override - public Integer convertToModel(String value, - Class targetType, Locale locale) - throws ConversionException { - Number n = convertToNumber(value, targetType, locale); - - if (n == null) { - return null; - } - - int intValue = n.intValue(); - if (intValue == n.longValue()) { - // If the value of n is outside the range of long, the return value - // of longValue() is either Long.MIN_VALUE or Long.MAX_VALUE. The - // above comparison promotes int to long and thus does not need to - // consider wrap-around. - return intValue; - } - - throw new ConversionException("Could not convert '" + value + "' to " - + Integer.class.getName() + ": value out of range"); - - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class getModelType() { - return Integer.class; - } - -} 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 deleted file mode 100644 index cd7cf49042..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2000-2014 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.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Long} and back. Uses - * the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - *

- * Override and overwrite {@link #getFormat(Locale)} to use a different format. - *

- * - * @author Vaadin Ltd - * @since 7.2 - */ -public class StringToLongConverter extends - AbstractStringToNumberConverter { - - /** - * Returns the format used by - * {@link #convertToPresentation(Long, Class, Locale)} and - * {@link #convertToModel(String, Class, Locale)} - * - * @param locale - * The locale to use - * @return A NumberFormat instance - */ - @Override - protected NumberFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - return NumberFormat.getIntegerInstance(locale); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.lang.Class, java.util.Locale) - */ - @Override - public Long convertToModel(String value, Class targetType, - Locale locale) throws ConversionException { - Number n = convertToNumber(value, targetType, locale); - return n == null ? null : n.longValue(); - - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class getModelType() { - return Long.class; - } - -} diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToShortConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToShortConverter.java deleted file mode 100644 index 554b958ee0..0000000000 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToShortConverter.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2000-2014 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.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Short} and back. Uses - * the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - *

- * Override and overwrite {@link #getFormat(Locale)} to use a different format. - *

- * - * @author Vaadin Ltd - * @since 7.4 - */ -public class StringToShortConverter extends - AbstractStringToNumberConverter { - - /** - * Returns the format used by - * {@link #convertToPresentation(Short, Class, Locale)} and - * {@link #convertToModel(String, Class, Locale)} - * - * @param locale - * The locale to use - * @return A NumberFormat instance - */ - @Override - protected NumberFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - return NumberFormat.getIntegerInstance(locale); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.lang.Class, java.util.Locale) - */ - @Override - public Short convertToModel(String value, - Class targetType, Locale locale) - throws ConversionException { - Number n = convertToNumber(value, targetType, locale); - - if (n == null) { - return null; - } - - short shortValue = n.shortValue(); - if (shortValue == n.longValue()) { - return shortValue; - } - - throw new ConversionException("Could not convert '" + value + "' to " - + Short.class.getName() + ": value out of range"); - - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class getModelType() { - return Short.class; - } - -} diff --git a/server/src/main/java/com/vaadin/data/util/sqlcontainer/ColumnProperty.java b/server/src/main/java/com/vaadin/data/util/sqlcontainer/ColumnProperty.java index e89b8c02a8..0f778ba2ab 100644 --- a/server/src/main/java/com/vaadin/data/util/sqlcontainer/ColumnProperty.java +++ b/server/src/main/java/com/vaadin/data/util/sqlcontainer/ColumnProperty.java @@ -22,7 +22,7 @@ import java.util.logging.Logger; import com.vaadin.data.Property; import com.vaadin.data.util.LegacyPropertyHelper; -import com.vaadin.data.util.converter.Converter.ConversionException; +import com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException; /** * ColumnProperty represents the value of one column in a RowItem. In addition diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyAbstractStringToNumberConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyAbstractStringToNumberConverter.java new file mode 100644 index 0000000000..4b14f66df9 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyAbstractStringToNumberConverter.java @@ -0,0 +1,123 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.text.NumberFormat; +import java.text.ParsePosition; +import java.util.Locale; + +/** + * A converter that converts from the number type T to {@link String} and back. + * Uses the given locale and {@link NumberFormat} for formatting and parsing. + * Automatically trims the input string, removing any leading and trailing white + * space. + *

+ * Override and overwrite {@link #getFormat(Locale)} to use a different format. + *

+ * + * @author Vaadin Ltd + * @since 7.1 + */ +public abstract class LegacyAbstractStringToNumberConverter implements + LegacyConverter { + + /** + * Returns the format used by {@link #convertToPresentation(Object, Locale)} + * and {@link #convertToModel(Object, Locale)}. + * + * @param locale + * The locale to use + * @return A NumberFormat instance + * @since 7.1 + */ + protected NumberFormat getFormat(Locale locale) { + if (locale == null) { + locale = Locale.getDefault(); + } + + return NumberFormat.getNumberInstance(locale); + } + + /** + * Convert the value to a Number using the given locale and + * {@link #getFormat(Locale)}. + * + * @param value + * The value to convert + * @param locale + * The locale to use for conversion + * @return The converted value + * @throws ConversionException + * If there was a problem converting the value + * @since 7.1 + */ + protected Number convertToNumber(String value, + Class targetType, Locale locale) + throws ConversionException { + if (value == null) { + return null; + } + + // Remove leading and trailing white space + value = value.trim(); + + // Parse and detect errors. If the full string was not used, it is + // an error. + ParsePosition parsePosition = new ParsePosition(0); + Number parsedValue = getFormat(locale).parse(value, parsePosition); + if (parsePosition.getIndex() != value.length()) { + throw new ConversionException("Could not convert '" + value + + "' to " + getModelType().getName()); + } + + if (parsedValue == null) { + // Convert "" to null + return null; + } + + return parsedValue; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang + * .Object, java.util.Locale) + */ + @Override + public String convertToPresentation(T value, + Class targetType, Locale locale) + throws ConversionException { + if (value == null) { + return null; + } + + return getFormat(locale).format(value); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getPresentationType() + */ + @Override + public Class getPresentationType() { + return String.class; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverter.java new file mode 100644 index 0000000000..1fdd0755b9 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverter.java @@ -0,0 +1,176 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.io.Serializable; +import java.util.Locale; + +/** + * Interface that implements conversion between a model and a presentation type. + *

+ * Typically {@link #convertToPresentation(Object, Class, Locale)} and + * {@link #convertToModel(Object, Class, Locale)} should be symmetric so that + * chaining these together returns the original result for all input but this is + * not a requirement. + *

+ *

+ * Converters must not have any side effects (never update UI from inside a + * converter). + *

+ *

+ * All Converters must be stateless and thread safe. + *

+ *

+ * If conversion of a value fails, a {@link ConversionException} is thrown. + *

+ * + * @param + * The presentation type. Must be compatible with what + * {@link #getPresentationType()} returns. + * @param + * The model type. Must be compatible with what + * {@link #getModelType()} returns. + * @author Vaadin Ltd. + * @since 7.0 + */ +public interface LegacyConverter extends Serializable { + + /** + * Converts the given value from target type to source type. + *

+ * A converter can optionally use locale to do the conversion. + *

+ * A converter should in most cases be symmetric so chaining + * {@link #convertToPresentation(Object, Class, Locale)} and + * {@link #convertToModel(Object, Class, Locale)} should return the original + * value. + * + * @param value + * The value to convert, compatible with the target type. Can be + * null + * @param targetType + * The requested type of the return value + * @param locale + * The locale to use for conversion. Can be null. + * @return The converted value compatible with the source type + * @throws ConversionException + * If the value could not be converted + */ + public MODEL convertToModel(PRESENTATION value, + Class targetType, Locale locale) + throws ConversionException; + + /** + * Converts the given value from source type to target type. + *

+ * A converter can optionally use locale to do the conversion. + *

+ * A converter should in most cases be symmetric so chaining + * {@link #convertToPresentation(Object, Class, Locale)} and + * {@link #convertToModel(Object, Class, Locale)} should return the original + * value. + * + * @param value + * The value to convert, compatible with the target type. Can be + * null + * @param targetType + * The requested type of the return value + * @param locale + * The locale to use for conversion. Can be null. + * @return The converted value compatible with the source type + * @throws ConversionException + * If the value could not be converted + */ + public PRESENTATION convertToPresentation(MODEL value, + Class targetType, Locale locale) + throws ConversionException; + + /** + * The source type of the converter. + * + * Values of this type can be passed to + * {@link #convertToPresentation(Object, Class, Locale)}. + * + * @return The source type + */ + public Class getModelType(); + + /** + * The target type of the converter. + * + * Values of this type can be passed to + * {@link #convertToModel(Object, Class, Locale)}. + * + * @return The target type + */ + public Class getPresentationType(); + + /** + * An exception that signals that the value passed to + * {@link LegacyConverter#convertToPresentation(Object, Class, Locale)} or + * {@link LegacyConverter#convertToModel(Object, Class, Locale)} could not be + * converted. + * + * @author Vaadin Ltd + * @since 7.0 + */ + public static class ConversionException extends RuntimeException { + + /** + * Constructs a new ConversionException without a detail + * message. + */ + public ConversionException() { + } + + /** + * Constructs a new ConversionException with the specified + * detail message. + * + * @param msg + * the detail message + */ + public ConversionException(String msg) { + super(msg); + } + + /** + * Constructs a new {@code ConversionException} with the specified + * cause. + * + * @param cause + * The cause of the the exception + */ + public ConversionException(Throwable cause) { + super(cause); + } + + /** + * Constructs a new ConversionException with the specified + * detail message and cause. + * + * @param message + * the detail message + * @param cause + * The cause of the the exception + */ + public ConversionException(String message, Throwable cause) { + super(message, cause); + } + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterFactory.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterFactory.java new file mode 100644 index 0000000000..8c5cbacf04 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterFactory.java @@ -0,0 +1,33 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.io.Serializable; + +/** + * Factory interface for providing Converters based on a presentation type and a + * model type. + * + * @author Vaadin Ltd. + * @since 7.0 + * + */ +public interface LegacyConverterFactory extends Serializable { + public LegacyConverter createConverter( + Class presentationType, Class modelType); + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterUtil.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterUtil.java new file mode 100644 index 0000000000..42d268f781 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterUtil.java @@ -0,0 +1,261 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.io.Serializable; +import java.util.Locale; + +import com.vaadin.server.VaadinSession; + +public class LegacyConverterUtil implements Serializable { + + /** + * Finds a converter that can convert from the given presentation type to + * the given model type and back. Uses the given application to find a + * {@link LegacyConverterFactory} or, if application is null, uses the + * {@link VaadinSession#getCurrent()}. + * + * @param + * the presentation type + * @param + * the model type + * @param presentationType + * the presentation type + * @param modelType + * the model type + * @param session + * the session to use to find a ConverterFactory or null to use + * the current session + * @return a Converter capable of converting between the given types or null + * if no converter was found + */ + public static LegacyConverter getConverter( + Class presentationType, + Class modelType, VaadinSession session) { + LegacyConverter converter = null; + if (session == null) { + session = VaadinSession.getCurrent(); + } + + if (session != null) { + LegacyConverterFactory factory = session.getConverterFactory(); + converter = factory.createConverter(presentationType, modelType); + } + return converter; + + } + + /** + * Convert the given value from the data source type to the UI type. + * + * @param modelValue + * the model value to convert + * @param presentationType + * the type of the presentation value + * @param converter + * the converter to use + * @param locale + * the locale to use for conversion + * @param + * the presentation type + * @param + * the model type + * + * @return the converted value, compatible with the presentation type, or + * the original value if its type is compatible and no converter is + * set. + * @throws LegacyConverter.ConversionException + * if there was a problem converting the value + */ + @SuppressWarnings("unchecked") + public static PRESENTATIONTYPE convertFromModel( + MODELTYPE modelValue, + Class presentationType, + LegacyConverter converter, Locale locale) + throws LegacyConverter.ConversionException { + if (converter != null) { + /* + * If there is a converter, always use it. It must convert or throw + * an exception. + */ + PRESENTATIONTYPE presentation = converter.convertToPresentation( + modelValue, presentationType, locale); + if (presentation != null + && !presentationType.isInstance(presentation)) { + throw new LegacyConverter.ConversionException( + "Converter returned an object of type " + + presentation.getClass().getName() + + " when expecting " + + presentationType.getName()); + } + + return presentation; + } + if (modelValue == null) { + // Null should always be passed through the converter but if there + // is no converter we can safely return null + return null; + } + + if (presentationType.isAssignableFrom(modelValue.getClass())) { + return (PRESENTATIONTYPE) modelValue; + } else { + throw new LegacyConverter.ConversionException( + "Unable to convert value of type " + + modelValue.getClass().getName() + + " to presentation type " + + presentationType + + ". No converter is set and the types are not compatible."); + } + } + + /** + * Convert the given value from the presentation (UI) type to model (data + * source) type. + * + * @param presentationValue + * the presentation value to convert + * @param modelType + * the type of the model + * @param converter + * the converter to use + * @param locale + * the locale to use for conversion + * @param + * the presentation type + * @param + * the model type + * + * @return the converted value, compatible with the model type, or the + * original value if its type is compatible and no converter is set. + * @throws LegacyConverter.ConversionException + * if there was a problem converting the value + */ + public static MODELTYPE convertToModel( + PRESENTATIONTYPE presentationValue, Class modelType, + LegacyConverter converter, Locale locale) + throws LegacyConverter.ConversionException { + if (converter != null) { + /* + * If there is a converter, always use it. It must convert or throw + * an exception. + */ + MODELTYPE model = converter.convertToModel(presentationValue, + modelType, locale); + if (model != null && !modelType.isInstance(model)) { + throw new LegacyConverter.ConversionException( + "Converter returned an object of type " + + model.getClass().getName() + + " when expecting " + modelType.getName()); + } + + return model; + } + + if (presentationValue == null) { + // Null should always be passed through the converter but if there + // is no converter we can safely return null + return null; + } + + if (modelType == null) { + // No model type, return original value + return (MODELTYPE) presentationValue; + } else if (modelType.isAssignableFrom(presentationValue.getClass())) { + // presentation type directly compatible with model type + return modelType.cast(presentationValue); + } else { + throw new LegacyConverter.ConversionException( + "Unable to convert value of type " + + presentationValue.getClass().getName() + + " to model type " + + modelType + + ". No converter is set and the types are not compatible."); + } + + } + + /** + * Checks if the given converter can handle conversion between the given + * presentation and model type. Does strict type checking and only returns + * true if the converter claims it can handle exactly the given types. + * + * @see #canConverterPossiblyHandle(LegacyConverter, Class, Class) + * + * @param converter + * The converter to check. If this is null the result is always + * false. + * @param presentationType + * The presentation type + * @param modelType + * The model type + * @return true if the converter supports conversion between the given + * presentation and model type, false otherwise + */ + public static boolean canConverterHandle(LegacyConverter converter, + Class presentationType, Class modelType) { + if (converter == null) { + return false; + } + + if (modelType != converter.getModelType()) { + return false; + } + if (presentationType != converter.getPresentationType()) { + return false; + } + + return true; + } + + /** + * Checks if it possible that the given converter can handle conversion + * between the given presentation and model type somehow. + * + * @param converter + * The converter to check. If this is null the result is always + * false. + * @param presentationType + * The presentation type + * @param modelType + * The model type + * @return true if the converter possibly support conversion between the + * given presentation and model type, false otherwise + */ + public static boolean canConverterPossiblyHandle(LegacyConverter converter, + Class presentationType, Class modelType) { + if (converter == null) { + return false; + } + Class converterModelType = converter.getModelType(); + + if (!modelType.isAssignableFrom(converterModelType) + && !converterModelType.isAssignableFrom(modelType)) { + // model types are not compatible in any way + return false; + } + + Class converterPresentationType = converter.getPresentationType(); + if (!presentationType.isAssignableFrom(converterPresentationType) + && !converterPresentationType + .isAssignableFrom(presentationType)) { + // presentation types are not compatible in any way + return false; + } + + return true; + } +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToLongConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToLongConverter.java new file mode 100644 index 0000000000..04d037b12f --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToLongConverter.java @@ -0,0 +1,89 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.util.Date; +import java.util.Locale; + +/** + * A converter that converts from {@link Long} to {@link Date} and back. + * + * @author Vaadin Ltd + * @since 7.0 + */ +public class LegacyDateToLongConverter implements LegacyConverter { + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, + * java.lang.Class, java.util.Locale) + */ + @Override + public Long convertToModel(Date value, Class targetType, + Locale locale) { + if (value == null) { + return null; + } + + return value.getTime(); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang + * .Object, java.lang.Class, java.util.Locale) + */ + @Override + public Date convertToPresentation(Long value, + Class targetType, Locale locale) { + if (targetType != getPresentationType()) { + throw new ConversionException("Converter only supports " + + getPresentationType().getName() + " (targetType was " + + targetType.getName() + ")"); + } + if (value == null) { + return null; + } + + return new Date(value); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getModelType() + */ + @Override + public Class getModelType() { + return Long.class; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getPresentationType() + */ + @Override + public Class getPresentationType() { + return Date.class; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToSqlDateConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToSqlDateConverter.java new file mode 100644 index 0000000000..1ea4eaca8c --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToSqlDateConverter.java @@ -0,0 +1,81 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.util.Date; +import java.util.Locale; + +/** + * Converter for handling conversion between {@link java.util.Date} and + * {@link java.sql.Date}. This is used when a PopupDateField or InlineDateField + * is connected to a java.sql.Date property, typically through a JPAContainer or + * SQLContainer. Note that information (time information) is lost when + * converting from {@link java.util.Date} to {@link java.sql.Date}. + * + * @since 7.1 + * @author Vaadin Ltd + */ +public class LegacyDateToSqlDateConverter implements LegacyConverter { + + @Override + public java.sql.Date convertToModel(Date value, + Class targetType, Locale locale) + throws ConversionException { + if (targetType != getModelType()) { + throw new ConversionException("Converter only supports " + + getModelType().getName() + " (targetType was " + + targetType.getName() + ")"); + } + + if (value == null) { + return null; + } + + return new java.sql.Date(value.getTime()); + } + + @Override + public Date convertToPresentation(java.sql.Date value, + Class targetType, Locale locale) + throws ConversionException { + if (targetType != getPresentationType()) { + throw new ConversionException("Converter only supports " + + getPresentationType().getName() + " (targetType was " + + targetType.getName() + ")"); + } + + if (value == null) { + return null; + } + + return new Date(value.getTime()); + } + + @Override + public Class getModelType() { + return java.sql.Date.class; + } + + @Override + public Class getPresentationType() { + return Date.class; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDefaultConverterFactory.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDefaultConverterFactory.java new file mode 100644 index 0000000000..c647981650 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDefaultConverterFactory.java @@ -0,0 +1,127 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.Date; +import java.util.logging.Logger; + +import com.vaadin.server.VaadinSession; + +/** + * Default implementation of {@link LegacyConverterFactory}. Provides converters for + * standard types like {@link String}, {@link Double} and {@link Date}.

+ *

+ * Custom converters can be provided by extending this class and using + * {@link VaadinSession#setConverterFactory(LegacyConverterFactory)}. + *

+ * + * @author Vaadin Ltd + * @since 7.0 + */ +public class LegacyDefaultConverterFactory implements LegacyConverterFactory { + + private final static Logger log = Logger + .getLogger(LegacyDefaultConverterFactory.class.getName()); + + @Override + public LegacyConverter createConverter( + Class presentationType, Class modelType) { + LegacyConverter converter = findConverter( + presentationType, modelType); + if (converter != null) { + log.finest(getClass().getName() + " created a " + + converter.getClass()); + return converter; + } + + // Try to find a reverse converter + LegacyConverter reverseConverter = findConverter( + modelType, presentationType); + if (reverseConverter != null) { + log.finest(getClass().getName() + " created a reverse " + + reverseConverter.getClass()); + return new LegacyReverseConverter(reverseConverter); + } + + log.finest(getClass().getName() + " could not find a converter for " + + presentationType.getName() + " to " + modelType.getName() + + " conversion"); + return null; + + } + + protected LegacyConverter findConverter( + Class presentationType, Class modelType) { + if (presentationType == String.class) { + // TextField converters and more + LegacyConverter converter = (LegacyConverter) createStringConverter(modelType); + if (converter != null) { + return converter; + } + } else if (presentationType == Date.class) { + // DateField converters and more + LegacyConverter converter = (LegacyConverter) createDateConverter(modelType); + if (converter != null) { + return converter; + } + } + + return null; + + } + + protected LegacyConverter createDateConverter(Class sourceType) { + if (Long.class.isAssignableFrom(sourceType)) { + return new LegacyDateToLongConverter(); + } else if (java.sql.Date.class.isAssignableFrom(sourceType)) { + return new LegacyDateToSqlDateConverter(); + } else { + return null; + } + } + + protected LegacyConverter createStringConverter(Class sourceType) { + if (Double.class.isAssignableFrom(sourceType)) { + return new LegacyStringToDoubleConverter(); + } else if (Float.class.isAssignableFrom(sourceType)) { + return new LegacyStringToFloatConverter(); + } else if (Integer.class.isAssignableFrom(sourceType)) { + return new LegacyStringToIntegerConverter(); + } else if (Long.class.isAssignableFrom(sourceType)) { + return new LegacyStringToLongConverter(); + } else if (BigDecimal.class.isAssignableFrom(sourceType)) { + return new LegacyStringToBigDecimalConverter(); + } else if (Boolean.class.isAssignableFrom(sourceType)) { + return new LegacyStringToBooleanConverter(); + } else if (Date.class.isAssignableFrom(sourceType)) { + return new LegacyStringToDateConverter(); + } else if (Enum.class.isAssignableFrom(sourceType)) { + return new LegacyStringToEnumConverter(); + } else if (BigInteger.class.isAssignableFrom(sourceType)) { + return new LegacyStringToBigIntegerConverter(); + } else if (Short.class.isAssignableFrom(sourceType)) { + return new LegacyStringToShortConverter(); + } else if (Byte.class.isAssignableFrom(sourceType)) { + return new LegacyStringToByteConverter(); + } else { + return null; + } + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyReverseConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyReverseConverter.java new file mode 100644 index 0000000000..6841e99b6e --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyReverseConverter.java @@ -0,0 +1,96 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.util.Locale; + +/** + * A converter that wraps another {@link LegacyConverter} and reverses source and + * target types. + * + * @param + * The source type + * @param + * The target type + * + * @author Vaadin Ltd + * @since 7.0 + */ +public class LegacyReverseConverter implements + LegacyConverter { + + private LegacyConverter realConverter; + + /** + * Creates a converter from source to target based on a converter that + * converts from target to source. + * + * @param converter + * The converter to use in a reverse fashion + */ + public LegacyReverseConverter(LegacyConverter converter) { + this.realConverter = converter; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#convertToModel(java + * .lang.Object, java.util.Locale) + */ + @Override + public MODEL convertToModel(PRESENTATION value, + Class targetType, Locale locale) + throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException { + return realConverter.convertToPresentation(value, targetType, locale); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang + * .Object, java.util.Locale) + */ + @Override + public PRESENTATION convertToPresentation(MODEL value, + Class targetType, Locale locale) + throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException { + return realConverter.convertToModel(value, targetType, locale); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getSourceType() + */ + @Override + public Class getModelType() { + return realConverter.getPresentationType(); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getTargetType() + */ + @Override + public Class getPresentationType() { + return realConverter.getModelType(); + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigDecimalConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigDecimalConverter.java new file mode 100644 index 0000000000..4b6c2151a0 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigDecimalConverter.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.Locale; + +/** + * A converter that converts from {@link String} to {@link BigDecimal} and back. + * Uses the given locale and a {@link NumberFormat} instance for formatting and + * parsing. + *

+ * Leading and trailing white spaces are ignored when converting from a String. + *

+ *

+ * Override and overwrite {@link #getFormat(Locale)} to use a different format. + *

+ * + * @author Vaadin Ltd + * @since 7.2 + */ +public class LegacyStringToBigDecimalConverter extends + LegacyAbstractStringToNumberConverter { + @Override + protected NumberFormat getFormat(Locale locale) { + NumberFormat numberFormat = super.getFormat(locale); + if (numberFormat instanceof DecimalFormat) { + ((DecimalFormat) numberFormat).setParseBigDecimal(true); + } + + return numberFormat; + } + + @Override + public BigDecimal convertToModel(String value, + Class targetType, Locale locale) + throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException { + return (BigDecimal) convertToNumber(value, BigDecimal.class, locale); + } + + @Override + public Class getModelType() { + return BigDecimal.class; + } +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigIntegerConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigIntegerConverter.java new file mode 100644 index 0000000000..f2d3754dd2 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigIntegerConverter.java @@ -0,0 +1,67 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.Locale; + +/** + * A converter that converts from {@link String} to {@link BigInteger} and back. + * Uses the given locale and a {@link NumberFormat} instance for formatting and + * parsing. + *

+ * Leading and trailing white spaces are ignored when converting from a String. + *

+ *

+ * Override and overwrite {@link #getFormat(Locale)} to use a different format. + *

+ * + * @author Vaadin Ltd + * @since 7.4 + */ +public class LegacyStringToBigIntegerConverter extends + LegacyAbstractStringToNumberConverter { + + @Override + protected NumberFormat getFormat(Locale locale) { + NumberFormat numberFormat = super.getFormat(locale); + if (numberFormat instanceof DecimalFormat) { + ((DecimalFormat) numberFormat).setParseBigDecimal(true); + } + + return numberFormat; + } + + @Override + public BigInteger convertToModel(String value, + Class targetType, Locale locale) + throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException { + + BigDecimal bigDecimalValue = (BigDecimal) convertToNumber(value, + BigDecimal.class, locale); + + return (bigDecimalValue != null) ? bigDecimalValue.toBigInteger() + : null; + } + + @Override + public Class getModelType() { + return BigInteger.class; + } +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBooleanConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBooleanConverter.java new file mode 100644 index 0000000000..7d36a9d248 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBooleanConverter.java @@ -0,0 +1,167 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.util.Locale; + +/** + * A converter that converts from {@link String} to {@link Boolean} and back. The String representation is given by + * {@link Boolean#toString()} or provided in constructor {@link #StringToBooleanConverter(String, String)}. + *

Leading and trailing white spaces are ignored when converting from a String.

+ *

For language-dependent representation, subclasses should overwrite {@link #getFalseString(Locale)} and {@link #getTrueString(Locale)}

+ * + * @author Vaadin Ltd + * @since 7.0 + */ +public class LegacyStringToBooleanConverter implements LegacyConverter { + + private final String trueString; + + private final String falseString; + + /** + * Creates converter with default string representations - "true" and "false" + * + */ + public LegacyStringToBooleanConverter() { + this(Boolean.TRUE.toString(), Boolean.FALSE.toString()); + } + + /** + * Creates converter with custom string representation. + * + * @since 7.5.4 + * @param falseString string representation for false + * @param trueString string representation for true + */ + public LegacyStringToBooleanConverter(String trueString, String falseString) { + this.trueString = trueString; + this.falseString = falseString; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, + * java.lang.Class, java.util.Locale) + */ + @Override + public Boolean convertToModel(String value, + Class targetType, Locale locale) + throws ConversionException { + if (value == null || value.isEmpty()) { + return null; + } + + // Remove leading and trailing white space + value = value.trim(); + + if (getTrueString().equals(value)) { + return true; + } else if (getFalseString().equals(value)) { + return false; + } else { + throw new ConversionException("Cannot convert " + value + " to " + + getModelType().getName()); + } + } + + /** + * Gets the string representation for true. Default is "true", if not set in constructor. + * + * @return the string representation for true + */ + protected String getTrueString() { + return trueString; + } + + /** + * Gets the string representation for false. Default is "false", if not set in constructor. + * + * @return the string representation for false + */ + protected String getFalseString() { + return falseString; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang + * .Object, java.lang.Class, java.util.Locale) + */ + @Override + public String convertToPresentation(Boolean value, + Class targetType, Locale locale) + throws ConversionException { + if (value == null) { + return null; + } + if (value) { + return getTrueString(locale); + } else { + return getFalseString(locale); + } + } + + /** + * Gets the locale-depended string representation for false. + * Default is locale-independent value provided by {@link #getFalseString()} + * + * @since 7.5.4 + * @param locale to be used + * @return the string representation for false + */ + protected String getFalseString(Locale locale) { + return getFalseString(); + } + + /** + * Gets the locale-depended string representation for true. + * Default is locale-independent value provided by {@link #getTrueString()} + * + * @since 7.5.4 + * @param locale to be used + * @return the string representation for true + */ + protected String getTrueString(Locale locale) { + return getTrueString(); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getModelType() + */ + @Override + public Class getModelType() { + return Boolean.class; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getPresentationType() + */ + @Override + public Class getPresentationType() { + return String.class; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToByteConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToByteConverter.java new file mode 100644 index 0000000000..0531322663 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToByteConverter.java @@ -0,0 +1,89 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.text.NumberFormat; +import java.util.Locale; + +/** + * A converter that converts from {@link String} to {@link Byte} and back. Uses + * the given locale and a {@link NumberFormat} instance for formatting and + * parsing. + *

+ * Override and overwrite {@link #getFormat(Locale)} to use a different format. + *

+ * + * @author Vaadin Ltd + * @since 7.4 + */ +public class LegacyStringToByteConverter extends + LegacyAbstractStringToNumberConverter { + + /** + * Returns the format used by + * {@link #convertToPresentation(Byte, Class, Locale)} and + * {@link #convertToModel(String, Class, Locale)} + * + * @param locale + * The locale to use + * @return A NumberFormat instance + */ + @Override + protected NumberFormat getFormat(Locale locale) { + if (locale == null) { + locale = Locale.getDefault(); + } + return NumberFormat.getIntegerInstance(locale); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, + * java.lang.Class, java.util.Locale) + */ + @Override + public Byte convertToModel(String value, Class targetType, + Locale locale) throws ConversionException { + Number n = convertToNumber(value, targetType, locale); + + if (n == null) { + return null; + } + + byte byteValue = n.byteValue(); + if (byteValue == n.longValue()) { + return byteValue; + } + + throw new ConversionException("Could not convert '" + value + "' to " + + Byte.class.getName() + ": value out of range"); + + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getModelType() + */ + @Override + public Class getModelType() { + return Byte.class; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToCollectionConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToCollectionConverter.java new file mode 100644 index 0000000000..59321d5fb3 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToCollectionConverter.java @@ -0,0 +1,243 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.io.Serializable; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Locale; + +/** + * A converter that converts from {@link String} to {@link Collection} of tokens + * and back. + *

+ * Allows to break a string into tokens using delimiter. Each token can be + * converted to its own model using provided converter. + *

+ * Default constructor uses ", " as delimiter string and + * {@link String} for token types. Other constructors allow to configure + * delimiter and token types. + * + * @since 7.5.0 + * + * @author Vaadin Ltd + */ +public class LegacyStringToCollectionConverter implements + LegacyConverter { + + private final String delimiter; + private final LegacyConverter tokenConverter; + private final Class tokenType; + private final CollectionFactory factory; + + /** + * Creates converter with ", " as delimiter and {@link String} + * as token model type in collection. + */ + public LegacyStringToCollectionConverter() { + this(", ", null, String.class); + } + + /** + * Creates converter with given {@code delimiter} and {@link String} as + * token model type in collection. + * + * @param delimiter + * custom delimiter + */ + public LegacyStringToCollectionConverter(String delimiter) { + this(delimiter, null, String.class); + } + + /** + * Creates converter with given {@code tokenConverter} for convert tokens + * and expected {@code tokenType}. + *

+ * If {@code tokenConverter} is null then no conversation is done and + * {@link String} is used as token type in resulting model collection. + * + * @param tokenConverter + * converter for token + * @param tokenType + * expected token model type + */ + public LegacyStringToCollectionConverter(LegacyConverter tokenConverter, + Class tokenType) { + this(", ", tokenConverter, tokenType); + } + + /** + * Creates converter with given {@code tokenConverter} for convert tokens + * and expected {@code tokenType}. + *

+ * If {@code tokenConverter} is null then no conversation is done and + * {@link String} is used as token type in resulting model collection. + * + * @param tokenConverter + * converter for token + * @param tokenType + * expected token model type + * @param delimiter + * delimiter in presentation string + */ + public LegacyStringToCollectionConverter(String delimiter, + LegacyConverter tokenConverter, Class tokenClass) { + this(delimiter, tokenConverter, tokenClass, + new DefaultCollectionFactory()); + } + + /** + * Creates converter with given {@code tokenConverter} for convert tokens + * and expected {@code tokenType}. + *

+ * If {@code tokenConverter} is null then no conversation is done and + * {@link String} is used as token type in resulting model collection. + * + * @param tokenConverter + * converter for token + * @param tokenType + * expected token model type + * @param delimiter + * delimiter in presentation string + * @param factory + * factory to create resulting collection + */ + public LegacyStringToCollectionConverter(String delimiter, + LegacyConverter tokenConverter, Class tokenClass, + CollectionFactory factory) { + if (delimiter == null || delimiter.isEmpty()) { + throw new IllegalArgumentException( + "Delimiter should be non-empty string"); + } + this.delimiter = delimiter; + this.tokenConverter = tokenConverter; + tokenType = tokenClass; + this.factory = factory; + } + + @Override + public Class getModelType() { + return Collection.class; + } + + @Override + public Class getPresentationType() { + return String.class; + } + + @Override + public Collection convertToModel(String value, + Class targetType, Locale locale) + throws LegacyConverter.ConversionException { + if (value == null) { + return null; + } + + int index = value.indexOf(delimiter); + int previous = 0; + Collection result = factory.createCollection(targetType); + LegacyConverter converter = tokenConverter; + while (index != -1) { + collectToken(value.substring(previous, index), result, converter, + locale); + previous = index + delimiter.length(); + index = value.indexOf(delimiter, previous); + } + collectToken(value.substring(previous), result, converter, locale); + return result; + } + + @Override + public String convertToPresentation(Collection value, + Class targetType, Locale locale) + throws LegacyConverter.ConversionException { + if (value == null) { + return null; + } + StringBuilder builder = new StringBuilder(); + LegacyConverter converter = tokenConverter; + for (Iterator iterator = value.iterator(); iterator.hasNext();) { + if (converter == null) { + builder.append(iterator.next()); + } else { + builder.append(converter.convertToPresentation(iterator.next(), + targetType, locale)); + } + builder.append(delimiter); + } + if (builder.length() > 0) { + return builder.substring(0, builder.length() - delimiter.length()); + } else { + return builder.toString(); + } + } + + private void collectToken(String token, Collection collection, + LegacyConverter converter, Locale locale) { + if (converter == null) { + collection.add(token); + } else { + collection.add(converter.convertToModel(token, tokenType, locale)); + } + } + + /** + * Default collection factory implementation. + * + * @author Vaadin Ltd + */ + public static class DefaultCollectionFactory implements CollectionFactory { + + @Override + public Collection createCollection(Class type) { + if (type.isAssignableFrom(ArrayList.class)) { + return new ArrayList(); + } else if (type.isAssignableFrom(HashSet.class)) { + return new HashSet(); + } else if (!type.isInterface() + && !Modifier.isAbstract(type.getModifiers())) { + try { + return type.newInstance(); + } catch (InstantiationException ignore) { + } catch (IllegalAccessException ignore) { + } + } + return new ArrayList(); + } + + } + + /** + * Collection factory. Defines a strategy to create collection by collection + * class. + * + * @author Vaadin Ltd + */ + public interface CollectionFactory extends Serializable { + + /** + * Create collection by its {@code type}. + * + * @param type + * collection type + * @return instantiated collection with given {@code type} + */ + Collection createCollection(Class type); + } +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDateConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDateConverter.java new file mode 100644 index 0000000000..40780bfdd0 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDateConverter.java @@ -0,0 +1,131 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.text.DateFormat; +import java.text.ParsePosition; +import java.util.Date; +import java.util.Locale; + +/** + * A converter that converts from {@link Date} to {@link String} and back. Uses + * the given locale and {@link DateFormat} for formatting and parsing. + *

+ * Leading and trailing white spaces are ignored when converting from a String. + *

+ *

+ * Override and overwrite {@link #getFormat(Locale)} to use a different format. + *

+ * + * @author Vaadin Ltd + * @since 7.0 + */ +public class LegacyStringToDateConverter implements LegacyConverter { + + /** + * Returns the format used by + * {@link #convertToPresentation(Date, Class,Locale)} and + * {@link #convertToModel(String, Class, Locale)}. + * + * @param locale + * The locale to use + * @return A DateFormat instance + */ + protected DateFormat getFormat(Locale locale) { + if (locale == null) { + locale = Locale.getDefault(); + } + + DateFormat f = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, + DateFormat.MEDIUM, locale); + f.setLenient(false); + return f; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, + * java.lang.Class, java.util.Locale) + */ + @Override + public Date convertToModel(String value, Class targetType, + Locale locale) + throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException { + if (targetType != getModelType()) { + throw new ConversionException("Converter only supports " + + getModelType().getName() + " (targetType was " + + targetType.getName() + ")"); + } + + if (value == null) { + return null; + } + + // Remove leading and trailing white space + value = value.trim(); + + ParsePosition parsePosition = new ParsePosition(0); + Date parsedValue = getFormat(locale).parse(value, parsePosition); + if (parsePosition.getIndex() != value.length()) { + throw new ConversionException("Could not convert '" + value + + "' to " + getModelType().getName()); + } + + return parsedValue; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang + * .Object, java.lang.Class, java.util.Locale) + */ + @Override + public String convertToPresentation(Date value, + Class targetType, Locale locale) + throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException { + if (value == null) { + return null; + } + + return getFormat(locale).format(value); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getModelType() + */ + @Override + public Class getModelType() { + return Date.class; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getPresentationType() + */ + @Override + public Class getPresentationType() { + return String.class; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDoubleConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDoubleConverter.java new file mode 100644 index 0000000000..191648fba9 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDoubleConverter.java @@ -0,0 +1,64 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.text.NumberFormat; +import java.util.Locale; + +/** + * A converter that converts from {@link String} to {@link Double} and back. + * Uses the given locale and a {@link NumberFormat} instance for formatting and + * parsing. + *

+ * Leading and trailing white spaces are ignored when converting from a String. + *

+ *

+ * Override and overwrite {@link #getFormat(Locale)} to use a different format. + *

+ * + * @author Vaadin Ltd + * @since 7.0 + */ +public class LegacyStringToDoubleConverter extends + LegacyAbstractStringToNumberConverter { + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, + * java.util.Locale) + */ + @Override + public Double convertToModel(String value, + Class targetType, Locale locale) + throws ConversionException { + Number n = convertToNumber(value, targetType, locale); + return n == null ? null : n.doubleValue(); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getModelType() + */ + @Override + public Class getModelType() { + return Double.class; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToEnumConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToEnumConverter.java new file mode 100644 index 0000000000..ef1dec64eb --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToEnumConverter.java @@ -0,0 +1,160 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.util.EnumSet; +import java.util.Locale; + +/** + * A converter that converts from {@link String} to an {@link Enum} and back. + *

+ * Designed to provide nice human readable strings for {@link Enum} classes + * conforming to one of these patterns: + *

    + *
  • The constants are named SOME_UPPERCASE_WORDS and there's no toString + * implementation.
  • + *
  • toString() always returns the same human readable string that is not the + * same as its name() value. Each constant in the enum type returns a distinct + * toString() value.
  • + *
+ * Will not necessarily work correctly for other cases. + *

+ * + * @author Vaadin Ltd + * @since 7.4 + */ +public class LegacyStringToEnumConverter implements LegacyConverter { + + @Override + public Enum convertToModel(String value, Class targetType, + Locale locale) throws ConversionException { + if (value == null || value.trim().equals("")) { + return null; + } + + return stringToEnum(value, targetType, locale); + } + + /** + * Converts the given string to the given enum type using the given locale + *

+ * Compatible with {@link #enumToString(Enum, Locale)} + * + * @param value + * The string value to convert + * @param enumType + * The type of enum to create + * @param locale + * The locale to use for conversion. If null, the JVM default + * locale will be used + * @return The enum which matches the given string + * @throws ConversionException + * if the conversion fails + */ + public static > T stringToEnum(String value, + Class enumType, Locale locale) throws ConversionException { + if (locale == null) { + locale = Locale.getDefault(); + } + + if (!enumType.isEnum()) { + throw new ConversionException(enumType.getName() + + " is not an enum type"); + } + + // First test for the human-readable value since that's the more likely + // input + String upperCaseValue = value.toUpperCase(locale); + T match = null; + for (T e : EnumSet.allOf(enumType)) { + String upperCase = enumToString(e, locale).toUpperCase(locale); + if (upperCase.equals(upperCaseValue)) { + if (match != null) { + throw new ConversionException("Both " + match.name() + + " and " + e.name() + + " are matching the input string " + value); + } + match = e; + } + } + + if (match != null) { + return match; + } + + // Then fall back to using a strict match based on name() + try { + return Enum.valueOf(enumType, upperCaseValue); + } catch (Exception ee) { + throw new ConversionException(ee); + } + } + + /** + * Converts the given enum to a human readable string using the given locale + *

+ * Compatible with {@link #stringToEnum(String, Class, Locale)} + * + * @param value + * The enum value to convert + * @param locale + * The locale to use for conversion. If null, the JVM default + * locale will be used + * @return A human readable string based on the enum + * @throws ConversionException + * if the conversion fails + */ + public static String enumToString(Enum value, Locale locale) { + if (locale == null) { + locale = Locale.getDefault(); + } + + String enumString = value.toString(); + if (enumString.equals(value.name())) { + // FOO -> Foo + // FOO_BAR -> Foo bar + // _FOO -> _foo + String result = enumString.substring(0, 1).toUpperCase(locale); + result += enumString.substring(1).toLowerCase(locale) + .replace('_', ' '); + return result; + } else { + return enumString; + } + } + + @Override + public String convertToPresentation(Enum value, + Class targetType, Locale locale) + throws ConversionException { + if (value == null) { + return null; + } + + return enumToString(value, locale); + } + + @Override + public Class getModelType() { + return Enum.class; + } + + @Override + public Class getPresentationType() { + return String.class; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToFloatConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToFloatConverter.java new file mode 100644 index 0000000000..0d24e16a35 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToFloatConverter.java @@ -0,0 +1,64 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.text.NumberFormat; +import java.util.Locale; + +/** + * A converter that converts from {@link String} to {@link Float} and back. Uses + * the given locale and a {@link NumberFormat} instance for formatting and + * parsing. + *

+ * Leading and trailing white spaces are ignored when converting from a String. + *

+ *

+ * Override and overwrite {@link #getFormat(Locale)} to use a different format. + *

+ * + * @author Vaadin Ltd + * @since 7.0 + */ +public class LegacyStringToFloatConverter extends + LegacyAbstractStringToNumberConverter { + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, + * java.util.Locale) + */ + @Override + public Float convertToModel(String value, + Class targetType, Locale locale) + throws ConversionException { + Number n = convertToNumber(value, targetType, locale); + return n == null ? null : n.floatValue(); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getModelType() + */ + @Override + public Class getModelType() { + return Float.class; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToIntegerConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToIntegerConverter.java new file mode 100644 index 0000000000..37c00b7c3e --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToIntegerConverter.java @@ -0,0 +1,94 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.text.NumberFormat; +import java.util.Locale; + +/** + * A converter that converts from {@link String} to {@link Integer} and back. + * Uses the given locale and a {@link NumberFormat} instance for formatting and + * parsing. + *

+ * Override and overwrite {@link #getFormat(Locale)} to use a different format. + *

+ * + * @author Vaadin Ltd + * @since 7.0 + */ +public class LegacyStringToIntegerConverter extends + LegacyAbstractStringToNumberConverter { + + /** + * Returns the format used by + * {@link #convertToPresentation(Integer, Class, Locale)} and + * {@link #convertToModel(String, Class, Locale)} + * + * @param locale + * The locale to use + * @return A NumberFormat instance + */ + @Override + protected NumberFormat getFormat(Locale locale) { + if (locale == null) { + locale = Locale.getDefault(); + } + return NumberFormat.getIntegerInstance(locale); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, + * java.lang.Class, java.util.Locale) + */ + @Override + public Integer convertToModel(String value, + Class targetType, Locale locale) + throws ConversionException { + Number n = convertToNumber(value, targetType, locale); + + if (n == null) { + return null; + } + + int intValue = n.intValue(); + if (intValue == n.longValue()) { + // If the value of n is outside the range of long, the return value + // of longValue() is either Long.MIN_VALUE or Long.MAX_VALUE. The + // above comparison promotes int to long and thus does not need to + // consider wrap-around. + return intValue; + } + + throw new ConversionException("Could not convert '" + value + "' to " + + Integer.class.getName() + ": value out of range"); + + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getModelType() + */ + @Override + public Class getModelType() { + return Integer.class; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToLongConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToLongConverter.java new file mode 100644 index 0000000000..0de27bc3f3 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToLongConverter.java @@ -0,0 +1,78 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.text.NumberFormat; +import java.util.Locale; + +/** + * A converter that converts from {@link String} to {@link Long} and back. Uses + * the given locale and a {@link NumberFormat} instance for formatting and + * parsing. + *

+ * Override and overwrite {@link #getFormat(Locale)} to use a different format. + *

+ * + * @author Vaadin Ltd + * @since 7.2 + */ +public class LegacyStringToLongConverter extends + LegacyAbstractStringToNumberConverter { + + /** + * Returns the format used by + * {@link #convertToPresentation(Long, Class, Locale)} and + * {@link #convertToModel(String, Class, Locale)} + * + * @param locale + * The locale to use + * @return A NumberFormat instance + */ + @Override + protected NumberFormat getFormat(Locale locale) { + if (locale == null) { + locale = Locale.getDefault(); + } + return NumberFormat.getIntegerInstance(locale); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, + * java.lang.Class, java.util.Locale) + */ + @Override + public Long convertToModel(String value, Class targetType, + Locale locale) throws ConversionException { + Number n = convertToNumber(value, targetType, locale); + return n == null ? null : n.longValue(); + + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getModelType() + */ + @Override + public Class getModelType() { + return Long.class; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToShortConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToShortConverter.java new file mode 100644 index 0000000000..8b1646542f --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToShortConverter.java @@ -0,0 +1,90 @@ +/* + * Copyright 2000-2014 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.legacy.data.util.converter; + +import java.text.NumberFormat; +import java.util.Locale; + +/** + * A converter that converts from {@link String} to {@link Short} and back. Uses + * the given locale and a {@link NumberFormat} instance for formatting and + * parsing. + *

+ * Override and overwrite {@link #getFormat(Locale)} to use a different format. + *

+ * + * @author Vaadin Ltd + * @since 7.4 + */ +public class LegacyStringToShortConverter extends + LegacyAbstractStringToNumberConverter { + + /** + * Returns the format used by + * {@link #convertToPresentation(Short, Class, Locale)} and + * {@link #convertToModel(String, Class, Locale)} + * + * @param locale + * The locale to use + * @return A NumberFormat instance + */ + @Override + protected NumberFormat getFormat(Locale locale) { + if (locale == null) { + locale = Locale.getDefault(); + } + return NumberFormat.getIntegerInstance(locale); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, + * java.lang.Class, java.util.Locale) + */ + @Override + public Short convertToModel(String value, + Class targetType, Locale locale) + throws ConversionException { + Number n = convertToNumber(value, targetType, locale); + + if (n == null) { + return null; + } + + short shortValue = n.shortValue(); + if (shortValue == n.longValue()) { + return shortValue; + } + + throw new ConversionException("Could not convert '" + value + "' to " + + Short.class.getName() + ": value out of range"); + + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getModelType() + */ + @Override + public Class getModelType() { + return Short.class; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDoubleValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDoubleValidator.java index 0c804e7247..dd45f365d0 100644 --- a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDoubleValidator.java +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDoubleValidator.java @@ -16,7 +16,7 @@ package com.vaadin.legacy.data.validator; import com.vaadin.data.Property; -import com.vaadin.data.util.converter.StringToDoubleConverter; +import com.vaadin.legacy.data.util.converter.LegacyStringToDoubleConverter; /** * String validator for a double precision floating point number. See @@ -25,7 +25,7 @@ import com.vaadin.data.util.converter.StringToDoubleConverter; * * @author Vaadin Ltd. * @since 5.4 - * @deprecated As of 7.0. Use a {@link StringToDoubleConverter} converter on the + * @deprecated As of 7.0. Use a {@link LegacyStringToDoubleConverter} converter on the * field instead or bind the field to a {@link Property} of type * {@link Double}. */ diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyIntegerValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyIntegerValidator.java index e6c2bae815..dc34215680 100644 --- a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyIntegerValidator.java +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyIntegerValidator.java @@ -16,7 +16,7 @@ package com.vaadin.legacy.data.validator; import com.vaadin.data.Property; -import com.vaadin.data.util.converter.StringToIntegerConverter; +import com.vaadin.legacy.data.util.converter.LegacyStringToIntegerConverter; /** * String validator for integers. See @@ -25,7 +25,7 @@ import com.vaadin.data.util.converter.StringToIntegerConverter; * * @author Vaadin Ltd. * @since 5.4 - * @deprecated As of 7.0. Use a {@link StringToIntegerConverter} converter on + * @deprecated As of 7.0. Use a {@link LegacyStringToIntegerConverter} converter on * the field instead or bind the field to a {@link Property} of type * {@link Integer}. */ diff --git a/server/src/main/java/com/vaadin/legacy/ui/LegacyAbstractField.java b/server/src/main/java/com/vaadin/legacy/ui/LegacyAbstractField.java index 5beada527f..402f1fb257 100644 --- a/server/src/main/java/com/vaadin/legacy/ui/LegacyAbstractField.java +++ b/server/src/main/java/com/vaadin/legacy/ui/LegacyAbstractField.java @@ -33,15 +33,15 @@ import org.jsoup.nodes.Element; import com.vaadin.data.Buffered; import com.vaadin.data.Property; import com.vaadin.data.util.LegacyPropertyHelper; -import com.vaadin.data.util.converter.Converter; -import com.vaadin.data.util.converter.Converter.ConversionException; -import com.vaadin.data.util.converter.ConverterUtil; import com.vaadin.event.Action; import com.vaadin.event.ShortcutAction; import com.vaadin.event.ShortcutListener; import com.vaadin.legacy.data.Validatable; import com.vaadin.legacy.data.Validator; import com.vaadin.legacy.data.Validator.InvalidValueException; +import com.vaadin.legacy.data.util.converter.LegacyConverter; +import com.vaadin.legacy.data.util.converter.LegacyConverterUtil; +import com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException; import com.vaadin.server.AbstractErrorMessage; import com.vaadin.server.CompositeErrorMessage; import com.vaadin.server.ErrorMessage; @@ -99,7 +99,7 @@ public abstract class LegacyAbstractField extends AbstractComponent * A converter used to convert from the data model type to the field type * and vice versa. */ - private Converter converter = null; + private LegacyConverter converter = null; /** * Connected data-source. */ @@ -459,7 +459,7 @@ public abstract class LegacyAbstractField extends AbstractComponent */ @Override public void setValue(T newFieldValue) throws Property.ReadOnlyException, - Converter.ConversionException { + LegacyConverter.ConversionException { setValue(newFieldValue, false); } @@ -471,7 +471,7 @@ public abstract class LegacyAbstractField extends AbstractComponent * @param repaintIsNotNeeded * True iff caller is sure that repaint is not needed. * @throws Property.ReadOnlyException - * @throws Converter.ConversionException + * @throws LegacyConverter.ConversionException * @throws InvalidValueException */ protected void setValue(T newFieldValue, boolean repaintIsNotNeeded) { @@ -489,12 +489,12 @@ public abstract class LegacyAbstractField extends AbstractComponent * @param ignoreReadOnly * True iff if the read-only check should be ignored * @throws Property.ReadOnlyException - * @throws Converter.ConversionException + * @throws LegacyConverter.ConversionException * @throws InvalidValueException */ protected void setValue(T newFieldValue, boolean repaintIsNotNeeded, boolean ignoreReadOnly) throws Property.ReadOnlyException, - Converter.ConversionException, InvalidValueException { + LegacyConverter.ConversionException, InvalidValueException { if (!SharedUtil.equals(newFieldValue, getInternalValue())) { @@ -656,7 +656,7 @@ public abstract class LegacyAbstractField extends AbstractComponent // Check if the current converter is compatible. if (newDataSource != null - && !ConverterUtil.canConverterPossiblyHandle(getConverter(), + && !LegacyConverterUtil.canConverterPossiblyHandle(getConverter(), getType(), newDataSource.getType())) { // There is no converter set or there is no way the current // converter can be compatible. @@ -714,7 +714,7 @@ public abstract class LegacyAbstractField extends AbstractComponent * from */ public void setConverter(Class datamodelType) { - Converter c = (Converter) ConverterUtil.getConverter( + LegacyConverter c = (LegacyConverter) LegacyConverterUtil.getConverter( getType(), datamodelType, getSession()); setConverter(c); } @@ -726,7 +726,7 @@ public abstract class LegacyAbstractField extends AbstractComponent * The data source value to convert. * @return The converted value that is compatible with the UI type or the * original value if its type is compatible and no converter is set. - * @throws Converter.ConversionException + * @throws LegacyConverter.ConversionException * if there is no converter and the type is not compatible with * the data source type. */ @@ -741,12 +741,12 @@ public abstract class LegacyAbstractField extends AbstractComponent * The data source value to convert. * @return The converted value that is compatible with the UI type or the * original value if its type is compatible and no converter is set. - * @throws Converter.ConversionException + * @throws LegacyConverter.ConversionException * if there is no converter and the type is not compatible with * the data source type. */ private T convertFromModel(Object newValue, Locale locale) { - return ConverterUtil.convertFromModel(newValue, getType(), + return LegacyConverterUtil.convertFromModel(newValue, getType(), getConverter(), locale); } @@ -757,12 +757,12 @@ public abstract class LegacyAbstractField extends AbstractComponent * The value to convert. Typically returned by * {@link #getFieldValue()} * @return The converted value that is compatible with the data source type. - * @throws Converter.ConversionException + * @throws LegacyConverter.ConversionException * if there is no converter and the type is not compatible with * the data source type. */ private Object convertToModel(T fieldValue) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { return convertToModel(fieldValue, getLocale()); } @@ -775,15 +775,15 @@ public abstract class LegacyAbstractField extends AbstractComponent * @param locale * The locale to use for the conversion * @return The converted value that is compatible with the data source type. - * @throws Converter.ConversionException + * @throws LegacyConverter.ConversionException * if there is no converter and the type is not compatible with * the data source type. */ private Object convertToModel(T fieldValue, Locale locale) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { Class modelType = getModelType(); try { - return ConverterUtil.convertToModel(fieldValue, + return LegacyConverterUtil.convertToModel(fieldValue, (Class) modelType, getConverter(), locale); } catch (ConversionException e) { throw new ConversionException(getConversionError(modelType, e), e); @@ -1727,7 +1727,7 @@ public abstract class LegacyAbstractField extends AbstractComponent * * @return The converter or null if none is set. */ - public Converter getConverter() { + public LegacyConverter getConverter() { return converter; } @@ -1739,8 +1739,8 @@ public abstract class LegacyAbstractField extends AbstractComponent * @param converter * The new converter to use. */ - public void setConverter(Converter converter) { - this.converter = (Converter) converter; + public void setConverter(LegacyConverter converter) { + this.converter = (LegacyConverter) converter; markAsDirty(); } diff --git a/server/src/main/java/com/vaadin/server/VaadinSession.java b/server/src/main/java/com/vaadin/server/VaadinSession.java index d99bd75de8..d7a85cecfd 100644 --- a/server/src/main/java/com/vaadin/server/VaadinSession.java +++ b/server/src/main/java/com/vaadin/server/VaadinSession.java @@ -47,10 +47,10 @@ import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; -import com.vaadin.data.util.converter.Converter; -import com.vaadin.data.util.converter.ConverterFactory; -import com.vaadin.data.util.converter.DefaultConverterFactory; import com.vaadin.event.EventRouter; +import com.vaadin.legacy.data.util.converter.LegacyConverter; +import com.vaadin.legacy.data.util.converter.LegacyConverterFactory; +import com.vaadin.legacy.data.util.converter.LegacyDefaultConverterFactory; import com.vaadin.legacy.ui.LegacyAbstractField; import com.vaadin.shared.communication.PushMode; import com.vaadin.ui.Table; @@ -228,7 +228,7 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { * The converter factory that is used to provide default converters for the * session. */ - private ConverterFactory converterFactory = new DefaultConverterFactory(); + private LegacyConverterFactory converterFactory = new LegacyDefaultConverterFactory(); private LinkedList requestHandlers = new LinkedList(); @@ -592,32 +592,32 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { } /** - * Gets the {@link ConverterFactory} used to locate a suitable - * {@link Converter} for fields in the session. + * Gets the {@link LegacyConverterFactory} used to locate a suitable + * {@link LegacyConverter} for fields in the session. * - * See {@link #setConverterFactory(ConverterFactory)} for more details + * See {@link #setConverterFactory(LegacyConverterFactory)} for more details * * @return The converter factory used in the session */ - public ConverterFactory getConverterFactory() { + public LegacyConverterFactory getConverterFactory() { assert hasLock(); return converterFactory; } /** - * Sets the {@link ConverterFactory} used to locate a suitable - * {@link Converter} for fields in the session. + * Sets the {@link LegacyConverterFactory} used to locate a suitable + * {@link LegacyConverter} for fields in the session. *

- * The {@link ConverterFactory} is used to find a suitable converter when + * The {@link LegacyConverterFactory} is used to find a suitable converter when * binding data to a UI component and the data type does not match the UI * component type, e.g. binding a Double to a TextField (which is based on a * String). *

*

- * The {@link Converter} for an individual field can be overridden using - * {@link LegacyAbstractField#setConverter(Converter)} and for individual property + * The {@link LegacyConverter} for an individual field can be overridden using + * {@link LegacyAbstractField#setConverter(LegacyConverter)} and for individual property * ids in a {@link Table} using - * {@link Table#setConverter(Object, Converter)}. + * {@link Table#setConverter(Object, LegacyConverter)}. *

*

* The converter factory must never be set to null. @@ -625,7 +625,7 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { * @param converterFactory * The converter factory used in the session */ - public void setConverterFactory(ConverterFactory converterFactory) { + public void setConverterFactory(LegacyConverterFactory converterFactory) { assert hasLock(); this.converterFactory = converterFactory; } diff --git a/server/src/main/java/com/vaadin/ui/AbstractSelect.java b/server/src/main/java/com/vaadin/ui/AbstractSelect.java index 1867e2edfd..d264b01f0e 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractSelect.java +++ b/server/src/main/java/com/vaadin/ui/AbstractSelect.java @@ -35,9 +35,6 @@ import com.vaadin.data.Container; import com.vaadin.data.Item; import com.vaadin.data.Property; import com.vaadin.data.util.IndexedContainer; -import com.vaadin.data.util.converter.Converter; -import com.vaadin.data.util.converter.Converter.ConversionException; -import com.vaadin.data.util.converter.ConverterUtil; import com.vaadin.event.DataBoundTransferable; import com.vaadin.event.Transferable; import com.vaadin.event.dd.DragAndDropEvent; @@ -47,6 +44,9 @@ import com.vaadin.event.dd.acceptcriteria.ClientSideCriterion; import com.vaadin.event.dd.acceptcriteria.ContainsDataFlavor; import com.vaadin.event.dd.acceptcriteria.TargetDetailIs; import com.vaadin.legacy.data.Validator.InvalidValueException; +import com.vaadin.legacy.data.util.converter.LegacyConverter; +import com.vaadin.legacy.data.util.converter.LegacyConverterUtil; +import com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException; import com.vaadin.legacy.ui.LegacyAbstractField; import com.vaadin.server.KeyMapper; import com.vaadin.server.PaintException; @@ -1265,9 +1265,9 @@ public abstract class AbstractSelect extends LegacyAbstractField impleme private String idToCaption(Object itemId) { try { - Converter c = (Converter) ConverterUtil + LegacyConverter c = (LegacyConverter) LegacyConverterUtil .getConverter(String.class, itemId.getClass(), getSession()); - return ConverterUtil.convertFromModel(itemId, String.class, c, + return LegacyConverterUtil.convertFromModel(itemId, String.class, c, getLocale()); } catch (Exception e) { return itemId.toString(); diff --git a/server/src/main/java/com/vaadin/ui/DateField.java b/server/src/main/java/com/vaadin/ui/DateField.java index f5a582f571..1630053ee1 100644 --- a/server/src/main/java/com/vaadin/ui/DateField.java +++ b/server/src/main/java/com/vaadin/ui/DateField.java @@ -29,7 +29,6 @@ import java.util.logging.Logger; import org.jsoup.nodes.Element; import com.vaadin.data.Property; -import com.vaadin.data.util.converter.Converter; import com.vaadin.event.FieldEvents; import com.vaadin.event.FieldEvents.BlurEvent; import com.vaadin.event.FieldEvents.BlurListener; @@ -38,6 +37,7 @@ import com.vaadin.event.FieldEvents.FocusListener; import com.vaadin.legacy.data.Validator; import com.vaadin.legacy.data.Validator.InvalidValueException; import com.vaadin.legacy.data.validator.LegacyDateRangeValidator; +import com.vaadin.legacy.data.util.converter.LegacyConverter; import com.vaadin.legacy.ui.LegacyAbstractField; import com.vaadin.legacy.ui.LegacyField; import com.vaadin.server.PaintException; @@ -558,7 +558,7 @@ public class DateField extends LegacyAbstractField implements * this case the invalid text remains in the DateField. */ markAsDirty(); - } catch (Converter.ConversionException e) { + } catch (LegacyConverter.ConversionException e) { /* * Datefield now contains some text that could't be parsed @@ -659,13 +659,13 @@ public class DateField extends LegacyAbstractField implements * * @param dateString * @return parsed Date - * @throws Converter.ConversionException + * @throws LegacyConverter.ConversionException * to keep the old value and indicate an error */ protected Date handleUnparsableDateString(String dateString) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { currentParseErrorMessage = null; - throw new Converter.ConversionException(getParseErrorMessage()); + throw new LegacyConverter.ConversionException(getParseErrorMessage()); } /* Property features */ diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index 592175eeff..1319f868c7 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -60,8 +60,6 @@ import com.vaadin.data.fieldgroup.FieldGroupFieldFactory; import com.vaadin.data.sort.Sort; import com.vaadin.data.sort.SortOrder; import com.vaadin.data.util.IndexedContainer; -import com.vaadin.data.util.converter.Converter; -import com.vaadin.data.util.converter.ConverterUtil; import com.vaadin.event.ContextClickEvent; import com.vaadin.event.ItemClickEvent; import com.vaadin.event.ItemClickEvent.ItemClickListener; @@ -73,6 +71,8 @@ import com.vaadin.event.SortEvent; import com.vaadin.event.SortEvent.SortListener; import com.vaadin.event.SortEvent.SortNotifier; import com.vaadin.legacy.data.Validator.InvalidValueException; +import com.vaadin.legacy.data.util.converter.LegacyConverter; +import com.vaadin.legacy.data.util.converter.LegacyConverterUtil; import com.vaadin.legacy.ui.LegacyField; import com.vaadin.server.AbstractClientConnector; import com.vaadin.server.AbstractExtension; @@ -133,7 +133,7 @@ import elemental.json.JsonValue; *

* Each column has its own {@link Renderer} that displays data into something * that can be displayed in the browser. That data is first converted with a - * {@link com.vaadin.data.util.converter.Converter Converter} into something + * {@link com.vaadin.legacy.data.util.converter.LegacyConverter Converter} into something * that the Renderer can process. This can also be an implicit step - if a * column has a simple data type, like a String, no explicit assignment is * needed. @@ -2196,7 +2196,7 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, private void writeData(CellReference cell, JsonObject data) { Column column = getColumn(cell.getPropertyId()); - Converter converter = column.getConverter(); + LegacyConverter converter = column.getConverter(); Renderer renderer = column.getRenderer(); Item item = cell.getItem(); @@ -3216,12 +3216,12 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, */ private final Object propertyId; - private Converter converter; + private LegacyConverter converter; /** * A check for allowing the * {@link #Column(Grid, GridColumnState, Object) constructor} to call - * {@link #setConverter(Converter)} with a null, even if + * {@link #setConverter(LegacyConverter)} with a null, even if * model and renderer aren't compatible. */ private boolean isFirstConverterAssignment = true; @@ -3462,8 +3462,8 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, * if no compatible converter could be found * * @see VaadinSession#getConverterFactory() - * @see ConverterUtil#getConverter(Class, Class, VaadinSession) - * @see #setConverter(Converter) + * @see LegacyConverterUtil#getConverter(Class, Class, VaadinSession) + * @see #setConverter(LegacyConverter) */ public Column setRenderer(Renderer renderer) { if (!internalSetRenderer(renderer)) { @@ -3491,7 +3491,7 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, * if the renderer is already associated with a grid column */ public Column setRenderer(Renderer renderer, - Converter converter) { + LegacyConverter converter) { if (renderer.getParent() != null) { throw new IllegalArgumentException( "Cannot set a renderer that is already connected to a grid column (in " @@ -3520,7 +3520,7 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, * @throws IllegalArgumentException * if the types are not compatible */ - public Column setConverter(Converter converter) + public Column setConverter(LegacyConverter converter) throws IllegalArgumentException { Class modelType = getModelType(); if (converter != null) { @@ -3574,7 +3574,7 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, isFirstConverterAssignment = false; @SuppressWarnings("unchecked") - Converter castConverter = (Converter) converter; + LegacyConverter castConverter = (LegacyConverter) converter; this.converter = castConverter; return this; @@ -3594,19 +3594,19 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, * * @return the converter */ - public Converter getConverter() { + public LegacyConverter getConverter() { return converter; } private boolean internalSetRenderer(Renderer renderer) { - Converter converter; + LegacyConverter converter; if (isCompatibleWithProperty(renderer, getConverter())) { // Use the existing converter (possibly none) if types // compatible - converter = (Converter) getConverter(); + converter = (LegacyConverter) getConverter(); } else { - converter = ConverterUtil.getConverter( + converter = LegacyConverterUtil.getConverter( renderer.getPresentationType(), getModelType(), getSession()); } @@ -3620,7 +3620,7 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, } private boolean isCompatibleWithProperty(Renderer renderer, - Converter converter) { + LegacyConverter converter) { Class type; if (converter == null) { type = getModelType(); @@ -4231,7 +4231,7 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, * @return an encoded value ready to be sent to the client */ public static JsonValue encodeValue(Object modelValue, - Renderer renderer, Converter converter, + Renderer renderer, LegacyConverter converter, Locale locale) { Class presentationType = renderer.getPresentationType(); T presentationValue; @@ -4246,7 +4246,7 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, // Class.cast(null) will always succeed presentationValue = (T) modelValue.toString(); } else { - throw new Converter.ConversionException( + throw new LegacyConverter.ConversionException( "Unable to convert value of type " + modelValue.getClass().getName() + " to presentation type " @@ -4258,7 +4258,7 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, assert presentationType.isAssignableFrom(converter .getPresentationType()); @SuppressWarnings("unchecked") - Converter safeConverter = (Converter) converter; + LegacyConverter safeConverter = (LegacyConverter) converter; presentationValue = safeConverter .convertToPresentation(modelValue, safeConverter.getPresentationType(), locale); diff --git a/server/src/main/java/com/vaadin/ui/Label.java b/server/src/main/java/com/vaadin/ui/Label.java index 1498246ec5..97b6648977 100644 --- a/server/src/main/java/com/vaadin/ui/Label.java +++ b/server/src/main/java/com/vaadin/ui/Label.java @@ -25,8 +25,8 @@ import org.jsoup.nodes.Element; import com.vaadin.data.Property; import com.vaadin.data.util.AbstractProperty; import com.vaadin.data.util.LegacyPropertyHelper; -import com.vaadin.data.util.converter.Converter; -import com.vaadin.data.util.converter.ConverterUtil; +import com.vaadin.legacy.data.util.converter.LegacyConverter; +import com.vaadin.legacy.data.util.converter.LegacyConverterUtil; import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.shared.ui.label.LabelState; import com.vaadin.shared.util.SharedUtil; @@ -100,7 +100,7 @@ public class Label extends AbstractComponent implements Property, * A converter used to convert from the data model type to the field type * and vice versa. Label type is always String. */ - private Converter converter = null; + private LegacyConverter converter = null; private Property dataSource = null; @@ -190,7 +190,7 @@ public class Label extends AbstractComponent implements Property, * @return */ private String getDataSourceValue() { - return ConverterUtil.convertFromModel(getPropertyDataSource() + return LegacyConverterUtil.convertFromModel(getPropertyDataSource() .getValue(), String.class, getConverter(), getLocale()); } @@ -258,11 +258,11 @@ public class Label extends AbstractComponent implements Property, // Check if the current converter is compatible. if (newDataSource != null - && !ConverterUtil.canConverterPossiblyHandle(getConverter(), + && !LegacyConverterUtil.canConverterPossiblyHandle(getConverter(), getType(), newDataSource.getType())) { // There is no converter set or there is no way the current // converter can be compatible. - Converter c = ConverterUtil.getConverter(String.class, + LegacyConverter c = LegacyConverterUtil.getConverter(String.class, newDataSource.getType(), getSession()); setConverter(c); } @@ -528,7 +528,7 @@ public class Label extends AbstractComponent implements Property, * * @return The converter or null if none is set. */ - public Converter getConverter() { + public LegacyConverter getConverter() { return converter; } @@ -539,8 +539,8 @@ public class Label extends AbstractComponent implements Property, * @param converter * The new converter to use. */ - public void setConverter(Converter converter) { - this.converter = (Converter) converter; + public void setConverter(LegacyConverter converter) { + this.converter = (LegacyConverter) converter; markAsDirty(); } diff --git a/server/src/main/java/com/vaadin/ui/Table.java b/server/src/main/java/com/vaadin/ui/Table.java index 4e85b9e028..b60fe473b1 100644 --- a/server/src/main/java/com/vaadin/ui/Table.java +++ b/server/src/main/java/com/vaadin/ui/Table.java @@ -42,8 +42,6 @@ import com.vaadin.data.Item; import com.vaadin.data.Property; import com.vaadin.data.util.ContainerOrderedWrapper; import com.vaadin.data.util.IndexedContainer; -import com.vaadin.data.util.converter.Converter; -import com.vaadin.data.util.converter.ConverterUtil; import com.vaadin.event.Action; import com.vaadin.event.Action.Handler; import com.vaadin.event.ContextClickEvent; @@ -57,6 +55,8 @@ import com.vaadin.event.dd.DragSource; import com.vaadin.event.dd.DropHandler; import com.vaadin.event.dd.DropTarget; import com.vaadin.event.dd.acceptcriteria.ServerSideCriterion; +import com.vaadin.legacy.data.util.converter.LegacyConverter; +import com.vaadin.legacy.data.util.converter.LegacyConverterUtil; import com.vaadin.legacy.ui.LegacyField; import com.vaadin.server.KeyMapper; import com.vaadin.server.LegacyCommunicationManager; @@ -586,7 +586,7 @@ public class Table extends AbstractSelect implements Action.Container, private boolean painted = false; - private HashMap> propertyValueConverters = new HashMap>(); + private HashMap> propertyValueConverters = new HashMap>(); /** * Set to true if the client-side should be informed that the key mapper has @@ -2777,7 +2777,7 @@ public class Table extends AbstractSelect implements Action.Container, .getContainerPropertyIds(); LinkedList retainableValueConverters = new LinkedList(); for (Object propertyId : newPropertyIds) { - Converter converter = getConverter(propertyId); + LegacyConverter converter = getConverter(propertyId); if (converter != null) { if (typeIsCompatible(converter.getModelType(), newDataSource.getType(propertyId))) { @@ -4163,12 +4163,12 @@ public class Table extends AbstractSelect implements Action.Container, if (property == null) { return ""; } - Converter converter = null; + LegacyConverter converter = null; if (hasConverter(colId)) { converter = getConverter(colId); } else { - converter = (Converter) ConverterUtil.getConverter(String.class, + converter = (LegacyConverter) LegacyConverterUtil.getConverter(String.class, property.getType(), getSession()); } Object value = property.getValue(); @@ -6071,7 +6071,7 @@ public class Table extends AbstractSelect implements Action.Container, * @param converter * The converter to use for the property id */ - public void setConverter(Object propertyId, Converter converter) { + public void setConverter(Object propertyId, LegacyConverter converter) { if (!getContainerPropertyIds().contains(propertyId)) { throw new IllegalArgumentException("PropertyId " + propertyId + " must be in the container"); @@ -6084,7 +6084,7 @@ public class Table extends AbstractSelect implements Action.Container, + converter.getModelType() + ")"); } propertyValueConverters.put(propertyId, - (Converter) converter); + (LegacyConverter) converter); refreshRowCache(); } @@ -6108,7 +6108,7 @@ public class Table extends AbstractSelect implements Action.Container, * @return The converter used to format the propertyId or null if no * converter has been set */ - public Converter getConverter(Object propertyId) { + public LegacyConverter getConverter(Object propertyId) { return propertyValueConverters.get(propertyId); } 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 bbfd90b2cc..35cbf755d4 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java +++ b/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java @@ -36,7 +36,7 @@ import org.jsoup.nodes.Attributes; import org.jsoup.nodes.Element; import org.jsoup.nodes.Node; -import com.vaadin.data.util.converter.Converter; +import com.vaadin.legacy.data.util.converter.LegacyConverter; import com.vaadin.shared.ui.AlignmentInfo; import com.vaadin.shared.util.SharedUtil; import com.vaadin.ui.Alignment; @@ -381,7 +381,7 @@ public class DesignAttributeHandler implements Serializable { // value is not null. How to represent null value in attributes? return ""; } - Converter converter = getFormatter().findConverterFor( + LegacyConverter converter = getFormatter().findConverterFor( sourceType); if (converter != null) { return converter.convertToPresentation(value, String.class, null); 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 509015130d..c5b67a06d8 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java @@ -30,11 +30,11 @@ import java.util.concurrent.ConcurrentHashMap; import org.jsoup.parser.Parser; -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.event.ShortcutAction; +import com.vaadin.legacy.data.util.converter.LegacyConverter; +import com.vaadin.legacy.data.util.converter.LegacyStringToBigDecimalConverter; +import com.vaadin.legacy.data.util.converter.LegacyStringToDoubleConverter; +import com.vaadin.legacy.data.util.converter.LegacyStringToFloatConverter; import com.vaadin.server.Resource; import com.vaadin.ui.AbstractSelect; import com.vaadin.ui.declarative.converters.DesignDateConverter; @@ -55,9 +55,9 @@ import com.vaadin.ui.declarative.converters.DesignToStringConverter; */ public class DesignFormatter implements Serializable { - private final Map, Converter> converterMap = new ConcurrentHashMap, Converter>(); - private final Converter stringEnumConverter = new DesignEnumConverter(); - private final Converter stringObjectConverter = new DesignObjectConverter(); + private final Map, LegacyConverter> converterMap = new ConcurrentHashMap, LegacyConverter>(); + private final LegacyConverter stringEnumConverter = new DesignEnumConverter(); + private final LegacyConverter stringObjectConverter = new DesignObjectConverter(); /** * Creates the formatter with default types already mapped. @@ -84,19 +84,19 @@ public class DesignFormatter implements Serializable { } // booleans use a bit different converter than the standard one // "false" is boolean false, everything else is boolean true - Converter booleanConverter = new Converter() { + LegacyConverter booleanConverter = new LegacyConverter() { @Override public Boolean convertToModel(String value, Class targetType, Locale locale) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { return !value.equalsIgnoreCase("false"); } @Override public String convertToPresentation(Boolean value, Class targetType, Locale locale) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { if (value.booleanValue()) { return ""; } else { @@ -124,7 +124,7 @@ public class DesignFormatter implements Serializable { final DecimalFormat fmt = new DecimalFormat("0.###", symbols); fmt.setGroupingUsed(false); - Converter floatConverter = new StringToFloatConverter() { + LegacyConverter floatConverter = new LegacyStringToFloatConverter() { @Override protected NumberFormat getFormat(Locale locale) { return fmt; @@ -133,7 +133,7 @@ public class DesignFormatter implements Serializable { converterMap.put(Float.class, floatConverter); converterMap.put(float.class, floatConverter); - Converter doubleConverter = new StringToDoubleConverter() { + LegacyConverter doubleConverter = new LegacyStringToDoubleConverter() { @Override protected NumberFormat getFormat(Locale locale) { return fmt; @@ -145,7 +145,7 @@ public class DesignFormatter implements Serializable { final DecimalFormat bigDecimalFmt = new DecimalFormat("0.###", symbols); bigDecimalFmt.setGroupingUsed(false); bigDecimalFmt.setParseBigDecimal(true); - converterMap.put(BigDecimal.class, new StringToBigDecimalConverter() { + converterMap.put(BigDecimal.class, new LegacyStringToBigDecimalConverter() { @Override protected NumberFormat getFormat(Locale locale) { return bigDecimalFmt; @@ -153,19 +153,19 @@ public class DesignFormatter implements Serializable { }); // strings do nothing - converterMap.put(String.class, new Converter() { + converterMap.put(String.class, new LegacyConverter() { @Override public String convertToModel(String value, Class targetType, Locale locale) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { return value; } @Override public String convertToPresentation(String value, Class targetType, Locale locale) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { return value; } @@ -182,13 +182,13 @@ public class DesignFormatter implements Serializable { }); // char takes the first character from the string - Converter charConverter = new DesignToStringConverter( + LegacyConverter charConverter = new DesignToStringConverter( Character.class) { @Override public Character convertToModel(String value, Class targetType, Locale locale) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { return value.charAt(0); } @@ -209,7 +209,7 @@ public class DesignFormatter implements Serializable { * @param converter * Converter to add. */ - protected void addConverter(Converter converter) { + protected void addConverter(LegacyConverter converter) { converterMap.put(converter.getModelType(), converter); } @@ -222,7 +222,7 @@ public class DesignFormatter implements Serializable { * Converter. */ protected void addConverter(Class type, - Converter converter) { + LegacyConverter converter) { converterMap.put(type, converter); } @@ -258,7 +258,7 @@ public class DesignFormatter implements Serializable { * converter for that type. */ public T parse(String value, Class type) { - Converter converter = findConverterFor(type); + LegacyConverter converter = findConverterFor(type); if (converter != null) { return converter.convertToModel(value, type, null); } else { @@ -292,7 +292,7 @@ public class DesignFormatter implements Serializable { if (object == null) { return null; } else { - Converter converter = findConverterFor(object + LegacyConverter converter = findConverterFor(object .getClass()); return converter.convertToPresentation(object, String.class, null); } @@ -325,27 +325,27 @@ public class DesignFormatter implements Serializable { * if it was not found. */ @SuppressWarnings("unchecked") - protected Converter findConverterFor( + protected LegacyConverter findConverterFor( Class sourceType, boolean strict) { if (sourceType == Object.class) { // Use for propertyIds, itemIds and such. Only string type objects // are really supported if no special logic is implemented in the // component. - return (Converter) stringObjectConverter; + return (LegacyConverter) stringObjectConverter; } if (converterMap.containsKey(sourceType)) { - return ((Converter) converterMap.get(sourceType)); + return ((LegacyConverter) converterMap.get(sourceType)); } else if (!strict) { for (Class supported : converterMap.keySet()) { if (supported.isAssignableFrom(sourceType)) { - return ((Converter) converterMap.get(supported)); + return ((LegacyConverter) converterMap.get(supported)); } } } if (sourceType.isEnum()) { - return (Converter) stringEnumConverter; + return (LegacyConverter) stringEnumConverter; } return null; } @@ -359,7 +359,7 @@ public class DesignFormatter implements Serializable { * @return A valid converter for a given type or its subtype, null if * it was not found. */ - protected Converter findConverterFor( + protected LegacyConverter findConverterFor( Class sourceType) { return findConverterFor(sourceType, false); } 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 d2d63ad16e..5fc7c239e1 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 @@ -20,7 +20,7 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; -import com.vaadin.data.util.converter.Converter; +import com.vaadin.legacy.data.util.converter.LegacyConverter; import com.vaadin.ui.declarative.DesignAttributeHandler; /** @@ -30,11 +30,11 @@ import com.vaadin.ui.declarative.DesignAttributeHandler; * @since 7.4 * @author Vaadin Ltd */ -public class DesignDateConverter implements Converter { +public class DesignDateConverter implements LegacyConverter { @Override public Date convertToModel(String value, Class targetType, - Locale locale) throws Converter.ConversionException { + Locale locale) throws LegacyConverter.ConversionException { 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" }) { @@ -50,7 +50,7 @@ public class DesignDateConverter implements Converter { @Override public String convertToPresentation(Date value, Class targetType, Locale locale) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { 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 9f07ff9560..1f14c0571d 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 @@ -17,7 +17,7 @@ package com.vaadin.ui.declarative.converters; import java.util.Locale; -import com.vaadin.data.util.converter.Converter; +import com.vaadin.legacy.data.util.converter.LegacyConverter; import com.vaadin.ui.declarative.DesignAttributeHandler; /** @@ -27,12 +27,12 @@ import com.vaadin.ui.declarative.DesignAttributeHandler; * @since 7.4 * @author Vaadin Ltd */ -public class DesignEnumConverter implements Converter { +public class DesignEnumConverter implements LegacyConverter { @Override public Enum convertToModel(String value, Class targetType, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { + throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException { if (value == null || value.trim().equals("")) { return null; } @@ -42,7 +42,7 @@ public class DesignEnumConverter implements Converter { @Override public String convertToPresentation(Enum value, Class targetType, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { + throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException { if (value == null) { return null; } 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 f11585d6b8..cb950aa821 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 @@ -17,7 +17,7 @@ package com.vaadin.ui.declarative.converters; import java.util.Locale; -import com.vaadin.data.util.converter.Converter; +import com.vaadin.legacy.data.util.converter.LegacyConverter; import com.vaadin.ui.declarative.DesignAttributeHandler; /** @@ -27,19 +27,19 @@ import com.vaadin.ui.declarative.DesignAttributeHandler; * @since 7.4 * @author Vaadin Ltd */ -public class DesignObjectConverter implements Converter { +public class DesignObjectConverter implements LegacyConverter { @Override public Object convertToModel(String value, Class targetType, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { + throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException { return value; } @Override public String convertToPresentation(Object value, Class targetType, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { + throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException { 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 ffa51fedd1..57a1682261 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 @@ -21,7 +21,7 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; -import com.vaadin.data.util.converter.Converter; +import com.vaadin.legacy.data.util.converter.LegacyConverter; import com.vaadin.server.ExternalResource; import com.vaadin.server.FileResource; import com.vaadin.server.FontAwesome; @@ -40,12 +40,12 @@ import com.vaadin.ui.declarative.DesignAttributeHandler; * @author Vaadin Ltd */ @SuppressWarnings("serial") -public class DesignResourceConverter implements Converter { +public class DesignResourceConverter implements LegacyConverter { @Override public Resource convertToModel(String value, Class targetType, Locale locale) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { 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 @@ -67,13 +67,13 @@ public class DesignResourceConverter implements Converter { @Override public String convertToPresentation(Resource value, Class targetType, Locale locale) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { ResourceConverterByProtocol byType = ResourceConverterByProtocol .byType(value.getClass()); if (byType != null) { return byType.format(value); } else { - throw new Converter.ConversionException("unknown Resource type - " + throw new LegacyConverter.ConversionException("unknown Resource type - " + value.getClass().getName()); } } @@ -108,7 +108,7 @@ public class DesignResourceConverter implements Converter { @Override public String format(Resource value) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { return new ResourceReference(value, null, null).getURL(); } }, @@ -137,7 +137,7 @@ public class DesignResourceConverter implements Converter { @Override public String format(Resource value) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { FontIcon icon = (FontIcon) value; return new ResourceReference(icon, null, null).getURL(); @@ -161,7 +161,7 @@ public class DesignResourceConverter implements Converter { @Override public String format(Resource value) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { throw new UnsupportedOperationException("Use " + ResourceConverterByProtocol.FONTICON.toString() + " instead"); @@ -175,7 +175,7 @@ public class DesignResourceConverter implements Converter { @Override public String format(Resource value) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { String path = ((FileResource) value).getSourceFile().getPath(); if (File.separatorChar != '/') { // make sure we use '/' as file separator in templates @@ -195,7 +195,7 @@ public class DesignResourceConverter implements Converter { @Override public String format(Resource value) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { // default behavior for HTTP, HTTPS, FTP and FTPS return ((ExternalResource) value).getURL(); } 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 d6f2f65938..e310222d46 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 @@ -21,10 +21,10 @@ import java.util.Locale; import java.util.Map; import java.util.Map.Entry; -import com.vaadin.data.util.converter.Converter; import com.vaadin.event.ShortcutAction; import com.vaadin.event.ShortcutAction.KeyCode; import com.vaadin.event.ShortcutAction.ModifierKey; +import com.vaadin.legacy.data.util.converter.LegacyConverter; /** * Converter for {@link ShortcutActions}. @@ -33,7 +33,7 @@ import com.vaadin.event.ShortcutAction.ModifierKey; * @author Vaadin Ltd */ public class DesignShortcutActionConverter implements - Converter { + LegacyConverter { private final Map keyCodeMap; private final Map presentationMap; @@ -122,7 +122,7 @@ public class DesignShortcutActionConverter implements @Override public ShortcutAction convertToModel(String value, Class targetType, Locale locale) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { if (value.length() == 0) { return null; } @@ -162,7 +162,7 @@ public class DesignShortcutActionConverter implements @Override public String convertToPresentation(ShortcutAction value, Class targetType, Locale locale) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { 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 51f47edac9..a15231b10f 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 @@ -18,7 +18,7 @@ package com.vaadin.ui.declarative.converters; import java.util.Locale; import java.util.TimeZone; -import com.vaadin.data.util.converter.Converter; +import com.vaadin.legacy.data.util.converter.LegacyConverter; import com.vaadin.ui.declarative.DesignAttributeHandler; /** @@ -28,12 +28,12 @@ import com.vaadin.ui.declarative.DesignAttributeHandler; * @since 7.4 * @author Vaadin Ltd */ -public class DesignTimeZoneConverter implements Converter { +public class DesignTimeZoneConverter implements LegacyConverter { @Override public TimeZone convertToModel(String value, Class targetTimeZone, Locale locale) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { if (value == null || value.isEmpty()) { return null; } @@ -44,7 +44,7 @@ public class DesignTimeZoneConverter implements Converter { @Override public String convertToPresentation(TimeZone value, Class targetTimeZone, Locale locale) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { 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 0c6cf55bed..0af6905dc8 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 @@ -18,7 +18,7 @@ package com.vaadin.ui.declarative.converters; import java.lang.reflect.InvocationTargetException; import java.util.Locale; -import com.vaadin.data.util.converter.Converter; +import com.vaadin.legacy.data.util.converter.LegacyConverter; import com.vaadin.ui.declarative.DesignAttributeHandler; /** @@ -30,7 +30,7 @@ import com.vaadin.ui.declarative.DesignAttributeHandler; * @param * Type of the data being converted. */ -public class DesignToStringConverter implements Converter { +public class DesignToStringConverter implements LegacyConverter { private final Class type; @@ -72,30 +72,30 @@ public class DesignToStringConverter implements Converter { @Override public TYPE convertToModel(String value, Class targetType, - Locale locale) throws Converter.ConversionException { + Locale locale) throws LegacyConverter.ConversionException { try { return type.cast(type .getMethod(this.staticMethodName, String.class).invoke( null, value)); } catch (IllegalAccessException e) { - throw new Converter.ConversionException(e); + throw new LegacyConverter.ConversionException(e); } catch (IllegalArgumentException e) { - throw new Converter.ConversionException(e); + throw new LegacyConverter.ConversionException(e); } catch (InvocationTargetException e) { - throw new Converter.ConversionException(e.getCause()); + throw new LegacyConverter.ConversionException(e.getCause()); } catch (NoSuchMethodException e) { - throw new Converter.ConversionException(e); + throw new LegacyConverter.ConversionException(e); } catch (SecurityException e) { - throw new Converter.ConversionException(e); + throw new LegacyConverter.ConversionException(e); } catch (RuntimeException e) { - throw new Converter.ConversionException(e); + throw new LegacyConverter.ConversionException(e); } } @Override public String convertToPresentation(TYPE value, Class targetType, Locale locale) - throws Converter.ConversionException { + throws LegacyConverter.ConversionException { if (value == null) { return NULL_VALUE_REPRESENTATION; } else { -- cgit v1.2.3