Browse Source

Add DesignerAttributeHelper.readAttribute with default value (#17416)

Change-Id: Ic746e761942c3d801c8e1c71bd5866bbd5daeaf4
tags/7.5.0.beta1
Teemu Suo-Anttila 9 years ago
parent
commit
8f6a7b9d36
1 changed files with 51 additions and 29 deletions
  1. 51
    29
      server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java

+ 51
- 29
server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java View File

@@ -218,17 +218,67 @@ public class DesignAttributeHandler implements Serializable {
}

/**
* Reads the given attribute from a set of attributes.
* Writes the given attribute value to a set of 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 inputType
* the type of the input value
*/
public static <T> void writeAttribute(String attribute,
Attributes attributes, T value, T defaultValue, Class<T> inputType) {
if (!getFormatter().canConvert(inputType)) {
throw new IllegalArgumentException("input type: "
+ inputType.getName() + " not supported");
}
if (!SharedUtil.equals(value, defaultValue)) {
String attributeValue = toAttributeValue(inputType, value);
attributes.put(attribute, attributeValue);
}
}

/**
* Reads the given attribute from a set of attributes. If attribute does not
* exist return a given default value.
*
* @param attribute
* the attribute key
* @param attributes
* the set of attributes to read from
* @param defaultValue
* the default value to return if attribute does not exist
* @param outputType
* the output type for the attribute
* @return the attribute value or the default value if the attribute is not
* found
*/
public static <T> T readAttribute(String attribute, Attributes attributes,
T defaultValue, Class<T> outputType) {
T value = readAttribute(attribute, attributes, outputType);
if (value != null) {
return value;
}
return defaultValue;
}

/**
* Reads the given attribute from a set of attributes.
*
* @param attribute
* the attribute key
* @param attributes
* the set of attributes to read from
* @param outputType
* the output type for the attribute
* @return the attribute value or null
*/
public static <T> T readAttribute(String attribute, Attributes attributes,
Class<T> outputType) {
if (!getFormatter().canConvert(outputType)) {
@@ -248,33 +298,6 @@ public class DesignAttributeHandler implements Serializable {
}
}

/**
* Writes the given attribute value to a set of 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 inputType
* the type of the input value
*/
public static <T> void writeAttribute(String attribute,
Attributes attributes, T value, T defaultValue, Class<T> inputType) {
if (!getFormatter().canConvert(inputType)) {
throw new IllegalArgumentException("input type: "
+ inputType.getName() + " not supported");
}
if (!SharedUtil.equals(value, defaultValue)) {
String attributeValue = toAttributeValue(inputType, value);
attributes.put(attribute, attributeValue);
}
}

/**
* Returns the design attribute name corresponding the given method name.
* For example given a method name <code>setPrimaryStyleName</code> the
@@ -425,5 +448,4 @@ public class DesignAttributeHandler implements Serializable {
return (methods != null && methods.length > 1) ? methods[1] : null;
}
}

}

Loading…
Cancel
Save