KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > event > MouseEvent


1 /*
2  * @(#)MouseEvent.java 1.49 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.event;
9
10 import java.awt.Component JavaDoc;
11 import java.awt.Event JavaDoc;
12 import java.awt.GraphicsEnvironment JavaDoc;
13 import java.awt.Point JavaDoc;
14 import java.awt.Toolkit JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.ObjectInputStream JavaDoc;
17
18 /**
19  * An event which indicates that a mouse action occurred in a component.
20  * A mouse action is considered to occur in a particular component if and only
21  * if the mouse cursor is over the unobscured part of the component's bounds
22  * when the action happens.
23  * Component bounds can be obscurred by the visible component's children or by a
24  * menu or by a top-level window.
25  * This event is used both for mouse events (click, enter, exit) and mouse
26  * motion events (moves and drags).
27  * <P>
28  * This low-level event is generated by a component object for:
29  * <ul>
30  * <li>Mouse Events
31  * <ul>
32  * <li>a mouse button is pressed
33  * <li>a mouse button is released
34  * <li>a mouse button is clicked (pressed and released)
35  * <li>the mouse cursor enters the unobscured part of component's geometry
36  * <li>the mouse cursor exits the unobscured part of component's geometry
37  * </ul>
38  * <li> Mouse Motion Events
39  * <ul>
40  * <li>the mouse is moved
41  * <li>the mouse is dragged
42  * </ul>
43  * </ul>
44  * <P>
45  * A <code>MouseEvent</code> object is passed to every
46  * <code>MouseListener</code>
47  * or <code>MouseAdapter</code> object which is registered to receive
48  * the "interesting" mouse events using the component's
49  * <code>addMouseListener</code> method.
50  * (<code>MouseAdapter</code> objects implement the
51  * <code>MouseListener</code> interface.) Each such listener object
52  * gets a <code>MouseEvent</code> containing the mouse event.
53  * <P>
54  * A <code>MouseEvent</code> object is also passed to every
55  * <code>MouseMotionListener</code> or
56  * <code>MouseMotionAdapter</code> object which is registered to receive
57  * mouse motion events using the component's
58  * <code>addMouseMotionListener</code>
59  * method. (<code>MouseMotionAdapter</code> objects implement the
60  * <code>MouseMotionListener</code> interface.) Each such listener object
61  * gets a <code>MouseEvent</code> containing the mouse motion event.
62  * <P>
63  * When a mouse button is clicked, events are generated and sent to the
64  * registered <code>MouseListener</code>s.
65  * The state of modal keys can be retrieved using {@link InputEvent#getModifiers}
66  * and {@link InputEvent#getModifiersEx}.
67  * The button mask returned by {@link InputEvent#getModifiers} reflects
68  * only the button that changed state, not the current state of all buttons.
69  * (Note: Due to overlap in the values of ALT_MASK/BUTTON2_MASK and
70  * META_MASK/BUTTON3_MASK, this is not always true for mouse events involving
71  * modifier keys).
72  * To get the state of all buttons and modifier keys, use
73  * {@link InputEvent#getModifiersEx}.
74  * The button which has changed state is returned by {@link MouseEvent#getButton}
75  * <P>
76  * For example, if the first mouse button is pressed, events are sent in the
77  * following order:
78  * <PRE>
79  * <b >id </b > <b >modifiers </b > <b >button </b >
80  * <code>MOUSE_PRESSED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code>
81  * <code>MOUSE_RELEASED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code>
82  * <code>MOUSE_CLICKED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code>
83  * </PRE>
84  * When multiple mouse buttons are pressed, each press, release, and click
85  * results in a separate event.
86  * <P>
87  * For example, if the user presses <b>button 1</b> followed by
88  * <b>button 2</b>, and then releases them in the same order,
89  * the following sequence of events is generated:
90  * <PRE>
91  * <b >id </b > <b >modifiers </b > <b >button </b >
92  * <code>MOUSE_PRESSED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code>
93  * <code>MOUSE_PRESSED</code>: <code>BUTTON2_MASK</code> <code>BUTTON2</code>
94  * <code>MOUSE_RELEASED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code>
95  * <code>MOUSE_CLICKED</code>: <code>BUTTON1_MASK</code> <code>BUTTON1</code>
96  * <code>MOUSE_RELEASED</code>: <code>BUTTON2_MASK</code> <code>BUTTON2</code>
97  * <code>MOUSE_CLICKED</code>: <code>BUTTON2_MASK</code> <code>BUTTON2</code>
98  * </PRE>
99  * If <b>button 2</b> is released first, the
100  * <code>MOUSE_RELEASED</code>/<code>MOUSE_CLICKED</code> pair
101  * for <code>BUTTON2_MASK</code> arrives first,
102  * followed by the pair for <code>BUTTON1_MASK</code>.
103  * <p>
104  *
105  * <code>MOUSE_DRAGGED</code> events are delivered to the <code>Component</code>
106  * in which the mouse button was pressed until the mouse button is released
107  * (regardless of whether the mouse position is within the bounds of the
108  * <code>Component</code>). Due to platform-dependent Drag&Drop implementations,
109  * <code>MOUSE_DRAGGED</code> events may not be delivered during a native
110  * Drag&Drop operation.
111  *
112  * In a multi-screen environment mouse drag events are delivered to the
113  * <code>Component</code> even if the mouse position is outside the bounds of the
114  * <code>GraphicsConfiguration</code> associated with that
115  * <code>Component</code>. However, the reported position for mouse drag events
116  * in this case may differ from the actual mouse position:
117  * <ul>
118  * <li>In a multi-screen environment without a virtual device:
119  * <br>
120  * The reported coordinates for mouse drag events are clipped to fit within the
121  * bounds of the <code>GraphicsConfiguration</code> associated with
122  * the <code>Component</code>.
123  * <li>In a multi-screen environment with a virtual device:
124  * <br>
125  * The reported coordinates for mouse drag events are clipped to fit within the
126  * bounds of the virtual device associated with the <code>Component</code>.
127  * </ul>
128  *
129  * @author Carl Quinn
130  * 1.49, 12/19/03
131  *
132  * @see MouseAdapter
133  * @see MouseListener
134  * @see MouseMotionAdapter
135  * @see MouseMotionListener
136  * @see MouseWheelListener
137  * @see <a HREF="http://java.sun.com/docs/books/tutorial/post1.0/ui/mouselistener.html">Tutorial: Writing a Mouse Listener</a>
138  * @see <a HREF="http://java.sun.com/docs/books/tutorial/post1.0/ui/mousemotionlistener.html">Tutorial: Writing a Mouse Motion Listener</a>
139  * @see <a HREF="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
140  *
141  * @since 1.1
142  */

143 public class MouseEvent extends InputEvent JavaDoc {
144
145     /**
146      * The first number in the range of ids used for mouse events.
147      */

148     public static final int MOUSE_FIRST = 500;
149
150     /**
151      * The last number in the range of ids used for mouse events.
152      */

153     public static final int MOUSE_LAST = 507;
154
155     /**
156      * The "mouse clicked" event. This <code>MouseEvent</code>
157      * occurs when a mouse button is pressed and released.
158      */

159     public static final int MOUSE_CLICKED = MOUSE_FIRST;
160
161     /**
162      * The "mouse pressed" event. This <code>MouseEvent</code>
163      * occurs when a mouse button is pushed down.
164      */

165     public static final int MOUSE_PRESSED = 1 + MOUSE_FIRST; //Event.MOUSE_DOWN
166

167     /**
168      * The "mouse released" event. This <code>MouseEvent</code>
169      * occurs when a mouse button is let up.
170      */

171     public static final int MOUSE_RELEASED = 2 + MOUSE_FIRST; //Event.MOUSE_UP
172

173     /**
174      * The "mouse moved" event. This <code>MouseEvent</code>
175      * occurs when the mouse position changes.
176      */

177     public static final int MOUSE_MOVED = 3 + MOUSE_FIRST; //Event.MOUSE_MOVE
178

179     /**
180      * The "mouse entered" event. This <code>MouseEvent</code>
181      * occurs when the mouse cursor enters the unobscured part of component's
182      * geometry.
183      */

184     public static final int MOUSE_ENTERED = 4 + MOUSE_FIRST; //Event.MOUSE_ENTER
185

186     /**
187      * The "mouse exited" event. This <code>MouseEvent</code>
188      * occurs when the mouse cursor exits the unobscured part of component's
189      * geometry.
190      */

191     public static final int MOUSE_EXITED = 5 + MOUSE_FIRST; //Event.MOUSE_EXIT
192

193     /**
194      * The "mouse dragged" event. This <code>MouseEvent</code>
195      * occurs when the mouse position changes while a mouse button is pressed.
196      */

197     public static final int MOUSE_DRAGGED = 6 + MOUSE_FIRST; //Event.MOUSE_DRAG
198

199     /**
200      * The "mouse wheel" event. This is the only <code>MouseWheelEvent</code>.
201      * It occurs when a mouse equipped with a wheel has its wheel rotated.
202      * @since 1.4
203      */

204     public static final int MOUSE_WHEEL = 7 + MOUSE_FIRST;
205
206     /**
207      * Indicates no mouse buttons; used by {@link #getButton}.
208      * @since 1.4
209      */

210     public static final int NOBUTTON = 0;
211
212     /**
213      * Indicates mouse button #1; used by {@link #getButton}.
214      * @since 1.4
215      */

216     public static final int BUTTON1 = 1;
217
218     /**
219      * Indicates mouse button #2; used by {@link #getButton}.
220      * @since 1.4
221      */

222     public static final int BUTTON2 = 2;
223
224     /**
225      * Indicates mouse button #3; used by {@link #getButton}.
226      * @since 1.4
227      */

228     public static final int BUTTON3 = 3;
229
230     /**
231      * The mouse event's x coordinate.
232      * The x value is relative to the component that fired the event.
233      *
234      * @serial
235      * @see #getX()
236      */

237     int x;
238
239     /**
240      * The mouse event's y coordinate.
241      * The y value is relative to the component that fired the event.
242      *
243      * @serial
244      * @see #getY()
245      */

246     int y;
247
248     /**
249      * Indicates the number of quick consecutive clicks of
250      * a mouse button.
251      * clickCount will be valid for only three mouse events :<BR>
252      * <code>MOUSE_CLICKED</code>,
253      * <code>MOUSE_PRESSED</code> and
254      * <code>MOUSE_RELEASED</code>.
255      * For the above, the <code>clickCount</code> will be at least 1.
256      * For all other events the count will be 0.
257      *
258      * @serial
259      * @see #getClickCount().
260      */

261     int clickCount;
262
263     /**
264      * Indicates which, if any, of the mouse buttons has changed state.
265      *
266      * The only legal values are the following constants:
267      * <code>NOBUTTON</code>,
268      * <code>BUTTON1</code>,
269      * <code>BUTTON2</code> or
270      * <code>BUTTON3</code>.
271      * @serial
272      * @see #getButton().
273      */

274     int button;
275
276     /**
277      * A property used to indicate whether a Popup Menu
278      * should appear with a certain gestures.
279      * If <code>popupTrigger</code> = <code>false</code>,
280      * no popup menu should appear. If it is <code>true</code>
281      * then a popup menu should appear.
282      *
283      * @serial
284      * @see java.awt.PopupMenu
285      * @see #isPopupTrigger()
286      */

287     boolean popupTrigger = false;
288
289     /*
290      * JDK 1.1 serialVersionUID
291      */

292     private static final long serialVersionUID = -991214153494842848L;
293
294     static {
295         /* ensure that the necessary native libraries are loaded */
296     NativeLibLoader.loadLibraries();
297         if (!GraphicsEnvironment.isHeadless()) {
298             initIDs();
299         }
300     }
301
302     /**
303      * Initialize JNI field and method IDs for fields that may be
304        accessed from C.
305      */

306     private static native void initIDs();
307
308     /**
309      * Constructs a <code>MouseEvent</code> object with the
310      * specified source component,
311      * type, modifiers, coordinates, and click count.
312      * <p>
313      * Note that passing in an invalid <code>id</code> results in
314      * unspecified behavior. Creating an invalid event (such
315      * as by using more than one of the old _MASKs, or modifier/button
316      * values which don't match) results in unspecified behavior.
317      * This method throws an
318      * <code>IllegalArgumentException</code> if <code>source</code>
319      * is <code>null</code>.
320      *
321      * @param source the <code>Component</code> that originated the event
322      * @param id the integer that identifies the event
323      * @param when a long int that gives the time the event occurred
324      * @param modifiers the modifier keys down during event (e.g. shift, ctrl,
325      * alt, meta)
326      * Either extended _DOWN_MASK or old _MASK modifiers
327      * should be used, but both models should not be mixed
328      * in one event. Use of the extended modifiers is
329      * preferred.
330      * @param x the horizontal x coordinate for the mouse location
331      * @param y the vertical y coordinate for the mouse location
332      * @param clickCount the number of mouse clicks associated with event
333      * @param popupTrigger a boolean, true if this event is a trigger for a
334      * popup menu
335      * @param button which of the mouse buttons has changed state.
336      * <code>NOBUTTON</code>,
337      * <code>BUTTON1</code>,
338      * <code>BUTTON2</code> or
339      * <code>BUTTON3</code>.
340      * @throws IllegalArgumentException if an invalid <code>button</code>
341      * value is passed in
342      * @throws IllegalArgumentException if <code>source</code> is null
343      * @since 1.4
344      */

345     public MouseEvent(Component JavaDoc source, int id, long when, int modifiers,
346                       int x, int y, int clickCount, boolean popupTrigger,
347                       int button)
348     {
349         super(source, id, when, modifiers);
350         this.x = x;
351         this.y = y;
352         this.clickCount = clickCount;
353         this.popupTrigger = popupTrigger;
354         if (button < NOBUTTON || button >BUTTON3) {
355             throw new IllegalArgumentException JavaDoc("Invalid button value");
356         }
357         this.button = button;
358         if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
359         setNewModifiers();
360     } else if ((getModifiers() == 0) &&
361                    (getModifiersEx() != 0 ||
362                     button != NOBUTTON))
363         {
364         setOldModifiers();
365     }
366     }
367
368     /**
369      * Constructs a <code>MouseEvent</code> object with the
370      * specified source component,
371      * type, modifiers, coordinates, and click count.
372      * <p>Note that passing in an invalid <code>id</code> results in
373      * unspecified behavior. This method throws an
374      * <code>IllegalArgumentException</code> if <code>source</code>
375      * is <code>null</code>.
376      *
377      * @param source the <code>Component</code> that originated the event
378      * @param id the integer that identifies the event
379      * @param when a long int that gives the time the event occurred
380      * @param modifiers the modifier keys down during event (e.g. shift, ctrl,
381      * alt, meta)
382      * Either extended _DOWN_MASK or old _MASK modifiers
383      * should be used, but both models should not be mixed
384      * in one event. Use of the extended modifiers is
385      * preferred.
386      * @param x the horizontal x coordinate for the mouse location
387      * @param y the vertical y coordinate for the mouse location
388      * @param clickCount the number of mouse clicks associated with event
389      * @param popupTrigger a boolean, true if this event is a trigger for a
390      * popup menu
391      * @throws IllegalArgumentException if <code>source</code> is null
392      */

393     public MouseEvent(Component JavaDoc source, int id, long when, int modifiers,
394                       int x, int y, int clickCount, boolean popupTrigger) {
395         this(source, id, when, modifiers, x, y, clickCount, popupTrigger, NOBUTTON);
396     }
397
398     /**
399      * Returns the horizontal x position of the event relative to the
400      * source component.
401      *
402      * @return x an integer indicating horizontal position relative to
403      * the component
404      */

405     public int getX() {
406         return x;
407     }
408
409     /**
410      * Returns the vertical y position of the event relative to the
411      * source component.
412      *
413      * @return y an integer indicating vertical position relative to
414      * the component
415      */

416     public int getY() {
417         return y;
418     }
419
420     /**
421      * Returns the x,y position of the event relative to the source component.
422      *
423      * @return a <code>Point</code> object containing the x and y coordinates
424      * relative to the source component
425      *
426      */

427     public Point JavaDoc getPoint() {
428     int x;
429     int y;
430     synchronized (this) {
431         x = this.x;
432         y = this.y;
433     }
434         return new Point JavaDoc(x, y);
435     }
436
437     /**
438      * Translates the event's coordinates to a new position
439      * by adding specified <code>x</code> (horizontal) and <code>y</code>
440      * (vertical) offsets.
441      *
442      * @param x the horizontal x value to add to the current x
443      * coordinate position
444      * @param y the vertical y value to add to the current y
445                 coordinate position
446      */

447     public synchronized void translatePoint(int x, int y) {
448         this.x += x;
449         this.y += y;
450     }
451
452     /**
453      * Returns the number of mouse clicks associated with this event.
454      *
455      * @return integer value for the number of clicks
456      */

457     public int getClickCount() {
458         return clickCount;
459     }
460
461     /**
462      * Returns which, if any, of the mouse buttons has changed state.
463      *
464      * @return one of the following constants:
465      * <code>NOBUTTON</code>,
466      * <code>BUTTON1</code>,
467      * <code>BUTTON2</code> or
468      * <code>BUTTON3</code>.
469      * @since 1.4
470      */

471     public int getButton() {
472     return button;
473     }
474
475     /**
476      * Returns whether or not this mouse event is the popup menu
477      * trigger event for the platform.
478      * <p><b>Note</b>: Popup menus are triggered differently
479      * on different systems. Therefore, <code>isPopupTrigger</code>
480      * should be checked in both <code>mousePressed</code>
481      * and <code>mouseReleased</code>
482      * for proper cross-platform functionality.
483      *
484      * @return boolean, true if this event is the popup menu trigger
485      * for this platform
486      */

487     public boolean isPopupTrigger() {
488         return popupTrigger;
489     }
490
491     /**
492      * Returns a <code>String</code> describing the modifier keys and
493      * mouse buttons that were down during the event, such as "Shift",
494      * or "Ctrl+Shift". These strings can be localized by changing
495      * the <code>awt.properties</code> file.
496      * <p>
497      * Note that <code>InputEvent.ALT_MASK</code> and
498      * <code>InputEvent.BUTTON2_MASK</code> have the same value,
499      * so the string "Alt" is returned for both modifiers. Likewise,
500      * <code>InputEvent.META_MASK</code> and
501      * <code>InputEvent.BUTTON3_MASK</code> have the same value,
502      * so the string "Meta" is returned for both modifiers.
503      *
504      * @param modifiers a modifier mask describing the modifier keys and
505      * mouse buttons that were down during the event
506      * @return string a text description of the combination of modifier
507      * keys and mouse buttons that were down during the event
508      * @see InputEvent#getModifiersExText(int)
509      * @since 1.4
510      */

511     public static String JavaDoc getMouseModifiersText(int modifiers) {
512         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
513         if ((modifiers & InputEvent.ALT_MASK) != 0) {
514             buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
515             buf.append("+");
516         }
517         if ((modifiers & InputEvent.META_MASK) != 0) {
518             buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
519             buf.append("+");
520         }
521         if ((modifiers & InputEvent.CTRL_MASK) != 0) {
522             buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
523             buf.append("+");
524         }
525         if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
526             buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
527             buf.append("+");
528         }
529         if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
530             buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));
531             buf.append("+");
532         }
533         if ((modifiers & InputEvent.BUTTON1_MASK) != 0) {
534             buf.append(Toolkit.getProperty("AWT.button1", "Button1"));
535             buf.append("+");
536         }
537         if ((modifiers & InputEvent.BUTTON2_MASK) != 0) {
538             buf.append(Toolkit.getProperty("AWT.button2", "Button2"));
539             buf.append("+");
540         }
541         if ((modifiers & InputEvent.BUTTON3_MASK) != 0) {
542             buf.append(Toolkit.getProperty("AWT.button3", "Button3"));
543             buf.append("+");
544         }
545         if (buf.length() > 0) {
546             buf.setLength(buf.length()-1); // remove trailing '+'
547
}
548         return buf.toString();
549     }
550
551     /**
552      * Returns a parameter string identifying this event.
553      * This method is useful for event-logging and for debugging.
554      *
555      * @return a string identifying the event and its attributes
556      */

557     public String JavaDoc paramString() {
558         StringBuffer JavaDoc str = new StringBuffer JavaDoc(80);
559
560         switch(id) {
561           case MOUSE_PRESSED:
562               str.append("MOUSE_PRESSED");
563               break;
564           case MOUSE_RELEASED:
565               str.append("MOUSE_RELEASED");
566               break;
567           case MOUSE_CLICKED:
568               str.append("MOUSE_CLICKED");
569               break;
570           case MOUSE_ENTERED:
571               str.append("MOUSE_ENTERED");
572               break;
573           case MOUSE_EXITED:
574               str.append("MOUSE_EXITED");
575               break;
576           case MOUSE_MOVED:
577               str.append("MOUSE_MOVED");
578               break;
579           case MOUSE_DRAGGED:
580               str.append("MOUSE_DRAGGED");
581               break;
582           case MOUSE_WHEEL:
583               str.append("MOUSE_WHEEL");
584               break;
585            default:
586               str.append("unknown type");
587         }
588
589         // (x,y) coordinates
590
str.append(",(").append(x).append(",").append(y).append(")");
591
592         str.append(",button=").append(getButton());
593
594         if (getModifiers() != 0) {
595             str.append(",modifiers=").append(getMouseModifiersText(modifiers));
596         }
597
598         if (getModifiersEx() != 0) {
599             str.append(",extModifiers=").append(getModifiersExText(modifiers));
600         }
601
602         str.append(",clickCount=").append(clickCount);
603
604         return str.toString();
605     }
606
607     /**
608      * Sets new modifiers by the old ones.
609      * Also sets button.
610      */

611     private void setNewModifiers() {
612         if ((modifiers & BUTTON1_MASK) != 0) {
613         modifiers |= BUTTON1_DOWN_MASK;
614     }
615     if ((modifiers & BUTTON2_MASK) != 0) {
616         modifiers |= BUTTON2_DOWN_MASK;
617     }
618     if ((modifiers & BUTTON3_MASK) != 0) {
619         modifiers |= BUTTON3_DOWN_MASK;
620     }
621     if (id == MOUSE_PRESSED
622             || id == MOUSE_RELEASED
623         || id == MOUSE_CLICKED)
624     {
625         if ((modifiers & BUTTON1_MASK) != 0) {
626         button = BUTTON1;
627         modifiers &= ~BUTTON2_MASK & ~BUTTON3_MASK;
628         if (id != MOUSE_PRESSED) {
629             modifiers &= ~BUTTON1_DOWN_MASK;
630         }
631         } else if ((modifiers & BUTTON2_MASK) != 0) {
632         button = BUTTON2;
633         modifiers &= ~BUTTON1_MASK & ~BUTTON3_MASK;
634         if (id != MOUSE_PRESSED) {
635             modifiers &= ~BUTTON2_DOWN_MASK;
636         }
637         } else if ((modifiers & BUTTON3_MASK) != 0) {
638         button = BUTTON3;
639         modifiers &= ~BUTTON1_MASK & ~BUTTON2_MASK;
640         if (id != MOUSE_PRESSED) {
641             modifiers &= ~BUTTON3_DOWN_MASK;
642         }
643         }
644     }
645     if ((modifiers & InputEvent.ALT_MASK) != 0) {
646         modifiers |= InputEvent.ALT_DOWN_MASK;
647     }
648     if ((modifiers & InputEvent.META_MASK) != 0) {
649         modifiers |= InputEvent.META_DOWN_MASK;
650     }
651     if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
652         modifiers |= InputEvent.SHIFT_DOWN_MASK;
653     }
654     if ((modifiers & InputEvent.CTRL_MASK) != 0) {
655         modifiers |= InputEvent.CTRL_DOWN_MASK;
656     }
657     if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
658         modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK;
659     }
660     }
661
662     /**
663      * Sets old modifiers by the new ones.
664      */

665     private void setOldModifiers() {
666     if (id == MOUSE_PRESSED
667             || id == MOUSE_RELEASED
668         || id == MOUSE_CLICKED)
669     {
670         switch(button) {
671         case BUTTON1:
672         modifiers |= BUTTON1_MASK;
673         break;
674         case BUTTON2:
675         modifiers |= BUTTON2_MASK;
676         break;
677         case BUTTON3:
678         modifiers |= BUTTON3_MASK;
679         break;
680         }
681     } else {
682         if ((modifiers & BUTTON1_DOWN_MASK) != 0) {
683         modifiers |= BUTTON1_MASK;
684         }
685         if ((modifiers & BUTTON2_DOWN_MASK) != 0) {
686         modifiers |= BUTTON2_MASK;
687         }
688         if ((modifiers & BUTTON3_DOWN_MASK) != 0) {
689         modifiers |= BUTTON3_MASK;
690         }
691     }
692     if ((modifiers & ALT_DOWN_MASK) != 0) {
693         modifiers |= ALT_MASK;
694     }
695     if ((modifiers & META_DOWN_MASK) != 0) {
696         modifiers |= META_MASK;
697     }
698     if ((modifiers & SHIFT_DOWN_MASK) != 0) {
699         modifiers |= SHIFT_MASK;
700     }
701     if ((modifiers & CTRL_DOWN_MASK) != 0) {
702         modifiers |= CTRL_MASK;
703     }
704     if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) {
705         modifiers |= ALT_GRAPH_MASK;
706     }
707     }
708
709     /**
710      * Sets new modifiers by the old ones.
711      * @serial
712      */

713     private void readObject(ObjectInputStream JavaDoc s)
714       throws IOException JavaDoc, ClassNotFoundException JavaDoc {
715     s.defaultReadObject();
716     if (getModifiers() != 0 && getModifiersEx() == 0) {
717         setNewModifiers();
718     }
719     }
720 }
721
722
Popular Tags