summaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/tickets/Ticket2998.java
blob: a3bbe4344bb1b8d6318c0feea103e230bf890d35 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package com.vaadin.tests.tickets;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import com.vaadin.Application;
import com.vaadin.data.Container;
import com.vaadin.data.Validator;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Layout.MarginHandler;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI.LegacyWindow;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.Reindeer;

/**
 * Table layout is very slow in Firefox 3.0.10 when the table contains
 * components.
 * 
 * This is adapted from the HbnContainer example application WorkoutLog.
 * 
 * Other browsers are much faster.
 */
public class Ticket2998 extends Application {
    private Table table;
    private VerticalLayout mainLayout;

    public class Workout implements Serializable {
        private Long id;
        private Date date = new Date();
        private String title = " -- new workout -- ";
        private float kilometers;

        private String trainingType;

        private Set<String> secondaryTypes;

        public Workout() {
        }

        public Long getId() {
            return id;
        }

        public Date getDate() {
            return date;
        }

        public void setDate(Date date) {
            this.date = date;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public float getKilometers() {
            return kilometers;
        }

        public void setKilometers(float kilometers) {
            this.kilometers = kilometers;
        }

        public String getTrainingType() {
            return trainingType;
        }

        public void setTrainingType(String trainingType) {
            this.trainingType = trainingType;
        }

        public void setSecondaryTypes(Set<String> secondaryTypes) {
            this.secondaryTypes = secondaryTypes;
        }

        public Set<String> getSecondaryTypes() {
            return secondaryTypes;
        }

    }

    public class WorkoutEditor extends Window {

        private DateField date = new DateField("Date");
        private TextField kilomiters = new TextField("Kilometers");
        private TextField title = new TextField("Title/note");

        private Ticket2998 workoutLog;

        public WorkoutEditor(Ticket2998 app) {
            super("Edit workout");
            workoutLog = app;
            Layout main = new VerticalLayout();
            setContent(main);
            main.setSizeUndefined();
            main.setStyleName(Reindeer.PANEL_LIGHT);

            FormLayout form = new FormLayout();
            form.setSizeUndefined();
            date.setResolution(DateField.RESOLUTION_MIN);
            form.addComponent(date);
            form.addComponent(kilomiters);
            form.addComponent(title);
            main.addComponent(form);

        }

        public void loadRun(Workout run) {
            if (run == null) {
                close();
            } else {
                date.setValue(run.getDate());
                kilomiters.setValue(String.valueOf(run.getKilometers()));
                title.setValue(run.getTitle());
                if (getParent() == null) {
                    workoutLog.getMainWindow().addWindow(this);
                }
                kilomiters.focus();
            }
        }
    }

    public class MyFieldFactory extends DefaultFieldFactory {

        public MyFieldFactory(Ticket2998 app) {
        }

        @Override
        public Field<?> createField(Container container, Object itemId,
                Object propertyId, Component uiContext) {

            /*
             * trainingType is manyToOne relation, give it a combobox
             */
            if (propertyId.equals("trainingType")) {
                return getTrainingTypeComboboxFor(itemId);
            }

            /*
             * Secondarytypes is manyToMany relation, give it a multiselect list
             */
            if (propertyId.equals("secondaryTypes")) {
                return getSecondaryTypesList(itemId);
            }

            final Field<?> f = super.createField(container, itemId, propertyId,
                    uiContext);
            if (f != null) {
                if (f instanceof TextField) {
                    TextField tf = (TextField) f;
                    tf.setWidth("100%");
                }
                if (propertyId.equals("kilometers")) {
                    f.setWidth("4em");
                    f.addValidator(new Validator() {
                        @Override
                        public void validate(Object value)
                                throws InvalidValueException {
                            // FIXME this does not follow the standard pattern
                            // for validators and has side effects!
                            try {
                                @SuppressWarnings("unused")
                                float f = Float.parseFloat((String) value);
                            } catch (Exception e) {
                                Notification.show("Bad number value");
                                f.setValue(0);
                            }
                        }
                    });
                }
                if (propertyId.equals("date")) {
                    ((DateField) f).setResolution(DateField.RESOLUTION_MIN);
                }
            }
            return f;

        }

        private Map<Object, ListSelect> workoutIdToList = new HashMap<Object, ListSelect>();

        private Field<?> getSecondaryTypesList(Object itemId) {
            ListSelect list = workoutIdToList.get(itemId);
            if (list == null) {
                list = new ListSelect();
                list.setMultiSelect(true);
                list.addItem("Item1");
                list.addItem("Item2");
                list.addItem("Item3");
                list.addItem("Item4");
                list.addItem("Item5");
                // list.setContainerDataSource(trainingTypes);
                list.setRows(4);
                workoutIdToList.put(itemId, list);
            }
            return list;
        }

        private Map<Object, ComboBox> workoutIdToCombobox = new HashMap<Object, ComboBox>();

        private Field<?> getTrainingTypeComboboxFor(Object itemId) {
            ComboBox cb = workoutIdToCombobox.get(itemId);
            if (cb == null) {
                final ComboBox cb2 = new ComboBox();
                cb2.addItem("value1");
                cb2.addItem("value2");
                cb2.addItem("value3");
                cb2.addItem("value4");
                cb2.setNewItemsAllowed(true);

                workoutIdToCombobox.put(itemId, cb2);
                cb = cb2;
            }
            return cb;
        }
    }

    @Override
    public void init() {
        buildView();
        setTheme("reindeer");
    }

    /**
     * Builds a simple view for application with Table and a row of buttons
     * below it.
     */
    private void buildView() {

        final LegacyWindow w = new LegacyWindow("Workout Log");

        // set theme and some layout stuff
        setMainWindow(w);
        w.getContent().setSizeFull();
        ((MarginHandler) w.getContent()).setMargin(false);

        Panel p = new Panel("Workout Log");
        p.setStyleName(Reindeer.PANEL_LIGHT);
        w.addComponent(p);
        mainLayout = new VerticalLayout();
        p.setContent(mainLayout);

        populateAndConfigureTable();

        mainLayout.addComponent(table);

        // make table consume all extra space
        p.setSizeFull();
        mainLayout.setSizeFull();
        mainLayout.setExpandRatio(table, 1);
        table.setSizeFull();
    }

    protected void populateAndConfigureTable() {
        table = new Table();

        table.setWidth("100%");
        table.setSelectable(true);
        table.setImmediate(true);
        table.setColumnCollapsingAllowed(true);
        table.setColumnWidth("date", 200);
        table.setColumnWidth("kilometers", 100);
        // table.addListener(this);
        table.setTableFieldFactory(new MyFieldFactory(this));

        loadWorkouts();

        table.setEditable(true);
    }

    /**
     * Loads container to Table
     */
    protected void loadWorkouts() {
        final BeanItemContainer<Workout> cont;
        // Use plain HbnContainer
        cont = new BeanItemContainer<Workout>(Workout.class);
        table.setContainerDataSource(cont);

        // insert some sample data
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MILLISECOND, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MINUTE, 0);

        String[] titles = new String[] { "A short easy one", "intervals",
                "very long", "just shaking legs after work",
                "long one with Paul", "test run" };

        c.add(Calendar.DATE, -1000);

        Random rnd = new Random();

        Workout r;

        for (int i = 0; i < 1000; i++) {
            r = new Workout();
            c.set(Calendar.HOUR_OF_DAY,
                    12 + (rnd.nextInt(11) - rnd.nextInt(11)));
            r.setDate(c.getTime());
            r.setTitle(titles[rnd.nextInt(titles.length)]);
            r.setKilometers(Math.round(rnd.nextFloat() * 30));
            r.setTrainingType("tt");
            c.add(Calendar.DATE, 1);
            cont.addBean(r);
        }
    }

}