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.

AbstractClickEventHandler.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.terminal.gwt.client.ui;
  17. import com.google.gwt.core.client.JavaScriptObject;
  18. import com.google.gwt.dom.client.NativeEvent;
  19. import com.google.gwt.event.dom.client.ContextMenuEvent;
  20. import com.google.gwt.event.dom.client.ContextMenuHandler;
  21. import com.google.gwt.event.dom.client.DomEvent;
  22. import com.google.gwt.event.dom.client.DoubleClickEvent;
  23. import com.google.gwt.event.dom.client.DoubleClickHandler;
  24. import com.google.gwt.event.dom.client.MouseDownEvent;
  25. import com.google.gwt.event.dom.client.MouseDownHandler;
  26. import com.google.gwt.event.dom.client.MouseUpEvent;
  27. import com.google.gwt.event.dom.client.MouseUpHandler;
  28. import com.google.gwt.event.shared.EventHandler;
  29. import com.google.gwt.event.shared.HandlerRegistration;
  30. import com.google.gwt.user.client.Element;
  31. import com.google.gwt.user.client.Event;
  32. import com.google.gwt.user.client.Event.NativePreviewEvent;
  33. import com.google.gwt.user.client.Event.NativePreviewHandler;
  34. import com.vaadin.terminal.gwt.client.ComponentConnector;
  35. import com.vaadin.terminal.gwt.client.Util;
  36. public abstract class AbstractClickEventHandler implements MouseDownHandler,
  37. MouseUpHandler, DoubleClickHandler, ContextMenuHandler {
  38. private HandlerRegistration mouseDownHandlerRegistration;
  39. private HandlerRegistration mouseUpHandlerRegistration;
  40. private HandlerRegistration doubleClickHandlerRegistration;
  41. private HandlerRegistration contextMenuHandlerRegistration;
  42. protected ComponentConnector connector;
  43. private String clickEventIdentifier;
  44. /**
  45. * The element where the last mouse down event was registered.
  46. */
  47. private JavaScriptObject lastMouseDownTarget;
  48. /**
  49. * Set to true by {@link #mouseUpPreviewHandler} if it gets a mouseup at the
  50. * same element as {@link #lastMouseDownTarget}.
  51. */
  52. private boolean mouseUpPreviewMatched = false;
  53. private HandlerRegistration mouseUpEventPreviewRegistration;
  54. /**
  55. * Previews events after a mousedown to detect where the following mouseup
  56. * hits.
  57. */
  58. private final NativePreviewHandler mouseUpPreviewHandler = new NativePreviewHandler() {
  59. @Override
  60. public void onPreviewNativeEvent(NativePreviewEvent event) {
  61. if (event.getTypeInt() == Event.ONMOUSEUP) {
  62. mouseUpEventPreviewRegistration.removeHandler();
  63. // Event's reported target not always correct if event
  64. // capture is in use
  65. Element elementUnderMouse = Util.getElementUnderMouse(event
  66. .getNativeEvent());
  67. if (lastMouseDownTarget != null
  68. && elementUnderMouse.cast() == lastMouseDownTarget) {
  69. mouseUpPreviewMatched = true;
  70. } else {
  71. System.out.println("Ignoring mouseup from "
  72. + elementUnderMouse + " when mousedown was on "
  73. + lastMouseDownTarget);
  74. }
  75. }
  76. }
  77. };
  78. public AbstractClickEventHandler(ComponentConnector connector,
  79. String clickEventIdentifier) {
  80. this.connector = connector;
  81. this.clickEventIdentifier = clickEventIdentifier;
  82. }
  83. public void handleEventHandlerRegistration() {
  84. // Handle registering/unregistering of click handler depending on if
  85. // server side listeners have been added or removed.
  86. if (hasEventListener()) {
  87. if (mouseDownHandlerRegistration == null) {
  88. mouseDownHandlerRegistration = registerHandler(this,
  89. MouseDownEvent.getType());
  90. mouseUpHandlerRegistration = registerHandler(this,
  91. MouseUpEvent.getType());
  92. doubleClickHandlerRegistration = registerHandler(this,
  93. DoubleClickEvent.getType());
  94. contextMenuHandlerRegistration = registerHandler(this,
  95. ContextMenuEvent.getType());
  96. }
  97. } else {
  98. if (mouseDownHandlerRegistration != null) {
  99. // Remove existing handlers
  100. mouseDownHandlerRegistration.removeHandler();
  101. mouseUpHandlerRegistration.removeHandler();
  102. doubleClickHandlerRegistration.removeHandler();
  103. contextMenuHandlerRegistration.removeHandler();
  104. mouseDownHandlerRegistration = null;
  105. mouseUpHandlerRegistration = null;
  106. doubleClickHandlerRegistration = null;
  107. contextMenuHandlerRegistration = null;
  108. }
  109. }
  110. }
  111. /**
  112. * Registers the given handler to the widget so that the necessary events
  113. * are passed to this {@link ClickEventHandler}.
  114. * <p>
  115. * By default registers the handler with the connector root widget.
  116. * </p>
  117. *
  118. * @param <H>
  119. * @param handler
  120. * The handler to register
  121. * @param type
  122. * The type of the handler.
  123. * @return A reference for the registration of the handler.
  124. */
  125. protected <H extends EventHandler> HandlerRegistration registerHandler(
  126. final H handler, DomEvent.Type<H> type) {
  127. return connector.getWidget().addDomHandler(handler, type);
  128. }
  129. /**
  130. * Checks if there is a server side event listener registered for clicks
  131. *
  132. * @return true if there is a server side event listener registered, false
  133. * otherwise
  134. */
  135. public boolean hasEventListener() {
  136. return connector.hasEventListener(clickEventIdentifier);
  137. }
  138. /**
  139. * Event handler for context menu. Prevents the browser context menu from
  140. * popping up if there is a listener for right clicks.
  141. */
  142. @Override
  143. public void onContextMenu(ContextMenuEvent event) {
  144. if (hasEventListener() && shouldFireEvent(event)) {
  145. // Prevent showing the browser's context menu when there is a right
  146. // click listener.
  147. event.preventDefault();
  148. }
  149. }
  150. @Override
  151. public void onMouseDown(MouseDownEvent event) {
  152. /*
  153. * When getting a mousedown event, we must detect where the
  154. * corresponding mouseup event if it's on a different part of the page.
  155. */
  156. lastMouseDownTarget = event.getNativeEvent().getEventTarget();
  157. mouseUpPreviewMatched = false;
  158. mouseUpEventPreviewRegistration = Event
  159. .addNativePreviewHandler(mouseUpPreviewHandler);
  160. }
  161. @Override
  162. public void onMouseUp(MouseUpEvent event) {
  163. /*
  164. * Only fire a click if the mouseup hits the same element as the
  165. * corresponding mousedown. This is first checked in the event preview
  166. * but we can't fire the even there as the event might get canceled
  167. * before it gets here.
  168. */
  169. if (hasEventListener()
  170. && mouseUpPreviewMatched
  171. && lastMouseDownTarget != null
  172. && Util.getElementUnderMouse(event.getNativeEvent()) == lastMouseDownTarget
  173. && shouldFireEvent(event)) {
  174. // "Click" with left, right or middle button
  175. fireClick(event.getNativeEvent());
  176. }
  177. mouseUpPreviewMatched = false;
  178. lastMouseDownTarget = null;
  179. }
  180. /**
  181. * Sends the click event based on the given native event.
  182. *
  183. * @param event
  184. * The native event that caused this click event
  185. */
  186. protected abstract void fireClick(NativeEvent event);
  187. /**
  188. * Called before firing a click event. Allows sub classes to decide if this
  189. * in an event that should cause an event or not.
  190. *
  191. * @param event
  192. * The user event
  193. * @return true if the event should be fired, false otherwise
  194. */
  195. protected boolean shouldFireEvent(DomEvent<?> event) {
  196. return true;
  197. }
  198. /**
  199. * Event handler for double clicks. Used to fire double click events. Note
  200. * that browsers typically fail to prevent the second click event so a
  201. * double click will result in two click events and one double click event.
  202. */
  203. @Override
  204. public void onDoubleClick(DoubleClickEvent event) {
  205. if (hasEventListener() && shouldFireEvent(event)) {
  206. fireClick(event.getNativeEvent());
  207. }
  208. }
  209. /**
  210. * Click event calculates and returns coordinates relative to the element
  211. * returned by this method. Default implementation uses the root element of
  212. * the widget. Override to provide a different relative element.
  213. *
  214. * @return The Element used for calculating relative coordinates for a click
  215. * or null if no relative coordinates can be calculated.
  216. */
  217. protected Element getRelativeToElement() {
  218. return connector.getWidget().getElement();
  219. }
  220. }