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.

ClickEventHandler.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import com.google.gwt.dom.client.NativeEvent;
  8. import com.google.gwt.event.dom.client.ContextMenuEvent;
  9. import com.google.gwt.event.dom.client.ContextMenuHandler;
  10. import com.google.gwt.event.dom.client.DomEvent;
  11. import com.google.gwt.event.dom.client.DoubleClickEvent;
  12. import com.google.gwt.event.dom.client.DoubleClickHandler;
  13. import com.google.gwt.event.dom.client.MouseUpEvent;
  14. import com.google.gwt.event.dom.client.MouseUpHandler;
  15. import com.google.gwt.event.shared.EventHandler;
  16. import com.google.gwt.event.shared.HandlerRegistration;
  17. import com.google.gwt.user.client.Element;
  18. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  19. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  20. import com.vaadin.terminal.gwt.client.VPaintableMap;
  21. import com.vaadin.terminal.gwt.client.VPaintableWidget;
  22. public abstract class ClickEventHandler implements DoubleClickHandler,
  23. ContextMenuHandler, MouseUpHandler {
  24. private HandlerRegistration doubleClickHandlerRegistration;
  25. private HandlerRegistration mouseUpHandlerRegistration;
  26. private HandlerRegistration contextMenuHandlerRegistration;
  27. protected String clickEventIdentifier;
  28. protected VPaintableWidget paintable;
  29. private ApplicationConnection client;
  30. public ClickEventHandler(VPaintableWidget paintable,
  31. String clickEventIdentifier) {
  32. this.paintable = paintable;
  33. this.clickEventIdentifier = clickEventIdentifier;
  34. }
  35. public void handleEventHandlerRegistration(ApplicationConnection client) {
  36. this.client = client;
  37. // Handle registering/unregistering of click handler depending on if
  38. // server side listeners have been added or removed.
  39. if (hasEventListener()) {
  40. if (mouseUpHandlerRegistration == null) {
  41. mouseUpHandlerRegistration = registerHandler(this,
  42. MouseUpEvent.getType());
  43. contextMenuHandlerRegistration = registerHandler(this,
  44. ContextMenuEvent.getType());
  45. doubleClickHandlerRegistration = registerHandler(this,
  46. DoubleClickEvent.getType());
  47. }
  48. } else {
  49. if (mouseUpHandlerRegistration != null) {
  50. // Remove existing handlers
  51. doubleClickHandlerRegistration.removeHandler();
  52. mouseUpHandlerRegistration.removeHandler();
  53. contextMenuHandlerRegistration.removeHandler();
  54. contextMenuHandlerRegistration = null;
  55. mouseUpHandlerRegistration = null;
  56. doubleClickHandlerRegistration = null;
  57. }
  58. }
  59. }
  60. protected abstract <H extends EventHandler> HandlerRegistration registerHandler(
  61. final H handler, DomEvent.Type<H> type);
  62. protected ApplicationConnection getApplicationConnection() {
  63. return client;
  64. }
  65. public boolean hasEventListener() {
  66. return getApplicationConnection().hasEventListeners(paintable,
  67. clickEventIdentifier);
  68. }
  69. protected void fireClick(NativeEvent event) {
  70. ApplicationConnection client = getApplicationConnection();
  71. String pid = VPaintableMap.get(getApplicationConnection()).getPid(
  72. paintable);
  73. MouseEventDetails mouseDetails = new MouseEventDetails(event,
  74. getRelativeToElement());
  75. Map<String, Object> parameters = new HashMap<String, Object>();
  76. parameters.put("mouseDetails", mouseDetails.serialize());
  77. client.updateVariable(pid, clickEventIdentifier, parameters, true);
  78. }
  79. public void onContextMenu(ContextMenuEvent event) {
  80. if (hasEventListener()) {
  81. // Prevent showing the browser's context menu when there is a right
  82. // click listener.
  83. event.preventDefault();
  84. }
  85. }
  86. public void onMouseUp(MouseUpEvent event) {
  87. // TODO For perfect accuracy we should check that a mousedown has
  88. // occured on this element before this mouseup and that no mouseup
  89. // has occured anywhere after that.
  90. if (hasEventListener()) {
  91. // "Click" with left, right or middle button
  92. fireClick(event.getNativeEvent());
  93. }
  94. }
  95. public void onDoubleClick(DoubleClickEvent event) {
  96. if (hasEventListener()) {
  97. fireClick(event.getNativeEvent());
  98. }
  99. }
  100. /**
  101. * Click event calculates and returns coordinates relative to the element
  102. * returned by this method. Default implementation uses the root element of
  103. * the widget. Override to provide a different relative element.
  104. *
  105. * @return The Element used for calculating relative coordinates for a click
  106. * or null if no relative coordinates can be calculated.
  107. */
  108. protected Element getRelativeToElement() {
  109. return paintable.getWidgetForPaintable().getElement();
  110. }
  111. }