From: Artur Signell Date: Wed, 21 Dec 2011 17:35:25 +0000 (+0200) Subject: #8101 Swapped generics parameter order for Converter based on API review X-Git-Tag: 7.0.0.alpha1~44 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5d09873ca96c8e61681299f79194b3e52325e4d5;p=vaadin-framework.git #8101 Swapped generics parameter order for Converter based on API review meeting --- diff --git a/src/com/vaadin/data/util/converter/BooleanToStringConverter.java b/src/com/vaadin/data/util/converter/BooleanToStringConverter.java deleted file mode 100644 index 9c6add7c99..0000000000 --- a/src/com/vaadin/data/util/converter/BooleanToStringConverter.java +++ /dev/null @@ -1,94 +0,0 @@ -/* -@VaadinApache2LicenseForJavaFiles@ - */ - -package com.vaadin.data.util.converter; - -import java.util.Locale; - -/** - * A converter that converts from {@link Boolean} to {@link String} and back. - * The String representation is given by Boolean.toString(). - *

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

- * - * @author Vaadin Ltd - * @version - * @VERSION@ - * @since 7.0 - */ -public class BooleanToStringConverter implements Converter { - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertFromTargetToSource(java - * .lang.Object, java.util.Locale) - */ - public Boolean convertToModel(String value, Locale locale) - throws ConversionException { - if (value == null) { - 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 Boolean"); - } - } - - protected String getTrueString() { - return Boolean.TRUE.toString(); - } - - protected String getFalseString() { - return Boolean.FALSE.toString(); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertFromSourceToTarget(java - * .lang.Object, java.util.Locale) - */ - public String convertToPresentation(Boolean value, Locale locale) - throws ConversionException { - if (value == null) { - return null; - } - if (value) { - return getTrueString(); - } else { - return getFalseString(); - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getSourceType() - */ - public Class getModelType() { - return Boolean.class; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getTargetType() - */ - public Class getPresentationType() { - return String.class; - } - -} diff --git a/src/com/vaadin/data/util/converter/Converter.java b/src/com/vaadin/data/util/converter/Converter.java index 2a2c3025d9..f48246ff4c 100644 --- a/src/com/vaadin/data/util/converter/Converter.java +++ b/src/com/vaadin/data/util/converter/Converter.java @@ -38,7 +38,7 @@ import java.util.Locale; * @VERSION@ * @since 7.0 */ -public interface Converter extends Serializable { +public interface Converter extends Serializable { /** * Converts the given value from target type to source type. diff --git a/src/com/vaadin/data/util/converter/ConverterFactory.java b/src/com/vaadin/data/util/converter/ConverterFactory.java index 1770bf85ac..451f84185d 100644 --- a/src/com/vaadin/data/util/converter/ConverterFactory.java +++ b/src/com/vaadin/data/util/converter/ConverterFactory.java @@ -7,7 +7,8 @@ package com.vaadin.data.util.converter; import java.io.Serializable; /** - * Factory interface for providing Converters based on a source and target type. + * Factory interface for providing Converters based on a presentation type and a + * model type. * * @author Vaadin Ltd. * @version @@ -16,7 +17,7 @@ import java.io.Serializable; * */ public interface ConverterFactory extends Serializable { - Converter createConverter( - Class sourceType, Class targetType); + public Converter createConverter( + Class presentationType, Class modelType); } diff --git a/src/com/vaadin/data/util/converter/DateToLongConverter.java b/src/com/vaadin/data/util/converter/DateToLongConverter.java new file mode 100644 index 0000000000..c9a76ec39b --- /dev/null +++ b/src/com/vaadin/data/util/converter/DateToLongConverter.java @@ -0,0 +1,68 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +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 + * @version + * @VERSION@ + * @since 7.0 + */ +public class DateToLongConverter implements Converter { + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertFromTargetToSource(java + * .lang.Object, java.util.Locale) + */ + public Long convertToModel(Date value, Locale locale) { + if (value == null) { + return null; + } + + return value.getTime(); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertFromSourceToTarget(java + * .lang.Object, java.util.Locale) + */ + public Date convertToPresentation(Long value, Locale locale) { + if (value == null) { + return null; + } + + return new Date(value); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getSourceType() + */ + public Class getModelType() { + return Long.class; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getTargetType() + */ + public Class getPresentationType() { + return Date.class; + } + +} diff --git a/src/com/vaadin/data/util/converter/DateToStringConverter.java b/src/com/vaadin/data/util/converter/DateToStringConverter.java deleted file mode 100644 index 947a426db6..0000000000 --- a/src/com/vaadin/data/util/converter/DateToStringConverter.java +++ /dev/null @@ -1,109 +0,0 @@ -/* -@VaadinApache2LicenseForJavaFiles@ - */ - -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 - * @version - * @VERSION@ - * @since 7.0 - */ -public class DateToStringConverter implements Converter { - - /** - * Returns the format used by - * {@link #convertToPresentation(Date, Locale)} and - * {@link #convertToModel(String, 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#convertFromTargetToSource(java - * .lang.Object, java.util.Locale) - */ - public Date convertToModel(String value, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { - 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 " + getPresentationType().getName()); - } - - return parsedValue; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertFromSourceToTarget(java - * .lang.Object, java.util.Locale) - */ - public String convertToPresentation(Date value, 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#getSourceType() - */ - public Class getModelType() { - return Date.class; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getTargetType() - */ - public Class getPresentationType() { - return String.class; - } - -} diff --git a/src/com/vaadin/data/util/converter/DefaultConverterFactory.java b/src/com/vaadin/data/util/converter/DefaultConverterFactory.java index 073eb10d55..9233624819 100644 --- a/src/com/vaadin/data/util/converter/DefaultConverterFactory.java +++ b/src/com/vaadin/data/util/converter/DefaultConverterFactory.java @@ -27,10 +27,10 @@ public class DefaultConverterFactory implements ConverterFactory { private final static Logger log = Logger .getLogger(DefaultConverterFactory.class.getName()); - public Converter createConverter( - Class sourceType, Class targetType) { - Converter converter = findConverter(sourceType, - targetType); + public Converter createConverter( + Class presentationType, Class modelType) { + Converter converter = findConverter( + presentationType, modelType); if (converter != null) { log.finest(getClass().getName() + " created a " + converter.getClass()); @@ -38,32 +38,32 @@ public class DefaultConverterFactory implements ConverterFactory { } // Try to find a reverse converter - Converter reverseConverter = findConverter(targetType, - sourceType); + Converter reverseConverter = findConverter( + modelType, presentationType); if (reverseConverter != null) { log.finest(getClass().getName() + " created a reverse " + reverseConverter.getClass()); - return new ReverseConverter(reverseConverter); + return new ReverseConverter(reverseConverter); } log.finest(getClass().getName() + " could not find a converter for " - + sourceType.getName() + " to " + targetType.getName() + + presentationType.getName() + " to " + modelType.getName() + " conversion"); return null; } - protected Converter findConverter( - Class sourceType, Class targetType) { - if (targetType == String.class) { + protected Converter findConverter( + Class presentationType, Class modelType) { + if (presentationType == String.class) { // TextField converters and more - Converter converter = (Converter) createStringConverter(sourceType); + Converter converter = (Converter) createStringConverter(modelType); if (converter != null) { return converter; } - } else if (targetType == Date.class) { + } else if (presentationType == Date.class) { // DateField converters and more - Converter converter = (Converter) createDateConverter(sourceType); + Converter converter = (Converter) createDateConverter(modelType); if (converter != null) { return converter; } @@ -73,25 +73,25 @@ public class DefaultConverterFactory implements ConverterFactory { } - protected Converter createDateConverter(Class sourceType) { + protected Converter createDateConverter(Class sourceType) { if (Long.class.isAssignableFrom(sourceType)) { - return new LongToDateConverter(); + return new DateToLongConverter(); } else { return null; } } - protected Converter createStringConverter(Class sourceType) { + protected Converter createStringConverter(Class sourceType) { if (Double.class.isAssignableFrom(sourceType)) { - return new DoubleToStringConverter(); + return new StringToDoubleConverter(); } else if (Integer.class.isAssignableFrom(sourceType)) { - return new IntegerToStringConverter(); + return new StringToIntegerConverter(); } else if (Boolean.class.isAssignableFrom(sourceType)) { - return new BooleanToStringConverter(); + return new StringToBooleanConverter(); } else if (Number.class.isAssignableFrom(sourceType)) { - return new NumberToStringConverter(); + return new StringToNumberConverter(); } else if (Date.class.isAssignableFrom(sourceType)) { - return new DateToStringConverter(); + return new StringToDateConverter(); } else { return null; } diff --git a/src/com/vaadin/data/util/converter/DoubleToStringConverter.java b/src/com/vaadin/data/util/converter/DoubleToStringConverter.java deleted file mode 100644 index a6bebaddd3..0000000000 --- a/src/com/vaadin/data/util/converter/DoubleToStringConverter.java +++ /dev/null @@ -1,104 +0,0 @@ -/* -@VaadinApache2LicenseForJavaFiles@ - */ - -package com.vaadin.data.util.converter; - -import java.text.NumberFormat; -import java.text.ParsePosition; -import java.util.Locale; - -/** - * A converter that converts from {@link Double} to {@link String} 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 - * @version - * @VERSION@ - * @since 7.0 - */ -public class DoubleToStringConverter implements Converter { - - /** - * Returns the format used by - * {@link #convertToPresentation(Double, Locale)} and - * {@link #convertToModel(String, Locale)}. - * - * @param locale - * The locale to use - * @return A NumberFormat instance - */ - protected NumberFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - - return NumberFormat.getNumberInstance(locale); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertFromTargetToSource(java - * .lang.Object, java.util.Locale) - */ - public Double convertToModel(String value, Locale locale) - throws ConversionException { - if (value == null) { - return null; - } - - // Remove leading and trailing white space - value = value.trim(); - - 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 " + getPresentationType().getName()); - } - return parsedValue.doubleValue(); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertFromSourceToTarget(java - * .lang.Object, java.util.Locale) - */ - public String convertToPresentation(Double value, Locale locale) - throws ConversionException { - if (value == null) { - return null; - } - - return getFormat(locale).format(value); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getSourceType() - */ - public Class getModelType() { - return Double.class; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getTargetType() - */ - public Class getPresentationType() { - return String.class; - } -} diff --git a/src/com/vaadin/data/util/converter/IntegerToStringConverter.java b/src/com/vaadin/data/util/converter/IntegerToStringConverter.java deleted file mode 100644 index 5c0d6761c0..0000000000 --- a/src/com/vaadin/data/util/converter/IntegerToStringConverter.java +++ /dev/null @@ -1,84 +0,0 @@ -/* -@VaadinApache2LicenseForJavaFiles@ - */ - -package com.vaadin.data.util.converter; - -import java.text.NumberFormat; -import java.text.ParsePosition; -import java.util.Locale; - -/** - * A converter that converts from {@link Integer} to {@link String} 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 - * @version - * @VERSION@ - * @since 7.0 - */ -public class IntegerToStringConverter implements Converter { - - /** - * Returns the format used by - * {@link #convertToPresentation(Integer, Locale)} and - * {@link #convertToModel(String, Locale)}. - * - * @param locale - * The locale to use - * @return A NumberFormat instance - */ - protected NumberFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - return NumberFormat.getIntegerInstance(locale); - } - - public Integer convertToModel(String value, 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.intValue(); - } - - public String convertToPresentation(Integer value, Locale locale) - throws ConversionException { - if (value == null) { - return null; - } - - return getFormat(locale).format(value); - } - - public Class getModelType() { - return Integer.class; - } - - public Class getPresentationType() { - return String.class; - } - -} diff --git a/src/com/vaadin/data/util/converter/LongToDateConverter.java b/src/com/vaadin/data/util/converter/LongToDateConverter.java deleted file mode 100644 index c22e8af16e..0000000000 --- a/src/com/vaadin/data/util/converter/LongToDateConverter.java +++ /dev/null @@ -1,68 +0,0 @@ -/* -@VaadinApache2LicenseForJavaFiles@ - */ - -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 - * @version - * @VERSION@ - * @since 7.0 - */ -public class LongToDateConverter implements Converter { - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertFromTargetToSource(java - * .lang.Object, java.util.Locale) - */ - public Long convertToModel(Date value, Locale locale) { - if (value == null) { - return null; - } - - return value.getTime(); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertFromSourceToTarget(java - * .lang.Object, java.util.Locale) - */ - public Date convertToPresentation(Long value, Locale locale) { - if (value == null) { - return null; - } - - return new Date(value); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getSourceType() - */ - public Class getModelType() { - return Long.class; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getTargetType() - */ - public Class getPresentationType() { - return Date.class; - } - -} diff --git a/src/com/vaadin/data/util/converter/NumberToStringConverter.java b/src/com/vaadin/data/util/converter/NumberToStringConverter.java deleted file mode 100644 index cd4c1363b7..0000000000 --- a/src/com/vaadin/data/util/converter/NumberToStringConverter.java +++ /dev/null @@ -1,108 +0,0 @@ -/* -@VaadinApache2LicenseForJavaFiles@ - */ - -package com.vaadin.data.util.converter; - -import java.text.NumberFormat; -import java.text.ParsePosition; -import java.util.Locale; - -/** - * A converter that converts from {@link Number} to {@link String} and back. - * Uses the given locale and {@link NumberFormat} for formatting and parsing. - *

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

- * - * @author Vaadin Ltd - * @version - * @VERSION@ - * @since 7.0 - */ -public class NumberToStringConverter implements Converter { - - /** - * Returns the format used by - * {@link #convertToPresentation(Number, Locale)} and - * {@link #convertToModel(String, Locale)}. - * - * @param locale - * The locale to use - * @return A NumberFormat instance - */ - protected NumberFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - - return NumberFormat.getNumberInstance(locale); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertFromTargetToSource(java - * .lang.Object, java.util.Locale) - */ - public Number convertToModel(String value, 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 " + getPresentationType().getName()); - } - - if (parsedValue == null) { - // Convert "" to null - return null; - } - return parsedValue; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertFromSourceToTarget(java - * .lang.Object, java.util.Locale) - */ - public String convertToPresentation(Number value, Locale locale) - throws ConversionException { - if (value == null) { - return null; - } - - return getFormat(locale).format(value); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getSourceType() - */ - public Class getModelType() { - return Number.class; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getTargetType() - */ - public Class getPresentationType() { - return String.class; - } - -} diff --git a/src/com/vaadin/data/util/converter/ReverseConverter.java b/src/com/vaadin/data/util/converter/ReverseConverter.java index c86ba72395..45f5abda65 100644 --- a/src/com/vaadin/data/util/converter/ReverseConverter.java +++ b/src/com/vaadin/data/util/converter/ReverseConverter.java @@ -20,10 +20,10 @@ import java.util.Locale; * @VERSION@ * @since 7.0 */ -public class ReverseConverter implements - Converter { +public class ReverseConverter implements + Converter { - private Converter realConverter; + private Converter realConverter; /** * Creates a converter from source to target based on a converter that @@ -32,7 +32,7 @@ public class ReverseConverter implements * @param converter * The converter to use in a reverse fashion */ - public ReverseConverter(Converter converter) { + public ReverseConverter(Converter converter) { this.realConverter = converter; } diff --git a/src/com/vaadin/data/util/converter/StringToBooleanConverter.java b/src/com/vaadin/data/util/converter/StringToBooleanConverter.java new file mode 100644 index 0000000000..5dfcf87e40 --- /dev/null +++ b/src/com/vaadin/data/util/converter/StringToBooleanConverter.java @@ -0,0 +1,94 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +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 Boolean.toString(). + *

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

+ * + * @author Vaadin Ltd + * @version + * @VERSION@ + * @since 7.0 + */ +public class StringToBooleanConverter implements Converter { + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertFromTargetToSource(java + * .lang.Object, java.util.Locale) + */ + public Boolean convertToModel(String value, Locale locale) + throws ConversionException { + if (value == null) { + 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 Boolean"); + } + } + + protected String getTrueString() { + return Boolean.TRUE.toString(); + } + + protected String getFalseString() { + return Boolean.FALSE.toString(); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertFromSourceToTarget(java + * .lang.Object, java.util.Locale) + */ + public String convertToPresentation(Boolean value, Locale locale) + throws ConversionException { + if (value == null) { + return null; + } + if (value) { + return getTrueString(); + } else { + return getFalseString(); + } + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getSourceType() + */ + public Class getModelType() { + return Boolean.class; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getTargetType() + */ + public Class getPresentationType() { + return String.class; + } + +} diff --git a/src/com/vaadin/data/util/converter/StringToDateConverter.java b/src/com/vaadin/data/util/converter/StringToDateConverter.java new file mode 100644 index 0000000000..c97090334d --- /dev/null +++ b/src/com/vaadin/data/util/converter/StringToDateConverter.java @@ -0,0 +1,108 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +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 + * @version + * @VERSION@ + * @since 7.0 + */ +public class StringToDateConverter implements Converter { + + /** + * Returns the format used by {@link #convertToPresentation(Date, Locale)} + * and {@link #convertToModel(String, 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#convertFromTargetToSource(java + * .lang.Object, java.util.Locale) + */ + public Date convertToModel(String value, Locale locale) + throws com.vaadin.data.util.converter.Converter.ConversionException { + 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 " + getPresentationType().getName()); + } + + return parsedValue; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertFromSourceToTarget(java + * .lang.Object, java.util.Locale) + */ + public String convertToPresentation(Date value, 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#getSourceType() + */ + public Class getModelType() { + return Date.class; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getTargetType() + */ + public Class getPresentationType() { + return String.class; + } + +} diff --git a/src/com/vaadin/data/util/converter/StringToDoubleConverter.java b/src/com/vaadin/data/util/converter/StringToDoubleConverter.java new file mode 100644 index 0000000000..0c60aea5cb --- /dev/null +++ b/src/com/vaadin/data/util/converter/StringToDoubleConverter.java @@ -0,0 +1,103 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.data.util.converter; + +import java.text.NumberFormat; +import java.text.ParsePosition; +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 + * @version + * @VERSION@ + * @since 7.0 + */ +public class StringToDoubleConverter implements Converter { + + /** + * Returns the format used by {@link #convertToPresentation(Double, Locale)} + * and {@link #convertToModel(String, Locale)}. + * + * @param locale + * The locale to use + * @return A NumberFormat instance + */ + protected NumberFormat getFormat(Locale locale) { + if (locale == null) { + locale = Locale.getDefault(); + } + + return NumberFormat.getNumberInstance(locale); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertFromTargetToSource(java + * .lang.Object, java.util.Locale) + */ + public Double convertToModel(String value, Locale locale) + throws ConversionException { + if (value == null) { + return null; + } + + // Remove leading and trailing white space + value = value.trim(); + + 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 " + getPresentationType().getName()); + } + return parsedValue.doubleValue(); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertFromSourceToTarget(java + * .lang.Object, java.util.Locale) + */ + public String convertToPresentation(Double value, Locale locale) + throws ConversionException { + if (value == null) { + return null; + } + + return getFormat(locale).format(value); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getSourceType() + */ + public Class getModelType() { + return Double.class; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getTargetType() + */ + public Class getPresentationType() { + return String.class; + } +} diff --git a/src/com/vaadin/data/util/converter/StringToIntegerConverter.java b/src/com/vaadin/data/util/converter/StringToIntegerConverter.java new file mode 100644 index 0000000000..7fa4458c51 --- /dev/null +++ b/src/com/vaadin/data/util/converter/StringToIntegerConverter.java @@ -0,0 +1,84 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.data.util.converter; + +import java.text.NumberFormat; +import java.text.ParsePosition; +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 + * @version + * @VERSION@ + * @since 7.0 + */ +public class StringToIntegerConverter implements Converter { + + /** + * Returns the format used by + * {@link #convertToPresentation(Integer, Locale)} and + * {@link #convertToModel(String, Locale)}. + * + * @param locale + * The locale to use + * @return A NumberFormat instance + */ + protected NumberFormat getFormat(Locale locale) { + if (locale == null) { + locale = Locale.getDefault(); + } + return NumberFormat.getIntegerInstance(locale); + } + + public Integer convertToModel(String value, 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.intValue(); + } + + public String convertToPresentation(Integer value, Locale locale) + throws ConversionException { + if (value == null) { + return null; + } + + return getFormat(locale).format(value); + } + + public Class getModelType() { + return Integer.class; + } + + public Class getPresentationType() { + return String.class; + } + +} diff --git a/src/com/vaadin/data/util/converter/StringToNumberConverter.java b/src/com/vaadin/data/util/converter/StringToNumberConverter.java new file mode 100644 index 0000000000..8975dad9f3 --- /dev/null +++ b/src/com/vaadin/data/util/converter/StringToNumberConverter.java @@ -0,0 +1,107 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.data.util.converter; + +import java.text.NumberFormat; +import java.text.ParsePosition; +import java.util.Locale; + +/** + * A converter that converts from {@link Number} to {@link String} and back. + * Uses the given locale and {@link NumberFormat} for formatting and parsing. + *

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

+ * + * @author Vaadin Ltd + * @version + * @VERSION@ + * @since 7.0 + */ +public class StringToNumberConverter implements Converter { + + /** + * Returns the format used by {@link #convertToPresentation(Number, Locale)} + * and {@link #convertToModel(String, Locale)}. + * + * @param locale + * The locale to use + * @return A NumberFormat instance + */ + protected NumberFormat getFormat(Locale locale) { + if (locale == null) { + locale = Locale.getDefault(); + } + + return NumberFormat.getNumberInstance(locale); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertFromTargetToSource(java + * .lang.Object, java.util.Locale) + */ + public Number convertToModel(String value, 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 " + getPresentationType().getName()); + } + + if (parsedValue == null) { + // Convert "" to null + return null; + } + return parsedValue; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.util.converter.Converter#convertFromSourceToTarget(java + * .lang.Object, java.util.Locale) + */ + public String convertToPresentation(Number value, Locale locale) + throws ConversionException { + if (value == null) { + return null; + } + + return getFormat(locale).format(value); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getSourceType() + */ + public Class getModelType() { + return Number.class; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.util.converter.Converter#getTargetType() + */ + public Class getPresentationType() { + return String.class; + } + +} diff --git a/src/com/vaadin/ui/AbstractField.java b/src/com/vaadin/ui/AbstractField.java index 9e4bdde064..cbdee0eae1 100644 --- a/src/com/vaadin/ui/AbstractField.java +++ b/src/com/vaadin/ui/AbstractField.java @@ -72,7 +72,7 @@ public abstract class AbstractField extends AbstractComponent implements * A converter used to convert from the data model type to the field type * and vice versa. */ - private Converter converter = null; + private Converter converter = null; /** * Connected data-source. */ @@ -787,13 +787,13 @@ public abstract class AbstractField extends AbstractComponent implements * from */ public void setConverter(Class datamodelType) { - Converter converter = null; + Converter converter = null; Application app = Application.getCurrentApplication(); if (app != null) { ConverterFactory factory = app.getConverterFactory(); - converter = (Converter) factory.createConverter( - datamodelType, getType()); + converter = (Converter) factory.createConverter(getType(), + datamodelType); } setConverter(converter); } @@ -850,8 +850,7 @@ public abstract class AbstractField extends AbstractComponent implements * an exception. */ try { - return converter.convertToModel(fieldValue, - getLocale()); + return converter.convertToModel(fieldValue, getLocale()); } catch (com.vaadin.data.util.converter.Converter.ConversionException e) { throw new Converter.ConversionException( getValueConversionError(converter.getModelType()), e); @@ -1030,8 +1029,8 @@ public abstract class AbstractField extends AbstractComponent implements // to validate the converted value if (getConverter() != null) { try { - valueToValidate = getConverter().convertToModel( - fieldValue, getLocale()); + valueToValidate = getConverter().convertToModel(fieldValue, + getLocale()); } catch (Exception e) { throw new InvalidValueException( getValueConversionError(getConverter().getModelType())); @@ -1586,23 +1585,20 @@ public abstract class AbstractField extends AbstractComponent implements * * @return The converter or null if none is set. */ - public Converter getConverter() { + public Converter getConverter() { return converter; } /** - * Sets the converter used to convert the property data source value to the - * field value. The converter must have a target type that matches the field - * type. - * - * The source for the converter is the data model and the target is the - * field. + * Sets the converter used to convert the field value to property data + * source type. The converter must have a presentation type that matches the + * field type. * * @param converter * The new converter to use. */ - public void setConverter(Converter converter) { - this.converter = (Converter) converter; + public void setConverter(Converter converter) { + this.converter = (Converter) converter; requestRepaint(); } diff --git a/src/com/vaadin/ui/Table.java b/src/com/vaadin/ui/Table.java index 5ab0fb9029..8b9dc3ef81 100644 --- a/src/com/vaadin/ui/Table.java +++ b/src/com/vaadin/ui/Table.java @@ -410,7 +410,7 @@ public class Table extends AbstractSelect implements Action.Container, private boolean painted = false; - private HashMap propertyValueConverters = new HashMap(); + private HashMap> propertyValueConverters = new HashMap>(); /* Table constructors */ @@ -3462,16 +3462,16 @@ public class Table extends AbstractSelect implements Action.Container, if (property == null) { return ""; } - Converter converter = null; + Converter converter = null; if (hasConverter(colId)) { converter = getConverter(colId); } else { Application app = Application.getCurrentApplication(); if (app != null) { - converter = (Converter) app - .getConverterFactory().createConverter( - property.getType(), String.class); + converter = (Converter) app + .getConverterFactory().createConverter(String.class, + property.getType()); } } Object value = property.getValue(); @@ -5143,7 +5143,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, Converter converter) { if (!getContainerPropertyIds().contains(propertyId)) { throw new IllegalArgumentException("PropertyId " + propertyId + " must be in the container"); @@ -5158,7 +5158,8 @@ public class Table extends AbstractSelect implements Action.Container, // + ") must match converter source type (" // + converter.getSourceType() + ")"); // } - propertyValueConverters.put(propertyId, converter); + propertyValueConverters.put(propertyId, + (Converter) converter); refreshRowCache(); } @@ -5182,7 +5183,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 Converter getConverter(Object propertyId) { return propertyValueConverters.get(propertyId); } diff --git a/tests/server-side/com/vaadin/tests/server/component/abstractfield/AbstractFieldValueConversions.java b/tests/server-side/com/vaadin/tests/server/component/abstractfield/AbstractFieldValueConversions.java index 9de7425423..753afbdd06 100644 --- a/tests/server-side/com/vaadin/tests/server/component/abstractfield/AbstractFieldValueConversions.java +++ b/tests/server-side/com/vaadin/tests/server/component/abstractfield/AbstractFieldValueConversions.java @@ -6,7 +6,7 @@ import junit.framework.TestCase; import com.vaadin.data.util.MethodProperty; import com.vaadin.data.util.converter.Converter; -import com.vaadin.data.util.converter.IntegerToStringConverter; +import com.vaadin.data.util.converter.StringToIntegerConverter; import com.vaadin.tests.data.bean.Address; import com.vaadin.tests.data.bean.Country; import com.vaadin.tests.data.bean.Person; @@ -64,7 +64,7 @@ public class AbstractFieldValueConversions extends TestCase { public void testFailingConversion() { TextField tf = new TextField(); - tf.setConverter(new Converter() { + tf.setConverter(new Converter() { public Integer convertToModel(String value, Locale locale) { throw new ConversionException("Failed"); @@ -95,7 +95,7 @@ public class AbstractFieldValueConversions extends TestCase { public void testIntegerStringConversion() { TextField tf = new TextField(); - tf.setConverter(new IntegerToStringConverter()); + tf.setConverter(new StringToIntegerConverter()); tf.setPropertyDataSource(new MethodProperty(paulaBean, "age")); assertEquals(34, tf.getPropertyDataSource().getValue()); assertEquals("34", tf.getValue()); @@ -111,16 +111,14 @@ public class AbstractFieldValueConversions extends TestCase { CheckBox cb = new CheckBox(); cb.setConverter(new Converter() { - public Boolean convertToModel(Boolean value, - Locale locale) { + public Boolean convertToModel(Boolean value, Locale locale) { // value from a CheckBox should never be null as long as it is // not set to null (handled by conversion below). assertNotNull(value); return value; } - public Boolean convertToPresentation(Boolean value, - Locale locale) { + public Boolean convertToPresentation(Boolean value, Locale locale) { // Datamodel -> field if (value == null) { return false; diff --git a/tests/testbench/com/vaadin/tests/components/abstractfield/Vaadin6ImplicitDoubleConverter.java b/tests/testbench/com/vaadin/tests/components/abstractfield/Vaadin6ImplicitDoubleConverter.java index 9ef42cf4b0..274ac64b44 100644 --- a/tests/testbench/com/vaadin/tests/components/abstractfield/Vaadin6ImplicitDoubleConverter.java +++ b/tests/testbench/com/vaadin/tests/components/abstractfield/Vaadin6ImplicitDoubleConverter.java @@ -5,7 +5,7 @@ import java.util.Locale; import com.vaadin.data.util.converter.Converter; public class Vaadin6ImplicitDoubleConverter implements - Converter { + Converter { public Double convertToModel(String value, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { diff --git a/tests/testbench/com/vaadin/tests/components/table/DoublesInTable.java b/tests/testbench/com/vaadin/tests/components/table/DoublesInTable.java index e92c1ab17a..2380b12933 100644 --- a/tests/testbench/com/vaadin/tests/components/table/DoublesInTable.java +++ b/tests/testbench/com/vaadin/tests/components/table/DoublesInTable.java @@ -9,7 +9,7 @@ import com.vaadin.data.Property.ValueChangeEvent; import com.vaadin.data.Property.ValueChangeListener; import com.vaadin.data.util.BeanItemContainer; import com.vaadin.data.util.converter.Converter; -import com.vaadin.data.util.converter.NumberToStringConverter; +import com.vaadin.data.util.converter.StringToNumberConverter; import com.vaadin.tests.components.TestBase; import com.vaadin.tests.data.bean.Address; import com.vaadin.tests.data.bean.Country; @@ -143,12 +143,11 @@ public class DoublesInTable extends TestBase { } private void addConverters(Table t) { - t.setConverter("sex", new Converter() { + t.setConverter("sex", new Converter() { public Sex convertToModel(String value, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { - // not used in this test - Table only converts from source to - // target + // not used in this test - Table only converts to presentation return null; } @@ -168,7 +167,7 @@ public class DoublesInTable extends TestBase { return String.class; } }); - t.setConverter("deceased", new Converter() { + t.setConverter("deceased", new Converter() { public Boolean convertToModel(String value, Locale locale) { // not used in this test - Table only converts from source to @@ -192,7 +191,7 @@ public class DoublesInTable extends TestBase { return String.class; } }); - t.setConverter("age", new Converter() { + t.setConverter("age", new Converter() { public Integer convertToModel(String value, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { @@ -225,12 +224,11 @@ public class DoublesInTable extends TestBase { return String.class; } }); - t.setConverter("address", new Converter() { + t.setConverter("address", new Converter() { public Address convertToModel(String value, Locale locale) throws ConversionException { - // not used in this test - Table only converts from source to - // target + // not used in this test - Table only converts to presentation return null; } @@ -250,7 +248,7 @@ public class DoublesInTable extends TestBase { }); - t.setConverter("rent", new NumberToStringConverter() { + t.setConverter("rent", new StringToNumberConverter() { @Override protected NumberFormat getFormat(Locale locale) { return NumberFormat.getCurrencyInstance(locale); diff --git a/tests/testbench/com/vaadin/tests/fieldbinder/BasicPersonForm.java b/tests/testbench/com/vaadin/tests/fieldbinder/BasicPersonForm.java index 1e7d90ec18..de7193d849 100644 --- a/tests/testbench/com/vaadin/tests/fieldbinder/BasicPersonForm.java +++ b/tests/testbench/com/vaadin/tests/fieldbinder/BasicPersonForm.java @@ -7,7 +7,7 @@ import com.vaadin.data.fieldbinder.FieldBinder.CommitException; import com.vaadin.data.fieldbinder.FieldBinder.CommitHandler; import com.vaadin.data.fieldbinder.FormBuilder; import com.vaadin.data.util.BeanItem; -import com.vaadin.data.util.converter.BooleanToStringConverter; +import com.vaadin.data.util.converter.StringToBooleanConverter; import com.vaadin.data.validator.EmailValidator; import com.vaadin.data.validator.IntegerRangeValidator; import com.vaadin.data.validator.StringLengthValidator; @@ -160,7 +160,7 @@ public class BasicPersonForm extends TestBase { age.addValidator(new IntegerRangeValidator( "Must be between 0 and 150, {0} is not", 0, 150)); sex.setPageLength(0); - deceased.setConverter(new BooleanToStringConverter() { + deceased.setConverter(new StringToBooleanConverter() { @Override protected String getTrueString() { return "YAY!";