aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/layouts/layouttester/LayoutTesterApplication.java
blob: 2294b1909b706ad4edd2908e3fdd563c5b96a50d (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.vaadin.tests.layouts.layouttester;

import java.lang.reflect.Method;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.tests.components.AbstractTestCase;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Layout;
import com.vaadin.ui.LegacyWindow;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

@SuppressWarnings("serial")
public class LayoutTesterApplication extends AbstractTestCase {
    Button nextButton = new Button("Next");
    private int layoutIndex = -1;
    private int layoutCount = 1;

    private Method[] layoutGetters;
    private LegacyWindow mainWindow;
    private NativeSelect layoutSelector;

    @Override
    public void init() {
        mainWindow = new LegacyWindow("LayoutTesterApplication");
        setMainWindow(mainWindow);
        loadLayoutGetters();
        nextLaytout();

        nextButton.addListener(new Button.ClickListener() {
            private static final long serialVersionUID = -1577298910202253538L;

            @Override
            public void buttonClick(ClickEvent event) {
                nextLaytout();
            }
        });
    }

    private void nextLaytout() {
        try {
            mainWindow.removeAllComponents();
            HorizontalLayout vlo = new HorizontalLayout();
            vlo.setSpacing(true);
            ++layoutIndex;
            if (layoutIndex >= layoutCount) {
                layoutIndex = 0;
            }
            mainWindow.addComponent(vlo);
            vlo.addComponent(nextButton);
            vlo.addComponent(getLayoutTypeSelect());
            vlo.addComponent(new UndefWideLabel(layoutGetters[layoutIndex]
                    .getName()));

            Layout lo = null;
            if (layoutSelector.getValue() == VerticalLayout.class) {
                lo = getVerticalTestLayout(layoutIndex);
            } else if (layoutSelector.getValue() == HorizontalLayout.class) {
                lo = getHorizontalTestLayout(layoutIndex);
            } else if (layoutSelector.getValue() == GridLayout.class) {
                lo = getGridTestLayout(layoutIndex);
            }
            if (lo != null) {
                lo.addStyleName(Reindeer.LAYOUT_BLUE);
                mainWindow.addComponent(lo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public void loadLayoutGetters() {
        layoutGetters = AbstractLayoutTests.class.getDeclaredMethods();
        layoutCount = layoutGetters.length;
    }

    public Layout getVerticalTestLayout(int index) throws Exception {
        VerticalLayoutTests vlotest = new VerticalLayoutTests(this);
        return (Layout) layoutGetters[index].invoke(vlotest, (Object[]) null);
    }

    public Layout getHorizontalTestLayout(int index) throws Exception {
        HorizontalLayoutTests hlotest = new HorizontalLayoutTests(this);
        return (Layout) layoutGetters[index].invoke(hlotest, (Object[]) null);
    }

    public Layout getGridTestLayout(int index) throws Exception {
        GridLayoutTests hlotest = new GridLayoutTests(this);
        return (Layout) layoutGetters[index].invoke(hlotest, (Object[]) null);
    }

    private NativeSelect getLayoutTypeSelect() {
        if (layoutSelector == null) {
            layoutSelector = new NativeSelect();
            layoutSelector.addItem(VerticalLayout.class);
            layoutSelector.addItem(HorizontalLayout.class);
            layoutSelector.addItem(GridLayout.class);
            layoutSelector.setNullSelectionAllowed(false);
            layoutSelector.setImmediate(true);
            layoutSelector.select(VerticalLayout.class);
            layoutSelector.addListener(new Property.ValueChangeListener() {
                private static final long serialVersionUID = -605319614765838359L;

                @Override
                public void valueChange(ValueChangeEvent event) {
                    layoutIndex = -1;
                    nextLaytout();
                }
            });
        }
        return layoutSelector;
    }

    @Override
    protected String getDescription() {
        return "Test application for VerticalLayout, HorizontalLayout, and GridLayout";
    }

    @Override
    protected Integer getTicketNumber() {
        return 5334;
    }

}