Search in sources :

Example 21 with ModelMap

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

the class TemplateModelUtil method resolveListAndRun.

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

Example 22 with ModelMap

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

the class BeanModelTypeTest method importBean_withTypeFilter.

@Test
public void importBean_withTypeFilter() {
    BeanModelType<Bean> beanType = new BeanModelType<>(Bean.class, new PropertyFilter(name -> "intValue".equals(name)));
    ModelMap model = ModelMap.get(new StateNode(ModelMap.class));
    Bean bean = new Bean(3);
    beanType.importProperties(model, bean, PropertyFilter.ACCEPT_ALL);
    Assert.assertEquals(1, model.getKeys().count());
    Assert.assertEquals(Integer.valueOf(3), model.getValue("intValue"));
}
Also used : PropertyFilter(com.vaadin.flow.templatemodel.PropertyFilter) Arrays(java.util.Arrays) StateNode(com.vaadin.flow.internal.StateNode) Date(java.util.Date) Test(org.junit.Test) Bean(com.vaadin.flow.templatemodel.Bean) Assert(org.junit.Assert) Collectors(java.util.stream.Collectors) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) StateNode(com.vaadin.flow.internal.StateNode) PropertyFilter(com.vaadin.flow.templatemodel.PropertyFilter) Bean(com.vaadin.flow.templatemodel.Bean) Test(org.junit.Test)

Example 23 with ModelMap

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

the class BeanModelTypeTest method importBean_differentBean.

@Test
public void importBean_differentBean() {
    BeanModelType<Bean> beanType = new BeanModelType<>(Bean.class, PropertyFilter.ACCEPT_ALL);
    ModelMap model = ModelMap.get(new StateNode(ModelMap.class));
    DifferentBean bean = new DifferentBean(3);
    // Ignore intValue which has an incompatible type
    beanType.importProperties(model, bean, new PropertyFilter(name -> !"intValue".equals(name)));
    Assert.assertEquals(1, model.getKeys().count());
    Assert.assertEquals("3", model.getValue("string"));
    Assert.assertFalse(model.hasValue("date"));
}
Also used : PropertyFilter(com.vaadin.flow.templatemodel.PropertyFilter) Arrays(java.util.Arrays) StateNode(com.vaadin.flow.internal.StateNode) Date(java.util.Date) Test(org.junit.Test) Bean(com.vaadin.flow.templatemodel.Bean) Assert(org.junit.Assert) Collectors(java.util.stream.Collectors) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) StateNode(com.vaadin.flow.internal.StateNode) PropertyFilter(com.vaadin.flow.templatemodel.PropertyFilter) Bean(com.vaadin.flow.templatemodel.Bean) Test(org.junit.Test)

Example 24 with ModelMap

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

the class BeanModelTypeTest method applicationToModel.

@Test
public void applicationToModel() {
    BeanModelType<Bean> beanType = new BeanModelType<>(Bean.class, PropertyFilter.ACCEPT_ALL);
    Bean bean = new Bean(3);
    StateNode applicationToModel = beanType.applicationToModel(bean, PropertyFilter.ACCEPT_ALL);
    ModelMap model = ModelMap.get(applicationToModel);
    assertThreeBean(model);
}
Also used : ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) StateNode(com.vaadin.flow.internal.StateNode) Bean(com.vaadin.flow.templatemodel.Bean) Test(org.junit.Test)

Example 25 with ModelMap

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

the class TemplateModelTest method setModelPropertyAndVerifyGetter.

private void setModelPropertyAndVerifyGetter(AngularTemplate template, Supplier<Object> getter, String beanPath, String property, Serializable expected) {
    ModelMap feature = getModelMap(template, beanPath);
    feature.setValue(property, expected);
    Assert.assertEquals(expected, getter.get());
}
Also used : ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap)

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