Search in sources :

Example 36 with ModelMap

use of com.vaadin.flow.internal.nodefeature.ModelMap in project flow by vaadin.

the class ModelMapTest method resolveAndCreateSubChildList.

@Test
public void resolveAndCreateSubChildList() {
    ModelList child = rootMap.resolveModelList("parent.child");
    ModelMap parent = getParentMapAndAssertMapping(child, "child");
    ModelMap resolvedRoot = getParentMapAndAssertMapping(parent, "parent");
    Assert.assertEquals(rootMap, resolvedRoot);
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) Test(org.junit.Test)

Example 37 with ModelMap

use of com.vaadin.flow.internal.nodefeature.ModelMap in project flow by vaadin.

the class ModelMapTest method resolveAndCreateImmediateChildMap.

@Test
public void resolveAndCreateImmediateChildMap() {
    ModelMap child = rootMap.resolveModelMap("child");
    ModelMap parent = getParentMapAndAssertMapping(child, "child");
    Assert.assertEquals(rootMap, parent);
}
Also used : ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) Test(org.junit.Test)

Example 38 with ModelMap

use of com.vaadin.flow.internal.nodefeature.ModelMap in project flow by vaadin.

the class ModelMapTest method resolveAndCreateImmediateChildList.

@Test
public void resolveAndCreateImmediateChildList() {
    ModelList child = rootMap.resolveModelList("child");
    ModelMap parent = getParentMapAndAssertMapping(child, "child");
    Assert.assertEquals(rootMap, parent);
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) Test(org.junit.Test)

Example 39 with ModelMap

use of com.vaadin.flow.internal.nodefeature.ModelMap in project flow by vaadin.

the class ModelValueBindingProvider method getValue.

@Override
public Object getValue(StateNode node) {
    ModelMap map = ModelMap.get(node);
    int dotLocation = key.lastIndexOf('.');
    if (dotLocation != -1) {
        String modelPath = key.substring(0, dotLocation);
        String modelKey = key.substring(dotLocation + 1);
        return map.resolveModelMap(modelPath).getValue(modelKey);
    } else {
        return map.getValue(key);
    }
}
Also used : ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap)

Example 40 with ModelMap

use of com.vaadin.flow.internal.nodefeature.ModelMap in project flow by vaadin.

the class BeanModelType method importProperties.

/**
 * Imports properties from a bean into a model map based on the properties
 * in this model type.
 *
 * @param model
 *            the model map to import values into
 * @param bean
 *            the bean to get values from
 * @param propertyFilter
 *            defines which properties from this model type to import
 */
public void importProperties(ModelMap model, Object bean, PropertyFilter propertyFilter) {
    Class<? extends Object> beanClass = bean.getClass();
    assert isBean(beanClass);
    /*
         * Collect all values and let getters throw before starting to populate
         * the model.
         *
         * Can't use Collectors.toMap() since it disallows null values.
         */
    Map<String, Object> values = new HashMap<>();
    properties.keySet().stream().filter(propertyFilter).map(name -> ReflectTools.getGetter(beanClass, name)).filter(Optional::isPresent).map(Optional::get).forEach(getter -> {
        String propertyName = ReflectTools.getPropertyName(getter);
        Type beanPropertyType = ReflectTools.getPropertyType(getter);
        ModelType modelPropertyType = getPropertyType(propertyName);
        if (!modelPropertyType.accepts(beanPropertyType)) {
            throw new IllegalArgumentException(propertyName + " is not of the expected type");
        }
        try {
            Object value = getter.invoke(bean);
            values.put(propertyName, value);
        } catch (Exception e) {
            throw new IllegalArgumentException("Cannot access bean property " + propertyName, e);
        }
    });
    // Populate the model with the extracted values
    values.forEach((name, value) -> {
        ModelType type = getPropertyType(name);
        model.setValue(name, type.applicationToModel(value, new PropertyFilter(propertyFilter, name)));
    });
}
Also used : BasicModelType(com.vaadin.flow.templatemodel.BasicModelType) TemplateModelUtil(com.vaadin.flow.templatemodel.TemplateModelUtil) StateNode(com.vaadin.flow.internal.StateNode) Predicate(java.util.function.Predicate) Json(elemental.json.Json) ReflectTools(com.vaadin.flow.internal.ReflectTools) HashMap(java.util.HashMap) Function(java.util.function.Function) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) TemplateElementStateProvider(com.vaadin.flow.dom.impl.TemplateElementStateProvider) Serializable(java.io.Serializable) InvalidTemplateModelException(com.vaadin.flow.templatemodel.InvalidTemplateModelException) PropertyFilter(com.vaadin.flow.templatemodel.PropertyFilter) JsonValue(elemental.json.JsonValue) ModelType(com.vaadin.flow.templatemodel.ModelType) Stream(java.util.stream.Stream) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Map(java.util.Map) Optional(java.util.Optional) JsonObject(elemental.json.JsonObject) Method(java.lang.reflect.Method) ComplexModelType(com.vaadin.flow.templatemodel.ComplexModelType) BasicModelType(com.vaadin.flow.templatemodel.BasicModelType) ModelType(com.vaadin.flow.templatemodel.ModelType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ComplexModelType(com.vaadin.flow.templatemodel.ComplexModelType) Optional(java.util.Optional) HashMap(java.util.HashMap) BasicModelType(com.vaadin.flow.templatemodel.BasicModelType) ModelType(com.vaadin.flow.templatemodel.ModelType) ComplexModelType(com.vaadin.flow.templatemodel.ComplexModelType) JsonObject(elemental.json.JsonObject) PropertyFilter(com.vaadin.flow.templatemodel.PropertyFilter) InvalidTemplateModelException(com.vaadin.flow.templatemodel.InvalidTemplateModelException)

Aggregations

ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)41 Test (org.junit.Test)30 StateNode (com.vaadin.flow.internal.StateNode)18 Bean (com.vaadin.flow.templatemodel.Bean)18 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)9 ModelType (com.vaadin.flow.templatemodel.ModelType)5 PropertyFilter (com.vaadin.flow.templatemodel.PropertyFilter)5 ArrayList (java.util.ArrayList)5 Collectors (java.util.stream.Collectors)5 Assert (org.junit.Assert)5 Arrays (java.util.Arrays)4 Date (java.util.Date)4 BeanContainingBeans (com.vaadin.flow.templatemodel.BeanContainingBeans)3 InvalidTemplateModelException (com.vaadin.flow.templatemodel.InvalidTemplateModelException)3 ReflectTools (com.vaadin.flow.internal.ReflectTools)2 NodeChange (com.vaadin.flow.internal.change.NodeChange)2 BasicModelType (com.vaadin.flow.templatemodel.BasicModelType)2 Serializable (java.io.Serializable)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClassList (com.vaadin.flow.dom.ClassList)1