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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client;
  5. import static com.vaadin.terminal.gwt.client.EventId.BLUR;
  6. import static com.vaadin.terminal.gwt.client.EventId.FOCUS;
  7. import com.google.gwt.event.dom.client.BlurHandler;
  8. import com.google.gwt.event.dom.client.FocusHandler;
  9. import com.google.gwt.event.dom.client.HasBlurHandlers;
  10. import com.google.gwt.event.dom.client.HasFocusHandlers;
  11. import com.google.gwt.event.shared.HandlerRegistration;
  12. /**
  13. * Helper class for attaching/detaching handlers for Vaadin client side
  14. * components, based on identifiers in UIDL. Helpers expect Paintables to be
  15. * both listeners and sources for events. This helper cannot be used for more
  16. * complex widgets.
  17. * <p>
  18. * Possible current registration is given as parameter. The returned
  19. * registration (possibly the same as given, should be store for next update.
  20. * <p>
  21. * Pseudocode what helpers do:
  22. *
  23. * <pre>
  24. *
  25. * if paintable has event listener in UIDL
  26. * if registration is null
  27. * register paintable as as handler for event
  28. * return the registration
  29. * else
  30. * if registration is not null
  31. * remove the handler from paintable
  32. * return null
  33. *
  34. *
  35. * </pre>
  36. *
  37. */
  38. public class EventHelper {
  39. public static HandlerRegistration updateFocusHandler(VPaintableWidget paintable,
  40. ApplicationConnection client,
  41. HandlerRegistration handlerRegistration) {
  42. if (client.hasEventListeners(paintable, FOCUS)) {
  43. if (handlerRegistration == null) {
  44. handlerRegistration = ((HasFocusHandlers) paintable)
  45. .addFocusHandler((FocusHandler) paintable);
  46. }
  47. return handlerRegistration;
  48. } else if (handlerRegistration != null) {
  49. handlerRegistration.removeHandler();
  50. handlerRegistration = null;
  51. }
  52. return null;
  53. }
  54. public static HandlerRegistration updateBlurHandler(VPaintableWidget paintable,
  55. ApplicationConnection client,
  56. HandlerRegistration handlerRegistration) {
  57. if (client.hasEventListeners(paintable, BLUR)) {
  58. if (handlerRegistration == null) {
  59. handlerRegistration = ((HasBlurHandlers) paintable)
  60. .addBlurHandler((BlurHandler) paintable);
  61. }
  62. return handlerRegistration;
  63. } else if (handlerRegistration != null) {
  64. handlerRegistration.removeHandler();
  65. handlerRegistration = null;
  66. }
  67. return null;
  68. }
  69. }