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.

TableReduceContainerSize.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.vaadin.tests.components.table;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import com.vaadin.tests.components.TestBase;
  6. import com.vaadin.ui.Button;
  7. import com.vaadin.ui.HorizontalLayout;
  8. import com.vaadin.ui.Label;
  9. import com.vaadin.v7.data.Container.Filter;
  10. import com.vaadin.v7.data.Item;
  11. import com.vaadin.v7.data.util.BeanItemContainer;
  12. import com.vaadin.v7.ui.Table;
  13. /**
  14. * Test for #8291 and #7666: NegativeArraySizeException when Table scrolled to
  15. * the end and its size reduced.
  16. */
  17. public class TableReduceContainerSize extends TestBase {
  18. @Override
  19. protected void setup() {
  20. addComponent(new TestView());
  21. }
  22. private static class DecimateFilter implements Filter {
  23. @Override
  24. public boolean passesFilter(Object itemId, Item item)
  25. throws UnsupportedOperationException {
  26. return ((((TestObject) itemId).property3 % 10) == 0);
  27. }
  28. @Override
  29. public boolean appliesToProperty(Object propertyId) {
  30. return true;
  31. }
  32. }
  33. private static class TestView extends HorizontalLayout {
  34. private Filter filter = null;
  35. private boolean reduceData;
  36. private TestView() {
  37. final Table table = new Table();
  38. List<TestObject> data = createData(1000);
  39. final BeanItemContainer<TestObject> container = new BeanItemContainer<TestObject>(
  40. TestObject.class, data) {
  41. @Override
  42. public int size() {
  43. if (reduceData) {
  44. return 100;
  45. } else {
  46. return super.size();
  47. }
  48. }
  49. };
  50. table.setContainerDataSource(container);
  51. addComponent(table);
  52. final Label label = new Label();
  53. addComponent(label);
  54. Button button = new Button("Click");
  55. button.addClickListener(event -> {
  56. try {
  57. reduceData = !reduceData;
  58. table.refreshRowCache();
  59. label.setValue(
  60. "Index: " + table.getCurrentPageFirstItemIndex());
  61. } catch (Exception e) {
  62. label.setValue(
  63. "Exception: " + e.getClass().getSimpleName());
  64. }
  65. });
  66. addComponent(button);
  67. Button button2 = new Button("Filter");
  68. button2.addClickListener(event -> {
  69. try {
  70. if (filter != null) {
  71. container.removeAllContainerFilters();
  72. filter = null;
  73. } else {
  74. filter = new DecimateFilter();
  75. container.addContainerFilter(filter);
  76. }
  77. table.refreshRowCache();
  78. label.setValue(
  79. "Index: " + table.getCurrentPageFirstItemIndex());
  80. } catch (Exception e) {
  81. label.setValue(
  82. "Exception: " + e.getClass().getSimpleName());
  83. }
  84. });
  85. addComponent(button2);
  86. }
  87. }
  88. private static List<TestObject> createData(int count) {
  89. List<TestObject> data = new ArrayList<>(count);
  90. for (int i = 0; i < count; i++) {
  91. data.add(new TestObject("string-" + i, new Date(), i));
  92. }
  93. return data;
  94. }
  95. public static class TestObject {
  96. private String property1;
  97. private Date property2;
  98. private Integer property3;
  99. public TestObject(String property1, Date property2, Integer property3) {
  100. this.property1 = property1;
  101. this.property2 = property2;
  102. this.property3 = property3;
  103. }
  104. public String getProperty1() {
  105. return property1;
  106. }
  107. public Date getProperty2() {
  108. return property2;
  109. }
  110. public Integer getProperty3() {
  111. return property3;
  112. }
  113. }
  114. @Override
  115. protected String getDescription() {
  116. return "Table throws NegativeArraySizeException if container size is reduced to less than current scroll position";
  117. }
  118. @Override
  119. protected Integer getTicketNumber() {
  120. return 8291;
  121. }
  122. }