1 | /* MouseWheelEvent.java -- a mouse wheel event
|
---|
2 | Copyright (C) 2002 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 |
|
---|
39 | package java.awt.event;
|
---|
40 |
|
---|
41 | import java.awt.Component;
|
---|
42 | import java.awt.Point;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * This event is generated for a mouse wheel rotation. The wheel (the middle
|
---|
46 | * mouse button on most modern mice) can be rotated towards or away from the
|
---|
47 | * user, and is ofteh used for scrolling.
|
---|
48 | *
|
---|
49 | * <p>Because of the special use for scrolling components, MouseWheelEvents
|
---|
50 | * often affect a different component than the one located at the point of
|
---|
51 | * the event. If the component under the mouse cursor does not accept wheel
|
---|
52 | * events, the event is passed to the first ancestor container which does. This
|
---|
53 | * is often a ScrollPane, which knows how to scroll. If an AWT component is
|
---|
54 | * built from a native widget that knows how to use mouse wheel events, that
|
---|
55 | * component will consume the event.
|
---|
56 | *
|
---|
57 | * <p>The two most common scroll types are "units" (lines at a time) or
|
---|
58 | * "blocks" (pages at a time). The initial setting is taken from the platform,
|
---|
59 | * although the user can adjust the setting at any time.
|
---|
60 | *
|
---|
61 | * @author Eric Blake <ebb9@email.byu.edu>
|
---|
62 | * @see MouseWheelListener
|
---|
63 | * @see ScrollPane
|
---|
64 | * @see ScrollPane#setWheelScrollingEnabled(boolean)
|
---|
65 | * @see JScrollPane
|
---|
66 | * @see JScrollPane#setWheelScrollingEnabled(boolean)
|
---|
67 | * @since 1.4
|
---|
68 | * @status updated to 1.4
|
---|
69 | */
|
---|
70 | public class MouseWheelEvent extends MouseEvent
|
---|
71 | {
|
---|
72 | /**
|
---|
73 | * Compatible with JDK 1.4+.
|
---|
74 | */
|
---|
75 | private static final long serialVersionUID = 6459879390515399677L;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Indicates scrolling by units (lines).
|
---|
79 | *
|
---|
80 | * @see #getScrollType()
|
---|
81 | */
|
---|
82 | public static final int WHEEL_UNIT_SCROLL = 0;
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Indicates scrolling by blocks (pages).
|
---|
86 | *
|
---|
87 | * @see #getScrollType()
|
---|
88 | */
|
---|
89 | public static final int WHEEL_BLOCK_SCROLL = 1;
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Indicates what scroll type should take place. This should be limited
|
---|
93 | * to {@link #WHEEL_UNIT_SCROLL} and {@link #WHEEL_BLOCK_SCROLL}.
|
---|
94 | *
|
---|
95 | * @serial the scroll type
|
---|
96 | */
|
---|
97 | private final int scrollType;
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Indicates the scroll amount. This is only meaningful if scrollType is
|
---|
101 | * WHEEL_UNIT_SCROLL.
|
---|
102 | *
|
---|
103 | * @serial the number of lines to scroll
|
---|
104 | */
|
---|
105 | private final int scrollAmount;
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Indicates how far the mouse wheel was rotated.
|
---|
109 | *
|
---|
110 | * @serial the rotation amount
|
---|
111 | */
|
---|
112 | private final int wheelRotation;
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Initializes a new instance of <code>MouseWheelEvent</code> with the
|
---|
116 | * specified information. Note that an invalid id leads to unspecified
|
---|
117 | * results.
|
---|
118 | *
|
---|
119 | * @param source the source of the event
|
---|
120 | * @param id the event id
|
---|
121 | * @param when the timestamp of when the event occurred
|
---|
122 | * @param modifiers any modifier bits for this event
|
---|
123 | * @param x the X coordinate of the mouse point
|
---|
124 | * @param y the Y coordinate of the mouse point
|
---|
125 | * @param clickCount the number of mouse clicks for this event
|
---|
126 | * @param popupTrigger true if this event triggers a popup menu
|
---|
127 | * @param scrollType one of {@link #WHEEL_UNIT_SCROLL},
|
---|
128 | * {@link #WHEEL_BLOCK_SCROLL}
|
---|
129 | * @param scrollAmount the number of units to scroll, ignored for block type
|
---|
130 | * @param wheelRotation the number of rotation "clicks"
|
---|
131 | * @throws IllegalArgumentException if source is null
|
---|
132 | * @see MouseEvent#MouseEvent(Component, int, long, int, int, int, int,
|
---|
133 | * boolean)
|
---|
134 | */
|
---|
135 | public MouseWheelEvent(Component source, int id, long when, int modifiers,
|
---|
136 | int x, int y, int clickCount, boolean popupTrigger,
|
---|
137 | int scrollType, int scrollAmount, int wheelRotation)
|
---|
138 | {
|
---|
139 | super(source, id, when, modifiers, x, y, clickCount, popupTrigger);
|
---|
140 | this.scrollType = scrollType;
|
---|
141 | this.scrollAmount = scrollAmount;
|
---|
142 | this.wheelRotation = wheelRotation;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * This method returns the scrolling pattern this event requests. Legal
|
---|
147 | * values are WHEEL_UNIT_SCROLL and WHEEL_BLOCK_SCROLL.
|
---|
148 | *
|
---|
149 | * @return the scroll type
|
---|
150 | * @see Adjustable#getUnitIncrement()
|
---|
151 | * @see Adjustable#getBlockIncrement()
|
---|
152 | * @see Scrollable#getScrollableUnitIncrement(Rectangle, int, int)
|
---|
153 | * @see Scrollable#getScrollableBlockIncrement(Rectangle, int, int)
|
---|
154 | */
|
---|
155 | public int getScrollType()
|
---|
156 | {
|
---|
157 | return scrollType;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Returns the number of units to scroll in response to this event. This
|
---|
162 | * only makes sense when the scroll type is WHEEL_UNIT_SCROLL.
|
---|
163 | *
|
---|
164 | * @return the number of scroll units, if defined
|
---|
165 | * @see #getScrollType()
|
---|
166 | */
|
---|
167 | public int getScrollAmount()
|
---|
168 | {
|
---|
169 | return scrollAmount;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Gets the number of "clicks" the wheel was rotated. Negative values move
|
---|
174 | * up (away) from the user, positive values move down (towards) the user.
|
---|
175 | *
|
---|
176 | * @return the number of rotation clicks
|
---|
177 | */
|
---|
178 | public int getWheelRotation()
|
---|
179 | {
|
---|
180 | return wheelRotation;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * This is a convenience method which aids in a common listener for scrolling
|
---|
185 | * a scrollpane (although this is already built into ScrollPane and
|
---|
186 | * JScrollPane). This method only makes sense when getScrollType() returns
|
---|
187 | * WHEEL_UNIT_SCROLL.
|
---|
188 | *
|
---|
189 | * <p>This accounts for direction of scroll and amount of wheel movement, as
|
---|
190 | * interpreted by the platform settings.
|
---|
191 | *
|
---|
192 | * @return the number of units to scroll
|
---|
193 | * @see #getScrollType()
|
---|
194 | * @see #getScrollAmount()
|
---|
195 | * @see MouseWheelListener
|
---|
196 | * @see Adjustable
|
---|
197 | * @see Adjustable#getUnitIncrement()
|
---|
198 | * @see Scrollable
|
---|
199 | * @see Scrollable#getScrollableUnitIncrement(Rectangle, int, int)
|
---|
200 | * @see ScrollPane
|
---|
201 | * @see ScrollPane#setWheelScrollingEnabled(boolean)
|
---|
202 | * @see JScrollPane
|
---|
203 | * @see JScrollPane#setWheelScrollingEnabled(boolean)
|
---|
204 | */
|
---|
205 | public int getUnitsToScroll()
|
---|
206 | {
|
---|
207 | return wheelRotation * scrollAmount;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Returns a string identifying this event. For mouse wheel events, this
|
---|
212 | * is <code>super.paramString() + ",scrollType=WHEEL_" +
|
---|
213 | * (getScrollType() == WHEEL_UNIT_SCROLL ? "UNIT" : "BLOCK")
|
---|
214 | * + "_SCROLL,scrollAmount=" + getScrollAmount() + ",wheelRotation="
|
---|
215 | * + getWheelRotation()</code>.
|
---|
216 | *
|
---|
217 | * @return a string identifying this event
|
---|
218 | */
|
---|
219 | public String paramString()
|
---|
220 | {
|
---|
221 | return super.paramString() + ",scrollType="
|
---|
222 | + (scrollType == WHEEL_UNIT_SCROLL ? "WHEEL_UNIT_SCROLL"
|
---|
223 | : scrollType == WHEEL_BLOCK_SCROLL ? "WHEEL_BLOCK_SCROLL"
|
---|
224 | : "unknown scroll type")
|
---|
225 | + ",scrollAmount=" + scrollAmount + ",wheelRotation=" + wheelRotation;
|
---|
226 | }
|
---|
227 | } // class MouseWheelEvent
|
---|