1. /*
  2. * @(#)Component.java 1.357 03/04/14
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.io.PrintStream;
  9. import java.io.PrintWriter;
  10. import java.util.Vector;
  11. import java.util.Locale;
  12. import java.util.EventListener;
  13. import java.util.Iterator;
  14. import java.util.HashSet;
  15. import java.util.Set;
  16. import java.util.Collections;
  17. import java.awt.peer.ComponentPeer;
  18. import java.awt.peer.ContainerPeer;
  19. import java.awt.peer.LightweightPeer;
  20. import java.awt.image.BufferStrategy;
  21. import java.awt.image.ImageObserver;
  22. import java.awt.image.ImageProducer;
  23. import java.awt.image.ColorModel;
  24. import java.awt.image.VolatileImage;
  25. import java.awt.event.*;
  26. import java.awt.datatransfer.Transferable;
  27. import java.awt.dnd.DnDConstants;
  28. import java.awt.dnd.DragSource;
  29. import java.awt.dnd.DragSourceContext;
  30. import java.awt.dnd.DragSourceListener;
  31. import java.awt.dnd.InvalidDnDOperationException;
  32. import java.io.Serializable;
  33. import java.io.ObjectOutputStream;
  34. import java.io.ObjectInputStream;
  35. import java.io.IOException;
  36. import java.beans.PropertyChangeListener;
  37. import java.awt.event.InputMethodListener;
  38. import java.awt.event.InputMethodEvent;
  39. import java.awt.im.InputContext;
  40. import java.awt.im.InputMethodRequests;
  41. import java.awt.dnd.DropTarget;
  42. import javax.accessibility.*;
  43. import java.awt.GraphicsConfiguration;
  44. import javax.accessibility.*;
  45. import sun.security.action.GetPropertyAction;
  46. import sun.awt.AppContext;
  47. import sun.awt.SunToolkit;
  48. import sun.awt.ConstrainableGraphics;
  49. import sun.awt.DebugHelper;
  50. import sun.awt.WindowClosingListener;
  51. import sun.awt.WindowClosingSupport;
  52. import sun.awt.GlobalCursorManager;
  53. import sun.awt.dnd.SunDropTargetEvent;
  54. import sun.awt.im.CompositionArea;
  55. /**
  56. * A <em>component</em> is an object having a graphical representation
  57. * that can be displayed on the screen and that can interact with the
  58. * user. Examples of components are the buttons, checkboxes, and scrollbars
  59. * of a typical graphical user interface. <p>
  60. * The <code>Component</code> class is the abstract superclass of
  61. * the nonmenu-related Abstract Window Toolkit components. Class
  62. * <code>Component</code> can also be extended directly to create a
  63. * lightweight component. A lightweight component is a component that is
  64. * not associated with a native opaque window.
  65. * <p>
  66. * <h3>Serialization</h3>
  67. * It is important to note that only AWT listeners which conform
  68. * to the <code>Serializable</code> protocol will be saved when
  69. * the object is stored. If an AWT object has listeners that
  70. * aren't marked serializable, they will be dropped at
  71. * <code>writeObject</code> time. Developers will need, as always,
  72. * to consider the implications of making an object serializable.
  73. * One situation to watch out for is this:
  74. * <pre>
  75. * import java.awt.*;
  76. * import java.awt.event.*;
  77. * import java.io.Serializable;
  78. *
  79. * class MyApp implements ActionListener, Serializable
  80. * {
  81. * BigObjectThatShouldNotBeSerializedWithAButton bigOne;
  82. * Button aButton = new Button();
  83. *
  84. * MyApp()
  85. * {
  86. * // Oops, now aButton has a listener with a reference
  87. * // to bigOne!
  88. * aButton.addActionListener(this);
  89. * }
  90. *
  91. * public void actionPerformed(ActionEvent e)
  92. * {
  93. * System.out.println("Hello There");
  94. * }
  95. * }
  96. * </pre>
  97. * In this example, serializing <code>aButton</code> by itself
  98. * will cause <code>MyApp</code> and everything it refers to
  99. * to be serialized as well. The problem is that the listener
  100. * is serializable by coincidence, not by design. To separate
  101. * the decisions about <code>MyApp</code> and the
  102. * <code>ActionListener</code> being serializable one can use a
  103. * nested class, as in the following example:
  104. * <pre>
  105. * import java.awt.*;
  106. * import java.awt.event.*;
  107. * import java.io.Serializable;
  108. *
  109. * class MyApp java.io.Serializable
  110. * {
  111. * BigObjectThatShouldNotBeSerializedWithAButton bigOne;
  112. * Button aButton = new Button();
  113. *
  114. * class MyActionListener implements ActionListener
  115. * {
  116. * public void actionPerformed(ActionEvent e)
  117. * {
  118. * System.out.println("Hello There");
  119. * }
  120. * }
  121. *
  122. * MyApp()
  123. * {
  124. * aButton.addActionListener(new MyActionListener());
  125. * }
  126. * }
  127. * </pre>
  128. * <p>
  129. * <b>Note</b>: For more information on the paint mechanisms utilitized
  130. * by AWT and Swing, including information on how to write the most
  131. * efficient painting code, see
  132. * <a href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html">Painting in AWT and Swing</a>.
  133. *
  134. * @version 1.357, 04/14/03
  135. * @author Arthur van Hoff
  136. * @author Sami Shaio
  137. */
  138. public abstract class Component implements ImageObserver, MenuContainer,
  139. Serializable
  140. {
  141. /**
  142. * The peer of the component. The peer implements the component's
  143. * behavior. The peer is set when the <code>Component</code> is
  144. * added to a container that also is a peer.
  145. * @see #addNotify
  146. * @see #removeNotify
  147. */
  148. transient ComponentPeer peer;
  149. /**
  150. * The parent of the object. It may be <code>null</code>
  151. * for top-level components.
  152. * @see #getParent
  153. */
  154. transient Container parent;
  155. /**
  156. * The <code>AppContext</code> of the component. This is set in
  157. * the constructor and never changes.
  158. */
  159. transient AppContext appContext;
  160. /**
  161. * The x position of the component in the parent's coordinate system.
  162. *
  163. * @serial
  164. * @see #getLocation
  165. */
  166. int x;
  167. /**
  168. * The y position of the component in the parent's coordinate system.
  169. *
  170. * @serial
  171. * @see #getLocation
  172. */
  173. int y;
  174. /**
  175. * The width of the component.
  176. *
  177. * @serial
  178. * @see #getSize
  179. */
  180. int width;
  181. /**
  182. * The height of the component.
  183. *
  184. * @serial
  185. * @see #getSize
  186. */
  187. int height;
  188. /**
  189. * The foreground color for this component.
  190. * <code>foreground</code> can be <code>null</code>.
  191. *
  192. * @serial
  193. * @see #getForeground
  194. * @see #setForeground
  195. */
  196. Color foreground;
  197. /**
  198. * The background color for this component.
  199. * <code>background</code> can be <code>null</code>.
  200. *
  201. * @serial
  202. * @see #getBackground
  203. * @see #setBackground
  204. */
  205. Color background;
  206. /**
  207. * The font used by this component.
  208. * The <code>font</code> can be <code>null</code>.
  209. *
  210. * @serial
  211. * @see #getFont
  212. * @see #setFont
  213. */
  214. Font font;
  215. /**
  216. * The font which the peer is currently using.
  217. * (<code>null</code> if no peer exists.)
  218. */
  219. Font peerFont;
  220. /**
  221. * The cursor displayed when pointer is over this component.
  222. * This value can be <code>null</code>.
  223. *
  224. * @serial
  225. * @see #getCursor
  226. * @see #setCursor
  227. */
  228. Cursor cursor;
  229. /**
  230. * The locale for the component.
  231. *
  232. * @serial
  233. * @see #getLocale
  234. * @see #setLocale
  235. */
  236. Locale locale;
  237. /**
  238. * A reference to a <code>GraphicsConfiguration</code> object
  239. * used to describe the characteristics of a graphics
  240. * destination.
  241. * This value can be <code>null</code>.
  242. *
  243. * @since 1.3
  244. * @serial
  245. * @see GraphicsConfiguration
  246. * @see #getGraphicsConfiguration
  247. */
  248. transient GraphicsConfiguration graphicsConfig = null;
  249. /**
  250. * A reference to a <code>BufferStrategy</code> object
  251. * used to manipulate the buffers on this component.
  252. *
  253. * @since 1.4
  254. * @see java.awt.image.BufferStrategy
  255. * @see #getBufferStrategy()
  256. */
  257. transient BufferStrategy bufferStrategy = null;
  258. /**
  259. * True when the object should ignore all repaint events.
  260. *
  261. * @since 1.4
  262. * @serial
  263. * @see #setIgnoreRepaint
  264. * @see #getIgnoreRepaint
  265. */
  266. boolean ignoreRepaint = false;
  267. /**
  268. * True when the object is visible. An object that is not
  269. * visible is not drawn on the screen.
  270. *
  271. * @serial
  272. * @see #isVisible
  273. * @see #setVisible
  274. */
  275. boolean visible = true;
  276. /**
  277. * True when the object is enabled. An object that is not
  278. * enabled does not interact with the user.
  279. *
  280. * @serial
  281. * @see #isEnabled
  282. * @see #setEnabled
  283. */
  284. boolean enabled = true;
  285. /**
  286. * True when the object is valid. An invalid object needs to
  287. * be layed out. This flag is set to false when the object
  288. * size is changed.
  289. *
  290. * @serial
  291. * @see #isValid
  292. * @see #validate
  293. * @see #invalidate
  294. */
  295. boolean valid = false;
  296. /**
  297. * The <code>DropTarget</code> associated with this component.
  298. *
  299. * @since 1.2
  300. * @serial
  301. * @see #setDropTarget
  302. * @see #getDropTarget
  303. */
  304. DropTarget dropTarget;
  305. /**
  306. * @serial
  307. * @see #add
  308. */
  309. Vector popups;
  310. /**
  311. * A component's name.
  312. * This field can be <code>null</code>.
  313. *
  314. * @serial
  315. * @see #getName
  316. * @see #setName(String)
  317. */
  318. private String name;
  319. /**
  320. * A bool to determine whether the name has
  321. * been set explicitly. <code>nameExplicitlySet</code> will
  322. * be false if the name has not been set and
  323. * true if it has.
  324. *
  325. * @serial
  326. * @see #getName
  327. * @see #setName(String)
  328. */
  329. private boolean nameExplicitlySet = false;
  330. /**
  331. * Indicates whether this Component can be focused.
  332. *
  333. * @serial
  334. * @see #setFocusable
  335. * @see #isFocusable
  336. * @since 1.4
  337. */
  338. private boolean focusable = true;
  339. private static final int FOCUS_TRAVERSABLE_UNKNOWN = 0;
  340. private static final int FOCUS_TRAVERSABLE_DEFAULT = 1;
  341. private static final int FOCUS_TRAVERSABLE_SET = 2;
  342. /**
  343. * Tracks whether this Component is relying on default focus travesability.
  344. *
  345. * @serial
  346. * @since 1.4
  347. */
  348. private int isFocusTraversableOverridden = FOCUS_TRAVERSABLE_UNKNOWN;
  349. /**
  350. * The focus traversal keys. These keys will generate focus traversal
  351. * behavior for Components for which focus traversal keys are enabled. If a
  352. * value of null is specified for a traversal key, this Component inherits
  353. * that traversal key from its parent. If all ancestors of this Component
  354. * have null specified for that traversal key, then the current
  355. * KeyboardFocusManager's default traversal key is used.
  356. *
  357. * @serial
  358. * @see #setFocusTraversalKeys
  359. * @see #getFocusTraversalKeys
  360. * @since 1.4
  361. */
  362. Set[] focusTraversalKeys;
  363. private static final String[] focusTraversalKeyPropertyNames = {
  364. "forwardFocusTraversalKeys",
  365. "backwardFocusTraversalKeys",
  366. "upCycleFocusTraversalKeys",
  367. "downCycleFocusTraversalKeys"
  368. };
  369. /**
  370. * Indicates whether focus traversal keys are enabled for this Component.
  371. * Components for which focus traversal keys are disabled receive key
  372. * events for focus traversal keys. Components for which focus traversal
  373. * keys are enabled do not see these events; instead, the events are
  374. * automatically converted to traversal operations.
  375. *
  376. * @serial
  377. * @see #setFocusTraversalKeysEnabled
  378. * @see #getFocusTraversalKeysEnabled
  379. * @since 1.4
  380. */
  381. private boolean focusTraversalKeysEnabled = true;
  382. /**
  383. * The locking object for AWT component-tree and layout operations.
  384. *
  385. * @see #getTreeLock
  386. */
  387. static final Object LOCK = new AWTTreeLock();
  388. static class AWTTreeLock {}
  389. /**
  390. * Internal, cached size information.
  391. * (This field perhaps should have been transient).
  392. *
  393. * @serial
  394. */
  395. Dimension minSize;
  396. /**
  397. * Internal, cached size information
  398. * (This field perhaps should have been transient).
  399. *
  400. * @serial
  401. */
  402. Dimension prefSize;
  403. /**
  404. * The orientation for this component.
  405. * @see #getComponentOrientation
  406. * @see #setComponentOrientation
  407. */
  408. transient ComponentOrientation componentOrientation
  409. = ComponentOrientation.UNKNOWN;
  410. /**
  411. * <code>newEventsOnly</code> will be true if the event is
  412. * one of the event types enabled for the component.
  413. * It will then allow for normal processing to
  414. * continue. If it is false the event is passed
  415. * to the component's parent and up the ancestor
  416. * tree until the event has been consumed.
  417. *
  418. * @serial
  419. * @see #dispatchEvent
  420. */
  421. boolean newEventsOnly = false;
  422. transient ComponentListener componentListener;
  423. transient FocusListener focusListener;
  424. transient HierarchyListener hierarchyListener;
  425. transient HierarchyBoundsListener hierarchyBoundsListener;
  426. transient KeyListener keyListener;
  427. transient MouseListener mouseListener;
  428. transient MouseMotionListener mouseMotionListener;
  429. transient MouseWheelListener mouseWheelListener;
  430. transient InputMethodListener inputMethodListener;
  431. transient RuntimeException windowClosingException = null;
  432. /** Internal, constants for serialization */
  433. final static String actionListenerK = "actionL";
  434. final static String adjustmentListenerK = "adjustmentL";
  435. final static String componentListenerK = "componentL";
  436. final static String containerListenerK = "containerL";
  437. final static String focusListenerK = "focusL";
  438. final static String itemListenerK = "itemL";
  439. final static String keyListenerK = "keyL";
  440. final static String mouseListenerK = "mouseL";
  441. final static String mouseMotionListenerK = "mouseMotionL";
  442. final static String mouseWheelListenerK = "mouseWheelL";
  443. final static String textListenerK = "textL";
  444. final static String ownedWindowK = "ownedL";
  445. final static String windowListenerK = "windowL";
  446. final static String inputMethodListenerK = "inputMethodL";
  447. final static String hierarchyListenerK = "hierarchyL";
  448. final static String hierarchyBoundsListenerK = "hierarchyBoundsL";
  449. final static String windowStateListenerK = "windowStateL";
  450. final static String windowFocusListenerK = "windowFocusL";
  451. /**
  452. * The <code>eventMask</code> is ONLY set by subclasses via
  453. * <code>enableEvents</code>.
  454. * The mask should NOT be set when listeners are registered
  455. * so that we can distinguish the difference between when
  456. * listeners request events and subclasses request them.
  457. * One bit is used to indicate whether input methods are
  458. * enabled; this bit is set by <code>enableInputMethods</code> and is
  459. * on by default.
  460. *
  461. * @serial
  462. * @see #enableInputMethods
  463. * @see AWTEvent
  464. */
  465. long eventMask = AWTEvent.INPUT_METHODS_ENABLED_MASK;
  466. private static final DebugHelper dbg = DebugHelper.create(Component.class);
  467. /**
  468. * Static properties for incremental drawing.
  469. * @see #imageUpdate
  470. */
  471. static boolean isInc;
  472. static int incRate;
  473. static {
  474. /* ensure that the necessary native libraries are loaded */
  475. Toolkit.loadLibraries();
  476. /* initialize JNI field and method ids */
  477. if (!GraphicsEnvironment.isHeadless()) {
  478. initIDs();
  479. }
  480. String s = (String) java.security.AccessController.doPrivileged(
  481. new GetPropertyAction("awt.image.incrementaldraw"));
  482. isInc = (s == null || s.equals("true"));
  483. s = (String) java.security.AccessController.doPrivileged(
  484. new GetPropertyAction("awt.image.redrawrate"));
  485. incRate = (s != null) ? Integer.parseInt(s) : 100;
  486. }
  487. /**
  488. * Ease-of-use constant for <code>getAlignmentY()</code>.
  489. * Specifies an alignment to the top of the component.
  490. * @see #getAlignmentY
  491. */
  492. public static final float TOP_ALIGNMENT = 0.0f;
  493. /**
  494. * Ease-of-use constant for <code>getAlignmentY</code> and
  495. * <code>getAlignmentX</code>. Specifies an alignment to
  496. * the center of the component
  497. * @see #getAlignmentX
  498. * @see #getAlignmentY
  499. */
  500. public static final float CENTER_ALIGNMENT = 0.5f;
  501. /**
  502. * Ease-of-use constant for <code>getAlignmentY</code>.
  503. * Specifies an alignment to the bottom of the component.
  504. * @see #getAlignmentY
  505. */
  506. public static final float BOTTOM_ALIGNMENT = 1.0f;
  507. /**
  508. * Ease-of-use constant for <code>getAlignmentX</code>.
  509. * Specifies an alignment to the left side of the component.
  510. * @see #getAlignmentX
  511. */
  512. public static final float LEFT_ALIGNMENT = 0.0f;
  513. /**
  514. * Ease-of-use constant for <code>getAlignmentX</code>.
  515. * Specifies an alignment to the right side of the component.
  516. * @see #getAlignmentX
  517. */
  518. public static final float RIGHT_ALIGNMENT = 1.0f;
  519. /*
  520. * JDK 1.1 serialVersionUID
  521. */
  522. private static final long serialVersionUID = -7644114512714619750L;
  523. /**
  524. * If any <code>PropertyChangeListeners</code> have been registered,
  525. * the <code>changeSupport</code> field describes them.
  526. *
  527. * @serial
  528. * @since 1.2
  529. * @see #addPropertyChangeListener
  530. * @see #removePropertyChangeListener
  531. * @see #firePropertyChange
  532. */
  533. private java.beans.PropertyChangeSupport changeSupport;
  534. boolean isPacked = false;
  535. /**
  536. * This object is used as a key for internal hashtables.
  537. */
  538. transient private Object privateKey = new Object();
  539. /**
  540. * Constructs a new component. Class <code>Component</code> can be
  541. * extended directly to create a lightweight component that does not
  542. * utilize an opaque native window. A lightweight component must be
  543. * hosted by a native container somewhere higher up in the component
  544. * tree (for example, by a <code>Frame</code> object).
  545. */
  546. protected Component() {
  547. appContext = AppContext.getAppContext();
  548. SunToolkit.insertTargetMapping(this, appContext);
  549. }
  550. void initializeFocusTraversalKeys() {
  551. focusTraversalKeys = new Set[3];
  552. }
  553. /**
  554. * Constructs a name for this component. Called by <code>getName</code>
  555. * when the name is <code>null</code>.
  556. */
  557. String constructComponentName() {
  558. return null; // For strict compliance with prior platform versions, a Component
  559. // that doesn't set its name should return null from
  560. // getName()
  561. }
  562. /**
  563. * Gets the name of the component.
  564. * @return this component's name
  565. * @see #setName
  566. * @since JDK1.1
  567. */
  568. public String getName() {
  569. if (name == null && !nameExplicitlySet) {
  570. synchronized(this) {
  571. if (name == null && !nameExplicitlySet)
  572. name = constructComponentName();
  573. }
  574. }
  575. return name;
  576. }
  577. /**
  578. * Sets the name of the component to the specified string.
  579. * @param name the string that is to be this
  580. * component's name
  581. * @see #getName
  582. * @since JDK1.1
  583. */
  584. public void setName(String name) {
  585. synchronized(this) {
  586. this.name = name;
  587. nameExplicitlySet = true;
  588. }
  589. }
  590. /**
  591. * Gets the parent of this component.
  592. * @return the parent container of this component
  593. * @since JDK1.0
  594. */
  595. public Container getParent() {
  596. return getParent_NoClientCode();
  597. }
  598. // NOTE: This method may be called by privileged threads.
  599. // This functionality is implemented in a package-private method
  600. // to insure that it cannot be overridden by client subclasses.
  601. // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  602. final Container getParent_NoClientCode() {
  603. return parent;
  604. }
  605. /**
  606. * @deprecated As of JDK version 1.1,
  607. * programs should not directly manipulate peers;
  608. * replaced by <code>boolean isDisplayable()</code>.
  609. */
  610. public ComponentPeer getPeer() {
  611. return peer;
  612. }
  613. /**
  614. * Associate a <code>DropTarget</code> with this component.
  615. * The <code>Component</code> will receive drops only if it
  616. * is enabled.
  617. *
  618. * @see #isEnabled
  619. * @param dt The DropTarget
  620. */
  621. public synchronized void setDropTarget(DropTarget dt) {
  622. if (dt == dropTarget || (dropTarget != null && dropTarget.equals(dt)))
  623. return;
  624. DropTarget old;
  625. if ((old = dropTarget) != null) {
  626. if (peer != null) dropTarget.removeNotify(peer);
  627. DropTarget t = dropTarget;
  628. dropTarget = null;
  629. try {
  630. t.setComponent(null);
  631. } catch (IllegalArgumentException iae) {
  632. // ignore it.
  633. }
  634. }
  635. // if we have a new one, and we have a peer, add it!
  636. if ((dropTarget = dt) != null) {
  637. try {
  638. dropTarget.setComponent(this);
  639. if (peer != null) dropTarget.addNotify(peer);
  640. } catch (IllegalArgumentException iae) {
  641. if (old != null) {
  642. try {
  643. old.setComponent(this);
  644. if (peer != null) dropTarget.addNotify(peer);
  645. } catch (IllegalArgumentException iae1) {
  646. // ignore it!
  647. }
  648. }
  649. }
  650. }
  651. }
  652. /**
  653. * Gets the <code>DropTarget</code> associated with this
  654. * <code>Component</code>.
  655. */
  656. public synchronized DropTarget getDropTarget() { return dropTarget; }
  657. /**
  658. * Gets the <code>GraphicsConfiguration</code> associated with this
  659. * <code>Component</code>.
  660. * If the <code>Component</code> has not been assigned a specific
  661. * <code>GraphicsConfiguration</code>,
  662. * the <code>GraphicsConfiguration</code> of the
  663. * <code>Component</code> object's top-level container is
  664. * returned.
  665. * If the <code>Component</code> has been created, but not yet added
  666. * to a <code>Container</code>, this method returns <code>null</code>.
  667. *
  668. * @return the <code>GraphicsConfiguration</code> used by this
  669. * <code>Component</code> or <code>null</code>
  670. * @since 1.3
  671. */
  672. public GraphicsConfiguration getGraphicsConfiguration() {
  673. synchronized(getTreeLock()) {
  674. if (graphicsConfig != null) {
  675. return graphicsConfig;
  676. } else if (getParent() != null) {
  677. return getParent().getGraphicsConfiguration();
  678. } else {
  679. return null;
  680. }
  681. }
  682. }
  683. /**
  684. * Resets this <code>Component</code>'s
  685. * <code>GraphicsConfiguration</code> back to a default
  686. * value. For most componenets, this is <code>null</code>.
  687. * Called from the Toolkit thread, so NO CLIENT CODE.
  688. */
  689. void resetGC() {
  690. synchronized(getTreeLock()) {
  691. graphicsConfig = null;
  692. }
  693. }
  694. /*
  695. * Not called on Component, but needed for Canvas and Window
  696. */
  697. void setGCFromPeer() {
  698. synchronized(getTreeLock()) {
  699. if (peer != null) { // can't imagine how this will be false,
  700. // but just in case
  701. graphicsConfig = peer.getGraphicsConfiguration();
  702. } else {
  703. graphicsConfig = null;
  704. }
  705. }
  706. }
  707. /**
  708. * Checks that this component's <code>GraphicsDevice</code>
  709. * <code>idString</code> matches the string argument.
  710. */
  711. void checkGD(String stringID) {
  712. if (graphicsConfig != null) {
  713. if (!graphicsConfig.getDevice().getIDstring().equals(stringID)) {
  714. throw new IllegalArgumentException(
  715. "adding a container to a container on a different GraphicsDevice");
  716. }
  717. }
  718. }
  719. /**
  720. * Gets this component's locking object (the object that owns the thread
  721. * sychronization monitor) for AWT component-tree and layout
  722. * operations.
  723. * @return this component's locking object
  724. */
  725. public final Object getTreeLock() {
  726. return LOCK;
  727. }
  728. /**
  729. * Gets the toolkit of this component. Note that
  730. * the frame that contains a component controls which
  731. * toolkit is used by that component. Therefore if the component
  732. * is moved from one frame to another, the toolkit it uses may change.
  733. * @return the toolkit of this component
  734. * @since JDK1.0
  735. */
  736. public Toolkit getToolkit() {
  737. return getToolkitImpl();
  738. }
  739. /*
  740. * This is called by the native code, so client code can't
  741. * be called on the toolkit thread.
  742. */
  743. final Toolkit getToolkitImpl() {
  744. ComponentPeer peer = this.peer;
  745. if ((peer != null) && ! (peer instanceof LightweightPeer)){
  746. return peer.getToolkit();
  747. }
  748. Container parent = this.parent;
  749. if (parent != null) {
  750. return parent.getToolkitImpl();
  751. }
  752. return Toolkit.getDefaultToolkit();
  753. }
  754. /**
  755. * Determines whether this component is valid. A component is valid
  756. * when it is correctly sized and positioned within its parent
  757. * container and all its children are also valid. Components are
  758. * invalidated when they are first shown on the screen.
  759. * @return <code>true</code> if the component is valid, <code>false</code>
  760. * otherwise
  761. * @see #validate
  762. * @see #invalidate
  763. * @since JDK1.0
  764. */
  765. public boolean isValid() {
  766. return (peer != null) && valid;
  767. }
  768. /**
  769. * Determines whether this component is displayable. A component is
  770. * displayable when it is connected to a native screen resource.
  771. * <p>
  772. * A component is made displayable either when it is added to
  773. * a displayable containment hierarchy or when its containment
  774. * hierarchy is made displayable.
  775. * A containment hierarchy is made displayable when its ancestor
  776. * window is either packed or made visible.
  777. * <p>
  778. * A component is made undisplayable either when it is removed from
  779. * a displayable containment hierarchy or when its containment hierarchy
  780. * is made undisplayable. A containment hierarchy is made
  781. * undisplayable when its ancestor window is disposed.
  782. *
  783. * @return <code>true</code> if the component is displayable,
  784. * <code>false</code> otherwise
  785. * @see Container#add(Component)
  786. * @see Window#pack
  787. * @see Window#show
  788. * @see Container#remove(Component)
  789. * @see Window#dispose
  790. * @since 1.2
  791. */
  792. public boolean isDisplayable() {
  793. return getPeer() != null;
  794. }
  795. /**
  796. * Determines whether this component should be visible when its
  797. * parent is visible. Components are
  798. * initially visible, with the exception of top level components such
  799. * as <code>Frame</code> objects.
  800. * @return <code>true</code> if the component is visible,
  801. * <code>false</code> otherwise
  802. * @see #setVisible
  803. * @since JDK1.0
  804. */
  805. public boolean isVisible() {
  806. return visible;
  807. }
  808. /**
  809. * Determines whether this component will be displayed on the screen
  810. * if it's displayable.
  811. * @return <code>true</code> if the component and all of its ancestors
  812. * are visible, <code>false</code> otherwise
  813. */
  814. boolean isRecursivelyVisible() {
  815. return visible && (parent == null || parent.isRecursivelyVisible());
  816. }
  817. /**
  818. * Determines whether this component is showing on screen. This means
  819. * that the component must be visible, and it must be in a container
  820. * that is visible and showing.
  821. * @return <code>true</code> if the component is showing,
  822. * <code>false</code> otherwise
  823. * @see #setVisible
  824. * @since JDK1.0
  825. */
  826. public boolean isShowing() {
  827. if (visible && (peer != null)) {
  828. Container parent = this.parent;
  829. return (parent == null) || parent.isShowing();
  830. }
  831. return false;
  832. }
  833. /**
  834. * Determines whether this component is enabled. An enabled component
  835. * can respond to user input and generate events. Components are
  836. * enabled initially by default. A component may be enabled or disabled by
  837. * calling its <code>setEnabled</code> method.
  838. * @return <code>true</code> if the component is enabled,
  839. * <code>false</code> otherwise
  840. * @see #setEnabled
  841. * @since JDK1.0
  842. */
  843. public boolean isEnabled() {
  844. return isEnabledImpl();
  845. }
  846. /*
  847. * This is called by the native code, so client code can't
  848. * be called on the toolkit thread.
  849. */
  850. final boolean isEnabledImpl() {
  851. return enabled;
  852. }
  853. /**
  854. * Enables or disables this component, depending on the value of the
  855. * parameter <code>b</code>. An enabled component can respond to user
  856. * input and generate events. Components are enabled initially by default.
  857. *
  858. * <p>Note: Disabling a lightweight component does not prevent it from
  859. * receiving MouseEvents.
  860. *
  861. * @param b If <code>true</code>, this component is
  862. * enabled; otherwise this component is disabled
  863. * @see #isEnabled
  864. * @see #isLightweight
  865. * @since JDK1.1
  866. */
  867. public void setEnabled(boolean b) {
  868. enable(b);
  869. }
  870. /**
  871. * @deprecated As of JDK version 1.1,
  872. * replaced by <code>setEnabled(boolean)</code>.
  873. */
  874. public void enable() {
  875. if (!enabled) {
  876. synchronized (getTreeLock()) {
  877. enabled = true;
  878. ComponentPeer peer = this.peer;
  879. if (peer != null) {
  880. peer.enable();
  881. if (visible) {
  882. updateCursorImmediately();
  883. }
  884. }
  885. }
  886. if (accessibleContext != null) {
  887. accessibleContext.firePropertyChange(
  888. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  889. null, AccessibleState.ENABLED);
  890. }
  891. }
  892. }
  893. /**
  894. * @deprecated As of JDK version 1.1,
  895. * replaced by <code>setEnabled(boolean)</code>.
  896. */
  897. public void enable(boolean b) {
  898. if (b) {
  899. enable();
  900. } else {
  901. disable();
  902. }
  903. }
  904. /**
  905. * @deprecated As of JDK version 1.1,
  906. * replaced by <code>setEnabled(boolean)</code>.
  907. */
  908. public void disable() {
  909. if (enabled) {
  910. KeyboardFocusManager.clearMostRecentFocusOwner(this);
  911. synchronized (getTreeLock()) {
  912. enabled = false;
  913. if (isFocusOwner()) {
  914. // Don't clear the global focus owner. If transferFocus
  915. // fails, we want the focus to stay on the disabled
  916. // Component so that keyboard traversal, et. al. still
  917. // makes sense to the user.
  918. autoTransferFocus(false);
  919. }
  920. ComponentPeer peer = this.peer;
  921. if (peer != null) {
  922. peer.disable();
  923. if (visible) {
  924. updateCursorImmediately();
  925. }
  926. }
  927. }
  928. if (accessibleContext != null) {
  929. accessibleContext.firePropertyChange(
  930. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  931. null, AccessibleState.ENABLED);
  932. }
  933. }
  934. }
  935. /**
  936. * Returns true if this component is painted to an offscreen image
  937. * ("buffer") that's copied to the screen later. Component
  938. * subclasses that support double buffering should override this
  939. * method to return true if double buffering is enabled.
  940. *
  941. * @return false by default
  942. */
  943. public boolean isDoubleBuffered() {
  944. return false;
  945. }
  946. /**
  947. * Enables or disables input method support for this component. If input
  948. * method support is enabled and the component also processes key events,
  949. * incoming events are offered to
  950. * the current input method and will only be processed by the component or
  951. * dispatched to its listeners if the input method does not consume them.
  952. * By default, input method support is enabled.
  953. *
  954. * @param enable true to enable, false to disable
  955. * @see #processKeyEvent
  956. * @since 1.2
  957. */
  958. public void enableInputMethods(boolean enable) {
  959. if (enable) {
  960. if ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0)
  961. return;
  962. // If this component already has focus, then activate the
  963. // input method by dispatching a synthesized focus gained
  964. // event.
  965. if (isFocusOwner()) {
  966. InputContext inputContext = getInputContext();
  967. if (inputContext != null) {
  968. FocusEvent focusGainedEvent =
  969. new FocusEvent(this, FocusEvent.FOCUS_GAINED);
  970. inputContext.dispatchEvent(focusGainedEvent);
  971. }
  972. }
  973. eventMask |= AWTEvent.INPUT_METHODS_ENABLED_MASK;
  974. } else {
  975. if (areInputMethodsEnabled()) {
  976. InputContext inputContext = getInputContext();
  977. if (inputContext != null) {
  978. inputContext.endComposition();
  979. inputContext.removeNotify(this);
  980. }
  981. }
  982. eventMask &= ~AWTEvent.INPUT_METHODS_ENABLED_MASK;
  983. }
  984. }
  985. /**
  986. * Shows or hides this component depending on the value of parameter
  987. * <code>b</code>.
  988. * @param b if <code>true</code>, shows this component;
  989. * otherwise, hides this component
  990. * @see #isVisible
  991. * @since JDK1.1
  992. */
  993. public void setVisible(boolean b) {
  994. show(b);
  995. }
  996. /**
  997. * @deprecated As of JDK version 1.1,
  998. * replaced by <code>setVisible(boolean)</code>.
  999. */
  1000. public void show() {
  1001. if (!visible) {
  1002. synchronized (getTreeLock()) {
  1003. visible = true;
  1004. ComponentPeer peer = this.peer;
  1005. if (peer != null) {
  1006. peer.show();
  1007. createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
  1008. this, parent,
  1009. HierarchyEvent.SHOWING_CHANGED,
  1010. Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
  1011. if (peer instanceof LightweightPeer) {
  1012. repaint();
  1013. }
  1014. updateCursorImmediately();
  1015. }
  1016. if (componentListener != null ||
  1017. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
  1018. Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
  1019. ComponentEvent e = new ComponentEvent(this,
  1020. ComponentEvent.COMPONENT_SHOWN);
  1021. Toolkit.getEventQueue().postEvent(e);
  1022. }
  1023. }
  1024. Container parent = this.parent;
  1025. if (parent != null) {
  1026. parent.invalidate();
  1027. }
  1028. }
  1029. }
  1030. /**
  1031. * @deprecated As of JDK version 1.1,
  1032. * replaced by <code>setVisible(boolean)</code>.
  1033. */
  1034. public void show(boolean b) {
  1035. if (b) {
  1036. show();
  1037. } else {
  1038. isPacked = false;
  1039. hide();
  1040. }
  1041. }
  1042. boolean containsFocus() {
  1043. return isFocusOwner();
  1044. }
  1045. void clearMostRecentFocusOwnerOnHide() {
  1046. KeyboardFocusManager.clearMostRecentFocusOwner(this);
  1047. }
  1048. void clearCurrentFocusCycleRootOnHide() {
  1049. /* do nothing */
  1050. }
  1051. /**
  1052. * @deprecated As of JDK version 1.1,
  1053. * replaced by <code>setVisible(boolean)</code>.
  1054. */
  1055. public void hide() {
  1056. if (visible) {
  1057. clearCurrentFocusCycleRootOnHide();
  1058. clearMostRecentFocusOwnerOnHide();
  1059. synchronized (getTreeLock()) {
  1060. visible = false;
  1061. if (containsFocus()) {
  1062. autoTransferFocus(true);
  1063. }
  1064. ComponentPeer peer = this.peer;
  1065. if (peer != null) {
  1066. peer.hide();
  1067. createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
  1068. this, parent,
  1069. HierarchyEvent.SHOWING_CHANGED,
  1070. Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
  1071. if (peer instanceof LightweightPeer) {
  1072. repaint();
  1073. }
  1074. updateCursorImmediately();
  1075. }
  1076. if (componentListener != null ||
  1077. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
  1078. Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
  1079. ComponentEvent e = new ComponentEvent(this,
  1080. ComponentEvent.COMPONENT_HIDDEN);
  1081. Toolkit.getEventQueue().postEvent(e);
  1082. }
  1083. }
  1084. Container parent = this.parent;
  1085. if (parent != null) {
  1086. parent.invalidate();
  1087. }
  1088. }
  1089. }
  1090. /**
  1091. * Gets the foreground color of this component.
  1092. * @return this component's foreground color; if this component does
  1093. * not have a foreground color, the foreground color of its parent
  1094. * is returned
  1095. * @see #setForeground
  1096. * @since JDK1.0
  1097. * @beaninfo
  1098. * bound: true
  1099. */
  1100. public Color getForeground() {
  1101. Color foreground = this.foreground;
  1102. if (foreground != null) {
  1103. return foreground;
  1104. }
  1105. Container parent = this.parent;
  1106. return (parent != null) ? parent.getForeground() : null;
  1107. }
  1108. /**
  1109. * Sets the foreground color of this component.
  1110. * @param c the color to become this component's
  1111. * foreground color; if this parameter is <code>null</code>
  1112. * then this component will inherit
  1113. * the foreground color of its parent
  1114. * @see #getForeground
  1115. * @since JDK1.0
  1116. */
  1117. public void setForeground(Color c) {
  1118. Color oldColor = foreground;
  1119. ComponentPeer peer = this.peer;
  1120. foreground = c;
  1121. if (peer != null) {
  1122. c = getForeground();
  1123. if (c != null) {
  1124. peer.setForeground(c);
  1125. }
  1126. }
  1127. // This is a bound property, so report the change to
  1128. // any registered listeners. (Cheap if there are none.)
  1129. firePropertyChange("foreground", oldColor, c);
  1130. }
  1131. /**
  1132. * Returns whether the foreground color has been explicitly set for this
  1133. * Component. If this method returns <code>false</code>, this Component is
  1134. * inheriting its foreground color from an ancestor.
  1135. *
  1136. * @return <code>true</code> if the foreground color has been explicitly
  1137. * set for this Component; <code>false</code> otherwise.
  1138. * @since 1.4
  1139. */
  1140. public boolean isForegroundSet() {
  1141. return (foreground != null);
  1142. }
  1143. /**
  1144. * Gets the background color of this component.
  1145. * @return this component's background color; if this component does
  1146. * not have a background color,
  1147. * the background color of its parent is returned
  1148. * @see #setBackground
  1149. * @since JDK1.0
  1150. */
  1151. public Color getBackground() {
  1152. Color background = this.background;
  1153. if (background != null) {
  1154. return background;
  1155. }
  1156. Container parent = this.parent;
  1157. return (parent != null) ? parent.getBackground() : null;
  1158. }
  1159. /**
  1160. * Sets the background color of this component.
  1161. * <p>
  1162. * The background color affects each component differently and the
  1163. * parts of the component that are affected by the background color
  1164. * may differ between operating systems.
  1165. *
  1166. * @param c the color to become this component's color;
  1167. * if this parameter is <code>null</code>, then this
  1168. * component will inherit the background color of its parent
  1169. * @see #getBackground
  1170. * @since JDK1.0
  1171. * @beaninfo
  1172. * bound: true
  1173. */
  1174. public void setBackground(Color c) {
  1175. Color oldColor = background;
  1176. ComponentPeer peer = this.peer;
  1177. background = c;
  1178. if (peer != null) {
  1179. c = getBackground();
  1180. if (c != null) {
  1181. peer.setBackground(c);
  1182. }
  1183. }
  1184. // This is a bound property, so report the change to
  1185. // any registered listeners. (Cheap if there are none.)
  1186. firePropertyChange("background", oldColor, c);
  1187. }
  1188. /**
  1189. * Returns whether the background color has been explicitly set for this
  1190. * Component. If this method returns <code>false</code>, this Component is
  1191. * inheriting its background color from an ancestor.
  1192. *
  1193. * @return <code>true</code> if the background color has been explicitly
  1194. * set for this Component; <code>false</code> otherwise.
  1195. * @since 1.4
  1196. */
  1197. public boolean isBackgroundSet() {
  1198. return (background != null);
  1199. }
  1200. /**
  1201. * Gets the font of this component.
  1202. * @return this component's font; if a font has not been set
  1203. * for this component, the font of its parent is returned
  1204. * @see #setFont
  1205. * @since JDK1.0
  1206. */
  1207. public Font getFont() {
  1208. return getFont_NoClientCode();
  1209. }
  1210. // NOTE: This method may be called by privileged threads.
  1211. // This functionality is implemented in a package-private method
  1212. // to insure that it cannot be overridden by client subclasses.
  1213. // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  1214. final Font getFont_NoClientCode() {
  1215. Font font = this.font;
  1216. if (font != null) {
  1217. return font;
  1218. }
  1219. Container parent = this.parent;
  1220. return (parent != null) ? parent.getFont_NoClientCode() : null;
  1221. }
  1222. /**
  1223. * Sets the font of this component.
  1224. * @param f the font to become this component's font;
  1225. * if this parameter is <code>null</code> then this
  1226. * component will inherit the font of its parent
  1227. * @see #getFont
  1228. * @since JDK1.0
  1229. * @beaninfo
  1230. * bound: true
  1231. */
  1232. public void setFont(Font f) {
  1233. Font oldFont, newFont;
  1234. synchronized (this) {
  1235. oldFont = font;
  1236. ComponentPeer peer = this.peer;
  1237. newFont = font = f;
  1238. if (peer != null) {
  1239. f = getFont();
  1240. if (f != null) {
  1241. peer.setFont(f);
  1242. peerFont = f;
  1243. }
  1244. }
  1245. }
  1246. // This is a bound property, so report the change to
  1247. // any registered listeners. (Cheap if there are none.)
  1248. firePropertyChange("font", oldFont, newFont);
  1249. // This could change the preferred size of the Component.
  1250. if (valid) {
  1251. invalidate();
  1252. }
  1253. }
  1254. /**
  1255. * Returns whether the font has been explicitly set for this Component. If
  1256. * this method returns <code>false</code>, this Component is inheriting its
  1257. * font from an ancestor.
  1258. *
  1259. * @return <code>true</code> if the font has been explicitly set for this
  1260. *