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

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import com.vaadin.data.Container;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Table;
import com.vaadin.ui.TableFieldFactory;

public class EditableModeChange extends TestBase {

    private ItemClickEvent selectionEvent;

    private final String[] names = { "Teemu", "Teppo", "Seppo", "Matti",
            "Pekka" };

    @Override
    public void setup() {

        final Table items = new Table("Items - double-click to edit");
        items.setSelectable(true);
        items.addContainerProperty("name", String.class, "");
        items.addContainerProperty("birthday", Date.class, "");

        final TableFieldFactory fieldFactory = new ItemFieldFactory();
        items.setTableFieldFactory(fieldFactory);

        Calendar cal = Calendar.getInstance();
        cal.set(2010, 7, 12, 12, 7, 54);

        for (String name : names) {
            items.addItem(name);
            items.getItem(name).getItemProperty("name").setValue(name);
            items.getItem(name).getItemProperty("birthday")
                    .setValue(new FormattedDate(cal.getTime().getTime()));
        }

        items.addListener(new ItemClickEvent.ItemClickListener() {

            @Override
            public void itemClick(ItemClickEvent event) {
                if (event.isDoubleClick()) {
                    selectionEvent = event;
                    items.setEditable(true);
                } else if (items.isEditable()) {
                    items.setEditable(false);
                }
            }
        });

        addComponent(items);
    }

    private class FormattedDate extends Date {

        private DateFormat formatter = DateFormat.getDateTimeInstance(
                DateFormat.MEDIUM, DateFormat.MEDIUM, new Locale("en", "US"));

        public FormattedDate(long time) {
            super(time);
        }

        @Override
        public String toString() {
            return formatter.format(this);
        }
    }

    private class ItemFieldFactory extends DefaultFieldFactory {
        @Override
        public Field<?> createField(Container container, Object itemId,
                Object propertyId, Component uiContext) {
            if (selectionEvent != null) {
                if ((selectionEvent.getItemId().equals(itemId))
                        && (selectionEvent.getPropertyId().equals(propertyId))) {
                    return super.createField(container, itemId, propertyId,
                            uiContext);
                }
            }
            return null;
        }
    }

    @Override
    protected String getDescription() {
        return "Double click a cell to edit, then click on another row to select it (editmode is set to false). The clicked row should now be selected without any flickering.";
    }

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