Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

EventHelper.java 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client;
  5. import static com.vaadin.shared.EventId.BLUR;
  6. import static com.vaadin.shared.EventId.FOCUS;
  7. import com.google.gwt.event.dom.client.BlurEvent;
  8. import com.google.gwt.event.dom.client.BlurHandler;
  9. import com.google.gwt.event.dom.client.DomEvent.Type;
  10. import com.google.gwt.event.dom.client.FocusEvent;
  11. import com.google.gwt.event.dom.client.FocusHandler;
  12. import com.google.gwt.event.shared.EventHandler;
  13. import com.google.gwt.event.shared.HandlerRegistration;
  14. /**
  15. * Helper class for attaching/detaching handlers for Vaadin client side
  16. * components, based on identifiers in UIDL. Helpers expect Paintables to be
  17. * both listeners and sources for events. This helper cannot be used for more
  18. * complex widgets.
  19. * <p>
  20. * Possible current registration is given as parameter. The returned
  21. * registration (possibly the same as given, should be store for next update.
  22. * <p>
  23. * Pseudocode what helpers do:
  24. *
  25. * <pre>
  26. *
  27. * if paintable has event listener in UIDL
  28. * if registration is null
  29. * register paintable as as handler for event
  30. * return the registration
  31. * else
  32. * if registration is not null
  33. * remove the handler from paintable
  34. * return null
  35. *
  36. *
  37. * </pre>
  38. *
  39. */
  40. public class EventHelper {
  41. /**
  42. * Adds or removes a focus handler depending on if the connector has focus
  43. * listeners on the server side or not.
  44. *
  45. * @param connector
  46. * The connector to update. Must implement focusHandler.
  47. * @param handlerRegistration
  48. * The old registration reference or null no handler has been
  49. * registered previously
  50. * @return a new registration handler that can be used to unregister the
  51. * handler later
  52. */
  53. public static <T extends ComponentConnector & FocusHandler> HandlerRegistration updateFocusHandler(
  54. T connector, HandlerRegistration handlerRegistration) {
  55. return updateHandler(connector, FOCUS, handlerRegistration,
  56. FocusEvent.getType());
  57. }
  58. /**
  59. * Adds or removes a blur handler depending on if the connector has blur
  60. * listeners on the server side or not.
  61. *
  62. * @param connector
  63. * The connector to update. Must implement BlurHandler.
  64. * @param handlerRegistration
  65. * The old registration reference or null no handler has been
  66. * registered previously
  67. * @return a new registration handler that can be used to unregister the
  68. * handler later
  69. */
  70. public static <T extends ComponentConnector & BlurHandler> HandlerRegistration updateBlurHandler(
  71. T connector, HandlerRegistration handlerRegistration) {
  72. return updateHandler(connector, BLUR, handlerRegistration,
  73. BlurEvent.getType());
  74. }
  75. private static <H extends EventHandler> HandlerRegistration updateHandler(
  76. ComponentConnector connector, String eventIdentifier,
  77. HandlerRegistration handlerRegistration, Type<H> type) {
  78. if (connector.hasEventListener(eventIdentifier)) {
  79. if (handlerRegistration == null) {
  80. handlerRegistration = connector.getWidget().addDomHandler(
  81. (H) connector, type);
  82. }
  83. } else if (handlerRegistration != null) {
  84. handlerRegistration.removeHandler();
  85. handlerRegistration = null;
  86. }
  87. return handlerRegistration;
  88. }
  89. }