aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/components/grid/GridRefreshRow.java
blob: b2e2eae195e470f66a08f39d738474fdd5a0e954 (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
package com.vaadin.tests.components.grid;

import com.vaadin.annotations.Theme;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUIWithLog;
import com.vaadin.tests.util.Person;
import com.vaadin.tests.util.PersonContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.v7.ui.Grid;
import com.vaadin.v7.ui.Grid.CellReference;
import com.vaadin.v7.ui.Grid.CellStyleGenerator;
import com.vaadin.v7.ui.Grid.RowReference;
import com.vaadin.v7.ui.Grid.RowStyleGenerator;

@Theme("valo")
public class GridRefreshRow extends AbstractTestUIWithLog {

    private PersonContainer container;
    private Grid grid;

    private boolean styles[] = { false, false, false };

    @Override
    protected void setup(VaadinRequest request) {
        getPage().getStyles()
                .add(".rowstyle td {background-color: lightgreen !important;}");
        getPage().getStyles()
                .add("td.cellstyle {background-color: lightblue !important;}");
        container = PersonContainer.createWithTestData(100);
        container.addNestedContainerBean("address");
        grid = new Grid(container);
        grid.setWidth("800px");
        grid.setRowStyleGenerator(new RowStyleGenerator() {
            @Override
            public String getStyle(RowReference row) {
                int index = container.indexOfId(row.getItemId());
                if (index < 3 && styles[index]) {
                    return "rowstyle";
                }

                return null;
            }
        });
        grid.setCellStyleGenerator(new CellStyleGenerator() {
            @Override
            public String getStyle(CellReference cell) {
                int index = container.indexOfId(cell.getItemId());
                if (index < 3 && styles[index]
                        && "email".equals(cell.getPropertyId())) {
                    return "cellstyle";
                }

                return null;
            }
        });
        addComponent(grid);

        addComponents(new HorizontalLayout(update(0), update(1), update(2)));
        Button refresh10 = new Button("Refresh 0-9", event -> grid
                .refreshRows(container.getItemIds(0, 9).toArray()));
        refresh10.setId("refresh10");
        addComponents(
                new HorizontalLayout(refresh(0), refresh(1), refresh(2),
                        new Button("Refresh non-existant",
                                event -> grid.refreshRows("foobar"))),
                refresh10);
        addComponents(new HorizontalLayout(style(0), style(1), style(2)));
    }

    private Component style(final int i) {
        final CheckBox checkBox = new CheckBox("Style for " + i);
        checkBox.addValueChangeListener(
                event -> styles[i] = checkBox.getValue());
        checkBox.setId("style" + i);
        return checkBox;
    }

    private Component update(final int i) {
        Button button = new Button("Update " + i, event -> {
            Person p = container.getIdByIndex(i);
            p.setFirstName("!" + p.getFirstName());
        });
        button.setId("update" + i);
        return button;
    }

    protected Component refresh(final int i) {
        Button button = new Button("Refresh row " + i,
                event -> grid.refreshRows(container.getIdByIndex(i)));
        button.setId("refresh" + i);
        return button;
    }

}