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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright 2000-2014 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.shared;
  17. import java.io.Serializable;
  18. /**
  19. * Helper class to store and transfer mouse event details.
  20. */
  21. public class MouseEventDetails implements Serializable {
  22. /**
  23. * @deprecated use {@link MouseButton#LEFT} instead.
  24. */
  25. @Deprecated
  26. public static final MouseButton BUTTON_LEFT = MouseButton.LEFT;
  27. /**
  28. * @deprecated use {@link MouseButton#MIDDLE} instead.
  29. */
  30. @Deprecated
  31. public static final MouseButton BUTTON_MIDDLE = MouseButton.MIDDLE;
  32. /**
  33. * @deprecated use {@link MouseButton#RIGHT} instead.
  34. */
  35. @Deprecated
  36. public static final MouseButton BUTTON_RIGHT = MouseButton.RIGHT;
  37. /**
  38. * Constants for mouse buttons.
  39. *
  40. * @author Vaadin Ltd
  41. * @version @VERSION@
  42. * @since 7.0
  43. *
  44. */
  45. public enum MouseButton {
  46. LEFT("left"), RIGHT("right"), MIDDLE("middle");
  47. private String name;
  48. private MouseButton(String name) {
  49. this.name = name;
  50. }
  51. /**
  52. * Returns a human readable text representing the button
  53. *
  54. * @return
  55. */
  56. public String getName() {
  57. return name;
  58. }
  59. }
  60. private static final char DELIM = ',';
  61. // From com.google.gwt.user.client.Event
  62. private static final int ONDBLCLICK = 0x00002;
  63. private MouseButton button;
  64. private int clientX;
  65. private int clientY;
  66. private boolean altKey;
  67. private boolean ctrlKey;
  68. private boolean metaKey;
  69. private boolean shiftKey;
  70. private int type;
  71. private int relativeX = -1;
  72. private int relativeY = -1;
  73. public MouseButton getButton() {
  74. return button;
  75. }
  76. public int getClientX() {
  77. return clientX;
  78. }
  79. public int getClientY() {
  80. return clientY;
  81. }
  82. public boolean isAltKey() {
  83. return altKey;
  84. }
  85. public boolean isCtrlKey() {
  86. return ctrlKey;
  87. }
  88. public boolean isMetaKey() {
  89. return metaKey;
  90. }
  91. public boolean isShiftKey() {
  92. return shiftKey;
  93. }
  94. public int getRelativeX() {
  95. return relativeX;
  96. }
  97. public int getRelativeY() {
  98. return relativeY;
  99. }
  100. public void setButton(MouseButton button) {
  101. this.button = button;
  102. }
  103. public void setClientX(int clientX) {
  104. this.clientX = clientX;
  105. }
  106. public void setClientY(int clientY) {
  107. this.clientY = clientY;
  108. }
  109. public void setAltKey(boolean altKey) {
  110. this.altKey = altKey;
  111. }
  112. public void setCtrlKey(boolean ctrlKey) {
  113. this.ctrlKey = ctrlKey;
  114. }
  115. public void setMetaKey(boolean metaKey) {
  116. this.metaKey = metaKey;
  117. }
  118. public void setShiftKey(boolean shiftKey) {
  119. this.shiftKey = shiftKey;
  120. }
  121. public void setType(int type) {
  122. this.type = type;
  123. }
  124. public void setRelativeX(int relativeX) {
  125. this.relativeX = relativeX;
  126. }
  127. public void setRelativeY(int relativeY) {
  128. this.relativeY = relativeY;
  129. }
  130. public MouseEventDetails() {
  131. }
  132. @Override
  133. public String toString() {
  134. return serialize();
  135. }
  136. public String serialize() {
  137. return button.toString() + DELIM + clientX + DELIM + clientY + DELIM
  138. + altKey + DELIM + ctrlKey + DELIM + metaKey + DELIM + shiftKey
  139. + DELIM + type + DELIM + relativeX + DELIM + relativeY;
  140. }
  141. public static MouseEventDetails deSerialize(String serializedString) {
  142. MouseEventDetails instance = new MouseEventDetails();
  143. String[] fields = serializedString.split(",");
  144. instance.button = MouseButton.valueOf(fields[0]);
  145. instance.clientX = Integer.parseInt(fields[1]);
  146. instance.clientY = Integer.parseInt(fields[2]);
  147. instance.altKey = Boolean.valueOf(fields[3]).booleanValue();
  148. instance.ctrlKey = Boolean.valueOf(fields[4]).booleanValue();
  149. instance.metaKey = Boolean.valueOf(fields[5]).booleanValue();
  150. instance.shiftKey = Boolean.valueOf(fields[6]).booleanValue();
  151. instance.type = Integer.parseInt(fields[7]);
  152. instance.relativeX = Integer.parseInt(fields[8]);
  153. instance.relativeY = Integer.parseInt(fields[9]);
  154. return instance;
  155. }
  156. public String getButtonName() {
  157. return button == null ? "" : button.getName();
  158. }
  159. public int getType() {
  160. return type;
  161. }
  162. public boolean isDoubleClick() {
  163. return type == ONDBLCLICK;
  164. }
  165. }