aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/tickets/Ticket1435.java
blob: 9c8f400e1d1edad37249c339bf902e70b5650475 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package com.vaadin.tests.tickets;

import com.vaadin.server.LegacyApplication;
import com.vaadin.ui.AbstractOrderedLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.LegacyWindow;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

public class Ticket1435 extends LegacyApplication {

    private static final boolean useWorkaround = true;

    @Override
    public void init() {

        final LegacyWindow mainWin = new LegacyWindow(
                "ButtonPanel containing a table test");
        setMainWindow(mainWin);
        ((AbstractOrderedLayout) mainWin.getContent()).setSpacing(true);

        ButtonPanel dataCardView1 = buildButtonPanel("My Tickets");
        ButtonPanel dataCardView2 = buildButtonPanel("My Tickets 2");

        mainWin.addComponent(dataCardView1);
        mainWin.addComponent(dataCardView2);

    }

    /**
     * A ButtonPanel is a Panel, which has context specific Buttons in its
     * header.
     * 
     * ButtonPanel also provides buttons for controlling its visibility
     * (collapse/expand).
     */
    public class ButtonPanel extends CustomComponent {

        VerticalLayout root = new VerticalLayout();

        // In header are the panel's title and the control buttons.
        // Panel title is expanded by default.
        HorizontalLayout header = new HorizontalLayout();

        // This is where the actual data is put.
        VerticalLayout containerLayout = new VerticalLayout();
        Panel container = new Panel(containerLayout);

        // Last known height before the panel was collapsed
        private float lastHeight = -1;
        private Unit lastHeightUnit = null;

        public ButtonPanel(String labelString) {
            setCompositionRoot(root);
            root.setSizeFull();

            root.setStyleName("toolbarpanel");
            header.setStyleName("toolbar");

            initHeader(labelString);

            containerLayout.setMargin(true);

            initContainer();
        }

        private void initHeader(String labelString) {
            root.addComponent(header);
            header.setWidth("100%");
            header.setHeight("26px");
            Label label = new Label(labelString);
            label.setStyleName("caption");
            header.addComponent(label);

            final Layout buttonContainer;
            if (useWorkaround) {
                buttonContainer = header;

            } else {
                buttonContainer = new HorizontalLayout();
                header.addComponent(buttonContainer);

            }

            Button edit = new Button("Edit");
            edit.setStyleName("link");
            buttonContainer.addComponent(edit);

            Button copy = new Button("Copy");
            copy.setStyleName("link");
            buttonContainer.addComponent(copy);

            Button move = new Button("Move");
            move.setStyleName("link");
            buttonContainer.addComponent(move);

            Button delete = new Button("Delete");
            delete.setStyleName("link");
            buttonContainer.addComponent(delete);

            Button bind = new Button("Bind");
            bind.setStyleName("link");
            buttonContainer.addComponent(bind);

            Button options = new Button("Options...");
            options.setStyleName("link");
            buttonContainer.addComponent(options);

            final Button expand = new Button("Expand");

            final Button collapse = new Button("Collapse");
            buttonContainer.addComponent(collapse);

            collapse.setStyleName("collapse");
            collapse.addClickListener(new Button.ClickListener() {
                @Override
                public void buttonClick(Button.ClickEvent event) {
                    if (useWorkaround) {
                        container.setVisible(false);
                        lastHeight = root.getHeight();
                        lastHeightUnit = root.getHeightUnits();
                        root.setHeight("26px");
                        buttonContainer.replaceComponent(collapse, expand);
                    } else {
                        boolean visible = container.isVisible();
                        container.setVisible(!visible);
                        if (visible) {
                            lastHeight = root.getHeight();
                            lastHeightUnit = root.getHeightUnits();
                            root.setHeight("26px");
                        } else {
                            root.setHeight(lastHeight, lastHeightUnit);
                        }
                        event.getButton().setCaption(
                                visible ? "Expand" : "Collapse");
                    }
                }
            });

            if (useWorkaround) {
                expand.addClickListener(new Button.ClickListener() {

                    @Override
                    public void buttonClick(ClickEvent event) {
                        container.setVisible(true);
                        root.setHeight(lastHeight, lastHeightUnit);
                        buttonContainer.replaceComponent(expand, collapse);
                    }
                });
            }

        }

        private void initContainer() {
            container.setStyleName("custompanel");
            container.setSizeFull();
            containerLayout.setMargin(false);
            containerLayout.setSizeFull();
            root.addComponent(container);
            root.setExpandRatio(container, 1);
        }

        public void setHeight(int height, Unit unit) {
            root.setHeight(height, unit);
            lastHeight = height;
            lastHeightUnit = unit;
            container.setHeight("100%");
        }

        @Override
        public void setHeight(String height) {
            root.setHeight(height);
            lastHeight = root.getHeight();
            lastHeightUnit = root.getHeightUnits();
            container.setHeight("100%");
        }

        @Override
        public void setWidth(String width) {
            root.setWidth(width);
        }

        public void setWidth(int width, Unit unit) {
            root.setWidth(width, unit);
        }

        @Override
        public void setSizeFull() {
            setWidth("100%");
            setHeight("100%");
        }

        public void setPanelComponent(Component component) {
            containerLayout.removeAllComponents();
            containerLayout.addComponent(component);
        }
    }

    public ButtonPanel buildButtonPanel(String caption) {
        ButtonPanel panel = new ButtonPanel(caption);

        panel.setHeight("250px");
        panel.setWidth("500px");

        Table table = new Table();

        table.setSizeFull();

        table.addContainerProperty("checkbox", CheckBox.class, new CheckBox());
        table.setColumnWidth("checkbox", 30);
        table.setColumnHeader("checkbox", "");

        table.addContainerProperty("Tickets", String.class, null);
        table.setColumnWidth("Tickets", 150);

        table.addContainerProperty("Deadline", String.class, null);

        for (int i = 0; i < 10; i++) {
            String name = "Name " + i;
            table.addItem(new Object[] { new CheckBox(), name,
                    "02-22-2007 13:37" }, new Integer(i));
        }

        panel.setPanelComponent(table);

        return panel;
    }
}