Search in sources :

Example 6 with GwtTestUiBinderException

use of com.googlecode.gwt.test.exceptions.GwtTestUiBinderException in project gwt-test-utils by gwt-test-utils.

the class UiTagBuilder method endTag.

UiTagBuilder<T> endTag(String nameSpaceURI, String localName) {
    // ignore <UiBinder> tag
    if (UiBinderXmlUtils.isUiBinderTag(nameSpaceURI, localName)) {
        return this;
    }
    Object currentObject = currentTag.endTag();
    UiTag<?> parentTag = currentTag.getParentTag();
    currentTag = parentTag;
    if (UiBinderXmlUtils.isResourceTag(nameSpaceURI, localName) || UiBinderXmlUtils.isImportTag(nameSpaceURI, localName)) {
        // ignore <ui:data>, <ui:image>, <ui:style> <ui:text> and <ui:import> tags
        return this;
    } else if (UiBinderXmlUtils.isMsgTag(nameSpaceURI, localName) || UiBinderXmlUtils.isTextTag(nameSpaceURI, localName)) {
        // special <ui:msg> and <ui:text> case
        parentTag.appendText((String) currentObject);
        return this;
    }
    if (parentTag == null) {
        // parsing is finished, this must be the root component
        if (rootComponent != null) {
            throw new GwtTestUiBinderException("UiBinder template '" + owner.getClass().getName() + "' should declare only one root widget in its corresponding .ui.xml file");
        } else {
            rootComponent = currentObject;
        }
    } else {
        // add to its parent
        if (IsWidget.class.isInstance(currentObject)) {
            parentTag.addWidget((IsWidget) currentObject);
        } else if (UIObject.class.isInstance(currentObject)) {
            // UIObject instance that is not a Widget
            parentTag.addUiObject((UIObject) currentObject);
        } else {
            parentTag.addElement((Element) currentObject);
        }
    }
    return this;
}
Also used : GwtTestUiBinderException(com.googlecode.gwt.test.exceptions.GwtTestUiBinderException) UIObject(com.google.gwt.user.client.ui.UIObject) Element(com.google.gwt.dom.client.Element) UIObject(com.google.gwt.user.client.ui.UIObject)

Example 7 with GwtTestUiBinderException

use of com.googlecode.gwt.test.exceptions.GwtTestUiBinderException in project gwt-test-utils by gwt-test-utils.

the class UiBinderInstanciator method getObjectFromUiFactory.

private static <U> U getObjectFromUiFactory(Class<U> clazz, Object owner) {
    Map<Method, UiFactory> map = GwtReflectionUtils.getAnnotatedMethod(owner.getClass(), UiFactory.class);
    List<Method> compatibleFactories = new ArrayList<Method>();
    for (Method factoryMethod : map.keySet()) {
        if (clazz.isAssignableFrom(factoryMethod.getReturnType())) {
            compatibleFactories.add(factoryMethod);
        }
    }
    switch(compatibleFactories.size()) {
        case 0:
            return null;
        case 1:
            return (U) GwtReflectionUtils.callPrivateMethod(owner, compatibleFactories.get(0));
        default:
            throw new GwtTestUiBinderException("Duplicate factory in class '" + owner.getClass().getName() + " for type '" + clazz.getName() + "'");
    }
}
Also used : GwtTestUiBinderException(com.googlecode.gwt.test.exceptions.GwtTestUiBinderException) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) UiFactory(com.google.gwt.uibinder.client.UiFactory)

Example 8 with GwtTestUiBinderException

use of com.googlecode.gwt.test.exceptions.GwtTestUiBinderException in project gwt-test-utils by gwt-test-utils.

the class UiResourceManager method registerResource.

/**
     * Register a new resource which should correspond to a resource tag in the .ui.xml file.
     *
     * @param localName  The type of the resource ('with', 'style', 'image' or 'data')
     * @param attributes Map of attributes parsed from the tag
     * @param parentTag  The parent tag if any
     * @param owner      The {@link UiBinder} owner widget, which calls the
     *                   {@link UiBinder#createAndBindUi(Object)} method to initialize itself.
     * @return The UiBinderTag which wraps the Resource instance.
     * @throws GwtTestUiBinderException If the localName is not managed or if the alias is already
     *                                  binded to another Resource object
     */
UiTag<?> registerResource(String localName, Map<String, Object> attributes, UiTag<?> parentTag, Object owner) throws GwtTestUiBinderException {
    String alias = getResourceAlias(localName, attributes);
    if (resources.containsKey(alias)) {
        throw new GwtTestUiBinderException("Two inner resources '" + alias + " are declared in " + owner.getClass().getSimpleName() + ".ui.xml. You should add a 'field' attribute to one of them");
    }
    Class<?> type = getResourceType(alias, localName, attributes);
    if ("with".equals(localName)) {
        // special resource <ui:with> : the resource can be annotated with
        // @UiConstructor, @UiFactory or @UiField(provided=true)
        Object resource = UiBinderInstanciator.getInstance(type, attributes, owner);
        resources.put(alias, resource);
        return new UiWithTag(resource);
    }
    ResourcePrototypeProxyBuilder builder = ResourcePrototypeProxyBuilder.createBuilder(type, owner.getClass());
    // common properties
    builder.name(alias);
    if ("style".equals(localName)) {
        // <ui:style>
        return new UiStyleTag(builder, alias, parentTag, owner, resources);
    } else if ("image".equals(localName)) {
        // <ui:image>
        return new UiImgTag(builder, alias, parentTag, owner, resources, attributes);
    } else if ("data".equals(localName)) {
        // <ui:data>
        return new UiDataTag(builder, alias, parentTag, owner, resources, attributes);
    } else {
        throw new GwtTestUiBinderException("resource <" + localName + "> element is not yet implemented by gwt-test-utils");
    }
}
Also used : GwtTestUiBinderException(com.googlecode.gwt.test.exceptions.GwtTestUiBinderException) ResourcePrototypeProxyBuilder(com.googlecode.gwt.test.internal.resources.ResourcePrototypeProxyBuilder) UIObject(com.google.gwt.user.client.ui.UIObject)

Aggregations

GwtTestUiBinderException (com.googlecode.gwt.test.exceptions.GwtTestUiBinderException)8 UIObject (com.google.gwt.user.client.ui.UIObject)5 GwtTestException (com.googlecode.gwt.test.exceptions.GwtTestException)2 Element (com.google.gwt.dom.client.Element)1 UiConstructor (com.google.gwt.uibinder.client.UiConstructor)1 UiFactory (com.google.gwt.uibinder.client.UiFactory)1 IsWidget (com.google.gwt.user.client.ui.IsWidget)1 ResourcePrototypeProxyBuilder (com.googlecode.gwt.test.internal.resources.ResourcePrototypeProxyBuilder)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 InputSource (org.xml.sax.InputSource)1 XMLReader (org.xml.sax.XMLReader)1