You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TableRepairsScrollPositionOnReAddingAllRows.java 8.2KB

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