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