aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/themes/ButtonsTest.java
blob: 551f9d8fbfe42218abd0441513db1e897eca5965 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.vaadin.tests.themes;

import com.vaadin.LegacyApplication;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.server.ThemeResource;
import com.vaadin.server.UserError;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Layout;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.UI;
import com.vaadin.ui.UI.LegacyWindow;

@SuppressWarnings("serial")
public class ButtonsTest extends com.vaadin.LegacyApplication {

    final UI.LegacyWindow main = new LegacyWindow("Button states & themes");

    CheckBox styleToggle;
    CheckBox iconToggle;
    CheckBox nativeToggle;
    CheckBox themeToggle;
    boolean largeIcons = false;
    boolean nativeButtons = false;

    final HorizontalLayout toggles = new HorizontalLayout();

    @Override
    public void init() {
        setMainWindow(main);
        setTheme("reindeer");

        themeToggle = new CheckBox("Runo theme");
        themeToggle.addListener(new ValueChangeListener() {

            @Override
            public void valueChange(ValueChangeEvent event) {
                if (getTheme() == "reindeer") {
                    setTheme("runo");
                } else {
                    setTheme("reindeer");
                }
            }
        });
        themeToggle.setStyleName("small");
        themeToggle.setImmediate(true);

        styleToggle = new CheckBox("Black style");
        styleToggle.addListener(new ValueChangeListener() {

            @Override
            public void valueChange(ValueChangeEvent event) {
                if (!main.getContent().getStyleName().contains("black")) {
                    main.getContent().setStyleName("black");
                } else {
                    main.getContent().setStyleName("");
                }
            }
        });
        styleToggle.setImmediate(true);
        styleToggle.setStyleName("small");

        iconToggle = new CheckBox("64x icons");
        iconToggle.addListener(new ValueChangeListener() {

            @Override
            public void valueChange(ValueChangeEvent event) {
                largeIcons = !largeIcons;
                recreateAll();
            }
        });
        iconToggle.setImmediate(true);
        iconToggle.setStyleName("small");

        nativeToggle = new CheckBox("Native buttons");
        nativeToggle.addListener(new ValueChangeListener() {

            @Override
            public void valueChange(ValueChangeEvent event) {
                nativeButtons = !nativeButtons;
                recreateAll();
            }
        });
        nativeToggle.setImmediate(true);
        nativeToggle.setStyleName("small");

        toggles.setSpacing(true);
        toggles.addComponent(themeToggle);
        toggles.addComponent(styleToggle);
        toggles.addComponent(iconToggle);
        toggles.addComponent(nativeToggle);
        main.addComponent(toggles);

        recreateAll();

    }

    private void recreateAll() {
        main.removeAllComponents();
        main.addComponent(toggles);

        main.addComponent(buildButtons(false, false, false, false));
        main.addComponent(buildButtons(false, false, true, false));
        main.addComponent(buildButtons(false, true, false, false));
        main.addComponent(buildButtons(false, true, true, false));
        main.addComponent(buildButtons(true, false, false, false));
        main.addComponent(buildButtons(true, false, true, false));
        main.addComponent(buildButtons(true, true, false, false));
        main.addComponent(buildButtons(true, true, true, false));

        main.addComponent(buildButtons(false, false, false, true));
        main.addComponent(buildButtons(false, false, true, true));
        main.addComponent(buildButtons(false, true, false, true));
        main.addComponent(buildButtons(false, true, true, true));
        main.addComponent(buildButtons(true, false, false, true));
        main.addComponent(buildButtons(true, false, true, true));
        main.addComponent(buildButtons(true, true, false, true));
        main.addComponent(buildButtons(true, true, true, true));

        final Button b = new Button("Tabindex");
        b.setTabIndex(1);
        main.addComponent(b);

        Button c = new Button("toggle enabled", new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                b.setEnabled(!b.isEnabled());
            }
        });
        main.addComponent(c);
    }

    private Layout buildButtons(boolean disabled, boolean icon, boolean error,
            boolean sized) {

        String[] buttonStyles = new String[] { "Normal", "Primary", "Small",
                "Link" };

        HorizontalLayout hl = new HorizontalLayout();
        hl.setSpacing(true);
        hl.setMargin(true);

        for (int i = 0; i < buttonStyles.length; i++) {
            Button b;
            if (nativeButtons) {
                b = new NativeButton(buttonStyles[i] + " style");
            } else {
                b = new Button(buttonStyles[i] + " style");
            }
            b.setStyleName(buttonStyles[i].toLowerCase());
            if (icon) {
                b.setIcon(new ThemeResource("../runo/icons/"
                        + (largeIcons ? "64" : "16") + "/document.png"));
            }
            if (error) {
                b.setComponentError(new UserError("Error"));
            }
            if (disabled) {
                b.setEnabled(false);
            }
            if (sized) {
                b.setWidth("250px");
                b.setCaption(b.getCaption() + " (250px)");
            }
            hl.addComponent(b);
        }

        return hl;
    }

}