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.

EventHelper.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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;
  17. import static com.vaadin.shared.EventId.BLUR;
  18. import static com.vaadin.shared.EventId.FOCUS;
  19. import com.google.gwt.event.dom.client.BlurEvent;
  20. import com.google.gwt.event.dom.client.BlurHandler;
  21. import com.google.gwt.event.dom.client.DomEvent.Type;
  22. import com.google.gwt.event.dom.client.FocusEvent;
  23. import com.google.gwt.event.dom.client.FocusHandler;
  24. import com.google.gwt.event.shared.EventHandler;
  25. import com.google.gwt.event.shared.HandlerRegistration;
  26. import com.google.gwt.user.client.ui.Widget;
  27. /**
  28. * Helper class for attaching/detaching handlers for Vaadin client side
  29. * components, based on identifiers in UIDL. Helpers expect Paintables to be
  30. * both listeners and sources for events. This helper cannot be used for more
  31. * complex widgets.
  32. * <p>
  33. * Possible current registration is given as parameter. The returned
  34. * registration (possibly the same as given, should be store for next update.
  35. * <p>
  36. * Pseudocode what helpers do:
  37. *
  38. * <pre>
  39. *
  40. * if paintable has event listener in UIDL
  41. * if registration is null
  42. * register paintable as as handler for event
  43. * return the registration
  44. * else
  45. * if registration is not null
  46. * remove the handler from paintable
  47. * return null
  48. *
  49. *
  50. * </pre>
  51. */
  52. public class EventHelper {
  53. /**
  54. * Adds or removes a focus handler depending on if the connector has focus
  55. * listeners on the server side or not.
  56. *
  57. * @param connector
  58. * The connector to update. Must implement focusHandler.
  59. * @param handlerRegistration
  60. * The old registration reference or null if no handler has been
  61. * registered previously
  62. * @return a new registration handler that can be used to unregister the
  63. * handler later
  64. */
  65. public static <T extends ComponentConnector & FocusHandler> HandlerRegistration updateFocusHandler(
  66. T connector, HandlerRegistration handlerRegistration) {
  67. return updateHandler(connector, connector, FOCUS, handlerRegistration,
  68. FocusEvent.getType(), connector.getWidget());
  69. }
  70. /**
  71. * Adds or removes a focus handler depending on if the connector has focus
  72. * listeners on the server side or not.
  73. *
  74. * @param connector
  75. * The connector to update. Must implement focusHandler.
  76. * @param handlerRegistration
  77. * The old registration reference or null if no handler has been
  78. * registered previously
  79. * @param widget
  80. * The widget which emits focus events
  81. * @return a new registration handler that can be used to unregister the
  82. * handler later
  83. */
  84. public static <T extends ComponentConnector & FocusHandler> HandlerRegistration updateFocusHandler(
  85. T connector, HandlerRegistration handlerRegistration,
  86. Widget widget) {
  87. return updateHandler(connector, connector, FOCUS, handlerRegistration,
  88. FocusEvent.getType(), widget);
  89. }
  90. /**
  91. * Adds or removes a blur handler depending on if the connector has blur
  92. * listeners on the server side or not.
  93. *
  94. * @param connector
  95. * The connector to update. Must implement BlurHandler.
  96. * @param handlerRegistration
  97. * The old registration reference or null if no handler has been
  98. * registered previously
  99. * @return a new registration handler that can be used to unregister the
  100. * handler later
  101. */
  102. public static <T extends ComponentConnector & BlurHandler> HandlerRegistration updateBlurHandler(
  103. T connector, HandlerRegistration handlerRegistration) {
  104. return updateHandler(connector, connector, BLUR, handlerRegistration,
  105. BlurEvent.getType(), connector.getWidget());
  106. }
  107. /**
  108. * Adds or removes a blur handler depending on if the connector has blur
  109. * listeners on the server side or not.
  110. *
  111. * @param connector
  112. * The connector to update. Must implement BlurHandler.
  113. * @param handlerRegistration
  114. * The old registration reference or null if no handler has been
  115. * registered previously
  116. * @param widget
  117. * The widget which emits blur events
  118. *
  119. * @return a new registration handler that can be used to unregister the
  120. * handler later
  121. */
  122. public static <T extends ComponentConnector & BlurHandler> HandlerRegistration updateBlurHandler(
  123. T connector, HandlerRegistration handlerRegistration,
  124. Widget widget) {
  125. return updateHandler(connector, connector, BLUR, handlerRegistration,
  126. BlurEvent.getType(), widget);
  127. }
  128. public static <H extends EventHandler> HandlerRegistration updateHandler(
  129. ComponentConnector connector, H handler, String eventIdentifier,
  130. HandlerRegistration handlerRegistration, Type<H> type,
  131. Widget widget) {
  132. if (connector.hasEventListener(eventIdentifier)) {
  133. if (handlerRegistration == null) {
  134. handlerRegistration = widget.addDomHandler(handler, type);
  135. }
  136. } else if (handlerRegistration != null) {
  137. handlerRegistration.removeHandler();
  138. handlerRegistration = null;
  139. }
  140. return handlerRegistration;
  141. }
  142. }