use of com.vaadin.flow.internal.nodefeature.ModelList 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;
}
use of com.vaadin.flow.internal.nodefeature.ModelList in project flow by vaadin.
the class TemplateModelListProxy method indexOf.
@Override
public int indexOf(Object object) {
if (object == null) {
return -1;
}
if (!TemplateModelProxyHandler.isProxy(object)) {
throw new IllegalArgumentException("Only proxy objects can be used together with proxy lists");
}
StateNode node = TemplateModelProxyHandler.getStateNodeForProxy(object);
ModelList modelList = getModelList();
int size = modelList.size();
for (int i = 0; i < size; i++) {
if (modelList.get(i).equals(node)) {
return i;
}
}
return -1;
}
use of com.vaadin.flow.internal.nodefeature.ModelList 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");
}
}
use of com.vaadin.flow.internal.nodefeature.ModelList 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 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);
ElementPropertyMap modelMap = ElementPropertyMap.getModel(stateNode);
ModelList modelList = modelMap.resolveModelList(modelPath);
return callback.apply((ListModelType<?>) listType, modelList);
} else {
throw new IllegalArgumentException(modelPath + " does not resolve to a list");
}
}
use of com.vaadin.flow.internal.nodefeature.ModelList 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");
ModelMap bean1 = ModelMap.get(modelList.get(0));
ModelMap bean2 = ModelMap.get(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());
}
Aggregations