1. /*
  2. * @(#)MouseEvent.java 1.45 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.event;
  8. import java.awt.Component;
  9. import java.awt.Event;
  10. import java.awt.GraphicsEnvironment;
  11. import java.awt.Point;
  12. import java.awt.Toolkit;
  13. import java.io.IOException;
  14. import java.io.ObjectInputStream;
  15. /**
  16. * An event which indicates that a mouse action occurred in a component.
  17. * A mouse action is considered to occur in a particular component if and only
  18. * if the mouse cursor is over the unobscured part of the component's bounds
  19. * when the action happens.
  20. * Component bounds can be obscurred by the visible component's children or by a
  21. * menu or by a top-level window.
  22. * This event is used both for mouse events (click, enter, exit) and mouse
  23. * motion events (moves and drags).
  24. * <P>
  25. * This low-level event is generated by a component object for:
  26. * <ul>
  27. * <li>Mouse Events
  28. * <ul>
  29. * <li>a mouse button is pressed
  30. * <li>a mouse button is released
  31. * <li>a mouse button is clicked (pressed and released)
  32. * <li>the mouse cursor enters the unobscured part of component's geometry
  33. * <li>the mouse cursor exits the unobscured part of component's geometry
  34. * </ul>
  35. * <li> Mouse Motion Events
  36. * <ul>
  37. * <li>the mouse is moved
  38. * <li>the mouse is dragged
  39. * </ul>
  40. * </ul>
  41. * <P>
  42. * A <code>MouseEvent</code> object is passed to every
  43. * <code>MouseListener</code>
  44. * or <code>MouseAdapter</code> object which is registered to receive
  45. * the "interesting" mouse events using the component's
  46. * <code>addMouseListener</code> method.
  47. * (<code>MouseAdapter</code> objects implement the
  48. * <code>MouseListener</code> interface.) Each such listener object
  49. * gets a <code>MouseEvent</code> containing the mouse event.
  50. * <P>
  51. * A <code>MouseEvent</code> object is also passed to every
  52. * <code>MouseMotionListener</code> or
  53. * <code>MouseMotionAdapter</code> object which is registered to receive
  54. * mouse motion events using the component's
  55. * <code>addMouseMotionListener</code>
  56. * method. (<code>MouseMotionAdapter</code> objects implement the
  57. * <code>MouseMotionListener</code> interface.) Each such listener object
  58. * gets a <code>MouseEvent</code> containing the mouse motion event.
  59. * <P>
  60. * When a mouse button is clicked, events are generated and sent to the
  61. * registered <code>MouseListener</code>s.
  62. * The state of modal keys can be retrieved using {@link InputEvent#getModifiers}
  63. * and {@link InputEvent#getModifiersEx}.
  64. * The button mask returned by {@link InputEvent#getModifiers} reflects
  65. * only the button that changed state, not the current state of all buttons.
  66. * (Note: Due to overlap in the values of ALT_MASK/BUTTON2_MASK and
  67. * META_MASK/BUTTON3_MASK, this is not always true for mouse events involving
  68. * modifier keys).
  69. * To get the state of all buttons and modifier keys, use
  70. * {@link InputEvent#getModifiersEx}.
  71. * The button which has changed state is returned by {@link MouseEvent#getButton}
  72. * <P>
  73. * For example, if the first mouse button is pressed, events are sent in the
  74. * following order:
  75. * <PRE>
  76. * <b >id </b > <b >modifiers </b > <b >button </b >
  77. * <code>MOUSE_PRESSED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code>
  78. * <code>MOUSE_RELEASED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code>
  79. * <code>MOUSE_CLICKED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code>
  80. * </PRE>
  81. * When multiple mouse buttons are pressed, each press, release, and click
  82. * results in a separate event.
  83. * <P>
  84. * For example, if the user presses <b>button 1</b> followed by
  85. * <b>button 2</b>, and then releases them in the same order,
  86. * the following sequence of events is generated:
  87. * <PRE>
  88. * <b >id </b > <b >modifiers </b > <b >button </b >
  89. * <code>MOUSE_PRESSED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code>
  90. * <code>MOUSE_PRESSED</code>: <code>BUTTON2_MASK</code> <code>BUTTON2</code>
  91. * <code>MOUSE_RELEASED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code>
  92. * <code>MOUSE_CLICKED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code>
  93. * <code>MOUSE_RELEASED</code>: <code>BUTTON2_MASK</code> <code>BUTTON2</code>
  94. * <code>MOUSE_CLICKED</code>: <code>BUTTON2_MASK</code> <code>BUTTON2</code>
  95. * </PRE>
  96. * If <b>button 2</b> is released first, the
  97. * <code>MOUSE_RELEASED</code>/<code>MOUSE_CLICKED</code> pair
  98. * for <code>BUTTON2_MASK</code> arrives first,
  99. * followed by the pair for <code>BUTTON1_MASK</code>.
  100. * <p>
  101. *
  102. * <code>MOUSE_DRAGGED</code> events are delivered to the <code>Component</code>
  103. * in which the mouse button was pressed until the mouse button is released
  104. * (regardless of whether the mouse position is within the bounds of the
  105. * <code>Component</code>). Due to platform-dependent Drag&Drop implementations,
  106. * <code>MOUSE_DRAGGED</code> events may not be delivered during a native
  107. * Drag&Drop operation.
  108. *
  109. * In a multi-screen environment mouse drag events are delivered to the
  110. * <code>Component</code> even if the mouse position is outside the bounds of the
  111. * <code>GraphicsConfiguration</code> associated with that
  112. * <code>Component</code>. However, the reported position for mouse drag events
  113. * in this case may differ from the actual mouse position:
  114. * <ul>
  115. * <li>In a multi-screen environment without a virtual device:
  116. * <br>
  117. * The reported coordinates for mouse drag events are clipped to fit within the
  118. * bounds of the <code>GraphicsConfiguration</code> associated with
  119. * the <code>Component</code>.
  120. * <li>In a multi-screen environment with a virtual device:
  121. * <br>
  122. * The reported coordinates for mouse drag events are clipped to fit within the
  123. * bounds of the virtual device associated with the <code>Component</code>.
  124. * </ul>
  125. *
  126. * @author Carl Quinn
  127. * 1.45, 01/23/03
  128. *
  129. * @see MouseAdapter
  130. * @see MouseListener
  131. * @see MouseMotionAdapter
  132. * @see MouseMotionListener
  133. * @see MouseWheelListener
  134. * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/mouselistener.html">Tutorial: Writing a Mouse Listener</a>
  135. * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/mousemotionlistener.html">Tutorial: Writing a Mouse Motion Listener</a>
  136. * @see <a href="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
  137. *
  138. * @since 1.1
  139. */
  140. public class MouseEvent extends InputEvent {
  141. /**
  142. * The first number in the range of ids used for mouse events.
  143. */
  144. public static final int MOUSE_FIRST = 500;
  145. /**
  146. * The last number in the range of ids used for mouse events.
  147. */
  148. public static final int MOUSE_LAST = 507;
  149. /**
  150. * The "mouse clicked" event. This <code>MouseEvent</code>
  151. * occurs when a mouse button is pressed and released.
  152. */
  153. public static final int MOUSE_CLICKED = MOUSE_FIRST;
  154. /**
  155. * The "mouse pressed" event. This <code>MouseEvent</code>
  156. * occurs when a mouse button is pushed down.
  157. */
  158. public static final int MOUSE_PRESSED = 1 + MOUSE_FIRST; //Event.MOUSE_DOWN
  159. /**
  160. * The "mouse released" event. This <code>MouseEvent</code>
  161. * occurs when a mouse button is let up.
  162. */
  163. public static final int MOUSE_RELEASED = 2 + MOUSE_FIRST; //Event.MOUSE_UP
  164. /**
  165. * The "mouse moved" event. This <code>MouseEvent</code>
  166. * occurs when the mouse position changes.
  167. */
  168. public static final int MOUSE_MOVED = 3 + MOUSE_FIRST; //Event.MOUSE_MOVE
  169. /**
  170. * The "mouse entered" event. This <code>MouseEvent</code>
  171. * occurs when the mouse cursor enters the unobscured part of component's
  172. * geometry.
  173. */
  174. public static final int MOUSE_ENTERED = 4 + MOUSE_FIRST; //Event.MOUSE_ENTER
  175. /**
  176. * The "mouse exited" event. This <code>MouseEvent</code>
  177. * occurs when the mouse cursor exits the unobscured part of component's
  178. * geometry.
  179. */
  180. public static final int MOUSE_EXITED = 5 + MOUSE_FIRST; //Event.MOUSE_EXIT
  181. /**
  182. * The "mouse dragged" event. This <code>MouseEvent</code>
  183. * occurs when the mouse position changes while a mouse button is pressed.
  184. */
  185. public static final int MOUSE_DRAGGED = 6 + MOUSE_FIRST; //Event.MOUSE_DRAG
  186. /**
  187. * The "mouse wheel" event. This is the only <code>MouseWheelEvent</code>.
  188. * It occurs when a mouse equipped with a wheel has its wheel rotated.
  189. * @since 1.4
  190. */
  191. public static final int MOUSE_WHEEL = 7 + MOUSE_FIRST;
  192. /**
  193. * Indicates no mouse buttons; used by {@link #getButton}.
  194. * @since 1.4
  195. */
  196. public static final int NOBUTTON = 0;
  197. /**
  198. * Indicates mouse button #1; used by {@link #getButton}.
  199. * @since 1.4
  200. */
  201. public static final int BUTTON1 = 1;
  202. /**
  203. * Indicates mouse button #2; used by {@link #getButton}.
  204. * @since 1.4
  205. */
  206. public static final int BUTTON2 = 2;
  207. /**
  208. * Indicates mouse button #3; used by {@link #getButton}.
  209. * @since 1.4
  210. */
  211. public static final int BUTTON3 = 3;
  212. /**
  213. * The mouse event's x coordinate.
  214. * The x value is relative to the component that fired the event.
  215. *
  216. * @serial
  217. * @see #getX()
  218. */
  219. int x;
  220. /**
  221. * The mouse event's y coordinate.
  222. * The y value is relative to the component that fired the event.
  223. *
  224. * @serial
  225. * @see #getY()
  226. */
  227. int y;
  228. /**
  229. * Indicates the number of quick consecutive clicks of
  230. * a mouse button.
  231. * clickCount will be valid for only three mouse events :<BR>
  232. * <code>MOUSE_CLICKED</code>,
  233. * <code>MOUSE_PRESSED</code> and
  234. * <code>MOUSE_RELEASED</code>.
  235. * For the above, the <code>clickCount</code> will be at least 1.
  236. * For all other events the count will be 0.
  237. *
  238. * @serial
  239. * @see #getClickCount().
  240. */
  241. int clickCount;
  242. /**
  243. * Indicates which, if any, of the mouse buttons has changed state.
  244. *
  245. * The only legal values are the following constants:
  246. * <code>NOBUTTON</code>,
  247. * <code>BUTTON1</code>,
  248. * <code>BUTTON2</code> or
  249. * <code>BUTTON3</code>.
  250. * @serial
  251. * @see #getButton().
  252. */
  253. int button;
  254. /**
  255. * A property used to indicate whether a Popup Menu
  256. * should appear with a certain gestures.
  257. * If <code>popupTrigger</code> = <code>false</code>,
  258. * no popup menu should appear. If it is <code>true</code>
  259. * then a popup menu should appear.
  260. *
  261. * @serial
  262. * @see java.awt.PopupMenu
  263. * @see #isPopupTrigger()
  264. */
  265. boolean popupTrigger = false;
  266. /*
  267. * JDK 1.1 serialVersionUID
  268. */
  269. private static final long serialVersionUID = -991214153494842848L;
  270. static {
  271. /* ensure that the necessary native libraries are loaded */
  272. NativeLibLoader.loadLibraries();
  273. if (!GraphicsEnvironment.isHeadless()) {
  274. initIDs();
  275. }
  276. }
  277. /**
  278. * Initialize JNI field and method IDs for fields that may be
  279. accessed from C.
  280. */
  281. private static native void initIDs();
  282. /**
  283. * Constructs a <code>MouseEvent</code> object with the
  284. * specified source component,
  285. * type, modifiers, coordinates, and click count.
  286. * <p>Note that passing in an invalid <code>id</code> results in
  287. * unspecified behavior.
  288. *
  289. * @param source the <code>Component</code> that originated the event
  290. * @param id the integer that identifies the event
  291. * @param when a long int that gives the time the event occurred
  292. * @param modifiers the modifier keys down during event (e.g. shift, ctrl,
  293. * alt, meta)
  294. * Either extended _DOWN_MASK or old _MASK modifiers
  295. * should be used, but both models should not be mixed
  296. * in one event. Use of the extended modifiers is
  297. * preferred.
  298. * @param x the horizontal x coordinate for the mouse location
  299. * @param y the vertical y coordinate for the mouse location
  300. * @param clickCount the number of mouse clicks associated with event
  301. * @param popupTrigger a boolean, true if this event is a trigger for a
  302. * popup menu
  303. * @param button which of the mouse buttons has changed state.
  304. * <code>NOBUTTON</code>,
  305. * <code>BUTTON1</code>,
  306. * <code>BUTTON2</code> or
  307. * <code>BUTTON3</code>.
  308. * @exception IllegalArgumentException if if an invalid <code>button</code>
  309. * value is passed in.
  310. * @since 1.4
  311. */
  312. public MouseEvent(Component source, int id, long when, int modifiers,
  313. int x, int y, int clickCount, boolean popupTrigger,
  314. int button)
  315. {
  316. super(source, id, when, modifiers);
  317. this.x = x;
  318. this.y = y;
  319. this.clickCount = clickCount;
  320. this.popupTrigger = popupTrigger;
  321. if (button < NOBUTTON || button >BUTTON3) {
  322. throw new IllegalArgumentException("Invalid button value");
  323. }
  324. this.button = button;
  325. if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
  326. setNewModifiers();
  327. } else if ((getModifiers() == 0) &&
  328. (getModifiersEx() != 0 ||
  329. button != NOBUTTON))
  330. {
  331. setOldModifiers();
  332. }
  333. }
  334. /**
  335. * Constructs a <code>MouseEvent</code> object with the
  336. * specified source component,
  337. * type, modifiers, coordinates, and click count.
  338. * <p>Note that passing in an invalid <code>id</code> results in
  339. * unspecified behavior.
  340. *
  341. * @param source the <code>Component</code> that originated the event
  342. * @param id the integer that identifies the event
  343. * @param when a long int that gives the time the event occurred
  344. * @param modifiers the modifier keys down during event (e.g. shift, ctrl,
  345. * alt, meta)
  346. * Either extended _DOWN_MASK or old _MASK modifiers
  347. * should be used, but both models should not be mixed
  348. * in one event. Use of the extended modifiers is
  349. * preferred.
  350. * @param x the horizontal x coordinate for the mouse location
  351. * @param y the vertical y coordinate for the mouse location
  352. * @param clickCount the number of mouse clicks associated with event
  353. * @param popupTrigger a boolean, true if this event is a trigger for a
  354. * popup menu
  355. */
  356. public MouseEvent(Component source, int id, long when, int modifiers,
  357. int x, int y, int clickCount, boolean popupTrigger) {
  358. this(source, id, when, modifiers, x, y, clickCount, popupTrigger, NOBUTTON);
  359. }
  360. /**
  361. * Returns the horizontal x position of the event relative to the
  362. * source component.
  363. *
  364. * @return x an integer indicating horizontal position relative to
  365. * the component
  366. */
  367. public int getX() {
  368. return x;
  369. }
  370. /**
  371. * Returns the vertical y position of the event relative to the
  372. * source component.
  373. *
  374. * @return y an integer indicating vertical position relative to
  375. * the component
  376. */
  377. public int getY() {
  378. return y;
  379. }
  380. /**
  381. * Returns the x,y position of the event relative to the source component.
  382. *
  383. * @return a <code>Point</code> object containing the x and y coordinates
  384. * relative to the source component
  385. *
  386. */
  387. public Point getPoint() {
  388. int x;
  389. int y;
  390. synchronized (this) {
  391. x = this.x;
  392. y = this.y;
  393. }
  394. return new Point(x, y);
  395. }
  396. /**
  397. * Translates the event's coordinates to a new position
  398. * by adding specified <code>x</code> (horizontal) and <code>y</code>
  399. * (vertical) offsets.
  400. *
  401. * @param x the horizontal x value to add to the current x
  402. * coordinate position
  403. * @param y the vertical y value to add to the current y
  404. coordinate position
  405. */
  406. public synchronized void translatePoint(int x, int y) {
  407. this.x += x;
  408. this.y += y;
  409. }
  410. /**
  411. * Returns the number of mouse clicks associated with this event.
  412. *
  413. * @return integer value for the number of clicks
  414. */
  415. public int getClickCount() {
  416. return clickCount;
  417. }
  418. /**
  419. * Returns which, if any, of the mouse buttons has changed state.
  420. *
  421. * @return one of the following constants:
  422. * <code>NOBUTTON</code>,
  423. * <code>BUTTON1</code>,
  424. * <code>BUTTON2</code> or
  425. * <code>BUTTON3</code>.
  426. * @since 1.4
  427. */
  428. public int getButton() {
  429. return button;
  430. }
  431. /**
  432. * Returns whether or not this mouse event is the popup menu
  433. * trigger event for the platform.
  434. * <p><b>Note</b>: Popup menus are triggered differently
  435. * on different systems. Therefore, <code>isPopupTrigger</code>
  436. * should be checked in both <code>mousePressed</code>
  437. * and <code>mouseReleased</code>
  438. * for proper cross-platform functionality.
  439. *
  440. * @return boolean, true if this event is the popup menu trigger
  441. * for this platform
  442. */
  443. public boolean isPopupTrigger() {
  444. return popupTrigger;
  445. }
  446. /**
  447. * Returns a String describing the modifier keys and mouse buttons
  448. * that were down during the event, such as "Shift", or "Ctrl+Shift".
  449. * These strings can be localized by changing the awt.properties file.
  450. *
  451. * @param modifiers a modifier mask describing the modifier keys and
  452. * mouse buttons that were down during the event
  453. * @return string a text description of the combination of modifier
  454. * keys and mouse buttons that were down during the event
  455. * @since 1.4
  456. */
  457. public static String getMouseModifiersText(int modifiers) {
  458. StringBuffer buf = new StringBuffer();
  459. if ((modifiers & InputEvent.ALT_MASK) != 0) {
  460. buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
  461. buf.append("+");
  462. }
  463. if ((modifiers & InputEvent.META_MASK) != 0) {
  464. buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
  465. buf.append("+");
  466. }
  467. if ((modifiers & InputEvent.CTRL_MASK) != 0) {
  468. buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
  469. buf.append("+");
  470. }
  471. if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
  472. buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
  473. buf.append("+");
  474. }
  475. if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
  476. buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));
  477. buf.append("+");
  478. }
  479. if ((modifiers & InputEvent.BUTTON1_MASK) != 0) {
  480. buf.append(Toolkit.getProperty("AWT.button1", "Button1"));
  481. buf.append("+");
  482. }
  483. if ((modifiers & InputEvent.BUTTON2_MASK) != 0) {
  484. buf.append(Toolkit.getProperty("AWT.button2", "Button2"));
  485. buf.append("+");
  486. }
  487. if ((modifiers & InputEvent.BUTTON3_MASK) != 0) {
  488. buf.append(Toolkit.getProperty("AWT.button3", "Button3"));
  489. buf.append("+");
  490. }
  491. if (buf.length() > 0) {
  492. buf.setLength(buf.length()-1); // remove trailing '+'
  493. }
  494. return buf.toString();
  495. }
  496. /**
  497. * Returns a parameter string identifying this event.
  498. * This method is useful for event-logging and for debugging.
  499. *
  500. * @return a string identifying the event and its attributes
  501. */
  502. public String paramString() {
  503. StringBuffer str = new StringBuffer(80);
  504. switch(id) {
  505. case MOUSE_PRESSED:
  506. str.append("MOUSE_PRESSED");
  507. break;
  508. case MOUSE_RELEASED:
  509. str.append("MOUSE_RELEASED");
  510. break;
  511. case MOUSE_CLICKED:
  512. str.append("MOUSE_CLICKED");
  513. break;
  514. case MOUSE_ENTERED:
  515. str.append("MOUSE_ENTERED");
  516. break;
  517. case MOUSE_EXITED:
  518. str.append("MOUSE_EXITED");
  519. break;
  520. case MOUSE_MOVED:
  521. str.append("MOUSE_MOVED");
  522. break;
  523. case MOUSE_DRAGGED:
  524. str.append("MOUSE_DRAGGED");
  525. break;
  526. case MOUSE_WHEEL:
  527. str.append("MOUSE_WHEEL");
  528. break;
  529. default:
  530. str.append("unknown type");
  531. }
  532. // (x,y) coordinates
  533. str.append(",(").append(x).append(",").append(y).append(")");
  534. str.append(",button=").append(getButton());
  535. if (getModifiers() != 0) {
  536. str.append(",modifiers=").append(getMouseModifiersText(modifiers));
  537. }
  538. if (getModifiersEx() != 0) {
  539. str.append(",extModifiers=").append(getModifiersExText(modifiers));
  540. }
  541. str.append(",clickCount=").append(clickCount);
  542. return str.toString();
  543. }
  544. /**
  545. * Sets new modifiers by the old ones.
  546. * Also sets button.
  547. */
  548. private void setNewModifiers() {
  549. if ((modifiers & BUTTON1_MASK) != 0) {
  550. modifiers |= BUTTON1_DOWN_MASK;
  551. }
  552. if ((modifiers & BUTTON2_MASK) != 0) {
  553. modifiers |= BUTTON2_DOWN_MASK;
  554. }
  555. if ((modifiers & BUTTON3_MASK) != 0) {
  556. modifiers |= BUTTON3_DOWN_MASK;
  557. }
  558. if (id == MOUSE_PRESSED
  559. || id == MOUSE_RELEASED
  560. || id == MOUSE_CLICKED)
  561. {
  562. if ((modifiers & BUTTON1_MASK) != 0) {
  563. button = BUTTON1;
  564. modifiers &= ~BUTTON2_MASK & ~BUTTON3_MASK;
  565. if (id != MOUSE_PRESSED) {
  566. modifiers &= ~BUTTON1_DOWN_MASK;
  567. }
  568. } else if ((modifiers & BUTTON2_MASK) != 0) {
  569. button = BUTTON2;
  570. modifiers &= ~BUTTON1_MASK & ~BUTTON3_MASK;
  571. if (id != MOUSE_PRESSED) {
  572. modifiers &= ~BUTTON2_DOWN_MASK;
  573. }
  574. } else if ((modifiers & BUTTON3_MASK) != 0) {
  575. button = BUTTON3;
  576. modifiers &= ~BUTTON1_MASK & ~BUTTON2_MASK;
  577. if (id != MOUSE_PRESSED) {
  578. modifiers &= ~BUTTON3_DOWN_MASK;
  579. }
  580. }
  581. }
  582. if ((modifiers & InputEvent.ALT_MASK) != 0) {
  583. modifiers |= InputEvent.ALT_DOWN_MASK;
  584. }
  585. if ((modifiers & InputEvent.META_MASK) != 0) {
  586. modifiers |= InputEvent.META_DOWN_MASK;
  587. }
  588. if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
  589. modifiers |= InputEvent.SHIFT_DOWN_MASK;
  590. }
  591. if ((modifiers & InputEvent.CTRL_MASK) != 0) {
  592. modifiers |= InputEvent.CTRL_DOWN_MASK;
  593. }
  594. if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
  595. modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK;
  596. }
  597. }
  598. /**
  599. * Sets old modifiers by the new ones.
  600. */
  601. private void setOldModifiers() {
  602. if (id == MOUSE_PRESSED
  603. || id == MOUSE_RELEASED
  604. || id == MOUSE_CLICKED)
  605. {
  606. switch(button) {
  607. case BUTTON1:
  608. modifiers |= BUTTON1_MASK;
  609. break;
  610. case BUTTON2:
  611. modifiers |= BUTTON2_MASK;
  612. break;
  613. case BUTTON3:
  614. modifiers |= BUTTON3_MASK;
  615. break;
  616. }
  617. } else {
  618. if ((modifiers & BUTTON1_DOWN_MASK) != 0) {
  619. modifiers |= BUTTON1_MASK;
  620. }
  621. if ((modifiers & BUTTON2_DOWN_MASK) != 0) {
  622. modifiers |= BUTTON2_MASK;
  623. }
  624. if ((modifiers & BUTTON3_DOWN_MASK) != 0) {
  625. modifiers |= BUTTON3_MASK;
  626. }
  627. }
  628. if ((modifiers & ALT_DOWN_MASK) != 0) {
  629. modifiers |= ALT_MASK;
  630. }
  631. if ((modifiers & META_DOWN_MASK) != 0) {
  632. modifiers |= META_MASK;
  633. }
  634. if ((modifiers & SHIFT_DOWN_MASK) != 0) {
  635. modifiers |= SHIFT_MASK;
  636. }
  637. if ((modifiers & CTRL_DOWN_MASK) != 0) {
  638. modifiers |= CTRL_MASK;
  639. }
  640. if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) {
  641. modifiers |= ALT_GRAPH_MASK;
  642. }
  643. }
  644. /**
  645. * Sets new modifiers by the old ones.
  646. * @serial
  647. */
  648. private void readObject(ObjectInputStream s)
  649. throws IOException, ClassNotFoundException {
  650. s.defaultReadObject();
  651. if (getModifiers() != 0 && getModifiersEx() == 0) {
  652. setNewModifiers();
  653. }
  654. }
  655. }