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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 com.vaadin.data.Listing;
  19. import com.vaadin.event.FieldEvents.BlurEvent;
  20. import com.vaadin.event.FieldEvents.BlurListener;
  21. import com.vaadin.event.FieldEvents.BlurNotifier;
  22. import com.vaadin.event.FieldEvents.FocusAndBlurServerRpcDecorator;
  23. import com.vaadin.event.FieldEvents.FocusEvent;
  24. import com.vaadin.event.FieldEvents.FocusListener;
  25. import com.vaadin.event.FieldEvents.FocusNotifier;
  26. import com.vaadin.server.SerializablePredicate;
  27. import com.vaadin.server.data.DataSource;
  28. import com.vaadin.shared.Registration;
  29. import com.vaadin.shared.ui.optiongroup.CheckBoxGroupState;
  30. /**
  31. * A group of Checkboxes. Individual checkboxes are made from items supplied by
  32. * a {@link DataSource}. Checkboxes may have captions and icons.
  33. *
  34. * @param <T>
  35. * item type
  36. * @author Vaadin Ltd
  37. * @since 8.0
  38. */
  39. public class CheckBoxGroup<T> extends AbstractMultiSelect<T>
  40. implements FocusNotifier, BlurNotifier {
  41. /**
  42. * Constructs a new CheckBoxGroup with caption.
  43. *
  44. * @param caption
  45. * caption text
  46. * @see Listing#setDataSource(DataSource)
  47. */
  48. public CheckBoxGroup(String caption) {
  49. this();
  50. setCaption(caption);
  51. }
  52. /**
  53. * Constructs a new CheckBoxGroup 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 CheckBoxGroup(String caption, DataSource<T> dataSource) {
  62. this(caption);
  63. setDataSource(dataSource);
  64. }
  65. /**
  66. * Constructs a new CheckBoxGroup 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 CheckBoxGroup(String caption, Collection<T> items) {
  76. this(caption, DataSource.create(items));
  77. }
  78. /**
  79. * Constructs a new CheckBoxGroup.
  80. *
  81. * @see Listing#setDataSource(DataSource)
  82. */
  83. public CheckBoxGroup() {
  84. registerRpc(new FocusAndBlurServerRpcDecorator(this, this::fireEvent));
  85. }
  86. /**
  87. * Sets whether html is allowed in the item captions. If set to true, the
  88. * captions are passed to the browser as html and the developer is
  89. * responsible for ensuring no harmful html is used. If set to false, the
  90. * content is passed to the browser as plain text.
  91. *
  92. * @param htmlContentAllowed
  93. * true if the captions are used as html, false if used as plain
  94. * text
  95. */
  96. public void setHtmlContentAllowed(boolean htmlContentAllowed) {
  97. getState().htmlContentAllowed = htmlContentAllowed;
  98. }
  99. /**
  100. * Checks whether captions are interpreted as html or plain text.
  101. *
  102. * @return true if the captions are used as html, false if used as plain
  103. * text
  104. * @see #setHtmlContentAllowed(boolean)
  105. */
  106. public boolean isHtmlContentAllowed() {
  107. return getState(false).htmlContentAllowed;
  108. }
  109. @Override
  110. protected CheckBoxGroupState getState() {
  111. return (CheckBoxGroupState) super.getState();
  112. }
  113. @Override
  114. protected CheckBoxGroupState getState(boolean markAsDirty) {
  115. return (CheckBoxGroupState) super.getState(markAsDirty);
  116. }
  117. @Override
  118. public IconGenerator<T> getItemIconGenerator() {
  119. return super.getItemIconGenerator();
  120. }
  121. @Override
  122. public void setItemIconGenerator(IconGenerator<T> itemIconGenerator) {
  123. super.setItemIconGenerator(itemIconGenerator);
  124. }
  125. @Override
  126. public SerializablePredicate<T> getItemEnabledProvider() {
  127. return super.getItemEnabledProvider();
  128. }
  129. @Override
  130. public void setItemEnabledProvider(
  131. SerializablePredicate<T> itemEnabledProvider) {
  132. super.setItemEnabledProvider(itemEnabledProvider);
  133. }
  134. @Override
  135. public Registration addFocusListener(FocusListener listener) {
  136. addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
  137. FocusListener.focusMethod);
  138. return () -> removeListener(FocusEvent.EVENT_ID, FocusEvent.class,
  139. listener);
  140. }
  141. @Override
  142. @Deprecated
  143. public void removeFocusListener(FocusListener listener) {
  144. removeListener(FocusEvent.EVENT_ID, FocusEvent.class, listener);
  145. }
  146. @Override
  147. public Registration addBlurListener(BlurListener listener) {
  148. addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
  149. BlurListener.blurMethod);
  150. return () -> removeListener(BlurEvent.EVENT_ID, BlurEvent.class,
  151. listener);
  152. }
  153. @Override
  154. @Deprecated
  155. public void removeBlurListener(BlurListener listener) {
  156. removeListener(BlurEvent.EVENT_ID, BlurEvent.class, listener);
  157. }
  158. }