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.

BeanItemContainerFilteringTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.vaadin.tests.containers;
  2. import com.vaadin.server.Sizeable;
  3. import com.vaadin.tests.components.TestBase;
  4. import com.vaadin.ui.Button;
  5. import com.vaadin.ui.Button.ClickEvent;
  6. import com.vaadin.ui.CheckBox;
  7. import com.vaadin.ui.Label;
  8. import com.vaadin.ui.VerticalLayout;
  9. import com.vaadin.v7.data.Item;
  10. import com.vaadin.v7.data.util.BeanItemContainer;
  11. import com.vaadin.v7.ui.Table;
  12. import com.vaadin.v7.ui.TextField;
  13. public class BeanItemContainerFilteringTest extends TestBase {
  14. private Table table;
  15. private BeanItemContainer<TestBean> container;
  16. private TextField filterString;
  17. private TextField position;
  18. private int nextToAdd = 1;
  19. private Label nextLabel;
  20. protected static class TestBean {
  21. private String id;
  22. private String value;
  23. public TestBean() {
  24. }
  25. public TestBean(String id, String value) {
  26. setId(id);
  27. setValue(value);
  28. }
  29. public void setId(String id) {
  30. this.id = id;
  31. }
  32. public String getId() {
  33. return id;
  34. }
  35. public void setValue(String value) {
  36. this.value = value;
  37. }
  38. public String getValue() {
  39. return value;
  40. }
  41. }
  42. @Override
  43. protected String getDescription() {
  44. return "Test adding items in a filtered BeanItemContainer.";
  45. }
  46. @Override
  47. protected Integer getTicketNumber() {
  48. return new Integer(1061);
  49. }
  50. @Override
  51. protected void setup() {
  52. table = new Table();
  53. try {
  54. container = new BeanItemContainer<>(TestBean.class);
  55. table.setContainerDataSource(container);
  56. table.setWidth(300, Sizeable.UNITS_PIXELS);
  57. table.setSelectable(true);
  58. table.setMultiSelect(false);
  59. table.setEditable(true);
  60. table.setImmediate(true);
  61. // table.addContainerProperty("column1", String.class, "test");
  62. for (int i = 0; i < 25; ++i) {
  63. container.addItem(new TestBean("Item " + i, "Value for " + i));
  64. }
  65. VerticalLayout vl = new VerticalLayout();
  66. // activate & deactivate filtering
  67. filterString = new TextField("Filter string:", "1");
  68. vl.addComponent(filterString);
  69. final CheckBox cb = new CheckBox("Filter on value");
  70. cb.addValueChangeListener(event -> {
  71. container.removeAllContainerFilters();
  72. if (event.getValue()) {
  73. container.addContainerFilter("value",
  74. filterString.getValue(), false, false);
  75. }
  76. });
  77. vl.addComponent(cb);
  78. nextLabel = new Label();
  79. nextLabel.setCaption("Next id: " + nextToAdd);
  80. vl.addComponent(nextLabel);
  81. // addItemAt(idx), addItemAfter(selection), addItem()
  82. final Button addItemButton = new Button("addItem()",
  83. new Button.ClickListener() {
  84. @Override
  85. public void buttonClick(ClickEvent event) {
  86. container.addItem(
  87. new TestBean("addItem() " + nextToAdd,
  88. "value " + nextToAdd));
  89. nextToAdd++;
  90. nextLabel.setCaption("Next id: " + nextToAdd);
  91. }
  92. });
  93. vl.addComponent(addItemButton);
  94. final Button addItemAfterButton = new Button("addItemAfter()",
  95. new Button.ClickListener() {
  96. @Override
  97. public void buttonClick(ClickEvent event) {
  98. Object selection = table.getValue();
  99. if (selection == null) {
  100. return;
  101. }
  102. TestBean bean = new TestBean(
  103. "addItemAfter() " + nextToAdd,
  104. "value " + nextToAdd);
  105. Item item = container.addItemAfter(selection, bean);
  106. if (item == null) {
  107. getMainWindow()
  108. .showNotification("Adding item after "
  109. + selection + " failed");
  110. }
  111. nextToAdd++;
  112. nextLabel.setCaption("Next id: " + nextToAdd);
  113. }
  114. });
  115. vl.addComponent(addItemAfterButton);
  116. position = new TextField("Position:", "0");
  117. vl.addComponent(position);
  118. final Button addItemAtButton = new Button("addItemAt()",
  119. new Button.ClickListener() {
  120. @Override
  121. public void buttonClick(ClickEvent event) {
  122. int index = Integer.parseInt(position.getValue());
  123. TestBean bean = new TestBean(
  124. "addItemAt() " + nextToAdd,
  125. "value " + nextToAdd);
  126. Item item = container.addItemAt(index, bean);
  127. if (item == null) {
  128. getMainWindow().showNotification(
  129. "Adding item at index "
  130. + position.getValue()
  131. + " failed");
  132. }
  133. nextToAdd++;
  134. nextLabel.setCaption("Next id: " + nextToAdd);
  135. }
  136. });
  137. vl.addComponent(addItemAtButton);
  138. getLayout().addComponent(table);
  139. getLayout().addComponent(vl);
  140. } catch (Exception ex) {
  141. ex.printStackTrace();
  142. }
  143. }
  144. }