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 6.4KB

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