001 /*
002 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt.event;
027
028 import java.awt.Component;
029 import sun.awt.DebugHelper;
030
031 /**
032 * An event which indicates that the mouse wheel was rotated in a component.
033 * <P>
034 * A wheel mouse is a mouse which has a wheel in place of the middle button.
035 * This wheel can be rotated towards or away from the user. Mouse wheels are
036 * most often used for scrolling, though other uses are possible.
037 * <P>
038 * A MouseWheelEvent object is passed to every <code>MouseWheelListener</code>
039 * object which registered to receive the "interesting" mouse events using the
040 * component's <code>addMouseWheelListener</code> method. Each such listener
041 * object gets a <code>MouseEvent</code> containing the mouse event.
042 * <P>
043 * Due to the mouse wheel's special relationship to scrolling Components,
044 * MouseWheelEvents are delivered somewhat differently than other MouseEvents.
045 * This is because while other MouseEvents usually affect a change on
046 * the Component directly under the mouse
047 * cursor (for instance, when clicking a button), MouseWheelEvents often have
048 * an effect away from the mouse cursor (moving the wheel while
049 * over a Component inside a ScrollPane should scroll one of the
050 * Scrollbars on the ScrollPane).
051 * <P>
052 * MouseWheelEvents start delivery from the Component underneath the
053 * mouse cursor. If MouseWheelEvents are not enabled on the
054 * Component, the event is delivered to the first ancestor
055 * Container with MouseWheelEvents enabled. This will usually be
056 * a ScrollPane with wheel scrolling enabled. The source
057 * Component and x,y coordinates will be relative to the event's
058 * final destination (the ScrollPane). This allows a complex
059 * GUI to be installed without modification into a ScrollPane, and
060 * for all MouseWheelEvents to be delivered to the ScrollPane for
061 * scrolling.
062 * <P>
063 * Some AWT Components are implemented using native widgets which
064 * display their own scrollbars and handle their own scrolling.
065 * The particular Components for which this is true will vary from
066 * platform to platform. When the mouse wheel is
067 * moved over one of these Components, the event is delivered straight to
068 * the native widget, and not propagated to ancestors.
069 * <P>
070 * Platforms offer customization of the amount of scrolling that
071 * should take place when the mouse wheel is moved. The two most
072 * common settings are to scroll a certain number of "units"
073 * (commonly lines of text in a text-based component) or an entire "block"
074 * (similar to page-up/page-down). The MouseWheelEvent offers
075 * methods for conforming to the underlying platform settings. These
076 * platform settings can be changed at any time by the user. MouseWheelEvents
077 * reflect the most recent settings.
078 *
079 * @author Brent Christian
080 * @version 1.20 05/05/07
081 * @see MouseWheelListener
082 * @see java.awt.ScrollPane
083 * @see java.awt.ScrollPane#setWheelScrollingEnabled(boolean)
084 * @see javax.swing.JScrollPane
085 * @see javax.swing.JScrollPane#setWheelScrollingEnabled(boolean)
086 * @since 1.4
087 */
088
089 public class MouseWheelEvent extends MouseEvent {
090
091 private static final DebugHelper dbg = DebugHelper
092 .create(MouseWheelEvent.class);
093
094 /**
095 * Constant representing scrolling by "units" (like scrolling with the
096 * arrow keys)
097 *
098 * @see #getScrollType
099 */
100 public static final int WHEEL_UNIT_SCROLL = 0;
101
102 /**
103 * Constant representing scrolling by a "block" (like scrolling
104 * with page-up, page-down keys)
105 *
106 * @see #getScrollType
107 */
108 public static final int WHEEL_BLOCK_SCROLL = 1;
109
110 /**
111 * Indicates what sort of scrolling should take place in response to this
112 * event, based on platform settings. Legal values are:
113 * <ul>
114 * <li> WHEEL_UNIT_SCROLL
115 * <li> WHEEL_BLOCK_SCROLL
116 * </ul>
117 *
118 * @see #getScrollType
119 */
120 int scrollType;
121
122 /**
123 * Only valid for scrollType WHEEL_UNIT_SCROLL.
124 * Indicates number of units that should be scrolled per
125 * click of mouse wheel rotation, based on platform settings.
126 *
127 * @see #getScrollAmount
128 * @see #getScrollType
129 */
130 int scrollAmount;
131
132 /**
133 * Indicates how far the mouse wheel was rotated.
134 *
135 * @see #getWheelRotation
136 */
137 int wheelRotation;
138
139 /*
140 * serialVersionUID
141 */
142
143 private static final long serialVersionUID = 6459879390515399677L;
144
145 /**
146 * Constructs a <code>MouseWheelEvent</code> object with the
147 * specified source component, type, modifiers, coordinates,
148 * scroll type, scroll amount, and wheel rotation.
149 * <p>Absolute coordinates xAbs and yAbs are set to source's location on screen plus
150 * relative coordinates x and y. xAbs and yAbs are set to zero if the source is not showing.
151 * <p>Note that passing in an invalid <code>id</code> results in
152 * unspecified behavior. This method throws an
153 * <code>IllegalArgumentException</code> if <code>source</code>
154 * is <code>null</code>.
155 *
156 * @param source the <code>Component</code> that originated
157 * the event
158 * @param id the integer that identifies the event
159 * @param when a long that gives the time the event occurred
160 * @param modifiers the modifier keys down during event
161 * (shift, ctrl, alt, meta)
162 * @param x the horizontal x coordinate for the mouse location
163 * @param y the vertical y coordinate for the mouse location
164 * @param clickCount the number of mouse clicks associated with event
165 * @param popupTrigger a boolean, true if this event is a trigger for a
166 * popup-menu
167 * @param scrollType the type of scrolling which should take place in
168 * response to this event; valid values are
169 * <code>WHEEL_UNIT_SCROLL</code> and
170 * <code>WHEEL_BLOCK_SCROLL</code>
171 * @param scrollAmount for scrollType <code>WHEEL_UNIT_SCROLL</code>,
172 * the number of units to be scrolled
173 * @param wheelRotation the amount that the mouse wheel was rotated (the
174 * number of "clicks")
175 *
176 * @throws IllegalArgumentException if <code>source</code> is null
177 * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, boolean)
178 * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, int, int, boolean, int)
179 */
180 public MouseWheelEvent(Component source, int id, long when,
181 int modifiers, int x, int y, int clickCount,
182 boolean popupTrigger, int scrollType, int scrollAmount,
183 int wheelRotation) {
184
185 this (source, id, when, modifiers, x, y, 0, 0, clickCount,
186 popupTrigger, scrollType, scrollAmount, wheelRotation);
187 }
188
189 /**
190 * Constructs a <code>MouseWheelEvent</code> object with the
191 * specified source component, type, modifiers, coordinates,
192 * absolute coordinates, scroll type, scroll amount, and wheel rotation.
193 * <p>Note that passing in an invalid <code>id</code> results in
194 * unspecified behavior. This method throws an
195 * <code>IllegalArgumentException</code> if <code>source</code>
196 * is <code>null</code>.<p>
197 * Even if inconsistent values for relative and absolute coordinates are
198 * passed to the constructor, the MouseWheelEvent instance is still
199 * created and no exception is thrown.
200 *
201 * @param source the <code>Component</code> that originated
202 * the event
203 * @param id the integer that identifies the event
204 * @param when a long that gives the time the event occurred
205 * @param modifiers the modifier keys down during event
206 * (shift, ctrl, alt, meta)
207 * @param x the horizontal x coordinate for the mouse location
208 * @param y the vertical y coordinate for the mouse location
209 * @param xAbs the absolute horizontal x coordinate for the mouse location
210 * @param yAbs the absolute vertical y coordinate for the mouse location
211 * @param clickCount the number of mouse clicks associated with event
212 * @param popupTrigger a boolean, true if this event is a trigger for a
213 * popup-menu
214 * @param scrollType the type of scrolling which should take place in
215 * response to this event; valid values are
216 * <code>WHEEL_UNIT_SCROLL</code> and
217 * <code>WHEEL_BLOCK_SCROLL</code>
218 * @param scrollAmount for scrollType <code>WHEEL_UNIT_SCROLL</code>,
219 * the number of units to be scrolled
220 * @param wheelRotation the amount that the mouse wheel was rotated (the
221 * number of "clicks")
222 *
223 * @throws IllegalArgumentException if <code>source</code> is null
224 * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, boolean)
225 * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, int, int, boolean, int)
226 * @since 1.6
227 */
228 public MouseWheelEvent(Component source, int id, long when,
229 int modifiers, int x, int y, int xAbs, int yAbs,
230 int clickCount, boolean popupTrigger, int scrollType,
231 int scrollAmount, int wheelRotation) {
232
233 super (source, id, when, modifiers, x, y, xAbs, yAbs,
234 clickCount, popupTrigger, MouseEvent.NOBUTTON);
235
236 this .scrollType = scrollType;
237 this .scrollAmount = scrollAmount;
238 this .wheelRotation = wheelRotation;
239
240 if (dbg.on) {
241 dbg.println("MouseWheelEvent constructor");
242 }
243 }
244
245 /**
246 * Returns the type of scrolling that should take place in response to this
247 * event. This is determined by the native platform. Legal values are:
248 * <ul>
249 * <li> MouseWheelEvent.WHEEL_UNIT_SCROLL
250 * <li> MouseWheelEvent.WHEEL_BLOCK_SCROLL
251 * </ul>
252 *
253 * @return either MouseWheelEvent.WHEEL_UNIT_SCROLL or
254 * MouseWheelEvent.WHEEL_BLOCK_SCROLL, depending on the configuration of
255 * the native platform.
256 * @see java.awt.Adjustable#getUnitIncrement
257 * @see java.awt.Adjustable#getBlockIncrement
258 * @see javax.swing.Scrollable#getScrollableUnitIncrement
259 * @see javax.swing.Scrollable#getScrollableBlockIncrement
260 */
261 public int getScrollType() {
262 return scrollType;
263 }
264
265 /**
266 * Returns the number of units that should be scrolled per
267 * click of mouse wheel rotation.
268 * Only valid if <code>getScrollType</code> returns
269 * <code>MouseWheelEvent.WHEEL_UNIT_SCROLL</code>
270 *
271 * @return number of units to scroll, or an undefined value if
272 * <code>getScrollType</code> returns
273 * <code>MouseWheelEvent.WHEEL_BLOCK_SCROLL</code>
274 * @see #getScrollType
275 */
276 public int getScrollAmount() {
277 return scrollAmount;
278 }
279
280 /**
281 * Returns the number of "clicks" the mouse wheel was rotated.
282 *
283 * @return negative values if the mouse wheel was rotated up/away from
284 * the user, and positive values if the mouse wheel was rotated down/
285 * towards the user
286 */
287 public int getWheelRotation() {
288 return wheelRotation;
289 }
290
291 /**
292 * This is a convenience method to aid in the implementation of
293 * the common-case MouseWheelListener - to scroll a ScrollPane or
294 * JScrollPane by an amount which conforms to the platform settings.
295 * (Note, however, that <code>ScrollPane</code> and
296 * <code>JScrollPane</code> already have this functionality built in.)
297 * <P>
298 * This method returns the number of units to scroll when scroll type is
299 * MouseWheelEvent.WHEEL_UNIT_SCROLL, and should only be called if
300 * <code>getScrollType</code> returns MouseWheelEvent.WHEEL_UNIT_SCROLL.
301 * <P>
302 * Direction of scroll, amount of wheel movement,
303 * and platform settings for wheel scrolling are all accounted for.
304 * This method does not and cannot take into account value of the
305 * Adjustable/Scrollable unit increment, as this will vary among
306 * scrolling components.
307 * <P>
308 * A simplified example of how this method might be used in a
309 * listener:
310 * <pre>
311 * mouseWheelMoved(MouseWheelEvent event) {
312 * ScrollPane sp = getScrollPaneFromSomewhere();
313 * Adjustable adj = sp.getVAdjustable()
314 * if (MouseWheelEvent.getScrollType() == WHEEL_UNIT_SCROLL) {
315 * int totalScrollAmount =
316 * event.getUnitsToScroll() *
317 * adj.getUnitIncrement();
318 * adj.setValue(adj.getValue() + totalScrollAmount);
319 * }
320 * }
321 * </pre>
322 *
323 * @return the number of units to scroll based on the direction and amount
324 * of mouse wheel rotation, and on the wheel scrolling settings of the
325 * native platform
326 * @see #getScrollType
327 * @see #getScrollAmount
328 * @see MouseWheelListener
329 * @see java.awt.Adjustable
330 * @see java.awt.Adjustable#getUnitIncrement
331 * @see javax.swing.Scrollable
332 * @see javax.swing.Scrollable#getScrollableUnitIncrement
333 * @see java.awt.ScrollPane
334 * @see java.awt.ScrollPane#setWheelScrollingEnabled
335 * @see javax.swing.JScrollPane
336 * @see javax.swing.JScrollPane#setWheelScrollingEnabled
337 */
338 public int getUnitsToScroll() {
339 return scrollAmount * wheelRotation;
340 }
341
342 /**
343 * Returns a parameter string identifying this event.
344 * This method is useful for event-logging and for debugging.
345 *
346 * @return a string identifying the event and its attributes
347 */
348 public String paramString() {
349 String scrollTypeStr = null;
350
351 if (getScrollType() == WHEEL_UNIT_SCROLL) {
352 scrollTypeStr = "WHEEL_UNIT_SCROLL";
353 } else if (getScrollType() == WHEEL_BLOCK_SCROLL) {
354 scrollTypeStr = "WHEEL_BLOCK_SCROLL";
355 } else {
356 scrollTypeStr = "unknown scroll type";
357 }
358 return super .paramString() + ",scrollType=" + scrollTypeStr
359 + ",scrollAmount=" + getScrollAmount()
360 + ",wheelRotation=" + getWheelRotation();
361 }
362 }
|