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

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