diff options
author | Matti Hosio <mhosio@vaadin.com> | 2014-12-10 09:56:12 +0200 |
---|---|---|
committer | Matti Hosio <mhosio@vaadin.com> | 2014-12-10 16:00:51 +0200 |
commit | 5d6271489b2ce52f3c4eb3ce40cc1148c1092501 (patch) | |
tree | 885ecf2046b336b704b8b840c2a9c207e5db3d63 /server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java | |
parent | 409727ec617bfc092f584769997417c05259614d (diff) | |
download | vaadin-framework-5d6271489b2ce52f3c4eb3ce40cc1148c1092501.tar.gz vaadin-framework-5d6271489b2ce52f3c4eb3ce40cc1148c1092501.zip |
Declarative support for AbstractField (#7749)
Change-Id: I8eb917186886aa1a9c63939d2dfd1f59df973aa2
Diffstat (limited to 'server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java')
-rw-r--r-- | server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java b/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java index f71b36f66c..1beddf57de 100644 --- a/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java +++ b/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java @@ -99,6 +99,7 @@ public class DesignAttributeHandler implements Serializable { * @param defaultInstance * the default instance of the class for fetching the default * values + * @return true on success */ public static boolean readAttribute(DesignSynchronizable component, String attribute, Attributes attributes, @@ -237,6 +238,69 @@ public class DesignAttributeHandler implements Serializable { } /** + * Reads the given attribute from attributes. If the attribute is not found, + * the provided default value is returned + * + * @param attribute + * the attribute key + * @param attributes + * the set of attributes to read from + * @param defaultValue + * the default value that is returned if the attribute is not + * found + * @param outputType + * the output type for the attribute + * @return the attribute value or the default value if the attribute is not + * found + */ + @SuppressWarnings("unchecked") + public static <T> T readAttribute(String attribute, Attributes attributes, + T defaultValue, Class<T> outputType) { + if (!isSupported(outputType)) { + throw new IllegalArgumentException("output type: " + + outputType.getName() + " not supported"); + } + if (!attributes.hasKey(attribute)) { + return defaultValue; + } else { + try { + String value = attributes.get(attribute); + return (T) fromAttributeValue(outputType, value); + } catch (Exception e) { + throw new DesignException("Failed to read attribute " + + attribute, e); + } + } + } + + /** + * Writes the given attribute value to attributes if it differs from the + * default attribute value + * + * @param attribute + * the attribute key + * @param attributes + * the set of attributes where the new attribute is written + * @param value + * the attribute value + * @param defaultValue + * the default attribute value + * @param the + * type of the input value + */ + public static <T> void writeAttribute(String attribute, + Attributes attributes, T value, T defaultValue, Class<T> inputType) { + if (!isSupported(inputType)) { + throw new IllegalArgumentException("input type: " + + inputType.getName() + " not supported"); + } + if (!SharedUtil.equals(value, defaultValue)) { + String attributeValue = toAttributeValue(value.getClass(), value); + attributes.put(attribute, attributeValue); + } + } + + /** * Formats the given design attribute value. The method is provided to * ensure consistent number formatting for design attribute values * |