Skip to content

Commit

Permalink
Change declarative support to use new converters
Browse files Browse the repository at this point in the history
Change-Id: I3bb2c106a4c4d8556f6f752867da1917e0d3c9a3
  • Loading branch information
Artur- committed Aug 24, 2016
1 parent 38b825c commit 2713507
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 237 deletions.
Expand Up @@ -36,10 +36,10 @@
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node; import org.jsoup.nodes.Node;


import com.vaadin.data.util.converter.Converter;
import com.vaadin.shared.ui.AlignmentInfo; import com.vaadin.shared.ui.AlignmentInfo;
import com.vaadin.shared.util.SharedUtil; import com.vaadin.shared.util.SharedUtil;
import com.vaadin.ui.Alignment; import com.vaadin.ui.Alignment;
import com.vaadin.v7.data.util.converter.Converter;


/** /**
* Default attribute handler implementation used when parsing designs to * Default attribute handler implementation used when parsing designs to
Expand Down Expand Up @@ -379,10 +379,11 @@ private static String toAttributeValue(Class<?> sourceType, Object value) {
// value is not null. How to represent null value in attributes? // value is not null. How to represent null value in attributes?
return ""; return "";
} }
Converter<String, Object> converter = getFormatter() @SuppressWarnings("unchecked")
Converter<String, Object> converter = (Converter<String, Object>) getFormatter()
.findConverterFor(sourceType); .findConverterFor(sourceType);
if (converter != null) { if (converter != null) {
return converter.convertToPresentation(value, String.class, null); return converter.convertToPresentation(value, null);
} else { } else {
return value.toString(); return value.toString();
} }
Expand Down
94 changes: 27 additions & 67 deletions server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java
Expand Up @@ -30,6 +30,11 @@


import org.jsoup.parser.Parser; import org.jsoup.parser.Parser;


import com.vaadin.data.Result;
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.event.ShortcutAction;
import com.vaadin.server.Resource; import com.vaadin.server.Resource;
import com.vaadin.ui.declarative.converters.DesignDateConverter; import com.vaadin.ui.declarative.converters.DesignDateConverter;
Expand All @@ -39,10 +44,6 @@
import com.vaadin.ui.declarative.converters.DesignShortcutActionConverter; import com.vaadin.ui.declarative.converters.DesignShortcutActionConverter;
import com.vaadin.ui.declarative.converters.DesignTimeZoneConverter; import com.vaadin.ui.declarative.converters.DesignTimeZoneConverter;
import com.vaadin.ui.declarative.converters.DesignToStringConverter; import com.vaadin.ui.declarative.converters.DesignToStringConverter;
import com.vaadin.v7.data.util.converter.Converter;
import com.vaadin.v7.data.util.converter.StringToBigDecimalConverter;
import com.vaadin.v7.data.util.converter.StringToDoubleConverter;
import com.vaadin.v7.data.util.converter.StringToFloatConverter;


/** /**
* Class focused on flexible and consistent formatting and parsing of different * Class focused on flexible and consistent formatting and parsing of different
Expand All @@ -55,7 +56,6 @@
public class DesignFormatter implements Serializable { public class DesignFormatter implements Serializable {


private final Map<Class<?>, Converter<String, ?>> converterMap = new ConcurrentHashMap<Class<?>, Converter<String, ?>>(); private final Map<Class<?>, Converter<String, ?>> converterMap = new ConcurrentHashMap<Class<?>, Converter<String, ?>>();
private final Converter<String, Enum> stringEnumConverter = new DesignEnumConverter();
private final Converter<String, Object> stringObjectConverter = new DesignObjectConverter(); private final Converter<String, Object> stringObjectConverter = new DesignObjectConverter();


/** /**
Expand Down Expand Up @@ -86,33 +86,19 @@ protected void mapDefaultTypes() {
Converter<String, Boolean> booleanConverter = new Converter<String, Boolean>() { Converter<String, Boolean> booleanConverter = new Converter<String, Boolean>() {


@Override @Override
public Boolean convertToModel(String value, public Result<Boolean> convertToModel(String value, Locale locale) {
Class<? extends Boolean> targetType, Locale locale) return Result.ok(!value.equalsIgnoreCase("false"));
throws Converter.ConversionException {
return !value.equalsIgnoreCase("false");
} }


@Override @Override
public String convertToPresentation(Boolean value, public String convertToPresentation(Boolean value, Locale locale) {
Class<? extends String> targetType, Locale locale)
throws Converter.ConversionException {
if (value.booleanValue()) { if (value.booleanValue()) {
return ""; return "";
} else { } else {
return "false"; return "false";
} }
} }


@Override
public Class<Boolean> getModelType() {
return Boolean.class;
}

@Override
public Class<String> getPresentationType() {
return String.class;
}

}; };
converterMap.put(Boolean.class, booleanConverter); converterMap.put(Boolean.class, booleanConverter);
converterMap.put(boolean.class, booleanConverter); converterMap.put(boolean.class, booleanConverter);
Expand Down Expand Up @@ -144,52 +130,36 @@ protected NumberFormat getFormat(Locale locale) {
final DecimalFormat bigDecimalFmt = new DecimalFormat("0.###", symbols); final DecimalFormat bigDecimalFmt = new DecimalFormat("0.###", symbols);
bigDecimalFmt.setGroupingUsed(false); bigDecimalFmt.setGroupingUsed(false);
bigDecimalFmt.setParseBigDecimal(true); bigDecimalFmt.setParseBigDecimal(true);
converterMap.put(BigDecimal.class, converterMap.put(BigDecimal.class, new StringToBigDecimalConverter() {
new StringToBigDecimalConverter() { @Override
@Override protected NumberFormat getFormat(Locale locale) {
protected NumberFormat getFormat(Locale locale) { return bigDecimalFmt;
return bigDecimalFmt; };
}; });
});


// strings do nothing // strings do nothing
converterMap.put(String.class, new Converter<String, String>() { converterMap.put(String.class, new Converter<String, String>() {


@Override @Override
public String convertToModel(String value, public Result<String> convertToModel(String value, Locale locale) {
Class<? extends String> targetType, Locale locale) return Result.ok(value);
throws Converter.ConversionException {
return value;
} }


@Override @Override
public String convertToPresentation(String value, public String convertToPresentation(String value, Locale locale) {
Class<? extends String> targetType, Locale locale)
throws Converter.ConversionException {
return value; return value;
} }


@Override
public Class<String> getModelType() {
return String.class;
}

@Override
public Class<String> getPresentationType() {
return String.class;
}

}); });


// char takes the first character from the string // char takes the first character from the string
Converter<String, Character> charConverter = new DesignToStringConverter<Character>( Converter<String, Character> charConverter = new DesignToStringConverter<Character>(
Character.class) { Character.class) {


@Override @Override
public Character convertToModel(String value, public Result<Character> convertToModel(String value,
Class<? extends Character> targetType, Locale locale) Locale locale) {
throws Converter.ConversionException { return Result.ok(value.charAt(0));
return value.charAt(0);
} }


}; };
Expand All @@ -203,16 +173,6 @@ public Character convertToModel(String value,
converterMap.put(TimeZone.class, new DesignTimeZoneConverter()); converterMap.put(TimeZone.class, new DesignTimeZoneConverter());
} }


/**
* Adds a converter for a new type.
*
* @param converter
* Converter to add.
*/
protected <T> void addConverter(Converter<String, T> converter) {
converterMap.put(converter.getModelType(), converter);
}

/** /**
* Adds a converter for a given type. * Adds a converter for a given type.
* *
Expand Down Expand Up @@ -248,7 +208,7 @@ protected Set<Class<?>> getRegisteredClasses() {
} }


/** /**
* Parses a given string as a value of given type * Parses a given string as a value of given type.
* *
* @param value * @param value
* String value to convert. * String value to convert.
Expand All @@ -260,7 +220,8 @@ protected Set<Class<?>> getRegisteredClasses() {
public <T> T parse(String value, Class<? extends T> type) { public <T> T parse(String value, Class<? extends T> type) {
Converter<String, T> converter = findConverterFor(type); Converter<String, T> converter = findConverterFor(type);
if (converter != null) { if (converter != null) {
return converter.convertToModel(value, type, null); Result<T> result = converter.convertToModel(value, null);
return result.getOrThrow(msg -> new IllegalArgumentException(msg));
} else { } else {
return null; return null;
} }
Expand Down Expand Up @@ -295,7 +256,7 @@ public <T> String format(T object, Class<? extends T> type) {
} else { } else {
Converter<String, Object> converter = findConverterFor( Converter<String, Object> converter = findConverterFor(
object.getClass()); object.getClass());
return converter.convertToPresentation(object, String.class, null); return converter.convertToPresentation(object, null);
} }
} }


Expand Down Expand Up @@ -325,7 +286,7 @@ public boolean canConvert(Class<?> type) {
* @return A valid converter for a given type or its supertype, <b>null</b> * @return A valid converter for a given type or its supertype, <b>null</b>
* if it was not found. * if it was not found.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings({ "unchecked", "rawtypes" })
protected <T> Converter<String, T> findConverterFor( protected <T> Converter<String, T> findConverterFor(
Class<? extends T> sourceType, boolean strict) { Class<? extends T> sourceType, boolean strict) {
if (sourceType == Object.class) { if (sourceType == Object.class) {
Expand All @@ -340,14 +301,13 @@ protected <T> Converter<String, T> findConverterFor(
} else if (!strict) { } else if (!strict) {
for (Class<?> supported : converterMap.keySet()) { for (Class<?> supported : converterMap.keySet()) {
if (supported.isAssignableFrom(sourceType)) { if (supported.isAssignableFrom(sourceType)) {
return ((Converter<String, T>) converterMap return ((Converter<String, T>) converterMap.get(supported));
.get(supported));
} }
} }
} }


if (sourceType.isEnum()) { if (sourceType.isEnum()) {
return (Converter<String, T>) stringEnumConverter; return new DesignEnumConverter(sourceType);
} }
return null; return null;
} }
Expand Down
Expand Up @@ -20,8 +20,9 @@
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;


import com.vaadin.data.Result;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.ui.declarative.DesignAttributeHandler; import com.vaadin.ui.declarative.DesignAttributeHandler;
import com.vaadin.v7.data.util.converter.Converter;


/** /**
* A date converter to be used by {@link DesignAttributeHandler}. Provides * A date converter to be used by {@link DesignAttributeHandler}. Provides
Expand All @@ -33,35 +34,22 @@
public class DesignDateConverter implements Converter<String, Date> { public class DesignDateConverter implements Converter<String, Date> {


@Override @Override
public Date convertToModel(String value, Class<? extends Date> targetType, public Result<Date> convertToModel(String value, Locale locale) {
Locale locale) throws Converter.ConversionException {
for (String pattern : new String[] { "yyyy-MM-dd HH:mm:ssZ", 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 HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH",
"yyyy-MM-dd", "yyyy-MM", "yyyy" }) { "yyyy-MM-dd", "yyyy-MM", "yyyy" }) {
try { try {
return new SimpleDateFormat(pattern).parse(value); return Result.ok(new SimpleDateFormat(pattern).parse(value));
} catch (ParseException e) { } catch (ParseException e) {
// not parseable, ignore and try another format // not parseable, ignore and try another format
} }
} }
return null; return Result.error("Could not parse date value: " + value);
} }


@Override @Override
public String convertToPresentation(Date value, public String convertToPresentation(Date value, Locale locale) {
Class<? extends String> targetType, Locale locale)
throws Converter.ConversionException {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ").format(value); return new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ").format(value);
} }


@Override
public Class<Date> getModelType() {
return Date.class;
}

@Override
public Class<String> getPresentationType() {
return String.class;
}

} }
Expand Up @@ -17,8 +17,9 @@


import java.util.Locale; import java.util.Locale;


import com.vaadin.data.Result;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.ui.declarative.DesignAttributeHandler; import com.vaadin.ui.declarative.DesignAttributeHandler;
import com.vaadin.v7.data.util.converter.Converter;


/** /**
* An converter for Enum to/from String for {@link DesignAttributeHandler} to * An converter for Enum to/from String for {@link DesignAttributeHandler} to
Expand All @@ -27,37 +28,44 @@
* @since 7.4 * @since 7.4
* @author Vaadin Ltd * @author Vaadin Ltd
*/ */
public class DesignEnumConverter implements Converter<String, Enum> { @SuppressWarnings("rawtypes")
public class DesignEnumConverter<T extends Enum>
implements Converter<String, T> {


private Class<T> type;

/**
* Creates a converter for the given enum type.
*
* @param type
* the enum type to convert to/from
*/
public DesignEnumConverter(Class<T> type) {
this.type = type;
}

@SuppressWarnings("unchecked")
@Override @Override
public Enum convertToModel(String value, Class<? extends Enum> targetType, public Result<T> convertToModel(String value, Locale locale) {
Locale locale)
throws com.vaadin.v7.data.util.converter.Converter.ConversionException {
if (value == null || value.trim().equals("")) { if (value == null || value.trim().equals("")) {
return null; return Result.ok(null);
}
try {
T result = (T) Enum.valueOf(type,
value.toUpperCase(Locale.ENGLISH));
return Result.ok(result);
} catch (Exception e) {
return Result.error(e.getMessage());
} }
return Enum.valueOf(targetType, value.toUpperCase(Locale.ENGLISH));
} }


@Override @Override
public String convertToPresentation(Enum value, public String convertToPresentation(T value, Locale locale) {
Class<? extends String> targetType, Locale locale)
throws com.vaadin.v7.data.util.converter.Converter.ConversionException {
if (value == null) { if (value == null) {
return null; return null;
} }


return value.name().toLowerCase(Locale.ENGLISH); return value.name().toLowerCase(Locale.ENGLISH);
} }


@Override
public Class<Enum> getModelType() {
return Enum.class;
}

@Override
public Class<String> getPresentationType() {
return String.class;
}

} }

0 comments on commit 2713507

Please sign in to comment.