aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/components/button/ButtonWithShortcutNotRendered.java
blob: f866928054a0f066253306737b3e74f14e0f0ec7 (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
package com.vaadin.tests.components.button;

import com.vaadin.annotations.PreserveOnRefresh;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalSplitPanel;

@PreserveOnRefresh
@SuppressWarnings("deprecation")
public class ButtonWithShortcutNotRendered extends AbstractTestUI {

    @Override
    protected String getTestDescription() {
        return "Button with shortcut broken if the shortcut is rendered before the button";
    }

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

    Button defaultButton;
    Button otherButton;

    @SuppressWarnings("unchecked")
    @Override
    protected void setup(VaadinRequest request) {
        getLayout().setWidth(100, Unit.PERCENTAGE);
        getLayout().setHeight(null);
        getLayout().setMargin(new MarginInfo(true, false, false, false));

        HierarchicalContainer container = new HierarchicalContainer();
        container.addContainerProperty("t1", String.class, "");
        container.addContainerProperty("t2", String.class, "");

        Item item = container.addItem("r1");
        item.getItemProperty("t1").setValue("Row1 t1");
        item.getItemProperty("t2").setValue("Row1 t2");

        item = container.addItem("r2");
        item.getItemProperty("t1").setValue("Row2 t1");
        item.getItemProperty("t2").setValue("Row2 t2");

        MyForm form = new MyForm();
        MyTable table = new MyTable(form, container);

        VerticalSplitPanel splitPanel = new VerticalSplitPanel();
        splitPanel.setWidth(100, Unit.PERCENTAGE);
        splitPanel.setHeight(400, Unit.PIXELS);
        splitPanel.setFirstComponent(table);
        splitPanel.setSecondComponent(form);
        splitPanel.setSplitPosition(50, Unit.PERCENTAGE);

        addComponent(splitPanel);
    }

    public class MyTable extends Table {
        final MyForm form;

        public MyTable(MyForm pform, HierarchicalContainer container) {
            form = pform;
            setContainerDataSource(container);
            setSelectable(true);
            setImmediate(true);
            setWidth(100, Unit.PERCENTAGE);
            setHeight(null);

            addValueChangeListener(new Property.ValueChangeListener() {

                @Override
                public void valueChange(
                        com.vaadin.data.Property.ValueChangeEvent event) {
                    final Item item = getItem(getValue());
                    form.setItemDataSource(item);
                }

            });
        }
    }

    public class MyForm extends Form implements ClickListener {
        final GridLayout layout;

        @SuppressWarnings("unchecked")
        public MyForm() {
            setWidth(100, Unit.PERCENTAGE);
            setHeight(null);
            setImmediate(true);

            setFormFieldFactory(new DefaultFieldFactory() {

                @Override
                public TextField createField(Item item, Object propertyId,
                        Component uiContext) {
                    TextField t = new TextField();
                    t.setWidth(100, Unit.PERCENTAGE);
                    t.setHeight(null);
                    t.setCaption((String) propertyId);
                    t.setImmediate(false);
                    return t;
                }
            });

            layout = new GridLayout(2, 1);
            layout.setWidth(100, Unit.PERCENTAGE);
            layout.setHeight(null);
            layout.setColumnExpandRatio(0, 0.5f);
            layout.setColumnExpandRatio(1, 0.5f);
            layout.setMargin(true);
            layout.setSpacing(true);
            setLayout(layout);

            HorizontalLayout footer = new HorizontalLayout();
            footer.setSpacing(true);
            footer.setMargin(false);
            footer.setVisible(false);
            setFooter(footer);

            defaultButton = new Button("Default Button", this);
            defaultButton.setClickShortcut(KeyCode.ENTER);
            footer.addComponent(defaultButton);

            otherButton = new Button("Other button", this);
            footer.addComponent(otherButton);
        }

        @Override
        public void setItemDataSource(Item newDataSource) {
            if (newDataSource != null) {
                super.setItemDataSource(newDataSource);
                layout.setVisible(true);
                getFooter().setVisible(true);
            } else {
                super.setItemDataSource(null);
                layout.setVisible(false);
                getFooter().setVisible(false);
            }
        }

        @Override
        @SuppressWarnings("rawtypes")
        protected void attachField(Object propertyId, Field field) {
            if (propertyId.equals("t1")) {
                layout.addComponent(field, 0, 0);
            } else if (propertyId.equals("t2")) {
                layout.addComponent(field, 1, 0);
            }
        }

        @Override
        public void buttonClick(ClickEvent event) {
            // NOP
        }

    }
}