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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.ComponentConnector;
  20. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  21. import com.vaadin.terminal.gwt.client.MouseEventDetailsBuilder;
  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 ComponentConnector connector;
  29. public ClickEventHandler(ComponentConnector connector,
  30. String clickEventIdentifier) {
  31. this.connector = connector;
  32. this.clickEventIdentifier = clickEventIdentifier;
  33. }
  34. public void handleEventHandlerRegistration() {
  35. // Handle registering/unregistering of click handler depending on if
  36. // server side listeners have been added or removed.
  37. if (hasEventListener()) {
  38. if (mouseUpHandlerRegistration == null) {
  39. mouseUpHandlerRegistration = registerHandler(this,
  40. MouseUpEvent.getType());
  41. contextMenuHandlerRegistration = registerHandler(this,
  42. ContextMenuEvent.getType());
  43. doubleClickHandlerRegistration = registerHandler(this,
  44. DoubleClickEvent.getType());
  45. }
  46. } else {
  47. if (mouseUpHandlerRegistration != null) {
  48. // Remove existing handlers
  49. doubleClickHandlerRegistration.removeHandler();
  50. mouseUpHandlerRegistration.removeHandler();
  51. contextMenuHandlerRegistration.removeHandler();
  52. contextMenuHandlerRegistration = null;
  53. mouseUpHandlerRegistration = null;
  54. doubleClickHandlerRegistration = null;
  55. }
  56. }
  57. }
  58. protected abstract <H extends EventHandler> HandlerRegistration registerHandler(
  59. final H handler, DomEvent.Type<H> type);
  60. protected ApplicationConnection getApplicationConnection() {
  61. return connector.getConnection();
  62. }
  63. public boolean hasEventListener() {
  64. return connector.hasEventListener(clickEventIdentifier);
  65. }
  66. protected void fireClick(NativeEvent event) {
  67. String pid = connector.getConnectorId();
  68. MouseEventDetails mouseDetails = MouseEventDetailsBuilder
  69. .buildMouseEventDetails(event, getRelativeToElement());
  70. Map<String, Object> parameters = new HashMap<String, Object>();
  71. parameters.put("mouseDetails", mouseDetails.serialize());
  72. connector.getConnection().updateVariable(pid, clickEventIdentifier,
  73. parameters, true);
  74. }
  75. public void onContextMenu(ContextMenuEvent event) {
  76. if (hasEventListener()) {
  77. // Prevent showing the browser's context menu when there is a right
  78. // click listener.
  79. event.preventDefault();
  80. }
  81. }
  82. public void onMouseUp(MouseUpEvent event) {
  83. // TODO For perfect accuracy we should check that a mousedown has
  84. // occured on this element before this mouseup and that no mouseup
  85. // has occured anywhere after that.
  86. if (hasEventListener()) {
  87. // "Click" with left, right or middle button
  88. fireClick(event.getNativeEvent());
  89. }
  90. }
  91. public void onDoubleClick(DoubleClickEvent event) {
  92. if (hasEventListener()) {
  93. fireClick(event.getNativeEvent());
  94. }
  95. }
  96. /**
  97. * Click event calculates and returns coordinates relative to the element
  98. * returned by this method. Default implementation uses the root element of
  99. * the widget. Override to provide a different relative element.
  100. *
  101. * @return The Element used for calculating relative coordinates for a click
  102. * or null if no relative coordinates can be calculated.
  103. */
  104. protected Element getRelativeToElement() {
  105. return connector.getWidget().getElement();
  106. }
  107. }