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.

CheckBoxGroup.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.Set;
  19. import org.jsoup.nodes.Element;
  20. import com.vaadin.data.HasDataProvider;
  21. import com.vaadin.data.provider.DataProvider;
  22. import com.vaadin.event.FieldEvents.BlurEvent;
  23. import com.vaadin.event.FieldEvents.BlurListener;
  24. import com.vaadin.event.FieldEvents.BlurNotifier;
  25. import com.vaadin.event.FieldEvents.FocusAndBlurServerRpcDecorator;
  26. import com.vaadin.event.FieldEvents.FocusEvent;
  27. import com.vaadin.event.FieldEvents.FocusListener;
  28. import com.vaadin.event.FieldEvents.FocusNotifier;
  29. import com.vaadin.server.SerializablePredicate;
  30. import com.vaadin.shared.Registration;
  31. import com.vaadin.shared.ui.optiongroup.CheckBoxGroupState;
  32. import com.vaadin.ui.declarative.DesignContext;
  33. import com.vaadin.ui.declarative.DesignFormatter;
  34. /**
  35. * A group of Checkboxes. Individual checkboxes are made from items supplied by
  36. * a {@link DataProvider}. Checkboxes may have captions and icons.
  37. *
  38. * @param <T>
  39. * item type
  40. * @author Vaadin Ltd
  41. * @since 8.0
  42. */
  43. public class CheckBoxGroup<T> extends AbstractMultiSelect<T>
  44. implements FocusNotifier, BlurNotifier, HasDataProvider<T> {
  45. /**
  46. * Constructs a new CheckBoxGroup with caption.
  47. *
  48. * @param caption
  49. * caption text
  50. */
  51. public CheckBoxGroup(String caption) {
  52. this();
  53. setCaption(caption);
  54. }
  55. /**
  56. * Constructs a new CheckBoxGroup with caption and DataProvider.
  57. *
  58. * @param caption
  59. * the caption text
  60. * @param dataProvider
  61. * the data provider, not null
  62. * @see HasDataProvider#setDataProvider(DataProvider)
  63. */
  64. public CheckBoxGroup(String caption, DataProvider<T, ?> dataProvider) {
  65. this(caption);
  66. setDataProvider(dataProvider);
  67. }
  68. /**
  69. * Constructs a new CheckBoxGroup with caption and DataProvider containing
  70. * given items.
  71. *
  72. * @param caption
  73. * the caption text
  74. * @param items
  75. * the data items to use, not null
  76. * @see #setItems(Collection)
  77. */
  78. public CheckBoxGroup(String caption, Collection<T> items) {
  79. this(caption, DataProvider.ofCollection(items));
  80. }
  81. /**
  82. * Constructs a new CheckBoxGroup.
  83. */
  84. public CheckBoxGroup() {
  85. registerRpc(new FocusAndBlurServerRpcDecorator(this, this::fireEvent));
  86. }
  87. /**
  88. * Sets whether html is allowed in the item captions. If set to true, the
  89. * captions are passed to the browser as html and the developer is
  90. * responsible for ensuring no harmful html is used. If set to false, the
  91. * content is passed to the browser as plain text.
  92. *
  93. * @param htmlContentAllowed
  94. * true if the captions are used as html, false if used as plain
  95. * text
  96. */
  97. public void setHtmlContentAllowed(boolean htmlContentAllowed) {
  98. getState().htmlContentAllowed = htmlContentAllowed;
  99. }
  100. /**
  101. * Checks whether captions are interpreted as html or plain text.
  102. *
  103. * @return true if the captions are used as html, false if used as plain
  104. * text
  105. * @see #setHtmlContentAllowed(boolean)
  106. */
  107. public boolean isHtmlContentAllowed() {
  108. return getState(false).htmlContentAllowed;
  109. }
  110. @Override
  111. protected CheckBoxGroupState getState() {
  112. return (CheckBoxGroupState) super.getState();
  113. }
  114. @Override
  115. protected CheckBoxGroupState getState(boolean markAsDirty) {
  116. return (CheckBoxGroupState) super.getState(markAsDirty);
  117. }
  118. @Override
  119. public IconGenerator<T> getItemIconGenerator() {
  120. return super.getItemIconGenerator();
  121. }
  122. @Override
  123. public void setItemIconGenerator(IconGenerator<T> itemIconGenerator) {
  124. super.setItemIconGenerator(itemIconGenerator);
  125. }
  126. @Override
  127. public SerializablePredicate<T> getItemEnabledProvider() {
  128. return super.getItemEnabledProvider();
  129. }
  130. @Override
  131. public void setItemEnabledProvider(
  132. SerializablePredicate<T> itemEnabledProvider) {
  133. super.setItemEnabledProvider(itemEnabledProvider);
  134. }
  135. @Override
  136. public Registration addFocusListener(FocusListener listener) {
  137. return addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
  138. FocusListener.focusMethod);
  139. }
  140. @Override
  141. public Registration addBlurListener(BlurListener listener) {
  142. return addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
  143. BlurListener.blurMethod);
  144. }
  145. @Override
  146. protected void readItems(Element design, DesignContext context) {
  147. setItemEnabledProvider(new DeclarativeItemEnabledProvider<>());
  148. super.readItems(design, context);
  149. }
  150. @SuppressWarnings({ "unchecked", "rawtypes" })
  151. @Override
  152. protected T readItem(Element child, Set<T> selected,
  153. DesignContext context) {
  154. T item = super.readItem(child, selected, context);
  155. SerializablePredicate<T> provider = getItemEnabledProvider();
  156. if (provider instanceof DeclarativeItemEnabledProvider) {
  157. if (child.hasAttr("disabled")) {
  158. ((DeclarativeItemEnabledProvider) provider).addDisabled(item);
  159. }
  160. } else {
  161. throw new IllegalStateException(String.format(
  162. "Don't know how "
  163. + "to disable item using current item enabled provider '%s'",
  164. provider.getClass().getName()));
  165. }
  166. return item;
  167. }
  168. @Override
  169. protected Element writeItem(Element design, T item, DesignContext context) {
  170. Element elem = super.writeItem(design, item, context);
  171. if (!getItemEnabledProvider().test(item)) {
  172. elem.attr("disabled", "");
  173. }
  174. if (isHtmlContentAllowed()) {
  175. // need to unencode HTML entities. AbstractMultiSelect.writeDesign
  176. // can't check if HTML content is allowed, so it always encodes
  177. // entities like '>', '<' and '&'; in case HTML content is allowed
  178. // this is undesirable so we need to unencode entities. Entities
  179. // other than '<' and '>' will be taken care by Jsoup.
  180. elem.html(DesignFormatter.decodeFromTextNode(elem.html()));
  181. }
  182. return elem;
  183. }
  184. @Override
  185. public DataProvider<T, ?> getDataProvider() {
  186. return internalGetDataProvider();
  187. }
  188. @Override
  189. public void setDataProvider(DataProvider<T, ?> dataProvider) {
  190. internalSetDataProvider(dataProvider);
  191. }
  192. }