blob: 40af56c44396759633e2c23769e4326e1e329d88 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package java.awt.event;
27
28import java.awt.Component;
29
30/**
31 * An event which indicates that the mouse wheel was rotated in a component.
32 * <P>
33 * A wheel mouse is a mouse which has a wheel in place of the middle button.
34 * This wheel can be rotated towards or away from the user. Mouse wheels are
35 * most often used for scrolling, though other uses are possible.
36 * <P>
37 * A MouseWheelEvent object is passed to every <code>MouseWheelListener</code>
38 * object which registered to receive the "interesting" mouse events using the
39 * component's <code>addMouseWheelListener</code> method. Each such listener
40 * object gets a <code>MouseEvent</code> containing the mouse event.
41 * <P>
42 * Due to the mouse wheel's special relationship to scrolling Components,
43 * MouseWheelEvents are delivered somewhat differently than other MouseEvents.
44 * This is because while other MouseEvents usually affect a change on
45 * the Component directly under the mouse
46 * cursor (for instance, when clicking a button), MouseWheelEvents often have
47 * an effect away from the mouse cursor (moving the wheel while
48 * over a Component inside a ScrollPane should scroll one of the
49 * Scrollbars on the ScrollPane).
50 * <P>
51 * MouseWheelEvents start delivery from the Component underneath the
52 * mouse cursor. If MouseWheelEvents are not enabled on the
53 * Component, the event is delivered to the first ancestor
54 * Container with MouseWheelEvents enabled. This will usually be
55 * a ScrollPane with wheel scrolling enabled. The source
56 * Component and x,y coordinates will be relative to the event's
57 * final destination (the ScrollPane). This allows a complex
58 * GUI to be installed without modification into a ScrollPane, and
59 * for all MouseWheelEvents to be delivered to the ScrollPane for
60 * scrolling.
61 * <P>
62 * Some AWT Components are implemented using native widgets which
63 * display their own scrollbars and handle their own scrolling.
64 * The particular Components for which this is true will vary from
65 * platform to platform. When the mouse wheel is
66 * moved over one of these Components, the event is delivered straight to
67 * the native widget, and not propagated to ancestors.
68 * <P>
69 * Platforms offer customization of the amount of scrolling that
70 * should take place when the mouse wheel is moved. The two most
71 * common settings are to scroll a certain number of "units"
72 * (commonly lines of text in a text-based component) or an entire "block"
73 * (similar to page-up/page-down). The MouseWheelEvent offers
74 * methods for conforming to the underlying platform settings. These
75 * platform settings can be changed at any time by the user. MouseWheelEvents
76 * reflect the most recent settings.
77 *
78 * @author Brent Christian
79 * @see MouseWheelListener
80 * @see java.awt.ScrollPane
81 * @see java.awt.ScrollPane#setWheelScrollingEnabled(boolean)
82 * @see javax.swing.JScrollPane
83 * @see javax.swing.JScrollPane#setWheelScrollingEnabled(boolean)
84 * @since 1.4
85 */
86
87public class MouseWheelEvent extends MouseEvent {
88
89 /**
90 * Constant representing scrolling by "units" (like scrolling with the
91 * arrow keys)
92 *
93 * @see #getScrollType
94 */
95 public static final int WHEEL_UNIT_SCROLL = 0;
96
97 /**
98 * Constant representing scrolling by a "block" (like scrolling
99 * with page-up, page-down keys)
100 *
101 * @see #getScrollType
102 */
103 public static final int WHEEL_BLOCK_SCROLL = 1;
104
105 /**
106 * Indicates what sort of scrolling should take place in response to this
107 * event, based on platform settings. Legal values are:
108 * <ul>
109 * <li> WHEEL_UNIT_SCROLL
110 * <li> WHEEL_BLOCK_SCROLL
111 * </ul>
112 *
113 * @see #getScrollType
114 */
115 int scrollType;
116
117 /**
118 * Only valid for scrollType WHEEL_UNIT_SCROLL.
119 * Indicates number of units that should be scrolled per
120 * click of mouse wheel rotation, based on platform settings.
121 *
122 * @see #getScrollAmount
123 * @see #getScrollType
124 */
125 int scrollAmount;
126
127 /**
128 * Indicates how far the mouse wheel was rotated.
129 *
130 * @see #getWheelRotation
131 */
132 int wheelRotation;
133
134 /*
135 * serialVersionUID
136 */
137
138 private static final long serialVersionUID = 6459879390515399677L;
139
140 /**
141 * Constructs a <code>MouseWheelEvent</code> object with the
142 * specified source component, type, modifiers, coordinates,
143 * scroll type, scroll amount, and wheel rotation.
144 * <p>Absolute coordinates xAbs and yAbs are set to source's location on screen plus
145 * relative coordinates x and y. xAbs and yAbs are set to zero if the source is not showing.
146 * <p>Note that passing in an invalid <code>id</code> results in
147 * unspecified behavior. This method throws an
148 * <code>IllegalArgumentException</code> if <code>source</code>
149 * is <code>null</code>.
150 *
151 * @param source the <code>Component</code> that originated
152 * the event
153 * @param id the integer that identifies the event
154 * @param when a long that gives the time the event occurred
155 * @param modifiers the modifier keys down during event
156 * (shift, ctrl, alt, meta)
157 * @param x the horizontal x coordinate for the mouse location
158 * @param y the vertical y coordinate for the mouse location
159 * @param clickCount the number of mouse clicks associated with event
160 * @param popupTrigger a boolean, true if this event is a trigger for a
161 * popup-menu
162 * @param scrollType the type of scrolling which should take place in
163 * response to this event; valid values are
164 * <code>WHEEL_UNIT_SCROLL</code> and
165 * <code>WHEEL_BLOCK_SCROLL</code>
166 * @param scrollAmount for scrollType <code>WHEEL_UNIT_SCROLL</code>,
167 * the number of units to be scrolled
168 * @param wheelRotation the amount that the mouse wheel was rotated (the
169 * number of "clicks")
170 *
171 * @throws IllegalArgumentException if <code>source</code> is null
172 * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, boolean)
173 * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, int, int, boolean, int)
174 */
175 public MouseWheelEvent (Component source, int id, long when, int modifiers,
176 int x, int y, int clickCount, boolean popupTrigger,
177 int scrollType, int scrollAmount, int wheelRotation) {
178
179 this(source, id, when, modifiers, x, y, 0, 0, clickCount,
180 popupTrigger, scrollType, scrollAmount, wheelRotation);
181 }
182
183 /**
184 * Constructs a <code>MouseWheelEvent</code> object with the
185 * specified source component, type, modifiers, coordinates,
186 * absolute coordinates, scroll type, scroll amount, and wheel rotation.
187 * <p>Note that passing in an invalid <code>id</code> results in
188 * unspecified behavior. This method throws an
189 * <code>IllegalArgumentException</code> if <code>source</code>
190 * is <code>null</code>.<p>
191 * Even if inconsistent values for relative and absolute coordinates are
192 * passed to the constructor, the MouseWheelEvent instance is still
193 * created and no exception is thrown.
194 *
195 * @param source the <code>Component</code> that originated
196 * the event
197 * @param id the integer that identifies the event
198 * @param when a long that gives the time the event occurred
199 * @param modifiers the modifier keys down during event
200 * (shift, ctrl, alt, meta)
201 * @param x the horizontal x coordinate for the mouse location
202 * @param y the vertical y coordinate for the mouse location
203 * @param xAbs the absolute horizontal x coordinate for the mouse location
204 * @param yAbs the absolute vertical y coordinate for the mouse location
205 * @param clickCount the number of mouse clicks associated with event
206 * @param popupTrigger a boolean, true if this event is a trigger for a
207 * popup-menu
208 * @param scrollType the type of scrolling which should take place in
209 * response to this event; valid values are
210 * <code>WHEEL_UNIT_SCROLL</code> and
211 * <code>WHEEL_BLOCK_SCROLL</code>
212 * @param scrollAmount for scrollType <code>WHEEL_UNIT_SCROLL</code>,
213 * the number of units to be scrolled
214 * @param wheelRotation the amount that the mouse wheel was rotated (the
215 * number of "clicks")
216 *
217 * @throws IllegalArgumentException if <code>source</code> is null
218 * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, boolean)
219 * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, int, int, boolean, int)
220 * @since 1.6
221 */
222 public MouseWheelEvent (Component source, int id, long when, int modifiers,
223 int x, int y, int xAbs, int yAbs, int clickCount, boolean popupTrigger,
224 int scrollType, int scrollAmount, int wheelRotation) {
225
226 super(source, id, when, modifiers, x, y, xAbs, yAbs, clickCount,
227 popupTrigger, MouseEvent.NOBUTTON);
228
229 this.scrollType = scrollType;
230 this.scrollAmount = scrollAmount;
231 this.wheelRotation = wheelRotation;
232 }
233
234 /**
235 * Returns the type of scrolling that should take place in response to this
236 * event. This is determined by the native platform. Legal values are:
237 * <ul>
238 * <li> MouseWheelEvent.WHEEL_UNIT_SCROLL
239 * <li> MouseWheelEvent.WHEEL_BLOCK_SCROLL
240 * </ul>
241 *
242 * @return either MouseWheelEvent.WHEEL_UNIT_SCROLL or
243 * MouseWheelEvent.WHEEL_BLOCK_SCROLL, depending on the configuration of
244 * the native platform.
245 * @see java.awt.Adjustable#getUnitIncrement
246 * @see java.awt.Adjustable#getBlockIncrement
247 * @see javax.swing.Scrollable#getScrollableUnitIncrement
248 * @see javax.swing.Scrollable#getScrollableBlockIncrement
249 */
250 public int getScrollType() {
251 return scrollType;
252 }
253
254 /**
255 * Returns the number of units that should be scrolled per
256 * click of mouse wheel rotation.
257 * Only valid if <code>getScrollType</code> returns
258 * <code>MouseWheelEvent.WHEEL_UNIT_SCROLL</code>
259 *
260 * @return number of units to scroll, or an undefined value if
261 * <code>getScrollType</code> returns
262 * <code>MouseWheelEvent.WHEEL_BLOCK_SCROLL</code>
263 * @see #getScrollType
264 */
265 public int getScrollAmount() {
266 return scrollAmount;
267 }
268
269 /**
270 * Returns the number of "clicks" the mouse wheel was rotated.
271 *
272 * @return negative values if the mouse wheel was rotated up/away from
273 * the user, and positive values if the mouse wheel was rotated down/
274 * towards the user
275 */
276 public int getWheelRotation() {
277 return wheelRotation;
278 }
279
280 /**
281 * This is a convenience method to aid in the implementation of
282 * the common-case MouseWheelListener - to scroll a ScrollPane or
283 * JScrollPane by an amount which conforms to the platform settings.
284 * (Note, however, that <code>ScrollPane</code> and
285 * <code>JScrollPane</code> already have this functionality built in.)
286 * <P>
287 * This method returns the number of units to scroll when scroll type is
288 * MouseWheelEvent.WHEEL_UNIT_SCROLL, and should only be called if
289 * <code>getScrollType</code> returns MouseWheelEvent.WHEEL_UNIT_SCROLL.
290 * <P>
291 * Direction of scroll, amount of wheel movement,
292 * and platform settings for wheel scrolling are all accounted for.
293 * This method does not and cannot take into account value of the
294 * Adjustable/Scrollable unit increment, as this will vary among
295 * scrolling components.
296 * <P>
297 * A simplified example of how this method might be used in a
298 * listener:
299 * <pre>
300 * mouseWheelMoved(MouseWheelEvent event) {
301 * ScrollPane sp = getScrollPaneFromSomewhere();
302 * Adjustable adj = sp.getVAdjustable()
303 * if (MouseWheelEvent.getScrollType() == WHEEL_UNIT_SCROLL) {
304 * int totalScrollAmount =
305 * event.getUnitsToScroll() *
306 * adj.getUnitIncrement();
307 * adj.setValue(adj.getValue() + totalScrollAmount);
308 * }
309 * }
310 * </pre>
311 *
312 * @return the number of units to scroll based on the direction and amount
313 * of mouse wheel rotation, and on the wheel scrolling settings of the
314 * native platform
315 * @see #getScrollType
316 * @see #getScrollAmount
317 * @see MouseWheelListener
318 * @see java.awt.Adjustable
319 * @see java.awt.Adjustable#getUnitIncrement
320 * @see javax.swing.Scrollable
321 * @see javax.swing.Scrollable#getScrollableUnitIncrement
322 * @see java.awt.ScrollPane
323 * @see java.awt.ScrollPane#setWheelScrollingEnabled
324 * @see javax.swing.JScrollPane
325 * @see javax.swing.JScrollPane#setWheelScrollingEnabled
326 */
327 public int getUnitsToScroll() {
328 return scrollAmount * wheelRotation;
329 }
330
331 /**
332 * Returns a parameter string identifying this event.
333 * This method is useful for event-logging and for debugging.
334 *
335 * @return a string identifying the event and its attributes
336 */
337 public String paramString() {
338 String scrollTypeStr = null;
339
340 if (getScrollType() == WHEEL_UNIT_SCROLL) {
341 scrollTypeStr = "WHEEL_UNIT_SCROLL";
342 }
343 else if (getScrollType() == WHEEL_BLOCK_SCROLL) {
344 scrollTypeStr = "WHEEL_BLOCK_SCROLL";
345 }
346 else {
347 scrollTypeStr = "unknown scroll type";
348 }
349 return super.paramString()+",scrollType="+scrollTypeStr+
350 ",scrollAmount="+getScrollAmount()+",wheelRotation="+
351 getWheelRotation();
352 }
353}