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.

RadioButtonGroup.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2000-2016 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.ui;
  17. import java.util.Collection;
  18. import java.util.Objects;
  19. import com.vaadin.data.Listing;
  20. import com.vaadin.server.Resource;
  21. import com.vaadin.server.ResourceReference;
  22. import com.vaadin.server.SerializablePredicate;
  23. import com.vaadin.server.data.DataGenerator;
  24. import com.vaadin.server.data.DataSource;
  25. import com.vaadin.shared.ui.ListingJsonConstants;
  26. import com.vaadin.shared.ui.optiongroup.RadioButtonGroupState;
  27. import elemental.json.JsonObject;
  28. /**
  29. * A group of RadioButtons. Individual radiobuttons are made from items supplied
  30. * by a {@link DataSource}. RadioButtons may have captions and icons.
  31. *
  32. * @param <T>
  33. * item type
  34. * @author Vaadin Ltd
  35. * @since 8.0
  36. */
  37. public class RadioButtonGroup<T> extends AbstractSingleSelect<T> {
  38. private IconGenerator<T> itemIconGenerator = item -> null;
  39. private ItemCaptionGenerator<T> itemCaptionGenerator = String::valueOf;
  40. private SerializablePredicate<T> itemEnabledProvider = item -> true;
  41. /**
  42. * Constructs a new RadioButtonGroup with caption.
  43. *
  44. * @param caption
  45. * caption text
  46. * @see Listing#setDataSource(DataSource)
  47. */
  48. public RadioButtonGroup(String caption) {
  49. this();
  50. setCaption(caption);
  51. }
  52. /**
  53. * Constructs a new RadioButtonGroup with caption and DataSource.
  54. *
  55. * @param caption
  56. * the caption text
  57. * @param dataSource
  58. * the data source, not null
  59. * @see Listing#setDataSource(DataSource)
  60. */
  61. public RadioButtonGroup(String caption, DataSource<T> dataSource) {
  62. this(caption);
  63. setDataSource(dataSource);
  64. }
  65. /**
  66. * Constructs a new RadioButtonGroup with caption and DataSource containing
  67. * given items.
  68. *
  69. * @param caption
  70. * the caption text
  71. * @param items
  72. * the data items to use, not null
  73. * @see Listing#setDataSource(DataSource)
  74. */
  75. public RadioButtonGroup(String caption, Collection<T> items) {
  76. this(caption, DataSource.create(items));
  77. }
  78. /**
  79. * Constructs a new RadioButtonGroup.
  80. *
  81. * @see Listing#setDataSource(DataSource)
  82. */
  83. public RadioButtonGroup() {
  84. setSelectionModel(new SimpleSingleSelection());
  85. addDataGenerator(new DataGenerator<T>() {
  86. @Override
  87. public void generateData(T data, JsonObject jsonObject) {
  88. String caption = getItemCaptionGenerator().apply(data);
  89. if (caption != null) {
  90. jsonObject.put(ListingJsonConstants.JSONKEY_ITEM_VALUE,
  91. caption);
  92. } else {
  93. jsonObject.put(ListingJsonConstants.JSONKEY_ITEM_VALUE, "");
  94. }
  95. Resource icon = getItemIconGenerator().apply(data);
  96. if (icon != null) {
  97. String iconUrl = ResourceReference
  98. .create(icon, RadioButtonGroup.this, null).getURL();
  99. jsonObject.put(ListingJsonConstants.JSONKEY_ITEM_ICON,
  100. iconUrl);
  101. }
  102. if (!itemEnabledProvider.test(data)) {
  103. jsonObject.put(ListingJsonConstants.JSONKEY_ITEM_DISABLED,
  104. true);
  105. }
  106. if (getSelectionModel().isSelected(data)) {
  107. jsonObject.put(ListingJsonConstants.JSONKEY_ITEM_SELECTED,
  108. true);
  109. }
  110. }
  111. @Override
  112. public void destroyData(T data) {
  113. }
  114. });
  115. }
  116. /**
  117. * Sets whether html is allowed in the item captions. If set to true, the
  118. * captions are passed to the browser as html and the developer is
  119. * responsible for ensuring no harmful html is used. If set to false, the
  120. * content is passed to the browser as plain text.
  121. *
  122. * @param htmlContentAllowed
  123. * true if the captions are used as html, false if used as plain
  124. * text
  125. */
  126. public void setHtmlContentAllowed(boolean htmlContentAllowed) {
  127. getState().htmlContentAllowed = htmlContentAllowed;
  128. }
  129. /**
  130. * Checks whether captions are interpreted as html or plain text.
  131. *
  132. * @return true if the captions are used as html, false if used as plain
  133. * text
  134. * @see #setHtmlContentAllowed(boolean)
  135. */
  136. public boolean isHtmlContentAllowed() {
  137. return getState(false).htmlContentAllowed;
  138. }
  139. @Override
  140. protected RadioButtonGroupState getState() {
  141. return (RadioButtonGroupState) super.getState();
  142. }
  143. @Override
  144. protected RadioButtonGroupState getState(boolean markAsDirty) {
  145. return (RadioButtonGroupState) super.getState(markAsDirty);
  146. }
  147. /**
  148. * Returns the item icon generator.
  149. *
  150. * @return the currently set icon generator
  151. * @see #setItemIconGenerator
  152. * @see IconGenerator
  153. */
  154. public IconGenerator<T> getItemIconGenerator() {
  155. return itemIconGenerator;
  156. }
  157. /**
  158. * Sets the item icon generator for this radiobutton group. The icon
  159. * generator is queried for each item to optionally display an icon next to
  160. * the item caption. If the generator returns null for an item, no icon is
  161. * displayed. The default generator always returns null (no icons).
  162. *
  163. * @param itemIconGenerator
  164. * the icon generator, not null
  165. * @see IconGenerator
  166. */
  167. public void setItemIconGenerator(IconGenerator<T> itemIconGenerator) {
  168. Objects.requireNonNull(itemIconGenerator,
  169. "Item icon generator cannot be null.");
  170. this.itemIconGenerator = itemIconGenerator;
  171. }
  172. /**
  173. * Returns the currently set item caption generator.
  174. *
  175. * @return the currently set caption generator
  176. * @see #setItemCaptionGenerator
  177. * @see ItemCaptionGenerator
  178. */
  179. public ItemCaptionGenerator<T> getItemCaptionGenerator() {
  180. return itemCaptionGenerator;
  181. }
  182. /**
  183. * Sets the item caption generator for this radiobutton group. The caption
  184. * generator is queried for each item to optionally display an item textual
  185. * representation. The default generator returns
  186. * {@code String.valueOf(item)}.
  187. *
  188. * @param itemCaptionGenerator
  189. * the item caption generator, not null
  190. * @see ItemCaptionGenerator
  191. */
  192. public void setItemCaptionGenerator(
  193. ItemCaptionGenerator<T> itemCaptionGenerator) {
  194. Objects.requireNonNull(itemCaptionGenerator,
  195. "Item caption generator cannot be null.");
  196. this.itemCaptionGenerator = itemCaptionGenerator;
  197. }
  198. /**
  199. * Returns the item enabled predicate.
  200. *
  201. * @return the item enabled predicate
  202. * @see #setItemEnabledProvider
  203. */
  204. public SerializablePredicate<T> getItemEnabledProvider() {
  205. return itemEnabledProvider;
  206. }
  207. /**
  208. * Sets the item enabled predicate for this radiobutton group. The predicate
  209. * is applied to each item to determine whether the item should be enabled
  210. * (true) or disabled (false). Disabled items are displayed as grayed out
  211. * and the user cannot select them. The default predicate always returns
  212. * true (all the items are enabled).
  213. *
  214. * @param itemEnabledProvider
  215. * the item enable predicate, not null
  216. */
  217. public void setItemEnabledProvider(
  218. SerializablePredicate<T> itemEnabledProvider) {
  219. Objects.requireNonNull(itemEnabledProvider);
  220. this.itemEnabledProvider = itemEnabledProvider;
  221. }
  222. }