Search in sources :

Example 16 with ModelMap

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

the class ModelMapTest method hasValue.

@Test
public void hasValue() {
    ModelMap map = new ModelMap(new StateNode());
    Assert.assertFalse(map.hasValue("foo"));
    map.setValue("foo", "bar");
    Assert.assertTrue(map.hasValue("foo"));
    map.remove("foo");
    Assert.assertFalse(map.hasValue("foo"));
}
Also used : ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test)

Example 17 with ModelMap

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

the class ModelMapTest method getParentMapAndAssertMapping.

private ModelMap getParentMapAndAssertMapping(ModelList child, String childKey) {
    assert child != null;
    assert child.getNode() != null;
    ModelMap parentMap = ModelMap.get(child.getNode().getParent());
    assert parentMap != null;
    Assert.assertEquals(child.getNode(), parentMap.getValue(childKey));
    return parentMap;
}
Also used : ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap)

Example 18 with ModelMap

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

the class ModelMapTest method resolveAndCreateSubSubChildList.

@Test
public void resolveAndCreateSubSubChildList() {
    ModelList child = rootMap.resolveModelList("grand.parent.child");
    ModelMap parent = getParentMapAndAssertMapping(child, "child");
    ModelMap grand = getParentMapAndAssertMapping(parent, "parent");
    ModelMap resolvedRoot = getParentMapAndAssertMapping(grand, "grand");
    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 19 with ModelMap

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

the class ForElementTemplateNodeTest method createModel.

@SuppressWarnings({ "unchecked" })
@SafeVarargs
private final StateNode createModel(Element templateRootElement, String key, Map<String, String>... map) {
    StateNode modelList = new StateNode(new Class[] { ModelList.class });
    for (Map<String, String> item : map) {
        StateNode modelItem = TemplateElementStateProvider.createSubModelNode(ModelMap.class);
        ModelMap modelItemMap = modelItem.getFeature(ModelMap.class);
        for (String itemKey : item.keySet()) {
            modelItemMap.setValue(itemKey, item.get(itemKey));
        }
        modelList.getFeature(ModelList.class).add(modelItem);
    }
    StateNode node = templateRootElement.getNode();
    ModelMap.get(node).setValue(key, modelList);
    return node;
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) StateNode(com.vaadin.flow.internal.StateNode)

Example 20 with ModelMap

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

the class TemplateModelProxyHandler method intercept.

/**
 * Processes a method invocation on a Byte buddy proxy instance and returns
 * the result. This method will be invoked on an invocation handler when a
 * method is invoked on a proxy instance that it is associated with.
 *
 * @param target
 *            the proxy instance
 * @param method
 *            the {@code Method} instance corresponding to the proxied
 *            method invoked on the proxy instance.
 *
 * @param args
 *            an array of objects containing the values of the arguments
 *            passed in the method invocation on the proxy instance.
 * @return the value to return from the method invocation on the proxy
 *         instance.
 */
@RuntimeType
@SuppressWarnings("static-method")
public Object intercept(@This Object target, @Origin Method method, @AllArguments Object[] args) {
    String propertyName = ReflectTools.getPropertyName(method);
    BeanModelType<?> modelType = getModelTypeForProxy(target);
    if (!modelType.hasProperty(propertyName)) {
        throw new InvalidTemplateModelException(modelType.getProxyType().getName() + " has no property named " + propertyName + " (or it has been excluded)");
    }
    ModelType propertyType = modelType.getPropertyType(propertyName);
    ModelMap modelMap = ModelMap.get(getStateNodeForProxy(target));
    if (ReflectTools.isGetter(method)) {
        return handleGetter(modelMap, propertyName, propertyType);
    } else if (ReflectTools.isSetter(method)) {
        Object value = args[0];
        handleSetter(modelMap, propertyName, propertyType, value);
        return null;
    }
    throw new InvalidTemplateModelException(getUnsupportedMethodMessage(method, args));
}
Also used : InvalidTemplateModelException(com.vaadin.flow.templatemodel.InvalidTemplateModelException) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) ModelType(com.vaadin.flow.templatemodel.ModelType) RuntimeType(net.bytebuddy.implementation.bind.annotation.RuntimeType)

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