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