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

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