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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.vaadin.terminal.gwt.client.ApplicationConnection;
  18. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  19. import com.vaadin.terminal.gwt.client.Paintable;
  20. public abstract class ClickEventHandler implements DoubleClickHandler,
  21. ContextMenuHandler, MouseUpHandler {
  22. private HandlerRegistration doubleClickHandlerRegistration;
  23. private HandlerRegistration mouseUpHandlerRegistration;
  24. private HandlerRegistration contextMenuHandlerRegistration;
  25. protected String clickEventIdentifier;
  26. protected Paintable paintable;
  27. private ApplicationConnection client;
  28. public ClickEventHandler(Paintable paintable, String clickEventIdentifier) {
  29. this.paintable = paintable;
  30. this.clickEventIdentifier = clickEventIdentifier;
  31. }
  32. public void handleEventHandlerRegistration(ApplicationConnection client) {
  33. this.client = client;
  34. // Handle registering/unregistering of click handler depending on if
  35. // server side listeners have been added or removed.
  36. if (hasEventListener()) {
  37. if (mouseUpHandlerRegistration == null) {
  38. mouseUpHandlerRegistration = registerHandler(this, MouseUpEvent
  39. .getType());
  40. contextMenuHandlerRegistration = registerHandler(this,
  41. ContextMenuEvent.getType());
  42. doubleClickHandlerRegistration = registerHandler(this,
  43. DoubleClickEvent.getType());
  44. }
  45. } else {
  46. if (mouseUpHandlerRegistration != null) {
  47. // Remove existing handlers
  48. doubleClickHandlerRegistration.removeHandler();
  49. mouseUpHandlerRegistration.removeHandler();
  50. contextMenuHandlerRegistration.removeHandler();
  51. contextMenuHandlerRegistration = null;
  52. mouseUpHandlerRegistration = null;
  53. doubleClickHandlerRegistration = null;
  54. }
  55. }
  56. }
  57. protected abstract <H extends EventHandler> HandlerRegistration registerHandler(
  58. final H handler, DomEvent.Type<H> type);
  59. protected ApplicationConnection getApplicationConnection() {
  60. return client;
  61. }
  62. public boolean hasEventListener() {
  63. return getApplicationConnection().hasEventListeners(paintable,
  64. clickEventIdentifier);
  65. }
  66. protected void fireClick(NativeEvent event) {
  67. ApplicationConnection client = getApplicationConnection();
  68. String pid = getApplicationConnection().getPid(paintable);
  69. MouseEventDetails mouseDetails = new MouseEventDetails(event);
  70. Map<String, Object> parameters = new HashMap<String, Object>();
  71. parameters.put("mouseDetails", mouseDetails.serialize());
  72. client.updateVariable(pid, clickEventIdentifier, parameters, true);
  73. }
  74. public void onContextMenu(ContextMenuEvent event) {
  75. if (hasEventListener()) {
  76. // Prevent showing the browser's context menu when there is a right
  77. // click listener.
  78. event.preventDefault();
  79. }
  80. }
  81. public void onMouseUp(MouseUpEvent event) {
  82. // TODO For perfect accuracy we should check that a mousedown has
  83. // occured on this element before this mouseup and that no mouseup
  84. // has occured anywhere after that.
  85. if (hasEventListener()) {
  86. // "Click" with left, right or middle button
  87. fireClick(event.getNativeEvent());
  88. }
  89. }
  90. public void onDoubleClick(DoubleClickEvent event) {
  91. if (hasEventListener()) {
  92. fireClick(event.getNativeEvent());
  93. }
  94. }
  95. }