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.

FilterSelect.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package com.itmill.toolkit.demo;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import com.itmill.toolkit.data.Item;
  6. import com.itmill.toolkit.ui.OrderedLayout;
  7. import com.itmill.toolkit.ui.Panel;
  8. import com.itmill.toolkit.ui.Select;
  9. import com.itmill.toolkit.ui.Window;
  10. import com.itmill.toolkit.ui.select.ContainsFilter;
  11. import com.itmill.toolkit.ui.select.OptionFilter;
  12. /**
  13. * The classic "hello, world!" example for IT Mill Toolkit. The class simply
  14. * implements the abstract {@link com.itmill.toolkit.Application#init() init()}
  15. * method in which it creates a Window and adds a Label to it.
  16. *
  17. * @author IT Mill Ltd.
  18. * @see com.itmill.toolkit.Application
  19. * @see com.itmill.toolkit.ui.Window
  20. * @see com.itmill.toolkit.ui.Label
  21. */
  22. public class FilterSelect extends com.itmill.toolkit.Application {
  23. private static final String[] firstnames = new String[] { "John", "Mary",
  24. "Joe", "Sarah", "Jeff", "Jane", "Peter", "Marc", "Robert", "Paula",
  25. "Lenny", "Kenny", "Nathan", "Nicole", "Laura", "Jos", "Josie",
  26. "Linus" };
  27. private static final String[] lastnames = new String[] { "Torvalds",
  28. "Smith", "Adams", "Black", "Wilson", "Richards", "Thompson",
  29. "McGoff", "Halas", "Jones", "Beck", "Sheridan", "Picard", "Hill",
  30. "Fielding", "Einstein" };
  31. /**
  32. * The initialization method that is the only requirement for inheriting the
  33. * com.itmill.toolkit.service.Application class. It will be automatically
  34. * called by the framework when a user accesses the application.
  35. */
  36. public void init() {
  37. /*
  38. * - Create new window for the application - Give the window a visible
  39. * title - Set the window to be the main window of the application
  40. */
  41. Window main = new Window("Filter select demo");
  42. setMainWindow(main);
  43. setTheme("corporate");
  44. // default filter
  45. Select s1 = new Select();
  46. for (int i = 0; i < 105; i++)
  47. s1
  48. .addItem(firstnames[(int) (Math.random() * (firstnames.length - 1))]
  49. + " "
  50. + lastnames[(int) (Math.random() * (lastnames.length - 1))]);
  51. s1.setLazyLoading(true);
  52. s1.setImmediate(true);
  53. // contains filter
  54. Select s2 = new Select();
  55. for (int i = 0; i < 500; i++)
  56. s2
  57. .addItem(firstnames[(int) (Math.random() * (firstnames.length - 1))]
  58. + " "
  59. + lastnames[(int) (Math.random() * (lastnames.length - 1))]);
  60. s2.setLazyLoading(true);
  61. s2.setOptionFilter(new ContainsFilter(s2));
  62. // custom filter
  63. Select s3 = new Select();
  64. for (int i = 0; i < 500; i++)
  65. s3
  66. .addItem(firstnames[(int) (Math.random() * (firstnames.length - 1))]
  67. + " "
  68. + lastnames[(int) (Math.random() * (lastnames.length - 1))]);
  69. s3.setLazyLoading(true);
  70. s3.setOptionFilter(new FilterSelect.EndsWithFilter(s3));
  71. // Add selects to UI using ordered layout and panels
  72. OrderedLayout orderedLayout = new OrderedLayout(
  73. OrderedLayout.ORIENTATION_HORIZONTAL);
  74. Panel panel1 = new Panel("Select with default filter");
  75. Panel panel2 = new Panel("Select with contains filter");
  76. Panel panel3 = new Panel("Select with custom 'EndsWith' filter");
  77. panel1.addComponent(s1);
  78. panel2.addComponent(s2);
  79. panel3.addComponent(s3);
  80. orderedLayout.addComponent(panel1);
  81. orderedLayout.addComponent(panel2);
  82. orderedLayout.addComponent(panel3);
  83. main.addComponent(orderedLayout);
  84. }
  85. /**
  86. * Custom filter that implements "ends with" functionality.
  87. *
  88. * @author IT Mill Ltd.
  89. *
  90. */
  91. public class EndsWithFilter implements OptionFilter {
  92. private Select s;
  93. private ArrayList filteredItemsBuffer;
  94. public EndsWithFilter(Select s) {
  95. this.s = s;
  96. }
  97. public List filter(String filterstring, int pagelength, int page) {
  98. // prefix MUST be in lowercase
  99. if (filterstring == null || "".equals(filterstring)) {
  100. this.filteredItemsBuffer = new ArrayList(s.getItemIds());
  101. return this.filteredItemsBuffer;
  102. } else if (s.getContainerDataSource() != null) {
  103. // all items will be iterated and tested.
  104. // SLOW when there are lot of items.
  105. this.filteredItemsBuffer = new ArrayList();
  106. for (Iterator iter = s.getItemIds().iterator(); iter.hasNext();) {
  107. Object id = iter.next();
  108. Item item = s.getItem(id);
  109. String test = "";
  110. if (s.getItemCaptionMode() == Select.ITEM_CAPTION_MODE_PROPERTY)
  111. test = item.getItemProperty(
  112. s.getItemCaptionPropertyId()).getValue()
  113. .toString().trim();
  114. else
  115. test = String.valueOf(id);
  116. if (test.toLowerCase().endsWith(filterstring)) {
  117. this.filteredItemsBuffer.add(id);
  118. }
  119. }
  120. }
  121. return this.filteredItemsBuffer;
  122. }
  123. public int getMatchCount() {
  124. return filteredItemsBuffer.size();
  125. }
  126. }
  127. }