KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > AWTEvent


1 /*
2  * @(#)AWTEvent.java 1.55 04/06/02
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;
9
10 import java.util.EventObject JavaDoc;
11 import java.awt.event.*;
12 import java.awt.peer.ComponentPeer;
13 import java.awt.peer.LightweightPeer;
14 import java.lang.reflect.Field JavaDoc;
15
16 /**
17  * The root event class for all AWT events.
18  * This class and its subclasses supercede the original
19  * java.awt.Event class.
20  * Subclasses of this root AWTEvent class defined outside of the
21  * java.awt.event package should define event ID values greater than
22  * the value defined by RESERVED_ID_MAX.
23  * <p>
24  * The event masks defined in this class are needed by Component subclasses
25  * which are using Component.enableEvents() to select for event types not
26  * selected by registered listeners. If a listener is registered on a
27  * component, the appropriate event mask is already set internally by the
28  * component.
29  * <p>
30  * The masks are also used to specify to which types of events an
31  * AWTEventListener should listen. The masks are bitwise-ORed together
32  * and passed to Toolkit.addAWTEventListener.
33  *
34  * @see Component#enableEvents
35  * @see Toolkit#addAWTEventListener
36  *
37  * @see java.awt.event.ActionEvent
38  * @see java.awt.event.AdjustmentEvent
39  * @see java.awt.event.ComponentEvent
40  * @see java.awt.event.ContainerEvent
41  * @see java.awt.event.FocusEvent
42  * @see java.awt.event.InputMethodEvent
43  * @see java.awt.event.InvocationEvent
44  * @see java.awt.event.ItemEvent
45  * @see java.awt.event.HierarchyEvent
46  * @see java.awt.event.KeyEvent
47  * @see java.awt.event.MouseEvent
48  * @see java.awt.event.MouseWheelEvent
49  * @see java.awt.event.PaintEvent
50  * @see java.awt.event.TextEvent
51  * @see java.awt.event.WindowEvent
52  *
53  * @author Carl Quinn
54  * @author Amy Fowler
55  * @version 1.55 06/02/04
56  * @since 1.1
57  */

58 public abstract class AWTEvent extends EventObject JavaDoc {
59     private byte bdata[];
60
61     /**
62      * The event's id.
63      * @serial
64      * @see #getID()
65      * @see #AWTEvent
66      */

67     protected int id;
68
69     /**
70      * Controls whether or not the event is sent back down to the peer once the
71      * source has processed it - false means it's sent to the peer; true means
72      * it's not. Semantic events always have a 'true' value since they were
73      * generated by the peer in response to a low-level event.
74      * @serial
75      * @see #consume
76      * @see #isConsumed
77      */

78     protected boolean consumed = false;
79   
80     transient boolean focusManagerIsDispatching = false;
81     transient boolean isPosted;
82  
83     /**
84      * The event mask for selecting component events.
85      */

86     public final static long COMPONENT_EVENT_MASK = 0x01;
87
88     /**
89      * The event mask for selecting container events.
90      */

91     public final static long CONTAINER_EVENT_MASK = 0x02;
92
93     /**
94      * The event mask for selecting focus events.
95      */

96     public final static long FOCUS_EVENT_MASK = 0x04;
97
98     /**
99      * The event mask for selecting key events.
100      */

101     public final static long KEY_EVENT_MASK = 0x08;
102
103     /**
104      * The event mask for selecting mouse events.
105      */

106     public final static long MOUSE_EVENT_MASK = 0x10;
107
108     /**
109      * The event mask for selecting mouse motion events.
110      */

111     public final static long MOUSE_MOTION_EVENT_MASK = 0x20;
112
113     /**
114      * The event mask for selecting window events.
115      */

116     public final static long WINDOW_EVENT_MASK = 0x40;
117
118     /**
119      * The event mask for selecting action events.
120      */

121     public final static long ACTION_EVENT_MASK = 0x80;
122
123     /**
124      * The event mask for selecting adjustment events.
125      */

126     public final static long ADJUSTMENT_EVENT_MASK = 0x100;
127
128     /**
129      * The event mask for selecting item events.
130      */

131     public final static long ITEM_EVENT_MASK = 0x200;
132
133     /**
134      * The event mask for selecting text events.
135      */

136     public final static long TEXT_EVENT_MASK = 0x400;
137
138     /**
139      * The event mask for selecting input method events.
140      */

141     public final static long INPUT_METHOD_EVENT_MASK = 0x800;
142
143     /**
144      * The pseudo event mask for enabling input methods.
145      * We're using one bit in the eventMask so we don't need
146      * a separate field inputMethodsEnabled.
147      */

148     final static long INPUT_METHODS_ENABLED_MASK = 0x1000;
149
150     /**
151      * The event mask for selecting paint events.
152      */

153     public final static long PAINT_EVENT_MASK = 0x2000;
154
155     /**
156      * The event mask for selecting invocation events.
157      */

158     public final static long INVOCATION_EVENT_MASK = 0x4000;
159
160     /**
161      * The event mask for selecting hierarchy events.
162      */

163     public final static long HIERARCHY_EVENT_MASK = 0x8000;
164
165     /**
166      * The event mask for selecting hierarchy bounds events.
167      */

168     public final static long HIERARCHY_BOUNDS_EVENT_MASK = 0x10000;
169
170     /**
171      * The event mask for selecting mouse wheel events.
172      * @since 1.4
173      */

174     public final static long MOUSE_WHEEL_EVENT_MASK = 0x20000;
175
176     /**
177      * The event mask for selecting window state events.
178      * @since 1.4
179      */

180     public final static long WINDOW_STATE_EVENT_MASK = 0x40000;
181
182     /**
183      * The event mask for selecting window focus events.
184      * @since 1.4
185      */

186     public final static long WINDOW_FOCUS_EVENT_MASK = 0x80000;
187
188     /**
189      * The maximum value for reserved AWT event IDs. Programs defining
190      * their own event IDs should use IDs greater than this value.
191      */

192     public final static int RESERVED_ID_MAX = 1999;
193
194     // security stuff
195
private static Field JavaDoc inputEvent_CanAccessSystemClipboard_Field = null;
196
197     /*
198      * JDK 1.1 serialVersionUID
199      */

200     private static final long serialVersionUID = -1825314779160409405L;
201
202     static {
203         /* ensure that the necessary native libraries are loaded */
204     Toolkit.loadLibraries();
205         if (!GraphicsEnvironment.isHeadless()) {
206             initIDs();
207         }
208     }
209
210     private static synchronized Field JavaDoc get_InputEvent_CanAccessSystemClipboard() {
211         if (inputEvent_CanAccessSystemClipboard_Field == null) {
212             inputEvent_CanAccessSystemClipboard_Field =
213                 (Field JavaDoc)java.security.AccessController.doPrivileged(
214                     new java.security.PrivilegedAction JavaDoc() {
215                             public Object JavaDoc run() {
216                                 Field JavaDoc field = null;
217                                 try {
218                                     field = InputEvent.class.
219                                         getDeclaredField("canAccessSystemClipboard");
220                                     field.setAccessible(true);
221                                     return field;
222                                 } catch (SecurityException JavaDoc e) {
223                                 } catch (NoSuchFieldException JavaDoc e) {
224                                 }
225                                 return null;
226                             }
227                         });
228         }
229
230         return inputEvent_CanAccessSystemClipboard_Field;
231     }
232
233     /**
234      * Initialize JNI field and method IDs for fields that may be
235      * accessed from C.
236      */

237     private static native void initIDs();
238
239     /**
240      * Constructs an AWTEvent object from the parameters of a 1.0-style event.
241      * @param event the old-style event
242      */

243     public AWTEvent(Event JavaDoc event) {
244         this(event.target, event.id);
245     }
246
247     /**
248      * Constructs an AWTEvent object with the specified source object and type.
249      * @param source the object where the event originated
250      * @id the event type
251      */

252     public AWTEvent(Object JavaDoc source, int id) {
253         super(source);
254     this.id = id;
255         switch(id) {
256           case ActionEvent.ACTION_PERFORMED:
257           case ItemEvent.ITEM_STATE_CHANGED:
258           case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
259           case TextEvent.TEXT_VALUE_CHANGED:
260             consumed = true;
261             break;
262           default:
263         }
264     }
265
266     /**
267      * Retargets an event to a new source. This method is typically used to
268      * retarget an event to a lightweight child Component of the original
269      * heavyweight source.
270      * <p>
271      * This method is intended to be used only by event targeting subsystems,
272      * such as client-defined KeyboardFocusManagers. It is not for general
273      * client use.
274      *
275      * @param newSource the new Object to which the event should be dispatched
276      */

277     public void setSource(Object JavaDoc newSource) {
278     if (source == newSource) {
279         return;
280     }
281  
282     Component JavaDoc comp = null;
283     if (newSource instanceof Component JavaDoc) {
284         comp = (Component JavaDoc)newSource;
285         while (comp != null && comp.peer != null &&
286            (comp.peer instanceof LightweightPeer)) {
287         comp = comp.parent;
288         }
289     }
290  
291     synchronized (this) {
292         source = newSource;
293         if (comp != null) {
294         ComponentPeer peer = comp.peer;
295         if (peer != null) {
296             nativeSetSource(peer);
297         }
298         }
299     }
300     }
301   
302     private native void nativeSetSource(ComponentPeer peer);
303  
304     /**
305      * Returns the event type.
306      */

307     public int getID() {
308         return id;
309     }
310
311     /**
312      * Returns a String representation of this object.
313      */

314     public String JavaDoc toString() {
315         String JavaDoc srcName = null;
316         if (source instanceof Component JavaDoc) {
317             srcName = ((Component JavaDoc)source).getName();
318         } else if (source instanceof MenuComponent JavaDoc) {
319             srcName = ((MenuComponent JavaDoc)source).getName();
320         }
321     return getClass().getName() + "[" + paramString() + "] on " +
322             (srcName != null? srcName : source);
323     }
324
325     /**
326      * Returns a string representing the state of this <code>Event</code>.
327      * This method is intended to be used only for debugging purposes, and the
328      * content and format of the returned string may vary between
329      * implementations. The returned string may be empty but may not be
330      * <code>null</code>.
331      *
332      * @return a string representation of this event
333      */

334     public String JavaDoc paramString() {
335         return "";
336     }
337
338     /**
339      * Consumes this event, if this event can be consumed. Only low-level,
340      * system events can be consumed
341      */

342     protected void consume() {
343         switch(id) {
344           case KeyEvent.KEY_PRESSED:
345           case KeyEvent.KEY_RELEASED:
346           case MouseEvent.MOUSE_PRESSED:
347           case MouseEvent.MOUSE_RELEASED:
348           case MouseEvent.MOUSE_MOVED:
349           case MouseEvent.MOUSE_DRAGGED:
350           case MouseEvent.MOUSE_ENTERED:
351           case MouseEvent.MOUSE_EXITED:
352           case MouseEvent.MOUSE_WHEEL:
353           case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
354           case InputMethodEvent.CARET_POSITION_CHANGED:
355               consumed = true;
356               break;
357           default:
358               // event type cannot be consumed
359
}
360     }
361
362     /**
363      * Returns whether this event has been consumed.
364      */

365     protected boolean isConsumed() {
366         return consumed;
367     }
368
369     /**
370      * Converts a new event to an old one (used for compatibility).
371      * If the new event cannot be converted (because no old equivalent
372      * exists) then this returns null.
373      *
374      * Note: this method is here instead of in each individual new
375      * event class in java.awt.event because we don't want to make
376      * it public and it needs to be called from java.awt.
377      */

378     Event JavaDoc convertToOld() {
379         Object JavaDoc src = getSource();
380         int newid = id;
381
382         switch(id) {
383           case KeyEvent.KEY_PRESSED:
384           case KeyEvent.KEY_RELEASED:
385               KeyEvent ke = (KeyEvent)this;
386               if (ke.isActionKey()) {
387                   newid = (id == KeyEvent.KEY_PRESSED?
388                            Event.KEY_ACTION : Event.KEY_ACTION_RELEASE);
389               }
390               int keyCode = ke.getKeyCode();
391               if (keyCode == KeyEvent.VK_SHIFT ||
392                   keyCode == KeyEvent.VK_CONTROL ||
393                   keyCode == KeyEvent.VK_ALT) {
394                   return null; // suppress modifier keys in old event model.
395
}
396               // no mask for button1 existed in old Event - strip it out
397
return new Event JavaDoc(src, ke.getWhen(), newid, 0, 0,
398                                Event.getOldEventKey(ke),
399                                (ke.getModifiers() & ~InputEvent.BUTTON1_MASK));
400
401           case MouseEvent.MOUSE_PRESSED:
402           case MouseEvent.MOUSE_RELEASED:
403           case MouseEvent.MOUSE_MOVED:
404           case MouseEvent.MOUSE_DRAGGED:
405           case MouseEvent.MOUSE_ENTERED:
406           case MouseEvent.MOUSE_EXITED:
407               MouseEvent me = (MouseEvent)this;
408               // no mask for button1 existed in old Event - strip it out
409
Event JavaDoc olde = new Event JavaDoc(src, me.getWhen(), newid,
410                                me.getX(), me.getY(), 0,
411                                (me.getModifiers() & ~InputEvent.BUTTON1_MASK));
412               olde.clickCount = me.getClickCount();
413               return olde;
414
415           case FocusEvent.FOCUS_GAINED:
416               return new Event JavaDoc(src, Event.GOT_FOCUS, null);
417
418           case FocusEvent.FOCUS_LOST:
419               return new Event JavaDoc(src, Event.LOST_FOCUS, null);
420
421           case WindowEvent.WINDOW_CLOSING:
422           case WindowEvent.WINDOW_ICONIFIED:
423           case WindowEvent.WINDOW_DEICONIFIED:
424               return new Event JavaDoc(src, newid, null);
425
426           case ComponentEvent.COMPONENT_MOVED:
427               if (src instanceof Frame JavaDoc || src instanceof Dialog JavaDoc) {
428                   Point JavaDoc p = ((Component JavaDoc)src).getLocation();
429                   return new Event JavaDoc(src, 0, Event.WINDOW_MOVED, p.x, p.y, 0, 0);
430               }
431               break;
432
433           case ActionEvent.ACTION_PERFORMED:
434               ActionEvent ae = (ActionEvent)this;
435               String JavaDoc cmd;
436               if (src instanceof Button JavaDoc) {
437                   cmd = ((Button JavaDoc)src).getLabel();
438               } else if (src instanceof MenuItem JavaDoc) {
439                   cmd = ((MenuItem JavaDoc)src).getLabel();
440               } else {
441                   cmd = ae.getActionCommand();
442               }
443               return new Event JavaDoc(src, 0, newid, 0, 0, 0, ae.getModifiers(), cmd);
444
445           case ItemEvent.ITEM_STATE_CHANGED:
446               ItemEvent ie = (ItemEvent)this;
447               Object JavaDoc arg;
448               if (src instanceof List JavaDoc) {
449                   newid = (ie.getStateChange() == ItemEvent.SELECTED?
450                            Event.LIST_SELECT : Event.LIST_DESELECT);
451                   arg = ie.getItem();
452               } else {
453                   newid = Event.ACTION_EVENT;
454                   if (src instanceof Choice JavaDoc) {
455                       arg = ie.getItem();
456
457                   } else { // Checkbox
458
arg = new Boolean JavaDoc(ie.getStateChange() == ItemEvent.SELECTED);
459                   }
460               }
461               return new Event JavaDoc(src, newid, arg);
462
463           case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
464               AdjustmentEvent aje = (AdjustmentEvent)this;
465               switch(aje.getAdjustmentType()) {
466                 case AdjustmentEvent.UNIT_INCREMENT:
467                   newid = Event.SCROLL_LINE_DOWN;
468                   break;
469                 case AdjustmentEvent.UNIT_DECREMENT:
470                   newid = Event.SCROLL_LINE_UP;
471                   break;
472                 case AdjustmentEvent.BLOCK_INCREMENT:
473                   newid = Event.SCROLL_PAGE_DOWN;
474                   break;
475                 case AdjustmentEvent.BLOCK_DECREMENT:
476                   newid = Event.SCROLL_PAGE_UP;
477                   break;
478                 case AdjustmentEvent.TRACK:
479           if (aje.getValueIsAdjusting()) {
480               newid = Event.SCROLL_ABSOLUTE;
481           }
482           else {
483               newid = Event.SCROLL_END;
484           }
485                   break;
486                 default:
487                   return null;
488               }
489               return new Event JavaDoc(src, newid, new Integer JavaDoc(aje.getValue()));
490
491           default:
492         }
493         return null;
494     }
495
496     /**
497      * Copies all private data from this event into that.
498      * Space is allocated for the copied data that will be
499      * freed when the that is finalized. Upon completion,
500      * this event is not changed.
501      */

502     void copyPrivateDataInto(AWTEvent JavaDoc that) {
503     that.bdata = this.bdata;
504         // Copy canAccessSystemClipboard value from this into that.
505
if (this instanceof InputEvent && that instanceof InputEvent) {
506             Field JavaDoc field = get_InputEvent_CanAccessSystemClipboard();
507             if (field != null) {
508                 try {
509                     boolean b = field.getBoolean(this);
510                     field.setBoolean(that, b);
511                 } catch(IllegalAccessException JavaDoc e) {
512                 }
513             }
514         }
515     }
516
517     void dispatched() {
518         if (this instanceof InputEvent) {
519             Field JavaDoc field = get_InputEvent_CanAccessSystemClipboard();
520             if (field != null) {
521                 try {
522                     field.setBoolean(this, false);
523                 } catch(IllegalAccessException JavaDoc e) {
524                 }
525             }
526         }
527     }
528 } // class AWTEvent
529

530
Popular Tags