Search in sources :

Example 31 with ElementPropertyMap

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

the class TemplateModelUtil method resolveBeanAndRun.

/**
 * Resolves a bean model type and model map based on a model instance and
 * passes those values to the provided callback.
 *
 * @param <R>
 *            the return type
 * @param model
 *            the model instance for which to resolve a type and a map, not
 *            <code>null</code>
 * @param modelPath
 *            the model path to resolve, not <code>null</code>
 * @param callback
 *            the callback to run with the resolved bean type and the model
 *            map, not <code>null</code>
 * @return the value returned by the callback
 */
public static <R> R resolveBeanAndRun(TemplateModel model, String modelPath, BiFunction<BeanModelType<?>, ElementPropertyMap, R> callback) {
    assert model != null;
    assert modelPath != null;
    assert callback != null;
    BeanModelType<?> modelType = TemplateModelProxyHandler.getModelTypeForProxy(model);
    ModelType beanType = modelType.resolveType(modelPath);
    if (beanType instanceof BeanModelType<?>) {
        StateNode stateNode = TemplateModelProxyHandler.getStateNodeForProxy(model);
        ElementPropertyMap modelMap = ElementPropertyMap.getModel(stateNode);
        ElementPropertyMap beanMap = modelMap.resolveModelMap(modelPath);
        return callback.apply((BeanModelType<?>) beanType, beanMap);
    } else {
        throw new IllegalArgumentException(modelPath + " does not resolve to a bean");
    }
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap)

Example 32 with ElementPropertyMap

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

the class TemplateModelWithEncodersTest method brokenModelType_throws.

@Test(expected = IllegalArgumentException.class)
public void brokenModelType_throws() {
    TemplateWithDate template = new TemplateWithDate();
    StateNode node = template.getElement().getNode();
    ElementPropertyMap map = node.getFeature(ElementPropertyMap.class).resolveModelMap("date");
    map.setProperty("day", "foo");
    template.getModel().getDate();
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 33 with ElementPropertyMap

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

the class TemplateModelTest method testBeanInModel.

@Test
public void testBeanInModel() {
    BeanModelTemplate template = new BeanModelTemplate();
    BeanModel model = template.getModel();
    AtomicInteger beanTriggered = new AtomicInteger();
    Bean bean = new BeanImpl(beanTriggered);
    bean.setString("foobar");
    StateNode stateNode = (StateNode) template.getElement().getNode().getFeature(ElementPropertyMap.class).getProperty("bean");
    Assert.assertEquals(0, beanTriggered.get());
    model.setBean(bean);
    stateNode = (StateNode) template.getElement().getNode().getFeature(ElementPropertyMap.class).getProperty("bean");
    // enough to verify that TemplateModelBeanUtil.importBeanIntoModel is
    // triggered, since TemplatemodelBeanUtilTests covers the bean import
    Assert.assertNotNull(stateNode);
    Assert.assertEquals(1, beanTriggered.get());
    ElementPropertyMap modelMap = ElementPropertyMap.getModel(stateNode);
    Assert.assertNotNull(modelMap);
    Assert.assertEquals("foobar", modelMap.getProperty("string"));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StateNode(com.vaadin.flow.internal.StateNode) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 34 with ElementPropertyMap

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

the class TemplateModelTest method setListWithExclude.

@Test
public void setListWithExclude() {
    TemplateWithExcludeOnList template = new TemplateWithExcludeOnList();
    List<Bean> beans = new ArrayList<>();
    beans.add(new Bean(1));
    beans.add(new Bean(2));
    template.getModel().setBeans(beans);
    ModelList modelList = getModelList(template, "beans");
    ElementPropertyMap bean1 = ElementPropertyMap.getModel(modelList.get(0));
    ElementPropertyMap bean2 = ElementPropertyMap.getModel(modelList.get(1));
    Set<String> bean1InMap = getKeys(bean1);
    Set<String> bean2InMap = getKeys(bean2);
    Assert.assertFalse("Bean1 in model should not have an 'intValue' property", bean1InMap.contains("intValue"));
    Assert.assertFalse("Bean2 in model should not have an 'intValue' property", bean2InMap.contains("intValue"));
    Assert.assertEquals("All other properties should have been included", 6, bean1InMap.size());
    Assert.assertEquals("All other properties should have been included", 6, bean2InMap.size());
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) ArrayList(java.util.ArrayList) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 35 with ElementPropertyMap

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

the class TemplateModelTest method includeExcludeOverrideInSubclass.

@Test
public void includeExcludeOverrideInSubclass() {
    TemplateWithExcludeAndIncludeSubclassOverrides template = new TemplateWithExcludeAndIncludeSubclassOverrides();
    template.getModel().setBean(new Bean(123));
    ElementPropertyMap modelMap = getModelMap(template, "bean");
    assertTrue(modelMap.hasProperty("doubleValue"));
    Assert.assertEquals(1, modelMap.getPropertyNames().count());
}
Also used : ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Aggregations

ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)45 Test (org.junit.Test)35 StateNode (com.vaadin.flow.internal.StateNode)21 HashSet (java.util.HashSet)9 Element (com.vaadin.flow.dom.Element)8 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)6 Json (elemental.json.Json)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 Assert (org.junit.Assert)6 ElementChildrenList (com.vaadin.flow.internal.nodefeature.ElementChildrenList)5 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)5 JsonObject (elemental.json.JsonObject)5 Serializable (java.io.Serializable)5 Arrays (java.util.Arrays)5 UI (com.vaadin.flow.component.UI)4 JsonCodec (com.vaadin.flow.internal.JsonCodec)4 JsonArray (elemental.json.JsonArray)4