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.

VOptionGroup.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.v7.client.ui;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Map;
  22. import com.google.gwt.aria.client.Roles;
  23. import com.google.gwt.core.client.Scheduler;
  24. import com.google.gwt.event.dom.client.BlurEvent;
  25. import com.google.gwt.event.dom.client.BlurHandler;
  26. import com.google.gwt.event.dom.client.ClickEvent;
  27. import com.google.gwt.event.dom.client.FocusEvent;
  28. import com.google.gwt.event.dom.client.FocusHandler;
  29. import com.google.gwt.event.dom.client.LoadEvent;
  30. import com.google.gwt.event.dom.client.LoadHandler;
  31. import com.google.gwt.event.shared.HandlerRegistration;
  32. import com.google.gwt.user.client.Command;
  33. import com.google.gwt.user.client.ui.CheckBox;
  34. import com.google.gwt.user.client.ui.FocusWidget;
  35. import com.google.gwt.user.client.ui.Focusable;
  36. import com.google.gwt.user.client.ui.HasEnabled;
  37. import com.google.gwt.user.client.ui.Panel;
  38. import com.google.gwt.user.client.ui.RadioButton;
  39. import com.google.gwt.user.client.ui.Widget;
  40. import com.vaadin.client.BrowserInfo;
  41. import com.vaadin.client.StyleConstants;
  42. import com.vaadin.client.UIDL;
  43. import com.vaadin.client.Util;
  44. import com.vaadin.client.WidgetUtil;
  45. import com.vaadin.client.ui.Icon;
  46. import com.vaadin.client.ui.VCheckBox;
  47. import com.vaadin.shared.EventId;
  48. import com.vaadin.v7.shared.ui.optiongroup.OptionGroupConstants;
  49. public class VOptionGroup extends VOptionGroupBase
  50. implements FocusHandler, BlurHandler {
  51. public static final String CLASSNAME = "v-select-optiongroup";
  52. /** For internal use only. May be removed or replaced in the future. */
  53. public final Panel panel;
  54. private final Map<CheckBox, String> optionsToKeys;
  55. private final Map<CheckBox, Boolean> optionsEnabled;
  56. /** For internal use only. May be removed or replaced in the future. */
  57. public boolean sendFocusEvents = false;
  58. /** For internal use only. May be removed or replaced in the future. */
  59. public boolean sendBlurEvents = false;
  60. /** For internal use only. May be removed or replaced in the future. */
  61. public List<HandlerRegistration> focusHandlers = null;
  62. /** For internal use only. May be removed or replaced in the future. */
  63. public List<HandlerRegistration> blurHandlers = null;
  64. private final LoadHandler iconLoadHandler = new LoadHandler() {
  65. @Override
  66. public void onLoad(LoadEvent event) {
  67. Util.notifyParentOfSizeChange(VOptionGroup.this, true);
  68. }
  69. };
  70. /**
  71. * used to check whether a blur really was a blur of the complete
  72. * optiongroup: if a control inside this optiongroup gains focus right after
  73. * blur of another control inside this optiongroup (meaning: if onFocus
  74. * fires after onBlur has fired), the blur and focus won't be sent to the
  75. * server side as only a focus change inside this optiongroup occurred
  76. */
  77. private boolean blurOccurred = false;
  78. /** For internal use only. May be removed or replaced in the future. */
  79. public boolean htmlContentAllowed = false;
  80. private boolean wasHtmlContentAllowed = false;
  81. private boolean wasMultiselect = false;
  82. public VOptionGroup() {
  83. super(CLASSNAME);
  84. panel = (Panel) optionsContainer;
  85. optionsToKeys = new HashMap<CheckBox, String>();
  86. optionsEnabled = new HashMap<CheckBox, Boolean>();
  87. wasMultiselect = isMultiselect();
  88. }
  89. /*
  90. * Try to update content of existing elements, rebuild panel entirely
  91. * otherwise
  92. */
  93. @Override
  94. public void buildOptions(UIDL uidl) {
  95. /*
  96. * In order to retain focus, we need to update values rather than
  97. * recreate panel from scratch (#10451). However, the panel will be
  98. * rebuilt (losing focus) if number of elements or their order is
  99. * changed.
  100. */
  101. Map<String, CheckBox> keysToOptions = new HashMap<String, CheckBox>();
  102. for (Map.Entry<CheckBox, String> entry : optionsToKeys.entrySet()) {
  103. keysToOptions.put(entry.getValue(), entry.getKey());
  104. }
  105. List<Widget> existingwidgets = new ArrayList<Widget>();
  106. List<Widget> newwidgets = new ArrayList<Widget>();
  107. // Get current order of elements
  108. for (Widget wid : panel) {
  109. existingwidgets.add(wid);
  110. }
  111. optionsEnabled.clear();
  112. if (isMultiselect()) {
  113. Roles.getGroupRole().set(getElement());
  114. } else {
  115. Roles.getRadiogroupRole().set(getElement());
  116. }
  117. for (final Object child : uidl) {
  118. final UIDL opUidl = (UIDL) child;
  119. String itemHtml = opUidl.getStringAttribute("caption");
  120. if (!htmlContentAllowed) {
  121. itemHtml = WidgetUtil.escapeHTML(itemHtml);
  122. }
  123. String iconUrl = opUidl.getStringAttribute("icon");
  124. if (iconUrl != null && !iconUrl.isEmpty()) {
  125. Icon icon = client.getIcon(iconUrl);
  126. itemHtml = icon.getElement().getString() + itemHtml;
  127. }
  128. String key = opUidl.getStringAttribute("key");
  129. CheckBox op = keysToOptions.get(key);
  130. // Need to recreate object if isMultiselect is changed (#10451)
  131. // OR if htmlContentAllowed changed due to Safari 5 issue
  132. if ((op == null) || (htmlContentAllowed != wasHtmlContentAllowed)
  133. || (isMultiselect() != wasMultiselect)) {
  134. // Create a new element
  135. if (isMultiselect()) {
  136. op = new VCheckBox();
  137. } else {
  138. op = new RadioButton(paintableId);
  139. op.setStyleName("v-radiobutton");
  140. }
  141. if (iconUrl != null && !iconUrl.isEmpty()) {
  142. WidgetUtil.sinkOnloadForImages(op.getElement());
  143. op.addHandler(iconLoadHandler, LoadEvent.getType());
  144. }
  145. op.addStyleName(CLASSNAME_OPTION);
  146. op.addClickHandler(this);
  147. optionsToKeys.put(op, key);
  148. }
  149. op.setHTML(itemHtml);
  150. op.setValue(opUidl.getBooleanAttribute("selected"));
  151. boolean optionEnabled = !opUidl.getBooleanAttribute(
  152. OptionGroupConstants.ATTRIBUTE_OPTION_DISABLED);
  153. boolean enabled = optionEnabled && !isReadonly() && isEnabled();
  154. op.setEnabled(enabled);
  155. optionsEnabled.put(op, optionEnabled);
  156. setStyleName(op.getElement(), StyleConstants.DISABLED,
  157. !(optionEnabled && isEnabled()));
  158. newwidgets.add(op);
  159. }
  160. if (!newwidgets.equals(existingwidgets)) {
  161. // Rebuild the panel, losing focus
  162. panel.clear();
  163. for (Widget wid : newwidgets) {
  164. panel.add(wid);
  165. }
  166. }
  167. wasHtmlContentAllowed = htmlContentAllowed;
  168. wasMultiselect = isMultiselect();
  169. }
  170. @Override
  171. protected String[] getSelectedItems() {
  172. return selectedKeys.toArray(new String[selectedKeys.size()]);
  173. }
  174. @Override
  175. public void onClick(ClickEvent event) {
  176. super.onClick(event);
  177. if (event.getSource() instanceof CheckBox) {
  178. CheckBox source = (CheckBox) event.getSource();
  179. if (!source.isEnabled()) {
  180. // Click events on the text are received even though the
  181. // checkbox is disabled
  182. return;
  183. }
  184. if (BrowserInfo.get().isWebkit()) {
  185. // Webkit does not focus non-text input elements on click
  186. // (#11854)
  187. source.setFocus(true);
  188. }
  189. final boolean selected = source.getValue();
  190. final String key = optionsToKeys.get(source);
  191. if (!isMultiselect()) {
  192. selectedKeys.clear();
  193. }
  194. if (selected) {
  195. selectedKeys.add(key);
  196. } else {
  197. selectedKeys.remove(key);
  198. }
  199. client.updateVariable(paintableId, "selected", getSelectedItems(),
  200. isImmediate());
  201. }
  202. }
  203. @Override
  204. public void setTabIndex(int tabIndex) {
  205. for (Widget widget : panel) {
  206. ((FocusWidget) widget).setTabIndex(tabIndex);
  207. }
  208. }
  209. @Override
  210. protected void updateEnabledState() {
  211. boolean optionGroupEnabled = isEnabled() && !isReadonly();
  212. // sets options enabled according to the widget's enabled,
  213. // readonly and each options own enabled
  214. for (Widget w : panel) {
  215. if (w instanceof HasEnabled) {
  216. HasEnabled hasEnabled = (HasEnabled) w;
  217. Boolean isOptionEnabled = optionsEnabled.get(w);
  218. if (isOptionEnabled == null) {
  219. hasEnabled.setEnabled(optionGroupEnabled);
  220. setStyleName(w.getElement(), StyleConstants.DISABLED,
  221. !isEnabled());
  222. } else {
  223. hasEnabled
  224. .setEnabled(isOptionEnabled && optionGroupEnabled);
  225. setStyleName(w.getElement(), StyleConstants.DISABLED,
  226. !(isOptionEnabled && isEnabled()));
  227. }
  228. }
  229. }
  230. }
  231. @Override
  232. public void focus() {
  233. Iterator<Widget> it = panel.iterator();
  234. if (it.hasNext()) {
  235. ((Focusable) it.next()).setFocus(true);
  236. }
  237. }
  238. @Override
  239. public void onFocus(FocusEvent arg0) {
  240. if (!blurOccurred) {
  241. // no blur occurred before this focus event
  242. // panel was blurred => fire the event to the server side if
  243. // requested by server side
  244. if (sendFocusEvents) {
  245. client.updateVariable(paintableId, EventId.FOCUS, "", true);
  246. }
  247. } else {
  248. // blur occurred before this focus event
  249. // another control inside the panel (checkbox / radio box) was
  250. // blurred => do not fire the focus and set blurOccurred to false,
  251. // so
  252. // blur will not be fired, too
  253. blurOccurred = false;
  254. }
  255. }
  256. @Override
  257. public void onBlur(BlurEvent arg0) {
  258. blurOccurred = true;
  259. if (sendBlurEvents) {
  260. Scheduler.get().scheduleDeferred(new Command() {
  261. @Override
  262. public void execute() {
  263. // check whether blurOccurred still is true and then send
  264. // the event out to the server
  265. if (blurOccurred) {
  266. client.updateVariable(paintableId, EventId.BLUR, "",
  267. true);
  268. blurOccurred = false;
  269. }
  270. }
  271. });
  272. }
  273. }
  274. }