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.

Comboboxes.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.vaadin.tests.components.combobox;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.LinkedHashMap;
  5. import java.util.List;
  6. import com.vaadin.server.ThemeResource;
  7. import com.vaadin.tests.components.ComponentTestCase;
  8. import com.vaadin.ui.ComboBox;
  9. import com.vaadin.ui.Component;
  10. public class Comboboxes extends ComponentTestCase<ComboBox> {
  11. private static class StringBean {
  12. private String value;
  13. public StringBean(String value) {
  14. this.value = value;
  15. }
  16. public String getValue() {
  17. return value;
  18. }
  19. }
  20. @Override
  21. protected Class<ComboBox> getTestClass() {
  22. return ComboBox.class;
  23. }
  24. @Override
  25. protected void initializeComponents() {
  26. ComboBox<String> s1 = createSelect(null);
  27. s1.setWidth(null);
  28. addTestComponent(s1);
  29. ComboBox<String> s2 = createSelect("Undefined wide, empty select");
  30. s2.setWidth(null);
  31. addTestComponent(s2);
  32. ComboBox<String> s3 = createSelect(
  33. "Undefined wide select with 5 items");
  34. s3.setWidth(null);
  35. s3.setItems("The first item", "The second item", "The third item",
  36. "The fourth item", "The fifth item");
  37. addTestComponent(s3);
  38. ComboBox<StringBean> s4 = new ComboBox<>(
  39. "Undefined wide select with 50 items");
  40. s4.setWidth(null);
  41. populate(s4, 50);
  42. s4.setItemCaptionGenerator(StringBean::getValue);
  43. s4.setScrollToSelectedItem(true);
  44. addTestComponent(s4);
  45. ComboBox<String> s5 = createSelect(null);
  46. s5.setWidth("100px");
  47. addTestComponent(s5);
  48. ComboBox<String> s6 = createSelect("100px wide, empty select");
  49. s6.setWidth("100px");
  50. addTestComponent(s6);
  51. ComboBox<String> s7 = createSelect("150px wide select with 5 items");
  52. s7.setWidth("150px");
  53. s7.setItems("The first item", "The second item", "The third item",
  54. "The fourth item", "The fifth item");
  55. addTestComponent(s7);
  56. ComboBox<StringBean> s8 = new ComboBox<>(
  57. "200px wide select with 50 items");
  58. s8.setWidth("200px");
  59. populate(s8, 50);
  60. s8.setItemCaptionGenerator(StringBean::getValue);
  61. addTestComponent(s8);
  62. ComboBox<StringBean> s9 = new PageLength0ComboBox();
  63. s9.setCaption("Pagelength 0");
  64. populate(s9, 15);
  65. s9.setItemCaptionGenerator(StringBean::getValue);
  66. addTestComponent(s9);
  67. }
  68. public class PageLength0ComboBox extends ComboBox<StringBean> {
  69. public PageLength0ComboBox() {
  70. super();
  71. setPageLength(0);
  72. }
  73. }
  74. private void populate(ComboBox<StringBean> s, int nr) {
  75. List<StringBean> beans = new ArrayList<>();
  76. String text = " an item ";
  77. String caption = "";
  78. for (int i = 0; i < nr; i++) {
  79. if (i % 2 == 0) {
  80. caption += text;
  81. } else {
  82. caption += i;
  83. }
  84. beans.add(new StringBean(caption));
  85. }
  86. s.setItems(beans);
  87. }
  88. private ComboBox<String> createSelect(String caption) {
  89. return new ComboBox<>(caption);
  90. }
  91. @Override
  92. protected String getTestDescription() {
  93. return "A generic test for ComboBoxes in different configurations";
  94. }
  95. @Override
  96. protected void createCustomActions(List<Component> actions) {
  97. actions.add(createIconSelect());
  98. }
  99. @SuppressWarnings("rawtypes")
  100. private Component createIconSelect() {
  101. LinkedHashMap<String, String> options = new LinkedHashMap<>();
  102. options.put("<None>", null);
  103. options.put("16x16", "../runo/icons/16/user.png");
  104. options.put("32x32", "../runo/icons/32/attention.png");
  105. options.put("64x64", "../runo/icons/64/email-reply.png");
  106. return createSelectAction("Icon", options, "<None>",
  107. new Command<ComboBox, String>() {
  108. @SuppressWarnings("unchecked")
  109. @Override
  110. public void execute(ComboBox c, String value, Object data) {
  111. if (value == null) {
  112. c.setItemIconGenerator(item -> null);
  113. } else {
  114. c.setItemIconGenerator(item -> new ThemeResource(
  115. value + "?" + new Date().getTime()));
  116. }
  117. }
  118. });
  119. }
  120. }