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.

CheckBoxGroupTestUI.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2000-2014 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.components.checkbox;
  17. import java.util.LinkedHashMap;
  18. import java.util.Objects;
  19. import com.vaadin.icons.VaadinIcons;
  20. import com.vaadin.server.SerializablePredicate;
  21. import com.vaadin.tests.components.abstractlisting.AbstractMultiSelectTestUI;
  22. import com.vaadin.ui.CheckBoxGroup;
  23. import com.vaadin.ui.DescriptionGenerator;
  24. import com.vaadin.ui.IconGenerator;
  25. /**
  26. * Test UI for CheckBoxGroup component
  27. *
  28. * @author Vaadin Ltd
  29. */
  30. public class CheckBoxGroupTestUI
  31. extends AbstractMultiSelectTestUI<CheckBoxGroup<Object>> {
  32. private static final IconGenerator<Object> DEFAULT_ICON_GENERATOR = item -> "Item 2"
  33. .equals(item) ? ICON_16_HELP_PNG_CACHEABLE : null;
  34. @SuppressWarnings({ "unchecked", "rawtypes" })
  35. @Override
  36. protected Class<CheckBoxGroup<Object>> getTestClass() {
  37. return (Class) CheckBoxGroup.class;
  38. }
  39. @Override
  40. protected CheckBoxGroup<Object> constructComponent() {
  41. CheckBoxGroup<Object> checkBoxGroup = super.constructComponent();
  42. checkBoxGroup.setItemIconGenerator(DEFAULT_ICON_GENERATOR);
  43. checkBoxGroup.setItemEnabledProvider(item -> !"Item 10".equals(item));
  44. return checkBoxGroup;
  45. }
  46. @Override
  47. protected void createActions() {
  48. super.createActions();
  49. createItemIconGenerator();
  50. createItemDescriptionGeneratorMenu();
  51. createItemEnabledProviderMenu();
  52. }
  53. private void createItemIconGenerator() {
  54. createBooleanAction("Use Item Icon Generator", "Item Generator", false,
  55. this::useItemIconProvider);
  56. }
  57. private void createItemDescriptionGeneratorMenu() {
  58. LinkedHashMap<String, DescriptionGenerator<Object>> options = new LinkedHashMap<>();
  59. options.put("Null Description Generator", item -> null);
  60. options.put("Default Description Generator", item -> item.toString());
  61. options.put("Custom Description Generator",
  62. item -> item + " Description");
  63. createSelectAction("Item Description Generator",
  64. "Item Description Generator", options, "None",
  65. (checkBoxGroup, generator, data) -> {
  66. checkBoxGroup.setItemDescriptionGenerator(generator);
  67. checkBoxGroup.getDataProvider().refreshAll();
  68. }, true);
  69. }
  70. private void createItemEnabledProviderMenu() {
  71. LinkedHashMap<String, SerializablePredicate<Object>> options = new LinkedHashMap<>();
  72. options.put("Disable Item 0", o -> !Objects.equals(o, "Item 0"));
  73. options.put("Disable Item 3", o -> !Objects.equals(o, "Item 3"));
  74. options.put("Disable Item 5", o -> !Objects.equals(o, "Item 5"));
  75. createSelectAction("Item Enabled Provider", "Item Enabled Provider",
  76. options, "None", (checkBoxGroup, generator, data) -> {
  77. checkBoxGroup.setItemEnabledProvider(generator);
  78. checkBoxGroup.getDataProvider().refreshAll();
  79. }, true);
  80. }
  81. private void useItemIconProvider(CheckBoxGroup<Object> group,
  82. boolean activate, Object data) {
  83. if (activate) {
  84. group.setItemIconGenerator(
  85. item -> VaadinIcons.values()[getIndex(item) + 1]);
  86. } else {
  87. group.setItemIconGenerator(DEFAULT_ICON_GENERATOR);
  88. }
  89. group.getDataProvider().refreshAll();
  90. }
  91. private int getIndex(Object item) {
  92. int index = item.toString().indexOf(' ');
  93. if (index < 0) {
  94. return 0;
  95. }
  96. String postfix = item.toString().substring(index + 1);
  97. index = postfix.indexOf(' ');
  98. if (index >= 0) {
  99. postfix = postfix.substring(0, index);
  100. }
  101. try {
  102. return Integer.parseInt(postfix);
  103. } catch (NumberFormatException e) {
  104. return 0;
  105. }
  106. }
  107. }