1. /*
  2. * @(#)MouseWheelEvent.java 1.7 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.event;
  8. import java.awt.Component;
  9. import sun.awt.DebugHelper;
  10. /**
  11. * An event which indicates that the mouse wheel was rotated in a component.
  12. * <P>
  13. * A wheel mouse is a mouse which has a wheel in place of the middle button.
  14. * This wheel can be rotated towards or away from the user. Mouse wheels are
  15. * most often used for scrolling, though other uses are possible.
  16. * <P>
  17. * A MouseWheelEvent object is passed to every <code>MouseWheelListener</code>
  18. * object which registered to receive the "interesting" mouse events using the
  19. * component's <code>addMouseWheelListener</code> method. Each such listener
  20. * object gets a <code>MouseEvent</code> containing the mouse event.
  21. * <P>
  22. * Due to the mouse wheel's special relationship to scrolling Components,
  23. * MouseWheelEvents are delivered somewhat differently than other MouseEvents.
  24. * This is because while other MouseEvents usually affect a change on
  25. * the Component directly under the mouse
  26. * cursor (for instance, when clicking a button), MouseWheelEvents often have
  27. * an effect away from the mouse cursor (moving the wheel while
  28. * over a Component inside a ScrollPane should scroll one of the
  29. * Scrollbars on the ScrollPane).
  30. * <P>
  31. * MouseWheelEvents start delivery from the Component underneath the
  32. * mouse cursor. If MouseWheelEvents are not enabled on the
  33. * Component, the event is delivered to the first ancestor
  34. * Container with MouseWheelEvents enabled. This will usually be
  35. * a ScrollPane with wheel scrolling enabled. The source
  36. * Component and x,y coordinates will be relative to the event's
  37. * final destination (the ScrollPane). This allows a complex
  38. * GUI to be installed without modification into a ScrollPane, and
  39. * for all MouseWheelEvents to be delivered to the ScrollPane for
  40. * scrolling.
  41. * <P>
  42. * Some AWT Components are implemented using native widgets which
  43. * display their own scrollbars and handle their own scrolling.
  44. * The particular Components for which this is true will vary from
  45. * platform to platform. When the mouse wheel is
  46. * moved over one of these Components, the event is delivered straight to
  47. * the native widget, and not propagated to ancestors.
  48. * <P>
  49. * Platforms offer customization of the amount of scrolling that
  50. * should take place when the mouse wheel is moved. The two most
  51. * common settings are to scroll a certain number of "units"
  52. * (commonly lines of text in a text-based component) or an entire "block"
  53. * (similar to page-up/page-down). The MouseWheelEvent offers
  54. * methods for conforming to the underlying platform settings. These
  55. * platform settings can be changed at any time by the user. MouseWheelEvents
  56. * reflect the most recent settings.
  57. *
  58. * @author Brent Christian
  59. * @version 1.7 01/23/03
  60. * @see MouseWheelListener
  61. * @see java.awt.ScrollPane
  62. * @see java.awt.ScrollPane#setWheelScrollingEnabled(boolean)
  63. * @see javax.swing.JScrollPane
  64. * @see javax.swing.JScrollPane#setWheelScrollingEnabled(boolean)
  65. * @since 1.4
  66. */
  67. public class MouseWheelEvent extends MouseEvent {
  68. private static final DebugHelper dbg = DebugHelper.create(MouseWheelEvent.class);
  69. /**
  70. * Constant representing scrolling by "units" (like scrolling with the
  71. * arrow keys)
  72. *
  73. * @see #getScrollType
  74. */
  75. public static final int WHEEL_UNIT_SCROLL = 0;
  76. /**
  77. * Constant representing scrolling by a "block" (like scrolling
  78. * with page-up, page-down keys)
  79. *
  80. * @see #getScrollType
  81. */
  82. public static final int WHEEL_BLOCK_SCROLL = 1;
  83. /**
  84. * Indicates what sort of scrolling should take place in response to this
  85. * event, based on platform settings. Legal values are:
  86. * <ul>
  87. * <li> WHEEL_UNIT_SCROLL
  88. * <li> WHEEL_BLOCK_SCROLL
  89. * </ul>
  90. *
  91. * @see #getScrollType
  92. */
  93. int scrollType;
  94. /**
  95. * Only valid for scrollType WHEEL_UNIT_SCROLL.
  96. * Indicates number of units that should be scrolled per
  97. * click of mouse wheel rotation, based on platform settings.
  98. *
  99. * @see #getScrollAmount
  100. * @see #getScrollType
  101. */
  102. int scrollAmount;
  103. /**
  104. * Indicates how far the mouse wheel was rotated.
  105. *
  106. * @see #getWheelRotation
  107. */
  108. int wheelRotation;
  109. // serial version id?
  110. /**
  111. * Constructs a <code>MouseWheelEvent</code> object with the
  112. * specified source component, type, modifiers, coordinates,
  113. * scroll type, scroll amount, and wheel rotation.
  114. * <p>Note that passing in an invalid <code>id</code> results in
  115. * unspecified behavior.
  116. *
  117. * @param source the <code>Component</code> that originated
  118. * the event
  119. * @param id the integer that identifies the event
  120. * @param when a long that gives the time the event occurred
  121. * @param modifiers the modifier keys down during event
  122. * (shift, ctrl, alt, meta)
  123. * @param x the horizontal x coordinate for the mouse location
  124. * @param y the vertical y coordinate for the mouse location
  125. * @param clickCount the number of mouse clicks associated with event
  126. * @param popupTrigger a boolean, true if this event is a trigger for a
  127. * popup-menu
  128. * @param scrollType the type of scrolling which should take place in
  129. * response to this event; valid values are
  130. * <code>WHEEL_UNIT_SCROLL</code> and
  131. * <code>WHEEL_BLOCK_SCROLL</code>
  132. * @param scrollAmount for scrollType <code>WHEEL_UNIT_SCROLL</code>,
  133. * the number of units to be scrolled
  134. * @param wheelRotation the amount that the mouse wheel was rotated (the
  135. * number of "clicks")
  136. *
  137. * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, boolean)
  138. */
  139. public MouseWheelEvent (Component source, int id, long when, int modifiers,
  140. int x, int y, int clickCount, boolean popupTrigger,
  141. int scrollType, int scrollAmount, int wheelRotation) {
  142. super(source, id, when, modifiers, x, y, clickCount, popupTrigger);
  143. this.scrollType = scrollType;
  144. this.scrollAmount = scrollAmount;
  145. this.wheelRotation = wheelRotation;
  146. if (dbg.on) {
  147. dbg.println("MouseWheelEvent constructor");
  148. // Thread.dumpStack();
  149. }
  150. }
  151. /**
  152. * Returns the type of scrolling that should take place in response to this
  153. * event. This is determined by the native platform. Legal values are:
  154. * <ul>
  155. * <li> MouseWheelEvent.WHEEL_UNIT_SCROLL
  156. * <li> MouseWheelEvent.WHEEL_BLOCK_SCROLL
  157. * </ul>
  158. *
  159. * @return either MouseWheelEvent.WHEEL_UNIT_SCROLL or
  160. * MouseWheelEvent.WHEEL_BLOCK_SCROLL, depending on the configuration of
  161. * the native platform.
  162. * @see java.awt.Adjustable#getUnitIncrement
  163. * @see java.awt.Adjustable#getBlockIncrement
  164. * @see javax.swing.Scrollable#getScrollableUnitIncrement
  165. * @see javax.swing.Scrollable#getScrollableBlockIncrement
  166. */
  167. public int getScrollType() {
  168. return scrollType;
  169. }
  170. /**
  171. * Returns the number of units that should be scrolled in response to this
  172. * event. Only valid if <code>getScrollType</code> returns
  173. * <code>MouseWheelEvent.WHEEL_UNIT_SCROLL</code>
  174. *
  175. * @return number of units to scroll, or an undefined value if
  176. * <code>getScrollType</code> returns
  177. * <code>MouseWheelEvent.WHEEL_BLOCK_SCROLL</code>
  178. * @see #getScrollType
  179. */
  180. public int getScrollAmount() {
  181. return scrollAmount;
  182. }
  183. /**
  184. * Returns the number of "clicks" the mouse wheel was rotated.
  185. *
  186. * @return negative values if the mouse wheel was rotated up/away from
  187. * the user, and positive values if the mouse wheel was rotated down/
  188. * towards the user
  189. */
  190. public int getWheelRotation() {
  191. return wheelRotation;
  192. }
  193. /**
  194. * This is a convenience method to aid in the implementation of
  195. * the common-case MouseWheelListener - to scroll a ScrollPane or
  196. * JScrollPane by an amount which conforms to the platform settings.
  197. * (Note, however, that <code>ScrollPane</code> and
  198. * <code>JScrollPane</code> already have this functionality built in.)
  199. * <P>
  200. * This method returns the number of units to scroll when scroll type is
  201. * MouseWheelEvent.WHEEL_UNIT_SCROLL, and should only be called if
  202. * <code>getScrollType</code> returns MouseWheelEvent.WHEEL_UNIT_SCROLL.
  203. * <P>
  204. * Direction of scroll, amount of wheel movement,
  205. * and platform settings for wheel scrolling are all accounted for.
  206. * This method does not and cannot take into account value of the
  207. * Adjustable/Scrollable unit increment, as this will vary among
  208. * scrolling components.
  209. * <P>
  210. * A simplified example of how this method might be used in a
  211. * listener:
  212. * <pre>
  213. * mouseWheelMoved(MouseWheelEvent event) {
  214. * ScrollPane sp = getScrollPaneFromSomewhere();
  215. * Adjustable adj = sp.getVAdjustable()
  216. * if (MouseWheelEvent.getScrollType() == WHEEL_UNIT_SCROLL) {
  217. * int totalScrollAmount =
  218. * event.getUnitsToScroll() *
  219. * adj.getUnitIncrement();
  220. * adj.setValue(adj.getValue() + totalScrollAmount);
  221. * }
  222. * }
  223. * </pre>
  224. *
  225. * @return the number of units to scroll based on the direction and amount
  226. * of mouse wheel rotation, and on the wheel scrolling settings of the
  227. * native platform
  228. * @see #getScrollType
  229. * @see #getScrollAmount
  230. * @see MouseWheelListener
  231. * @see java.awt.Adjustable
  232. * @see java.awt.Adjustable#getUnitIncrement
  233. * @see javax.swing.Scrollable
  234. * @see javax.swing.Scrollable#getScrollableUnitIncrement
  235. * @see java.awt.ScrollPane
  236. * @see java.awt.ScrollPane#setWheelScrollingEnabled
  237. * @see javax.swing.JScrollPane
  238. * @see javax.swing.JScrollPane#setWheelScrollingEnabled
  239. */
  240. public int getUnitsToScroll() {
  241. return scrollAmount * wheelRotation;
  242. }
  243. /**
  244. * Returns a parameter string identifying this event.
  245. * This method is useful for event-logging and for debugging.
  246. *
  247. * @return a string identifying the event and its attributes
  248. */
  249. public String paramString() {
  250. String scrollTypeStr = null;
  251. if (getScrollType() == WHEEL_UNIT_SCROLL) {
  252. scrollTypeStr = "WHEEL_UNIT_SCROLL";
  253. }
  254. else if (getScrollType() == WHEEL_BLOCK_SCROLL) {
  255. scrollTypeStr = "WHEEL_BLOCK_SCROLL";
  256. }
  257. else {
  258. scrollTypeStr = "unknown scroll type";
  259. }
  260. return super.paramString()+",scrollType="+scrollTypeStr+
  261. ",scrollAmount="+getScrollAmount()+",wheelRotation="+
  262. getWheelRotation();
  263. }
  264. }