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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package com.vaadin.tests.components.combobox;
  2. import java.util.Date;
  3. import java.util.LinkedHashMap;
  4. import java.util.List;
  5. import com.vaadin.server.ThemeResource;
  6. import com.vaadin.tests.components.ComponentTestCase;
  7. import com.vaadin.ui.ComboBox;
  8. import com.vaadin.ui.Component;
  9. public class Comboboxes extends ComponentTestCase<ComboBox> {
  10. private static final Object CAPTION = "caption";
  11. @Override
  12. protected Class<ComboBox> getTestClass() {
  13. return ComboBox.class;
  14. }
  15. @Override
  16. protected void initializeComponents() {
  17. ComboBox s;
  18. s = createSelect(null);
  19. s.setWidth(null);
  20. addTestComponent(s);
  21. s = createSelect("Undefined wide, empty select");
  22. s.setWidth(null);
  23. addTestComponent(s);
  24. s = createSelect("Undefined wide select with 5 items");
  25. s.setWidth(null);
  26. addItem(s, "The first item");
  27. addItem(s, "The second item");
  28. addItem(s, "The third item");
  29. addItem(s, "The fourth item");
  30. addItem(s, "The fifth item");
  31. addTestComponent(s);
  32. s = createSelect("Undefined wide select with 50 items");
  33. s.setWidth(null);
  34. populate(s, 50);
  35. addTestComponent(s);
  36. s = createSelect(null);
  37. s.setWidth("100px");
  38. addTestComponent(s);
  39. s = createSelect("100px wide, empty select");
  40. s.setWidth("100px");
  41. addTestComponent(s);
  42. s = createSelect("150px wide select with 5 items");
  43. s.setWidth("150px");
  44. addItem(s, "The first item");
  45. addItem(s, "The second item");
  46. addItem(s, "The third item");
  47. addItem(s, "The fourth item");
  48. addItem(s, "The fifth item");
  49. addTestComponent(s);
  50. s = createSelect("200px wide select with 50 items");
  51. s.setWidth("200px");
  52. populate(s, 50);
  53. addTestComponent(s);
  54. s = new PageLength0ComboBox();
  55. s.setImmediate(true);
  56. s.addContainerProperty(CAPTION, String.class, "");
  57. s.setItemCaptionPropertyId(CAPTION);
  58. s.setCaption("Pagelength 0");
  59. populate(s, 15);
  60. addTestComponent(s);
  61. }
  62. public class PageLength0ComboBox extends ComboBox {
  63. public PageLength0ComboBox() {
  64. super();
  65. setPageLength(0);
  66. }
  67. }
  68. private void populate(ComboBox s, int nr) {
  69. String text = " an item ";
  70. String caption = "";
  71. for (int i = 0; i < nr; i++) {
  72. if (i % 2 == 0) {
  73. caption += text;
  74. } else {
  75. caption += i;
  76. }
  77. addItem(s, caption);
  78. }
  79. }
  80. private void addItem(ComboBox s, String string) {
  81. Object id = s.addItem();
  82. s.getItem(id).getItemProperty(CAPTION).setValue(string);
  83. }
  84. private ComboBox createSelect(String caption) {
  85. ComboBox cb = new ComboBox();
  86. cb.setImmediate(true);
  87. cb.addContainerProperty(CAPTION, String.class, "");
  88. cb.setItemCaptionPropertyId(CAPTION);
  89. cb.setCaption(caption);
  90. return cb;
  91. }
  92. @Override
  93. protected String getDescription() {
  94. return "A generic test for ComboBoxes in different configurations";
  95. }
  96. @Override
  97. protected void createCustomActions(List<Component> actions) {
  98. actions.add(createIconSelect());
  99. }
  100. private Component createIconSelect() {
  101. LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
  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. @Override
  109. public void execute(ComboBox c, String value, Object data) {
  110. for (Object id : c.getItemIds()) {
  111. if (value == null) {
  112. c.setItemIcon(id, null);
  113. } else {
  114. c.setItemIcon(id, new ThemeResource(value + "?"
  115. + new Date().getTime()));
  116. }
  117. }
  118. }
  119. });
  120. }
  121. }