Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

EventHelper.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2011 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. /**
  27. * Helper class for attaching/detaching handlers for Vaadin client side
  28. * components, based on identifiers in UIDL. Helpers expect Paintables to be
  29. * both listeners and sources for events. This helper cannot be used for more
  30. * complex widgets.
  31. * <p>
  32. * Possible current registration is given as parameter. The returned
  33. * registration (possibly the same as given, should be store for next update.
  34. * <p>
  35. * Pseudocode what helpers do:
  36. *
  37. * <pre>
  38. *
  39. * if paintable has event listener in UIDL
  40. * if registration is null
  41. * register paintable as as handler for event
  42. * return the registration
  43. * else
  44. * if registration is not null
  45. * remove the handler from paintable
  46. * return null
  47. *
  48. *
  49. * </pre>
  50. *
  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 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, FOCUS, handlerRegistration,
  68. FocusEvent.getType());
  69. }
  70. /**
  71. * Adds or removes a blur handler depending on if the connector has blur
  72. * listeners on the server side or not.
  73. *
  74. * @param connector
  75. * The connector to update. Must implement BlurHandler.
  76. * @param handlerRegistration
  77. * The old registration reference or null no handler has been
  78. * registered previously
  79. * @return a new registration handler that can be used to unregister the
  80. * handler later
  81. */
  82. public static <T extends ComponentConnector & BlurHandler> HandlerRegistration updateBlurHandler(
  83. T connector, HandlerRegistration handlerRegistration) {
  84. return updateHandler(connector, BLUR, handlerRegistration,
  85. BlurEvent.getType());
  86. }
  87. private static <H extends EventHandler> HandlerRegistration updateHandler(
  88. ComponentConnector connector, String eventIdentifier,
  89. HandlerRegistration handlerRegistration, Type<H> type) {
  90. if (connector.hasEventListener(eventIdentifier)) {
  91. if (handlerRegistration == null) {
  92. handlerRegistration = connector.getWidget().addDomHandler(
  93. (H) connector, type);
  94. }
  95. } else if (handlerRegistration != null) {
  96. handlerRegistration.removeHandler();
  97. handlerRegistration = null;
  98. }
  99. return handlerRegistration;
  100. }
  101. }