1 7 8 package java.awt; 9 10 import java.util.EventObject ; 11 import java.awt.event.*; 12 import java.awt.peer.ComponentPeer; 13 import java.awt.peer.LightweightPeer; 14 import java.lang.reflect.Field ; 15 16 58 public abstract class AWTEvent extends EventObject { 59 private byte bdata[]; 60 61 67 protected int id; 68 69 78 protected boolean consumed = false; 79 80 transient boolean focusManagerIsDispatching = false; 81 transient boolean isPosted; 82 83 86 public final static long COMPONENT_EVENT_MASK = 0x01; 87 88 91 public final static long CONTAINER_EVENT_MASK = 0x02; 92 93 96 public final static long FOCUS_EVENT_MASK = 0x04; 97 98 101 public final static long KEY_EVENT_MASK = 0x08; 102 103 106 public final static long MOUSE_EVENT_MASK = 0x10; 107 108 111 public final static long MOUSE_MOTION_EVENT_MASK = 0x20; 112 113 116 public final static long WINDOW_EVENT_MASK = 0x40; 117 118 121 public final static long ACTION_EVENT_MASK = 0x80; 122 123 126 public final static long ADJUSTMENT_EVENT_MASK = 0x100; 127 128 131 public final static long ITEM_EVENT_MASK = 0x200; 132 133 136 public final static long TEXT_EVENT_MASK = 0x400; 137 138 141 public final static long INPUT_METHOD_EVENT_MASK = 0x800; 142 143 148 final static long INPUT_METHODS_ENABLED_MASK = 0x1000; 149 150 153 public final static long PAINT_EVENT_MASK = 0x2000; 154 155 158 public final static long INVOCATION_EVENT_MASK = 0x4000; 159 160 163 public final static long HIERARCHY_EVENT_MASK = 0x8000; 164 165 168 public final static long HIERARCHY_BOUNDS_EVENT_MASK = 0x10000; 169 170 174 public final static long MOUSE_WHEEL_EVENT_MASK = 0x20000; 175 176 180 public final static long WINDOW_STATE_EVENT_MASK = 0x40000; 181 182 186 public final static long WINDOW_FOCUS_EVENT_MASK = 0x80000; 187 188 192 public final static int RESERVED_ID_MAX = 1999; 193 194 private static Field inputEvent_CanAccessSystemClipboard_Field = null; 196 197 200 private static final long serialVersionUID = -1825314779160409405L; 201 202 static { 203 204 Toolkit.loadLibraries(); 205 if (!GraphicsEnvironment.isHeadless()) { 206 initIDs(); 207 } 208 } 209 210 private static synchronized Field get_InputEvent_CanAccessSystemClipboard() { 211 if (inputEvent_CanAccessSystemClipboard_Field == null) { 212 inputEvent_CanAccessSystemClipboard_Field = 213 (Field )java.security.AccessController.doPrivileged( 214 new java.security.PrivilegedAction () { 215 public Object run() { 216 Field field = null; 217 try { 218 field = InputEvent.class. 219 getDeclaredField("canAccessSystemClipboard"); 220 field.setAccessible(true); 221 return field; 222 } catch (SecurityException e) { 223 } catch (NoSuchFieldException e) { 224 } 225 return null; 226 } 227 }); 228 } 229 230 return inputEvent_CanAccessSystemClipboard_Field; 231 } 232 233 237 private static native void initIDs(); 238 239 243 public AWTEvent(Event event) { 244 this(event.target, event.id); 245 } 246 247 252 public AWTEvent(Object 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 277 public void setSource(Object newSource) { 278 if (source == newSource) { 279 return; 280 } 281 282 Component comp = null; 283 if (newSource instanceof Component ) { 284 comp = (Component )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 307 public int getID() { 308 return id; 309 } 310 311 314 public String toString() { 315 String srcName = null; 316 if (source instanceof Component ) { 317 srcName = ((Component )source).getName(); 318 } else if (source instanceof MenuComponent ) { 319 srcName = ((MenuComponent )source).getName(); 320 } 321 return getClass().getName() + "[" + paramString() + "] on " + 322 (srcName != null? srcName : source); 323 } 324 325 334 public String paramString() { 335 return ""; 336 } 337 338 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 } 360 } 361 362 365 protected boolean isConsumed() { 366 return consumed; 367 } 368 369 378 Event convertToOld() { 379 Object 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; } 396 return new Event (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 Event olde = new Event (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 (src, Event.GOT_FOCUS, null); 417 418 case FocusEvent.FOCUS_LOST: 419 return new Event (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 (src, newid, null); 425 426 case ComponentEvent.COMPONENT_MOVED: 427 if (src instanceof Frame || src instanceof Dialog ) { 428 Point p = ((Component )src).getLocation(); 429 return new Event (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 cmd; 436 if (src instanceof Button ) { 437 cmd = ((Button )src).getLabel(); 438 } else if (src instanceof MenuItem ) { 439 cmd = ((MenuItem )src).getLabel(); 440 } else { 441 cmd = ae.getActionCommand(); 442 } 443 return new Event (src, 0, newid, 0, 0, 0, ae.getModifiers(), cmd); 444 445 case ItemEvent.ITEM_STATE_CHANGED: 446 ItemEvent ie = (ItemEvent)this; 447 Object arg; 448 if (src instanceof List ) { 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 ) { 455 arg = ie.getItem(); 456 457 } else { arg = new Boolean (ie.getStateChange() == ItemEvent.SELECTED); 459 } 460 } 461 return new Event (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 (src, newid, new Integer (aje.getValue())); 490 491 default: 492 } 493 return null; 494 } 495 496 502 void copyPrivateDataInto(AWTEvent that) { 503 that.bdata = this.bdata; 504 if (this instanceof InputEvent && that instanceof InputEvent) { 506 Field field = get_InputEvent_CanAccessSystemClipboard(); 507 if (field != null) { 508 try { 509 boolean b = field.getBoolean(this); 510 field.setBoolean(that, b); 511 } catch(IllegalAccessException e) { 512 } 513 } 514 } 515 } 516 517 void dispatched() { 518 if (this instanceof InputEvent) { 519 Field field = get_InputEvent_CanAccessSystemClipboard(); 520 if (field != null) { 521 try { 522 field.setBoolean(this, false); 523 } catch(IllegalAccessException e) { 524 } 525 } 526 } 527 } 528 } 530 | Popular Tags |