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