Example usage for com.google.gwt.uibinder.rebind XMLElement consumeAttribute

List of usage examples for com.google.gwt.uibinder.rebind XMLElement consumeAttribute

Introduction

In this page you can find the example usage for com.google.gwt.uibinder.rebind XMLElement consumeAttribute.

Prototype

public String consumeAttribute(String name, JType type) throws UnableToCompleteException 

Source Link

Document

Consumes the given attribute as a literal or field reference.

Usage

From source file:com.jhickman.web.gwt.gxtuibinder.elementparsers.custom.PortalParser.java

License:Apache License

@Override
public void parse(XMLElement elem, String fieldName, JClassType type, UiBinderWriter writer)
        throws UnableToCompleteException {
    JClassType integerType = writer.getOracle().findType("java.lang.Integer");

    JClassType doubleType = writer.getOracle().findType("java.lang.Double");

    String columnCountAttribute = elem.consumeRequiredAttribute("numColumns", integerType);
    Integer columnCount = Integer.valueOf(columnCountAttribute);

    Interpreter<Boolean> interpreter = new SimpleInterpreter(elem.getNamespaceUri(), "column");
    int columnIndex = 0;
    for (XMLElement column : elem.consumeChildElements(interpreter)) {
        String width = column.consumeAttribute("width", doubleType);
        if (width != null) {
            writer.addStatement("%s.setColumnWidth(%d, %s);", fieldName, columnIndex, width);
        }//w  w  w .java2s .  c om

        for (XMLElement columnChild : column.consumeChildElements()) {
            String childField = writer.parseElementToField(columnChild);
            writer.addStatement("%s.add(%s, %d);", fieldName, childField, columnIndex);
        }

        columnIndex++;
    }

    if (columnIndex > columnCount) {
        writer.die(elem, "numColumns set to %s, but found %s", columnCountAttribute, columnIndex);
    }

    writer.setFieldInitializerAsConstructor(fieldName, type, "" + columnIndex);
}

From source file:com.jhickman.web.gwt.gxtuibinder.elementparsers.form.SimpleComboBoxParser.java

License:Apache License

@Override
public void parse(XMLElement elem, String fieldName, JClassType type, UiBinderWriter writer)
        throws UnableToCompleteException {

    String parameterizedType = elem.consumeRawAttribute("type", "java.lang.String");
    JClassType valueType = writer.getOracle().findType(parameterizedType);
    if (valueType == null) {
        writer.die(elem, "Found type attribute, but unable to resolve the value: %s", parameterizedType);
    }/*from   ww  w  .j a v  a2s.  c o m*/

    for (XMLElement child : elem.consumeChildElements()) {
        if (!child.getNamespaceUri().equals(elem.getNamespaceUri())) {
            writer.die(elem,
                    "Children of SimpleComboBox must be in the same namespace.  Expected '%s' but found '%s'",
                    elem.getPrefix(), child.getPrefix());
        }

        String data = parseChildElement(child, valueType, writer);

        writer.addStatement("%s.add(%s);", fieldName, data);
    }

    if (elem.getAttribute("simpleValue") != null) {
        writer.addStatement("%s.setSimpleValue(%s);", fieldName,
                elem.consumeAttribute("simpleValue", valueType));
    }
}

From source file:com.jhickman.web.gwt.gxtuibinder.elementparsers.grid.GridParser.java

License:Apache License

@Override
public void parse(XMLElement elem, String fieldName, JClassType type, UiBinderWriter writer)
        throws UnableToCompleteException {
    JClassType listStoreType = writer.getOracle().findType(GxtClassnameConstants.LISTSTORE);
    String store = elem.consumeAttribute("store", listStoreType);

    if (store == null) {
        writer.die(elem, "Attribute 'store' is required");
    }/*from  w  w w  .  j a  v a  2 s .c  om*/

    // have to set the following fields to null temporarily.  
    // Due to the order of operations added, we need to construct
    // all ColumnConfigs before constructing a ColumnModel.
    writer.setFieldInitializer(fieldName, "null");
    // new ColumnModel(columnConfig)
    String columnModel = writer.declareField(GxtClassnameConstants.COLUMNMODEL, elem);
    writer.setFieldInitializer(columnModel, "null");

    JClassType arrayListType = writer.getOracle().findType(ArrayList.class.getName());
    JClassType columnConfigType = writer.getOracle().findType(GxtClassnameConstants.COLUMNCONFIG);
    JParameterizedType parameterizedArrayListType = writer.getOracle()
            .getParameterizedType(arrayListType.isGenericType(), new JClassType[] { columnConfigType });

    Map<String, JType> columnConfigSetterTypes = fetchColumnConfigProperties(columnConfigType);

    // List<ColumnConfig>
    String columnConfigList = writer.declareField(List.class.getName(), elem);
    writer.setFieldInitializerAsConstructor(columnConfigList, parameterizedArrayListType);

    for (XMLElement child : elem.consumeChildElements()) {
        if (!elem.getPrefix().equals(child.getPrefix())) {
            writer.die(child, "Child node of %s must use the same prefix.  Expecting %s, but found %s", elem,
                    elem.getPrefix(), child.getPrefix());
        }

        if (!"column".equals(child.getLocalName())) {
            writer.die(child, "Only <%s:column> children are allowed. Found %s.", elem.getPrefix(), child);
        }

        String columnConfig = writer.declareField(GxtClassnameConstants.COLUMNCONFIG, elem);

        applyColumnConfigProperties(writer, columnConfigSetterTypes, child, columnConfig);

        writer.addStatement("%s.add(%s);", columnConfigList, columnConfig);
    }

    // now that we have all ColumnConfigs created and added to list, we can now
    // construct the ColumnModel and Grid
    writer.addStatement("%s = new %s(%s);", columnModel, GxtClassnameConstants.COLUMNMODEL, columnConfigList);
    writer.addStatement("%s = new %s(%s, %s);", fieldName, type.getQualifiedSourceName(), store, columnModel);
}

From source file:com.jhickman.web.gwt.gxtuibinder.elementparsers.grid.GridParser.java

License:Apache License

protected void applyColumnConfigProperties(UiBinderWriter writer, Map<String, JType> columnConfigSetterTypes,
        XMLElement child, String columnConfig) throws UnableToCompleteException {

    int attributeCount = child.getAttributeCount();
    for (int i = 0; i < attributeCount; i++) {
        // always get 0 because we're consuming them
        XMLAttribute attribute = child.getAttribute(0);

        String setterMethod = "set" + initialCap(attribute.getName());
        String value = child.consumeAttribute(attribute.getName(), columnConfigSetterTypes.get(setterMethod));
        writer.addStatement("%s.%s(%s);", columnConfig, setterMethod, value);
    }//w  w  w.  j  av  a  2 s  .com
}

From source file:com.jhickman.web.gwt.gxtuibinder.elementparsers.layout.RowLayoutParser.java

License:Apache License

@Override
public void parse(XMLElement layoutElem, XMLElement elem, String fieldName, JClassType type,
        UiBinderWriter writer) throws UnableToCompleteException {
    String layout = createAndSetLayout(layoutElem, elem, fieldName, writer);

    JClassType orientationType = writer.getOracle().findType(GxtClassnameConstants.STYLEORIENTATION);

    String layoutOrientation = elem.consumeAttribute("rowLayoutOrientation", orientationType);
    if (layoutOrientation != null) {
        writer.warn(elem,//from  w w w  .  j av  a2  s . c o m
                "rowLayoutOrientation has been deprecated. Please use nested <%s:layout orientation='%s'> instead.",
                elem.getPrefix(), layoutOrientation);
        writer.addStatement("%s.setOrientation(%s);", layout, layoutOrientation);
    }

    // override with layoutOrientation
    layoutOrientation = elem.consumeAttribute("layoutOrientation", orientationType);
    if (layoutOrientation != null) {
        writer.warn(elem,
                "layoutOrientation has been deprecated. Please use nested <%s:layout orientation='%s'> instead.",
                elem.getPrefix(), layoutOrientation);
        writer.addStatement("%s.setOrientation(%s);", layout, layoutOrientation);
    }

    handleChildren(elem, fieldName, writer);
}

From source file:com.jhickman.web.gwt.gxtuibinder.elementparsers.util.ElementParserUtil.java

License:Apache License

/**
 * Consumes and applies attributes to given element
 * //from w w  w . j  a  v  a2s  .c o m
 * @param elem
 * @param fieldName
 * @param type
 * @param writer
 * @throws UnableToCompleteException
 */
public static void applyAttributes(XMLElement elem, String fieldName, JClassType type, UiBinderWriter writer)
        throws UnableToCompleteException {

    // first get the special Margin attribute
    ElementParserUtil.consumeRegionAttributes(elem, fieldName, writer);

    Map<String, JType> setterMethods = fetchSetterMethods(type);
    int attributeCount = elem.getAttributeCount();

    // count backwards since we're consuming as we go
    for (int i = attributeCount - 1; i >= 0; i--) {
        // always get 0 because we're consuming them
        XMLAttribute attribute = elem.getAttribute(i);

        String setterMethod = "set" + initialCap(attribute.getName());

        if (setterMethods.containsKey(setterMethod)) {
            JType setterType = setterMethods.get(setterMethod);
            String value;

            if ("float".equals(setterType.getQualifiedSourceName())) {
                value = attribute.consumeRawValue();
                float floatValue = 0;
                try {
                    floatValue = Float.parseFloat(value);
                } catch (NumberFormatException e) {
                    writer.die(elem, "Cannot parse float value for attribute '%s'.  Found %s.",
                            attribute.getName(), value);
                }
                // assuming that we didn't die due to a NumberFormatException, we can use the value
                value = floatValue + "f";
            } else if ("java.lang.Number".equals(setterType.getQualifiedSourceName())) {
                value = elem.consumeRawAttribute(attribute.getName());
            } else {
                value = elem.consumeAttribute(attribute.getName(), setterType);
            }
            writer.addStatement("%s.%s(%s);", fieldName, setterMethod, value);

        } else {
            try {
                writer.findFieldType(elem);
            } catch (UnableToCompleteException e) {
                writer.warn(elem, "Found attribute without associated setter method: %s.  IGNORING",
                        attribute.getName());
            }
        }
    }
}

From source file:org.vectomatic.dev.svg.impl.gen.SVGImageParser.java

License:Open Source License

@Override
public void parse(XMLElement elem, String fieldName, JClassType type, UiBinderWriter writer)
        throws UnableToCompleteException {
    if (elem.hasAttribute(ATTR_RESOURCE) && elem.hasChildNodes()) {
        writer.die("In %s, attribute \"%s\" and inline svg are mutually exclusive", elem, ATTR_RESOURCE);
    }/*from  w w  w .j  av a  2s  .  c  om*/
    if (!(elem.hasAttribute(ATTR_RESOURCE) || elem.hasChildNodes())) {
        writer.die("In %s, attribute \"%s\" or inline svg must be present", elem, ATTR_RESOURCE);
    }
    if (elem.hasAttribute(ATTR_RESOURCE)) {
        JClassType svgResourceType = writer.getOracle().findType(SVGResource.class.getCanonicalName());
        String resource = elem.consumeAttribute(ATTR_RESOURCE, svgResourceType);
        writer.addStatement("%s.setResource(%s);", fieldName, resource);
    } else {
        boolean validated = true;
        if (elem.hasAttribute(ATTR_VALIDATED)) {
            String value = elem.consumeBooleanAttribute(ATTR_VALIDATED);
            validated = Boolean.valueOf(value);
        }
        Element container = elem.getElement();
        NodeList childNodes = container.getChildNodes();
        Element root = null;
        for (int i = 0, length = childNodes.getLength(); i < length; i++) {
            Node node = childNodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                if (root == null && SVGConstants.SVG_NAMESPACE_URI.equals(node.getNamespaceURI())
                        && SVGConstants.SVG_SVG_TAG.equals(node.getLocalName())) {
                    root = (Element) node;
                } else {
                    writer.die("In %s, attribute \"%s\" or inline svg must be present", elem, ATTR_RESOURCE);
                }
            }
        }
        if (root == null) {
            writer.die("In %s, attribute \"%s\" or inline svg must be present", elem, ATTR_RESOURCE);
        }
        writer.beginAttachedSection(fieldName + ".getElement()");
        SvgInterpreter interpreter = SvgInterpreter.newInterpreterForUiObject(writer, fieldName, root);
        String rawSvg = elem.consumeInnerHtml(interpreter);
        if (validated) {
            SVGValidator.validate(rawSvg, elem.getLocation().getSystemId(), null, writer);
        }
        String omSvgParser = OMSVGParser.class.getName();
        writer.addStatement("%s.setSvgElement(%s.parse(\"%s\"));", fieldName, omSvgParser, rawSvg);
        writer.endAttachedSection();
    }
}