您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VCheckBoxGroup.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 2000-2018 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.core.client.Scheduler;
  24. import com.google.gwt.dom.client.Element;
  25. import com.google.gwt.event.dom.client.ClickEvent;
  26. import com.google.gwt.event.dom.client.ClickHandler;
  27. import com.google.gwt.user.client.ui.FocusWidget;
  28. import com.google.gwt.user.client.ui.HasEnabled;
  29. import com.google.gwt.user.client.ui.Widget;
  30. import com.vaadin.client.ApplicationConnection;
  31. import com.vaadin.client.BrowserInfo;
  32. import com.vaadin.client.StyleConstants;
  33. import com.vaadin.client.WidgetUtil;
  34. import com.vaadin.client.widgets.FocusableFlowPanelComposite;
  35. import com.vaadin.shared.Registration;
  36. import com.vaadin.shared.ui.ListingJsonConstants;
  37. import elemental.json.JsonObject;
  38. /**
  39. * The client-side widget for the {@code CheckBoxGroup} component.
  40. *
  41. * @author Vaadin Ltd.
  42. * @since 8.0
  43. */
  44. public class VCheckBoxGroup extends FocusableFlowPanelComposite
  45. implements Field, ClickHandler, HasEnabled {
  46. public static final String CLASSNAME = "v-select-optiongroup";
  47. public static final String CLASSNAME_OPTION = "v-select-option";
  48. public static final String CLASSNAME_OPTION_SELECTED = "v-select-option-selected";
  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. private boolean htmlContentAllowed = false;
  55. private boolean enabled;
  56. private boolean readonly;
  57. private List<BiConsumer<JsonObject, Boolean>> selectionChangeListeners;
  58. public VCheckBoxGroup() {
  59. getWidget().setStyleName(CLASSNAME);
  60. optionsToItems = new HashMap<>();
  61. selectionChangeListeners = new ArrayList<>();
  62. }
  63. /*
  64. * Build all the options
  65. */
  66. public void buildOptions(List<JsonObject> items) {
  67. Roles.getGroupRole().set(getElement());
  68. int i = 0;
  69. int widgetsToRemove = getWidget().getWidgetCount() - items.size();
  70. if (widgetsToRemove < 0) {
  71. widgetsToRemove = 0;
  72. }
  73. List<Widget> remove = new ArrayList<>(widgetsToRemove);
  74. for (Widget widget : getWidget()) {
  75. if (i < items.size()) {
  76. updateItem((VCheckBox) widget, items.get(i), false);
  77. i++;
  78. } else {
  79. remove.add(widget);
  80. }
  81. }
  82. remove.stream().forEach(this::remove);
  83. while (i < items.size()) {
  84. updateItem(new VCheckBox(), items.get(i), true);
  85. i++;
  86. }
  87. }
  88. /**
  89. * Returns the JsonObject used to populate the CheckBox widget that contains
  90. * given Element.
  91. *
  92. * @since 8.2
  93. * @param element
  94. * the element to search for
  95. * @return the related JsonObject; {@code null} if not found
  96. */
  97. public JsonObject getItem(Element element) {
  98. return optionsToItems.entrySet().stream()
  99. .filter(entry -> entry.getKey().getElement()
  100. .isOrHasChild(element))
  101. .map(entry -> entry.getValue()).findFirst().orElse(null);
  102. }
  103. private void remove(Widget widget) {
  104. getWidget().remove(widget);
  105. optionsToItems.remove(widget);
  106. }
  107. private void updateItem(VCheckBox widget, JsonObject item,
  108. boolean requireInitialization) {
  109. String itemHtml = item
  110. .getString(ListingJsonConstants.JSONKEY_ITEM_VALUE);
  111. if (!isHtmlContentAllowed()) {
  112. itemHtml = WidgetUtil.escapeHTML(itemHtml);
  113. }
  114. String iconUrl = item.getString(ListingJsonConstants.JSONKEY_ITEM_ICON);
  115. if (iconUrl != null && !iconUrl.isEmpty()) {
  116. Icon icon = client.getIcon(iconUrl);
  117. itemHtml = icon.getElement().getString() + itemHtml;
  118. }
  119. widget.setHTML(itemHtml);
  120. widget.setValue(
  121. item.getBoolean(ListingJsonConstants.JSONKEY_ITEM_SELECTED));
  122. setOptionEnabled(widget, item);
  123. setOptionReadOnly(widget, item);
  124. widget.setStyleName(CLASSNAME_OPTION_SELECTED, widget.getValue());
  125. if (requireInitialization) {
  126. widget.addStyleName(CLASSNAME_OPTION);
  127. widget.addClickHandler(this);
  128. getWidget().add(widget);
  129. }
  130. optionsToItems.put(widget, item);
  131. }
  132. @Override
  133. public void onClick(ClickEvent event) {
  134. if (event.getSource() instanceof VCheckBox) {
  135. VCheckBox source = (VCheckBox) event.getSource();
  136. if (!source.isEnabled()) {
  137. // Click events on the text are received even though the
  138. // checkbox is disabled
  139. return;
  140. }
  141. if (BrowserInfo.get().isWebkit() || BrowserInfo.get().isIE11()) {
  142. // Webkit does not focus non-text input elements on click
  143. // (#11854)
  144. source.setFocus(true);
  145. }
  146. Boolean selected = source.getValue();
  147. JsonObject item = optionsToItems.get(source);
  148. assert item != null;
  149. new ArrayList<>(selectionChangeListeners)
  150. .forEach(listener -> listener.accept(item, selected));
  151. }
  152. }
  153. public void setTabIndex(int tabIndex) {
  154. for (Widget anOptionsContainer : getWidget()) {
  155. FocusWidget widget = (FocusWidget) anOptionsContainer;
  156. widget.setTabIndex(tabIndex);
  157. }
  158. }
  159. /**
  160. * Updates the checkbox's enabled state according to the widget's enabled,
  161. * read only and the item's enabled.
  162. *
  163. * @param checkBox
  164. * the checkbox to update
  165. * @param item
  166. * the item for the checkbox
  167. */
  168. protected void setOptionEnabled(VCheckBox checkBox, JsonObject item) {
  169. boolean optionEnabled = !item
  170. .getBoolean(ListingJsonConstants.JSONKEY_ITEM_DISABLED);
  171. boolean enabled = optionEnabled && !isReadonly() && isEnabled();
  172. checkBox.setEnabled(enabled);
  173. // #9258 apply the v-disabled class when disabled for UX
  174. checkBox.setStyleName(StyleConstants.DISABLED,
  175. !isEnabled() || !optionEnabled);
  176. }
  177. protected void setOptionReadOnly(VCheckBox checkBox, JsonObject item) {
  178. if (isReadonly()) {
  179. checkBox.addStyleName("v-readonly");
  180. checkBox.setEnabled(false);
  181. } else {
  182. checkBox.removeStyleName("v-readonly");
  183. boolean optionEnabled = !item
  184. .getBoolean(ListingJsonConstants.JSONKEY_ITEM_DISABLED);
  185. checkBox.setEnabled(isEnabled() && optionEnabled);
  186. }
  187. }
  188. public boolean isHtmlContentAllowed() {
  189. return htmlContentAllowed;
  190. }
  191. public void setHtmlContentAllowed(boolean htmlContentAllowed) {
  192. this.htmlContentAllowed = htmlContentAllowed;
  193. }
  194. @Override
  195. public boolean isEnabled() {
  196. return enabled;
  197. }
  198. public boolean isReadonly() {
  199. return readonly;
  200. }
  201. public void setReadonly(boolean readonly) {
  202. if (this.readonly != readonly) {
  203. this.readonly = readonly;
  204. optionsToItems.forEach(this::setOptionReadOnly);
  205. }
  206. }
  207. @Override
  208. public void setEnabled(boolean enabled) {
  209. if (this.enabled != enabled) {
  210. this.enabled = enabled;
  211. optionsToItems.forEach(this::setOptionEnabled);
  212. }
  213. }
  214. public Registration addSelectionChangeHandler(
  215. BiConsumer<JsonObject, Boolean> selectionChanged) {
  216. selectionChangeListeners.add(selectionChanged);
  217. return (Registration) () -> selectionChangeListeners
  218. .remove(selectionChanged);
  219. }
  220. /**
  221. * Set focus to the first check box.
  222. */
  223. @Override
  224. public void focus() {
  225. // If focus is set on creation, need to wait until options are populated
  226. Scheduler.get().scheduleDeferred(() -> {
  227. getWidget().focusFirstEnabledChild();
  228. });
  229. }
  230. }