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.

TestForContainerFilterable.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests;
  17. import com.vaadin.ui.Button;
  18. import com.vaadin.ui.Button.ClickEvent;
  19. import com.vaadin.ui.CustomComponent;
  20. import com.vaadin.ui.HorizontalLayout;
  21. import com.vaadin.ui.Label;
  22. import com.vaadin.ui.Panel;
  23. import com.vaadin.ui.VerticalLayout;
  24. import com.vaadin.v7.data.util.IndexedContainer;
  25. import com.vaadin.v7.ui.Table;
  26. import com.vaadin.v7.ui.TextField;
  27. public class TestForContainerFilterable extends CustomComponent {
  28. VerticalLayout lo = new VerticalLayout();
  29. IndexedContainer ic = new IndexedContainer();
  30. Table t = new Table();
  31. private static String parts[] = { "Neo", "Sa", "rem", "the", "adi", "za",
  32. "tre", "day", "Ca", "re", "cen", "ter", "mi", "nal" };
  33. TextField fooFilter = new TextField("foo-filter");
  34. TextField barFilter = new TextField("bar-filter");
  35. Button filterButton = new Button("Filter");
  36. Label count = new Label();
  37. public TestForContainerFilterable() {
  38. setCompositionRoot(lo);
  39. // Init datasource
  40. ic.addContainerProperty("foo", String.class, "");
  41. ic.addContainerProperty("bar", String.class, "");
  42. for (int i = 0; i < 1000; i++) {
  43. final Object id = ic.addItem();
  44. ic.getContainerProperty(id, "foo").setValue(randomWord());
  45. ic.getContainerProperty(id, "bar").setValue(randomWord());
  46. }
  47. // Init filtering view
  48. final HorizontalLayout filterLayout = new HorizontalLayout();
  49. final Panel filterPanel = new Panel("Filter", filterLayout);
  50. filterPanel.setWidth(100, Panel.UNITS_PERCENTAGE);
  51. lo.addComponent(filterPanel);
  52. filterLayout.addComponent(fooFilter);
  53. filterLayout.addComponent(barFilter);
  54. filterLayout.addComponent(filterButton);
  55. fooFilter.setDescription(
  56. "Filters foo column in case-sensitive contains manner.");
  57. barFilter.setDescription(
  58. "Filters bar column in case-insensitive prefix manner.");
  59. filterLayout.addComponent(count);
  60. // Table
  61. lo.addComponent(t);
  62. t.setPageLength(12);
  63. t.setWidth(100, Table.UNITS_PERCENTAGE);
  64. t.setContainerDataSource(ic);
  65. // Handler
  66. filterButton.addClickListener(new Button.ClickListener() {
  67. @Override
  68. public void buttonClick(ClickEvent event) {
  69. ic.removeAllContainerFilters();
  70. if (!fooFilter.getValue().isEmpty()) {
  71. ic.addContainerFilter("foo", fooFilter.getValue(), false,
  72. false);
  73. }
  74. if (!barFilter.getValue().isEmpty()) {
  75. ic.addContainerFilter("bar", barFilter.getValue(), true,
  76. true);
  77. }
  78. count.setValue("Rows in table: " + ic.size());
  79. }
  80. });
  81. // Resetbutton
  82. lo.addComponent(new Button("Rebind table datasource",
  83. new Button.ClickListener() {
  84. @Override
  85. public void buttonClick(ClickEvent event) {
  86. t.setContainerDataSource(ic);
  87. }
  88. }));
  89. }
  90. private String randomWord() {
  91. int len = (int) (Math.random() * 4);
  92. final StringBuilder buf = new StringBuilder();
  93. while (len-- >= 0) {
  94. buf.append(parts[(int) (Math.random() * parts.length)]);
  95. }
  96. return buf.toString();
  97. }
  98. }