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

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