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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import java.util.Map;
  10. import com.google.gwt.core.client.Scheduler;
  11. import com.google.gwt.event.dom.client.BlurEvent;
  12. import com.google.gwt.event.dom.client.BlurHandler;
  13. import com.google.gwt.event.dom.client.ClickEvent;
  14. import com.google.gwt.event.dom.client.FocusEvent;
  15. import com.google.gwt.event.dom.client.FocusHandler;
  16. import com.google.gwt.event.dom.client.LoadEvent;
  17. import com.google.gwt.event.dom.client.LoadHandler;
  18. import com.google.gwt.event.shared.HandlerRegistration;
  19. import com.google.gwt.user.client.Command;
  20. import com.google.gwt.user.client.ui.CheckBox;
  21. import com.google.gwt.user.client.ui.FocusWidget;
  22. import com.google.gwt.user.client.ui.Focusable;
  23. import com.google.gwt.user.client.ui.Panel;
  24. import com.google.gwt.user.client.ui.RadioButton;
  25. import com.google.gwt.user.client.ui.Widget;
  26. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  27. import com.vaadin.terminal.gwt.client.EventId;
  28. import com.vaadin.terminal.gwt.client.UIDL;
  29. import com.vaadin.terminal.gwt.client.Util;
  30. public class VOptionGroup extends VOptionGroupBase implements FocusHandler,
  31. BlurHandler {
  32. public static final String HTML_CONTENT_ALLOWED = "usehtml";
  33. public static final String CLASSNAME = "v-select-optiongroup";
  34. private final Panel panel;
  35. private final Map<CheckBox, String> optionsToKeys;
  36. private boolean sendFocusEvents = false;
  37. private boolean sendBlurEvents = false;
  38. private List<HandlerRegistration> focusHandlers = null;
  39. private List<HandlerRegistration> blurHandlers = null;
  40. private final LoadHandler iconLoadHandler = new LoadHandler() {
  41. public void onLoad(LoadEvent event) {
  42. Util.notifyParentOfSizeChange(VOptionGroup.this, true);
  43. }
  44. };
  45. /**
  46. * used to check whether a blur really was a blur of the complete
  47. * optiongroup: if a control inside this optiongroup gains focus right after
  48. * blur of another control inside this optiongroup (meaning: if onFocus
  49. * fires after onBlur has fired), the blur and focus won't be sent to the
  50. * server side as only a focus change inside this optiongroup occured
  51. */
  52. private boolean blurOccured = false;
  53. private boolean htmlContentAllowed = false;
  54. public VOptionGroup() {
  55. super(CLASSNAME);
  56. panel = (Panel) optionsContainer;
  57. optionsToKeys = new HashMap<CheckBox, String>();
  58. }
  59. @Override
  60. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  61. htmlContentAllowed = uidl.hasAttribute(HTML_CONTENT_ALLOWED);
  62. super.updateFromUIDL(uidl, client);
  63. sendFocusEvents = client.hasEventListeners(this, EventId.FOCUS);
  64. sendBlurEvents = client.hasEventListeners(this, EventId.BLUR);
  65. if (focusHandlers != null) {
  66. for (HandlerRegistration reg : focusHandlers) {
  67. reg.removeHandler();
  68. }
  69. focusHandlers.clear();
  70. focusHandlers = null;
  71. for (HandlerRegistration reg : blurHandlers) {
  72. reg.removeHandler();
  73. }
  74. blurHandlers.clear();
  75. blurHandlers = null;
  76. }
  77. if (sendFocusEvents || sendBlurEvents) {
  78. focusHandlers = new ArrayList<HandlerRegistration>();
  79. blurHandlers = new ArrayList<HandlerRegistration>();
  80. // add focus and blur handlers to checkboxes / radio buttons
  81. for (Widget wid : panel) {
  82. if (wid instanceof CheckBox) {
  83. focusHandlers.add(((CheckBox) wid).addFocusHandler(this));
  84. blurHandlers.add(((CheckBox) wid).addBlurHandler(this));
  85. }
  86. }
  87. }
  88. }
  89. /*
  90. * Return true if no elements were changed, false otherwise.
  91. */
  92. @Override
  93. protected void buildOptions(UIDL uidl) {
  94. panel.clear();
  95. for (final Iterator<?> it = uidl.getChildIterator(); it.hasNext();) {
  96. final UIDL opUidl = (UIDL) it.next();
  97. CheckBox op;
  98. String itemHtml = opUidl.getStringAttribute("caption");
  99. if (!htmlContentAllowed) {
  100. itemHtml = Util.escapeHTML(itemHtml);
  101. }
  102. String icon = opUidl.getStringAttribute("icon");
  103. if (icon != null && icon.length() != 0) {
  104. String iconUrl = client.translateVaadinUri(icon);
  105. itemHtml = "<img src=\"" + iconUrl + "\" class=\""
  106. + Icon.CLASSNAME + "\" alt=\"\" />" + itemHtml;
  107. }
  108. if (isMultiselect()) {
  109. op = new VCheckBox();
  110. op.setHTML(itemHtml);
  111. } else {
  112. op = new RadioButton(id, itemHtml, true);
  113. op.setStyleName("v-radiobutton");
  114. }
  115. if (icon != null && icon.length() != 0) {
  116. Util.sinkOnloadForImages(op.getElement());
  117. op.addHandler(iconLoadHandler, LoadEvent.getType());
  118. }
  119. op.addStyleName(CLASSNAME_OPTION);
  120. op.setValue(opUidl.getBooleanAttribute("selected"));
  121. boolean enabled = !opUidl.getBooleanAttribute("disabled")
  122. && !isReadonly() && !isDisabled();
  123. op.setEnabled(enabled);
  124. setStyleName(op.getElement(),
  125. ApplicationConnection.DISABLED_CLASSNAME, !enabled);
  126. op.addClickHandler(this);
  127. optionsToKeys.put(op, opUidl.getStringAttribute("key"));
  128. panel.add(op);
  129. }
  130. }
  131. @Override
  132. protected String[] getSelectedItems() {
  133. return selectedKeys.toArray(new String[selectedKeys.size()]);
  134. }
  135. @Override
  136. public void onClick(ClickEvent event) {
  137. super.onClick(event);
  138. if (event.getSource() instanceof CheckBox) {
  139. final boolean selected = ((CheckBox) event.getSource()).getValue();
  140. final String key = optionsToKeys.get(event.getSource());
  141. if (!isMultiselect()) {
  142. selectedKeys.clear();
  143. }
  144. if (selected) {
  145. selectedKeys.add(key);
  146. } else {
  147. selectedKeys.remove(key);
  148. }
  149. client.updateVariable(id, "selected", getSelectedItems(),
  150. isImmediate());
  151. }
  152. }
  153. @Override
  154. protected void setTabIndex(int tabIndex) {
  155. for (Iterator<Widget> iterator = panel.iterator(); iterator.hasNext();) {
  156. FocusWidget widget = (FocusWidget) iterator.next();
  157. widget.setTabIndex(tabIndex);
  158. }
  159. }
  160. public void focus() {
  161. Iterator<Widget> iterator = panel.iterator();
  162. if (iterator.hasNext()) {
  163. ((Focusable) iterator.next()).setFocus(true);
  164. }
  165. }
  166. public void onFocus(FocusEvent arg0) {
  167. if (!blurOccured) {
  168. // no blur occured before this focus event
  169. // panel was blurred => fire the event to the server side if
  170. // requested by server side
  171. if (sendFocusEvents) {
  172. client.updateVariable(id, EventId.FOCUS, "", true);
  173. }
  174. } else {
  175. // blur occured before this focus event
  176. // another control inside the panel (checkbox / radio box) was
  177. // blurred => do not fire the focus and set blurOccured to false, so
  178. // blur will not be fired, too
  179. blurOccured = false;
  180. }
  181. }
  182. public void onBlur(BlurEvent arg0) {
  183. blurOccured = true;
  184. if (sendBlurEvents) {
  185. Scheduler.get().scheduleDeferred(new Command() {
  186. public void execute() {
  187. // check whether blurOccured still is true and then send the
  188. // event out to the server
  189. if (blurOccured) {
  190. client.updateVariable(id, EventId.BLUR, "", true);
  191. blurOccured = false;
  192. }
  193. }
  194. });
  195. }
  196. }
  197. public Widget getWidgetForPaintable() {
  198. return this;
  199. }
  200. }