author | dav |
Fri, 22 May 2009 16:09:45 +0400 | |
changeset 2810 | fa49c6a06baf |
parent 1962 | 6c293d33645b |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 1996-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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 |
||
26 |
package java.awt.event; |
|
27 |
||
28 |
import java.awt.Component; |
|
29 |
import java.awt.GraphicsEnvironment; |
|
30 |
import java.awt.Point; |
|
31 |
import java.awt.Toolkit; |
|
32 |
import java.io.IOException; |
|
33 |
import java.io.ObjectInputStream; |
|
34 |
import java.awt.IllegalComponentStateException; |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
35 |
import java.awt.MouseInfo; |
2810
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
36 |
import sun.awt.SunToolkit; |
2 | 37 |
|
38 |
/** |
|
39 |
* An event which indicates that a mouse action occurred in a component. |
|
40 |
* A mouse action is considered to occur in a particular component if and only |
|
41 |
* if the mouse cursor is over the unobscured part of the component's bounds |
|
42 |
* when the action happens. |
|
43 |
* For lightweight components, such as Swing's components, mouse events |
|
44 |
* are only dispatched to the component if the mouse event type has been |
|
45 |
* enabled on the component. A mouse event type is enabled by adding the |
|
46 |
* appropriate mouse-based {@code EventListener} to the component |
|
47 |
* ({@link MouseListener} or {@link MouseMotionListener}), or by invoking |
|
48 |
* {@link Component#enableEvents(long)} with the appropriate mask parameter |
|
49 |
* ({@code AWTEvent.MOUSE_EVENT_MASK} or {@code AWTEvent.MOUSE_MOTION_EVENT_MASK}). |
|
50 |
* If the mouse event type has not been enabled on the component, the |
|
51 |
* corresponding mouse events are dispatched to the first ancestor that |
|
52 |
* has enabled the mouse event type. |
|
53 |
*<p> |
|
54 |
* For example, if a {@code MouseListener} has been added to a component, or |
|
55 |
* {@code enableEvents(AWTEvent.MOUSE_EVENT_MASK)} has been invoked, then all |
|
56 |
* the events defined by {@code MouseListener} are dispatched to the component. |
|
57 |
* On the other hand, if a {@code MouseMotionListener} has not been added and |
|
58 |
* {@code enableEvents} has not been invoked with |
|
59 |
* {@code AWTEvent.MOUSE_MOTION_EVENT_MASK}, then mouse motion events are not |
|
60 |
* dispatched to the component. Instead the mouse motion events are |
|
61 |
* dispatched to the first ancestors that has enabled mouse motion |
|
62 |
* events. |
|
63 |
* <P> |
|
64 |
* This low-level event is generated by a component object for: |
|
65 |
* <ul> |
|
66 |
* <li>Mouse Events |
|
67 |
* <ul> |
|
68 |
* <li>a mouse button is pressed |
|
69 |
* <li>a mouse button is released |
|
70 |
* <li>a mouse button is clicked (pressed and released) |
|
71 |
* <li>the mouse cursor enters the unobscured part of component's geometry |
|
72 |
* <li>the mouse cursor exits the unobscured part of component's geometry |
|
73 |
* </ul> |
|
74 |
* <li> Mouse Motion Events |
|
75 |
* <ul> |
|
76 |
* <li>the mouse is moved |
|
77 |
* <li>the mouse is dragged |
|
78 |
* </ul> |
|
79 |
* </ul> |
|
80 |
* <P> |
|
81 |
* A <code>MouseEvent</code> object is passed to every |
|
82 |
* <code>MouseListener</code> |
|
83 |
* or <code>MouseAdapter</code> object which is registered to receive |
|
84 |
* the "interesting" mouse events using the component's |
|
85 |
* <code>addMouseListener</code> method. |
|
86 |
* (<code>MouseAdapter</code> objects implement the |
|
87 |
* <code>MouseListener</code> interface.) Each such listener object |
|
88 |
* gets a <code>MouseEvent</code> containing the mouse event. |
|
89 |
* <P> |
|
90 |
* A <code>MouseEvent</code> object is also passed to every |
|
91 |
* <code>MouseMotionListener</code> or |
|
92 |
* <code>MouseMotionAdapter</code> object which is registered to receive |
|
93 |
* mouse motion events using the component's |
|
94 |
* <code>addMouseMotionListener</code> |
|
95 |
* method. (<code>MouseMotionAdapter</code> objects implement the |
|
96 |
* <code>MouseMotionListener</code> interface.) Each such listener object |
|
97 |
* gets a <code>MouseEvent</code> containing the mouse motion event. |
|
98 |
* <P> |
|
99 |
* When a mouse button is clicked, events are generated and sent to the |
|
100 |
* registered <code>MouseListener</code>s. |
|
101 |
* The state of modal keys can be retrieved using {@link InputEvent#getModifiers} |
|
102 |
* and {@link InputEvent#getModifiersEx}. |
|
103 |
* The button mask returned by {@link InputEvent#getModifiers} reflects |
|
104 |
* only the button that changed state, not the current state of all buttons. |
|
105 |
* (Note: Due to overlap in the values of ALT_MASK/BUTTON2_MASK and |
|
106 |
* META_MASK/BUTTON3_MASK, this is not always true for mouse events involving |
|
107 |
* modifier keys). |
|
108 |
* To get the state of all buttons and modifier keys, use |
|
109 |
* {@link InputEvent#getModifiersEx}. |
|
110 |
* The button which has changed state is returned by {@link MouseEvent#getButton} |
|
111 |
* <P> |
|
112 |
* For example, if the first mouse button is pressed, events are sent in the |
|
113 |
* following order: |
|
114 |
* <PRE> |
|
115 |
* <b >id </b > <b >modifiers </b > <b >button </b > |
|
116 |
* <code>MOUSE_PRESSED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code> |
|
117 |
* <code>MOUSE_RELEASED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code> |
|
118 |
* <code>MOUSE_CLICKED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code> |
|
119 |
* </PRE> |
|
120 |
* When multiple mouse buttons are pressed, each press, release, and click |
|
121 |
* results in a separate event. |
|
122 |
* <P> |
|
123 |
* For example, if the user presses <b>button 1</b> followed by |
|
124 |
* <b>button 2</b>, and then releases them in the same order, |
|
125 |
* the following sequence of events is generated: |
|
126 |
* <PRE> |
|
127 |
* <b >id </b > <b >modifiers </b > <b >button </b > |
|
128 |
* <code>MOUSE_PRESSED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code> |
|
129 |
* <code>MOUSE_PRESSED</code>: <code>BUTTON2_MASK</code> <code>BUTTON2</code> |
|
130 |
* <code>MOUSE_RELEASED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code> |
|
131 |
* <code>MOUSE_CLICKED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code> |
|
132 |
* <code>MOUSE_RELEASED</code>: <code>BUTTON2_MASK</code> <code>BUTTON2</code> |
|
133 |
* <code>MOUSE_CLICKED</code>: <code>BUTTON2_MASK</code> <code>BUTTON2</code> |
|
134 |
* </PRE> |
|
135 |
* If <b>button 2</b> is released first, the |
|
136 |
* <code>MOUSE_RELEASED</code>/<code>MOUSE_CLICKED</code> pair |
|
137 |
* for <code>BUTTON2_MASK</code> arrives first, |
|
138 |
* followed by the pair for <code>BUTTON1_MASK</code>. |
|
139 |
* <p> |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
140 |
* Some extra mouse buttons are added to extend the standard set of buttons |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
141 |
* represented by the following constants:{@code BUTTON1}, {@code BUTTON2}, and {@code BUTTON3}. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
142 |
* Extra buttons have no assigned {@code BUTTONx} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
143 |
* constants as well as their button masks have no assigned {@code BUTTONx_DOWN_MASK} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
144 |
* constants. Nevertheless, ordinal numbers starting from 4 may be |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
145 |
* used as button numbers (button ids). Values obtained by the |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
146 |
* {@link InputEvent#getMaskForButton(int) getMaskForButton(button)} method may be used |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
147 |
* as button masks. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
148 |
* <p> |
2 | 149 |
* <code>MOUSE_DRAGGED</code> events are delivered to the <code>Component</code> |
150 |
* in which the mouse button was pressed until the mouse button is released |
|
151 |
* (regardless of whether the mouse position is within the bounds of the |
|
152 |
* <code>Component</code>). Due to platform-dependent Drag&Drop implementations, |
|
153 |
* <code>MOUSE_DRAGGED</code> events may not be delivered during a native |
|
154 |
* Drag&Drop operation. |
|
155 |
* |
|
156 |
* In a multi-screen environment mouse drag events are delivered to the |
|
157 |
* <code>Component</code> even if the mouse position is outside the bounds of the |
|
158 |
* <code>GraphicsConfiguration</code> associated with that |
|
159 |
* <code>Component</code>. However, the reported position for mouse drag events |
|
160 |
* in this case may differ from the actual mouse position: |
|
161 |
* <ul> |
|
162 |
* <li>In a multi-screen environment without a virtual device: |
|
163 |
* <br> |
|
164 |
* The reported coordinates for mouse drag events are clipped to fit within the |
|
165 |
* bounds of the <code>GraphicsConfiguration</code> associated with |
|
166 |
* the <code>Component</code>. |
|
167 |
* <li>In a multi-screen environment with a virtual device: |
|
168 |
* <br> |
|
169 |
* The reported coordinates for mouse drag events are clipped to fit within the |
|
170 |
* bounds of the virtual device associated with the <code>Component</code>. |
|
171 |
* </ul> |
|
440 | 172 |
* <p> |
173 |
* An unspecified behavior will be caused if the {@code id} parameter |
|
174 |
* of any particular {@code MouseEvent} instance is not |
|
175 |
* in the range from {@code MOUSE_FIRST} to {@code MOUSE_LAST}-1 |
|
176 |
* ({@code MOUSE_WHEEL} is not acceptable). |
|
2 | 177 |
* |
178 |
* @author Carl Quinn |
|
179 |
* |
|
180 |
* @see MouseAdapter |
|
181 |
* @see MouseListener |
|
182 |
* @see MouseMotionAdapter |
|
183 |
* @see MouseMotionListener |
|
184 |
* @see MouseWheelListener |
|
185 |
* @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/mouselistener.html">Tutorial: Writing a Mouse Listener</a> |
|
186 |
* @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/mousemotionlistener.html">Tutorial: Writing a Mouse Motion Listener</a> |
|
187 |
* |
|
188 |
* @since 1.1 |
|
189 |
*/ |
|
190 |
public class MouseEvent extends InputEvent { |
|
191 |
||
192 |
/** |
|
193 |
* The first number in the range of ids used for mouse events. |
|
194 |
*/ |
|
195 |
public static final int MOUSE_FIRST = 500; |
|
196 |
||
197 |
/** |
|
198 |
* The last number in the range of ids used for mouse events. |
|
199 |
*/ |
|
200 |
public static final int MOUSE_LAST = 507; |
|
201 |
||
202 |
/** |
|
203 |
* The "mouse clicked" event. This <code>MouseEvent</code> |
|
204 |
* occurs when a mouse button is pressed and released. |
|
205 |
*/ |
|
206 |
public static final int MOUSE_CLICKED = MOUSE_FIRST; |
|
207 |
||
208 |
/** |
|
209 |
* The "mouse pressed" event. This <code>MouseEvent</code> |
|
210 |
* occurs when a mouse button is pushed down. |
|
211 |
*/ |
|
212 |
public static final int MOUSE_PRESSED = 1 + MOUSE_FIRST; //Event.MOUSE_DOWN |
|
213 |
||
214 |
/** |
|
215 |
* The "mouse released" event. This <code>MouseEvent</code> |
|
216 |
* occurs when a mouse button is let up. |
|
217 |
*/ |
|
218 |
public static final int MOUSE_RELEASED = 2 + MOUSE_FIRST; //Event.MOUSE_UP |
|
219 |
||
220 |
/** |
|
221 |
* The "mouse moved" event. This <code>MouseEvent</code> |
|
222 |
* occurs when the mouse position changes. |
|
223 |
*/ |
|
224 |
public static final int MOUSE_MOVED = 3 + MOUSE_FIRST; //Event.MOUSE_MOVE |
|
225 |
||
226 |
/** |
|
227 |
* The "mouse entered" event. This <code>MouseEvent</code> |
|
228 |
* occurs when the mouse cursor enters the unobscured part of component's |
|
229 |
* geometry. |
|
230 |
*/ |
|
231 |
public static final int MOUSE_ENTERED = 4 + MOUSE_FIRST; //Event.MOUSE_ENTER |
|
232 |
||
233 |
/** |
|
234 |
* The "mouse exited" event. This <code>MouseEvent</code> |
|
235 |
* occurs when the mouse cursor exits the unobscured part of component's |
|
236 |
* geometry. |
|
237 |
*/ |
|
238 |
public static final int MOUSE_EXITED = 5 + MOUSE_FIRST; //Event.MOUSE_EXIT |
|
239 |
||
240 |
/** |
|
241 |
* The "mouse dragged" event. This <code>MouseEvent</code> |
|
242 |
* occurs when the mouse position changes while a mouse button is pressed. |
|
243 |
*/ |
|
244 |
public static final int MOUSE_DRAGGED = 6 + MOUSE_FIRST; //Event.MOUSE_DRAG |
|
245 |
||
246 |
/** |
|
247 |
* The "mouse wheel" event. This is the only <code>MouseWheelEvent</code>. |
|
248 |
* It occurs when a mouse equipped with a wheel has its wheel rotated. |
|
249 |
* @since 1.4 |
|
250 |
*/ |
|
251 |
public static final int MOUSE_WHEEL = 7 + MOUSE_FIRST; |
|
252 |
||
253 |
/** |
|
254 |
* Indicates no mouse buttons; used by {@link #getButton}. |
|
255 |
* @since 1.4 |
|
256 |
*/ |
|
257 |
public static final int NOBUTTON = 0; |
|
258 |
||
259 |
/** |
|
260 |
* Indicates mouse button #1; used by {@link #getButton}. |
|
261 |
* @since 1.4 |
|
262 |
*/ |
|
263 |
public static final int BUTTON1 = 1; |
|
264 |
||
265 |
/** |
|
266 |
* Indicates mouse button #2; used by {@link #getButton}. |
|
267 |
* @since 1.4 |
|
268 |
*/ |
|
269 |
public static final int BUTTON2 = 2; |
|
270 |
||
271 |
/** |
|
272 |
* Indicates mouse button #3; used by {@link #getButton}. |
|
273 |
* @since 1.4 |
|
274 |
*/ |
|
275 |
public static final int BUTTON3 = 3; |
|
276 |
||
277 |
/** |
|
278 |
* The mouse event's x coordinate. |
|
279 |
* The x value is relative to the component that fired the event. |
|
280 |
* |
|
281 |
* @serial |
|
282 |
* @see #getX() |
|
283 |
*/ |
|
284 |
int x; |
|
285 |
||
286 |
/** |
|
287 |
* The mouse event's y coordinate. |
|
288 |
* The y value is relative to the component that fired the event. |
|
289 |
* |
|
290 |
* @serial |
|
291 |
* @see #getY() |
|
292 |
*/ |
|
293 |
int y; |
|
294 |
||
295 |
/** |
|
296 |
* The mouse event's x absolute coordinate. |
|
297 |
* In a virtual device multi-screen environment in which the |
|
298 |
* desktop area could span multiple physical screen devices, |
|
299 |
* this coordinate is relative to the virtual coordinate system. |
|
300 |
* Otherwise, this coordinate is relative to the coordinate system |
|
301 |
* associated with the Component's GraphicsConfiguration. |
|
302 |
* |
|
303 |
* @serial |
|
304 |
*/ |
|
305 |
private int xAbs; |
|
306 |
||
307 |
/** |
|
308 |
* The mouse event's y absolute coordinate. |
|
309 |
* In a virtual device multi-screen environment in which the |
|
310 |
* desktop area could span multiple physical screen devices, |
|
311 |
* this coordinate is relative to the virtual coordinate system. |
|
312 |
* Otherwise, this coordinate is relative to the coordinate system |
|
313 |
* associated with the Component's GraphicsConfiguration. |
|
314 |
* |
|
315 |
* @serial |
|
316 |
*/ |
|
317 |
private int yAbs; |
|
318 |
||
319 |
/** |
|
320 |
* Indicates the number of quick consecutive clicks of |
|
321 |
* a mouse button. |
|
322 |
* clickCount will be valid for only three mouse events :<BR> |
|
323 |
* <code>MOUSE_CLICKED</code>, |
|
324 |
* <code>MOUSE_PRESSED</code> and |
|
325 |
* <code>MOUSE_RELEASED</code>. |
|
326 |
* For the above, the <code>clickCount</code> will be at least 1. |
|
327 |
* For all other events the count will be 0. |
|
328 |
* |
|
329 |
* @serial |
|
330 |
* @see #getClickCount(). |
|
331 |
*/ |
|
332 |
int clickCount; |
|
333 |
||
334 |
/** |
|
335 |
* Indicates which, if any, of the mouse buttons has changed state. |
|
336 |
* |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
337 |
* The valid values are ranged from 0 to the value returned by the |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
338 |
* {@link java.awt.MouseInfo#getNumberOfButtons() MouseInfo.getNumberOfButtons()} method. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
339 |
* This range already includes constants {@code NOBUTTON}, {@code BUTTON1}, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
340 |
* {@code BUTTON2}, and {@code BUTTON3} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
341 |
* if these buttons are present. So it is allowed to use these constants too. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
342 |
* For example, for a mouse with two buttons this field may contain the following values: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
343 |
* <ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
344 |
* <li> 0 ({@code NOBUTTON}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
345 |
* <li> 1 ({@code BUTTON1}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
346 |
* <li> 2 ({@code BUTTON2}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
347 |
* </ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
348 |
* If a mouse has 5 buttons, this field may contain the following values: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
349 |
* <ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
350 |
* <li> 0 ({@code NOBUTTON}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
351 |
* <li> 1 ({@code BUTTON1}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
352 |
* <li> 2 ({@code BUTTON2}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
353 |
* <li> 3 ({@code BUTTON3}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
354 |
* <li> 4 |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
355 |
* <li> 5 |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
356 |
* </ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
357 |
* If support for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled()} disabled by Java |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
358 |
* then the field may not contain the value larger than {@code BUTTON3}. |
2 | 359 |
* @serial |
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
360 |
* @see #getButton() |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
361 |
* @see java.awt.Toolkit#areExtraMouseButtonsEnabled() |
2 | 362 |
*/ |
363 |
int button; |
|
364 |
||
365 |
/** |
|
366 |
* A property used to indicate whether a Popup Menu |
|
367 |
* should appear with a certain gestures. |
|
368 |
* If <code>popupTrigger</code> = <code>false</code>, |
|
369 |
* no popup menu should appear. If it is <code>true</code> |
|
370 |
* then a popup menu should appear. |
|
371 |
* |
|
372 |
* @serial |
|
373 |
* @see java.awt.PopupMenu |
|
374 |
* @see #isPopupTrigger() |
|
375 |
*/ |
|
376 |
boolean popupTrigger = false; |
|
377 |
||
378 |
/* |
|
379 |
* JDK 1.1 serialVersionUID |
|
380 |
*/ |
|
381 |
private static final long serialVersionUID = -991214153494842848L; |
|
382 |
||
2810
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
383 |
/** |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
384 |
* A number of buttons available on the mouse at the {@code Toolkit} machinery startup. |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
385 |
*/ |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
386 |
private static int cachedNumberOfButtons; |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
387 |
|
2 | 388 |
static { |
389 |
/* ensure that the necessary native libraries are loaded */ |
|
390 |
NativeLibLoader.loadLibraries(); |
|
391 |
if (!GraphicsEnvironment.isHeadless()) { |
|
392 |
initIDs(); |
|
393 |
} |
|
2810
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
394 |
final Toolkit tk = Toolkit.getDefaultToolkit(); |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
395 |
if (tk instanceof SunToolkit) { |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
396 |
cachedNumberOfButtons = ((SunToolkit)tk).getNumberOfButtons(); |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
397 |
} else { |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
398 |
//It's expected that some toolkits (Headless, |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
399 |
//whatever besides SunToolkit) could also operate. |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
400 |
cachedNumberOfButtons = 3; |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
401 |
} |
2 | 402 |
} |
403 |
||
404 |
/** |
|
405 |
* Initialize JNI field and method IDs for fields that may be |
|
406 |
accessed from C. |
|
407 |
*/ |
|
408 |
private static native void initIDs(); |
|
409 |
||
410 |
/** |
|
411 |
* Returns the absolute x, y position of the event. |
|
412 |
* In a virtual device multi-screen environment in which the |
|
413 |
* desktop area could span multiple physical screen devices, |
|
414 |
* these coordinates are relative to the virtual coordinate system. |
|
415 |
* Otherwise, these coordinates are relative to the coordinate system |
|
416 |
* associated with the Component's GraphicsConfiguration. |
|
417 |
* |
|
418 |
* @return a <code>Point</code> object containing the absolute x |
|
419 |
* and y coordinates. |
|
420 |
* |
|
421 |
* @see java.awt.GraphicsConfiguration |
|
422 |
* @since 1.6 |
|
423 |
*/ |
|
424 |
public Point getLocationOnScreen(){ |
|
425 |
return new Point(xAbs, yAbs); |
|
426 |
} |
|
427 |
||
428 |
/** |
|
429 |
* Returns the absolute horizontal x position of the event. |
|
430 |
* In a virtual device multi-screen environment in which the |
|
431 |
* desktop area could span multiple physical screen devices, |
|
432 |
* this coordinate is relative to the virtual coordinate system. |
|
433 |
* Otherwise, this coordinate is relative to the coordinate system |
|
434 |
* associated with the Component's GraphicsConfiguration. |
|
435 |
* |
|
436 |
* @return x an integer indicating absolute horizontal position. |
|
437 |
* |
|
438 |
* @see java.awt.GraphicsConfiguration |
|
439 |
* @since 1.6 |
|
440 |
*/ |
|
441 |
public int getXOnScreen() { |
|
442 |
return xAbs; |
|
443 |
} |
|
444 |
||
445 |
/** |
|
446 |
* Returns the absolute vertical y position of the event. |
|
447 |
* In a virtual device multi-screen environment in which the |
|
448 |
* desktop area could span multiple physical screen devices, |
|
449 |
* this coordinate is relative to the virtual coordinate system. |
|
450 |
* Otherwise, this coordinate is relative to the coordinate system |
|
451 |
* associated with the Component's GraphicsConfiguration. |
|
452 |
* |
|
453 |
* @return y an integer indicating absolute vertical position. |
|
454 |
* |
|
455 |
* @see java.awt.GraphicsConfiguration |
|
456 |
* @since 1.6 |
|
457 |
*/ |
|
458 |
public int getYOnScreen() { |
|
459 |
return yAbs; |
|
460 |
} |
|
461 |
||
462 |
/** |
|
463 |
* Constructs a <code>MouseEvent</code> object with the |
|
464 |
* specified source component, |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
465 |
* type, time, modifiers, coordinates, click count, popupTrigger flag, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
466 |
* and button number. |
2 | 467 |
* <p> |
440 | 468 |
* Creating an invalid event (such |
2 | 469 |
* as by using more than one of the old _MASKs, or modifier/button |
470 |
* values which don't match) results in unspecified behavior. |
|
471 |
* An invocation of the form |
|
472 |
* <tt>MouseEvent(source, id, when, modifiers, x, y, clickCount, popupTrigger, button)</tt> |
|
473 |
* behaves in exactly the same way as the invocation |
|
474 |
* <tt> {@link #MouseEvent(Component, int, long, int, int, int, |
|
475 |
* int, int, int, boolean, int) MouseEvent}(source, id, when, modifiers, |
|
476 |
* x, y, xAbs, yAbs, clickCount, popupTrigger, button)</tt> |
|
477 |
* where xAbs and yAbs defines as source's location on screen plus |
|
478 |
* relative coordinates x and y. |
|
479 |
* xAbs and yAbs are set to zero if the source is not showing. |
|
480 |
* This method throws an |
|
481 |
* <code>IllegalArgumentException</code> if <code>source</code> |
|
482 |
* is <code>null</code>. |
|
483 |
* |
|
440 | 484 |
* @param source The <code>Component</code> that originated the event |
485 |
* @param id An integer indicating the type of event. |
|
486 |
* For information on allowable values, see |
|
487 |
* the class description for {@link MouseEvent} |
|
488 |
* @param when A long integer that gives the time the event occurred. |
|
489 |
* Passing negative or zero value |
|
490 |
* is not recommended |
|
491 |
* @param modifiers The modifier keys down during event (e.g. shift, ctrl, |
|
2 | 492 |
* alt, meta) |
440 | 493 |
* Passing negative parameter |
494 |
* is not recommended. |
|
495 |
* Zero value means that no modifiers were passed. |
|
496 |
* Use either an extended _DOWN_MASK or old _MASK modifiers, |
|
497 |
* however do not mix models in the one event. |
|
498 |
* The extended modifiers are preferred for using |
|
499 |
* @param x The horizontal x coordinate for the mouse location. |
|
500 |
* It is allowed to pass negative values |
|
501 |
* @param y The vertical y coordinate for the mouse location. |
|
502 |
* It is allowed to pass negative values |
|
503 |
* @param clickCount The number of mouse clicks associated with event. |
|
504 |
* Passing negative value |
|
505 |
* is not recommended |
|
506 |
* @param popupTrigger A boolean that equals {@code true} if this event |
|
507 |
* is a trigger for a popup menu |
|
508 |
* @param button An integer that indicates, which of the mouse buttons has |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
509 |
* changed its state. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
510 |
* The following rules are applied to this parameter: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
511 |
* <ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
512 |
* <li>If support for the extended mouse buttons is |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
513 |
* {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
514 |
* then it is allowed to create {@code MouseEvent} objects only with the standard buttons: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
515 |
* {@code NOBUTTON}, {@code BUTTON1}, {@code BUTTON2}, and |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
516 |
* {@code BUTTON3}. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
517 |
* <li> If support for the extended mouse buttons is |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
518 |
* {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
519 |
* then it is allowed to create {@code MouseEvent} objects with |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
520 |
* the standard buttons. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
521 |
* In case the support for extended mouse buttons is |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
522 |
* {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java, then |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
523 |
* in addition to the standard buttons, {@code MouseEvent} objects can be created |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
524 |
* using buttons from the range starting from 4 to |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
525 |
* {@link java.awt.MouseInfo#getNumberOfButtons() MouseInfo.getNumberOfButtons()} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
526 |
* if the mouse has more than three buttons. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
527 |
* </ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
528 |
* @throws IllegalArgumentException if {@code button} is less then zero |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
529 |
* @throws IllegalArgumentException if <code>source</code> is null |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
530 |
* @throws IllegalArgumentException if {@code button} is greater then BUTTON3 and the support for extended mouse buttons is |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
531 |
* {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
532 |
* @throws IllegalArgumentException if {@code button} is greater then the |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
533 |
* {@link java.awt.MouseInfo#getNumberOfButtons() current number of buttons} and the support |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
534 |
* for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() enabled} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
535 |
* by Java |
2 | 536 |
* @throws IllegalArgumentException if an invalid <code>button</code> |
537 |
* value is passed in |
|
538 |
* @throws IllegalArgumentException if <code>source</code> is null |
|
440 | 539 |
* @see #getSource() |
540 |
* @see #getID() |
|
541 |
* @see #getWhen() |
|
542 |
* @see #getModifiers() |
|
543 |
* @see #getX() |
|
544 |
* @see #getY() |
|
545 |
* @see #getClickCount() |
|
546 |
* @see #isPopupTrigger() |
|
547 |
* @see #getButton() |
|
2 | 548 |
* @since 1.4 |
549 |
*/ |
|
550 |
public MouseEvent(Component source, int id, long when, int modifiers, |
|
551 |
int x, int y, int clickCount, boolean popupTrigger, |
|
552 |
int button) |
|
553 |
{ |
|
554 |
this(source, id, when, modifiers, x, y, 0, 0, clickCount, popupTrigger, button); |
|
555 |
Point eventLocationOnScreen = new Point(0, 0); |
|
556 |
try { |
|
557 |
eventLocationOnScreen = source.getLocationOnScreen(); |
|
558 |
this.xAbs = eventLocationOnScreen.x + x; |
|
559 |
this.yAbs = eventLocationOnScreen.y + y; |
|
560 |
} catch (IllegalComponentStateException e){ |
|
561 |
this.xAbs = 0; |
|
562 |
this.yAbs = 0; |
|
563 |
} |
|
564 |
} |
|
565 |
||
566 |
/** |
|
567 |
* Constructs a <code>MouseEvent</code> object with the |
|
568 |
* specified source component, |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
569 |
* type, modifiers, coordinates, click count, and popupTrigger flag. |
2 | 570 |
* An invocation of the form |
571 |
* <tt>MouseEvent(source, id, when, modifiers, x, y, clickCount, popupTrigger)</tt> |
|
572 |
* behaves in exactly the same way as the invocation |
|
573 |
* <tt> {@link #MouseEvent(Component, int, long, int, int, int, |
|
574 |
* int, int, int, boolean, int) MouseEvent}(source, id, when, modifiers, |
|
575 |
* x, y, xAbs, yAbs, clickCount, popupTrigger, MouseEvent.NOBUTTON)</tt> |
|
576 |
* where xAbs and yAbs defines as source's location on screen plus |
|
577 |
* relative coordinates x and y. |
|
578 |
* xAbs and yAbs are set to zero if the source is not showing. |
|
579 |
* This method throws an <code>IllegalArgumentException</code> |
|
580 |
* if <code>source</code> is <code>null</code>. |
|
581 |
* |
|
440 | 582 |
* @param source The <code>Component</code> that originated the event |
583 |
* @param id An integer indicating the type of event. |
|
584 |
* For information on allowable values, see |
|
585 |
* the class description for {@link MouseEvent} |
|
586 |
* @param when A long integer that gives the time the event occurred. |
|
587 |
* Passing negative or zero value |
|
588 |
* is not recommended |
|
589 |
* @param modifiers The modifier keys down during event (e.g. shift, ctrl, |
|
2 | 590 |
* alt, meta) |
440 | 591 |
* Passing negative parameter |
592 |
* is not recommended. |
|
593 |
* Zero value means that no modifiers were passed. |
|
594 |
* Use either an extended _DOWN_MASK or old _MASK modifiers, |
|
595 |
* however do not mix models in the one event. |
|
596 |
* The extended modifiers are preferred for using |
|
597 |
* @param x The horizontal x coordinate for the mouse location. |
|
598 |
* It is allowed to pass negative values |
|
599 |
* @param y The vertical y coordinate for the mouse location. |
|
600 |
* It is allowed to pass negative values |
|
601 |
* @param clickCount The number of mouse clicks associated with event. |
|
602 |
* Passing negative value |
|
603 |
* is not recommended |
|
604 |
* @param popupTrigger A boolean that equals {@code true} if this event |
|
605 |
* is a trigger for a popup menu |
|
2 | 606 |
* @throws IllegalArgumentException if <code>source</code> is null |
440 | 607 |
* @see #getSource() |
608 |
* @see #getID() |
|
609 |
* @see #getWhen() |
|
610 |
* @see #getModifiers() |
|
611 |
* @see #getX() |
|
612 |
* @see #getY() |
|
613 |
* @see #getClickCount() |
|
614 |
* @see #isPopupTrigger() |
|
2 | 615 |
*/ |
616 |
public MouseEvent(Component source, int id, long when, int modifiers, |
|
617 |
int x, int y, int clickCount, boolean popupTrigger) { |
|
618 |
this(source, id, when, modifiers, x, y, clickCount, popupTrigger, NOBUTTON); |
|
619 |
} |
|
620 |
||
621 |
||
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
622 |
/* if the button is an extra button and it is released or clicked then in Xsystem its state |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
623 |
is not modified. Exclude this button number from ExtModifiers mask.*/ |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
624 |
transient private boolean shouldExcludeButtonFromExtModifiers = false; |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
625 |
|
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
626 |
/** |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
627 |
* {@inheritDoc} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
628 |
*/ |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
629 |
public int getModifiersEx() { |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
630 |
int tmpModifiers = modifiers; |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
631 |
if (shouldExcludeButtonFromExtModifiers) { |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
632 |
tmpModifiers &= ~(InputEvent.getMaskForButton(getButton())); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
633 |
} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
634 |
return tmpModifiers & ~JDK_1_3_MODIFIERS; |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
635 |
} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
636 |
|
2 | 637 |
/** |
638 |
* Constructs a <code>MouseEvent</code> object with the |
|
639 |
* specified source component, |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
640 |
* type, time, modifiers, coordinates, absolute coordinates, click count, popupTrigger flag, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
641 |
* and button number. |
2 | 642 |
* <p> |
440 | 643 |
* Creating an invalid event (such |
2 | 644 |
* as by using more than one of the old _MASKs, or modifier/button |
645 |
* values which don't match) results in unspecified behavior. |
|
646 |
* Even if inconsistent values for relative and absolute coordinates are |
|
647 |
* passed to the constructor, the mouse event instance is still |
|
648 |
* created and no exception is thrown. |
|
649 |
* This method throws an |
|
650 |
* <code>IllegalArgumentException</code> if <code>source</code> |
|
651 |
* is <code>null</code>. |
|
652 |
* |
|
440 | 653 |
* @param source The <code>Component</code> that originated the event |
654 |
* @param id An integer indicating the type of event. |
|
655 |
* For information on allowable values, see |
|
656 |
* the class description for {@link MouseEvent} |
|
657 |
* @param when A long integer that gives the time the event occurred. |
|
658 |
* Passing negative or zero value |
|
659 |
* is not recommended |
|
660 |
* @param modifiers The modifier keys down during event (e.g. shift, ctrl, |
|
2 | 661 |
* alt, meta) |
440 | 662 |
* Passing negative parameter |
663 |
* is not recommended. |
|
664 |
* Zero value means that no modifiers were passed. |
|
665 |
* Use either an extended _DOWN_MASK or old _MASK modifiers, |
|
666 |
* however do not mix models in the one event. |
|
667 |
* The extended modifiers are preferred for using |
|
668 |
* @param x The horizontal x coordinate for the mouse location. |
|
669 |
* It is allowed to pass negative values |
|
670 |
* @param y The vertical y coordinate for the mouse location. |
|
671 |
* It is allowed to pass negative values |
|
672 |
* @param xAbs The absolute horizontal x coordinate for the mouse location |
|
673 |
* It is allowed to pass negative values |
|
674 |
* @param yAbs The absolute vertical y coordinate for the mouse location |
|
675 |
* It is allowed to pass negative values |
|
676 |
* @param clickCount The number of mouse clicks associated with event. |
|
677 |
* Passing negative value |
|
678 |
* is not recommended |
|
679 |
* @param popupTrigger A boolean that equals {@code true} if this event |
|
680 |
* is a trigger for a popup menu |
|
681 |
* @param button An integer that indicates, which of the mouse buttons has |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
682 |
* changed its state. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
683 |
* The following rules are applied to this parameter: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
684 |
* <ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
685 |
* <li>If support for the extended mouse buttons is |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
686 |
* {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
687 |
* then it is allowed to create {@code MouseEvent} objects only with the standard buttons: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
688 |
* {@code NOBUTTON}, {@code BUTTON1}, {@code BUTTON2}, and |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
689 |
* {@code BUTTON3}. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
690 |
* <li> If support for the extended mouse buttons is |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
691 |
* {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
692 |
* then it is allowed to create {@code MouseEvent} objects with |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
693 |
* the standard buttons. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
694 |
* In case the support for extended mouse buttons is |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
695 |
* {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java, then |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
696 |
* in addition to the standard buttons, {@code MouseEvent} objects can be created |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
697 |
* using buttons from the range starting from 4 to |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
698 |
* {@link java.awt.MouseInfo#getNumberOfButtons() MouseInfo.getNumberOfButtons()} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
699 |
* if the mouse has more than three buttons. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
700 |
* </ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
701 |
* @throws IllegalArgumentException if {@code button} is less then zero |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
702 |
* @throws IllegalArgumentException if <code>source</code> is null |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
703 |
* @throws IllegalArgumentException if {@code button} is greater then BUTTON3 and the support for extended mouse buttons is |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
704 |
* {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
705 |
* @throws IllegalArgumentException if {@code button} is greater then the |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
706 |
* {@link java.awt.MouseInfo#getNumberOfButtons() current number of buttons} and the support |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
707 |
* for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() enabled} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
708 |
* by Java |
2 | 709 |
* @throws IllegalArgumentException if an invalid <code>button</code> |
710 |
* value is passed in |
|
711 |
* @throws IllegalArgumentException if <code>source</code> is null |
|
440 | 712 |
* @see #getSource() |
713 |
* @see #getID() |
|
714 |
* @see #getWhen() |
|
715 |
* @see #getModifiers() |
|
716 |
* @see #getX() |
|
717 |
* @see #getY() |
|
718 |
* @see #getXOnScreen() |
|
719 |
* @see #getYOnScreen() |
|
720 |
* @see #getClickCount() |
|
721 |
* @see #isPopupTrigger() |
|
722 |
* @see #getButton() |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
723 |
* @see #button |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
724 |
* @see Toolkit#areExtraMouseButtonsEnabled() |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
725 |
* @see java.awt.MouseInfo#getNumberOfButtons() |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
726 |
* @see InputEvent#getMaskForButton(int) |
2 | 727 |
* @since 1.6 |
728 |
*/ |
|
729 |
public MouseEvent(Component source, int id, long when, int modifiers, |
|
730 |
int x, int y, int xAbs, int yAbs, |
|
731 |
int clickCount, boolean popupTrigger, int button) |
|
732 |
{ |
|
733 |
super(source, id, when, modifiers); |
|
734 |
this.x = x; |
|
735 |
this.y = y; |
|
736 |
this.xAbs = xAbs; |
|
737 |
this.yAbs = yAbs; |
|
738 |
this.clickCount = clickCount; |
|
739 |
this.popupTrigger = popupTrigger; |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
740 |
if (button < NOBUTTON){ |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
741 |
throw new IllegalArgumentException("Invalid button value :" + button); |
2 | 742 |
} |
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
743 |
if (button > BUTTON3) { |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
744 |
if (!Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled()){ |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
745 |
throw new IllegalArgumentException("Extra mouse events are disabled " + button); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
746 |
} else { |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
747 |
if (button > cachedNumberOfButtons) { |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
748 |
throw new IllegalArgumentException("Nonexistent button " + button); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
749 |
} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
750 |
} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
751 |
// XToolkit: extra buttons are not reporting about their state correctly. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
752 |
// Being pressed they report the state=0 both on the press and on the release. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
753 |
// For 1-3 buttons the state value equals zero on press and non-zero on release. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
754 |
// Other modifiers like Shift, ALT etc seem report well with extra buttons. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
755 |
// The problem reveals as follows: one button is pressed and then another button is pressed and released. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
756 |
// So, the getModifiersEx() would not be zero due to a first button and we will skip this modifier. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
757 |
// This may have to be moved into the peer code instead if possible. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
758 |
|
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
759 |
if (getModifiersEx() != 0) { //There is at least one more button in a pressed state. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
760 |
if (id == MouseEvent.MOUSE_RELEASED || id == MouseEvent.MOUSE_CLICKED){ |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
761 |
System.out.println("MEvent. CASE!"); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
762 |
shouldExcludeButtonFromExtModifiers = true; |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
763 |
} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
764 |
} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
765 |
} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
766 |
|
2 | 767 |
this.button = button; |
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
768 |
|
2 | 769 |
if ((getModifiers() != 0) && (getModifiersEx() == 0)) { |
770 |
setNewModifiers(); |
|
771 |
} else if ((getModifiers() == 0) && |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
772 |
(getModifiersEx() != 0 || button != NOBUTTON) && |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
773 |
(button <= BUTTON3)) |
2 | 774 |
{ |
775 |
setOldModifiers(); |
|
776 |
} |
|
777 |
} |
|
778 |
||
779 |
/** |
|
780 |
* Returns the horizontal x position of the event relative to the |
|
781 |
* source component. |
|
782 |
* |
|
783 |
* @return x an integer indicating horizontal position relative to |
|
784 |
* the component |
|
785 |
*/ |
|
786 |
public int getX() { |
|
787 |
return x; |
|
788 |
} |
|
789 |
||
790 |
/** |
|
791 |
* Returns the vertical y position of the event relative to the |
|
792 |
* source component. |
|
793 |
* |
|
794 |
* @return y an integer indicating vertical position relative to |
|
795 |
* the component |
|
796 |
*/ |
|
797 |
public int getY() { |
|
798 |
return y; |
|
799 |
} |
|
800 |
||
801 |
/** |
|
802 |
* Returns the x,y position of the event relative to the source component. |
|
803 |
* |
|
804 |
* @return a <code>Point</code> object containing the x and y coordinates |
|
805 |
* relative to the source component |
|
806 |
* |
|
807 |
*/ |
|
808 |
public Point getPoint() { |
|
809 |
int x; |
|
810 |
int y; |
|
811 |
synchronized (this) { |
|
812 |
x = this.x; |
|
813 |
y = this.y; |
|
814 |
} |
|
815 |
return new Point(x, y); |
|
816 |
} |
|
817 |
||
818 |
/** |
|
819 |
* Translates the event's coordinates to a new position |
|
820 |
* by adding specified <code>x</code> (horizontal) and <code>y</code> |
|
821 |
* (vertical) offsets. |
|
822 |
* |
|
823 |
* @param x the horizontal x value to add to the current x |
|
824 |
* coordinate position |
|
825 |
* @param y the vertical y value to add to the current y |
|
826 |
coordinate position |
|
827 |
*/ |
|
828 |
public synchronized void translatePoint(int x, int y) { |
|
829 |
this.x += x; |
|
830 |
this.y += y; |
|
831 |
} |
|
832 |
||
833 |
/** |
|
834 |
* Returns the number of mouse clicks associated with this event. |
|
835 |
* |
|
836 |
* @return integer value for the number of clicks |
|
837 |
*/ |
|
838 |
public int getClickCount() { |
|
839 |
return clickCount; |
|
840 |
} |
|
841 |
||
842 |
/** |
|
843 |
* Returns which, if any, of the mouse buttons has changed state. |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
844 |
* The returned value is ranged |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
845 |
* from 0 to the {@link java.awt.MouseInfo#getNumberOfButtons() MouseInfo.getNumberOfButtons()} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
846 |
* value. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
847 |
* The returned value includes at least the following constants: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
848 |
* <ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
849 |
* <li> {@code NOBUTTON} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
850 |
* <li> {@code BUTTON1} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
851 |
* <li> {@code BUTTON2} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
852 |
* <li> {@code BUTTON3} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
853 |
* </ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
854 |
* It is allowed to use those constants to compare with the returned button number in the application. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
855 |
* For example, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
856 |
* <pre> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
857 |
* if (anEvent.getButton() == MouseEvent.BUTTON1) { |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
858 |
* </pre> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
859 |
* In particular, for a mouse with one, two, or three buttons this method may return the following values: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
860 |
* <ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
861 |
* <li> 0 ({@code NOBUTTON}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
862 |
* <li> 1 ({@code BUTTON1}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
863 |
* <li> 2 ({@code BUTTON2}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
864 |
* <li> 3 ({@code BUTTON3}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
865 |
* </ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
866 |
* Button numbers greater then {@code BUTTON3} have no constant identifier. So if a mouse with five buttons is |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
867 |
* installed, this method may return the following values: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
868 |
* <ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
869 |
* <li> 0 ({@code NOBUTTON}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
870 |
* <li> 1 ({@code BUTTON1}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
871 |
* <li> 2 ({@code BUTTON2}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
872 |
* <li> 3 ({@code BUTTON3}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
873 |
* <li> 4 |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
874 |
* <li> 5 |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
875 |
* </ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
876 |
* <p> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
877 |
* Note: If support for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
878 |
* then the AWT event subsystem does not produce mouse events for the extended mouse |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
879 |
* buttons. So it is not expected that this method returns anything except {@code NOBUTTON}, {@code BUTTON1}, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
880 |
* {@code BUTTON2}, {@code BUTTON3}. |
2 | 881 |
* |
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
882 |
* @return one of the values from 0 to {@link java.awt.MouseInfo#getNumberOfButtons() MouseInfo.getNumberOfButtons()} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
883 |
* if support for the extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
884 |
* That range includes {@code NOBUTTON}, {@code BUTTON1}, {@code BUTTON2}, {@code BUTTON3}; |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
885 |
* <br> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
886 |
* {@code NOBUTTON}, {@code BUTTON1}, {@code BUTTON2} or {@code BUTTON3} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
887 |
* if support for the extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java |
2 | 888 |
* @since 1.4 |
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
889 |
* @see Toolkit#areExtraMouseButtonsEnabled() |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
890 |
* @see java.awt.MouseInfo#getNumberOfButtons() |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
891 |
* @see #MouseEvent(Component, int, long, int, int, int, int, int, int, boolean, int) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
892 |
* @see InputEvent#getMaskForButton(int) |
2 | 893 |
*/ |
894 |
public int getButton() { |
|
895 |
return button; |
|
896 |
} |
|
897 |
||
898 |
/** |
|
899 |
* Returns whether or not this mouse event is the popup menu |
|
900 |
* trigger event for the platform. |
|
901 |
* <p><b>Note</b>: Popup menus are triggered differently |
|
902 |
* on different systems. Therefore, <code>isPopupTrigger</code> |
|
903 |
* should be checked in both <code>mousePressed</code> |
|
904 |
* and <code>mouseReleased</code> |
|
905 |
* for proper cross-platform functionality. |
|
906 |
* |
|
907 |
* @return boolean, true if this event is the popup menu trigger |
|
908 |
* for this platform |
|
909 |
*/ |
|
910 |
public boolean isPopupTrigger() { |
|
911 |
return popupTrigger; |
|
912 |
} |
|
913 |
||
914 |
/** |
|
440 | 915 |
* Returns a <code>String</code> instance describing the modifier keys and |
2 | 916 |
* mouse buttons that were down during the event, such as "Shift", |
917 |
* or "Ctrl+Shift". These strings can be localized by changing |
|
918 |
* the <code>awt.properties</code> file. |
|
919 |
* <p> |
|
440 | 920 |
* Note that the <code>InputEvent.ALT_MASK</code> and |
921 |
* <code>InputEvent.BUTTON2_MASK</code> have equal values, |
|
922 |
* so the "Alt" string is returned for both modifiers. Likewise, |
|
923 |
* the <code>InputEvent.META_MASK</code> and |
|
924 |
* <code>InputEvent.BUTTON3_MASK</code> have equal values, |
|
925 |
* so the "Meta" string is returned for both modifiers. |
|
926 |
* <p> |
|
927 |
* Note that passing negative parameter is incorrect, |
|
928 |
* and will cause the returning an unspecified string. |
|
929 |
* Zero parameter means that no modifiers were passed and will |
|
930 |
* cause the returning an empty string. |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
931 |
* <p> |
440 | 932 |
* @param modifiers A modifier mask describing the modifier keys and |
2 | 933 |
* mouse buttons that were down during the event |
440 | 934 |
* @return string string text description of the combination of modifier |
2 | 935 |
* keys and mouse buttons that were down during the event |
936 |
* @see InputEvent#getModifiersExText(int) |
|
937 |
* @since 1.4 |
|
938 |
*/ |
|
939 |
public static String getMouseModifiersText(int modifiers) { |
|
940 |
StringBuilder buf = new StringBuilder(); |
|
941 |
if ((modifiers & InputEvent.ALT_MASK) != 0) { |
|
942 |
buf.append(Toolkit.getProperty("AWT.alt", "Alt")); |
|
943 |
buf.append("+"); |
|
944 |
} |
|
945 |
if ((modifiers & InputEvent.META_MASK) != 0) { |
|
946 |
buf.append(Toolkit.getProperty("AWT.meta", "Meta")); |
|
947 |
buf.append("+"); |
|
948 |
} |
|
949 |
if ((modifiers & InputEvent.CTRL_MASK) != 0) { |
|
950 |
buf.append(Toolkit.getProperty("AWT.control", "Ctrl")); |
|
951 |
buf.append("+"); |
|
952 |
} |
|
953 |
if ((modifiers & InputEvent.SHIFT_MASK) != 0) { |
|
954 |
buf.append(Toolkit.getProperty("AWT.shift", "Shift")); |
|
955 |
buf.append("+"); |
|
956 |
} |
|
957 |
if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) { |
|
958 |
buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph")); |
|
959 |
buf.append("+"); |
|
960 |
} |
|
961 |
if ((modifiers & InputEvent.BUTTON1_MASK) != 0) { |
|
962 |
buf.append(Toolkit.getProperty("AWT.button1", "Button1")); |
|
963 |
buf.append("+"); |
|
964 |
} |
|
965 |
if ((modifiers & InputEvent.BUTTON2_MASK) != 0) { |
|
966 |
buf.append(Toolkit.getProperty("AWT.button2", "Button2")); |
|
967 |
buf.append("+"); |
|
968 |
} |
|
969 |
if ((modifiers & InputEvent.BUTTON3_MASK) != 0) { |
|
970 |
buf.append(Toolkit.getProperty("AWT.button3", "Button3")); |
|
971 |
buf.append("+"); |
|
972 |
} |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
973 |
|
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
974 |
int mask; |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
975 |
|
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
976 |
// TODO: add a toolkit field that holds a number of button on the mouse. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
977 |
// As the method getMouseModifiersText() is static and obtain |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
978 |
// an integer as a parameter then we may not restrict this with the number |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
979 |
// of buttons installed on the mouse. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
980 |
// It's a temporary solution. We need to somehow hold the number of buttons somewhere else. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
981 |
for (int i = 1; i <= cachedNumberOfButtons; i++){ |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
982 |
mask = InputEvent.getMaskForButton(i); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
983 |
if ((modifiers & mask) != 0 && |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
984 |
buf.indexOf(Toolkit.getProperty("AWT.button"+i, "Button"+i)) == -1) //1,2,3 buttons may already be there; so don't duplicate it. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
985 |
{ |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
986 |
buf.append(Toolkit.getProperty("AWT.button"+i, "Button"+i)); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
987 |
buf.append("+"); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
988 |
} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
989 |
} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
990 |
|
2 | 991 |
if (buf.length() > 0) { |
992 |
buf.setLength(buf.length()-1); // remove trailing '+' |
|
993 |
} |
|
994 |
return buf.toString(); |
|
995 |
} |
|
996 |
||
997 |
/** |
|
998 |
* Returns a parameter string identifying this event. |
|
999 |
* This method is useful for event-logging and for debugging. |
|
1000 |
* |
|
1001 |
* @return a string identifying the event and its attributes |
|
1002 |
*/ |
|
1003 |
public String paramString() { |
|
1004 |
StringBuilder str = new StringBuilder(80); |
|
1005 |
||
1006 |
switch(id) { |
|
1007 |
case MOUSE_PRESSED: |
|
1008 |
str.append("MOUSE_PRESSED"); |
|
1009 |
break; |
|
1010 |
case MOUSE_RELEASED: |
|
1011 |
str.append("MOUSE_RELEASED"); |
|
1012 |
break; |
|
1013 |
case MOUSE_CLICKED: |
|
1014 |
str.append("MOUSE_CLICKED"); |
|
1015 |
break; |
|
1016 |
case MOUSE_ENTERED: |
|
1017 |
str.append("MOUSE_ENTERED"); |
|
1018 |
break; |
|
1019 |
case MOUSE_EXITED: |
|
1020 |
str.append("MOUSE_EXITED"); |
|
1021 |
break; |
|
1022 |
case MOUSE_MOVED: |
|
1023 |
str.append("MOUSE_MOVED"); |
|
1024 |
break; |
|
1025 |
case MOUSE_DRAGGED: |
|
1026 |
str.append("MOUSE_DRAGGED"); |
|
1027 |
break; |
|
1028 |
case MOUSE_WHEEL: |
|
1029 |
str.append("MOUSE_WHEEL"); |
|
1030 |
break; |
|
1031 |
default: |
|
1032 |
str.append("unknown type"); |
|
1033 |
} |
|
1034 |
||
1035 |
// (x,y) coordinates |
|
1036 |
str.append(",(").append(x).append(",").append(y).append(")"); |
|
1037 |
str.append(",absolute(").append(xAbs).append(",").append(yAbs).append(")"); |
|
1038 |
||
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
1039 |
if (id != MOUSE_DRAGGED && id != MOUSE_MOVED){ |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
1040 |
str.append(",button=").append(getButton()); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
1041 |
} |
2 | 1042 |
|
1043 |
if (getModifiers() != 0) { |
|
1044 |
str.append(",modifiers=").append(getMouseModifiersText(modifiers)); |
|
1045 |
} |
|
1046 |
||
1047 |
if (getModifiersEx() != 0) { |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
1048 |
//Using plain "modifiers" here does show an excluded extended buttons in the string event representation. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
1049 |
//getModifiersEx() solves the problem. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
1050 |
str.append(",extModifiers=").append(getModifiersExText(getModifiersEx())); |
2 | 1051 |
} |
1052 |
||
1053 |
str.append(",clickCount=").append(clickCount); |
|
1054 |
||
1055 |
return str.toString(); |
|
1056 |
} |
|
1057 |
||
1058 |
/** |
|
1059 |
* Sets new modifiers by the old ones. |
|
1060 |
* Also sets button. |
|
1061 |
*/ |
|
1062 |
private void setNewModifiers() { |
|
1063 |
if ((modifiers & BUTTON1_MASK) != 0) { |
|
1064 |
modifiers |= BUTTON1_DOWN_MASK; |
|
1065 |
} |
|
1066 |
if ((modifiers & BUTTON2_MASK) != 0) { |
|
1067 |
modifiers |= BUTTON2_DOWN_MASK; |
|
1068 |
} |
|
1069 |
if ((modifiers & BUTTON3_MASK) != 0) { |
|
1070 |
modifiers |= BUTTON3_DOWN_MASK; |
|
1071 |
} |
|
1072 |
if (id == MOUSE_PRESSED |
|
1073 |
|| id == MOUSE_RELEASED |
|
1074 |
|| id == MOUSE_CLICKED) |
|
1075 |
{ |
|
1076 |
if ((modifiers & BUTTON1_MASK) != 0) { |
|
1077 |
button = BUTTON1; |
|
1078 |
modifiers &= ~BUTTON2_MASK & ~BUTTON3_MASK; |
|
1079 |
if (id != MOUSE_PRESSED) { |
|
1080 |
modifiers &= ~BUTTON1_DOWN_MASK; |
|
1081 |
} |
|
1082 |
} else if ((modifiers & BUTTON2_MASK) != 0) { |
|
1083 |
button = BUTTON2; |
|
1084 |
modifiers &= ~BUTTON1_MASK & ~BUTTON3_MASK; |
|
1085 |
if (id != MOUSE_PRESSED) { |
|
1086 |
modifiers &= ~BUTTON2_DOWN_MASK; |
|
1087 |
} |
|
1088 |
} else if ((modifiers & BUTTON3_MASK) != 0) { |
|
1089 |
button = BUTTON3; |
|
1090 |
modifiers &= ~BUTTON1_MASK & ~BUTTON2_MASK; |
|
1091 |
if (id != MOUSE_PRESSED) { |
|
1092 |
modifiers &= ~BUTTON3_DOWN_MASK; |
|
1093 |
} |
|
1094 |
} |
|
1095 |
} |
|
1096 |
if ((modifiers & InputEvent.ALT_MASK) != 0) { |
|
1097 |
modifiers |= InputEvent.ALT_DOWN_MASK; |
|
1098 |
} |
|
1099 |
if ((modifiers & InputEvent.META_MASK) != 0) { |
|
1100 |
modifiers |= InputEvent.META_DOWN_MASK; |
|
1101 |
} |
|
1102 |
if ((modifiers & InputEvent.SHIFT_MASK) != 0) { |
|
1103 |
modifiers |= InputEvent.SHIFT_DOWN_MASK; |
|
1104 |
} |
|
1105 |
if ((modifiers & InputEvent.CTRL_MASK) != 0) { |
|
1106 |
modifiers |= InputEvent.CTRL_DOWN_MASK; |
|
1107 |
} |
|
1108 |
if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) { |
|
1109 |
modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK; |
|
1110 |
} |
|
1111 |
} |
|
1112 |
||
1113 |
/** |
|
1114 |
* Sets old modifiers by the new ones. |
|
1115 |
*/ |
|
1116 |
private void setOldModifiers() { |
|
1117 |
if (id == MOUSE_PRESSED |
|
1118 |
|| id == MOUSE_RELEASED |
|
1119 |
|| id == MOUSE_CLICKED) |
|
1120 |
{ |
|
1121 |
switch(button) { |
|
1122 |
case BUTTON1: |
|
1123 |
modifiers |= BUTTON1_MASK; |
|
1124 |
break; |
|
1125 |
case BUTTON2: |
|
1126 |
modifiers |= BUTTON2_MASK; |
|
1127 |
break; |
|
1128 |
case BUTTON3: |
|
1129 |
modifiers |= BUTTON3_MASK; |
|
1130 |
break; |
|
1131 |
} |
|
1132 |
} else { |
|
1133 |
if ((modifiers & BUTTON1_DOWN_MASK) != 0) { |
|
1134 |
modifiers |= BUTTON1_MASK; |
|
1135 |
} |
|
1136 |
if ((modifiers & BUTTON2_DOWN_MASK) != 0) { |
|
1137 |
modifiers |= BUTTON2_MASK; |
|
1138 |
} |
|
1139 |
if ((modifiers & BUTTON3_DOWN_MASK) != 0) { |
|
1140 |
modifiers |= BUTTON3_MASK; |
|
1141 |
} |
|
1142 |
} |
|
1143 |
if ((modifiers & ALT_DOWN_MASK) != 0) { |
|
1144 |
modifiers |= ALT_MASK; |
|
1145 |
} |
|
1146 |
if ((modifiers & META_DOWN_MASK) != 0) { |
|
1147 |
modifiers |= META_MASK; |
|
1148 |
} |
|
1149 |
if ((modifiers & SHIFT_DOWN_MASK) != 0) { |
|
1150 |
modifiers |= SHIFT_MASK; |
|
1151 |
} |
|
1152 |
if ((modifiers & CTRL_DOWN_MASK) != 0) { |
|
1153 |
modifiers |= CTRL_MASK; |
|
1154 |
} |
|
1155 |
if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) { |
|
1156 |
modifiers |= ALT_GRAPH_MASK; |
|
1157 |
} |
|
1158 |
} |
|
1159 |
||
1160 |
/** |
|
1161 |
* Sets new modifiers by the old ones. |
|
1162 |
* @serial |
|
1163 |
*/ |
|
1164 |
private void readObject(ObjectInputStream s) |
|
1165 |
throws IOException, ClassNotFoundException { |
|
1166 |
s.defaultReadObject(); |
|
1167 |
if (getModifiers() != 0 && getModifiersEx() == 0) { |
|
1168 |
setNewModifiers(); |
|
1169 |
} |
|
1170 |
} |
|
1171 |
} |