KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > Component


1 /*
2  * @(#)Component.java 1.392 07/03/13
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.awt;
8
9 import java.io.PrintStream JavaDoc;
10 import java.io.PrintWriter JavaDoc;
11 import java.util.Vector JavaDoc;
12 import java.util.Locale JavaDoc;
13 import java.util.EventListener JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Set JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.awt.peer.ComponentPeer;
19 import java.awt.peer.ContainerPeer;
20 import java.awt.peer.LightweightPeer;
21 import java.awt.image.BufferStrategy JavaDoc;
22 import java.awt.image.ImageObserver JavaDoc;
23 import java.awt.image.ImageProducer JavaDoc;
24 import java.awt.image.ColorModel JavaDoc;
25 import java.awt.image.VolatileImage JavaDoc;
26 import java.awt.event.*;
27 import java.awt.datatransfer.Transferable JavaDoc;
28 import java.awt.dnd.DnDConstants JavaDoc;
29 import java.awt.dnd.DragSource JavaDoc;
30 import java.awt.dnd.DragSourceContext JavaDoc;
31 import java.awt.dnd.DragSourceListener JavaDoc;
32 import java.awt.dnd.InvalidDnDOperationException JavaDoc;
33 import java.io.Serializable JavaDoc;
34 import java.io.ObjectOutputStream JavaDoc;
35 import java.io.ObjectInputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.beans.PropertyChangeListener JavaDoc;
38 import java.beans.PropertyChangeSupport JavaDoc;
39 import java.awt.event.InputMethodListener JavaDoc;
40 import java.awt.event.InputMethodEvent JavaDoc;
41 import java.awt.im.InputContext JavaDoc;
42 import java.awt.im.InputMethodRequests JavaDoc;
43 import java.awt.dnd.DropTarget JavaDoc;
44 import java.lang.reflect.InvocationTargetException JavaDoc;
45 import java.lang.reflect.Method JavaDoc;
46 import javax.accessibility.*;
47 import java.awt.GraphicsConfiguration JavaDoc;
48 import java.security.AccessController JavaDoc;
49 import java.security.PrivilegedAction JavaDoc;
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 /**
68  * A <em>component</em> is an object having a graphical representation
69  * that can be displayed on the screen and that can interact with the
70  * user. Examples of components are the buttons, checkboxes, and scrollbars
71  * of a typical graphical user interface. <p>
72  * The <code>Component</code> class is the abstract superclass of
73  * the nonmenu-related Abstract Window Toolkit components. Class
74  * <code>Component</code> can also be extended directly to create a
75  * lightweight component. A lightweight component is a component that is
76  * not associated with a native opaque window.
77  * <p>
78  * <h3>Serialization</h3>
79  * It is important to note that only AWT listeners which conform
80  * to the <code>Serializable</code> protocol will be saved when
81  * the object is stored. If an AWT object has listeners that
82  * aren't marked serializable, they will be dropped at
83  * <code>writeObject</code> time. Developers will need, as always,
84  * to consider the implications of making an object serializable.
85  * One situation to watch out for is this:
86  * <pre>
87  * import java.awt.*;
88  * import java.awt.event.*;
89  * import java.io.Serializable;
90  *
91  * class MyApp implements ActionListener, Serializable
92  * {
93  * BigObjectThatShouldNotBeSerializedWithAButton bigOne;
94  * Button aButton = new Button();
95  *
96  * MyApp()
97  * {
98  * // Oops, now aButton has a listener with a reference
99  * // to bigOne!
100  * aButton.addActionListener(this);
101  * }
102  *
103  * public void actionPerformed(ActionEvent e)
104  * {
105  * System.out.println("Hello There");
106  * }
107  * }
108  * </pre>
109  * In this example, serializing <code>aButton</code> by itself
110  * will cause <code>MyApp</code> and everything it refers to
111  * to be serialized as well. The problem is that the listener
112  * is serializable by coincidence, not by design. To separate
113  * the decisions about <code>MyApp</code> and the
114  * <code>ActionListener</code> being serializable one can use a
115  * nested class, as in the following example:
116  * <pre>
117  * import java.awt.*;
118  * import java.awt.event.*;
119  * import java.io.Serializable;
120  *
121  * class MyApp java.io.Serializable
122  * {
123  * BigObjectThatShouldNotBeSerializedWithAButton bigOne;
124  * Button aButton = new Button();
125  *
126  * class MyActionListener implements ActionListener
127  * {
128  * public void actionPerformed(ActionEvent e)
129  * {
130  * System.out.println("Hello There");
131  * }
132  * }
133  *
134  * MyApp()
135  * {
136  * aButton.addActionListener(new MyActionListener());
137  * }
138  * }
139  * </pre>
140  * <p>
141  * <b>Note</b>: For more information on the paint mechanisms utilitized
142  * by AWT and Swing, including information on how to write the most
143  * efficient painting code, see
144  * <a HREF="http://java.sun.com/products/jfc/tsc/articles/painting/index.html">Painting in AWT and Swing</a>.
145  * <p>
146  * For details on the focus subsystem, see
147  * <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
148  * How to Use the Focus Subsystem</a>,
149  * a section in <em>The Java Tutorial</em>, and the
150  * <a HREF="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
151  * for more information.
152  *
153  * @version 1.392, 03/13/07
154  * @author Arthur van Hoff
155  * @author Sami Shaio
156  */

157 public abstract class Component implements ImageObserver JavaDoc, MenuContainer JavaDoc,
158                                            Serializable JavaDoc
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     /**
164      * The peer of the component. The peer implements the component's
165      * behavior. The peer is set when the <code>Component</code> is
166      * added to a container that also is a peer.
167      * @see #addNotify
168      * @see #removeNotify
169      */

170     transient ComponentPeer peer;
171
172     /**
173      * The parent of the object. It may be <code>null</code>
174      * for top-level components.
175      * @see #getParent
176      */

177     transient Container JavaDoc parent;
178
179     /**
180      * The <code>AppContext</code> of the component. Applets/Plugin may
181      * change the AppContext.
182      */

183     transient AppContext appContext;
184
185     /**
186      * The x position of the component in the parent's coordinate system.
187      *
188      * @serial
189      * @see #getLocation
190      */

191     int x;
192
193     /**
194      * The y position of the component in the parent's coordinate system.
195      *
196      * @serial
197      * @see #getLocation
198      */

199     int y;
200
201     /**
202      * The width of the component.
203      *
204      * @serial
205      * @see #getSize
206      */

207     int width;
208
209     /**
210      * The height of the component.
211      *
212      * @serial
213      * @see #getSize
214      */

215     int height;
216
217     /**
218      * The foreground color for this component.
219      * <code>foreground</code> can be <code>null</code>.
220      *
221      * @serial
222      * @see #getForeground
223      * @see #setForeground
224      */

225     Color JavaDoc foreground;
226
227     /**
228      * The background color for this component.
229      * <code>background</code> can be <code>null</code>.
230      *
231      * @serial
232      * @see #getBackground
233      * @see #setBackground
234      */

235     Color JavaDoc background;
236
237     /**
238      * The font used by this component.
239      * The <code>font</code> can be <code>null</code>.
240      *
241      * @serial
242      * @see #getFont
243      * @see #setFont
244      */

245     Font JavaDoc font;
246
247     /**
248      * The font which the peer is currently using.
249      * (<code>null</code> if no peer exists.)
250      */

251     Font JavaDoc peerFont;
252
253     /**
254      * The cursor displayed when pointer is over this component.
255      * This value can be <code>null</code>.
256      *
257      * @serial
258      * @see #getCursor
259      * @see #setCursor
260      */

261     Cursor JavaDoc cursor;
262
263     /**
264      * The locale for the component.
265      *
266      * @serial
267      * @see #getLocale
268      * @see #setLocale
269      */

270     Locale JavaDoc locale;
271
272     /**
273      * A reference to a <code>GraphicsConfiguration</code> object
274      * used to describe the characteristics of a graphics
275      * destination.
276      * This value can be <code>null</code>.
277      *
278      * @since 1.3
279      * @serial
280      * @see GraphicsConfiguration
281      * @see #getGraphicsConfiguration
282      */

283     transient GraphicsConfiguration JavaDoc graphicsConfig = null;
284         
285     /**
286      * A reference to a <code>BufferStrategy</code> object
287      * used to manipulate the buffers on this component.
288      *
289      * @since 1.4
290      * @see java.awt.image.BufferStrategy
291      * @see #getBufferStrategy()
292      */

293     transient BufferStrategy JavaDoc bufferStrategy = null;
294         
295     /**
296      * True when the object should ignore all repaint events.
297      *
298      * @since 1.4
299      * @serial
300      * @see #setIgnoreRepaint
301      * @see #getIgnoreRepaint
302      */

303     boolean ignoreRepaint = false;
304
305     /**
306      * True when the object is visible. An object that is not
307      * visible is not drawn on the screen.
308      *
309      * @serial
310      * @see #isVisible
311      * @see #setVisible
312      */

313     boolean visible = true;
314
315     /**
316      * True when the object is enabled. An object that is not
317      * enabled does not interact with the user.
318      *
319      * @serial
320      * @see #isEnabled
321      * @see #setEnabled
322      */

323     boolean enabled = true;
324
325     /**
326      * True when the object is valid. An invalid object needs to
327      * be layed out. This flag is set to false when the object
328      * size is changed.
329      *
330      * @serial
331      * @see #isValid
332      * @see #validate
333      * @see #invalidate
334      */

335     boolean valid = false;
336
337     /**
338      * The <code>DropTarget</code> associated with this component.
339      *
340      * @since 1.2
341      * @serial
342      * @see #setDropTarget
343      * @see #getDropTarget
344      */

345     DropTarget JavaDoc dropTarget;
346
347     /**
348      * @serial
349      * @see #add
350      */

351     Vector JavaDoc popups;
352
353     /**
354      * A component's name.
355      * This field can be <code>null</code>.
356      *
357      * @serial
358      * @see #getName
359      * @see #setName(String)
360      */

361     private String JavaDoc name;
362   
363     /**
364      * A bool to determine whether the name has
365      * been set explicitly. <code>nameExplicitlySet</code> will
366      * be false if the name has not been set and
367      * true if it has.
368      *
369      * @serial
370      * @see #getName
371      * @see #setName(String)
372      */

373     private boolean nameExplicitlySet = false;
374
375     /**
376      * Indicates whether this Component can be focused.
377      *
378      * @serial
379      * @see #setFocusable
380      * @see #isFocusable
381      * @since 1.4
382      */

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     /**
390      * Tracks whether this Component is relying on default focus travesability.
391      *
392      * @serial
393      * @since 1.4
394      */

395     private int isFocusTraversableOverridden = FOCUS_TRAVERSABLE_UNKNOWN;
396
397     /**
398      * The focus traversal keys. These keys will generate focus traversal
399      * behavior for Components for which focus traversal keys are enabled. If a
400      * value of null is specified for a traversal key, this Component inherits
401      * that traversal key from its parent. If all ancestors of this Component
402      * have null specified for that traversal key, then the current
403      * KeyboardFocusManager's default traversal key is used.
404      *
405      * @serial
406      * @see #setFocusTraversalKeys
407      * @see #getFocusTraversalKeys
408      * @since 1.4
409      */

410     Set JavaDoc[] focusTraversalKeys;
411
412     private static final String JavaDoc[] focusTraversalKeyPropertyNames = {
413         "forwardFocusTraversalKeys",
414         "backwardFocusTraversalKeys",
415         "upCycleFocusTraversalKeys",
416         "downCycleFocusTraversalKeys"
417     };
418
419     /**
420      * Indicates whether focus traversal keys are enabled for this Component.
421      * Components for which focus traversal keys are disabled receive key
422      * events for focus traversal keys. Components for which focus traversal
423      * keys are enabled do not see these events; instead, the events are
424      * automatically converted to traversal operations.
425      *
426      * @serial
427      * @see #setFocusTraversalKeysEnabled
428      * @see #getFocusTraversalKeysEnabled
429      * @since 1.4
430      */

431     private boolean focusTraversalKeysEnabled = true;
432
433     /**
434      * The locking object for AWT component-tree and layout operations.
435      *
436      * @see #getTreeLock
437      */

438     static final Object JavaDoc LOCK = new AWTTreeLock();
439     static class AWTTreeLock {}
440
441     /**
442      * Minimum size.
443      * (This field perhaps should have been transient).
444      *
445      * @serial
446      */

447     Dimension JavaDoc minSize;
448
449     /**
450      * Whether or not setMinimumSize has been invoked with a non-null value.
451      */

452     boolean minSizeSet;
453
454     /**
455      * Preferred size.
456      * (This field perhaps should have been transient).
457      *
458      * @serial
459      */

460     Dimension JavaDoc prefSize;
461
462     /**
463      * Whether or not setPreferredSize has been invoked with a non-null value.
464      */

465     boolean prefSizeSet;
466
467     /**
468      * Maximum size
469      *
470      * @serial
471      */

472     Dimension JavaDoc maxSize;
473
474     /**
475      * Whether or not setMaximumSize has been invoked with a non-null value.
476      */

477     boolean maxSizeSet;
478
479     /**
480      * The orientation for this component.
481      * @see #getComponentOrientation
482      * @see #setComponentOrientation
483      */

484     transient ComponentOrientation JavaDoc componentOrientation
485     = ComponentOrientation.UNKNOWN;
486
487     /**
488      * <code>newEventsOnly</code> will be true if the event is
489      * one of the event types enabled for the component.
490      * It will then allow for normal processing to
491      * continue. If it is false the event is passed
492      * to the component's parent and up the ancestor
493      * tree until the event has been consumed.
494      *
495      * @serial
496      * @see #dispatchEvent
497      */

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 JavaDoc inputMethodListener;
508     
509     transient RuntimeException JavaDoc windowClosingException = null;
510
511     /** Internal, constants for serialization */
512     final static String JavaDoc actionListenerK = "actionL";
513     final static String JavaDoc adjustmentListenerK = "adjustmentL";
514     final static String JavaDoc componentListenerK = "componentL";
515     final static String JavaDoc containerListenerK = "containerL";
516     final static String JavaDoc focusListenerK = "focusL";
517     final static String JavaDoc itemListenerK = "itemL";
518     final static String JavaDoc keyListenerK = "keyL";
519     final static String JavaDoc mouseListenerK = "mouseL";
520     final static String JavaDoc mouseMotionListenerK = "mouseMotionL";
521     final static String JavaDoc mouseWheelListenerK = "mouseWheelL";
522     final static String JavaDoc textListenerK = "textL";
523     final static String JavaDoc ownedWindowK = "ownedL";
524     final static String JavaDoc windowListenerK = "windowL";
525     final static String JavaDoc inputMethodListenerK = "inputMethodL";
526     final static String JavaDoc hierarchyListenerK = "hierarchyL";
527     final static String JavaDoc hierarchyBoundsListenerK = "hierarchyBoundsL";
528     final static String JavaDoc windowStateListenerK = "windowStateL";
529     final static String JavaDoc windowFocusListenerK = "windowFocusL";
530
531     /**
532      * The <code>eventMask</code> is ONLY set by subclasses via
533      * <code>enableEvents</code>.
534      * The mask should NOT be set when listeners are registered
535      * so that we can distinguish the difference between when
536      * listeners request events and subclasses request them.
537      * One bit is used to indicate whether input methods are
538      * enabled; this bit is set by <code>enableInputMethods</code> and is
539      * on by default.
540      *
541      * @serial
542      * @see #enableInputMethods
543      * @see AWTEvent
544      */

545     long eventMask = AWTEvent.INPUT_METHODS_ENABLED_MASK;
546
547     private static final DebugHelper dbg = DebugHelper.create(Component JavaDoc.class);
548
549     /**
550      * Static properties for incremental drawing.
551      * @see #imageUpdate
552      */

553     static boolean isInc;
554     static int incRate;
555     static {
556         /* ensure that the necessary native libraries are loaded */
557         Toolkit.loadLibraries();
558         /* initialize JNI field and method ids */
559         if (!GraphicsEnvironment.isHeadless()) {
560             initIDs();
561         }
562
563         String JavaDoc s = (String JavaDoc) java.security.AccessController.doPrivileged(
564                                                                         new GetPropertyAction("awt.image.incrementaldraw"));
565         isInc = (s == null || s.equals("true"));
566
567         s = (String JavaDoc) java.security.AccessController.doPrivileged(
568                                                                  new GetPropertyAction("awt.image.redrawrate"));
569         incRate = (s != null) ? Integer.parseInt(s) : 100;
570     }
571
572     /**
573      * Ease-of-use constant for <code>getAlignmentY()</code>.
574      * Specifies an alignment to the top of the component.
575      * @see #getAlignmentY
576      */

577     public static final float TOP_ALIGNMENT = 0.0f;
578
579     /**
580      * Ease-of-use constant for <code>getAlignmentY</code> and
581      * <code>getAlignmentX</code>. Specifies an alignment to
582      * the center of the component
583      * @see #getAlignmentX
584      * @see #getAlignmentY
585      */

586     public static final float CENTER_ALIGNMENT = 0.5f;
587
588     /**
589      * Ease-of-use constant for <code>getAlignmentY</code>.
590      * Specifies an alignment to the bottom of the component.
591      * @see #getAlignmentY
592      */

593     public static final float BOTTOM_ALIGNMENT = 1.0f;
594
595     /**
596      * Ease-of-use constant for <code>getAlignmentX</code>.
597      * Specifies an alignment to the left side of the component.
598      * @see #getAlignmentX
599      */

600     public static final float LEFT_ALIGNMENT = 0.0f;
601
602     /**
603      * Ease-of-use constant for <code>getAlignmentX</code>.
604      * Specifies an alignment to the right side of the component.
605      * @see #getAlignmentX
606      */

607     public static final float RIGHT_ALIGNMENT = 1.0f;
608
609     /*
610      * JDK 1.1 serialVersionUID
611      */

612     private static final long serialVersionUID = -7644114512714619750L;
613
614     /**
615      * If any <code>PropertyChangeListeners</code> have been registered,
616      * the <code>changeSupport</code> field describes them.
617      *
618      * @serial
619      * @since 1.2
620      * @see #addPropertyChangeListener
621      * @see #removePropertyChangeListener
622      * @see #firePropertyChange
623      */

624     private PropertyChangeSupport JavaDoc changeSupport;
625
626     boolean isPacked = false;
627
628     /**
629      * This object is used as a key for internal hashtables.
630      */

631     transient private Object JavaDoc privateKey = new Object JavaDoc();
632
633     /**
634      * Pseudoparameter for direct Geometry API (setLocation, setBounds setSize
635      * to signal setBounds what's changing. Should be used under TreeLock.
636      * This is only needed due to the inability to change the cross-calling
637      * order of public and deprecated methods.
638      */

639     private int boundsOp = ComponentPeer.DEFAULT_OPERATION;
640      
641     /**
642      * Should only be used in subclass getBounds to check that part of bounds
643      * is actualy changing
644      */

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     /**
661      * Constructs a new component. Class <code>Component</code> can be
662      * extended directly to create a lightweight component that does not
663      * utilize an opaque native window. A lightweight component must be
664      * hosted by a native container somewhere higher up in the component
665      * tree (for example, by a <code>Frame</code> object).
666      */

667     protected Component() {
668         appContext = AppContext.getAppContext();
669     }
670
671     void initializeFocusTraversalKeys() {
672         focusTraversalKeys = new Set JavaDoc[3];
673     }
674
675     /**
676      * Constructs a name for this component. Called by <code>getName</code>
677      * when the name is <code>null</code>.
678      */

679     String JavaDoc constructComponentName() {
680         return null; // For strict compliance with prior platform versions, a Component
681
// that doesn't set its name should return null from
682
// getName()
683
}
684
685     /**
686      * Gets the name of the component.
687      * @return this component's name
688      * @see #setName
689      * @since JDK1.1
690      */

691     public String JavaDoc 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     /**
702      * Sets the name of the component to the specified string.
703      * @param name the string that is to be this
704      * component's name
705      * @see #getName
706      * @since JDK1.1
707      */

708     public void setName(String JavaDoc name) {
709         String JavaDoc oldName;
710         synchronized(this) {
711             oldName = this.name;
712             this.name = name;
713             nameExplicitlySet = true;
714         }
715         firePropertyChange("name", oldName, name);
716     }
717
718     /**
719      * Gets the parent of this component.
720      * @return the parent container of this component
721      * @since JDK1.0
722      */

723     public Container JavaDoc getParent() {
724         return getParent_NoClientCode();
725     }
726
727     // NOTE: This method may be called by privileged threads.
728
// This functionality is implemented in a package-private method
729
// to insure that it cannot be overridden by client subclasses.
730
// DO NOT INVOKE CLIENT CODE ON THIS THREAD!
731
final Container JavaDoc getParent_NoClientCode() {
732         return parent;
733     }
734
735     /**
736      * @deprecated As of JDK version 1.1,
737      * programs should not directly manipulate peers;
738      * replaced by <code>boolean isDisplayable()</code>.
739      */

740     @Deprecated JavaDoc
741     public ComponentPeer getPeer() {
742         return peer;
743     }
744
745     /**
746      * Associate a <code>DropTarget</code> with this component.
747      * The <code>Component</code> will receive drops only if it
748      * is enabled.
749      *
750      * @see #isEnabled
751      * @param dt The DropTarget
752      */

753
754     public synchronized void setDropTarget(DropTarget JavaDoc dt) {
755         if (dt == dropTarget || (dropTarget != null && dropTarget.equals(dt)))
756             return;
757
758         DropTarget JavaDoc old;
759
760         if ((old = dropTarget) != null) {
761             if (peer != null) dropTarget.removeNotify(peer);
762
763             DropTarget JavaDoc t = dropTarget;
764
765             dropTarget = null;
766
767             try {
768                 t.setComponent(null);
769             } catch (IllegalArgumentException JavaDoc iae) {
770                 // ignore it.
771
}
772         }
773
774         // if we have a new one, and we have a peer, add it!
775

776         if ((dropTarget = dt) != null) {
777             try {
778                 dropTarget.setComponent(this);
779                 if (peer != null) dropTarget.addNotify(peer);
780             } catch (IllegalArgumentException JavaDoc iae) {
781                 if (old != null) {
782                     try {
783                         old.setComponent(this);
784                         if (peer != null) dropTarget.addNotify(peer);
785                     } catch (IllegalArgumentException JavaDoc iae1) {
786                         // ignore it!
787
}
788                 }
789             }
790         }
791     }
792
793     /**
794      * Gets the <code>DropTarget</code> associated with this
795      * <code>Component</code>.
796      */

797
798     public synchronized DropTarget JavaDoc getDropTarget() { return dropTarget; }
799
800     /**
801      * Gets the <code>GraphicsConfiguration</code> associated with this
802      * <code>Component</code>.
803      * If the <code>Component</code> has not been assigned a specific
804      * <code>GraphicsConfiguration</code>,
805      * the <code>GraphicsConfiguration</code> of the
806      * <code>Component</code> object's top-level container is
807      * returned.
808      * If the <code>Component</code> has been created, but not yet added
809      * to a <code>Container</code>, this method returns <code>null</code>.
810      *
811      * @return the <code>GraphicsConfiguration</code> used by this
812      * <code>Component</code> or <code>null</code>
813      * @since 1.3
814      */

815     public GraphicsConfiguration JavaDoc 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     /**
828      * Resets this <code>Component</code>'s
829      * <code>GraphicsConfiguration</code> back to a default
830      * value. For most componenets, this is <code>null</code>.
831      * Called from the Toolkit thread, so NO CLIENT CODE.
832      */

833     void resetGC() {
834         synchronized(getTreeLock()) {
835             graphicsConfig = null;
836         }
837     }
838
839     /*
840      * Not called on Component, but needed for Canvas and Window
841      */

842     void setGCFromPeer() {
843         synchronized(getTreeLock()) {
844             if (peer != null) { // can't imagine how this will be false,
845
// but just in case
846
graphicsConfig = peer.getGraphicsConfiguration();
847             } else {
848                 graphicsConfig = null;
849             }
850         }
851     }
852
853     /**
854      * Checks that this component's <code>GraphicsDevice</code>
855      * <code>idString</code> matches the string argument.
856      */

857     void checkGD(String JavaDoc stringID) {
858         if (graphicsConfig != null) {
859             if (!graphicsConfig.getDevice().getIDstring().equals(stringID)) {
860                 throw new IllegalArgumentException JavaDoc(
861                                                    "adding a container to a container on a different GraphicsDevice");
862             }
863         }
864     }
865         
866     /**
867      * Gets this component's locking object (the object that owns the thread
868      * sychronization monitor) for AWT component-tree and layout
869      * operations.
870      * @return this component's locking object
871      */

872     public final Object JavaDoc getTreeLock() {
873         return LOCK;
874     }
875
876     /**
877      * Gets the toolkit of this component. Note that
878      * the frame that contains a component controls which
879      * toolkit is used by that component. Therefore if the component
880      * is moved from one frame to another, the toolkit it uses may change.
881      * @return the toolkit of this component
882      * @since JDK1.0
883      */

884     public Toolkit JavaDoc getToolkit() {
885         return getToolkitImpl();
886     }
887
888     /*
889      * This is called by the native code, so client code can't
890      * be called on the toolkit thread.
891      */

892     final Toolkit JavaDoc getToolkitImpl() {
893         ComponentPeer peer = this.peer;
894         if ((peer != null) && ! (peer instanceof LightweightPeer)){
895             return peer.getToolkit();
896         }
897         Container JavaDoc parent = this.parent;
898         if (parent != null) {
899             return parent.getToolkitImpl();
900         }
901         return Toolkit.getDefaultToolkit();
902     }
903
904     /**
905      * Determines whether this component is valid. A component is valid
906      * when it is correctly sized and positioned within its parent
907      * container and all its children are also valid.
908      * In order to account for peers' size requirements, components are invalidated
909      * before they are first shown on the screen. By the time the parent container
910      * is fully realized, all its components will be valid.
911      * @return <code>true</code> if the component is valid, <code>false</code>
912      * otherwise
913      * @see #validate
914      * @see #invalidate
915      * @since JDK1.0
916      */

917     public boolean isValid() {
918         return (peer != null) && valid;
919     }
920
921     /**
922      * Determines whether this component is displayable. A component is
923      * displayable when it is connected to a native screen resource.
924      * <p>
925      * A component is made displayable either when it is added to
926      * a displayable containment hierarchy or when its containment
927      * hierarchy is made displayable.
928      * A containment hierarchy is made displayable when its ancestor
929      * window is either packed or made visible.
930      * <p>
931      * A component is made undisplayable either when it is removed from
932      * a displayable containment hierarchy or when its containment hierarchy
933      * is made undisplayable. A containment hierarchy is made
934      * undisplayable when its ancestor window is disposed.
935      *
936      * @return <code>true</code> if the component is displayable,
937      * <code>false</code> otherwise
938      * @see Container#add(Component)
939      * @see Window#pack
940      * @see Window#show
941      * @see Container#remove(Component)
942      * @see Window#dispose
943      * @since 1.2
944      */

945     public boolean isDisplayable() {
946         return getPeer() != null;
947     }
948
949     /**
950      * Determines whether this component should be visible when its
951      * parent is visible. Components are
952      * initially visible, with the exception of top level components such
953      * as <code>Frame</code> objects.
954      * @return <code>true</code> if the component is visible,
955      * <code>false</code> otherwise
956      * @see #setVisible
957      * @since JDK1.0
958      */

959     public boolean isVisible() {
960         return visible;
961     }
962
963     /**
964      * Determines whether this component will be displayed on the screen
965      * if it's displayable.
966      * @return <code>true</code> if the component and all of its ancestors
967      * are visible, <code>false</code> otherwise
968      */

969     boolean isRecursivelyVisible() {
970         return visible && (parent == null || parent.isRecursivelyVisible());
971     }
972
973     /**
974      * Translates absolute coordinates into coordinates in the coordinate
975      * space of this component.
976      */

977     Point JavaDoc pointRelativeToComponent(Point JavaDoc absolute) {
978         Point JavaDoc compCoords = getLocationOnScreen();
979         return new Point JavaDoc(absolute.x - comp