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.

VCheckBoxGroup.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.client.ui;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.function.BiConsumer;
  22. import com.google.gwt.aria.client.Roles;
  23. import com.google.gwt.event.dom.client.BlurHandler;
  24. import com.google.gwt.event.dom.client.ClickEvent;
  25. import com.google.gwt.event.dom.client.ClickHandler;
  26. import com.google.gwt.event.dom.client.FocusHandler;
  27. import com.google.gwt.event.dom.client.HasAllFocusHandlers;
  28. import com.google.gwt.event.shared.HandlerRegistration;
  29. import com.google.gwt.user.client.ui.Composite;
  30. import com.google.gwt.user.client.ui.FocusWidget;
  31. import com.google.gwt.user.client.ui.HasEnabled;
  32. import com.google.gwt.user.client.ui.Widget;
  33. import com.vaadin.client.ApplicationConnection;
  34. import com.vaadin.client.WidgetUtil;
  35. import com.vaadin.client.widgets.ChildFocusAwareFlowPanel;
  36. import com.vaadin.shared.Registration;
  37. import com.vaadin.shared.ui.ListingJsonConstants;
  38. import elemental.json.JsonObject;
  39. /**
  40. * The client-side widget for the {@code CheckBoxGroup} component.
  41. *
  42. * @author Vaadin Ltd.
  43. * @since 8.0
  44. */
  45. public class VCheckBoxGroup extends Composite implements Field, ClickHandler,
  46. com.vaadin.client.Focusable, HasEnabled, HasAllFocusHandlers {
  47. public static final String CLASSNAME = "v-select-optiongroup";
  48. public static final String CLASSNAME_OPTION = "v-select-option";
  49. private final Map<VCheckBox, JsonObject> optionsToItems;
  50. /**
  51. * For internal use only. May be removed or replaced in the future.
  52. */
  53. public ApplicationConnection client;
  54. /**
  55. * Widget holding the different options (e.g. ListBox or Panel for radio
  56. * buttons) (optional, fallbacks to container Panel)
  57. * <p>
  58. * For internal use only. May be removed or replaced in the future.
  59. */
  60. private ChildFocusAwareFlowPanel optionsContainer;
  61. private boolean htmlContentAllowed = false;
  62. private boolean enabled;
  63. private boolean readonly;
  64. private List<BiConsumer<JsonObject, Boolean>> selectionChangeListeners;
  65. public VCheckBoxGroup() {
  66. optionsContainer = new ChildFocusAwareFlowPanel();
  67. initWidget(optionsContainer);
  68. optionsContainer.setStyleName(CLASSNAME);
  69. optionsToItems = new HashMap<>();
  70. selectionChangeListeners = new ArrayList<>();
  71. }
  72. /*
  73. * Build all the options
  74. */
  75. public void buildOptions(List<JsonObject> items) {
  76. Roles.getGroupRole().set(getElement());
  77. int i = 0;
  78. int widgetsToRemove = optionsContainer.getWidgetCount() - items.size();
  79. if (widgetsToRemove < 0) {
  80. widgetsToRemove = 0;
  81. }
  82. List<Widget> remove = new ArrayList<>(widgetsToRemove);
  83. for (Widget widget : optionsContainer) {
  84. if (i < items.size()) {
  85. updateItem((VCheckBox) widget, items.get(i), false);
  86. i++;
  87. } else {
  88. remove.add(widget);
  89. }
  90. }
  91. remove.stream().forEach(this::remove);
  92. while (i < items.size()) {
  93. updateItem(new VCheckBox(), items.get(i), true);
  94. i++;
  95. }
  96. }
  97. private void remove(Widget widget) {
  98. optionsContainer.remove(widget);
  99. optionsToItems.remove(widget);
  100. }
  101. private void updateItem(VCheckBox widget, JsonObject item,
  102. boolean requireInitializations) {
  103. String itemHtml = item
  104. .getString(ListingJsonConstants.JSONKEY_ITEM_VALUE);
  105. if (!isHtmlContentAllowed()) {
  106. itemHtml = WidgetUtil.escapeHTML(itemHtml);
  107. }
  108. String iconUrl = item.getString(ListingJsonConstants.JSONKEY_ITEM_ICON);
  109. if (iconUrl != null && iconUrl.length() != 0) {
  110. Icon icon = client.getIcon(iconUrl);
  111. itemHtml = icon.getElement().getString() + itemHtml;
  112. }
  113. widget.setHTML(itemHtml);
  114. widget.setValue(
  115. item.getBoolean(ListingJsonConstants.JSONKEY_ITEM_SELECTED));
  116. setOptionEnabled(widget, item);
  117. if (requireInitializations) {
  118. widget.addStyleName(CLASSNAME_OPTION);
  119. widget.addClickHandler(this);
  120. optionsContainer.add(widget);
  121. }
  122. optionsToItems.put(widget, item);
  123. }
  124. @Override
  125. public void onClick(ClickEvent event) {
  126. if (event.getSource() instanceof VCheckBox) {
  127. VCheckBox source = (VCheckBox) event.getSource();
  128. if (!source.isEnabled()) {
  129. // Click events on the text are received even though the
  130. // checkbox is disabled
  131. return;
  132. }
  133. Boolean selected = source.getValue();
  134. JsonObject item = optionsToItems.get(source);
  135. assert item != null;
  136. new ArrayList<>(selectionChangeListeners)
  137. .forEach(listener -> listener.accept(item, selected));
  138. }
  139. }
  140. public void setTabIndex(int tabIndex) {
  141. for (Widget anOptionsContainer : optionsContainer) {
  142. FocusWidget widget = (FocusWidget) anOptionsContainer;
  143. widget.setTabIndex(tabIndex);
  144. }
  145. }
  146. /**
  147. * Updates the checkbox's enabled state according to the widget's enabled,
  148. * read only and the item's enabled.
  149. *
  150. * @param checkBox
  151. * the checkbox to update
  152. * @param item
  153. * the item for the checkbox
  154. */
  155. protected void setOptionEnabled(VCheckBox checkBox, JsonObject item) {
  156. boolean optionEnabled = !item
  157. .getBoolean(ListingJsonConstants.JSONKEY_ITEM_DISABLED);
  158. boolean enabled = optionEnabled && !isReadonly() && isEnabled();
  159. checkBox.setEnabled(enabled);
  160. }
  161. @Override
  162. public void focus() {
  163. optionsContainer.focus();
  164. }
  165. public boolean isHtmlContentAllowed() {
  166. return htmlContentAllowed;
  167. }
  168. public void setHtmlContentAllowed(boolean htmlContentAllowed) {
  169. this.htmlContentAllowed = htmlContentAllowed;
  170. }
  171. @Override
  172. public boolean isEnabled() {
  173. return enabled;
  174. }
  175. public boolean isReadonly() {
  176. return readonly;
  177. }
  178. public void setReadonly(boolean readonly) {
  179. if (this.readonly != readonly) {
  180. this.readonly = readonly;
  181. optionsToItems.forEach(this::setOptionEnabled);
  182. }
  183. }
  184. @Override
  185. public void setEnabled(boolean enabled) {
  186. if (this.enabled != enabled) {
  187. this.enabled = enabled;
  188. optionsToItems.forEach(this::setOptionEnabled);
  189. }
  190. }
  191. public Registration addSelectionChangeHandler(
  192. BiConsumer<JsonObject, Boolean> selectionChanged) {
  193. selectionChangeListeners.add(selectionChanged);
  194. return (Registration) () -> selectionChangeListeners
  195. .remove(selectionChanged);
  196. }
  197. @Override
  198. public HandlerRegistration addFocusHandler(FocusHandler handler) {
  199. return optionsContainer.addFocusHandler(handler);
  200. }
  201. @Override
  202. public HandlerRegistration addBlurHandler(BlurHandler handler) {
  203. return optionsContainer.addBlurHandler(handler);
  204. }
  205. }