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

import java.util.Date;

import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractReindeerTestUI;
import com.vaadin.tests.util.Log;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.v7.data.util.BeanContainer;
import com.vaadin.v7.data.util.BeanItem;
import com.vaadin.v7.ui.Table;
import com.vaadin.v7.ui.themes.Reindeer;

public class TableWithContainerRequiringEqualsForItemId
        extends AbstractReindeerTestUI {

    private MyEntityContainer container = new MyEntityContainer();
    private Log log = new Log(10);

    public static class MyEntityContainer
            extends BeanContainer<Long, MyEntity> {

        public MyEntityContainer() {
            super(MyEntity.class);
            setBeanIdResolver(
                    new BeanIdResolver<Long, TableWithContainerRequiringEqualsForItemId.MyEntity>() {

                        @Override
                        public Long getIdForBean(MyEntity bean) {
                            // Return a new instance every time to ensure Table
                            // can
                            // handle it
                            return new Long(bean.getId());
                        }
                    });

        }

        @Override
        public Long getIdByIndex(int index) {
            // Explicitly get the id using the resolver to make sure the
            // instance does not stay the same
            BeanItem<MyEntity> beanItem = getItem(super.getIdByIndex(index));
            return getBeanIdResolver().getIdForBean(beanItem.getBean());
        }

    }

    @Override
    protected void setup(VaadinRequest request) {
        Table t = new Table("Table with 1000 item");
        t.addGeneratedColumn("Actions", new Table.ColumnGenerator() {
            @Override
            public Component generateCell(final Table source,
                    final Object itemId, final Object columnId) {
                Button tripFolderLink = new Button("Button" + itemId);
                tripFolderLink.addClickListener(event -> log.log("Button "
                        + event.getButton().getCaption() + " clicked"));
                tripFolderLink.setStyleName(Reindeer.BUTTON_SMALL);
                return tripFolderLink;
            }
        });

        for (int i = 0; i < 1000; i++) {
            MyEntity myEntity = new MyEntity(i + "st");
            myEntity.setCreated(
                    new Date(new Date().getTime() - 24 * 60 * 60 * 1000L));
            myEntity.setId(i);
            container.addBean(myEntity);
        }

        t.setContainerDataSource(container);
        t.setVisibleColumns("id", "created", "name", "Actions");

        addComponent(t);
        addComponent(log);

        t.sort(new Object[] { "id" }, new boolean[] { false });

    }

    @Override
    protected String getTestDescription() {
        return "Test that verifies that Table works correctly with containers which do not return the same instance of the itemId object but instead requires an itemId.equals(otherItemId) check";
    }

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

    public static class MyEntity {

        private long id;

        private String name;

        private Date created;

        public MyEntity() {
        }

        public MyEntity(String string) {
            name = string;
        }

        public String getName() {
            return name;
        }

        public Date getCreated() {
            return created;
        }

        public void setCreated(Date created) {
            this.created = created;
        }

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }
    }

}