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