aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/components/HasValueRequiredIndicator.java
blob: 8afc0577ee39a9df782f4b7658381eab6fafffe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.vaadin.tests.components;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.googlecode.gentyref.GenericTypeReflector;
import com.vaadin.data.HasValue;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.AbsoluteLayout.ComponentPosition;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Layout;
import com.vaadin.ui.VerticalLayout;

/**
 * @author Vaadin Ltd
 *
 */
public abstract class HasValueRequiredIndicator<C extends HasValue<?> & Component>
        extends AbstractTestUI {

    @Override
    protected void setup(VaadinRequest request) {
        getContent().setSizeFull();
        getVaadinLayouts().stream().map(this::createLayout).forEach(layout -> {
            addComponent(layout, createComponent());
            addComponent(layout);
        });
    }

    protected void addComponent(Layout layout, C component) {
        layout.addComponent(component);
        if (layout instanceof AbsoluteLayout) {
            AbsoluteLayout absLayout = (AbsoluteLayout) layout;
            ComponentPosition position = absLayout.new ComponentPosition();
            position.setTop(30f, Unit.PIXELS);
            absLayout.setPosition(component, position);
        }
    }

    protected Layout createLayout(Class<? extends Layout> clazz) {
        try {
            Layout layout = clazz.newInstance();
            if (clazz.equals(AbsoluteLayout.class)) {
                layout.setWidth("100px");
                layout.setHeight("150px");
            }
            layout.addStyleName("vaadin-layout");
            return layout;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    protected C createComponent() {
        Type type = GenericTypeReflector.getTypeParameter(getClass(),
                HasValueRequiredIndicator.class.getTypeParameters()[0]);
        if (type instanceof ParameterizedType) {
            type = ((ParameterizedType) type).getRawType();
        }

        if (type instanceof Class<?>) {
            Class<?> clazz = (Class<?>) type;
            try {
                @SuppressWarnings("unchecked")
                C component = (C) clazz.newInstance();
                initValue(component);
                component.setRequiredIndicatorVisible(true);
                component.addStyleName("test-component");
                return component;
            } catch (InstantiationException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        } else {
            throw new IllegalStateException(
                    "Cannot infer component type " + type.getTypeName());
        }
    }

    /**
     * Initialize value for the {@code component}.
     *
     * @param component
     *            a UI component
     */
    protected abstract void initValue(C component);

    private static List<Class<? extends Layout>> getVaadinLayouts() {
        List<Class<? extends Layout>> layouts = new ArrayList<>();
        layouts.add(AbsoluteLayout.class);
        layouts.add(VerticalLayout.class);
        layouts.add(HorizontalLayout.class);
        layouts.add(FormLayout.class);
        layouts.add(CssLayout.class);
        layouts.add(GridLayout.class);
        return layouts;
    }

}