From: Artur Signell Date: Tue, 19 Jun 2012 12:35:04 +0000 (+0300) Subject: Split generic converter methods from AbstractField (#8991) X-Git-Tag: 7.0.0.alpha3~87 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a406fd2c798b4e13197d5a8be0752497e8b3c2f4;p=vaadin-framework.git Split generic converter methods from AbstractField (#8991) --- diff --git a/src/com/vaadin/data/util/converter/ConverterUtil.java b/src/com/vaadin/data/util/converter/ConverterUtil.java new file mode 100644 index 0000000000..8be8ae799e --- /dev/null +++ b/src/com/vaadin/data/util/converter/ConverterUtil.java @@ -0,0 +1,113 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ +package com.vaadin.data.util.converter; + +import java.util.Locale; + +import com.vaadin.Application; + +public class ConverterUtil { + + public static Converter getConverter( + Class uiType, Class modelType) { + Converter converter = null; + + Application app = Application.getCurrentApplication(); + if (app != null) { + ConverterFactory factory = app.getConverterFactory(); + converter = factory.createConverter(uiType, 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 (try to) use + * @param locale + * The locale to use for conversion + * @param + * Presentation 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 is no converter and the type is not compatible with + * the model type. + */ + @SuppressWarnings("unchecked") + public static PRESENTATIONTYPE convertFromModel( + MODELTYPE modelValue, + Class presentationType, + Converter converter, Locale locale) + throws Converter.ConversionException { + if (converter != null) { + return converter.convertToPresentation(modelValue, locale); + } + if (modelValue == 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."); + } + } + + /** + * @param + * @param + * @param presentationValue + * @param modelType + * @param converter + * @param locale + * @return + * @throws Converter.ConversionException + */ + 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. + */ + return converter.convertToModel(presentationValue, locale); + } + + if (presentationValue == null) { + // Null should always be passed through the converter but if there + // is no converter we can safely return null + return null; + } + + // check that the value class is compatible with the model type + if (modelType.isAssignableFrom(presentationValue.getClass())) { + 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."); + } + + } + +} diff --git a/src/com/vaadin/ui/AbstractField.java b/src/com/vaadin/ui/AbstractField.java index 4efed11e2c..3c78c21b53 100644 --- a/src/com/vaadin/ui/AbstractField.java +++ b/src/com/vaadin/ui/AbstractField.java @@ -14,14 +14,14 @@ import java.util.LinkedList; import java.util.List; import java.util.logging.Logger; -import com.vaadin.Application; import com.vaadin.data.Buffered; import com.vaadin.data.Property; import com.vaadin.data.Validatable; import com.vaadin.data.Validator; import com.vaadin.data.Validator.InvalidValueException; import com.vaadin.data.util.converter.Converter; -import com.vaadin.data.util.converter.ConverterFactory; +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; @@ -760,15 +760,9 @@ public abstract class AbstractField extends AbstractComponent implements * from */ public void setConverter(Class datamodelType) { - Converter converter = null; - - Application app = Application.getCurrentApplication(); - if (app != null) { - ConverterFactory factory = app.getConverterFactory(); - converter = (Converter) factory.createConverter(getType(), - datamodelType); - } - setConverter(converter); + Converter c = (Converter) ConverterUtil.getConverter( + getType(), datamodelType); + setConverter(c); } /** @@ -782,26 +776,9 @@ public abstract class AbstractField extends AbstractComponent implements * if there is no converter and the type is not compatible with * the data source type. */ - @SuppressWarnings("unchecked") - private T convertFromDataSource(Object newValue) - throws Converter.ConversionException { - if (converter != null) { - return converter.convertToPresentation(newValue, getLocale()); - } - if (newValue == null) { - return null; - } - - if (getType().isAssignableFrom(newValue.getClass())) { - return (T) newValue; - } else { - throw new Converter.ConversionException( - "Unable to convert value of type " - + newValue.getClass().getName() - + " to " - + getType() - + ". No converter is set and the types are not compatible."); - } + private T convertFromDataSource(Object newValue) { + return ConverterUtil.convertFromModel(newValue, getType(), + getConverter(), getLocale()); } /** @@ -817,38 +794,17 @@ public abstract class AbstractField extends AbstractComponent implements */ private Object convertToDataSource(T fieldValue) throws Converter.ConversionException { - if (converter != null) { - /* - * If there is a converter, always use it. It must convert or throw - * an exception. - */ - try { - return converter.convertToModel(fieldValue, getLocale()); - } catch (com.vaadin.data.util.converter.Converter.ConversionException e) { - throw new Converter.ConversionException( - getConversionError(converter.getModelType()), e); + try { + Class modelType = null; + Property pd = getPropertyDataSource(); + if (pd != null) { + modelType = pd.getType(); } - } - - if (fieldValue == null) { - // Null should always be passed through the converter but if there - // is no converter we can safely return null - return null; - } - - // check that the value class is compatible with the data source type - // (if data source set) or field type - Class type; - if (getPropertyDataSource() != null) { - type = getPropertyDataSource().getType(); - } else { - type = getType(); - } - - if (type.isAssignableFrom(fieldValue.getClass())) { - return fieldValue; - } else { - throw new Converter.ConversionException(getConversionError(type)); + return ConverterUtil.convertToModel(fieldValue, + (Class) modelType, getConverter(), getLocale()); + } catch (ConversionException e) { + throw new ConversionException( + getConversionError(converter.getModelType()), e); } }