1 package com.vaadin.tests.components.table;
3 import java.io.Serializable;
4 import java.util.ArrayList;
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;
15 * Scroll position should be restored when removing and re-adding all rows in
20 public class TableRepairsScrollPositionOnReAddingAllRows
21 extends AbstractTestUI {
23 private static final long serialVersionUID = 1L;
26 protected void setup(VaadinRequest request) {
27 final BeanItemContainer<TableItem> cont = new BeanItemContainer<TableItem>(
29 final List<TableItem> restoringItemList = new ArrayList<TableItem>();
31 final Table table = new Table();
32 table.setWidth("400px");
33 table.setPageLength(-1);
34 table.setContainerDataSource(cont);
35 table.setSelectable(true);
37 Button buttonRestore = new Button("Restore table rows");
38 buttonRestore.setId("buttonRestore");
39 buttonRestore.addClickListener(new ClickListener() {
42 public void buttonClick(com.vaadin.ui.Button.ClickEvent event) {
43 cont.removeAllItems();
44 cont.addAll(restoringItemList);
48 Button buttonReAddAllViaAddAll = new Button("Re-add rows all at once");
49 buttonReAddAllViaAddAll.setId("buttonReAddAllViaAddAll");
50 buttonReAddAllViaAddAll.addClickListener(new ClickListener() {
53 public void buttonClick(com.vaadin.ui.Button.ClickEvent event) {
54 List<TableItem> originalItemIds = new ArrayList<TableItem>(
56 cont.removeAllItems();
57 cont.addAll(originalItemIds);
61 Button buttonReplaceByAnotherCollectionViaAddAll = new Button(
62 "Replace by another items (via addAll())");
63 buttonReplaceByAnotherCollectionViaAddAll
64 .setId("buttonReplaceByAnotherCollectionViaAddAll");
65 buttonReplaceByAnotherCollectionViaAddAll
66 .addClickListener(new ClickListener() {
69 public void buttonClick(
70 com.vaadin.ui.Button.ClickEvent event) {
71 cont.removeAllItems();
72 // create new collection (of different items) with other
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);
80 cont.addAll(itemList);
84 Button buttonReplaceByAnotherCollectionViaAdd = new Button(
85 "Replace by another items (via add(), add()..)");
86 buttonReplaceByAnotherCollectionViaAdd
87 .setId("buttonReplaceByAnotherCollectionViaAdd");
88 buttonReplaceByAnotherCollectionViaAdd
89 .addClickListener(new ClickListener() {
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
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() {
112 public void buttonClick(
113 com.vaadin.ui.Button.ClickEvent event) {
114 cont.removeAllItems();
115 cont.addAll(restoringItemList.subList(0, 20));
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() {
127 public void buttonClick(
128 com.vaadin.ui.Button.ClickEvent event) {
129 cont.removeAllItems();
131 List<TableItem> list = new ArrayList<TableItem>(
133 TableItem ti = new TableItem();
134 ti.setName("AnotherItem3_" + 80);
140 Button buttonRemoveAllAddOne = new Button(
141 "Remove all items and add only one new item");
142 buttonRemoveAllAddOne.setId("buttonRemoveAllAddOne");
143 buttonRemoveAllAddOne.addClickListener(new ClickListener() {
146 public void buttonClick(com.vaadin.ui.Button.ClickEvent event) {
147 cont.removeAllItems();
148 TableItem ti = new TableItem();
149 ti.setName("Item_" + 20);
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() {
161 public void buttonClick(com.vaadin.ui.Button.ClickEvent event) {
162 cont.removeAllItems();
163 BeanItemContainer<TableItem> newContainer = new BeanItemContainer<TableItem>(
165 for (int i = 0; i < 50; i++) {
166 TableItem ti = new TableItem();
167 ti.setName("Item_" + i);
168 newContainer.addBean(ti);
170 table.setContainerDataSource(newContainer);
174 for (int i = 0; i < 80; i++) {
175 TableItem ti = new TableItem();
176 ti.setName("Item_" + i);
177 restoringItemList.add(ti);
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);
192 public class TableItem implements Serializable {
193 private static final long serialVersionUID = -745849615488792221L;
196 public String getName() {
200 public void setName(String name) {
206 protected Integer getTicketNumber() {
211 protected String getTestDescription() {
212 return "The scroll position should not be changed if removing and re-adding all rows in Table.";