001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dmitry A. Durnev, Michael Danilov
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.util.EventObject;
023: import java.util.Hashtable;
024: import java.util.EventListener;
025:
026: import java.awt.event.*;
027:
028: public abstract class AWTEvent extends EventObject {
029: private static final long serialVersionUID = -1825314779160409405L;
030:
031: public static final long COMPONENT_EVENT_MASK = 1;
032:
033: public static final long CONTAINER_EVENT_MASK = 2;
034:
035: public static final long FOCUS_EVENT_MASK = 4;
036:
037: public static final long KEY_EVENT_MASK = 8;
038:
039: public static final long MOUSE_EVENT_MASK = 16;
040:
041: public static final long MOUSE_MOTION_EVENT_MASK = 32;
042:
043: public static final long WINDOW_EVENT_MASK = 64;
044:
045: public static final long ACTION_EVENT_MASK = 128;
046:
047: public static final long ADJUSTMENT_EVENT_MASK = 256;
048:
049: public static final long ITEM_EVENT_MASK = 512;
050:
051: public static final long TEXT_EVENT_MASK = 1024;
052:
053: public static final long INPUT_METHOD_EVENT_MASK = 2048;
054:
055: public static final long PAINT_EVENT_MASK = 8192;
056:
057: public static final long INVOCATION_EVENT_MASK = 16384;
058:
059: public static final long HIERARCHY_EVENT_MASK = 32768;
060:
061: public static final long HIERARCHY_BOUNDS_EVENT_MASK = 65536;
062:
063: public static final long MOUSE_WHEEL_EVENT_MASK = 131072;
064:
065: public static final long WINDOW_STATE_EVENT_MASK = 262144;
066:
067: public static final long WINDOW_FOCUS_EVENT_MASK = 524288;
068:
069: public static final int RESERVED_ID_MAX = 1999;
070:
071: private static final Hashtable<Integer, EventDescriptor> eventsMap = new Hashtable<Integer, EventDescriptor>();
072:
073: private static EventConverter converter;
074:
075: protected int id;
076:
077: protected boolean consumed;
078:
079: boolean dispatchedByKFM;
080:
081: transient boolean isPosted;
082:
083: static {
084: eventsMap.put(new Integer(KeyEvent.KEY_TYPED),
085: new EventDescriptor(KEY_EVENT_MASK, KeyListener.class));
086: eventsMap.put(new Integer(KeyEvent.KEY_PRESSED),
087: new EventDescriptor(KEY_EVENT_MASK, KeyListener.class));
088: eventsMap.put(new Integer(KeyEvent.KEY_RELEASED),
089: new EventDescriptor(KEY_EVENT_MASK, KeyListener.class));
090: eventsMap.put(new Integer(MouseEvent.MOUSE_CLICKED),
091: new EventDescriptor(MOUSE_EVENT_MASK,
092: MouseListener.class));
093: eventsMap.put(new Integer(MouseEvent.MOUSE_PRESSED),
094: new EventDescriptor(MOUSE_EVENT_MASK,
095: MouseListener.class));
096: eventsMap.put(new Integer(MouseEvent.MOUSE_RELEASED),
097: new EventDescriptor(MOUSE_EVENT_MASK,
098: MouseListener.class));
099: eventsMap.put(new Integer(MouseEvent.MOUSE_MOVED),
100: new EventDescriptor(MOUSE_MOTION_EVENT_MASK,
101: MouseMotionListener.class));
102: eventsMap.put(new Integer(MouseEvent.MOUSE_ENTERED),
103: new EventDescriptor(MOUSE_EVENT_MASK,
104: MouseListener.class));
105: eventsMap.put(new Integer(MouseEvent.MOUSE_EXITED),
106: new EventDescriptor(MOUSE_EVENT_MASK,
107: MouseListener.class));
108: eventsMap.put(new Integer(MouseEvent.MOUSE_DRAGGED),
109: new EventDescriptor(MOUSE_MOTION_EVENT_MASK,
110: MouseMotionListener.class));
111: eventsMap.put(new Integer(MouseEvent.MOUSE_WHEEL),
112: new EventDescriptor(MOUSE_WHEEL_EVENT_MASK,
113: MouseWheelListener.class));
114: eventsMap.put(new Integer(ComponentEvent.COMPONENT_MOVED),
115: new EventDescriptor(COMPONENT_EVENT_MASK,
116: ComponentListener.class));
117: eventsMap.put(new Integer(ComponentEvent.COMPONENT_RESIZED),
118: new EventDescriptor(COMPONENT_EVENT_MASK,
119: ComponentListener.class));
120: eventsMap.put(new Integer(ComponentEvent.COMPONENT_SHOWN),
121: new EventDescriptor(COMPONENT_EVENT_MASK,
122: ComponentListener.class));
123: eventsMap.put(new Integer(ComponentEvent.COMPONENT_HIDDEN),
124: new EventDescriptor(COMPONENT_EVENT_MASK,
125: ComponentListener.class));
126: eventsMap.put(new Integer(FocusEvent.FOCUS_GAINED),
127: new EventDescriptor(FOCUS_EVENT_MASK,
128: FocusListener.class));
129: eventsMap.put(new Integer(FocusEvent.FOCUS_LOST),
130: new EventDescriptor(FOCUS_EVENT_MASK,
131: FocusListener.class));
132: eventsMap.put(new Integer(PaintEvent.PAINT),
133: new EventDescriptor(PAINT_EVENT_MASK, null));
134: eventsMap.put(new Integer(PaintEvent.UPDATE),
135: new EventDescriptor(PAINT_EVENT_MASK, null));
136: eventsMap.put(new Integer(WindowEvent.WINDOW_OPENED),
137: new EventDescriptor(WINDOW_EVENT_MASK,
138: WindowListener.class));
139: eventsMap.put(new Integer(WindowEvent.WINDOW_CLOSING),
140: new EventDescriptor(WINDOW_EVENT_MASK,
141: WindowListener.class));
142: eventsMap.put(new Integer(WindowEvent.WINDOW_CLOSED),
143: new EventDescriptor(WINDOW_EVENT_MASK,
144: WindowListener.class));
145: eventsMap.put(new Integer(WindowEvent.WINDOW_DEICONIFIED),
146: new EventDescriptor(WINDOW_EVENT_MASK,
147: WindowListener.class));
148: eventsMap.put(new Integer(WindowEvent.WINDOW_ICONIFIED),
149: new EventDescriptor(WINDOW_EVENT_MASK,
150: WindowListener.class));
151: eventsMap.put(new Integer(WindowEvent.WINDOW_STATE_CHANGED),
152: new EventDescriptor(WINDOW_STATE_EVENT_MASK,
153: WindowStateListener.class));
154: eventsMap.put(new Integer(WindowEvent.WINDOW_LOST_FOCUS),
155: new EventDescriptor(WINDOW_FOCUS_EVENT_MASK,
156: WindowFocusListener.class));
157: eventsMap.put(new Integer(WindowEvent.WINDOW_GAINED_FOCUS),
158: new EventDescriptor(WINDOW_FOCUS_EVENT_MASK,
159: WindowFocusListener.class));
160: eventsMap.put(new Integer(WindowEvent.WINDOW_DEACTIVATED),
161: new EventDescriptor(WINDOW_EVENT_MASK,
162: WindowListener.class));
163: eventsMap.put(new Integer(WindowEvent.WINDOW_ACTIVATED),
164: new EventDescriptor(WINDOW_EVENT_MASK,
165: WindowListener.class));
166: eventsMap.put(new Integer(HierarchyEvent.HIERARCHY_CHANGED),
167: new EventDescriptor(HIERARCHY_EVENT_MASK,
168: HierarchyListener.class));
169: eventsMap.put(new Integer(HierarchyEvent.ANCESTOR_MOVED),
170: new EventDescriptor(HIERARCHY_BOUNDS_EVENT_MASK,
171: HierarchyBoundsListener.class));
172: eventsMap.put(new Integer(HierarchyEvent.ANCESTOR_RESIZED),
173: new EventDescriptor(HIERARCHY_BOUNDS_EVENT_MASK,
174: HierarchyBoundsListener.class));
175: eventsMap.put(new Integer(ContainerEvent.COMPONENT_ADDED),
176: new EventDescriptor(CONTAINER_EVENT_MASK,
177: ContainerListener.class));
178: eventsMap.put(new Integer(ContainerEvent.COMPONENT_REMOVED),
179: new EventDescriptor(CONTAINER_EVENT_MASK,
180: ContainerListener.class));
181: eventsMap.put(new Integer(
182: InputMethodEvent.INPUT_METHOD_TEXT_CHANGED),
183: new EventDescriptor(INPUT_METHOD_EVENT_MASK,
184: InputMethodListener.class));
185: eventsMap.put(new Integer(
186: InputMethodEvent.CARET_POSITION_CHANGED),
187: new EventDescriptor(INPUT_METHOD_EVENT_MASK,
188: InputMethodListener.class));
189: eventsMap.put(new Integer(InvocationEvent.INVOCATION_DEFAULT),
190: new EventDescriptor(INVOCATION_EVENT_MASK, null));
191: eventsMap
192: .put(new Integer(ItemEvent.ITEM_STATE_CHANGED),
193: new EventDescriptor(ITEM_EVENT_MASK,
194: ItemListener.class));
195: eventsMap
196: .put(new Integer(TextEvent.TEXT_VALUE_CHANGED),
197: new EventDescriptor(TEXT_EVENT_MASK,
198: TextListener.class));
199: eventsMap.put(new Integer(ActionEvent.ACTION_PERFORMED),
200: new EventDescriptor(ACTION_EVENT_MASK,
201: ActionListener.class));
202: eventsMap.put(new Integer(
203: AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED),
204: new EventDescriptor(ADJUSTMENT_EVENT_MASK,
205: AdjustmentListener.class));
206: converter = new EventConverter();
207: }
208:
209: public AWTEvent(Event event) {
210: this (event.target, event.id);
211: }
212:
213: public AWTEvent(Object source, int id) {
214: super (source);
215: this .id = id;
216: consumed = false;
217: }
218:
219: public int getID() {
220: return id;
221: }
222:
223: public void setSource(Object newSource) {
224: source = newSource;
225: }
226:
227: @Override
228: public String toString() {
229: /* The format is based on 1.5 release behavior
230: * which can be revealed by the following code:
231: *
232: * AWTEvent event = new AWTEvent(new Component(){}, 1){};
233: * System.out.println(event);
234: */
235: String name = ""; //$NON-NLS-1$
236: if (source instanceof Component && (source != null)) {
237: Component comp = (Component) getSource();
238: name = comp.getName();
239: if (name == null) {
240: name = ""; //$NON-NLS-1$
241: }
242: }
243: return (getClass().getName() + "[" + paramString() + "]" //$NON-NLS-1$ //$NON-NLS-2$
244: + " on " + (name.length() > 0 ? name : source)); //$NON-NLS-1$
245: }
246:
247: public String paramString() {
248: //nothing to implement: all event types must override this method
249: return ""; //$NON-NLS-1$
250: }
251:
252: protected boolean isConsumed() {
253: return consumed;
254: }
255:
256: protected void consume() {
257: consumed = true;
258: }
259:
260: /**
261: * Convert AWTEvent object to deprecated Event object
262: *
263: * @return new Event object which is a converted AWTEvent object or null
264: * if the conversion is not possible
265: */
266: Event getEvent() {
267:
268: if (id == ActionEvent.ACTION_PERFORMED) {
269: ActionEvent ae = (ActionEvent) this ;
270: return converter.convertActionEvent(ae);
271:
272: } else if (id == AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED) {
273: AdjustmentEvent ae = (AdjustmentEvent) this ;
274: return converter.convertAdjustmentEvent(ae);
275:
276: } else if (id == ComponentEvent.COMPONENT_MOVED
277: && source instanceof Window) {
278: //the only type of Component events is COMPONENT_MOVED on window
279: ComponentEvent ce = (ComponentEvent) this ;
280: return converter.convertComponentEvent(ce);
281:
282: } else if (id >= FocusEvent.FOCUS_FIRST
283: && id <= FocusEvent.FOCUS_LAST) {
284: //nothing to convert
285: } else if (id == ItemEvent.ITEM_STATE_CHANGED) {
286: ItemEvent ie = (ItemEvent) this ;
287: return converter.convertItemEvent(ie);
288:
289: } else if (id == KeyEvent.KEY_PRESSED
290: || id == KeyEvent.KEY_RELEASED) {
291: KeyEvent ke = (KeyEvent) this ;
292: return converter.convertKeyEvent(ke);
293: } else if (id >= MouseEvent.MOUSE_FIRST
294: && id <= MouseEvent.MOUSE_LAST) {
295: MouseEvent me = (MouseEvent) this ;
296: return converter.convertMouseEvent(me);
297: } else if (id == WindowEvent.WINDOW_CLOSING
298: || id == WindowEvent.WINDOW_ICONIFIED
299: || id == WindowEvent.WINDOW_DEICONIFIED) {
300: //nothing to convert
301: } else {
302: return null;
303: }
304:
305: return new Event(source, id, null);
306: }
307:
308: static final class EventDescriptor {
309:
310: final long eventMask;
311:
312: final Class<? extends EventListener> listenerType;
313:
314: EventDescriptor(long eventMask,
315: Class<? extends EventListener> listenerType) {
316: this .eventMask = eventMask;
317: this .listenerType = listenerType;
318: }
319:
320: }
321:
322: static final class EventTypeLookup {
323: private AWTEvent lastEvent = null;
324: private EventDescriptor lastEventDescriptor = null;
325:
326: EventDescriptor getEventDescriptor(AWTEvent event) {
327: synchronized (this ) {
328: if (event != lastEvent) {
329: lastEvent = event;
330: lastEventDescriptor = eventsMap.get(new Integer(
331: event.id));
332: }
333:
334: return lastEventDescriptor;
335: }
336: }
337:
338: long getEventMask(AWTEvent event) {
339: final EventDescriptor ed = getEventDescriptor(event);
340: return ed == null ? -1 : ed.eventMask;
341: }
342: }
343:
344: static final class EventConverter {
345: static final int OLD_MOD_MASK = Event.ALT_MASK
346: | Event.CTRL_MASK | Event.META_MASK | Event.SHIFT_MASK;
347:
348: Event convertActionEvent(ActionEvent ae) {
349: Event evt = new Event(ae.getSource(), ae.getID(), ae
350: .getActionCommand());
351: evt.when = ae.getWhen();
352: evt.modifiers = ae.getModifiers() & OLD_MOD_MASK;
353:
354: /* if (source instanceof Button) {
355: arg = ((Button) source).getLabel();
356: } else if (source instanceof Checkbox) {
357: arg = new Boolean(((Checkbox) source).getState());
358: } else if (source instanceof CheckboxMenuItem) {
359: arg = ((CheckboxMenuItem) source).getLabel();
360: } else if (source instanceof Choice) {
361: arg = ((Choice) source).getSelectedItem();
362: } else if (source instanceof List) {
363: arg = ((List) source).getSelectedItem();
364: } else if (source instanceof MenuItem) {
365: arg = ((MenuItem) source).getLabel();
366: } else if (source instanceof TextField) {
367: arg = ((TextField) source).getText();
368: }
369: */
370: return evt;
371: }
372:
373: Event convertAdjustmentEvent(AdjustmentEvent ae) {
374: //TODO: Event.SCROLL_BEGIN/SCROLL_END
375: return new Event(ae.source, ae.id + ae.getAdjustmentType()
376: - 1, new Integer(ae.getValue()));
377: }
378:
379: Event convertComponentEvent(ComponentEvent ce) {
380: Component comp = ce.getComponent();
381: Event evt = new Event(comp, Event.WINDOW_MOVED, null);
382: evt.x = comp.getX();
383: evt.y = comp.getY();
384: return evt;
385: }
386:
387: Event convertItemEvent(ItemEvent ie) {
388: int oldId = ie.id + ie.getStateChange() - 1;
389: Object source = ie.source;
390: int idx = -1;
391: if (source instanceof List) {
392: List list = (List) source;
393: idx = list.getSelectedIndex();
394: } else if (source instanceof Choice) {
395: Choice choice = (Choice) source;
396: idx = choice.getSelectedIndex();
397: }
398: Object arg = idx >= 0 ? new Integer(idx) : null;
399: return new Event(source, oldId, arg);
400: }
401:
402: Event convertKeyEvent(KeyEvent ke) {
403: int oldId = ke.id;
404: //leave only old Event's modifiers
405:
406: int mod = ke.getModifiers() & OLD_MOD_MASK;
407: Component comp = ke.getComponent();
408: char keyChar = ke.getKeyChar();
409: int keyCode = ke.getKeyCode();
410: int key = convertKey(keyChar, keyCode);
411: if (key >= Event.HOME && key <= Event.INSERT) {
412: oldId += 2; //non-ASCII key -> action key
413: }
414: return new Event(comp, ke.getWhen(), oldId, 0, 0, key, mod);
415: }
416:
417: Event convertMouseEvent(MouseEvent me) {
418: int id = me.id;
419: if (id != MouseEvent.MOUSE_CLICKED) {
420: Event evt = new Event(me.source, id, null);
421: evt.x = me.getX();
422: evt.y = me.getY();
423: int mod = me.getModifiers();
424: //in Event modifiers mean button number for mouse events:
425: evt.modifiers = mod
426: & (Event.ALT_MASK | Event.META_MASK);
427: if (id == MouseEvent.MOUSE_PRESSED) {
428: evt.clickCount = me.getClickCount();
429: }
430: return evt;
431: }
432: return null;
433: }
434:
435: int convertKey(char keyChar, int keyCode) {
436: int key;
437: //F1 - F12
438: if (keyCode >= KeyEvent.VK_F1 && keyCode <= KeyEvent.VK_F12) {
439: key = Event.F1 + keyCode - KeyEvent.VK_F1;
440: } else {
441: switch (keyCode) {
442: default: //non-action key
443: key = keyChar;
444: break;
445: //action keys:
446: case KeyEvent.VK_HOME:
447: key = Event.HOME;
448: break;
449: case KeyEvent.VK_END:
450: key = Event.END;
451: break;
452: case KeyEvent.VK_PAGE_UP:
453: key = Event.PGUP;
454: break;
455: case KeyEvent.VK_PAGE_DOWN:
456: key = Event.PGDN;
457: break;
458: case KeyEvent.VK_UP:
459: key = Event.UP;
460: break;
461: case KeyEvent.VK_DOWN:
462: key = Event.DOWN;
463: break;
464: case KeyEvent.VK_LEFT:
465: key = Event.LEFT;
466: break;
467: case KeyEvent.VK_RIGHT:
468: key = Event.RIGHT;
469: break;
470: case KeyEvent.VK_PRINTSCREEN:
471: key = Event.PRINT_SCREEN;
472: break;
473: case KeyEvent.VK_SCROLL_LOCK:
474: key = Event.SCROLL_LOCK;
475: break;
476: case KeyEvent.VK_CAPS_LOCK:
477: key = Event.CAPS_LOCK;
478: break;
479: case KeyEvent.VK_NUM_LOCK:
480: key = Event.NUM_LOCK;
481: break;
482: case KeyEvent.VK_PAUSE:
483: key = Event.PAUSE;
484: break;
485: case KeyEvent.VK_INSERT:
486: key = Event.INSERT;
487: break;
488: }
489: }
490: return key;
491: }
492:
493: }
494:
495: }
|