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.

MouseEvents.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2011 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.event;
  17. import java.lang.reflect.Method;
  18. import com.vaadin.shared.MouseEventDetails;
  19. import com.vaadin.shared.MouseEventDetails.MouseButton;
  20. import com.vaadin.ui.Component;
  21. import com.vaadin.util.ReflectTools;
  22. /**
  23. * Interface that serves as a wrapper for mouse related events.
  24. *
  25. * @author Vaadin Ltd.
  26. * @see ClickListener
  27. * @since 6.2
  28. */
  29. public interface MouseEvents {
  30. /**
  31. * Class for holding information about a mouse click event. A
  32. * {@link ClickEvent} is fired when the user clicks on a
  33. * <code>Component</code>.
  34. *
  35. * The information available for click events are terminal dependent.
  36. * Correct values for all event details cannot be guaranteed.
  37. *
  38. * @author Vaadin Ltd.
  39. * @see ClickListener
  40. * @since 6.2
  41. */
  42. public static class ClickEvent extends Component.Event {
  43. /**
  44. * @deprecated As of 7.0, use {@link Button#LEFT} instead.
  45. */
  46. @Deprecated
  47. public static final MouseButton BUTTON_LEFT = MouseButton.LEFT;
  48. /**
  49. * @deprecated As of 7.0, use {@link Button#MIDDLE} instead.
  50. */
  51. @Deprecated
  52. public static final MouseButton BUTTON_MIDDLE = MouseButton.MIDDLE;
  53. /**
  54. * @deprecated As of 7.0, use {@link Button#RIGHT} instead.
  55. */
  56. @Deprecated
  57. public static final MouseButton BUTTON_RIGHT = MouseButton.RIGHT;
  58. private MouseEventDetails details;
  59. public ClickEvent(Component source, MouseEventDetails mouseEventDetails) {
  60. super(source);
  61. details = mouseEventDetails;
  62. }
  63. /**
  64. * Returns an identifier describing which mouse button the user pushed.
  65. * Compare with {@link MouseButton#LEFT},{@link MouseButton#MIDDLE},
  66. * {@link Button#RIGHT} to find out which button it is.
  67. *
  68. * @return one of {@link MouseButton#LEFT}, {@link MouseButton#MIDDLE},
  69. * {@link MouseButton#RIGHT}.
  70. */
  71. public MouseButton getButton() {
  72. return details.getButton();
  73. }
  74. /**
  75. * Returns the mouse position (x coordinate) when the click took place.
  76. * The position is relative to the browser client area.
  77. *
  78. * @return The mouse cursor x position
  79. */
  80. public int getClientX() {
  81. return details.getClientX();
  82. }
  83. /**
  84. * Returns the mouse position (y coordinate) when the click took place.
  85. * The position is relative to the browser client area.
  86. *
  87. * @return The mouse cursor y position
  88. */
  89. public int getClientY() {
  90. return details.getClientY();
  91. }
  92. /**
  93. * Returns the relative mouse position (x coordinate) when the click
  94. * took place. The position is relative to the clicked component.
  95. *
  96. * @return The mouse cursor x position relative to the clicked layout
  97. * component or -1 if no x coordinate available
  98. */
  99. public int getRelativeX() {
  100. return details.getRelativeX();
  101. }
  102. /**
  103. * Returns the relative mouse position (y coordinate) when the click
  104. * took place. The position is relative to the clicked component.
  105. *
  106. * @return The mouse cursor y position relative to the clicked layout
  107. * component or -1 if no y coordinate available
  108. */
  109. public int getRelativeY() {
  110. return details.getRelativeY();
  111. }
  112. /**
  113. * Checks if the event is a double click event.
  114. *
  115. * @return true if the event is a double click event, false otherwise
  116. */
  117. public boolean isDoubleClick() {
  118. return details.isDoubleClick();
  119. }
  120. /**
  121. * Checks if the Alt key was down when the mouse event took place.
  122. *
  123. * @return true if Alt was down when the event occured, false otherwise
  124. */
  125. public boolean isAltKey() {
  126. return details.isAltKey();
  127. }
  128. /**
  129. * Checks if the Ctrl key was down when the mouse event took place.
  130. *
  131. * @return true if Ctrl was pressed when the event occured, false
  132. * otherwise
  133. */
  134. public boolean isCtrlKey() {
  135. return details.isCtrlKey();
  136. }
  137. /**
  138. * Checks if the Meta key was down when the mouse event took place.
  139. *
  140. * @return true if Meta was pressed when the event occured, false
  141. * otherwise
  142. */
  143. public boolean isMetaKey() {
  144. return details.isMetaKey();
  145. }
  146. /**
  147. * Checks if the Shift key was down when the mouse event took place.
  148. *
  149. * @return true if Shift was pressed when the event occured, false
  150. * otherwise
  151. */
  152. public boolean isShiftKey() {
  153. return details.isShiftKey();
  154. }
  155. /**
  156. * Returns a human readable string representing which button has been
  157. * pushed. This is meant for debug purposes only and the string returned
  158. * could change. Use {@link #getButton()} to check which button was
  159. * pressed.
  160. *
  161. * @since 6.3
  162. * @return A string representation of which button was pushed.
  163. */
  164. public String getButtonName() {
  165. return details.getButtonName();
  166. }
  167. }
  168. /**
  169. * Interface for listening for a {@link ClickEvent} fired by a
  170. * {@link Component}.
  171. *
  172. * @see ClickEvent
  173. * @author Vaadin Ltd.
  174. * @since 6.2
  175. */
  176. public interface ClickListener extends ComponentEventListener {
  177. public static final Method clickMethod = ReflectTools.findMethod(
  178. ClickListener.class, "click", ClickEvent.class);
  179. /**
  180. * Called when a {@link Component} has been clicked. A reference to the
  181. * component is given by {@link ClickEvent#getComponent()}.
  182. *
  183. * @param event
  184. * An event containing information about the click.
  185. */
  186. public void click(ClickEvent event);
  187. }
  188. /**
  189. * Class for holding additional event information for DoubleClick events.
  190. * Fired when the user double-clicks on a <code>Component</code>.
  191. *
  192. * @see ClickEvent
  193. * @author Vaadin Ltd.
  194. * @since 6.2
  195. */
  196. public static class DoubleClickEvent extends Component.Event {
  197. public DoubleClickEvent(Component source) {
  198. super(source);
  199. }
  200. }
  201. /**
  202. * Interface for listening for a {@link DoubleClickEvent} fired by a
  203. * {@link Component}.
  204. *
  205. * @see DoubleClickEvent
  206. * @author Vaadin Ltd.
  207. * @since 6.2
  208. */
  209. public interface DoubleClickListener extends ComponentEventListener {
  210. public static final Method doubleClickMethod = ReflectTools.findMethod(
  211. DoubleClickListener.class, "doubleClick",
  212. DoubleClickEvent.class);
  213. /**
  214. * Called when a {@link Component} has been double clicked. A reference
  215. * to the component is given by {@link DoubleClickEvent#getComponent()}.
  216. *
  217. * @param event
  218. * An event containing information about the double click.
  219. */
  220. public void doubleClick(DoubleClickEvent event);
  221. }
  222. }