Search in sources :

Example 1 with Id

use of com.vaadin.flow.component.polymertemplate.Id in project flow by vaadin.

the class AngularTemplate method tryMapComponentOrElement.

@SuppressWarnings("unchecked")
private void tryMapComponentOrElement(Field field) {
    Optional<Id> idAnnotation = AnnotationReader.getAnnotationFor(field, Id.class);
    if (!idAnnotation.isPresent()) {
        return;
    }
    String id = idAnnotation.get().value();
    Class<?> fieldType = field.getType();
    String fieldName = field.getName();
    Element element = getElementById(id).orElseThrow(() -> new IllegalArgumentException(String.format("No element with id '%s' found while binding field '%s' in '%s'", id, fieldName, getClass().getName())));
    if (element.equals(getElement())) {
        throw new IllegalArgumentException("Cannot map the root element of the template. " + "This is always mapped to the template instance itself (" + getClass().getName() + ")");
    }
    if (Component.class.isAssignableFrom(fieldType)) {
        Class<? extends Component> componentType = (Class<? extends Component>) fieldType;
        Component c = Component.from(element, componentType);
        ReflectTools.setJavaFieldValue(this, field, c);
    } else if (Element.class.isAssignableFrom(fieldType)) {
        ReflectTools.setJavaFieldValue(this, field, element);
    } else {
        throw new IllegalArgumentException(String.format("The field '%s' in '%s' has an @'%s' " + "annotation but the field type '%s' " + "does not extend neither '%s' nor '%s'", fieldName, getClass().getName(), Id.class.getSimpleName(), fieldType.getName(), Component.class.getSimpleName(), Element.class.getSimpleName()));
    }
}
Also used : Element(com.vaadin.flow.dom.Element) Id(com.vaadin.flow.component.polymertemplate.Id) Component(com.vaadin.flow.component.Component)

Aggregations

Component (com.vaadin.flow.component.Component)1 Id (com.vaadin.flow.component.polymertemplate.Id)1 Element (com.vaadin.flow.dom.Element)1