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

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