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

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