Search in sources :

Example 6 with ModelList

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;
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) StateNode(com.vaadin.flow.internal.StateNode)

Example 7 with ModelList

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;
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) StateNode(com.vaadin.flow.internal.StateNode)

Example 8 with ModelList

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");
    }
}
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 9 with ModelList

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");
    }
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) StateNode(com.vaadin.flow.internal.StateNode) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap)

Example 10 with ModelList

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());
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) ArrayList(java.util.ArrayList) Bean(com.vaadin.flow.templatemodel.Bean) Test(org.junit.Test)

Aggregations

ModelList (com.vaadin.flow.internal.nodefeature.ModelList)14 Test (org.junit.Test)10 ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)8 ArrayList (java.util.ArrayList)6 StateNode (com.vaadin.flow.internal.StateNode)5 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)5 Bean (com.vaadin.flow.templatemodel.Bean)3 HashSet (java.util.HashSet)2 TestComponent (com.vaadin.flow.component.ComponentTest.TestComponent)1 UI (com.vaadin.flow.component.UI)1 Element (com.vaadin.flow.dom.Element)1 BeanContainingBeans (com.vaadin.flow.templatemodel.BeanContainingBeans)1 ModelType (com.vaadin.flow.templatemodel.ModelType)1 JsonObject (elemental.json.JsonObject)1 Serializable (java.io.Serializable)1