use of com.vaadin.generator.metadata.ComponentEventData in project flow by vaadin.
the class ComponentGenerator method generateEventForPropertyWithNotify.
private void generateEventForPropertyWithNotify(ComponentMetadata metadata, ComponentPropertyData property) {
String eventName = ComponentGeneratorUtils.convertCamelCaseToHyphens(property.getName()) + "-changed";
List<ComponentEventData> events = metadata.getEvents();
if (events == null) {
events = new ArrayList<>();
metadata.setEvents(events);
}
if (events.stream().anyMatch(event -> event.getName().equals(eventName))) {
return;
}
ComponentEventData event = new ComponentEventData();
event.setName(eventName);
event.setDescription(String.format("Event fired every time the `%s` property is changed.", property.getName()));
event.setProperties(Collections.singletonList(property));
events.add(event);
}
use of com.vaadin.generator.metadata.ComponentEventData in project flow by vaadin.
the class ComponentGeneratorTest method componentContainsValueProperty_generatedClassImplementsHasValue.
@Test
public void componentContainsValueProperty_generatedClassImplementsHasValue() {
ComponentPropertyData property = new ComponentPropertyData();
property.setName("value");
property.setType(Collections.singleton(ComponentBasicType.STRING));
componentMetadata.setProperties(Collections.singletonList(property));
ComponentEventData event = new ComponentEventData();
event.setName("value-changed");
componentMetadata.setEvents(Collections.singletonList(event));
String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
generatedClass = ComponentGeneratorTestUtils.removeIndentation(generatedClass);
ComponentGeneratorTestUtils.assertClassImplementsInterface(generatedClass, "MyComponent", HasValue.class);
Assert.assertThat(generatedClass, CoreMatchers.containsString("@Override public String getValue()"));
Assert.assertThat(generatedClass, CoreMatchers.containsString("@Override public void setValue(String value)"));
}
use of com.vaadin.generator.metadata.ComponentEventData in project flow by vaadin.
the class ComponentGeneratorTest method componentContainsNumberValueProperty_generatedSetValuesPreventSettingTheSameValue.
@Test
public void componentContainsNumberValueProperty_generatedSetValuesPreventSettingTheSameValue() {
ComponentPropertyData property = new ComponentPropertyData();
property.setName("value");
property.setType(Collections.singleton(ComponentBasicType.NUMBER));
componentMetadata.setProperties(Collections.singletonList(property));
ComponentEventData event = new ComponentEventData();
event.setName("value-changed");
componentMetadata.setEvents(Collections.singletonList(event));
String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
generatedClass = ComponentGeneratorTestUtils.removeIndentation(generatedClass);
Assert.assertTrue(generatedClass.contains("@Override public void setValue(Double value) { Objects.requireNonNull(value, \"MyComponent value must not be null\"); if (!Objects.equals(value, getValue())) {"));
Assert.assertTrue(generatedClass.contains("public void setValue(Number value) { Objects.requireNonNull(value, \"MyComponent value must not be null\"); if (!Objects.equals(value, getValue())) {"));
}
use of com.vaadin.generator.metadata.ComponentEventData in project flow by vaadin.
the class ComponentGeneratorTest method classContainsGetterAndRelatedChangeEvent_getterContainsSynchronizeAnnotation.
@Test
public void classContainsGetterAndRelatedChangeEvent_getterContainsSynchronizeAnnotation() {
ComponentPropertyData property = new ComponentPropertyData();
property.setName("someproperty");
property.setType(Collections.singleton(ComponentBasicType.STRING));
componentMetadata.setProperties(Collections.singletonList(property));
ComponentEventData event = new ComponentEventData();
event.setName("someproperty-changed");
componentMetadata.setEvents(Collections.singletonList(event));
String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
generatedClass = ComponentGeneratorTestUtils.removeIndentation(generatedClass);
Assert.assertTrue("Wrong getter definition. It should contains @Synchronize(property = \"somepropery\", value = \"someproperty-changed\")", generatedClass.contains("@Synchronize(property = \"someproperty\", value = \"someproperty-changed\") " + "public String getSomeproperty() {"));
}
use of com.vaadin.generator.metadata.ComponentEventData in project flow by vaadin.
the class ComponentGeneratorTest method generateClassWithEvent_classTypedComponentEvent.
@Test
public void generateClassWithEvent_classTypedComponentEvent() {
ComponentEventData eventData = new ComponentEventData();
eventData.setName("change");
eventData.setDescription("Component change event.");
componentMetadata.setEvents(Collections.singletonList(eventData));
String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
generatedClass = ComponentGeneratorTestUtils.removeIndentation(generatedClass);
Assert.assertTrue("Custom event class was not found.", generatedClass.contains("public static class ChangeEvent<R extends MyComponent<R>> extends ComponentEvent<R> {"));
Assert.assertTrue("No DomEvent annotation found", generatedClass.contains("@DomEvent(\"change\")"));
// Using matcher as the formatter may cut the method.
Pattern pattern = Pattern.compile("addChangeListener\\((\\w?)(\\s*?)ComponentEventListener<ChangeEvent<R>> listener\\)");
Matcher matcher = pattern.matcher(generatedClass);
Assert.assertTrue("Couldn't find correct listener for event.", matcher.find());
Assert.assertTrue("Missing DomEvent import", generatedClass.contains("import " + DomEvent.class.getName() + ";"));
Assert.assertTrue("Missing ComponentEvent import", generatedClass.contains("import " + ComponentEvent.class.getName() + ";"));
Assert.assertTrue("Missing ComponentEventListener import", generatedClass.contains("import " + ComponentEventListener.class.getName() + ";"));
Assert.assertFalse("EventData imported even without events", generatedClass.contains("import " + EventData.class.getName() + ";"));
}
Aggregations