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");
}
}
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();
}
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"));
}
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());
}
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());
}
Aggregations