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.

MouseEventDetails.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client;
  5. import java.io.Serializable;
  6. import com.google.gwt.dom.client.Element;
  7. import com.google.gwt.dom.client.NativeEvent;
  8. import com.google.gwt.user.client.Event;
  9. /**
  10. * Helper class to store and transfer mouse event details.
  11. */
  12. public class MouseEventDetails implements Serializable {
  13. public static final int BUTTON_LEFT = Event.BUTTON_LEFT;
  14. public static final int BUTTON_MIDDLE = Event.BUTTON_MIDDLE;
  15. public static final int BUTTON_RIGHT = Event.BUTTON_RIGHT;
  16. private static final char DELIM = ',';
  17. private int button;
  18. private int clientX;
  19. private int clientY;
  20. private boolean altKey;
  21. private boolean ctrlKey;
  22. private boolean metaKey;
  23. private boolean shiftKey;
  24. private int type;
  25. private int relativeX = -1;
  26. private int relativeY = -1;
  27. public int getButton() {
  28. return button;
  29. }
  30. public int getClientX() {
  31. return clientX;
  32. }
  33. public int getClientY() {
  34. return clientY;
  35. }
  36. public boolean isAltKey() {
  37. return altKey;
  38. }
  39. public boolean isCtrlKey() {
  40. return ctrlKey;
  41. }
  42. public boolean isMetaKey() {
  43. return metaKey;
  44. }
  45. public boolean isShiftKey() {
  46. return shiftKey;
  47. }
  48. public int getRelativeX() {
  49. return relativeX;
  50. }
  51. public int getRelativeY() {
  52. return relativeY;
  53. }
  54. public void setButton(int button) {
  55. this.button = button;
  56. }
  57. public void setClientX(int clientX) {
  58. this.clientX = clientX;
  59. }
  60. public void setClientY(int clientY) {
  61. this.clientY = clientY;
  62. }
  63. public void setAltKey(boolean altKey) {
  64. this.altKey = altKey;
  65. }
  66. public void setCtrlKey(boolean ctrlKey) {
  67. this.ctrlKey = ctrlKey;
  68. }
  69. public void setMetaKey(boolean metaKey) {
  70. this.metaKey = metaKey;
  71. }
  72. public void setShiftKey(boolean shiftKey) {
  73. this.shiftKey = shiftKey;
  74. }
  75. public void setType(int type) {
  76. this.type = type;
  77. }
  78. public void setRelativeX(int relativeX) {
  79. this.relativeX = relativeX;
  80. }
  81. public void setRelativeY(int relativeY) {
  82. this.relativeY = relativeY;
  83. }
  84. public MouseEventDetails() {
  85. }
  86. public MouseEventDetails(NativeEvent evt) {
  87. this(evt, null);
  88. }
  89. public MouseEventDetails(NativeEvent evt, Element relativeToElement) {
  90. type = Event.getTypeInt(evt.getType());
  91. clientX = Util.getTouchOrMouseClientX(evt);
  92. clientY = Util.getTouchOrMouseClientY(evt);
  93. button = evt.getButton();
  94. altKey = evt.getAltKey();
  95. ctrlKey = evt.getCtrlKey();
  96. metaKey = evt.getMetaKey();
  97. shiftKey = evt.getShiftKey();
  98. if (relativeToElement != null) {
  99. relativeX = getRelativeX(clientX, relativeToElement);
  100. relativeY = getRelativeY(clientY, relativeToElement);
  101. }
  102. }
  103. @Override
  104. public String toString() {
  105. return serialize();
  106. }
  107. public String serialize() {
  108. return "" + button + DELIM + clientX + DELIM + clientY + DELIM + altKey
  109. + DELIM + ctrlKey + DELIM + metaKey + DELIM + shiftKey + DELIM
  110. + type + DELIM + relativeX + DELIM + relativeY;
  111. }
  112. public static MouseEventDetails deSerialize(String serializedString) {
  113. MouseEventDetails instance = new MouseEventDetails();
  114. String[] fields = serializedString.split(",");
  115. instance.button = Integer.parseInt(fields[0]);
  116. instance.clientX = Integer.parseInt(fields[1]);
  117. instance.clientY = Integer.parseInt(fields[2]);
  118. instance.altKey = Boolean.valueOf(fields[3]).booleanValue();
  119. instance.ctrlKey = Boolean.valueOf(fields[4]).booleanValue();
  120. instance.metaKey = Boolean.valueOf(fields[5]).booleanValue();
  121. instance.shiftKey = Boolean.valueOf(fields[6]).booleanValue();
  122. instance.type = Integer.parseInt(fields[7]);
  123. instance.relativeX = Integer.parseInt(fields[8]);
  124. instance.relativeY = Integer.parseInt(fields[9]);
  125. return instance;
  126. }
  127. public String getButtonName() {
  128. if (button == BUTTON_LEFT) {
  129. return "left";
  130. } else if (button == BUTTON_RIGHT) {
  131. return "right";
  132. } else if (button == BUTTON_MIDDLE) {
  133. return "middle";
  134. }
  135. return "";
  136. }
  137. public int getType() {
  138. return type;
  139. }
  140. public boolean isDoubleClick() {
  141. return type == Event.ONDBLCLICK;
  142. }
  143. private static int getRelativeX(int clientX, Element target) {
  144. return clientX - target.getAbsoluteLeft() + target.getScrollLeft()
  145. + target.getOwnerDocument().getScrollLeft();
  146. }
  147. private static int getRelativeY(int clientY, Element target) {
  148. return clientY - target.getAbsoluteTop() + target.getScrollTop()
  149. + target.getOwnerDocument().getScrollTop();
  150. }
  151. }