001: /*
002: * @(#)AWTEvent.java 1.37 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: /*
029: * Warning :
030: * Two versions of this file exist in this workspace.
031: * One for Personal Basis, and one for Personal Profile.
032: * Don't edit the wrong one !!!
033: */
034:
035: package java.awt;
036:
037: import java.util.EventObject;
038: import java.awt.event.*;
039:
040: /**
041: * The root event class for all AWT events.
042: * This class and its subclasses supercede the original
043: * java.awt.Event class.
044: * Subclasses of this root AWTEvent class defined outside of the
045: * java.awt.event package should define event ID values greater than
046: * the value defined by RESERVED_ID_MAX.
047: *
048: * The event masks defined in this class are needed ONLY by
049: * component subclasses which are using Component.enableEvents()
050: * to select for event types not selected by registered listeners.
051: * If a listener is registered on a component, the appropriate event
052: * mask is already set internally by the component.
053: * @see Component#enableEvents
054: *
055: * @see java.awt.event.ComponentEvent
056: * @see java.awt.event.FocusEvent
057: * @see java.awt.event.KeyEvent
058: * @see java.awt.event.MouseEvent
059: * @see java.awt.event.WindowEvent
060: * @see java.awt.event.ActionEvent
061: * @see java.awt.event.AdjustmentEvent
062: * @see java.awt.event.ItemEvent
063: * @see java.awt.event.TextEvent
064: *
065: * @version 1.27 08/19/02
066: * @author Carl Quinn
067: * @author Amy Fowler
068: */
069: public abstract class AWTEvent extends EventObject {
070: private transient int data;
071: protected int id;
072: // This field controls whether or not the event is sent back
073: // down to the peer once the source has processed it -
074: // false means it's sent to the peer, true means it's not.
075: // Semantic events always have a 'true' value since they were
076: // generated by the peer in response to a low-level event.
077: protected boolean consumed = false;
078:
079: transient boolean focusManagerIsDispatching = false;
080: /**
081: * The event mask for selecting component events.
082: */
083: public final static long COMPONENT_EVENT_MASK = 0x01;
084: /**
085: * The event mask for selecting container events.
086: */
087: public final static long CONTAINER_EVENT_MASK = 0x02;
088: /**
089: * The event mask for selecting focus events.
090: */
091: public final static long FOCUS_EVENT_MASK = 0x04;
092: /**
093: * The event mask for selecting key events.
094: */
095: public final static long KEY_EVENT_MASK = 0x08;
096: /**
097: * The event mask for selecting mouse events.
098: */
099: public final static long MOUSE_EVENT_MASK = 0x10;
100: /**
101: * The event mask for selecting mouse motion events.
102: */
103: public final static long MOUSE_MOTION_EVENT_MASK = 0x20;
104: /**
105: * The event mask for selecting window events.
106: */
107: public final static long WINDOW_EVENT_MASK = 0x40;
108: /**
109: * The event mask for selecting action events.
110: */
111: public final static long ACTION_EVENT_MASK = 0x80;
112: /**
113: * The event mask for selecting adjustment events.
114: */
115: public final static long ADJUSTMENT_EVENT_MASK = 0x100;
116: /**
117: * The event mask for selecting item events.
118: */
119: public final static long ITEM_EVENT_MASK = 0x200;
120: /**
121: * The event mask for selecting text events.
122: */
123: public final static long TEXT_EVENT_MASK = 0x400;
124: /**
125: * The event mask for selecting input method events.
126: */
127: public final static long INPUT_METHOD_EVENT_MASK = 0x800;
128:
129: /**
130: * The pseudo event mask for enabling input methods.
131: * We're using one bit in the eventMask so we don't need
132: * a separate field inputMethodsEnabled.
133: */
134: final static long INPUT_METHODS_ENABLED_MASK = 0x1000;
135:
136: /**
137: * The event mask for selecting paint events.
138: */
139: public final static long PAINT_EVENT_MASK = 0x2000;
140:
141: /**
142: * The event mask for selecting invocation events.
143: */
144: public final static long INVOCATION_EVENT_MASK = 0x4000;
145:
146: /**
147: * The event mask for selecting mouse wheel events.
148: * @since 1.4
149: */
150: public final static long MOUSE_WHEEL_EVENT_MASK = 0x20000;
151:
152: /**
153: * The event mask for selecting window state events.
154: * @since 1.4
155: */
156: // public final static long WINDOW_STATE_EVENT_MASK = 0x40000;
157: /**
158: * The event mask for selecting window focus events.
159: */
160: public final static long WINDOW_FOCUS_EVENT_MASK = 0x80000;
161:
162: /**
163: * The maximum value for reserved AWT event IDs. Programs defining
164: * their own event IDs should use IDs greater than this value.
165: */
166: public final static int RESERVED_ID_MAX = 1999;
167: /*
168: * JDK 1.1 serialVersionUID
169: */
170: private static final long serialVersionUID = -1825314779160409405L;
171:
172: /**
173: * Constructs an AWTEvent object from the parameters of a 1.0-style event.
174: * @param event the old-style event
175: */
176: public AWTEvent(Event event) {
177: this (event.target, event.id);
178: }
179:
180: /**
181: * Constructs an AWTEvent object with the specified source object and type.
182: * @param source the object where the event originated
183: * @id the event type
184: */
185: public AWTEvent(Object source, int id) {
186: super (source);
187: this .id = id;
188: switch (id) {
189: case ActionEvent.ACTION_PERFORMED:
190: case ItemEvent.ITEM_STATE_CHANGED:
191: case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
192: case TextEvent.TEXT_VALUE_CHANGED:
193: consumed = true;
194: break;
195:
196: default:
197: }
198: }
199:
200: /**
201: * Returns the event type.
202: */
203: public int getID() {
204: return id;
205: }
206:
207: public String toString() {
208: String srcName = null;
209: if (source instanceof Component) {
210: srcName = ((Component) source).getName();
211: } else if (source instanceof MenuComponent) {
212: srcName = ((MenuComponent) source).getName();
213: }
214: return getClass().getName() + "[" + paramString() + "] on "
215: + (srcName != null ? srcName : source);
216: }
217:
218: public String paramString() {
219: return "";
220: }
221:
222: protected void consume() {
223: switch (id) {
224: case KeyEvent.KEY_PRESSED:
225: case KeyEvent.KEY_RELEASED:
226: case MouseEvent.MOUSE_PRESSED:
227: case MouseEvent.MOUSE_RELEASED:
228: case MouseEvent.MOUSE_MOVED:
229: case MouseEvent.MOUSE_DRAGGED:
230: case MouseEvent.MOUSE_ENTERED:
231: case MouseEvent.MOUSE_EXITED:
232: consumed = true;
233: break;
234:
235: default:
236: // event type cannot be consumed
237: }
238: }
239:
240: protected boolean isConsumed() {
241: return consumed;
242: }
243:
244: /* Converts a new event to an old one (used for compatibility).
245: * If the new event cannot be converted (because no old equivelent
246: * exists) then this returns null.
247: *
248: * Note: this method is here instead of in each individual new
249: * event class in java.awt.event because we don't want to make
250: * it public and it needs to be called from java.awt.
251: */
252: Event convertToOld() {
253: Object src = getSource();
254: int newid = id;
255: switch (id) {
256: case KeyEvent.KEY_PRESSED:
257: case KeyEvent.KEY_RELEASED:
258: KeyEvent ke = (KeyEvent) this ;
259: if (ke.isActionKey()) {
260: newid = (id == KeyEvent.KEY_PRESSED ? Event.KEY_ACTION
261: : Event.KEY_ACTION_RELEASE);
262: }
263: int keyCode = ke.getKeyCode();
264: if (keyCode == KeyEvent.VK_SHIFT
265: || keyCode == KeyEvent.VK_CONTROL
266: || keyCode == KeyEvent.VK_ALT) {
267: return null; // suppress modifier keys in old event model.
268: }
269: // no mask for button1 existed in old Event - strip it out
270: return new Event(src, ke.getWhen(), newid, 0, 0, Event
271: .getOldEventKey(ke),
272: (ke.getModifiers() & ~InputEvent.BUTTON1_MASK));
273:
274: case MouseEvent.MOUSE_PRESSED:
275: case MouseEvent.MOUSE_RELEASED:
276: case MouseEvent.MOUSE_MOVED:
277: case MouseEvent.MOUSE_DRAGGED:
278: case MouseEvent.MOUSE_ENTERED:
279: case MouseEvent.MOUSE_EXITED:
280: MouseEvent me = (MouseEvent) this ;
281: // no mask for button1 existed in old Event - strip it out
282: Event olde = new Event(src, me.getWhen(), newid, me.getX(),
283: me.getY(), 0,
284: (me.getModifiers() & ~InputEvent.BUTTON1_MASK));
285: olde.clickCount = me.getClickCount();
286: return olde;
287:
288: case FocusEvent.FOCUS_GAINED:
289: return new Event(src, Event.GOT_FOCUS, null);
290:
291: case FocusEvent.FOCUS_LOST:
292: return new Event(src, Event.LOST_FOCUS, null);
293:
294: case WindowEvent.WINDOW_CLOSING:
295: case WindowEvent.WINDOW_ICONIFIED:
296: case WindowEvent.WINDOW_DEICONIFIED:
297: return new Event(src, newid, null);
298:
299: case ComponentEvent.COMPONENT_MOVED:
300: if (src instanceof Frame || src instanceof Dialog) {
301: Point p = ((Component) src).getLocation();
302: return new Event(src, 0, Event.WINDOW_MOVED, p.x, p.y,
303: 0, 0);
304: }
305: break;
306:
307: case ActionEvent.ACTION_PERFORMED:
308: ActionEvent ae = (ActionEvent) this ;
309: String cmd;
310: if (src instanceof Button) {
311: cmd = ((Button) src).getLabel();
312: } else if (src instanceof MenuItem) {
313: cmd = ((MenuItem) src).getLabel();
314: } else {
315: cmd = ae.getActionCommand();
316: }
317: return new Event(src, 0, newid, 0, 0, 0, ae.getModifiers(),
318: cmd);
319:
320: case ItemEvent.ITEM_STATE_CHANGED:
321: ItemEvent ie = (ItemEvent) this ;
322: Object arg;
323: if (src instanceof List) {
324: newid = (ie.getStateChange() == ItemEvent.SELECTED ? Event.LIST_SELECT
325: : Event.LIST_DESELECT);
326: arg = ie.getItem();
327: } else {
328: newid = Event.ACTION_EVENT;
329: //Netscape: CheckboxMenuItems use the name of the
330: //menu choice as the arg, NOT a Bool
331:
332: if (src instanceof Choice
333: || src instanceof CheckboxMenuItem) {
334: arg = ie.getItem();
335: } else { // Checkbox
336: arg = new Boolean(
337: ie.getStateChange() == ItemEvent.SELECTED);
338: }
339: }
340: return new Event(src, newid, arg);
341:
342: case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
343: AdjustmentEvent aje = (AdjustmentEvent) this ;
344: switch (aje.getAdjustmentType()) {
345: case AdjustmentEvent.UNIT_INCREMENT:
346: newid = Event.SCROLL_LINE_DOWN;
347: break;
348:
349: case AdjustmentEvent.UNIT_DECREMENT:
350: newid = Event.SCROLL_LINE_UP;
351: break;
352:
353: case AdjustmentEvent.BLOCK_INCREMENT:
354: newid = Event.SCROLL_PAGE_DOWN;
355: break;
356:
357: case AdjustmentEvent.BLOCK_DECREMENT:
358: newid = Event.SCROLL_PAGE_UP;
359: break;
360:
361: case AdjustmentEvent.TRACK:
362: newid = Event.SCROLL_ABSOLUTE;
363: break;
364:
365: default:
366: return null;
367: }
368: return new Event(src, newid, new Integer(aje.getValue()));
369:
370: default:
371: }
372: return null;
373: }
374:
375: /* Package-private method to change a KeyEvent's source to the new
376: * focus owner. This method needs to be here instead of in KeyEvent
377: * because it should only be called from by the EventQueue.
378: */
379: // 6238261 - per spec bug this method should be package private again
380: void setSource(Object newSource) {
381: source = newSource;
382: }
383: }
|