Search in sources :

Example 6 with ModelType

use of com.vaadin.flow.templatemodel.ModelType in project flow by vaadin.

the class PolymerPublishedEventRpcHandler method getTemplateItem.

/**
 * Get the template model object and type.
 *
 * @param template
 *            polymer template to get model from
 * @param argValue
 *            argument value
 * @param convertedType
 *            value type
 * @return the provided model value
 * @throws IllegalStateException
 *             if the component is not attached to the UI
 */
@Override
public Object getTemplateItem(Component template, JsonObject argValue, Type convertedType) {
    final Optional<UI> ui = template.getUI();
    if (ui.isPresent()) {
        StateNode node = ui.get().getInternals().getStateTree().getNodeById((int) argValue.getNumber("nodeId"));
        ModelType propertyType = ((PolymerTemplate<?>) template).getModelType(convertedType);
        return propertyType.modelToApplication(node);
    }
    throw new IllegalArgumentException("Event sent for a non attached template component");
}
Also used : UI(com.vaadin.flow.component.UI) StateNode(com.vaadin.flow.internal.StateNode) ModelType(com.vaadin.flow.templatemodel.ModelType) PolymerTemplate(com.vaadin.flow.component.polymertemplate.PolymerTemplate)

Example 7 with ModelType

use of com.vaadin.flow.templatemodel.ModelType in project flow by vaadin.

the class PolymerTemplate method isSupportedClass.

/**
 * Check if the given Class {@code type} is found in the Model.
 *
 * @param type
 *            Class to check support for
 * @return True if supported by this PolymerTemplate
 */
public boolean isSupportedClass(Class<?> type) {
    List<ModelType> modelTypes = ModelDescriptor.get(getModelType()).getPropertyNames().map(this::getModelType).collect(Collectors.toList());
    boolean result = false;
    for (ModelType modelType : modelTypes) {
        if (type.equals(modelType.getJavaType())) {
            result = true;
        } else if (modelType instanceof ListModelType) {
            result = checkListType(type, modelType);
        }
        if (result) {
            break;
        }
    }
    return result;
}
Also used : ListModelType(com.vaadin.flow.templatemodel.ListModelType) BeanModelType(com.vaadin.flow.templatemodel.BeanModelType) ListModelType(com.vaadin.flow.templatemodel.ListModelType) ModelType(com.vaadin.flow.templatemodel.ModelType)

Example 8 with ModelType

use of com.vaadin.flow.templatemodel.ModelType in project flow by vaadin.

the class ModelDescriptorTest method listInsideListInsideList.

@Test
public void listInsideListInsideList() {
    ModelDescriptor<?> descriptor = ModelDescriptor.get(ListInsideListInsideList.class);
    Assert.assertEquals(1, descriptor.getPropertyNames().count());
    ListModelType<?> listPropertyType = (ListModelType<?>) descriptor.getPropertyType("beans");
    Type javaType = listPropertyType.getJavaType();
    Assert.assertTrue("Expected instanceof ParameterizedType for List", javaType instanceof ParameterizedType);
    javaType = ((ParameterizedType) javaType).getActualTypeArguments()[0];
    Assert.assertTrue("Expected instanceof ParameterizedType for List in List", javaType instanceof ParameterizedType);
    javaType = ((ParameterizedType) javaType).getActualTypeArguments()[0];
    Assert.assertTrue("Expected instanceof ParameterizedType for List in List in List", javaType instanceof ParameterizedType);
    Assert.assertEquals(Bean.class, ((ParameterizedType) javaType).getActualTypeArguments()[0]);
    Assert.assertTrue(listPropertyType.getItemType() instanceof ListModelType<?>);
    ListModelType<?> type = (ListModelType<?>) listPropertyType.getItemType();
    Assert.assertTrue(type.getItemType() instanceof ListModelType<?>);
    type = (ListModelType<?>) type.getItemType();
    Assert.assertTrue(type.getItemType() instanceof BeanModelType<?>);
    BeanModelType<?> modelType = (BeanModelType<?>) type.getItemType();
    Assert.assertSame(Bean.class, modelType.getProxyType());
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) BasicModelType(com.vaadin.flow.templatemodel.BasicModelType) ListModelType(com.vaadin.flow.template.angular.model.ListModelType) ModelType(com.vaadin.flow.templatemodel.ModelType) BeanModelType(com.vaadin.flow.template.angular.model.BeanModelType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ListModelType(com.vaadin.flow.template.angular.model.ListModelType) BeanModelType(com.vaadin.flow.template.angular.model.BeanModelType) Test(org.junit.Test)

Example 9 with ModelType

use of com.vaadin.flow.templatemodel.ModelType in project flow by vaadin.

the class JsExpressionBindingProviderTest method jsExpressionWithSubProperty.

@Test
public void jsExpressionWithSubProperty() {
    JsExpressionBindingProvider binding = new JsExpressionBindingProvider("bean.property");
    StateNode beanNode = new StateNode(ModelMap.class);
    ModelMap beanModel = ModelMap.get(beanNode);
    beanModel.setValue("property", "foo");
    StateNode rootNode = new StateNode(TemplateMap.class, ModelMap.class);
    Map<String, ModelType> beanProperties = Collections.singletonMap("property", BasicModelType.get(String.class).get());
    Map<String, ModelType> modelProperties = Collections.singletonMap("bean", new BeanModelType<>(Object.class, beanProperties));
    rootNode.getFeature(TemplateMap.class).setModelDescriptor(new TestModelDescriptor(modelProperties));
    ModelMap.get(rootNode).setValue("bean", beanNode);
    Assert.assertEquals("foo", binding.getValue(rootNode));
}
Also used : TemplateMap(com.vaadin.flow.internal.nodefeature.TemplateMap) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) StateNode(com.vaadin.flow.internal.StateNode) JsExpressionBindingProvider(com.vaadin.flow.template.angular.JsExpressionBindingProvider) BasicModelType(com.vaadin.flow.templatemodel.BasicModelType) ModelType(com.vaadin.flow.templatemodel.ModelType) BeanModelType(com.vaadin.flow.template.angular.model.BeanModelType) TestModelDescriptor(com.vaadin.flow.template.angular.model.TestModelDescriptor) Test(org.junit.Test)

Example 10 with ModelType

use of com.vaadin.flow.templatemodel.ModelType in project flow by vaadin.

the class PublishedServerEventHandlerRpcHandler method getTemplateItem.

private static Object getTemplateItem(PolymerTemplate<?> template, JsonObject argValue, Type convertedType) {
    StateNode node = template.getUI().get().getInternals().getStateTree().getNodeById((int) argValue.getNumber("nodeId"));
    ModelType propertyType = template.getModelType(convertedType);
    return propertyType.modelToApplication(node);
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) ModelType(com.vaadin.flow.templatemodel.ModelType)

Aggregations

ModelType (com.vaadin.flow.templatemodel.ModelType)16 StateNode (com.vaadin.flow.internal.StateNode)7 BasicModelType (com.vaadin.flow.templatemodel.BasicModelType)6 ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)5 BeanModelType (com.vaadin.flow.templatemodel.BeanModelType)4 ListModelType (com.vaadin.flow.templatemodel.ListModelType)4 ParameterizedType (java.lang.reflect.ParameterizedType)4 Type (java.lang.reflect.Type)4 BeanModelType (com.vaadin.flow.template.angular.model.BeanModelType)3 ComplexModelType (com.vaadin.flow.templatemodel.ComplexModelType)3 InvalidTemplateModelException (com.vaadin.flow.templatemodel.InvalidTemplateModelException)3 Test (org.junit.Test)3 UI (com.vaadin.flow.component.UI)2 PolymerTemplate (com.vaadin.flow.component.polymertemplate.PolymerTemplate)2 ListModelType (com.vaadin.flow.template.angular.model.ListModelType)2 TemplateElementStateProvider (com.vaadin.flow.dom.impl.TemplateElementStateProvider)1 ReflectTools (com.vaadin.flow.internal.ReflectTools)1 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)1 TemplateMap (com.vaadin.flow.internal.nodefeature.TemplateMap)1 JsExpressionBindingProvider (com.vaadin.flow.template.angular.JsExpressionBindingProvider)1