From: Artur Signell Date: Fri, 25 Nov 2011 15:48:47 +0000 (+0200) Subject: First take on Converters X-Git-Tag: 7.0.0.alpha1~230^2~18 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3053cf8f876e4bac0bfce4e90ad7301ffc3f27b8;p=vaadin-framework.git First take on Converters --- diff --git a/src/com/vaadin/data/util/converter/BooleanToStringConverter.java b/src/com/vaadin/data/util/converter/BooleanToStringConverter.java new file mode 100644 index 0000000000..cdb7d9e9bc --- /dev/null +++ b/src/com/vaadin/data/util/converter/BooleanToStringConverter.java @@ -0,0 +1,32 @@ +package com.vaadin.data.util.converter; + +import java.util.Locale; + +public class BooleanToStringConverter implements Converter { + + public Boolean convertFromTargetToSource(String value, Locale locale) { + try { + return Boolean.valueOf(value); + } catch (Exception e) { + throw new ConversionException("Cannot convert " + value + + " to Boolean"); + } + } + + public String convertFromSourceToTarget(Boolean value, Locale locale) { + if (value == null) { + return ""; + } + + return value.toString(); + } + + public Class getSourceType() { + return Boolean.class; + } + + public Class getTargetType() { + return String.class; + } + +} diff --git a/src/com/vaadin/data/util/converter/Converter.java b/src/com/vaadin/data/util/converter/Converter.java new file mode 100644 index 0000000000..fc1db77703 --- /dev/null +++ b/src/com/vaadin/data/util/converter/Converter.java @@ -0,0 +1,72 @@ +package com.vaadin.data.util.converter; + +import java.io.Serializable; +import java.util.Locale; + +public interface Converter extends Serializable { + + public SOURCE convertFromTargetToSource(TARGET value, Locale locale) + throws ConversionException; + + public TARGET convertFromSourceToTarget(SOURCE value, Locale locale) + throws ConversionException; + + public Class getSourceType(); + + public Class getTargetType(); + + /** + * An exception that signals that the value passed to #convert or + * Converter.convertFromTargetToSource could not be converted. + * + * @author Vaadin Ltd + * @version + * @VERSION@ + * @since 7.0 + */ + public 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/src/com/vaadin/data/util/converter/ConverterFactory.java b/src/com/vaadin/data/util/converter/ConverterFactory.java new file mode 100644 index 0000000000..e36e8c0578 --- /dev/null +++ b/src/com/vaadin/data/util/converter/ConverterFactory.java @@ -0,0 +1,7 @@ +package com.vaadin.data.util.converter; + +public interface ConverterFactory { + Converter createConverter( + Class sourceType, Class targetType); + +} diff --git a/src/com/vaadin/data/util/converter/DateToStringConverter.java b/src/com/vaadin/data/util/converter/DateToStringConverter.java new file mode 100644 index 0000000000..b46fa75c75 --- /dev/null +++ b/src/com/vaadin/data/util/converter/DateToStringConverter.java @@ -0,0 +1,50 @@ +package com.vaadin.data.util.converter; + +import java.text.DateFormat; +import java.text.ParsePosition; +import java.util.Date; +import java.util.Locale; + +public class DateToStringConverter implements Converter { + + public Date convertFromTargetToSource(String value, Locale locale) + throws com.vaadin.data.util.converter.Converter.ConversionException { + if (value == null) { + return null; + } + + 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 " + getTargetType().getName()); + } + + return parsedValue; + } + + public String convertFromSourceToTarget(Date value, Locale locale) + throws com.vaadin.data.util.converter.Converter.ConversionException { + if (value == null) { + return null; + } + + return getFormat(locale).format(value); + } + + protected DateFormat getFormat(Locale locale) { + DateFormat f = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, + DateFormat.MEDIUM, locale); + f.setLenient(false); + return f; + } + + public Class getSourceType() { + return Date.class; + } + + public Class getTargetType() { + return String.class; + } + +} diff --git a/src/com/vaadin/data/util/converter/DefaultConverterFactory.java b/src/com/vaadin/data/util/converter/DefaultConverterFactory.java new file mode 100644 index 0000000000..8ddd35a2d8 --- /dev/null +++ b/src/com/vaadin/data/util/converter/DefaultConverterFactory.java @@ -0,0 +1,77 @@ +package com.vaadin.data.util.converter; + +import java.util.Date; + +public class DefaultConverterFactory implements ConverterFactory { + + public Converter createConverter( + Class sourceType, Class targetType) { + Converter converter = findConverter(sourceType, + targetType); + if (converter != null) { + System.out.println(getClass().getName() + " created a " + + converter.getClass()); + return converter; + } + + // Try to find a reverse converter + Converter reverseConverter = findConverter(targetType, + sourceType); + if (reverseConverter != null) { + System.out.println(getClass().getName() + " created a reverse " + + reverseConverter.getClass()); + return new ReverseConverter(reverseConverter); + } + + System.out.println(getClass().getName() + + " could not find a converter for " + sourceType.getName() + + " to " + targetType.getName() + " conversion"); + return null; + + } + + protected Converter findConverter( + Class sourceType, Class targetType) { + if (targetType == String.class) { + // TextField converters and more + Converter converter = (Converter) createStringConverter(sourceType); + if (converter != null) { + return converter; + } + } else if (targetType == Date.class) { + // DateField converters and more + Converter converter = (Converter) createDateConverter(sourceType); + if (converter != null) { + return converter; + } + } + + return null; + + } + + protected Converter createDateConverter(Class sourceType) { + if (Long.class.isAssignableFrom(sourceType)) { + return new LongToDateConverter(); + } else { + return null; + } + } + + protected Converter createStringConverter(Class sourceType) { + if (Double.class.isAssignableFrom(sourceType)) { + return new DoubleToStringConverter(); + } else if (Integer.class.isAssignableFrom(sourceType)) { + return new IntegerToStringConverter(); + } else if (Boolean.class.isAssignableFrom(sourceType)) { + return new BooleanToStringConverter(); + } else if (Number.class.isAssignableFrom(sourceType)) { + return new NumberToStringConverter(); + } else if (Date.class.isAssignableFrom(sourceType)) { + return new DateToStringConverter(); + } else { + return null; + } + } + +} diff --git a/src/com/vaadin/data/util/converter/DoubleToStringConverter.java b/src/com/vaadin/data/util/converter/DoubleToStringConverter.java new file mode 100644 index 0000000000..299ed79bcb --- /dev/null +++ b/src/com/vaadin/data/util/converter/DoubleToStringConverter.java @@ -0,0 +1,38 @@ +package com.vaadin.data.util.converter; + +import java.text.NumberFormat; +import java.text.ParsePosition; +import java.util.Locale; + +public class DoubleToStringConverter implements Converter { + + protected NumberFormat getFormatter(Locale locale) { + return NumberFormat.getNumberInstance(locale); + } + + public Double convertFromTargetToSource(String value, Locale locale) { + ParsePosition parsePosition = new ParsePosition(0); + Number parsedValue = getFormatter(locale).parse(value, parsePosition); + if (parsePosition.getIndex() != value.length()) { + throw new ConversionException("Could not convert '" + value + + "' to " + getTargetType().getName()); + } + return parsedValue.doubleValue(); + } + + public String convertFromSourceToTarget(Double value, Locale locale) { + if (value == null) { + return null; + } + + return getFormatter(locale).format(value); + } + + public Class getSourceType() { + return Double.class; + } + + public Class getTargetType() { + return String.class; + } +} diff --git a/src/com/vaadin/data/util/converter/FieldValueConverterFactory.java b/src/com/vaadin/data/util/converter/FieldValueConverterFactory.java new file mode 100644 index 0000000000..1dc6f42b2e --- /dev/null +++ b/src/com/vaadin/data/util/converter/FieldValueConverterFactory.java @@ -0,0 +1,14 @@ +package com.vaadin.data.util.converter; +//package com.vaadin.data; +// +//import java.io.Serializable; +// +//import com.vaadin.ui.Field; +// +//public interface FieldValueConverterFactory extends Serializable { +// +// Converter createConverter( +// Field field, Item item, Object propertyId, +// Class targetType); +// +// } diff --git a/src/com/vaadin/data/util/converter/IntegerToStringConverter.java b/src/com/vaadin/data/util/converter/IntegerToStringConverter.java new file mode 100644 index 0000000000..c6d48d55a8 --- /dev/null +++ b/src/com/vaadin/data/util/converter/IntegerToStringConverter.java @@ -0,0 +1,57 @@ +package com.vaadin.data.util.converter; + +import java.text.NumberFormat; +import java.text.ParsePosition; +import java.util.Locale; + +public class IntegerToStringConverter implements Converter { + + protected NumberFormat getFormatter(Locale locale) { + if (locale == null) { + return NumberFormat.getIntegerInstance(); + } else { + return NumberFormat.getIntegerInstance(locale); + } + } + + public Integer convertFromTargetToSource(String value, Locale locale) { + if (value == null) { + return null; + } + + // Remove extra spaces + 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 = getFormatter(locale).parse(value, parsePosition); + if (parsePosition.getIndex() != value.length()) { + throw new ConversionException("Could not convert '" + value + + "' to " + getTargetType().getName()); + } + + if (parsedValue == null) { + // Convert "" to null + return null; + } + return parsedValue.intValue(); + } + + public String convertFromSourceToTarget(Integer value, Locale locale) { + if (value == null) { + return null; + } + + return getFormatter(locale).format(value); + } + + public Class getSourceType() { + return Integer.class; + } + + public Class getTargetType() { + return String.class; + } + +} diff --git a/src/com/vaadin/data/util/converter/LongToDateConverter.java b/src/com/vaadin/data/util/converter/LongToDateConverter.java new file mode 100644 index 0000000000..666c532ae5 --- /dev/null +++ b/src/com/vaadin/data/util/converter/LongToDateConverter.java @@ -0,0 +1,32 @@ +package com.vaadin.data.util.converter; + +import java.util.Date; +import java.util.Locale; + +public class LongToDateConverter implements Converter { + + public Long convertFromTargetToSource(Date value, Locale locale) { + if (value == null) { + return null; + } + + return value.getTime(); + } + + public Date convertFromSourceToTarget(Long value, Locale locale) { + if (value == null) { + return null; + } + + return new Date(value); + } + + public Class getSourceType() { + return Long.class; + } + + public Class getTargetType() { + return Date.class; + } + +} diff --git a/src/com/vaadin/data/util/converter/NumberToStringConverter.java b/src/com/vaadin/data/util/converter/NumberToStringConverter.java new file mode 100644 index 0000000000..07c6af523d --- /dev/null +++ b/src/com/vaadin/data/util/converter/NumberToStringConverter.java @@ -0,0 +1,57 @@ +package com.vaadin.data.util.converter; + +import java.text.NumberFormat; +import java.text.ParsePosition; +import java.util.Locale; + +public class NumberToStringConverter implements Converter { + + protected NumberFormat getFormatter(Locale locale) { + if (locale == null) { + return NumberFormat.getNumberInstance(); + } else { + return NumberFormat.getNumberInstance(locale); + } + } + + public Number convertFromTargetToSource(String value, Locale locale) { + if (value == null) { + return null; + } + + // Remove extra spaces + 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 = getFormatter(locale).parse(value, parsePosition); + if (parsePosition.getIndex() != value.length()) { + throw new ConversionException("Could not convert '" + value + + "' to " + getTargetType().getName()); + } + + if (parsedValue == null) { + // Convert "" to null + return null; + } + return parsedValue; + } + + public String convertFromSourceToTarget(Number value, Locale locale) { + if (value == null) { + return null; + } + + return getFormatter(locale).format(value); + } + + public Class getSourceType() { + return Number.class; + } + + public Class getTargetType() { + return String.class; + } + +} diff --git a/src/com/vaadin/data/util/converter/ReverseConverter.java b/src/com/vaadin/data/util/converter/ReverseConverter.java new file mode 100644 index 0000000000..590927a769 --- /dev/null +++ b/src/com/vaadin/data/util/converter/ReverseConverter.java @@ -0,0 +1,32 @@ +package com.vaadin.data.util.converter; + +import java.util.Locale; + +public class ReverseConverter implements + Converter { + + private Converter realConverter; + + public ReverseConverter(Converter realConverter) { + this.realConverter = realConverter; + } + + public SOURCE convertFromTargetToSource(TARGET value, Locale locale) + throws com.vaadin.data.util.converter.Converter.ConversionException { + return realConverter.convertFromSourceToTarget(value, locale); + } + + public TARGET convertFromSourceToTarget(SOURCE value, Locale locale) + throws com.vaadin.data.util.converter.Converter.ConversionException { + return realConverter.convertFromTargetToSource(value, locale); + } + + public Class getSourceType() { + return realConverter.getTargetType(); + } + + public Class getTargetType() { + return realConverter.getSourceType(); + } + +}