]> source.dussan.org Git - vaadin-framework.git/blob
a1177b7db335046decff828cfb5e9940cca0f64f
[vaadin-framework.git] /
1 package com.vaadin.tests.components.table;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import com.vaadin.server.VaadinRequest;
8 import com.vaadin.tests.components.AbstractTestUI;
9 import com.vaadin.ui.Button;
10 import com.vaadin.ui.Button.ClickListener;
11 import com.vaadin.v7.data.util.BeanItemContainer;
12 import com.vaadin.v7.ui.Table;
13
14 /**
15  * Scroll position should be restored when removing and re-adding all rows in
16  * Table.
17  *
18  * @author Vaadin Ltd
19  */
20 public class TableRepairsScrollPositionOnReAddingAllRows
21         extends AbstractTestUI {
22
23     private static final long serialVersionUID = 1L;
24
25     @Override
26     protected void setup(VaadinRequest request) {
27         final BeanItemContainer<TableItem> cont = new BeanItemContainer<TableItem>(
28                 TableItem.class);
29         final List<TableItem> restoringItemList = new ArrayList<TableItem>();
30
31         final Table table = new Table();
32         table.setWidth("400px");
33         table.setPageLength(-1);
34         table.setContainerDataSource(cont);
35         table.setSelectable(true);
36
37         Button buttonRestore = new Button("Restore table rows");
38         buttonRestore.setId("buttonRestore");
39         buttonRestore.addClickListener(new ClickListener() {
40
41             @Override
42             public void buttonClick(com.vaadin.ui.Button.ClickEvent event) {
43                 cont.removeAllItems();
44                 cont.addAll(restoringItemList);
45             }
46         });
47
48         Button buttonReAddAllViaAddAll = new Button("Re-add rows all at once");
49         buttonReAddAllViaAddAll.setId("buttonReAddAllViaAddAll");
50         buttonReAddAllViaAddAll.addClickListener(new ClickListener() {
51
52             @Override
53             public void buttonClick(com.vaadin.ui.Button.ClickEvent event) {
54                 List<TableItem> originalItemIds = new ArrayList<TableItem>(
55                         cont.getItemIds());
56                 cont.removeAllItems();
57                 cont.addAll(originalItemIds);
58             }
59         });
60
61         Button buttonReplaceByAnotherCollectionViaAddAll = new Button(
62                 "Replace by another items (via addAll())");
63         buttonReplaceByAnotherCollectionViaAddAll
64                 .setId("buttonReplaceByAnotherCollectionViaAddAll");
65         buttonReplaceByAnotherCollectionViaAddAll
66                 .addClickListener(new ClickListener() {
67
68                     @Override
69                     public void buttonClick(
70                             com.vaadin.ui.Button.ClickEvent event) {
71                         cont.removeAllItems();
72                         // create new collection (of different items) with other
73                         // size
74                         List<TableItem> itemList = new ArrayList<TableItem>();
75                         for (int i = 0; i < 79; i++) {
76                             TableItem ti = new TableItem();
77                             ti.setName("AnotherItem1_" + i);
78                             itemList.add(ti);
79                         }
80                         cont.addAll(itemList);
81                     }
82                 });
83
84         Button buttonReplaceByAnotherCollectionViaAdd = new Button(
85                 "Replace by another items (via add(), add()..)");
86         buttonReplaceByAnotherCollectionViaAdd
87                 .setId("buttonReplaceByAnotherCollectionViaAdd");
88         buttonReplaceByAnotherCollectionViaAdd
89                 .addClickListener(new ClickListener() {
90
91                     @Override
92                     public void buttonClick(
93                             com.vaadin.ui.Button.ClickEvent event) {
94                         cont.removeAllItems();
95                         for (int i = 0; i < 81; i++) {
96                             TableItem ti = new TableItem();
97                             ti.setName("AnotherItem2_" + i);
98                             // add one by one in container
99                             cont.addBean(ti);
100                         }
101                     }
102                 });
103
104         Button buttonReplaceBySubsetOfSmallerSize = new Button(
105                 "Replace rows by sub-set of smaller size (size not enought for restoring scroll position)");
106         buttonReplaceBySubsetOfSmallerSize
107                 .setId("buttonReplaceBySubsetOfSmallerSize");
108         buttonReplaceBySubsetOfSmallerSize
109                 .addClickListener(new ClickListener() {
110
111                     @Override
112                     public void buttonClick(
113                             com.vaadin.ui.Button.ClickEvent event) {
114                         cont.removeAllItems();
115                         cont.addAll(restoringItemList.subList(0, 20));
116                     }
117                 });
118
119         Button buttonReplaceByWholeSubsetPlusOneNew = new Button(
120                 "Replace rows by whole subset plus one new item");
121         buttonReplaceByWholeSubsetPlusOneNew
122                 .setId("buttonReplaceByWholeSubsetPlusOneNew");
123         buttonReplaceByWholeSubsetPlusOneNew
124                 .addClickListener(new ClickListener() {
125
126                     @Override
127                     public void buttonClick(
128                             com.vaadin.ui.Button.ClickEvent event) {
129                         cont.removeAllItems();
130
131                         List<TableItem> list = new ArrayList<TableItem>(
132                                 restoringItemList);
133                         TableItem ti = new TableItem();
134                         ti.setName("AnotherItem3_" + 80);
135                         list.add(ti);
136                         cont.addAll(list);
137                     }
138                 });
139
140         Button buttonRemoveAllAddOne = new Button(
141                 "Remove all items and add only one new item");
142         buttonRemoveAllAddOne.setId("buttonRemoveAllAddOne");
143         buttonRemoveAllAddOne.addClickListener(new ClickListener() {
144
145             @Override
146             public void buttonClick(com.vaadin.ui.Button.ClickEvent event) {
147                 cont.removeAllItems();
148                 TableItem ti = new TableItem();
149                 ti.setName("Item_" + 20);
150                 cont.addBean(ti);
151             }
152         });
153
154         // This should be the last test as it changes the table datasource
155         Button buttonReplaceByNewDatasource = new Button(
156                 "Remove all items and add new datasource");
157         buttonReplaceByNewDatasource.setId("buttonReplaceByNewDatasource");
158         buttonReplaceByNewDatasource.addClickListener(new ClickListener() {
159
160             @Override
161             public void buttonClick(com.vaadin.ui.Button.ClickEvent event) {
162                 cont.removeAllItems();
163                 BeanItemContainer<TableItem> newContainer = new BeanItemContainer<TableItem>(
164                         TableItem.class);
165                 for (int i = 0; i < 50; i++) {
166                     TableItem ti = new TableItem();
167                     ti.setName("Item_" + i);
168                     newContainer.addBean(ti);
169                 }
170                 table.setContainerDataSource(newContainer);
171             }
172         });
173
174         for (int i = 0; i < 80; i++) {
175             TableItem ti = new TableItem();
176             ti.setName("Item_" + i);
177             restoringItemList.add(ti);
178             cont.addBean(ti);
179         }
180
181         getLayout().addComponent(buttonReAddAllViaAddAll);
182         getLayout().addComponent(buttonReplaceByAnotherCollectionViaAddAll);
183         getLayout().addComponent(buttonReplaceByAnotherCollectionViaAdd);
184         getLayout().addComponent(buttonReplaceBySubsetOfSmallerSize);
185         getLayout().addComponent(buttonReplaceByWholeSubsetPlusOneNew);
186         getLayout().addComponent(buttonRemoveAllAddOne);
187         getLayout().addComponent(buttonReplaceByNewDatasource);
188         getLayout().addComponent(buttonRestore);
189         getLayout().addComponent(table);
190     }
191
192     public class TableItem implements Serializable {
193         private static final long serialVersionUID = -745849615488792221L;
194         private String name;
195
196         public String getName() {
197             return name;
198         }
199
200         public void setName(String name) {
201             this.name = name;
202         }
203     }
204
205     @Override
206     protected Integer getTicketNumber() {
207         return 14581;
208     }
209
210     @Override
211     protected String getTestDescription() {
212         return "The scroll position should not be changed if removing and re-adding all rows in Table.";
213     }
214 }