1. /*
  2. * @(#)Component.java 1.265 00/02/28
  3. *
  4. * Copyright 1995-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.awt;
  11. import java.io.PrintStream;
  12. import java.io.PrintWriter;
  13. import java.util.Vector;
  14. import java.util.Locale;
  15. import java.util.EventListener;
  16. import java.awt.peer.ComponentPeer;
  17. import java.awt.image.ImageObserver;
  18. import java.awt.image.ImageProducer;
  19. import java.awt.image.ColorModel;
  20. import java.awt.event.*;
  21. import java.awt.datatransfer.Transferable;
  22. import java.awt.dnd.DnDConstants;
  23. import java.awt.dnd.DragSource;
  24. import java.awt.dnd.DragSourceContext;
  25. import java.awt.dnd.DragSourceListener;
  26. import java.awt.dnd.InvalidDnDOperationException;
  27. import java.io.Serializable;
  28. import java.io.ObjectOutputStream;
  29. import java.io.ObjectInputStream;
  30. import java.io.IOException;
  31. import java.beans.PropertyChangeListener;
  32. import java.awt.event.InputMethodListener;
  33. import java.awt.event.InputMethodEvent;
  34. import java.awt.im.InputContext;
  35. import java.awt.im.InputMethodRequests;
  36. import java.awt.dnd.DropTarget;
  37. import javax.accessibility.*;
  38. import java.awt.GraphicsConfiguration;
  39. import javax.accessibility.*;
  40. import sun.security.action.GetPropertyAction;
  41. import sun.awt.AppContext;
  42. import sun.awt.SunToolkit;
  43. import sun.awt.ConstrainableGraphics;
  44. import sun.awt.DebugHelper;
  45. import sun.awt.WindowClosingListener;
  46. import sun.awt.WindowClosingSupport;
  47. import sun.awt.GlobalCursorManager;
  48. import sun.awt.im.CompositionArea;
  49. /**
  50. * A <em>component</em> is an object having a graphical representation
  51. * that can be displayed on the screen and that can interact with the
  52. * user. Examples of components are the buttons, checkboxes, and scrollbars
  53. * of a typical graphical user interface. <p>
  54. * The <code>Component</code> class is the abstract superclass of
  55. * the nonmenu-related Abstract Window Toolkit components. Class
  56. * <code>Component</code> can also be extended directly to create a
  57. * lightweight component. A lightweight component is a component that is
  58. * not associated with a native opaque window.
  59. *
  60. * @version 1.265, 02/28/00
  61. * @author Arthur van Hoff
  62. * @author Sami Shaio
  63. */
  64. public abstract class Component implements ImageObserver, MenuContainer,
  65. Serializable
  66. {
  67. /**
  68. * The peer of the component. The peer implements the component's
  69. * behaviour. The peer is set when the Component is added to a
  70. * container that also is a peer.
  71. * @see #addNotify
  72. * @see #removeNotify
  73. */
  74. transient ComponentPeer peer;
  75. /**
  76. * The parent of the object. It may be null for top-level components.
  77. * @see #getParent
  78. */
  79. transient Container parent;
  80. /**
  81. * The AppContext of the component. This is set in the constructor
  82. * and never changes.
  83. */
  84. transient AppContext appContext;
  85. /**
  86. * The x position of the component in the parent's coordinate system.
  87. *
  88. * @serial
  89. * @see #getLocation
  90. */
  91. int x;
  92. /**
  93. * The y position of the component in the parent's coordinate system.
  94. *
  95. * @serial
  96. * @see #getLocation
  97. */
  98. int y;
  99. /**
  100. * The width of the component.
  101. *
  102. * @serial
  103. * @see #getSize
  104. */
  105. int width;
  106. /**
  107. * The height of the component.
  108. *
  109. * @serial
  110. * @see #getSize
  111. */
  112. int height;
  113. /**
  114. * The foreground color for this component.
  115. * foreground color can be null.
  116. *
  117. * @serial
  118. * @see #getForeground
  119. * @see #setForeground
  120. */
  121. Color foreground;
  122. /**
  123. * The background color for this component.
  124. * background can be null.
  125. *
  126. * @serial
  127. * @see #getBackground
  128. * @see #setBackground
  129. */
  130. Color background;
  131. /**
  132. * The font used by this component.
  133. * The font can be null.
  134. *
  135. * @serial
  136. * @see #getFont
  137. * @see #setFont
  138. */
  139. Font font;
  140. /**
  141. * The font which the peer is currently using. (null if no peer exists.)
  142. */
  143. Font peerFont;
  144. /**
  145. * The cursor displayed when pointer is over this component.
  146. * This value can be null.
  147. *
  148. * @serial
  149. * @see #getCursor
  150. * @see #setCursor
  151. */
  152. Cursor cursor;
  153. /**
  154. * The locale for the component.
  155. *
  156. * @serial
  157. * @see #getLocale
  158. * @see #setLocale
  159. */
  160. Locale locale;
  161. /**
  162. * A reference to a GraphicsConfiguration object
  163. * used to describe the characteristics of a graphics
  164. * destination.
  165. * This value can be null.
  166. *
  167. * @since 1.3
  168. * @serial
  169. * @see java.awt.GraphicsConfiguration
  170. * @see #getGraphicsConfiguration
  171. */
  172. transient GraphicsConfiguration graphicsConfig = null;
  173. /**
  174. * True when the object is visible. An object that is not
  175. * visible is not drawn on the screen.
  176. *
  177. * @serial
  178. * @see #isVisible
  179. * @see #setVisible
  180. */
  181. boolean visible = true;
  182. /**
  183. * True when the object is enabled. An object that is not
  184. * enabled does not interact with the user.
  185. *
  186. * @serial
  187. * @see #isEnabled
  188. * @see #setEnabled
  189. */
  190. boolean enabled = true;
  191. /**
  192. * True when the object is valid. An invalid object needs to
  193. * be layed out. This flag is set to false when the object
  194. * size is changed.
  195. *
  196. * @serial
  197. * @see #isValid
  198. * @see #validate
  199. * @see #invalidate
  200. */
  201. boolean valid = false;
  202. /**
  203. * The DropTarget associated with this Component.
  204. *
  205. * @since 1.2
  206. * @serial
  207. * @see #setDropTarget
  208. * @see #getDropTarget
  209. */
  210. DropTarget dropTarget;
  211. /**
  212. * True if this component has enabled focus events and currently
  213. * has the focus.
  214. *
  215. * @serial
  216. * @see #hasFocus
  217. * @see #processFocusEvent
  218. */
  219. boolean hasFocus = false;
  220. /**
  221. * @serial
  222. * @see add()
  223. */
  224. Vector popups;
  225. /**
  226. * A components name.
  227. * This field can be null.
  228. *
  229. * @serial
  230. * @see getName()
  231. * @see setName(String)
  232. */
  233. private String name;
  234. /**
  235. * A bool to determine whether the name has
  236. * been set explicitly. nameExplicitlySet will
  237. * be false if the name has not been set and
  238. * true if it has.
  239. *
  240. * @serial
  241. * @see getName()
  242. * @see setName(String)
  243. */
  244. private boolean nameExplicitlySet = false;
  245. /**
  246. * The locking object for AWT component-tree and layout operations.
  247. *
  248. * @see #getTreeLock
  249. */
  250. static final Object LOCK = new AWTTreeLock();
  251. static class AWTTreeLock {}
  252. /**
  253. * Internal, cached size information.
  254. * (This field perhaps should have been transient).
  255. *
  256. * @serial
  257. */
  258. Dimension minSize;
  259. /** Internal, cached size information
  260. * (This field perhaps should have been transient).
  261. *
  262. * @serial
  263. */
  264. Dimension prefSize;
  265. /**
  266. * The orientation for this component.
  267. * @see #getComponentOrientation
  268. * @see #setComponentOrientation
  269. */
  270. transient ComponentOrientation componentOrientation
  271. = ComponentOrientation.UNKNOWN;
  272. /**
  273. * newEventsOnly will be true if the event is
  274. * one of the event types enabled for the component.
  275. * It will then allow for normal processing to
  276. * continue. If it is false the event is passed
  277. * to the components parent and up the ancestor
  278. * tree until the event has been consumed.
  279. *
  280. * @serial
  281. * @see dispatchEvent()
  282. */
  283. boolean newEventsOnly = false;
  284. transient ComponentListener componentListener;
  285. transient FocusListener focusListener;
  286. transient HierarchyListener hierarchyListener;
  287. transient HierarchyBoundsListener hierarchyBoundsListener;
  288. transient KeyListener keyListener;
  289. transient MouseListener mouseListener;
  290. transient MouseMotionListener mouseMotionListener;
  291. transient InputMethodListener inputMethodListener;
  292. transient RuntimeException windowClosingException = null;
  293. /** Internal, constants for serialization */
  294. final static String actionListenerK = "actionL";
  295. final static String adjustmentListenerK = "adjustmentL";
  296. final static String componentListenerK = "componentL";
  297. final static String containerListenerK = "containerL";
  298. final static String focusListenerK = "focusL";
  299. final static String itemListenerK = "itemL";
  300. final static String keyListenerK = "keyL";
  301. final static String mouseListenerK = "mouseL";
  302. final static String mouseMotionListenerK = "mouseMotionL";
  303. final static String textListenerK = "textL";
  304. final static String ownedWindowK = "ownedL";
  305. final static String windowListenerK = "windowL";
  306. final static String inputMethodListenerK = "inputMethodL";
  307. final static String hierarchyListenerK = "hierarchyL";
  308. final static String hierarchyBoundsListenerK = "hierarchyBoundsL";
  309. /**
  310. * The eventMask is ONLY set by subclasses via enableEvents.
  311. * The mask should NOT be set when listeners are registered
  312. * so that we can distinguish the difference between when
  313. * listeners request events and subclasses request them.
  314. * One bit is used to indicate whether input methods are
  315. * enabled; this bit is set by enableInputMethods and is
  316. * on by default.
  317. *
  318. * @serial
  319. * @see enableInputMethods()
  320. */
  321. long eventMask = AWTEvent.INPUT_METHODS_ENABLED_MASK;
  322. private static final DebugHelper dbg = DebugHelper.create(Component.class);
  323. /**
  324. * Static properties for incremental drawing.
  325. * @see #imageUpdate
  326. */
  327. static boolean isInc;
  328. static int incRate;
  329. static {
  330. /* ensure that the necessary native libraries are loaded */
  331. Toolkit.loadLibraries();
  332. /* initialize JNI field and method ids */
  333. initIDs();
  334. String s = (String) java.security.AccessController.doPrivileged(
  335. new GetPropertyAction("awt.image.incrementaldraw"));
  336. isInc = (s == null || s.equals("true"));
  337. s = (String) java.security.AccessController.doPrivileged(
  338. new GetPropertyAction("awt.image.redrawrate"));
  339. incRate = (s != null) ? Integer.parseInt(s) : 100;
  340. }
  341. /**
  342. * Ease-of-use constant for <code>getAlignmentY()</code>. Specifies an
  343. * alignment to the top of the component.
  344. * @see #getAlignmentY
  345. */
  346. public static final float TOP_ALIGNMENT = 0.0f;
  347. /**
  348. * Ease-of-use constant for <code>getAlignmentY</code> and
  349. * <code>getAlignmentX</code>. Specifies an alignment to
  350. * the center of the component
  351. * @see #getAlignmentX
  352. * @see #getAlignmentY
  353. */
  354. public static final float CENTER_ALIGNMENT = 0.5f;
  355. /**
  356. * Ease-of-use constant for <code>getAlignmentY</code>. Specifies an
  357. * alignment to the bottom of the component.
  358. * @see #getAlignmentY
  359. */
  360. public static final float BOTTOM_ALIGNMENT = 1.0f;
  361. /**
  362. * Ease-of-use constant for <code>getAlignmentX</code>. Specifies an
  363. * alignment to the left side of the component.
  364. * @see #getAlignmentX
  365. */
  366. public static final float LEFT_ALIGNMENT = 0.0f;
  367. /**
  368. * Ease-of-use constant for <code>getAlignmentX</code>. Specifies an
  369. * alignment to the right side of the component.
  370. * @see #getAlignmentX
  371. */
  372. public static final float RIGHT_ALIGNMENT = 1.0f;
  373. /*
  374. * JDK 1.1 serialVersionUID
  375. */
  376. private static final long serialVersionUID = -7644114512714619750L;
  377. /**
  378. * If any PropertyChangeListeners have been registered, the
  379. * changeSupport field describes them.
  380. *
  381. * @serial
  382. * @since 1.2
  383. * @see addPropertyChangeListener
  384. * @see removePropertyChangeListener
  385. * @see firePropertyChange
  386. */
  387. private java.beans.PropertyChangeSupport changeSupport;
  388. boolean isPacked = false;
  389. /**
  390. * This object is used as a key for internal hashtables.
  391. */
  392. transient private Object privateKey = new Object();
  393. /**
  394. * Constructs a new component. Class <code>Component</code> can be
  395. * extended directly to create a lightweight component that does not
  396. * utilize an opaque native window. A lightweight component must be
  397. * hosted by a native container somewhere higher up in the component
  398. * tree (for example, by a <code>Frame</code> object).
  399. */
  400. protected Component() {
  401. appContext = AppContext.getAppContext();
  402. SunToolkit.insertTargetMapping(this, appContext);
  403. }
  404. /**
  405. * Construct a name for this component. Called by getName() when the
  406. * name is null.
  407. */
  408. String constructComponentName() {
  409. return null; // For strict compliance with prior platform versions, a Component
  410. // that doesn't set its name should return null from
  411. // getName()
  412. }
  413. /**
  414. * Gets the name of the component.
  415. * @return This component's name.
  416. * @see #setName
  417. * @since JDK1.1
  418. */
  419. public String getName() {
  420. if (name == null && !nameExplicitlySet) {
  421. synchronized(this) {
  422. if (name == null && !nameExplicitlySet)
  423. name = constructComponentName();
  424. }
  425. }
  426. return name;
  427. }
  428. /**
  429. * Sets the name of the component to the specified string.
  430. * @param name The string that is to be this
  431. * component's name.
  432. * @see #getName
  433. * @since JDK1.1
  434. */
  435. public void setName(String name) {
  436. synchronized(this) {
  437. this.name = name;
  438. nameExplicitlySet = true;
  439. }
  440. }
  441. /**
  442. * Gets the parent of this component.
  443. * @return The parent container of this component.
  444. * @since JDK1.0
  445. */
  446. public Container getParent() {
  447. return getParent_NoClientCode();
  448. }
  449. // NOTE: This method may be called by privileged threads.
  450. // This functionality is implemented in a package-private method
  451. // to insure that it cannot be overridden by client subclasses.
  452. // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  453. final Container getParent_NoClientCode() {
  454. return parent;
  455. }
  456. /**
  457. * @deprecated As of JDK version 1.1,
  458. * programs should not directly manipulate peers.
  459. * replaced by <code>boolean isDisplayable()</code>.
  460. */
  461. public ComponentPeer getPeer() {
  462. return peer;
  463. }
  464. /**
  465. * Associate a DropTarget with this Component. The Component will
  466. * receive drops only if it is enabled.
  467. *
  468. * @see #isEnabled
  469. * @param dt The DropTarget
  470. */
  471. public synchronized void setDropTarget(DropTarget dt) {
  472. if (dt == dropTarget || (dropTarget != null && dropTarget.equals(dt)))
  473. return;
  474. DropTarget old;
  475. if ((old = dropTarget) != null) {
  476. if (peer != null) dropTarget.removeNotify(peer);
  477. DropTarget t = dropTarget;
  478. dropTarget = null;
  479. try {
  480. t.setComponent(null);
  481. } catch (IllegalArgumentException iae) {
  482. // ignore it.
  483. }
  484. }
  485. // if we have a new one, and we have a peer, add it!
  486. if ((dropTarget = dt) != null) {
  487. try {
  488. dropTarget.setComponent(this);
  489. if (peer != null) dropTarget.addNotify(peer);
  490. } catch (IllegalArgumentException iae) {
  491. if (old != null) {
  492. try {
  493. old.setComponent(this);
  494. if (peer != null) dropTarget.addNotify(peer);
  495. } catch (IllegalArgumentException iae1) {
  496. // ignore it!
  497. }
  498. }
  499. }
  500. }
  501. }
  502. /**
  503. * Get the DropTarget associated with this Component
  504. */
  505. public synchronized DropTarget getDropTarget() { return dropTarget; }
  506. /**
  507. * Get the <code>GraphicsConfiguration</code> associated with this
  508. * <code>Component</code>.
  509. * If the <code>Component</code> has not been assigned a specific
  510. * <code>GraphicsConfiguration</code>,
  511. * the <code>GraphicsConfiguration</code> of the
  512. * <code>Component</code> object's top-level container is
  513. * returned.
  514. * If the <code>Component</code> has been created, but not yet added
  515. * to a <code>Container</code>, this method returns <code>null</code>.
  516. * @return the <code>GraphicsConfiguration</code> used by this
  517. * <code>Component</code> or <code>null</code>
  518. * @since 1.3
  519. */
  520. public GraphicsConfiguration getGraphicsConfiguration() {
  521. synchronized(getTreeLock()) {
  522. if (graphicsConfig != null) {
  523. return graphicsConfig;
  524. } else if (getParent() != null) {
  525. return getParent().getGraphicsConfiguration();
  526. } else {
  527. return null;
  528. }
  529. }
  530. }
  531. /**
  532. * Reset this Componenet's GraphicsConfiguration back to a default
  533. * value. For most Componenets, this is null.
  534. * Called from the Toolkit thread, so NO CLIENT CODE.
  535. */
  536. void resetGC() {
  537. synchronized(getTreeLock()) {
  538. graphicsConfig = null;
  539. }
  540. }
  541. /**
  542. * Checks that this Component's GraphicsDevice idString matches
  543. * the String argument
  544. */
  545. void checkGD(String stringID) {
  546. if (graphicsConfig != null) {
  547. if (!graphicsConfig.getDevice().getIDstring().equals(stringID)) {
  548. throw new IllegalArgumentException(
  549. "adding a container to a container on a different GraphicsDevice");
  550. }
  551. }
  552. }
  553. /**
  554. * Gets the locking object for AWT component-tree and layout
  555. * Gets this component's locking object (the object that owns the thread
  556. * sychronization monitor) for AWT component-tree and layout
  557. * operations.
  558. * @return This component's locking object.
  559. */
  560. public final Object getTreeLock() {
  561. return LOCK;
  562. }
  563. /**
  564. * Gets the toolkit of this component. Note that
  565. * the frame that contains a component controls which
  566. * toolkit is used by that component. Therefore if the component
  567. * is moved from one frame to another, the toolkit it uses may change.
  568. * @return The toolkit of this component.
  569. * @since JDK1.0
  570. */
  571. public Toolkit getToolkit() {
  572. return getToolkitImpl();
  573. }
  574. /*
  575. * This is called by the native code, so client code can't
  576. * be called on the toolkit thread.
  577. */
  578. final Toolkit getToolkitImpl() {
  579. ComponentPeer peer = this.peer;
  580. if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)){
  581. return peer.getToolkit();
  582. }
  583. Container parent = this.parent;
  584. if (parent != null) {
  585. return parent.getToolkitImpl();
  586. }
  587. return Toolkit.getDefaultToolkit();
  588. }
  589. /**
  590. * Determines whether this component is valid. A component is valid
  591. * when it is correctly sized and positioned within its parent
  592. * container and all its children are also valid. Components are
  593. * invalidated when they are first shown on the screen.
  594. * @return <code>true</code> if the component is valid; <code>false</code>
  595. * otherwise.
  596. * @see #validate
  597. * @see #invalidate
  598. * @since JDK1.0
  599. */
  600. public boolean isValid() {
  601. return (peer != null) && valid;
  602. }
  603. /**
  604. * Determines whether this component is displayable. A component is
  605. * displayable when it is connected to a native screen resource.
  606. * <p>
  607. * A component is made displayable either when it is added to
  608. * a displayable containment hierarchy or when its containment
  609. * hierarchy is made displayable.
  610. * A containment hierarchy is made displayable when its ancestor
  611. * window is either packed or made visible.
  612. * <p>
  613. * A component is made undisplayable either when it is removed from
  614. * a displayable containment hierarchy or when its containment hierarchy
  615. * is made undisplayable. A containment hierarchy is made
  616. * undisplayable when its ancestor window is disposed.
  617. *
  618. * @return <code>true</code> if the component is displayable;
  619. * <code>false</code> otherwise.
  620. * @see java.awt.Container#add(java.awt.Component)
  621. * @see java.awt.Window#pack
  622. * @see java.awt.Window#show
  623. * @see java.awt.Container#remove(java.awt.Component)
  624. * @see java.awt.Window#dispose
  625. * @since 1.2
  626. */
  627. public boolean isDisplayable() {
  628. return getPeer() != null;
  629. }
  630. /**
  631. * Determines whether this component should be visible when its
  632. * parent is visible. Components are
  633. * initially visible, with the exception of top level components such
  634. * as <code>Frame</code> objects.
  635. * @return <code>true</code> if the component is visible;
  636. * <code>false</code> otherwise.
  637. * @see #setVisible
  638. * @since JDK1.0
  639. */
  640. public boolean isVisible() {
  641. return visible;
  642. }
  643. /**
  644. * Determines whether this component will be displayed on the screen
  645. * if it's displayable.
  646. * @return <code>true</code> if the component and all of its ancestors
  647. * are visible; <code>false</code> otherwise.
  648. */
  649. boolean isRecursivelyVisible() {
  650. return visible && (parent == null || parent.isRecursivelyVisible());
  651. }
  652. /**
  653. * Determines whether this component is showing on screen. This means
  654. * that the component must be visible, and it must be in a container
  655. * that is visible and showing.
  656. * @return <code>true</code> if the component is showing;
  657. * <code>false</code> otherwise.
  658. * @see #setVisible
  659. * @since JDK1.0
  660. */
  661. public boolean isShowing() {
  662. if (visible && (peer != null)) {
  663. Container parent = this.parent;
  664. return (parent == null) || parent.isShowing();
  665. }
  666. return false;
  667. }
  668. /**
  669. * Determines whether this component is enabled. An enabled component
  670. * can respond to user input and generate events. Components are
  671. * enabled initially by default. A component may be enabled or disabled by
  672. * calling its <code>setEnabled</code> method.
  673. * @return <code>true</code> if the component is enabled;
  674. * <code>false</code> otherwise.
  675. * @see #setEnabled
  676. * @since JDK1.0
  677. */
  678. public boolean isEnabled() {
  679. return isEnabledImpl();
  680. }
  681. /*
  682. * This is called by the native code, so client code can't
  683. * be called on the toolkit thread.
  684. */
  685. final boolean isEnabledImpl() {
  686. return enabled;
  687. }
  688. /**
  689. * Enables or disables this component, depending on the value of the
  690. * parameter <code>b</code>. An enabled component can respond to user
  691. * input and generate events. Components are enabled initially by default.
  692. * @param b If <code>true</code>, this component is
  693. * enabled; otherwise this component is disabled.
  694. * @see #isEnabled
  695. * @since JDK1.1
  696. */
  697. public void setEnabled(boolean b) {
  698. enable(b);
  699. }
  700. /**
  701. * @deprecated As of JDK version 1.1,
  702. * replaced by <code>setEnabled(boolean)</code>.
  703. */
  704. public void enable() {
  705. if (enabled != true) {
  706. synchronized (getTreeLock()) {
  707. enabled = true;
  708. ComponentPeer peer = this.peer;
  709. if (peer != null) {
  710. peer.enable();
  711. if (visible) {
  712. GlobalCursorManager.updateCursorImmediately();
  713. }
  714. }
  715. }
  716. if (accessibleContext != null) {
  717. accessibleContext.firePropertyChange(
  718. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  719. null, AccessibleState.ENABLED);
  720. }
  721. }
  722. }
  723. /**
  724. * @deprecated As of JDK version 1.1,
  725. * replaced by <code>setEnabled(boolean)</code>.
  726. */
  727. public void enable(boolean b) {
  728. if (b) {
  729. enable();
  730. } else {
  731. disable();
  732. }
  733. }
  734. /**
  735. * @deprecated As of JDK version 1.1,
  736. * replaced by <code>setEnabled(boolean)</code>.
  737. */
  738. public void disable() {
  739. if (enabled != false) {
  740. synchronized (getTreeLock()) {
  741. enabled = false;
  742. ComponentPeer peer = this.peer;
  743. if (peer != null) {
  744. peer.disable();
  745. if (visible) {
  746. GlobalCursorManager.updateCursorImmediately();
  747. }
  748. }
  749. }
  750. if (accessibleContext != null) {
  751. accessibleContext.firePropertyChange(
  752. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  753. null, AccessibleState.ENABLED);
  754. }
  755. }
  756. }
  757. /**
  758. * Returns true if this component is painted to an offscreen image
  759. * ("buffer") that's copied to the screen later. Component
  760. * subclasses that support double buffering should override this
  761. * method to return true if double buffering is enabled.
  762. *
  763. * @return false by default
  764. */
  765. public boolean isDoubleBuffered() {
  766. return false;
  767. }
  768. /**
  769. * Enables or disables input method support for this component. If input
  770. * method support is enabled and the component also processes key events,
  771. * incoming events are offered to
  772. * the current input method and will only be processed by the component or
  773. * dispatched to its listeners if the input method does not consume them.
  774. * By default, input method support is enabled.
  775. *
  776. * @param enable true to enable, false to disable.
  777. * @see java.awt.Component#processKeyEvent
  778. * @since 1.2
  779. */
  780. public void enableInputMethods(boolean enable) {
  781. if (enable) {
  782. if ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0)
  783. return;
  784. // If this component already has focus, then activate the
  785. // input method by dispatching a synthesized focus gained
  786. // event.
  787. if (hasFocus() == true) {
  788. InputContext inputContext = getInputContext();
  789. if (inputContext != null) {
  790. FocusEvent focusGainedEvent = new FocusEvent(this,
  791. FocusEvent.FOCUS_GAINED);
  792. inputContext.dispatchEvent(focusGainedEvent);
  793. }
  794. }
  795. eventMask |= AWTEvent.INPUT_METHODS_ENABLED_MASK;
  796. } else {
  797. if (areInputMethodsEnabled()) {
  798. InputContext inputContext = getInputContext();
  799. if (inputContext != null) {
  800. inputContext.endComposition();
  801. inputContext.removeNotify(this);
  802. }
  803. }
  804. eventMask &= ~AWTEvent.INPUT_METHODS_ENABLED_MASK;
  805. }
  806. }
  807. /**
  808. * Shows or hides this component depending on the value of parameter
  809. * <code>b</code>.
  810. * @param b If <code>true</code>, shows this component;
  811. * otherwise, hides this component.
  812. * @see #isVisible
  813. * @since JDK1.1
  814. */
  815. public void setVisible(boolean b) {
  816. show(b);
  817. }
  818. /**
  819. * @deprecated As of JDK version 1.1,
  820. * replaced by <code>setVisible(boolean)</code>.
  821. */
  822. public void show() {
  823. if (!visible) {
  824. synchronized (getTreeLock()) {
  825. visible = true;
  826. ComponentPeer peer = this.peer;
  827. if (peer != null) {
  828. peer.show();
  829. createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
  830. this, parent,
  831. HierarchyEvent.SHOWING_CHANGED);
  832. if (peer instanceof java.awt.peer.LightweightPeer) {
  833. repaint();
  834. }
  835. GlobalCursorManager.updateCursorImmediately();
  836. }
  837. if (componentListener != null ||
  838. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
  839. ComponentEvent e = new ComponentEvent(this,
  840. ComponentEvent.COMPONENT_SHOWN);
  841. Toolkit.getEventQueue().postEvent(e);
  842. }
  843. }
  844. Container parent = this.parent;
  845. if (parent != null) {
  846. parent.invalidate();
  847. }
  848. }
  849. }
  850. /**
  851. * @deprecated As of JDK version 1.1,
  852. * replaced by <code>setVisible(boolean)</code>.
  853. */
  854. public void show(boolean b) {
  855. if (b) {
  856. show();
  857. } else {
  858. hide();
  859. }
  860. }
  861. /**
  862. * @deprecated As of JDK version 1.1,
  863. * replaced by <code>setVisible(boolean)</code>.
  864. */
  865. public void hide() {
  866. if (visible) {
  867. synchronized (getTreeLock()) {
  868. visible = false;
  869. ComponentPeer peer = this.peer;
  870. if (peer != null) {
  871. peer.hide();
  872. createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
  873. this, parent,
  874. HierarchyEvent.SHOWING_CHANGED);
  875. if (peer instanceof java.awt.peer.LightweightPeer) {
  876. repaint();
  877. }
  878. GlobalCursorManager.updateCursorImmediately();
  879. }
  880. if (componentListener != null ||
  881. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
  882. ComponentEvent e = new ComponentEvent(this,
  883. ComponentEvent.COMPONENT_HIDDEN);
  884. Toolkit.getEventQueue().postEvent(e);
  885. }
  886. }
  887. Container parent = this.parent;
  888. if (parent != null) {
  889. parent.invalidate();
  890. }
  891. }
  892. }
  893. /**
  894. * Gets the foreground color of this component.
  895. * @return This component's foreground color. If this component does
  896. * not have a foreground color, the foreground color of its parent
  897. * is returned.
  898. * @see #setForeground
  899. * @since JDK1.0
  900. */
  901. public Color getForeground() {
  902. Color foreground = this.foreground;
  903. if (foreground != null) {
  904. return foreground;
  905. }
  906. Container parent = this.parent;
  907. return (parent != null) ? parent.getForeground() : null;
  908. }
  909. /**
  910. * Sets the foreground color of this component.
  911. * @param c The color to become this component's
  912. * foreground color.
  913. * If this parameter is null then this component will inherit
  914. * the foreground color of its parent.
  915. * @see #getForeground
  916. * @since JDK1.0
  917. */
  918. public void setForeground(Color c) {
  919. Color oldColor = foreground;
  920. ComponentPeer peer = this.peer;
  921. foreground = c;
  922. if (peer != null) {
  923. c = getForeground();
  924. if (c != null) {
  925. peer.setForeground(c);
  926. }
  927. }
  928. // This is a bound property, so report the change to
  929. // any registered listeners. (Cheap if there are none.)
  930. firePropertyChange("foreground", oldColor, c);
  931. }
  932. /**
  933. * Gets the background color of this component.
  934. * @return This component's background color. If this component does
  935. * not have a background color, the background color of its parent
  936. * is returned.
  937. * @see java.awt.Component#setBackground(java.awt.Color)
  938. * @since JDK1.0
  939. */
  940. public Color getBackground() {
  941. Color background = this.background;
  942. if (background != null) {
  943. return background;
  944. }
  945. Container parent = this.parent;
  946. return (parent != null) ? parent.getBackground() : null;
  947. }
  948. /**
  949. * Sets the background color of this component.
  950. * @param c The color to become this component's color.
  951. * If this parameter is null then this component will inherit
  952. * the background color of its parent.
  953. * background color.
  954. * @see #getBackground
  955. * @since JDK1.0
  956. */
  957. public void setBackground(Color c) {
  958. Color oldColor = background;
  959. ComponentPeer peer = this.peer;
  960. background = c;
  961. if (peer != null) {
  962. c = getBackground();
  963. if (c != null) {
  964. peer.setBackground(c);
  965. }
  966. }
  967. // This is a bound property, so report the change to
  968. // any registered listeners. (Cheap if there are none.)
  969. firePropertyChange("background", oldColor, c);
  970. }
  971. /**
  972. * Gets the font of this component.
  973. * @return This component's font. If a font has not been set
  974. * for this component, the font of its parent is returned.
  975. * @see #setFont
  976. * @since JDK1.0
  977. */
  978. public Font getFont() {
  979. return getFont_NoClientCode();
  980. }
  981. // NOTE: This method may be called by privileged threads.
  982. // This functionality is implemented in a package-private method
  983. // to insure that it cannot be overridden by client subclasses.
  984. // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  985. final Font getFont_NoClientCode() {
  986. Font font = this.font;
  987. if (font != null) {
  988. return font;
  989. }
  990. Container parent = this.parent;
  991. return (parent != null) ? parent.getFont_NoClientCode() : null;
  992. }
  993. /**
  994. * Sets the font of this component.
  995. * @param f The font to become this component's font.
  996. * If this parameter is null then this component will inherit
  997. * the font of its parent.
  998. * @see #getFont
  999. * @since JDK1.0
  1000. */
  1001. public void setFont(Font f) {
  1002. Font oldFont, newFont;
  1003. synchronized (this) {
  1004. oldFont = font;
  1005. ComponentPeer peer = this.peer;
  1006. newFont = font = f;
  1007. if (peer != null) {
  1008. f = getFont();
  1009. if (f != null) {
  1010. peer.setFont(f);
  1011. peerFont = f;
  1012. }
  1013. }
  1014. }
  1015. // This is a bound property, so report the change to
  1016. // any registered listeners. (Cheap if there are none.)
  1017. firePropertyChange("font", oldFont, newFont);
  1018. // This could change the preferred size of the Component.
  1019. if (valid) {
  1020. invalidate();
  1021. }
  1022. }
  1023. /**
  1024. * Gets the locale of this component.
  1025. * @return This component's locale. If this component does not
  1026. * have a locale, the locale of its parent is returned.
  1027. * @see #setLocale
  1028. * @exception IllegalComponentStateException If the Component
  1029. * does not have its own locale and has not yet been added to
  1030. * a containment hierarchy such that the locale can be determined
  1031. * from the containing parent.
  1032. * @since JDK1.1
  1033. */
  1034. public Locale getLocale() {
  1035. Locale locale = this.locale;
  1036. if (locale != null) {
  1037. return locale;
  1038. }
  1039. Container parent = this.parent;
  1040. if (parent == null) {
  1041. throw new IllegalComponentStateException("This component must have a parent in order to determine its locale");
  1042. } else {
  1043. return parent.getLocale();
  1044. }
  1045. }
  1046. /**
  1047. * Sets the locale of this component.
  1048. * @param l The locale to become this component's locale.
  1049. * @see #getLocale
  1050. * @since JDK1.1
  1051. */
  1052. public void setLocale(Locale l) {
  1053. locale = l;
  1054. // This could change the preferred size of the Component.
  1055. if (valid) {
  1056. invalidate();
  1057. }
  1058. }
  1059. /**
  1060. * Gets the instance of <code>ColorModel</code> used to display
  1061. * the component on the output device.
  1062. * @return The color model used by this component.
  1063. * @see java.awt.image.ColorModel
  1064. * @see java.awt.peer.ComponentPeer#getColorModel()
  1065. * @see java.awt.Toolkit#getColorModel()
  1066. * @since JDK1.0
  1067. */
  1068. public ColorModel getColorModel() {
  1069. ComponentPeer peer = this.peer;
  1070. if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)) {
  1071. return peer.getColorModel();
  1072. }
  1073. return getToolkit().getColorModel();
  1074. }
  1075. /**
  1076. * Gets the location of this component in the form of a
  1077. * point specifying the component's top-left corner.
  1078. * The location will be relative to the parent's coordinate space.
  1079. * <p>
  1080. * Due to the asynchronous nature of native event handling, this
  1081. * method can return outdated values (for instance, after several calls
  1082. * of <code>setLocation()</code> in rapid succession). For this
  1083. * reason, the recommended method of obtaining a Component's position is
  1084. * within <code>java.awt.event.ComponentListener.componentMoved()</code>,
  1085. * which is called after the operating system has finished moving the
  1086. * Component.
  1087. * </p>
  1088. * @return An instance of <code>Point</code> representing
  1089. * the top-left corner of the component's bounds in the coordinate
  1090. * space of the component's parent.
  1091. * @see #setLocation
  1092. * @see #getLocationOnScreen
  1093. * @since JDK1.1
  1094. */
  1095. public Point getLocation() {
  1096. return location();
  1097. }
  1098. /**
  1099. * Gets the location of this component in the form of a point
  1100. * specifying the component's top-left corner in the screen's
  1101. * coordinate space.
  1102. * @return An instance of <code>Point</code> representing
  1103. * the top-left corner of the component's bounds in the
  1104. * coordinate space of the screen.
  1105. * @see #setLocation
  1106. * @see #getLocation
  1107. */
  1108. public Point getLocationOnScreen() {
  1109. synchronized (getTreeLock()) {
  1110. return getLocationOnScreen_NoTreeLock();
  1111. }
  1112. }
  1113. /*
  1114. * a package private version of getLocationOnScreen
  1115. * used by GlobalCursormanager to update cursor
  1116. */
  1117. final Point getLocationOnScreen_NoTreeLock() {
  1118. if (peer != null && isShowing()) {
  1119. if (peer instanceof java.awt.peer.LightweightPeer) {
  1120. // lightweight component location needs to be translated
  1121. // relative to a native component.
  1122. Container host = getNativeContainer();
  1123. Point pt = host.peer.getLocationOnScreen();
  1124. for(Component c = this; c != host; c = c.getParent()) {
  1125. pt.x += c.x;
  1126. pt.y += c.y;
  1127. }
  1128. return pt;
  1129. } else {
  1130. Point pt = peer.getLocationOnScreen();
  1131. return pt;
  1132. }
  1133. } else {
  1134. throw new IllegalComponentStateException("component must be showing on the screen to determine its location");
  1135. }
  1136. }
  1137. /**
  1138. * @deprecated As of JDK version 1.1,
  1139. * replaced by <code>getLocation()</code>.
  1140. */
  1141. public Point location() {
  1142. return new Point(x, y);
  1143. }
  1144. /**
  1145. * Moves this component to a new location. The top-left corner of
  1146. * the new location is specified by the <code>x</code> and <code>y</code>
  1147. * parameters in the coordinate space of this component's parent.
  1148. * @param x The <i>x</i>-coordinate of the new location's
  1149. * top-left corner in the parent's coordinate space.
  1150. * @param y The <i>y</i>-coordinate of the new location's
  1151. * top-left corner in the parent's coordinate space.
  1152. * @see #getLocation
  1153. * @see #setBounds
  1154. * @since JDK1.1
  1155. */
  1156. public void setLocation(int x, int y) {
  1157. move(x, y);
  1158. }
  1159. /**
  1160. * @deprecated As of JDK version 1.1,
  1161. * replaced by <code>setLocation(int, int)</code>.
  1162. */
  1163. public void move(int x, int y) {
  1164. setBounds(x, y, width, height);
  1165. }
  1166. /**
  1167. * Moves this component to a new location. The top-left corner of
  1168. * the new location is specified by point <code>p</code>. Point
  1169. * <code>p</code> is given in the parent's coordinate space.
  1170. * @param p The point defining the top-left corner
  1171. * of the new location, given in the coordinate space of this
  1172. * component's parent.
  1173. * @see #getLocation
  1174. * @see #setBounds
  1175. * @since JDK1.1
  1176. */
  1177. public void setLocation(Point p) {
  1178. setLocation(p.x, p.y);
  1179. }
  1180. /**
  1181. * Returns the size of this component in the form of a
  1182. * <code>Dimension</code> object. The <code>height</code>
  1183. * field of the <code>Dimension</code> object contains
  1184. * this component's height, and the <code>width</code>
  1185. * field of the <code>Dimension</code> object contains
  1186. * this component's width.
  1187. * @return A <code>Dimension</code> object that indicates the
  1188. * size of this component.
  1189. * @see #setSize
  1190. * @since JDK1.1
  1191. */
  1192. public Dimension getSize() {
  1193. return size();
  1194. }
  1195. /**
  1196. * @deprecated As of JDK version 1.1,
  1197. * replaced by <code>getSize()</code>.
  1198. */
  1199. public Dimension size() {
  1200. return new Dimension(width, height);
  1201. }
  1202. /**
  1203. * Resizes this component so that it has width <code>width</code>
  1204. * and <code>height</code>.
  1205. * @param width The new width of this component in pixels.
  1206. * @param height The new height of this component in pixels.
  1207. * @see #getSize
  1208. * @see #setBounds
  1209. * @since JDK1.1
  1210. */
  1211. public void setSize(int width, int height) {
  1212. resize(width, height);
  1213. }
  1214. /**
  1215. * @deprecated As of JDK version 1.1,
  1216. * replaced by <code>setSize(int, int)</code>.
  1217. */
  1218. public void resize(int width, int height) {
  1219. setBounds(x, y, width, height);
  1220. }
  1221. /**
  1222. * Resizes this component so that it has width <code>d.width</code>
  1223. * and height <code>d.height</code>.
  1224. * @param d The dimension specifying the new size
  1225. * of this component.
  1226. * @see #setSize
  1227. * @see #setBounds
  1228. * @since JDK1.1
  1229. */
  1230. public void setSize(Dimension d) {
  1231. resize(d);
  1232. }
  1233. /**
  1234. * @deprecated As of JDK version 1.1,
  1235. * replaced by <code>setSize(Dimension)</code>.
  1236. */
  1237. public void resize(Dimension d) {
  1238. setSize(d.width, d.height);
  1239. }
  1240. /**
  1241. * Gets the bounds of this component in the form of a
  1242. * <code>Rectangle</code> object. The bounds specify this
  1243. * component's width, height, and location relative to
  1244. * its parent.
  1245. * @return A rectangle indicating this component's bounds.
  1246. * @see #setBounds
  1247. * @see #getLocation
  1248. * @see #getSize
  1249. */
  1250. public Rectangle getBounds() {
  1251. return bounds();
  1252. }
  1253. /**
  1254. * @deprecated As of JDK version 1.1,
  1255. * replaced by <code>getBounds()</code>.
  1256. */
  1257. public Rectangle bounds() {
  1258. return new Rectangle(x, y, width, height);
  1259. }
  1260. /**
  1261. * Moves and resizes this component. The new location of the top-left
  1262. * corner is specified by <code>x</code> and <code>y</code>, and the
  1263. * new size is specified by <code>width</code> and <code>height</code>.
  1264. * @param x The new <i>x</i>-coordinate of this component.
  1265. * @param y The new <i>y</i>-coordinate of this component.
  1266. * @param width The new <code>width</code> of this component.
  1267. * @param height The new <code>height</code> of this
  1268. * component.
  1269. * @see java.awt.Component#getBounds
  1270. * @see java.awt.Component#setLocation(int, int)
  1271. * @see java.awt.Component#setLocation(java.awt.Point)
  1272. * @see java.awt.Component#setSize(int, int)
  1273. * @see java.awt.Component#setSize(java.awt.Dimension)
  1274. * @JDK1.1
  1275. */
  1276. public void setBounds(int x, int y, int width, int height) {
  1277. reshape(x, y, width, height);
  1278. }
  1279. /**
  1280. * @deprecated As of JDK version 1.1,
  1281. * replaced by <code>setBounds(int, int, int, int)</code>.
  1282. */
  1283. public void reshape(int x, int y, int width, int height) {
  1284. synchronized (getTreeLock()) {
  1285. boolean resized = (this.width != width) || (this.height != height);
  1286. boolean moved = (this.x != x) || (this.y != y);
  1287. boolean isLightweight = peer instanceof java.awt.peer.LightweightPeer;
  1288. if (resized) {
  1289. isPacked = false;
  1290. }
  1291. if (resized || moved) {
  1292. if (isLightweight && visible) {
  1293. // Have the parent redraw the area this component occupied.
  1294. repaint();
  1295. }
  1296. this.x = x;
  1297. this.y = y;
  1298. this.width = width;
  1299. this.height = height;
  1300. if (peer != null) {
  1301. if (isLightweight) {
  1302. peer.setBounds(x, y, width, height);
  1303. } else {
  1304. // native peer might be offset by more than direct
  1305. // parent since parent might be lightweight.
  1306. int nativeX = x;
  1307. int nativeY = y;
  1308. for(Component c = parent; (c != null) &&
  1309. (c.peer instanceof java.awt.peer.LightweightPeer);
  1310. c = c.parent) {
  1311. nativeX += c.x;
  1312. nativeY += c.y;
  1313. }
  1314. peer.setBounds(nativeX, nativeY, width, height);
  1315. }
  1316. if (resized) {
  1317. invalidate();
  1318. }
  1319. if (parent != null && parent.valid) {
  1320. parent.invalidate();
  1321. }
  1322. }
  1323. if (resized) {
  1324. if (componentListener != null ||
  1325. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
  1326. ComponentEvent e = new ComponentEvent(this,
  1327. ComponentEvent.COMPONENT_RESIZED);
  1328. Toolkit.getEventQueue().postEvent(e);
  1329. // Container.dispatchEventImpl will create
  1330. // HierarchyEvents
  1331. } else {
  1332. createChildHierarchyEvents(
  1333. HierarchyEvent.ANCESTOR_RESIZED,
  1334. 0);
  1335. }
  1336. }
  1337. if (moved) {
  1338. if (componentListener != null ||
  1339. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0){
  1340. ComponentEvent e = new ComponentEvent(this,
  1341. ComponentEvent.COMPONENT_MOVED);
  1342. Toolkit.getEventQueue().postEvent(e);
  1343. // Container.dispatchEventImpl will create
  1344. // HierarchyEvents
  1345. } else {
  1346. createChildHierarchyEvents(
  1347. HierarchyEvent.ANCESTOR_MOVED,
  1348. 0);
  1349. }
  1350. }
  1351. if (isLightweight && visible) {
  1352. // Have the parent redraw the area this component *now* occupies.
  1353. repaint();
  1354. }
  1355. }
  1356. }
  1357. }
  1358. /**
  1359. * Moves and resizes this component to conform to the new
  1360. * bounding rectangle <code>r</code>. This component's new
  1361. * position is specified by <code>r.x</code> and <code>r.y</code>,
  1362. * and its new size is specified by <code>r.width</code> and
  1363. * <code>r.height</code>
  1364. * @param r The new bounding rectangle for this component.
  1365. * @see java.awt.Component#getBounds
  1366. * @see java.awt.Component#setLocation(int, int)
  1367. * @see java.awt.Component#setLocation(java.awt.Point)
  1368. * @see java.awt.Component#setSize(int, int)
  1369. * @see java.awt.Component#setSize(java.awt.Dimension)
  1370. * @since JDK1.1
  1371. */
  1372. public void setBounds(Rectangle r) {
  1373. setBounds(r.x, r.y, r.width, r.height);
  1374. }
  1375. /**
  1376. * Return the current x coordinate of the components origin.
  1377. * This method is preferable to writing component.getBounds().x,
  1378. * or component.getLocation().x because it doesn't cause any
  1379. * heap allocations.
  1380. *
  1381. * @return the current x coordinate of the components origin.
  1382. * @since 1.2
  1383. */
  1384. public int getX() {
  1385. return x;
  1386. }
  1387. /**
  1388. * Return the current y coordinate of the components origin.
  1389. * This method is preferable to writing component.getBounds().y,
  1390. * or component.getLocation().y because it doesn't cause any
  1391. * heap allocations.
  1392. *
  1393. * @return the current y coordinate of the components origin.
  1394. * @since 1.2
  1395. */
  1396. public int getY() {
  1397. return y;
  1398. }
  1399. /**
  1400. * Return the current width of this component.
  1401. * This method is preferable to writing component.getBounds().width,
  1402. * or component.getSize().width because it doesn't cause any
  1403. * heap allocations.
  1404. *
  1405. * @return the current width of this component.
  1406. * @since 1.2
  1407. */
  1408. public int getWidth() {
  1409. return width;
  1410. }
  1411. /**
  1412. * Return the current height of this component.
  1413. * This method is preferable to writing component.getBounds().height,
  1414. * or component.getSize().height because it doesn't cause any
  1415. * heap allocations.
  1416. *
  1417. * @return the current height of this component.
  1418. * @since 1.2
  1419. */
  1420. public int getHeight() {
  1421. return height;
  1422. }
  1423. /**
  1424. * Store the bounds of this component into "return value" <b>rv</b> and
  1425. * return <b>rv</b>. If rv is null a new Rectangle is allocated.
  1426. * This version of getBounds() is useful if the caller
  1427. * wants to avoid allocating a new Rectangle object on the heap.
  1428. *
  1429. * @param rv the return value, modified to the components bounds
  1430. * @return rv
  1431. */
  1432. public Rectangle getBounds(Rectangle rv) {
  1433. if (rv == null) {
  1434. return new Rectangle(getX(), getY(), getWidth(), getHeight());
  1435. }
  1436. else {
  1437. rv.setBounds(getX(), getY(), getWidth(), getHeight());
  1438. return rv;
  1439. }
  1440. }
  1441. /**
  1442. * Store the width/height of this component into "return value" <b>rv</b>
  1443. * and return <b>rv</b>. If rv is null a new Dimension object is
  1444. * allocated. This version of getSize() is useful if the
  1445. * caller wants to avoid allocating a new Dimension object on the heap.
  1446. *
  1447. * @param rv the return value, modified to the components size
  1448. * @return rv
  1449. */
  1450. public Dimension getSize(Dimension rv) {
  1451. if (rv == null) {
  1452. return new Dimension(getWidth(), getHeight());
  1453. }
  1454. else {
  1455. rv.setSize(getWidth(), getHeight());
  1456. return rv;
  1457. }
  1458. }
  1459. /**
  1460. * Store the x,y origin of this component into "return value" <b>rv</b>
  1461. * and return <b>rv</b>. If rv is null a new Point is allocated.
  1462. * This version of getLocation() is useful if the
  1463. * caller wants to avoid allocating a new Point object on the heap.
  1464. *
  1465. * @param rv the return value, modified to the components location
  1466. * @return rv
  1467. */
  1468. public Point getLocation(Point rv) {
  1469. if (rv == null) {
  1470. return new Point(getX(), getY());
  1471. }
  1472. else {
  1473. rv.setLocation(getX(), getY());
  1474. return rv;
  1475. }
  1476. }
  1477. /**
  1478. * Returns true if this component is completely opaque, returns
  1479. * false by default.
  1480. * <p>
  1481. * An opaque component paints every pixel within its
  1482. * rectangular region. A non-opaque component paints only some of
  1483. * its pixels, allowing the pixels underneath it to "show through".
  1484. * A component that does not fully paint its pixels therefore
  1485. * provides a degree of transparency. Only lightweight
  1486. * components can be transparent.
  1487. * <p>
  1488. * Subclasses that guarantee to always completely paint their
  1489. * contents should override this method and return true. All
  1490. * of the "heavyweight" AWT components are opaque.
  1491. *
  1492. * @return true if this component is completely opaque.