Search in sources :

Example 1 with ComponentEventData

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);
}
Also used : ComponentEventData(com.vaadin.generator.metadata.ComponentEventData)

Example 2 with ComponentEventData

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)"));
}
Also used : ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) Test(org.junit.Test)

Example 3 with ComponentEventData

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())) {"));
}
Also used : ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) Test(org.junit.Test)

Example 4 with ComponentEventData

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() {"));
}
Also used : ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) Test(org.junit.Test)

Example 5 with ComponentEventData

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() + ";"));
}
Also used : Pattern(java.util.regex.Pattern) ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) Matcher(java.util.regex.Matcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Aggregations

ComponentEventData (com.vaadin.generator.metadata.ComponentEventData)17 Test (org.junit.Test)14 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)12 ComponentPropertyData (com.vaadin.generator.metadata.ComponentPropertyData)10 ComponentPropertyBaseData (com.vaadin.generator.metadata.ComponentPropertyBaseData)4 Matcher (java.util.regex.Matcher)4 Pattern (java.util.regex.Pattern)4 ComponentGenerator (com.vaadin.generator.ComponentGenerator)3 ComponentMetadata (com.vaadin.generator.metadata.ComponentMetadata)3 ComponentObjectType (com.vaadin.generator.metadata.ComponentObjectType)2 EventData (com.vaadin.flow.component.EventData)1 ComponentObjectTypeInnerType (com.vaadin.generator.metadata.ComponentObjectType.ComponentObjectTypeInnerType)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)1 Before (org.junit.Before)1