Change-Id: I3bb2c106a4c4d8556f6f752867da1917e0d3c9a3tags/8.0.0.alpha1
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 | ||||
// 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(); | ||||
} | } |
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; | ||||
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 | ||||
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(); | ||||
/** | /** | ||||
Converter<String, Boolean> booleanConverter = new Converter<String, Boolean>() { | Converter<String, Boolean> booleanConverter = new Converter<String, Boolean>() { | ||||
@Override | @Override | ||||
public Boolean convertToModel(String value, | |||||
Class<? extends Boolean> targetType, Locale locale) | |||||
throws Converter.ConversionException { | |||||
return !value.equalsIgnoreCase("false"); | |||||
public Result<Boolean> convertToModel(String value, Locale locale) { | |||||
return Result.ok(!value.equalsIgnoreCase("false")); | |||||
} | } | ||||
@Override | @Override | ||||
public String convertToPresentation(Boolean value, | |||||
Class<? extends String> targetType, Locale locale) | |||||
throws Converter.ConversionException { | |||||
public String convertToPresentation(Boolean value, Locale locale) { | |||||
if (value.booleanValue()) { | if (value.booleanValue()) { | ||||
return ""; | return ""; | ||||
} else { | } else { | ||||
} | } | ||||
} | } | ||||
@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); | ||||
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, | |||||
new StringToBigDecimalConverter() { | |||||
@Override | |||||
protected NumberFormat getFormat(Locale locale) { | |||||
return bigDecimalFmt; | |||||
}; | |||||
}); | |||||
converterMap.put(BigDecimal.class, new StringToBigDecimalConverter() { | |||||
@Override | |||||
protected NumberFormat getFormat(Locale locale) { | |||||
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, | |||||
Class<? extends String> targetType, Locale locale) | |||||
throws Converter.ConversionException { | |||||
return value; | |||||
public Result<String> convertToModel(String value, Locale locale) { | |||||
return Result.ok(value); | |||||
} | } | ||||
@Override | @Override | ||||
public String convertToPresentation(String value, | |||||
Class<? extends String> targetType, Locale locale) | |||||
throws Converter.ConversionException { | |||||
public String convertToPresentation(String value, Locale locale) { | |||||
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 | ||||
Character.class) { | Character.class) { | ||||
@Override | @Override | ||||
public Character convertToModel(String value, | |||||
Class<? extends Character> targetType, Locale locale) | |||||
throws Converter.ConversionException { | |||||
return value.charAt(0); | |||||
public Result<Character> convertToModel(String value, | |||||
Locale locale) { | |||||
return Result.ok(value.charAt(0)); | |||||
} | } | ||||
}; | }; | ||||
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. | ||||
* | * | ||||
} | } | ||||
/** | /** | ||||
* 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. | ||||
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; | ||||
} | } | ||||
} 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); | |||||
} | } | ||||
} | } | ||||
* @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) { | ||||
} 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 | |||||
.get(supported)); | |||||
return ((Converter<String, T>) converterMap.get(supported)); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
if (sourceType.isEnum()) { | if (sourceType.isEnum()) { | ||||
return (Converter<String, T>) stringEnumConverter; | |||||
return new DesignEnumConverter(sourceType); | |||||
} | } | ||||
return null; | return null; | ||||
} | } |
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 | ||||
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, | |||||
Locale locale) throws Converter.ConversionException { | |||||
public Result<Date> convertToModel(String value, Locale locale) { | |||||
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, | |||||
Class<? extends String> targetType, Locale locale) | |||||
throws Converter.ConversionException { | |||||
public String convertToPresentation(Date value, Locale locale) { | |||||
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; | |||||
} | |||||
} | } |
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 | ||||
* @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, | |||||
Locale locale) | |||||
throws com.vaadin.v7.data.util.converter.Converter.ConversionException { | |||||
public Result<T> convertToModel(String value, Locale locale) { | |||||
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, | |||||
Class<? extends String> targetType, Locale locale) | |||||
throws com.vaadin.v7.data.util.converter.Converter.ConversionException { | |||||
public String convertToPresentation(T value, Locale locale) { | |||||
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; | |||||
} | |||||
} | } |
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 Object to/from String for {@link DesignAttributeHandler} to | * An converter for Object to/from String for {@link DesignAttributeHandler} to | ||||
public class DesignObjectConverter implements Converter<String, Object> { | public class DesignObjectConverter implements Converter<String, Object> { | ||||
@Override | @Override | ||||
public Object convertToModel(String value, | |||||
Class<? extends Object> targetType, Locale locale) | |||||
throws com.vaadin.v7.data.util.converter.Converter.ConversionException { | |||||
return value; | |||||
public Result<Object> convertToModel(String value, Locale locale) { | |||||
return Result.ok(value); | |||||
} | } | ||||
@Override | @Override | ||||
public String convertToPresentation(Object value, | |||||
Class<? extends String> targetType, Locale locale) | |||||
throws com.vaadin.v7.data.util.converter.Converter.ConversionException { | |||||
public String convertToPresentation(Object value, Locale locale) { | |||||
if (value == null) { | if (value == null) { | ||||
return null; | return null; | ||||
} | } | ||||
return value.toString(); | return value.toString(); | ||||
} | } | ||||
@Override | |||||
public Class<Object> getModelType() { | |||||
return Object.class; | |||||
} | |||||
@Override | |||||
public Class<String> getPresentationType() { | |||||
return String.class; | |||||
} | |||||
} | } |
import java.util.Locale; | import java.util.Locale; | ||||
import java.util.Map; | import java.util.Map; | ||||
import com.vaadin.data.Result; | |||||
import com.vaadin.data.util.converter.Converter; | |||||
import com.vaadin.server.ExternalResource; | import com.vaadin.server.ExternalResource; | ||||
import com.vaadin.server.FileResource; | import com.vaadin.server.FileResource; | ||||
import com.vaadin.server.FontAwesome; | import com.vaadin.server.FontAwesome; | ||||
import com.vaadin.server.ResourceReference; | import com.vaadin.server.ResourceReference; | ||||
import com.vaadin.server.ThemeResource; | import com.vaadin.server.ThemeResource; | ||||
import com.vaadin.ui.declarative.DesignAttributeHandler; | import com.vaadin.ui.declarative.DesignAttributeHandler; | ||||
import com.vaadin.v7.data.util.converter.Converter; | |||||
import com.vaadin.v7.data.util.converter.Converter.ConversionException; | |||||
/** | /** | ||||
* A converter for {@link Resource} implementations supported by | * A converter for {@link Resource} implementations supported by | ||||
* @author Vaadin Ltd | * @author Vaadin Ltd | ||||
*/ | */ | ||||
@SuppressWarnings("serial") | @SuppressWarnings("serial") | ||||
public class DesignResourceConverter | |||||
implements Converter<String, Resource> { | |||||
public class DesignResourceConverter implements Converter<String, Resource> { | |||||
@Override | @Override | ||||
public Resource convertToModel(String value, | |||||
Class<? extends Resource> targetType, Locale locale) | |||||
throws Converter.ConversionException { | |||||
public Result<Resource> convertToModel(String value, Locale locale) { | |||||
if (!value.contains("://")) { | if (!value.contains("://")) { | ||||
// assume it'is "file://" protocol, one that is used to access a | // 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 | // file on a given path on the server, this will later be striped | ||||
try { | try { | ||||
ResourceConverterByProtocol converter = ResourceConverterByProtocol | ResourceConverterByProtocol converter = ResourceConverterByProtocol | ||||
.valueOf(protocol.toUpperCase(Locale.ENGLISH)); | .valueOf(protocol.toUpperCase(Locale.ENGLISH)); | ||||
return converter.parse(value); | |||||
return Result.ok(converter.parse(value)); | |||||
} catch (IllegalArgumentException iae) { | } catch (IllegalArgumentException iae) { | ||||
throw new ConversionException("Unrecognized protocol: " + protocol, | |||||
iae); | |||||
return Result.error("Unrecognized protocol: " + protocol); | |||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
public String convertToPresentation(Resource value, | |||||
Class<? extends String> targetType, Locale locale) | |||||
throws Converter.ConversionException { | |||||
public String convertToPresentation(Resource value, Locale locale) { | |||||
ResourceConverterByProtocol byType = ResourceConverterByProtocol | ResourceConverterByProtocol byType = ResourceConverterByProtocol | ||||
.byType(value.getClass()); | .byType(value.getClass()); | ||||
if (byType != null) { | if (byType != null) { | ||||
return byType.format(value); | return byType.format(value); | ||||
} else { | } else { | ||||
throw new Converter.ConversionException( | |||||
throw new IllegalArgumentException( | |||||
"unknown Resource type - " + value.getClass().getName()); | "unknown Resource type - " + value.getClass().getName()); | ||||
} | } | ||||
} | } | ||||
@Override | |||||
public Class<Resource> getModelType() { | |||||
return Resource.class; | |||||
} | |||||
@Override | |||||
public Class<String> getPresentationType() { | |||||
return String.class; | |||||
} | |||||
private static interface ProtocolResourceConverter extends Serializable { | private static interface ProtocolResourceConverter extends Serializable { | ||||
public String format(Resource value); | public String format(Resource value); | ||||
} | } | ||||
@Override | @Override | ||||
public String format(Resource value) | |||||
throws Converter.ConversionException { | |||||
public String format(Resource value) { | |||||
return new ResourceReference(value, null, null).getURL(); | return new ResourceReference(value, null, null).getURL(); | ||||
} | } | ||||
}, | }, | ||||
} | } | ||||
@Override | @Override | ||||
public String format(Resource value) | |||||
throws Converter.ConversionException { | |||||
public String format(Resource value) { | |||||
FontIcon icon = (FontIcon) value; | FontIcon icon = (FontIcon) value; | ||||
return new ResourceReference(icon, null, null).getURL(); | return new ResourceReference(icon, null, null).getURL(); | ||||
} | } | ||||
@Override | @Override | ||||
public String format(Resource value) | |||||
throws Converter.ConversionException { | |||||
public String format(Resource value) { | |||||
throw new UnsupportedOperationException( | throw new UnsupportedOperationException( | ||||
"Use " + ResourceConverterByProtocol.FONTICON.toString() | "Use " + ResourceConverterByProtocol.FONTICON.toString() | ||||
+ " instead"); | + " instead"); | ||||
} | } | ||||
@Override | @Override | ||||
public String format(Resource value) | |||||
throws Converter.ConversionException { | |||||
public String format(Resource value) { | |||||
String path = ((FileResource) value).getSourceFile().getPath(); | String path = ((FileResource) value).getSourceFile().getPath(); | ||||
if (File.separatorChar != '/') { | if (File.separatorChar != '/') { | ||||
// make sure we use '/' as file separator in templates | // make sure we use '/' as file separator in templates | ||||
} | } | ||||
@Override | @Override | ||||
public String format(Resource value) | |||||
throws Converter.ConversionException { | |||||
public String format(Resource value) { | |||||
// default behavior for HTTP, HTTPS, FTP and FTPS | // default behavior for HTTP, HTTPS, FTP and FTPS | ||||
return ((ExternalResource) value).getURL(); | return ((ExternalResource) value).getURL(); | ||||
} | } |
import java.util.Map; | import java.util.Map; | ||||
import java.util.Map.Entry; | import java.util.Map.Entry; | ||||
import com.vaadin.data.Result; | |||||
import com.vaadin.data.util.converter.Converter; | |||||
import com.vaadin.event.ShortcutAction; | import com.vaadin.event.ShortcutAction; | ||||
import com.vaadin.event.ShortcutAction.KeyCode; | import com.vaadin.event.ShortcutAction.KeyCode; | ||||
import com.vaadin.event.ShortcutAction.ModifierKey; | import com.vaadin.event.ShortcutAction.ModifierKey; | ||||
import com.vaadin.v7.data.util.converter.Converter; | |||||
/** | /** | ||||
* Converter for {@link ShortcutActions}. | * Converter for {@link ShortcutActions}. | ||||
} | } | ||||
@Override | @Override | ||||
public ShortcutAction convertToModel(String value, | |||||
Class<? extends ShortcutAction> targetType, Locale locale) | |||||
throws Converter.ConversionException { | |||||
public Result<ShortcutAction> convertToModel(String value, Locale locale) { | |||||
if (value.length() == 0) { | if (value.length() == 0) { | ||||
return null; | |||||
return Result.ok(null); | |||||
} | } | ||||
String[] data = value.split(" ", 2); | String[] data = value.split(" ", 2); | ||||
"Invalid modifier '" + parts[i] + "'"); | "Invalid modifier '" + parts[i] + "'"); | ||||
} | } | ||||
} | } | ||||
return new ShortcutAction(data.length == 2 ? data[1] : null, | |||||
keyCode, modifiers); | |||||
return Result.ok(new ShortcutAction( | |||||
data.length == 2 ? data[1] : null, keyCode, modifiers)); | |||||
} catch (Exception e) { | } catch (Exception e) { | ||||
throw new ConversionException("Invalid shortcut '" + value + "'", | |||||
e); | |||||
return Result.error("Invalid shortcut '" + value + "'"); | |||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
public String convertToPresentation(ShortcutAction value, | |||||
Class<? extends String> targetType, Locale locale) | |||||
throws Converter.ConversionException { | |||||
public String convertToPresentation(ShortcutAction value, Locale locale) { | |||||
StringBuilder sb = new StringBuilder(); | StringBuilder sb = new StringBuilder(); | ||||
// handle modifiers | // handle modifiers | ||||
if (value.getModifiers() != null) { | if (value.getModifiers() != null) { | ||||
return sb.toString(); | return sb.toString(); | ||||
} | } | ||||
@Override | |||||
public Class<ShortcutAction> getModelType() { | |||||
return ShortcutAction.class; | |||||
} | |||||
@Override | |||||
public Class<String> getPresentationType() { | |||||
return String.class; | |||||
} | |||||
public int getKeycodeForString(String attributePresentation) { | public int getKeycodeForString(String attributePresentation) { | ||||
Integer code = presentationMap.get(attributePresentation); | Integer code = presentationMap.get(attributePresentation); | ||||
return code != null ? code.intValue() : -1; | return code != null ? code.intValue() : -1; |
import java.util.Locale; | import java.util.Locale; | ||||
import java.util.TimeZone; | import java.util.TimeZone; | ||||
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; | |||||
/** | /** | ||||
* Utility class for {@link DesignAttributeHandler} that deals with converting | * Utility class for {@link DesignAttributeHandler} that deals with converting | ||||
* @since 7.4 | * @since 7.4 | ||||
* @author Vaadin Ltd | * @author Vaadin Ltd | ||||
*/ | */ | ||||
public class DesignTimeZoneConverter | |||||
implements Converter<String, TimeZone> { | |||||
public class DesignTimeZoneConverter implements Converter<String, TimeZone> { | |||||
@Override | @Override | ||||
public TimeZone convertToModel(String value, | |||||
Class<? extends TimeZone> targetTimeZone, Locale locale) | |||||
throws Converter.ConversionException { | |||||
public Result<TimeZone> convertToModel(String value, Locale locale) { | |||||
if (value == null || value.isEmpty()) { | if (value == null || value.isEmpty()) { | ||||
return null; | |||||
return Result.ok(null); | |||||
} | } | ||||
return TimeZone.getTimeZone(value); | |||||
return Result.ok(TimeZone.getTimeZone(value)); | |||||
} | } | ||||
@Override | @Override | ||||
public String convertToPresentation(TimeZone value, | |||||
Class<? extends String> targetTimeZone, Locale locale) | |||||
throws Converter.ConversionException { | |||||
public String convertToPresentation(TimeZone value, Locale locale) { | |||||
if (value == null) { | if (value == null) { | ||||
return ""; | return ""; | ||||
} else { | } else { | ||||
} | } | ||||
} | } | ||||
@Override | |||||
public Class<TimeZone> getModelType() { | |||||
return TimeZone.class; | |||||
} | |||||
@Override | |||||
public Class<String> getPresentationType() { | |||||
return String.class; | |||||
} | |||||
} | } |
import java.lang.reflect.InvocationTargetException; | import java.lang.reflect.InvocationTargetException; | ||||
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; | |||||
/** | /** | ||||
* Utility class for {@link DesignAttributeHandler} that deals with converting | * Utility class for {@link DesignAttributeHandler} that deals with converting | ||||
* @param <TYPE> | * @param <TYPE> | ||||
* Type of the data being converted. | * Type of the data being converted. | ||||
*/ | */ | ||||
public class DesignToStringConverter<TYPE> | |||||
implements Converter<String, TYPE> { | |||||
public class DesignToStringConverter<TYPE> implements Converter<String, TYPE> { | |||||
private final Class<? extends TYPE> type; | private final Class<? extends TYPE> type; | ||||
} | } | ||||
@Override | @Override | ||||
public TYPE convertToModel(String value, Class<? extends TYPE> targetType, | |||||
Locale locale) throws Converter.ConversionException { | |||||
public Result<TYPE> convertToModel(String value, Locale locale) { | |||||
try { | try { | ||||
return type.cast(type.getMethod(this.staticMethodName, String.class) | |||||
.invoke(null, value)); | |||||
} catch (IllegalAccessException e) { | |||||
throw new Converter.ConversionException(e); | |||||
} catch (IllegalArgumentException e) { | |||||
throw new Converter.ConversionException(e); | |||||
return Result.ok(type | |||||
.cast(type.getMethod(this.staticMethodName, String.class) | |||||
.invoke(null, value))); | |||||
} catch (InvocationTargetException e) { | } catch (InvocationTargetException e) { | ||||
throw new Converter.ConversionException(e.getCause()); | |||||
} catch (NoSuchMethodException e) { | |||||
throw new Converter.ConversionException(e); | |||||
} catch (SecurityException e) { | |||||
throw new Converter.ConversionException(e); | |||||
} catch (RuntimeException e) { | |||||
throw new Converter.ConversionException(e); | |||||
return Result.error(e.getCause().getMessage()); | |||||
} catch (Exception e) { | |||||
return Result.error(e.getMessage()); | |||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
public String convertToPresentation(TYPE value, | |||||
Class<? extends String> targetType, Locale locale) | |||||
throws Converter.ConversionException { | |||||
public String convertToPresentation(TYPE value, Locale locale) { | |||||
if (value == null) { | if (value == null) { | ||||
return NULL_VALUE_REPRESENTATION; | return NULL_VALUE_REPRESENTATION; | ||||
} else { | } else { | ||||
} | } | ||||
} | } | ||||
@Override | |||||
public Class<TYPE> getModelType() { | |||||
return (Class<TYPE>) this.type; | |||||
} | |||||
@Override | |||||
public Class<String> getPresentationType() { | |||||
return String.class; | |||||
} | |||||
} | } |
import com.vaadin.shared.ApplicationConstants; | import com.vaadin.shared.ApplicationConstants; | ||||
import com.vaadin.shared.util.SharedUtil; | import com.vaadin.shared.util.SharedUtil; | ||||
import com.vaadin.ui.declarative.DesignFormatter; | import com.vaadin.ui.declarative.DesignFormatter; | ||||
import com.vaadin.v7.data.util.converter.Converter.ConversionException; | |||||
/** | /** | ||||
* Various tests related to formatter. | * Various tests related to formatter. | ||||
try { | try { | ||||
formatter.parse(shortcut, ShortcutAction.class); | formatter.parse(shortcut, ShortcutAction.class); | ||||
Assert.fail("Invalid shortcut '" + shortcut + "' should throw"); | Assert.fail("Invalid shortcut '" + shortcut + "' should throw"); | ||||
} catch (ConversionException e) { | |||||
} catch (IllegalArgumentException e) { | |||||
// expected | // expected | ||||
} | } | ||||
} | } | ||||
formatter.format(new FileResource(new File(fileSystemPath)))); | formatter.format(new FileResource(new File(fileSystemPath)))); | ||||
} | } | ||||
@Test(expected = ConversionException.class) | |||||
@Test(expected = IllegalArgumentException.class) | |||||
public void testResourceParseException() { | public void testResourceParseException() { | ||||
String someRandomResourceUrl = "random://url"; | String someRandomResourceUrl = "random://url"; | ||||
formatter.parse(someRandomResourceUrl, Resource.class); | formatter.parse(someRandomResourceUrl, Resource.class); | ||||
} | } | ||||
@Test(expected = ConversionException.class) | |||||
@Test(expected = IllegalArgumentException.class) | |||||
public void testResourceFormatException() { | public void testResourceFormatException() { | ||||
formatter.format(new Resource() { // must use unknown resource type | formatter.format(new Resource() { // must use unknown resource type | ||||
@Override | @Override |