aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/data/GridRefreshWithGetId.java
blob: 509aee84950d43e3990685b02d2ecb790e91703d (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
package com.vaadin.tests.data;

import java.util.ArrayList;
import java.util.List;

import com.vaadin.annotations.Widgetset;
import com.vaadin.data.provider.ListDataProvider;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;

@Widgetset("com.vaadin.DefaultWidgetSet")
public class GridRefreshWithGetId extends AbstractTestUI {

    private static class TestObject {

        private final int id;
        private String name;

        public TestObject(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        /**
         * The class intentionally has strange {@code hashCode()} and
         * {@code equals()} implementation to ensure if {@code Grid} relies on
         * bean id rather than on bean hashcode/equals identification.
         *
         * {@see Object.equals}
         */
        @Override
        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;

            TestObject myObject = (TestObject) o;

            if (id != myObject.id)
                return false;
            return name != null ? name.equals(myObject.name)
                    : myObject.name == null;
        }

        /**
         * The class intentionally has strange {@code hashCode()} and
         * {@code equals()} implementation to ensure if {@code Grid} relies on
         * bean id rather than on bean hashcode/equals identification.
         *
         * {@see Object.hashCode}
         */
        @Override
        public int hashCode() {
            int result = id;
            result = 31 * result + (name != null ? name.hashCode() : 0);
            return result;
        }
    }

    @Override
    protected void setup(VaadinRequest request) {
        List<TestObject> data = new ArrayList<>();
        data.add(new TestObject(0, "blue"));
        data.add(new TestObject(1, "red"));
        data.add(new TestObject(2, "green"));
        data.add(new TestObject(3, "yellow"));
        data.add(new TestObject(4, "purple"));

        ListDataProvider<TestObject> dataProvider = new ListDataProvider<TestObject>(
                data) {

            @Override
            public Object getId(TestObject item) {
                return item.getId();
            }
        };

        Grid<TestObject> grid = new Grid<>();
        grid.setDataProvider(dataProvider);
        addComponent(grid);

        grid.addColumn(TestObject::getName);

        Button button = new Button("Change green to black");
        button.addClickListener(event1 -> {
            TestObject myObject = data.get(2);
            myObject.setName("black");
            dataProvider.refreshItem(myObject);
        });
        addComponent(button);
    }
}