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.

RadioButtonGroupTestUI.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.radiobutton;
  17. import java.util.LinkedHashMap;
  18. import java.util.Objects;
  19. import java.util.stream.IntStream;
  20. import com.vaadin.icons.VaadinIcons;
  21. import com.vaadin.server.SerializablePredicate;
  22. import com.vaadin.tests.components.abstractlisting.AbstractListingTestUI;
  23. import com.vaadin.ui.DescriptionGenerator;
  24. import com.vaadin.ui.ItemCaptionGenerator;
  25. import com.vaadin.ui.RadioButtonGroup;
  26. /**
  27. * Test UI for RadioButtonGroup component
  28. *
  29. * @author Vaadin Ltd
  30. */
  31. public class RadioButtonGroupTestUI
  32. extends AbstractListingTestUI<RadioButtonGroup<Object>> {
  33. private final String selectionCategory = "Selection";
  34. @SuppressWarnings({ "unchecked", "rawtypes" })
  35. @Override
  36. protected Class<RadioButtonGroup<Object>> getTestClass() {
  37. return (Class) RadioButtonGroup.class;
  38. }
  39. @Override
  40. protected void createActions() {
  41. super.createActions();
  42. createListenerMenu();
  43. createSelectionMenu();
  44. createItemIconGeneratorMenu();
  45. createItemCaptionGeneratorMenu();
  46. createItemDescriptionGeneratorMenu();
  47. createItemEnabledProviderMenu();
  48. }
  49. protected void createSelectionMenu() {
  50. createClickAction("Clear selection", selectionCategory,
  51. (component, item, data) -> component.getSelectedItem()
  52. .ifPresent(value -> component.setValue(null)),
  53. "");
  54. Command<RadioButtonGroup<Object>, String> toggleSelection = (component,
  55. item, data) -> toggleSelection(item);
  56. IntStream.of(0, 1, 5, 10, 25).mapToObj(i -> "Item " + i)
  57. .forEach(item -> createClickAction("Toggle " + item,
  58. selectionCategory, toggleSelection, item));
  59. }
  60. private void createItemIconGeneratorMenu() {
  61. createBooleanAction("Use Item Icon Generator", "Item Icon Generator",
  62. false, this::useItemIconGenerator);
  63. }
  64. private void useItemIconGenerator(RadioButtonGroup<Object> group,
  65. boolean activate, Object data) {
  66. if (activate) {
  67. group.setItemIconGenerator(
  68. item -> VaadinIcons.values()[getIndex(item) + 1]);
  69. } else {
  70. group.setItemIconGenerator(item -> null);
  71. }
  72. group.getDataProvider().refreshAll();
  73. }
  74. private void createItemCaptionGeneratorMenu() {
  75. LinkedHashMap<String, ItemCaptionGenerator<Object>> options = new LinkedHashMap<>();
  76. options.put("Null Caption Generator", item -> null);
  77. options.put("Default Caption Generator", item -> item.toString());
  78. options.put("Custom Caption Generator", item -> item + " Caption");
  79. createSelectAction("Item Caption Generator", "Item Caption Generator",
  80. options, "None", (radioButtonGroup, captionGenerator, data) -> {
  81. radioButtonGroup.setItemCaptionGenerator(captionGenerator);
  82. radioButtonGroup.getDataProvider().refreshAll();
  83. }, true);
  84. }
  85. private void createItemDescriptionGeneratorMenu() {
  86. LinkedHashMap<String, DescriptionGenerator<Object>> options = new LinkedHashMap<>();
  87. options.put("Null Description Generator", item -> null);
  88. options.put("Default Description Generator", item -> item.toString());
  89. options.put("Custom Description Generator",
  90. item -> item + " Description");
  91. createSelectAction("Item Description Generator",
  92. "Item Description Generator", options, "None",
  93. (radioButtonGroup, generator, data) -> {
  94. radioButtonGroup.setItemDescriptionGenerator(generator);
  95. }, true);
  96. }
  97. private void createItemEnabledProviderMenu() {
  98. LinkedHashMap<String, SerializablePredicate<Object>> options = new LinkedHashMap<>();
  99. options.put("Disable Item 0", o -> !Objects.equals(o, "Item 0"));
  100. options.put("Disable Item 3", o -> !Objects.equals(o, "Item 3"));
  101. options.put("Disable Item 5", o -> !Objects.equals(o, "Item 5"));
  102. createSelectAction("Item Enabled Provider", "Item Enabled Provider",
  103. options, "None", (radioButtonGroup, generator, data) -> {
  104. radioButtonGroup.setItemEnabledProvider(generator);
  105. radioButtonGroup.getDataProvider().refreshAll();
  106. }, true);
  107. }
  108. private void toggleSelection(String item) {
  109. if (getComponent().isSelected(item)) {
  110. getComponent().setValue(null);
  111. } else {
  112. getComponent().setValue(item);
  113. }
  114. }
  115. protected void createListenerMenu() {
  116. createListenerAction("Selection listener", "Listeners",
  117. c -> c.addSelectionListener(
  118. event -> log("Selected: " + event.getSelectedItem())));
  119. }
  120. private int getIndex(Object item) {
  121. int index = item.toString().indexOf(' ');
  122. if (index < 0) {
  123. return 0;
  124. }
  125. String postfix = item.toString().substring(index + 1);
  126. index = postfix.indexOf(' ');
  127. if (index >= 0) {
  128. postfix = postfix.substring(0, index);
  129. }
  130. try {
  131. return Integer.parseInt(postfix);
  132. } catch (NumberFormatException e) {
  133. return 0;
  134. }
  135. }
  136. }