| 1 7 package java.awt; 8 9 import java.io.PrintStream ; 10 import java.io.PrintWriter ; 11 import java.util.Vector ; 12 import java.util.Locale ; 13 import java.util.EventListener ; 14 import java.util.Iterator ; 15 import java.util.HashSet ; 16 import java.util.Set ; 17 import java.util.Collections ; 18 import java.awt.peer.ComponentPeer; 19 import java.awt.peer.ContainerPeer; 20 import java.awt.peer.LightweightPeer; 21 import java.awt.image.BufferStrategy ; 22 import java.awt.image.ImageObserver ; 23 import java.awt.image.ImageProducer ; 24 import java.awt.image.ColorModel ; 25 import java.awt.image.VolatileImage ; 26 import java.awt.event.*; 27 import java.awt.datatransfer.Transferable ; 28 import java.awt.dnd.DnDConstants ; 29 import java.awt.dnd.DragSource ; 30 import java.awt.dnd.DragSourceContext ; 31 import java.awt.dnd.DragSourceListener ; 32 import java.awt.dnd.InvalidDnDOperationException ; 33 import java.io.Serializable ; 34 import java.io.ObjectOutputStream ; 35 import java.io.ObjectInputStream ; 36 import java.io.IOException ; 37 import java.beans.PropertyChangeListener ; 38 import java.beans.PropertyChangeSupport ; 39 import java.awt.event.InputMethodListener ; 40 import java.awt.event.InputMethodEvent ; 41 import java.awt.im.InputContext ; 42 import java.awt.im.InputMethodRequests ; 43 import java.awt.dnd.DropTarget ; 44 import java.lang.reflect.InvocationTargetException ; 45 import java.lang.reflect.Method ; 46 import javax.accessibility.*; 47 import java.awt.GraphicsConfiguration ; 48 import java.security.AccessController ; 49 import java.security.PrivilegedAction ; 50 import javax.accessibility.*; 51 52 import java.util.logging.*; 53 54 import sun.security.action.GetPropertyAction; 55 import sun.awt.AppContext; 56 import sun.awt.SunToolkit; 57 import sun.awt.ConstrainableGraphics; 58 import sun.awt.DebugHelper; 59 import sun.awt.WindowClosingListener; 60 import sun.awt.WindowClosingSupport; 61 import sun.awt.GlobalCursorManager; 62 import sun.awt.dnd.SunDropTargetEvent; 63 import sun.awt.im.CompositionArea; 64 65 import sun.font.FontManager; 66 67 157 public abstract class Component implements ImageObserver , MenuContainer , 158 Serializable  159 { 160 161 private static final Logger focusLog = Logger.getLogger("java.awt.focus.Component"); 162 private static final Logger log = Logger.getLogger("java.awt.Component"); 163 170 transient ComponentPeer peer; 171 172 177 transient Container parent; 178 179 183 transient AppContext appContext; 184 185 191 int x; 192 193 199 int y; 200 201 207 int width; 208 209 215 int height; 216 217 225 Color foreground; 226 227 235 Color background; 236 237 245 Font font; 246 247 251 Font peerFont; 252 253 261 Cursor cursor; 262 263 270 Locale locale; 271 272 283 transient GraphicsConfiguration graphicsConfig = null; 284 285 293 transient BufferStrategy bufferStrategy = null; 294 295 303 boolean ignoreRepaint = false; 304 305 313 boolean visible = true; 314 315 323 boolean enabled = true; 324 325 335 boolean valid = false; 336 337 345 DropTarget dropTarget; 346 347 351 Vector popups; 352 353 361 private String name; 362 363 373 private boolean nameExplicitlySet = false; 374 375 383 private boolean focusable = true; 384 385 private static final int FOCUS_TRAVERSABLE_UNKNOWN = 0; 386 private static final int FOCUS_TRAVERSABLE_DEFAULT = 1; 387 private static final int FOCUS_TRAVERSABLE_SET = 2; 388 389 395 private int isFocusTraversableOverridden = FOCUS_TRAVERSABLE_UNKNOWN; 396 397 410 Set [] focusTraversalKeys; 411 412 private static final String [] focusTraversalKeyPropertyNames = { 413 "forwardFocusTraversalKeys", 414 "backwardFocusTraversalKeys", 415 "upCycleFocusTraversalKeys", 416 "downCycleFocusTraversalKeys" 417 }; 418 419 431 private boolean focusTraversalKeysEnabled = true; 432 433 438 static final Object LOCK = new AWTTreeLock(); 439 static class AWTTreeLock {} 440 441 447 Dimension minSize; 448 449 452 boolean minSizeSet; 453 454 460 Dimension prefSize; 461 462 465 boolean prefSizeSet; 466 467 472 Dimension maxSize; 473 474 477 boolean maxSizeSet; 478 479 484 transient ComponentOrientation componentOrientation 485 = ComponentOrientation.UNKNOWN; 486 487 498 boolean newEventsOnly = false; 499 transient ComponentListener componentListener; 500 transient FocusListener focusListener; 501 transient HierarchyListener hierarchyListener; 502 transient HierarchyBoundsListener hierarchyBoundsListener; 503 transient KeyListener keyListener; 504 transient MouseListener mouseListener; 505 transient MouseMotionListener mouseMotionListener; 506 transient MouseWheelListener mouseWheelListener; 507 transient InputMethodListener inputMethodListener; 508 509 transient RuntimeException windowClosingException = null; 510 511 512 final static String actionListenerK = "actionL"; 513 final static String adjustmentListenerK = "adjustmentL"; 514 final static String componentListenerK = "componentL"; 515 final static String containerListenerK = "containerL"; 516 final static String focusListenerK = "focusL"; 517 final static String itemListenerK = "itemL"; 518 final static String keyListenerK = "keyL"; 519 final static String mouseListenerK = "mouseL"; 520 final static String mouseMotionListenerK = "mouseMotionL"; 521 final static String mouseWheelListenerK = "mouseWheelL"; 522 final static String textListenerK = "textL"; 523 final static String ownedWindowK = "ownedL"; 524 final static String windowListenerK = "windowL"; 525 final static String inputMethodListenerK = "inputMethodL"; 526 final static String hierarchyListenerK = "hierarchyL"; 527 final static String hierarchyBoundsListenerK = "hierarchyBoundsL"; 528 final static String windowStateListenerK = "windowStateL"; 529 final static String windowFocusListenerK = "windowFocusL"; 530 531 545 long eventMask = AWTEvent.INPUT_METHODS_ENABLED_MASK; 546 547 private static final DebugHelper dbg = DebugHelper.create(Component .class); 548 549 553 static boolean isInc; 554 static int incRate; 555 static { 556 557 Toolkit.loadLibraries(); 558 559 if (!GraphicsEnvironment.isHeadless()) { 560 initIDs(); 561 } 562 563 String s = (String ) java.security.AccessController.doPrivileged( 564 new GetPropertyAction("awt.image.incrementaldraw")); 565 isInc = (s == null || s.equals("true")); 566 567 s = (String ) java.security.AccessController.doPrivileged( 568 new GetPropertyAction("awt.image.redrawrate")); 569 incRate = (s != null) ? Integer.parseInt(s) : 100; 570 } 571 572 577 public static final float TOP_ALIGNMENT = 0.0f; 578 579 586 public static final float CENTER_ALIGNMENT = 0.5f; 587 588 593 public static final float BOTTOM_ALIGNMENT = 1.0f; 594 595 600 public static final float LEFT_ALIGNMENT = 0.0f; 601 602 607 public static final float RIGHT_ALIGNMENT = 1.0f; 608 609 612 private static final long serialVersionUID = -7644114512714619750L; 613 614 624 private PropertyChangeSupport changeSupport; 625 626 boolean isPacked = false; 627 628 631 transient private Object privateKey = new Object (); 632 633 639 private int boundsOp = ComponentPeer.DEFAULT_OPERATION; 640 641 645 int getBoundsOp() { 646 assert Thread.holdsLock(getTreeLock()); 647 return boundsOp; 648 } 649 650 void setBoundsOp(int op) { 651 assert Thread.holdsLock(getTreeLock()); 652 if (op == ComponentPeer.RESET_OPERATION) { 653 boundsOp = ComponentPeer.DEFAULT_OPERATION; 654 } else 655 if (boundsOp == ComponentPeer.DEFAULT_OPERATION) { 656 boundsOp = op; 657 } 658 } 659 660 667 protected Component() { 668 appContext = AppContext.getAppContext(); 669 } 670 671 void initializeFocusTraversalKeys() { 672 focusTraversalKeys = new Set [3]; 673 } 674 675 679 String constructComponentName() { 680 return null; } 684 685 691 public String getName() { 692 if (name == null && !nameExplicitlySet) { 693 synchronized(this) { 694 if (name == null && !nameExplicitlySet) 695 name = constructComponentName(); 696 } 697 } 698 return name; 699 } 700 701 708 public void setName(String name) { 709 String oldName; 710 synchronized(this) { 711 oldName = this.name; 712 this.name = name; 713 nameExplicitlySet = true; 714 } 715 firePropertyChange("name", oldName, name); 716 } 717 718 723 public Container getParent() { 724 return getParent_NoClientCode(); 725 } 726 727 final Container getParent_NoClientCode() { 732 return parent; 733 } 734 735 740 @Deprecated  741 public ComponentPeer getPeer() { 742 return peer; 743 } 744 745 753 754 public synchronized void setDropTarget(DropTarget dt) { 755 if (dt == dropTarget || (dropTarget != null && dropTarget.equals(dt))) 756 return; 757 758 DropTarget old; 759 760 if ((old = dropTarget) != null) { 761 if (peer != null) dropTarget.removeNotify(peer); 762 763 DropTarget t = dropTarget; 764 765 dropTarget = null; 766 767 try { 768 t.setComponent(null); 769 } catch (IllegalArgumentException iae) { 770 } 772 } 773 774 776 if ((dropTarget = dt) != null) { 777 try { 778 dropTarget.setComponent(this); 779 if (peer != null) dropTarget.addNotify(peer); 780 } catch (IllegalArgumentException iae) { 781 if (old != null) { 782 try { 783 old.setComponent(this); 784 if (peer != null) dropTarget.addNotify(peer); 785 } catch (IllegalArgumentException iae1) { 786 } 788 } 789 } 790 } 791 } 792 793 797 798 public synchronized DropTarget getDropTarget() { return dropTarget; } 799 800 815 public GraphicsConfiguration getGraphicsConfiguration() { 816 synchronized(getTreeLock()) { 817 if (graphicsConfig != null) { 818 return graphicsConfig; 819 } else if (getParent() != null) { 820 return getParent().getGraphicsConfiguration(); 821 } else { 822 return null; 823 } 824 } 825 } 826 827 833 void resetGC() { 834 synchronized(getTreeLock()) { 835 graphicsConfig = null; 836 } 837 } 838 839 842 void setGCFromPeer() { 843 synchronized(getTreeLock()) { 844 if (peer != null) { graphicsConfig = peer.getGraphicsConfiguration(); 847 } else { 848 graphicsConfig = null; 849 } 850 } 851 } 852 853 857 void checkGD(String stringID) { 858 if (graphicsConfig != null) { 859 if (!graphicsConfig.getDevice().getIDstring().equals(stringID)) { 860 throw new IllegalArgumentException ( 861 "adding a container to a container on a different GraphicsDevice"); 862 } 863 } 864 } 865 866 872 public final Object getTreeLock() { 873 return LOCK; 874 } 875 876 884 public Toolkit getToolkit() { 885 return getToolkitImpl(); 886 } 887 888 892 final Toolkit getToolkitImpl() { 893 ComponentPeer peer = this.peer; 894 if ((peer != null) && ! (peer instanceof LightweightPeer)){ 895 return peer.getToolkit(); 896 } 897 Container parent = this.parent; 898 if (parent != null) { 899 return parent.getToolkitImpl(); 900 } 901 return Toolkit.getDefaultToolkit(); 902 } 903 904 917 public boolean isValid() { 918 return (peer != null) && valid; 919 } 920 921 945 public boolean isDisplayable() { 946 return getPeer() != null; 947 } 948 949 959 public boolean isVisible() { 960 return visible; 961 } 962 963 969 boolean isRecursivelyVisible() { 970 return visible && (parent == null || parent.isRecursivelyVisible()); 971 } 972 973 977 Point pointRelativeToComponent(Point absolute) { 978 Point compCoords = getLocationOnScreen(); 979 return new Point (absolute.x - comp
|