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