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. * Component; <code>false</code> otherwise.
  1261. * @since 1.4
  1262. */
  1263. public boolean isFontSet() {
  1264. return (font != null);
  1265. }
  1266. /**
  1267. * Gets the locale of this component.
  1268. * @return this component's locale; if this component does not
  1269. * have a locale, the locale of its parent is returned
  1270. * @see #setLocale
  1271. * @exception IllegalComponentStateException if the <code>Component</code>
  1272. * does not have its own locale and has not yet been added to
  1273. * a containment hierarchy such that the locale can be determined
  1274. * from the containing parent
  1275. * @since JDK1.1
  1276. */
  1277. public Locale getLocale() {
  1278. Locale locale = this.locale;
  1279. if (locale != null) {
  1280. return locale;
  1281. }
  1282. Container parent = this.parent;
  1283. if (parent == null) {
  1284. throw new IllegalComponentStateException("This component must have a parent in order to determine its locale");
  1285. } else {
  1286. return parent.getLocale();
  1287. }
  1288. }
  1289. /**
  1290. * Sets the locale of this component. This is a bound property.
  1291. * @param l the locale to become this component's locale
  1292. * @see #getLocale
  1293. * @since JDK1.1
  1294. */
  1295. public void setLocale(Locale l) {
  1296. Locale oldValue = locale;
  1297. locale = l;
  1298. // This is a bound property, so report the change to
  1299. // any registered listeners. (Cheap if there are none.)
  1300. firePropertyChange("locale", oldValue, l);
  1301. // This could change the preferred size of the Component.
  1302. if (valid) {
  1303. invalidate();
  1304. }
  1305. }
  1306. /**
  1307. * Gets the instance of <code>ColorModel</code> used to display
  1308. * the component on the output device.
  1309. * @return the color model used by this component
  1310. * @see java.awt.image.ColorModel
  1311. * @see java.awt.peer.ComponentPeer#getColorModel()
  1312. * @see Toolkit#getColorModel()
  1313. * @since JDK1.0
  1314. */
  1315. public ColorModel getColorModel() {
  1316. ComponentPeer peer = this.peer;
  1317. if ((peer != null) && ! (peer instanceof LightweightPeer)) {
  1318. return peer.getColorModel();
  1319. } else if (GraphicsEnvironment.isHeadless()) {
  1320. return ColorModel.getRGBdefault();
  1321. } // else
  1322. return getToolkit().getColorModel();
  1323. }
  1324. /**
  1325. * Gets the location of this component in the form of a
  1326. * point specifying the component's top-left corner.
  1327. * The location will be relative to the parent's coordinate space.
  1328. * <p>
  1329. * Due to the asynchronous nature of native event handling, this
  1330. * method can return outdated values (for instance, after several calls
  1331. * of <code>setLocation()</code> in rapid succession). For this
  1332. * reason, the recommended method of obtaining a component's position is
  1333. * within <code>java.awt.event.ComponentListener.componentMoved()</code>,
  1334. * which is called after the operating system has finished moving the
  1335. * component.
  1336. * </p>
  1337. * @return an instance of <code>Point</code> representing
  1338. * the top-left corner of the component's bounds in
  1339. * the coordinate space of the component's parent
  1340. * @see #setLocation
  1341. * @see #getLocationOnScreen
  1342. * @since JDK1.1
  1343. */
  1344. public Point getLocation() {
  1345. return location();
  1346. }
  1347. /**
  1348. * Gets the location of this component in the form of a point
  1349. * specifying the component's top-left corner in the screen's
  1350. * coordinate space.
  1351. * @return an instance of <code>Point</code> representing
  1352. * the top-left corner of the component's bounds in the
  1353. * coordinate space of the screen
  1354. * @throws <code>IllegalComponentStateException</code> if the
  1355. * component is not showing on the screen
  1356. * @see #setLocation
  1357. * @see #getLocation
  1358. */
  1359. public Point getLocationOnScreen() {
  1360. synchronized (getTreeLock()) {
  1361. return getLocationOnScreen_NoTreeLock();
  1362. }
  1363. }
  1364. /*
  1365. * a package private version of getLocationOnScreen
  1366. * used by GlobalCursormanager to update cursor
  1367. */
  1368. final Point getLocationOnScreen_NoTreeLock() {
  1369. if (peer != null && isShowing()) {
  1370. if (peer instanceof LightweightPeer) {
  1371. // lightweight component location needs to be translated
  1372. // relative to a native component.
  1373. Container host = getNativeContainer();
  1374. Point pt = host.peer.getLocationOnScreen();
  1375. for(Component c = this; c != host; c = c.getParent()) {
  1376. pt.x += c.x;
  1377. pt.y += c.y;
  1378. }
  1379. return pt;
  1380. } else {
  1381. Point pt = peer.getLocationOnScreen();
  1382. return pt;
  1383. }
  1384. } else {
  1385. throw new IllegalComponentStateException("component must be showing on the screen to determine its location");
  1386. }
  1387. }
  1388. /**
  1389. * @deprecated As of JDK version 1.1,
  1390. * replaced by <code>getLocation()</code>.
  1391. */
  1392. public Point location() {
  1393. return new Point(x, y);
  1394. }
  1395. /**
  1396. * Moves this component to a new location. The top-left corner of
  1397. * the new location is specified by the <code>x</code> and <code>y</code>
  1398. * parameters in the coordinate space of this component's parent.
  1399. * @param x the <i>x</i>-coordinate of the new location's
  1400. * top-left corner in the parent's coordinate space
  1401. * @param y the <i>y</i>-coordinate of the new location's
  1402. * top-left corner in the parent's coordinate space
  1403. * @see #getLocation
  1404. * @see #setBounds
  1405. * @since JDK1.1
  1406. */
  1407. public void setLocation(int x, int y) {
  1408. move(x, y);
  1409. }
  1410. /**
  1411. * @deprecated As of JDK version 1.1,
  1412. * replaced by <code>setLocation(int, int)</code>.
  1413. */
  1414. public void move(int x, int y) {
  1415. setBounds(x, y, width, height);
  1416. }
  1417. /**
  1418. * Moves this component to a new location. The top-left corner of
  1419. * the new location is specified by point <code>p</code>. Point
  1420. * <code>p</code> is given in the parent's coordinate space.
  1421. * @param p the point defining the top-left corner
  1422. * of the new location, given in the coordinate space of this
  1423. * component's parent
  1424. * @see #getLocation
  1425. * @see #setBounds
  1426. * @since JDK1.1
  1427. */
  1428. public void setLocation(Point p) {
  1429. setLocation(p.x, p.y);
  1430. }
  1431. /**
  1432. * Returns the size of this component in the form of a
  1433. * <code>Dimension</code> object. The <code>height</code>
  1434. * field of the <code>Dimension</code> object contains
  1435. * this component's height, and the <code>width</code>
  1436. * field of the <code>Dimension</code> object contains
  1437. * this component's width.
  1438. * @return a <code>Dimension</code> object that indicates the
  1439. * size of this component
  1440. * @see #setSize
  1441. * @since JDK1.1
  1442. */
  1443. public Dimension getSize() {
  1444. return size();
  1445. }
  1446. /**
  1447. * @deprecated As of JDK version 1.1,
  1448. * replaced by <code>getSize()</code>.
  1449. */
  1450. public Dimension size() {
  1451. return new Dimension(width, height);
  1452. }
  1453. /**
  1454. * Resizes this component so that it has width <code>width</code>
  1455. * and height <code>height</code>.
  1456. * @param width the new width of this component in pixels
  1457. * @param height the new height of this component in pixels
  1458. * @see #getSize
  1459. * @see #setBounds
  1460. * @since JDK1.1
  1461. */
  1462. public void setSize(int width, int height) {
  1463. resize(width, height);
  1464. }
  1465. /**
  1466. * @deprecated As of JDK version 1.1,
  1467. * replaced by <code>setSize(int, int)</code>.
  1468. */
  1469. public void resize(int width, int height) {
  1470. setBounds(x, y, width, height);
  1471. }
  1472. /**
  1473. * Resizes this component so that it has width <code>d.width</code>
  1474. * and height <code>d.height</code>.
  1475. * @param d the dimension specifying the new size
  1476. * of this component
  1477. * @see #setSize
  1478. * @see #setBounds
  1479. * @since JDK1.1
  1480. */
  1481. public void setSize(Dimension d) {
  1482. resize(d);
  1483. }
  1484. /**
  1485. * @deprecated As of JDK version 1.1,
  1486. * replaced by <code>setSize(Dimension)</code>.
  1487. */
  1488. public void resize(Dimension d) {
  1489. setSize(d.width, d.height);
  1490. }
  1491. /**
  1492. * Gets the bounds of this component in the form of a
  1493. * <code>Rectangle</code> object. The bounds specify this
  1494. * component's width, height, and location relative to
  1495. * its parent.
  1496. * @return a rectangle indicating this component's bounds
  1497. * @see #setBounds
  1498. * @see #getLocation
  1499. * @see #getSize
  1500. */
  1501. public Rectangle getBounds() {
  1502. return bounds();
  1503. }
  1504. /**
  1505. * @deprecated As of JDK version 1.1,
  1506. * replaced by <code>getBounds()</code>.
  1507. */
  1508. public Rectangle bounds() {
  1509. return new Rectangle(x, y, width, height);
  1510. }
  1511. /**
  1512. * Moves and resizes this component. The new location of the top-left
  1513. * corner is specified by <code>x</code> and <code>y</code>, and the
  1514. * new size is specified by <code>width</code> and <code>height</code>.
  1515. * @param x the new <i>x</i>-coordinate of this component
  1516. * @param y the new <i>y</i>-coordinate of this component
  1517. * @param width the new <code>width</code> of this component
  1518. * @param height the new <code>height</code> of this
  1519. * component
  1520. * @see #getBounds
  1521. * @see #setLocation(int, int)
  1522. * @see #setLocation(Point)
  1523. * @see #setSize(int, int)
  1524. * @see #setSize(Dimension)
  1525. * @since JDK1.1
  1526. */
  1527. public void setBounds(int x, int y, int width, int height) {
  1528. reshape(x, y, width, height);
  1529. }
  1530. /**
  1531. * @deprecated As of JDK version 1.1,
  1532. * replaced by <code>setBounds(int, int, int, int)</code>.
  1533. */
  1534. public void reshape(int x, int y, int width, int height) {
  1535. synchronized (getTreeLock()) {
  1536. boolean resized = (this.width != width) || (this.height != height);
  1537. boolean moved = (this.x != x) || (this.y != y);
  1538. boolean isLightweight = peer instanceof LightweightPeer;
  1539. boolean needRepaint = false;
  1540. int oldX = this.x;
  1541. int oldY = this.y;
  1542. int oldWidth = this.width;
  1543. int oldHeight = this.height;
  1544. this.x = x;
  1545. this.y = y;
  1546. this.width = width;
  1547. this.height = height;
  1548. if (resized) {
  1549. isPacked = false;
  1550. }
  1551. if (resized || moved) {
  1552. if (peer != null) {
  1553. if (isLightweight) {
  1554. peer.setBounds(x, y, width, height);
  1555. } else {
  1556. // native peer might be offset by more than direct
  1557. // parent since parent might be lightweight.
  1558. int nativeX = x;
  1559. int nativeY = y;
  1560. for(Component c = parent; (c != null) &&
  1561. (c.peer instanceof LightweightPeer);
  1562. c = c.parent) {
  1563. nativeX += c.x;
  1564. nativeY += c.y;
  1565. }
  1566. peer.setBounds(nativeX, nativeY, width, height);
  1567. }
  1568. // Check peer actualy changed coordinates
  1569. resized = (oldWidth != this.width) || (oldHeight != this.height);
  1570. moved = (oldX != this.x) || (oldY != this.y);
  1571. if (resized) {
  1572. invalidate();
  1573. }
  1574. if (parent != null && parent.valid) {
  1575. parent.invalidate();
  1576. }
  1577. }
  1578. if (resized) {
  1579. if (componentListener != null ||
  1580. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
  1581. Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
  1582. ComponentEvent e = new ComponentEvent(this,
  1583. ComponentEvent.COMPONENT_RESIZED);
  1584. Toolkit.getEventQueue().postEvent(e);
  1585. // Container.dispatchEventImpl will create
  1586. // HierarchyEvents
  1587. } else {
  1588. createChildHierarchyEvents(
  1589. HierarchyEvent.ANCESTOR_RESIZED,
  1590. 0,
  1591. Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
  1592. }
  1593. }
  1594. if (moved) {
  1595. if (componentListener != null ||
  1596. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
  1597. Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
  1598. ComponentEvent e = new ComponentEvent(this,
  1599. ComponentEvent.COMPONENT_MOVED);
  1600. Toolkit.getEventQueue().postEvent(e);
  1601. // Container.dispatchEventImpl will create
  1602. // HierarchyEvents
  1603. } else {
  1604. createChildHierarchyEvents(
  1605. HierarchyEvent.ANCESTOR_MOVED,
  1606. 0,
  1607. Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
  1608. }
  1609. }
  1610. if (isLightweight && visible) {
  1611. // Get HW ancestor peer
  1612. Container hwAncestor = parent;
  1613. for(; hwAncestor != null && hwAncestor.isLightweight(); hwAncestor = hwAncestor.parent);
  1614. ContainerPeer hwAncestorPeer = (hwAncestor != null) &&
  1615. hwAncestor.peer instanceof ContainerPeer ?
  1616. (ContainerPeer)hwAncestor.peer : null;
  1617. needRepaint = (hwAncestorPeer != null && hwAncestor.visible
  1618. && !hwAncestorPeer.isPaintPending());
  1619. }
  1620. }
  1621. if (needRepaint) {
  1622. // Have the parent redraw the area this component occupied.
  1623. parent.repaint(oldX, oldY, oldWidth, oldHeight);
  1624. // Have the parent redraw the area this component *now* occupies.
  1625. repaint();
  1626. }
  1627. }
  1628. }
  1629. /**
  1630. * Moves and resizes this component to conform to the new
  1631. * bounding rectangle <code>r</code>. This component's new
  1632. * position is specified by <code>r.x</code> and <code>r.y</code>,
  1633. * and its new size is specified by <code>r.width</code> and
  1634. * <code>r.height</code>
  1635. * @param r the new bounding rectangle for this component
  1636. * @see #getBounds
  1637. * @see #setLocation(int, int)
  1638. * @see #setLocation(Point)
  1639. * @see #setSize(int, int)
  1640. * @see #setSize(Dimension)
  1641. * @since JDK1.1
  1642. */
  1643. public void setBounds(Rectangle r) {
  1644. setBounds(r.x, r.y, r.width, r.height);
  1645. }
  1646. /**
  1647. * Returns the current x coordinate of the components origin.
  1648. * This method is preferable to writing
  1649. * <code>component.getBounds().x</code>,
  1650. * or <code>component.getLocation().x</code> because it doesn't
  1651. * cause any heap allocations.
  1652. *
  1653. * @return the current x coordinate of the components origin
  1654. * @since 1.2
  1655. */
  1656. public int getX() {
  1657. return x;
  1658. }
  1659. /**
  1660. * Returns the current y coordinate of the components origin.
  1661. * This method is preferable to writing
  1662. * <code>component.getBounds().y</code>,
  1663. * or <code>component.getLocation().y</code> because it
  1664. * doesn't cause any heap allocations.
  1665. *
  1666. * @return the current y coordinate of the components origin
  1667. * @since 1.2
  1668. */
  1669. public int getY() {
  1670. return y;
  1671. }
  1672. /**
  1673. * Returns the current width of this component.
  1674. * This method is preferable to writing
  1675. * <code>component.getBounds().width</code>,
  1676. * or <code>component.getSize().width</code> because it
  1677. * doesn't cause any heap allocations.
  1678. *
  1679. * @return the current width of this component
  1680. * @since 1.2
  1681. */
  1682. public int getWidth() {
  1683. return width;
  1684. }
  1685. /**
  1686. * Returns the current height of this component.
  1687. * This method is preferable to writing
  1688. * <code>component.getBounds().height</code.,
  1689. * or <code>component.getSize().height</code> because it
  1690. * doesn't cause any heap allocations.
  1691. *
  1692. * @return the current height of this component
  1693. * @since 1.2
  1694. */
  1695. public int getHeight() {
  1696. return height;
  1697. }
  1698. /**
  1699. * Stores the bounds of this component into "return value" <b>rv</b> and
  1700. * return <b>rv</b>. If rv is <code>null</code> a new
  1701. * <code>Rectangle</code> is allocated.
  1702. * This version of <code>getBounds</code> is useful if the caller
  1703. * wants to avoid allocating a new <code>Rectangle</code> object
  1704. * on the heap.
  1705. *
  1706. * @param rv the return value, modified to the components bounds
  1707. * @return rv
  1708. */
  1709. public Rectangle getBounds(Rectangle rv) {
  1710. if (rv == null) {
  1711. return new Rectangle(getX(), getY(), getWidth(), getHeight());
  1712. }
  1713. else {
  1714. rv.setBounds(getX(), getY(), getWidth(), getHeight());
  1715. return rv;
  1716. }
  1717. }
  1718. /**
  1719. * Stores the width/height of this component into "return value" <b>rv</b>
  1720. * and return <b>rv</b>. If rv is <code>null</code> a new
  1721. * <code>Dimension</code> object is allocated. This version of
  1722. * <code>getSize</code> is useful if the caller wants to avoid
  1723. * allocating a new <code>Dimension</code> object on the heap.
  1724. *
  1725. * @param rv the return value, modified to the components size
  1726. * @return rv
  1727. */
  1728. public Dimension getSize(Dimension rv) {
  1729. if (rv == null) {
  1730. return new Dimension(getWidth(), getHeight());
  1731. }
  1732. else {
  1733. rv.setSize(getWidth(), getHeight());
  1734. return rv;
  1735. }
  1736. }
  1737. /**
  1738. * Stores the x,y origin of this component into "return value" <b>rv</b>
  1739. * and return <b>rv</b>. If rv is <code>null</code> a new
  1740. * <code>Point</code> is allocated.
  1741. * This version of <code>getLocation</code> is useful if the
  1742. * caller wants to avoid allocating a new <code>Point</code>
  1743. * object on the heap.
  1744. *
  1745. * @param rv the return value, modified to the components location
  1746. * @return rv
  1747. */
  1748. public Point getLocation(Point rv) {
  1749. if (rv == null) {
  1750. return new Point(getX(), getY());
  1751. }
  1752. else {
  1753. rv.setLocation(getX(), getY());
  1754. return rv;
  1755. }
  1756. }
  1757. /**
  1758. * Returns true if this component is completely opaque, returns
  1759. * false by default.
  1760. * <p>
  1761. * An opaque component paints every pixel within its
  1762. * rectangular region. A non-opaque component paints only some of
  1763. * its pixels, allowing the pixels underneath it to "show through".
  1764. * A component that does not fully paint its pixels therefore
  1765. * provides a degree of transparency. Only lightweight
  1766. * components can be transparent.
  1767. * <p>
  1768. * Subclasses that guarantee to always completely paint their
  1769. * contents should override this method and return true. All
  1770. * of the "heavyweight" AWT components are opaque.
  1771. *
  1772. * @return true if this component is completely opaque
  1773. * @see #isLightweight
  1774. * @since 1.2
  1775. */
  1776. public boolean isOpaque() {
  1777. if (getPeer() == null) {
  1778. return false;
  1779. }
  1780. else {
  1781. return !isLightweight();
  1782. }
  1783. }
  1784. /**
  1785. * A lightweight component doesn't have a native toolkit peer.
  1786. * Subclasses of <code>Component</code> and <code>Container</code>,
  1787. * other than the ones defined in this package like <code>Button</code>
  1788. * or <code>Scrollbar</code>, are lightweight.
  1789. * All of the Swing components are lightweights.
  1790. * <p>
  1791. * This method will always return <code>false</code> if this component
  1792. * is not displayable because it is impossible to determine the
  1793. * weight of an undisplayable component.
  1794. *
  1795. * @return true if this component has a lightweight peer; false if
  1796. * it has a native peer or no peer
  1797. * @see #isDisplayable
  1798. * @since 1.2
  1799. */
  1800. public boolean isLightweight() {
  1801. return getPeer() instanceof LightweightPeer;
  1802. }
  1803. /**
  1804. * Gets the preferred size of this component.
  1805. * @return a dimension object indicating this component's preferred size
  1806. * @see #getMinimumSize
  1807. * @see LayoutManager
  1808. */
  1809. public Dimension getPreferredSize() {
  1810. return preferredSize();
  1811. }
  1812. /**
  1813. * @deprecated As of JDK version 1.1,
  1814. * replaced by <code>getPreferredSize()</code>.
  1815. */
  1816. public Dimension preferredSize() {
  1817. /* Avoid grabbing the lock if a reasonable cached size value
  1818. * is available.
  1819. */
  1820. Dimension dim = prefSize;
  1821. if (dim != null && isValid()) {
  1822. return dim;
  1823. }
  1824. synchronized (getTreeLock()) {
  1825. prefSize = (peer != null) ?
  1826. peer.preferredSize() :
  1827. getMinimumSize();
  1828. return prefSize;
  1829. }
  1830. }
  1831. /**
  1832. * Gets the mininimum size of this component.
  1833. * @return a dimension object indicating this component's minimum size
  1834. * @see #getPreferredSize
  1835. * @see LayoutManager
  1836. */
  1837. public Dimension getMinimumSize() {
  1838. return minimumSize();
  1839. }
  1840. /**
  1841. * @deprecated As of JDK version 1.1,
  1842. * replaced by <code>getMinimumSize()</code>.
  1843. */
  1844. public Dimension minimumSize() {
  1845. /* Avoid grabbing the lock if a reasonable cached size value
  1846. * is available.
  1847. */
  1848. Dimension dim = minSize;
  1849. if (dim != null && isValid()) {
  1850. return dim;
  1851. }
  1852. synchronized (getTreeLock()) {
  1853. minSize = (peer != null) ?
  1854. peer.minimumSize() :
  1855. size();
  1856. return minSize;
  1857. }
  1858. }
  1859. /**
  1860. * Gets the maximum size of this component.
  1861. * @return a dimension object indicating this component's maximum size
  1862. * @see #getMinimumSize
  1863. * @see #getPreferredSize
  1864. * @see LayoutManager
  1865. */
  1866. public Dimension getMaximumSize() {
  1867. return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
  1868. }
  1869. /**
  1870. * Returns the alignment along the x axis. This specifies how
  1871. * the component would like to be aligned relative to other
  1872. * components. The value should be a number between 0 and 1
  1873. * where 0 represents alignment along the origin, 1 is aligned
  1874. * the furthest away from the origin, 0.5 is centered, etc.
  1875. */
  1876. public float getAlignmentX() {
  1877. return CENTER_ALIGNMENT;
  1878. }
  1879. /**
  1880. * Returns the alignment along the y axis. This specifies how
  1881. * the component would like to be aligned relative to other
  1882. * components. The value should be a number between 0 and 1
  1883. * where 0 represents alignment along the origin, 1 is aligned
  1884. * the furthest away from the origin, 0.5 is centered, etc.
  1885. */
  1886. public float getAlignmentY() {
  1887. return CENTER_ALIGNMENT;
  1888. }
  1889. /**
  1890. * Prompts the layout manager to lay out this component. This is
  1891. * usually called when the component (more specifically, container)
  1892. * is validated.
  1893. * @see #validate
  1894. * @see LayoutManager
  1895. */
  1896. public void doLayout() {
  1897. layout();
  1898. }
  1899. /**
  1900. * @deprecated As of JDK version 1.1,
  1901. * replaced by <code>doLayout()</code>.
  1902. */
  1903. public void layout() {
  1904. }
  1905. /**
  1906. * Ensures that this component has a valid layout. This method is
  1907. * primarily intended to operate on instances of <code>Container</code>.
  1908. * @see #invalidate
  1909. * @see #doLayout()
  1910. * @see LayoutManager
  1911. * @see Container#validate
  1912. * @since JDK1.0
  1913. */
  1914. public void validate() {
  1915. if (!valid) {
  1916. synchronized (getTreeLock()) {
  1917. ComponentPeer peer = this.peer;
  1918. if (!valid && peer != null) {
  1919. Font newfont = getFont();
  1920. Font oldfont = peerFont;
  1921. if (newfont != oldfont && (oldfont == null
  1922. || !oldfont.equals(newfont))) {
  1923. peer.setFont(newfont);
  1924. peerFont = newfont;
  1925. }
  1926. }
  1927. }
  1928. valid = true;
  1929. }
  1930. }
  1931. /**
  1932. * Invalidates this component. This component and all parents
  1933. * above it are marked as needing to be laid out. This method can
  1934. * be called often, so it needs to execute quickly.
  1935. * @see #validate
  1936. * @see #doLayout
  1937. * @see LayoutManager
  1938. * @since JDK1.0
  1939. */
  1940. public void invalidate() {
  1941. synchronized (getTreeLock()) {
  1942. /* Nullify cached layout and size information.
  1943. * For efficiency, propagate invalidate() upwards only if
  1944. * some other component hasn't already done so first.
  1945. */
  1946. valid = false;
  1947. prefSize = null;
  1948. minSize = null;
  1949. if (parent != null && parent.valid) {
  1950. parent.invalidate();
  1951. }
  1952. }
  1953. }
  1954. /**
  1955. * Creates a graphics context for this component. This method will
  1956. * return <code>null</code> if this component is currently not
  1957. * displayable.
  1958. * @return a graphics context for this component, or <code>null</code>
  1959. * if it has none
  1960. * @see #paint
  1961. * @since JDK1.0
  1962. */
  1963. public Graphics getGraphics() {
  1964. if (peer instanceof LightweightPeer) {
  1965. // This is for a lightweight component, need to
  1966. // translate coordinate spaces and clip relative
  1967. // to the parent.
  1968. if (parent == null) return null;
  1969. Graphics g = parent.getGraphics();
  1970. if (g == null) return null;
  1971. if (g instanceof ConstrainableGraphics) {
  1972. ((ConstrainableGraphics) g).constrain(x, y, width, height);
  1973. } else {
  1974. g.translate(x,y);
  1975. g.setClip(0, 0, width, height);
  1976. }
  1977. g.setFont(getFont());
  1978. return g;
  1979. } else {
  1980. ComponentPeer peer = this.peer;
  1981. return (peer != null) ? peer.getGraphics() : null;
  1982. }
  1983. }
  1984. /** saves an internal cache of FontMetrics for better performance **/
  1985. static java.util.Hashtable metrics = new java.util.Hashtable();
  1986. /**
  1987. * Gets the font metrics for the specified font.
  1988. * @param font the font for which font metrics is to be
  1989. * obtained
  1990. * @return the font metrics for <code>font</code>
  1991. * @see #getFont
  1992. * @see #getPeer
  1993. * @see java.awt.peer.ComponentPeer#getFontMetrics(Font)
  1994. * @see Toolkit#getFontMetrics(Font)
  1995. * @since JDK1.0
  1996. */
  1997. public FontMetrics getFontMetrics(Font font) {
  1998. FontMetrics result = (FontMetrics) metrics.get(font);
  1999. if (result != null) {
  2000. return result;
  2001. }
  2002. // REMIND: PlatformFont flag should be obsolete soon...
  2003. if (sun.awt.font.NativeFontWrapper.usePlatformFontMetrics()) {
  2004. if (peer != null &&
  2005. !(peer instanceof LightweightPeer)) {
  2006. result = peer.getFontMetrics(font);
  2007. metrics.put(font, result);
  2008. return result;
  2009. }
  2010. }
  2011. if (parent != null) {
  2012. // These are the lines that cost the big dollars. Calling
  2013. // parent.getGraphics triggers the construcion (at great
  2014. // expense) of a new Graphics object that is then quickly
  2015. // discarded. - Graham
  2016. Graphics g = parent.getGraphics();
  2017. if (g != null) {
  2018. try {
  2019. result = g.getFontMetrics(font);
  2020. metrics.put(font, result);
  2021. return result;
  2022. } finally {
  2023. g.dispose();
  2024. }
  2025. }
  2026. }
  2027. result = getToolkit().getFontMetrics(font);
  2028. metrics.put(font, result);
  2029. return result;
  2030. }
  2031. /**
  2032. * Sets the cursor image to the specified cursor. This cursor
  2033. * image is displayed when the <code>contains</code> method for
  2034. * this component returns true for the current cursor location, and
  2035. * this Component is visible, displayable, and enabled. Setting the
  2036. * cursor of a <code>Container</code> causes that cursor to be displayed
  2037. * within all of the container's subcomponents, except for those
  2038. * that have a non-<code>null</code> cursor.
  2039. *
  2040. * @param cursor One of the constants defined
  2041. * by the <code>Cursor</code> class;
  2042. * if this parameter is <code>null</code>
  2043. * then this component will inherit
  2044. * the cursor of its parent
  2045. * @see #isEnabled
  2046. * @see #isShowing
  2047. * @see #getCursor
  2048. * @see #contains
  2049. * @see Toolkit#createCustomCursor
  2050. * @see Cursor
  2051. * @since JDK1.1
  2052. */
  2053. public void setCursor(Cursor cursor) {
  2054. this.cursor = cursor;
  2055. updateCursorImmediately();
  2056. }
  2057. /**
  2058. * Updates the cursor. May not be invoked from the native
  2059. * message pump.
  2060. */
  2061. final void updateCursorImmediately() {
  2062. if (peer instanceof LightweightPeer) {
  2063. Container nativeContainer = getNativeContainer();
  2064. if (nativeContainer == null) return;
  2065. ComponentPeer cPeer = nativeContainer.getPeer();
  2066. if (cPeer != null) {
  2067. cPeer.updateCursorImmediately();
  2068. }
  2069. } else if (peer != null) {
  2070. peer.updateCursorImmediately();
  2071. }
  2072. }
  2073. /**
  2074. * Gets the cursor set in the component. If the component does
  2075. * not have a cursor set, the cursor of its parent is returned.
  2076. * If no cursor is set in the entire hierarchy,
  2077. * <code>Cursor.DEFAULT_CURSOR</code> is returned.
  2078. * @see #setCursor
  2079. * @since JDK1.1
  2080. */
  2081. public Cursor getCursor() {
  2082. Cursor cursor = this.cursor;
  2083. if (cursor != null) {
  2084. return cursor;
  2085. }
  2086. Container parent = this.parent;
  2087. if (parent != null) {
  2088. return parent.getCursor();
  2089. } else {
  2090. return Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
  2091. }
  2092. }
  2093. /**
  2094. * Returns whether the cursor has been explicitly set for this Component.
  2095. * If this method returns <code>false</code>, this Component is inheriting
  2096. * its cursor from an ancestor.
  2097. *
  2098. * @return <code>true</code> if the cursor has been explicitly set for this
  2099. * Component; <code>false</code> otherwise.
  2100. * @since 1.4
  2101. */
  2102. public boolean isCursorSet() {
  2103. return (cursor != null);
  2104. }
  2105. /**
  2106. * Paints this component.
  2107. * <p>
  2108. * This method is called when the contents of the component should
  2109. * be painted; such as when the component is first being shown or
  2110. * is damaged and in need of repair. The clip rectangle in the
  2111. * <code>Graphics</code> parameter is set to the area
  2112. * which needs to be painted.
  2113. * Subclasses of <code>Component</code> that override this
  2114. * method need not call <code>super.paint(g)</code>.
  2115. * <p>
  2116. * For performance reasons, <code>Component</code>s with zero width
  2117. * or height aren't considered to need painting when they are first shown,
  2118. * and also aren't considered to need repair.
  2119. * <p>
  2120. * <b>Note</b>: For more information on the paint mechanisms utilitized
  2121. * by AWT and Swing, including information on how to write the most
  2122. * efficient painting code, see
  2123. * <a href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html">Painting in AWT and Swing</a>.
  2124. *
  2125. * @param g the graphics context to use for painting
  2126. * @see #update
  2127. * @since JDK1.0
  2128. */
  2129. public void paint(Graphics g) {
  2130. }
  2131. /**
  2132. * Updates this component.
  2133. * <p>
  2134. * If this component is not a lightweight component, the
  2135. * AWT calls the <code>update</code> method in response to
  2136. * a call to <code>repaint</code>. You can assume that
  2137. * the background is not cleared.
  2138. * <p>
  2139. * The <code>update</code>method of <code>Component</code>
  2140. * calls this component's <code>paint</code> method to redraw
  2141. * this component. This method is commonly overridden by subclasses
  2142. * which need to do additional work in response to a call to
  2143. * <code>repaint</code>.
  2144. * Subclasses of Component that override this method should either
  2145. * call <code>super.update(g)</code>, or call <code>paint</code> directly.
  2146. * <p>
  2147. * The origin of the graphics context, its
  2148. * (<code>0</code>, <code>0</code>) coordinate point, is the
  2149. * top-left corner of this component. The clipping region of the
  2150. * graphics context is the bounding rectangle of this component.
  2151. *
  2152. * <p>
  2153. * <b>Note</b>: For more information on the paint mechanisms utilitized
  2154. * by AWT and Swing, including information on how to write the most
  2155. * efficient painting code, see
  2156. * <a href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html">Painting in AWT and Swing</a>.
  2157. *
  2158. * @param g the specified context to use for updating
  2159. * @see #paint
  2160. * @see #repaint()
  2161. * @since JDK1.0
  2162. */
  2163. public void update(Graphics g) {
  2164. paint(g);
  2165. }
  2166. /**
  2167. * Paints this component and all of its subcomponents.
  2168. * <p>
  2169. * The origin of the graphics context, its
  2170. * (<code>0</code>, <code>0</code>) coordinate point, is the
  2171. * top-left corner of this component. The clipping region of the
  2172. * graphics context is the bounding rectangle of this component.
  2173. *
  2174. * @param g the graphics context to use for painting
  2175. * @see #paint
  2176. * @since JDK1.0
  2177. */
  2178. public void paintAll(Graphics g) {
  2179. if (isShowing()) {
  2180. GraphicsCallback.PeerPaintCallback.getInstance().
  2181. runOneComponent(this, new Rectangle(0, 0, width, height),
  2182. g, g.getClip(),
  2183. GraphicsCallback.LIGHTWEIGHTS |
  2184. GraphicsCallback.HEAVYWEIGHTS);
  2185. }
  2186. }
  2187. /**
  2188. * Simulates the peer callbacks into java.awt for painting of
  2189. * lightweight Components.
  2190. * @param g the graphics context to use for painting
  2191. * @see #paintAll
  2192. */
  2193. void lightweightPaint(Graphics g) {
  2194. paint(g);
  2195. }
  2196. /**
  2197. * Paints all the heavyweight subcomponents.
  2198. */
  2199. void paintHeavyweightComponents(Graphics g) {
  2200. }
  2201. /**
  2202. * Repaints this component.
  2203. * <p>
  2204. * If this component is a lightweight component, this method
  2205. * causes a call to this component's <code>paint</code>
  2206. * method as soon as possible. Otherwise, this method causes
  2207. * a call to this component's <code>update</code> method as soon
  2208. * as possible.
  2209. * <p>
  2210. * <b>Note</b>: For more information on the paint mechanisms utilitized
  2211. * by AWT and Swing, including information on how to write the most
  2212. * efficient painting code, see
  2213. * <a href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html">Painting in AWT and Swing</a>.
  2214. *
  2215. * @see #update(Graphics)
  2216. * @since JDK1.0
  2217. */
  2218. public void repaint() {
  2219. repaint(0, 0, 0, width, height);
  2220. }
  2221. /**
  2222. * Repaints the component. If this component is a lightweight
  2223. * component, this results in a call to <code>paint</code>
  2224. * within <code>tm</code> milliseconds.
  2225. * <p>
  2226. * <b>Note</b>: For more information on the paint mechanisms utilitized
  2227. * by AWT and Swing, including information on how to write the most
  2228. * efficient painting code, see
  2229. * <a href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html">Painting in AWT and Swing</a>.
  2230. *
  2231. * @param tm maximum time in milliseconds before update
  2232. * @see #paint
  2233. * @see #update(Graphics)
  2234. * @since JDK1.0
  2235. */
  2236. public void repaint(long tm) {
  2237. repaint(tm, 0, 0, width, height);
  2238. }
  2239. /**
  2240. * Repaints the specified rectangle of this component.
  2241. * <p>
  2242. * If this component is a lightweight component, this method
  2243. * causes a call to this component's <code>paint</code> method
  2244. * as soon as possible. Otherwise, this method causes a call to
  2245. * this component's <code>update</code> method as soon as possible.
  2246. * <p>
  2247. * <b>Note</b>: For more information on the paint mechanisms utilitized
  2248. * by AWT and Swing, including information on how to write the most
  2249. * efficient painting code, see
  2250. * <a href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html">Painting in AWT and Swing</a>.
  2251. *
  2252. * @param x the <i>x</i> coordinate
  2253. * @param y the <i>y</i> coordinate
  2254. * @param width the width
  2255. * @param height the height
  2256. * @see #update(Graphics)
  2257. * @since JDK1.0
  2258. */
  2259. public void repaint(int x, int y, int width, int height) {
  2260. repaint(0, x, y, width, height);
  2261. }
  2262. /**
  2263. * Repaints the specified rectangle of this component within
  2264. * <code>tm</code> milliseconds.
  2265. * <p>
  2266. * If this component is a lightweight component, this method causes
  2267. * a call to this component's <code>paint</code> method.
  2268. * Otherwise, this method causes a call to this component's
  2269. * <code>update</code> method.
  2270. * <p>
  2271. * <b>Note</b>: For more information on the paint mechanisms utilitized
  2272. * by AWT and Swing, including information on how to write the most
  2273. * efficient painting code, see
  2274. * <a href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html">Painting in AWT and Swing</a>.
  2275. *
  2276. * @param tm maximum time in milliseconds before update
  2277. * @param x the <i>x</i> coordinate
  2278. * @param y the <i>y</i> coordinate
  2279. * @param width the width
  2280. * @param height the height
  2281. * @see #update(Graphics)
  2282. * @since JDK1.0
  2283. */
  2284. public void repaint(long tm, int x, int y, int width, int height) {
  2285. if (this.peer instanceof LightweightPeer) {
  2286. // Needs to be translated to parent coordinates since
  2287. // a parent native container provides the actual repaint
  2288. // services. Additionally, the request is restricted to
  2289. // the bounds of the component.
  2290. if (parent != null) {
  2291. int px = this.x + ((x < 0) ? 0 : x);
  2292. int py = this.y + ((y < 0) ? 0 : y);
  2293. int pwidth = (width > this.width) ? this.width : width;
  2294. int pheight = (height > this.height) ? this.height : height;
  2295. parent.repaint(tm, px, py, pwidth, pheight);
  2296. }
  2297. } else {
  2298. if (isVisible() && (this.peer != null) &&
  2299. (width > 0) && (height > 0)) {
  2300. PaintEvent e = new PaintEvent(this, PaintEvent.UPDATE,
  2301. new Rectangle(x, y, width, height));
  2302. Toolkit.getEventQueue().postEvent(e);
  2303. }
  2304. }
  2305. }
  2306. /**
  2307. * Prints this component. Applications should override this method
  2308. * for components that must do special processing before being
  2309. * printed or should be printed differently than they are painted.
  2310. * <p>
  2311. * The default implementation of this method calls the
  2312. * <code>paint</code> method.
  2313. * <p>
  2314. * The origin of the graphics context, its
  2315. * (<code>0</code>, <code>0</code>) coordinate point, is the
  2316. * top-left corner of this component. The clipping region of the
  2317. * graphics context is the bounding rectangle of this component.
  2318. * @param g the graphics context to use for printing
  2319. * @see #paint(Graphics)
  2320. * @since JDK1.0
  2321. */
  2322. public void print(Graphics g) {
  2323. paint(g);
  2324. }
  2325. /**
  2326. * Prints this component and all of its subcomponents.
  2327. * <p>
  2328. * The origin of the graphics context, its
  2329. * (<code>0</code>, <code>0</code>) coordinate point, is the
  2330. * top-left corner of this component. The clipping region of the
  2331. * graphics context is the bounding rectangle of this component.
  2332. * @param g the graphics context to use for printing
  2333. * @see #print(Graphics)
  2334. * @since JDK1.0
  2335. */
  2336. public void printAll(Graphics g) {
  2337. if (isShowing()) {
  2338. GraphicsCallback.PeerPrintCallback.getInstance().
  2339. runOneComponent(this, new Rectangle(0, 0, width, height),
  2340. g, g.getClip(),
  2341. GraphicsCallback.LIGHTWEIGHTS |
  2342. GraphicsCallback.HEAVYWEIGHTS);
  2343. }
  2344. }
  2345. /**
  2346. * Simulates the peer callbacks into java.awt for printing of
  2347. * lightweight Components.
  2348. * @param g the graphics context to use for printing
  2349. * @see #printAll
  2350. */
  2351. void lightweightPrint(Graphics g) {
  2352. print(g);
  2353. }
  2354. /**
  2355. * Prints all the heavyweight subcomponents.
  2356. */
  2357. void printHeavyweightComponents(Graphics g) {
  2358. }
  2359. /**
  2360. * Repaints the component when the image has changed.
  2361. * This <code>imageUpdate</code> method of an <code>ImageObserver</code>
  2362. * is called when more information about an
  2363. * image which had been previously requested using an asynchronous
  2364. * routine such as the <code>drawImage</code> method of
  2365. * <code>Graphics</code> becomes available.
  2366. * See the definition of <code>imageUpdate</code> for
  2367. * more information on this method and its arguments.
  2368. * <p>
  2369. * The <code>imageUpdate</code> method of <code>Component</code>
  2370. * incrementally draws an image on the component as more of the bits
  2371. * of the image are available.
  2372. * <p>
  2373. * If the system property <code>awt.image.incrementaldraw</code>
  2374. * is missing or has the value <code>true</code>, the image is
  2375. * incrementally drawn. If the system property has any other value,
  2376. * then the image is not drawn until it has been completely loaded.
  2377. * <p>
  2378. * Also, if incremental drawing is in effect, the value of the
  2379. * system property <code>awt.image.redrawrate</code> is interpreted
  2380. * as an integer to give the maximum redraw rate, in milliseconds. If
  2381. * the system property is missing or cannot be interpreted as an
  2382. * integer, the redraw rate is once every 100ms.
  2383. * <p>
  2384. * The interpretation of the <code>x</code>, <code>y</code>,
  2385. * <code>width</code>, and <code>height</code> arguments depends on
  2386. * the value of the <code>infoflags</code> argument.
  2387. *
  2388. * @param img the image being observed
  2389. * @param infoflags see <code>imageUpdate</code> for more information
  2390. * @param x the <i>x</i> coordinate
  2391. * @param y the <i>y</i> coordinate
  2392. * @param w the width
  2393. * @param h the height
  2394. * @return <code>false</code> if the infoflags indicate that the
  2395. * image is completely loaded; <code>true</code> otherwise.
  2396. *
  2397. * @see java.awt.image.ImageObserver
  2398. * @see Graphics#drawImage(Image, int, int, Color, java.awt.image.ImageObserver)
  2399. * @see Graphics#drawImage(Image, int, int, java.awt.image.ImageObserver)
  2400. * @see Graphics#drawImage(Image, int, int, int, int, Color, java.awt.image.ImageObserver)
  2401. * @see Graphics#drawImage(Image, int, int, int, int, java.awt.image.ImageObserver)
  2402. * @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
  2403. * @since JDK1.0
  2404. */
  2405. public boolean imageUpdate(Image img, int infoflags,
  2406. int x, int y, int w, int h) {
  2407. int rate = -1;
  2408. if ((infoflags & (FRAMEBITS|ALLBITS)) != 0) {
  2409. rate = 0;
  2410. } else if ((infoflags & SOMEBITS) != 0) {
  2411. if (isInc) {
  2412. rate = incRate;
  2413. if (rate < 0) {
  2414. rate = 0;
  2415. }
  2416. }
  2417. }
  2418. if (rate >= 0) {
  2419. repaint(rate, 0, 0, width, height);
  2420. }
  2421. return (infoflags & (ALLBITS|ABORT)) == 0;
  2422. }
  2423. /**
  2424. * Creates an image from the specified image producer.
  2425. * @param producer the image producer
  2426. * @return the image produced
  2427. * @since JDK1.0
  2428. */
  2429. public Image createImage(ImageProducer producer) {
  2430. ComponentPeer peer = this.peer;
  2431. if ((peer != null) && ! (peer instanceof LightweightPeer)) {
  2432. return peer.createImage(producer);
  2433. }
  2434. return getToolkit().createImage(producer);
  2435. }
  2436. /**
  2437. * Creates an off-screen drawable image
  2438. * to be used for double buffering.
  2439. * @param width the specified width
  2440. * @param height the specified height
  2441. * @return an off-screen drawable image, which can be used for double
  2442. * buffering. The return value may be <code>null</code> if the
  2443. * component is not displayable. This will always happen if
  2444. * <code>GraphicsEnvironment.isHeadless()</code> returns
  2445. * <code>true</code>.
  2446. * @see #isDisplayable
  2447. * @see GraphicsEnvironment#isHeadless
  2448. * @since JDK1.0
  2449. */
  2450. public Image createImage(int width, int height) {
  2451. ComponentPeer peer = this.peer;
  2452. if (peer instanceof LightweightPeer) {
  2453. if (parent != null) { return parent.createImage(width, height); }
  2454. else { return null;}
  2455. } else {
  2456. return (peer != null) ? peer.createImage(width, height) : null;
  2457. }
  2458. }
  2459. /**
  2460. * Creates a volatile off-screen drawable image
  2461. * to be used for double buffering.
  2462. * @param width the specified width.
  2463. * @param height the specified height.
  2464. * @return an off-screen drawable image, which can be used for double
  2465. * buffering. The return value may be <code>null</code> if the
  2466. * component is not displayable. This will always happen if
  2467. * <code>GraphicsEnvironment.isHeadless()</code> returns
  2468. * <code>true</code>.
  2469. * @see java.awt.image.VolatileImage
  2470. * @see #isDisplayable
  2471. * @see GraphicsEnvironment#isHeadless
  2472. * @since 1.4
  2473. */
  2474. public VolatileImage createVolatileImage(int width, int height) {
  2475. ComponentPeer peer = this.peer;
  2476. if (peer instanceof LightweightPeer) {
  2477. if (parent != null) {
  2478. return parent.createVolatileImage(width, height);
  2479. }
  2480. else { return null;}
  2481. } else {
  2482. return (peer != null) ?
  2483. peer.createVolatileImage(width, height) : null;
  2484. }
  2485. }
  2486. /**
  2487. * Creates a volatile off-screen drawable image, with the given capabilities.
  2488. * The contents of this image may be lost at any time due
  2489. * to operating system issues, so the image must be managed
  2490. * via the <code>VolatileImage</code> interface.
  2491. * @param width the specified width.
  2492. * @param height the specified height.
  2493. * @param caps the image capabilities
  2494. * @exception AWTException if an image with the specified capabilities cannot
  2495. * be created
  2496. * @return a VolatileImage object, which can be used
  2497. * to manage surface contents loss and capabilities.
  2498. * @see java.awt.image.VolatileImage
  2499. * @since 1.4
  2500. */
  2501. public VolatileImage createVolatileImage(int width, int height,
  2502. ImageCapabilities caps) throws AWTException {
  2503. // REMIND : check caps
  2504. return createVolatileImage(width, height);
  2505. }
  2506. /**
  2507. * Prepares an image for rendering on this component. The image
  2508. * data is downloaded asynchronously in another thread and the
  2509. * appropriate screen representation of the image is generated.
  2510. * @param image the <code>Image</code> for which to
  2511. * prepare a screen representation
  2512. * @param observer the <code>ImageObserver</code> object
  2513. * to be notified as the image is being prepared
  2514. * @return <code>true</code> if the image has already been fully
  2515. * prepared; <code>false</code> otherwise
  2516. * @since JDK1.0
  2517. */
  2518. public boolean prepareImage(Image image, ImageObserver observer) {
  2519. return prepareImage(image, -1, -1, observer);
  2520. }
  2521. /**
  2522. * Prepares an image for rendering on this component at the
  2523. * specified width and height.
  2524. * <p>
  2525. * The image data is downloaded asynchronously in another thread,
  2526. * and an appropriately scaled screen representation of the image is
  2527. * generated.
  2528. * @param image the instance of <code>Image</code>
  2529. * for which to prepare a screen representation
  2530. * @param width the width of the desired screen representation
  2531. * @param height the height of the desired screen representation
  2532. * @param observer the <code>ImageObserver</code> object
  2533. * to be notified as the image is being prepared
  2534. * @return <code>true</code> if the image has already been fully
  2535. * prepared; <code>false</code> otherwise
  2536. * @see java.awt.image.ImageObserver
  2537. * @since JDK1.0
  2538. */
  2539. public boolean prepareImage(Image image, int width, int height,
  2540. ImageObserver observer) {
  2541. ComponentPeer peer = this.peer;
  2542. if (peer instanceof LightweightPeer) {
  2543. return (parent != null)
  2544. ? parent.prepareImage(image, width, height, observer)
  2545. : getToolkit().prepareImage(image, width, height, observer);
  2546. } else {
  2547. return (peer != null)
  2548. ? peer.prepareImage(image, width, height, observer)
  2549. : getToolkit().prepareImage(image, width, height, observer);
  2550. }
  2551. }
  2552. /**
  2553. * Returns the status of the construction of a screen representation
  2554. * of the specified image.
  2555. * <p>
  2556. * This method does not cause the image to begin loading. An
  2557. * application must use the <code>prepareImage</code> method
  2558. * to force the loading of an image.
  2559. * <p>
  2560. * Information on the flags returned by this method can be found
  2561. * with the discussion of the <code>ImageObserver</code> interface.
  2562. * @param image the <code>Image</code> object whose status
  2563. * is being checked
  2564. * @param observer the <code>ImageObserver</code>
  2565. * object to be notified as the image is being prepared
  2566. * @return the bitwise inclusive <b>OR</b> of
  2567. * <code>ImageObserver</code> flags indicating what
  2568. * information about the image is currently available
  2569. * @see #prepareImage(Image, int, int, java.awt.image.ImageObserver)
  2570. * @see Toolkit#checkImage(Image, int, int, java.awt.image.ImageObserver)
  2571. * @see java.awt.image.ImageObserver
  2572. * @since JDK1.0
  2573. */
  2574. public int checkImage(Image image, ImageObserver observer) {
  2575. return checkImage(image, -1, -1, observer);
  2576. }
  2577. /**
  2578. * Returns the status of the construction of a screen representation
  2579. * of the specified image.
  2580. * <p>
  2581. * This method does not cause the image to begin loading. An
  2582. * application must use the <code>prepareImage</code> method
  2583. * to force the loading of an image.
  2584. * <p>
  2585. * The <code>checkImage</code> method of <code>Component</code>
  2586. * calls its peer's <code>checkImage</code> method to calculate
  2587. * the flags. If this component does not yet have a peer, the
  2588. * component's toolkit's <code>checkImage</code> method is called
  2589. * instead.
  2590. * <p>
  2591. * Information on the flags returned by this method can be found
  2592. * with the discussion of the <code>ImageObserver</code> interface.
  2593. * @param image the <code>Image</code> object whose status
  2594. * is being checked
  2595. * @param width the width of the scaled version
  2596. * whose status is to be checked
  2597. * @param height the height of the scaled version
  2598. * whose status is to be checked
  2599. * @param observer the <code>ImageObserver</code> object
  2600. * to be notified as the image is being prepared
  2601. * @return the bitwise inclusive <b>OR</b> of
  2602. * <code>ImageObserver</code> flags indicating what
  2603. * information about the image is currently available
  2604. * @see #prepareImage(Image, int, int, java.awt.image.ImageObserver)
  2605. * @see Toolkit#checkImage(Image, int, int, java.awt.image.ImageObserver)
  2606. * @see java.awt.image.ImageObserver
  2607. * @since JDK1.0
  2608. */
  2609. public int checkImage(Image image, int width, int height,
  2610. ImageObserver observer) {
  2611. ComponentPeer peer = this.peer;
  2612. if (peer instanceof LightweightPeer) {
  2613. return (parent != null)
  2614. ? parent.checkImage(image, width, height, observer)
  2615. : getToolkit().checkImage(image, width, height, observer);
  2616. } else {
  2617. return (peer != null)
  2618. ? peer.checkImage(image, width, height, observer)
  2619. : getToolkit().checkImage(image, width, height, observer);
  2620. }
  2621. }
  2622. /**
  2623. * Creates a new strategy for multi-buffering on this component.
  2624. * Multi-buffering is useful for rendering performance. This method
  2625. * attempts to create the best strategy available with the number of
  2626. * buffers supplied. It will always create a <code>BufferStrategy</code>
  2627. * with that number of buffers.
  2628. * A page-flipping strategy is attempted first, then a blitting strategy
  2629. * using accelerated buffers. Finally, an unaccelerated blitting
  2630. * strategy is used.
  2631. * <p>
  2632. * Each time this method is called,
  2633. * the existing buffer strategy for this component is discarded.
  2634. * @param numBuffers number of buffers to create, including the front buffer
  2635. * @exception IllegalArgumentException if numBuffers is less than 1.
  2636. * @exception IllegalStateException if the component is not displayable
  2637. * @see #isDisplayable
  2638. * @see #getBufferStrategy()
  2639. * @since 1.4
  2640. */
  2641. void createBufferStrategy(int numBuffers) {
  2642. BufferCapabilities bufferCaps;
  2643. if (numBuffers > 1) {
  2644. // Try to create a page-flipping strategy
  2645. bufferCaps = new BufferCapabilities(
  2646. new ImageCapabilities(true), new ImageCapabilities(true),
  2647. BufferCapabilities.FlipContents.UNDEFINED);
  2648. try {
  2649. createBufferStrategy(numBuffers, bufferCaps);
  2650. return; // Success
  2651. } catch (AWTException e) {
  2652. // Failed
  2653. }
  2654. }
  2655. // Try a blitting (but still accelerated) strategy
  2656. bufferCaps = new BufferCapabilities(
  2657. new ImageCapabilities(true), new ImageCapabilities(true), null);
  2658. try {
  2659. createBufferStrategy(numBuffers, bufferCaps);
  2660. return; // Success
  2661. } catch (AWTException e) {
  2662. // Failed
  2663. }
  2664. // Try an unaccelerated blitting strategy
  2665. bufferCaps = new BufferCapabilities(
  2666. new ImageCapabilities(false), new ImageCapabilities(false), null);
  2667. try {
  2668. createBufferStrategy(numBuffers, bufferCaps);
  2669. return; // Success
  2670. } catch (AWTException e) {
  2671. // Failed
  2672. }
  2673. // Code should never reach here (an unaccelerated blitting
  2674. // strategy should always work)
  2675. throw new InternalError("Could not create a buffer strategy");
  2676. }
  2677. /**
  2678. * Creates a new strategy for multi-buffering on this component with the
  2679. * required buffer capabilities. This is useful, for example, if only
  2680. * accelerated memory or page flipping is desired (as specified by the
  2681. * buffer capabilities).
  2682. * <p>
  2683. * Each time this method
  2684. * is called, the existing buffer strategy for this component is discarded.
  2685. * @param numBuffers number of buffers to create
  2686. * @param caps the required capabilities for creating the buffer strategy;
  2687. * cannot be <code>null</code>
  2688. * @exception AWTException if the capabilities supplied could not be
  2689. * supported or met; this may happen, for example, if there is not enough
  2690. * accelerated memory currently available, or if page flipping is specified
  2691. * but not possible.
  2692. * @exception IllegalArgumentException if numBuffers is less than 1, or if
  2693. * caps is <code>null</code>
  2694. * @see #getBufferStrategy()
  2695. * @since 1.4
  2696. */
  2697. void createBufferStrategy(int numBuffers,
  2698. BufferCapabilities caps) throws AWTException {
  2699. // Check arguments
  2700. if (numBuffers < 1) {
  2701. throw new IllegalArgumentException(
  2702. "Number of buffers must be at least 1");
  2703. }
  2704. if (caps == null) {
  2705. throw new IllegalArgumentException("No capabilities specified");
  2706. }
  2707. // Destroy old buffers
  2708. if (bufferStrategy instanceof FlipBufferStrategy) {
  2709. ((FlipBufferStrategy)bufferStrategy).destroyBuffers();
  2710. }
  2711. if (numBuffers == 1) {
  2712. bufferStrategy = new SingleBufferStrategy(caps);
  2713. } else {
  2714. // assert numBuffers > 1;
  2715. if (caps.isPageFlipping()) {
  2716. bufferStrategy = new FlipBufferStrategy(numBuffers, caps);
  2717. } else {
  2718. bufferStrategy = new BltBufferStrategy(numBuffers, caps);
  2719. }
  2720. }
  2721. }
  2722. /**
  2723. * @return the buffer strategy used by this component
  2724. * @see Window#createBufferStrategy
  2725. * @see Canvas#createBufferStrategy
  2726. * @since 1.4
  2727. */
  2728. BufferStrategy getBufferStrategy() {
  2729. if (bufferStrategy == null) {
  2730. createBufferStrategy(1);
  2731. }
  2732. return bufferStrategy;
  2733. }
  2734. /**
  2735. * @return the back buffer currently used by this component's
  2736. * BufferStrategy. If there is no BufferStrategy or no
  2737. * back buffer, this method returns null.
  2738. */
  2739. Image getBackBuffer() {
  2740. if (bufferStrategy != null) {
  2741. if (bufferStrategy instanceof BltBufferStrategy) {
  2742. BltBufferStrategy bltBS = (BltBufferStrategy)bufferStrategy;
  2743. return bltBS.getBackBuffer();
  2744. } else if (bufferStrategy instanceof FlipBufferStrategy) {
  2745. FlipBufferStrategy flipBS = (FlipBufferStrategy)bufferStrategy;
  2746. return flipBS.getBackBuffer();
  2747. }
  2748. }
  2749. return null;
  2750. }
  2751. /**
  2752. * Inner class for flipping buffers on a component. That component must
  2753. * be a <code>Canvas</code> or <code>Window</code>.
  2754. * @see Canvas
  2755. * @see Window
  2756. * @see java.awt.image.BufferStrategy
  2757. * @author Michael Martak
  2758. * @since 1.4
  2759. */
  2760. protected class FlipBufferStrategy extends BufferStrategy {
  2761. /**
  2762. * The number of buffers
  2763. */
  2764. protected int numBuffers; // = 0
  2765. /**
  2766. * The buffering capabilities
  2767. */
  2768. protected BufferCapabilities caps; // = null
  2769. /**
  2770. * The drawing buffer
  2771. */
  2772. protected Image drawBuffer; // = null
  2773. /**
  2774. * The drawing buffer as a volatile image
  2775. */
  2776. protected VolatileImage drawVBuffer; // = null
  2777. /**
  2778. * Whether or not the drawing buffer has been recently restored from
  2779. * a lost state.
  2780. */
  2781. protected boolean validatedContents; // = false
  2782. /**
  2783. * Creates a new flipping buffer strategy for this component.
  2784. * The component must be a <code>Canvas</code> or <code>Window</code>.
  2785. * @see Canvas
  2786. * @see Window
  2787. * @param numBuffers the number of buffers
  2788. * @param caps the capabilities of the buffers
  2789. * @exception AWTException if the capabilities supplied could not be
  2790. * supported or met
  2791. * @exception ClassCastException if the component is not a canvas or
  2792. * window.
  2793. */
  2794. protected FlipBufferStrategy(int numBuffers, BufferCapabilities caps)
  2795. throws AWTException {
  2796. if (!(Component.this instanceof Window) &&
  2797. !(Component.this instanceof Canvas)) {
  2798. throw new ClassCastException(
  2799. "Component must be a Canvas or Window");
  2800. }
  2801. this.numBuffers = numBuffers;
  2802. this.caps = caps;
  2803. createBuffers(numBuffers, caps);
  2804. }
  2805. /**
  2806. * Creates one or more complex, flipping buffers with the given
  2807. * capabilities.
  2808. * @param numBuffers number of buffers to create; must be greater than
  2809. * one
  2810. * @param caps the capabilities of the buffers.
  2811. * <code>BufferCapabilities.isPageFlipping</code> must be
  2812. * <code>true</code>.
  2813. * @exception AWTException if the capabilities supplied could not be
  2814. * supported or met
  2815. * @exception IllegalStateException if the component has no peer
  2816. * @exception IllegalArgumentException if numBuffers is less than two,
  2817. * or if <code>BufferCapabilities.isPageFlipping</code> is not
  2818. * <code>true</code>.
  2819. * @see java.awt.BufferCapabilities#isPageFlipping()
  2820. */
  2821. protected void createBuffers(int numBuffers, BufferCapabilities caps)
  2822. throws AWTException {
  2823. if (numBuffers < 2) {
  2824. throw new IllegalArgumentException(
  2825. "Number of buffers cannot be less than two");
  2826. } else if (peer == null) {
  2827. throw new IllegalStateException(
  2828. "Component must have a valid peer");
  2829. } else if (caps == null || !caps.isPageFlipping()) {
  2830. throw new IllegalArgumentException(
  2831. "Page flipping capabilities must be specified");
  2832. } else {
  2833. peer.createBuffers(numBuffers, caps);
  2834. }
  2835. }
  2836. /**
  2837. * @return direct access to the back buffer, as an image.
  2838. * @exception IllegalStateException if the buffers have not yet
  2839. * been created
  2840. */
  2841. protected Image getBackBuffer() {
  2842. if (peer != null) {
  2843. Image drawBuffer = peer.getBackBuffer();
  2844. if (drawBuffer instanceof VolatileImage) {
  2845. drawVBuffer = (VolatileImage)drawBuffer;
  2846. }
  2847. revalidate();
  2848. return drawBuffer;
  2849. } else {
  2850. throw new IllegalStateException(
  2851. "Component must have a valid peer");
  2852. }
  2853. }
  2854. /**
  2855. * Flipping moves the contents of the back buffer to the front buffer,
  2856. * either by copying or by moving the video pointer.
  2857. * @param flipAction an integer value describing the flipping action
  2858. * for the contents of the back buffer. This should be one of the
  2859. * values of the <code>BufferCapabilities.FlipContents</code>
  2860. * property.
  2861. * @exception IllegalStateException if the buffers have not yet
  2862. * been created
  2863. * @see java.awt.BufferCapabilities#getFlipContents()
  2864. */
  2865. protected void flip(BufferCapabilities.FlipContents flipAction) {
  2866. if (peer != null) {
  2867. peer.flip(flipAction);
  2868. } else {
  2869. throw new IllegalStateException(
  2870. "Component must have a valid peer");
  2871. }
  2872. }
  2873. /**
  2874. * Destroys the buffers created through this object
  2875. */
  2876. protected void destroyBuffers() {
  2877. if (peer != null) {
  2878. peer.destroyBuffers();
  2879. } else {
  2880. throw new IllegalStateException(
  2881. "Component must have a valid peer");
  2882. }
  2883. }
  2884. /**
  2885. * @return the buffering capabilities of this strategy
  2886. */
  2887. public BufferCapabilities getCapabilities() {
  2888. return caps;
  2889. }
  2890. /**
  2891. * @return the graphics on the drawing buffer. This method may not
  2892. * be synchronized for performance reasons; use of this method by multiple
  2893. * threads should be handled at the application level. Disposal of the
  2894. * graphics object must be handled by the application.
  2895. */
  2896. public Graphics getDrawGraphics() {
  2897. if (drawBuffer == null) {
  2898. // Set up drawing buffer
  2899. drawBuffer = getBackBuffer();
  2900. if (drawBuffer instanceof VolatileImage) {
  2901. drawVBuffer = (VolatileImage)drawBuffer;
  2902. }
  2903. }
  2904. revalidate();
  2905. return drawBuffer.getGraphics();
  2906. }
  2907. /**
  2908. * Restore the drawing buffer if it has been lost
  2909. */
  2910. protected void revalidate() {
  2911. // REMIND: this whole validation mechanism needs to be re-examined
  2912. // for correctness. Currently, the value of validatedContents
  2913. // may be changed and overwritten before the user has a chance to
  2914. // see that there was any restoration to the surface. Also, we
  2915. // need to handle the IMAGE_INCOMPATIBLE case.
  2916. if (drawVBuffer != null) {
  2917. validatedContents = (drawVBuffer.validate(
  2918. getGraphicsConfiguration()) == VolatileImage.IMAGE_RESTORED);
  2919. } else {
  2920. validatedContents = false;
  2921. }
  2922. }
  2923. /**
  2924. * @return whether the drawing buffer was lost since the last call to
  2925. * <code>getDrawGraphics</code>
  2926. */
  2927. public boolean contentsLost() {
  2928. if (drawVBuffer == null) {
  2929. return false;
  2930. }
  2931. return drawVBuffer.contentsLost();
  2932. }
  2933. /**
  2934. * @return whether the drawing buffer was recently restored from a lost
  2935. * state and reinitialized to the default background color (white)
  2936. */
  2937. public boolean contentsRestored() {
  2938. return validatedContents;
  2939. }
  2940. /**
  2941. * Makes the next available buffer visible by either blitting or
  2942. * flipping.
  2943. */
  2944. public void show() {
  2945. flip(caps.getFlipContents());
  2946. }
  2947. } // Inner class FlipBufferStrategy
  2948. /**
  2949. * Inner class for blitting offscreen surfaces to a component.
  2950. *
  2951. * @author Michael Martak
  2952. * @since 1.4
  2953. */
  2954. protected class BltBufferStrategy extends BufferStrategy {
  2955. /**
  2956. * The buffering capabilities
  2957. */
  2958. protected BufferCapabilities caps; // = null
  2959. /**
  2960. * The back buffers
  2961. */
  2962. protected VolatileImage[] backBuffers; // = null
  2963. /**
  2964. * Whether or not the drawing buffer has been recently restored from
  2965. * a lost state.
  2966. */
  2967. protected boolean validatedContents; // = false
  2968. /**
  2969. * Size of the back buffers
  2970. */
  2971. protected int width;
  2972. protected int height;
  2973. /**
  2974. * Creates a new blt buffer strategy around a component
  2975. * @param numBuffers the component to use as the front buffer
  2976. * @param caps the capabilities of the buffers
  2977. */
  2978. protected BltBufferStrategy(int numBuffers, BufferCapabilities caps) {
  2979. this.caps = caps;
  2980. createBackBuffers(numBuffers - 1);
  2981. }
  2982. /**
  2983. * Creates the back buffers
  2984. */
  2985. protected void createBackBuffers(int numBuffers) {
  2986. if (numBuffers == 0) {
  2987. backBuffers = null;
  2988. } else {
  2989. width = getWidth();
  2990. height = getHeight();
  2991. backBuffers = new VolatileImage[numBuffers];
  2992. for (int i = 0; i < numBuffers; i++) {
  2993. backBuffers[i] = createVolatileImage(width, height);
  2994. }
  2995. }
  2996. }
  2997. /**
  2998. * @return the buffering capabilities of this strategy
  2999. */
  3000. public BufferCapabilities getCapabilities() {
  3001. return caps;
  3002. }
  3003. /**
  3004. * @return the draw graphics
  3005. */
  3006. public Graphics getDrawGraphics() {
  3007. revalidate();
  3008. Image backBuffer = getBackBuffer();
  3009. if (backBuffer == null) {
  3010. return getGraphics();
  3011. }
  3012. return backBuffer.getGraphics();
  3013. }
  3014. /**
  3015. * @return direct access to the back buffer, as an image.
  3016. * If there is no back buffer, returns null.
  3017. */
  3018. Image getBackBuffer() {
  3019. if (backBuffers != null) {
  3020. return backBuffers[backBuffers.length - 1];
  3021. } else {
  3022. return null;
  3023. }
  3024. }
  3025. /**
  3026. * Makes the next available buffer visible.
  3027. */
  3028. public void show() {
  3029. if (backBuffers == null) {
  3030. return;
  3031. }
  3032. Graphics g = getGraphics();
  3033. try {
  3034. for (int i = 0; i < backBuffers.length; i++) {
  3035. g.drawImage(backBuffers[i], 0, 0, Component.this);
  3036. g.dispose();
  3037. g = null;
  3038. g = backBuffers[i].getGraphics();
  3039. }
  3040. } finally {
  3041. if (g != null) {
  3042. g.dispose();
  3043. }
  3044. }
  3045. }
  3046. /**
  3047. * Restore the drawing buffer if it has been lost
  3048. */
  3049. protected void revalidate() {
  3050. if (backBuffers == null) {
  3051. validatedContents = false;
  3052. } else if (getWidth() != width || getHeight() != height) {
  3053. createBackBuffers(backBuffers.length);
  3054. validatedContents = true;
  3055. } else {
  3056. validatedContents =
  3057. (backBuffers[backBuffers.length - 1].validate(
  3058. getGraphicsConfiguration())
  3059. == VolatileImage.IMAGE_RESTORED);
  3060. // REMIND : handle IMAGE_INCOMPATIBLE
  3061. }
  3062. }
  3063. /**
  3064. * @return whether the drawing buffer was lost since the last call to
  3065. * <code>getDrawGraphics</code>
  3066. */
  3067. public boolean contentsLost() {
  3068. if (width < getWidth() || height < getHeight()) {
  3069. return true;
  3070. } else {
  3071. return backBuffers[backBuffers.length - 1].contentsLost();
  3072. }
  3073. }
  3074. /**
  3075. * @return whether the drawing buffer was recently restored from a lost
  3076. * state and reinitialized to the default background color (white)
  3077. */
  3078. public boolean contentsRestored() {
  3079. return validatedContents;
  3080. }
  3081. } // Inner class BltBufferStrategy
  3082. /**
  3083. * Inner class for flipping buffers on a component. That component must
  3084. * be a <code>Canvas</code> or <code>Window</code>.
  3085. * @see Canvas
  3086. * @see Window
  3087. * @see java.awt.image.BufferStrategy
  3088. * @author Michael Martak
  3089. * @since 1.4
  3090. */
  3091. private class SingleBufferStrategy extends BufferStrategy {
  3092. private BufferCapabilities caps;
  3093. public SingleBufferStrategy(BufferCapabilities caps) {
  3094. this.caps = caps;
  3095. }
  3096. public BufferCapabilities getCapabilities() {
  3097. return caps;
  3098. }
  3099. public Graphics getDrawGraphics() {
  3100. return getGraphics();
  3101. }
  3102. public boolean contentsLost() {
  3103. // return peer.getSurfaceData().contentsLost();
  3104. return false;
  3105. }
  3106. public boolean contentsRestored() {
  3107. // return peer.getSurfaceData().validate();
  3108. return false;
  3109. }
  3110. public void show() {
  3111. // Do nothing; could repaint
  3112. }
  3113. } // Inner class SingleBufferStrategy
  3114. /**
  3115. * Sets whether or not paint messages received from the operating system
  3116. * should be ignored. This does not affect paint events generated in
  3117. * software by the AWT, unless they are an immediate response to an
  3118. * OS-level paint message.
  3119. * <p>
  3120. * This is useful, for example, if running under full-screen mode and
  3121. * better performance is desired, or if page-flipping is used as the
  3122. * buffer strategy.
  3123. *
  3124. * @since 1.4
  3125. * @see #getIgnoreRepaint
  3126. * @see Canvas#createBufferStrategy
  3127. * @see Window#createBufferStrategy
  3128. * @see java.awt.image.BufferStrategy
  3129. * @see GraphicsDevice#setFullScreenWindow
  3130. */
  3131. public void setIgnoreRepaint(boolean ignoreRepaint) {
  3132. this.ignoreRepaint = ignoreRepaint;
  3133. }
  3134. /**
  3135. * @return whether or not paint messages received from the operating system
  3136. * should be ignored.
  3137. *
  3138. * @since 1.4
  3139. * @see #setIgnoreRepaint
  3140. */
  3141. public boolean getIgnoreRepaint() {
  3142. return ignoreRepaint;
  3143. }
  3144. /**
  3145. * Checks whether this component "contains" the specified point,
  3146. * where <code>x</code> and <code>y</code> are defined to be
  3147. * relative to the coordinate system of this component.
  3148. * @param x the <i>x</i> coordinate of the point
  3149. * @param y the <i>y</i> coordinate of the point
  3150. * @see #getComponentAt(int, int)
  3151. * @since JDK1.1
  3152. */
  3153. public boolean contains(int x, int y) {
  3154. return inside(x, y);
  3155. }
  3156. /**
  3157. * @deprecated As of JDK version 1.1,
  3158. * replaced by contains(int, int).
  3159. */
  3160. public boolean inside(int x, int y) {
  3161. return (x >= 0) && (x < width) && (y >= 0) && (y < height);
  3162. }
  3163. /**
  3164. * Checks whether this component "contains" the specified point,
  3165. * where the point's <i>x</i> and <i>y</i> coordinates are defined
  3166. * to be relative to the coordinate system of this component.
  3167. * @param p the point
  3168. * @see #getComponentAt(Point)
  3169. * @since JDK1.1
  3170. */
  3171. public boolean contains(Point p) {
  3172. return contains(p.x, p.y);
  3173. }
  3174. /**
  3175. * Determines if this component or one of its immediate
  3176. * subcomponents contains the (<i>x</i>, <i>y</i>) location,
  3177. * and if so, returns the containing component. This method only
  3178. * looks one level deep. If the point (<i>x</i>, <i>y</i>) is
  3179. * inside a subcomponent that itself has subcomponents, it does not
  3180. * go looking down the subcomponent tree.
  3181. * <p>
  3182. * The <code>locate</code> method of <code>Component</code> simply
  3183. * returns the component itself if the (<i>x</i>, <i>y</i>)
  3184. * coordinate location is inside its bounding box, and <code>null</code>
  3185. * otherwise.
  3186. * @param x the <i>x</i> coordinate
  3187. * @param y the <i>y</i> coordinate
  3188. * @return the component or subcomponent that contains the
  3189. * (<i>x</i>, <i>y</i>) location;
  3190. * <code>null</code> if the location
  3191. * is outside this component
  3192. * @see #contains(int, int)
  3193. * @since JDK1.0
  3194. */
  3195. public Component getComponentAt(int x, int y) {
  3196. return locate(x, y);
  3197. }
  3198. /**
  3199. * @deprecated As of JDK version 1.1,
  3200. * replaced by getComponentAt(int, int).
  3201. */
  3202. public Component locate(int x, int y) {
  3203. return contains(x, y) ? this : null;
  3204. }
  3205. /**
  3206. * Returns the component or subcomponent that contains the
  3207. * specified point.
  3208. * @param p the point
  3209. * @see java.awt.Component#contains
  3210. * @since JDK1.1
  3211. */
  3212. public Component getComponentAt(Point p) {
  3213. return getComponentAt(p.x, p.y);
  3214. }
  3215. /**
  3216. * @deprecated As of JDK version 1.1,
  3217. * replaced by <code>dispatchEvent(AWTEvent e)</code>.
  3218. */
  3219. public void deliverEvent(Event e) {
  3220. postEvent(e);
  3221. }
  3222. /**
  3223. * Dispatches an event to this component or one of its sub components.
  3224. * Calls <code>processEvent</code> before returning for 1.1-style
  3225. * events which have been enabled for the <code>Component</code>.
  3226. * @param e the event
  3227. */
  3228. public final void dispatchEvent(AWTEvent e) {
  3229. dispatchEventImpl(e);
  3230. }
  3231. void dispatchEventImpl(AWTEvent e) {
  3232. int id = e.getID();
  3233. /*
  3234. * 0. Set timestamp and modifiers of current event.
  3235. */
  3236. EventQueue.setCurrentEventAndMostRecentTime(e);
  3237. /*
  3238. * 1. Pre-dispatchers. Do any necessary retargeting/reordering here
  3239. * before we notify AWTEventListeners.
  3240. */
  3241. if (e instanceof SunDropTargetEvent) {
  3242. ((SunDropTargetEvent)e).dispatch();
  3243. return;
  3244. }
  3245. if (!e.focusManagerIsDispatching) {
  3246. // Invoke the private focus retargeting method which provides
  3247. // lightweight Component support
  3248. e = KeyboardFocusManager.retargetFocusEvent(e);
  3249. // Now, with the event properly targeted to a lightweight
  3250. // descendant if necessary, invoke the public focus retargeting
  3251. // and dispatching function
  3252. if (KeyboardFocusManager.getCurrentKeyboardFocusManager().
  3253. dispatchEvent(e))
  3254. {
  3255. return;
  3256. }
  3257. }
  3258. // MouseWheel may need to be retargeted here so that
  3259. // AWTEventListener sees the event go to the correct
  3260. // Component. If the MouseWheelEvent needs to go to an ancestor,
  3261. // the event is dispatched to the ancestor, and dispatching here
  3262. // stops.
  3263. if (id == MouseEvent.MOUSE_WHEEL &&
  3264. (!eventTypeEnabled(id)) &&
  3265. (peer != null && !peer.handlesWheelScrolling()) &&
  3266. (dispatchMouseWheelToAncestor((MouseWheelEvent)e)))
  3267. {
  3268. return;
  3269. }
  3270. /*
  3271. * 2. Allow the Toolkit to pass this to AWTEventListeners.
  3272. */
  3273. Toolkit toolkit = Toolkit.getDefaultToolkit();
  3274. toolkit.notifyAWTEventListeners(e);
  3275. /*
  3276. * 3. If no one has consumed a key event, allow the
  3277. * KeyboardFocusManager to process it.
  3278. */
  3279. if (!e.isConsumed()) {
  3280. if (e instanceof java.awt.event.KeyEvent) {
  3281. KeyboardFocusManager.getCurrentKeyboardFocusManager().
  3282. processKeyEvent(this, (KeyEvent)e);
  3283. if (e.isConsumed()) {
  3284. return;
  3285. }
  3286. }
  3287. }
  3288. /*
  3289. * 4. Allow input methods to process the event
  3290. */
  3291. if (areInputMethodsEnabled()
  3292. && (
  3293. // We need to pass on InputMethodEvents since some host
  3294. // input method adapters send them through the Java
  3295. // event queue instead of directly to the component,
  3296. // and the input context also handles the Java composition window
  3297. ((e instanceof InputMethodEvent) && !(this instanceof CompositionArea))
  3298. ||
  3299. // Otherwise, we only pass on input and focus events, because
  3300. // a) input methods shouldn't know about semantic or component-level events
  3301. // b) passing on the events takes time
  3302. // c) isConsumed() is always true for semantic events.
  3303. (e instanceof InputEvent) || (e instanceof FocusEvent))) {
  3304. InputContext inputContext = getInputContext();
  3305. if (inputContext != null) {
  3306. inputContext.dispatchEvent(e);
  3307. if (e.isConsumed()) {
  3308. return;
  3309. }
  3310. }
  3311. }
  3312. /*
  3313. * 5. Pre-process any special events before delivery
  3314. */
  3315. switch(id) {
  3316. // Handling of the PAINT and UPDATE events is now done in the
  3317. // peer's handleEvent() method so the background can be cleared
  3318. // selectively for non-native components on Windows only.
  3319. // - Fred.Ecks@Eng.sun.com, 5-8-98
  3320. case KeyEvent.KEY_PRESSED:
  3321. case KeyEvent.KEY_RELEASED:
  3322. Container p = (Container)((this instanceof Container) ? this : parent);
  3323. if (p != null) {
  3324. p.preProcessKeyEvent((KeyEvent)e);
  3325. if (e.isConsumed()) {
  3326. return;
  3327. }
  3328. }
  3329. break;
  3330. case WindowEvent.WINDOW_CLOSING:
  3331. if (toolkit instanceof WindowClosingListener) {
  3332. windowClosingException = ((WindowClosingListener)
  3333. toolkit).windowClosingNotify((WindowEvent)e);
  3334. if (checkWindowClosingException()) {
  3335. return;
  3336. }
  3337. }
  3338. break;
  3339. default:
  3340. break;
  3341. }
  3342. /*
  3343. * 6. Deliver event for normal processing
  3344. */
  3345. if (newEventsOnly) {
  3346. // Filtering needs to really be moved to happen at a lower
  3347. // level in order to get maximum performance gain; it is
  3348. // here temporarily to ensure the API spec is honored.
  3349. //
  3350. if (eventEnabled(e)) {
  3351. processEvent(e);
  3352. }
  3353. } else if (id == MouseEvent.MOUSE_WHEEL) {
  3354. // newEventsOnly will be false for a listenerless ScrollPane, but
  3355. // MouseWheelEvents still need to be dispatched to it so scrolling
  3356. // can be done.
  3357. autoProcessMouseWheel((MouseWheelEvent)e);
  3358. } else if (!(e instanceof MouseEvent && !postsOldMouseEvents())) {
  3359. //
  3360. // backward compatibility
  3361. //
  3362. Event olde = e.convertToOld();
  3363. if (olde != null) {
  3364. int key = olde.key;
  3365. int modifiers = olde.modifiers;
  3366. postEvent(olde);
  3367. if (olde.isConsumed()) {
  3368. e.consume();
  3369. }
  3370. // if target changed key or modifier values, copy them
  3371. // back to original event
  3372. //
  3373. switch(olde.id) {
  3374. case Event.KEY_PRESS:
  3375. case Event.KEY_RELEASE:
  3376. case Event.KEY_ACTION:
  3377. case Event.KEY_ACTION_RELEASE:
  3378. if (olde.key != key) {
  3379. ((KeyEvent)e).setKeyChar(olde.getKeyEventChar());
  3380. }
  3381. if (olde.modifiers != modifiers) {
  3382. ((KeyEvent)e).setModifiers(olde.modifiers);
  3383. }
  3384. break;
  3385. default:
  3386. break;
  3387. }
  3388. }
  3389. }
  3390. /*
  3391. * 8. Special handling for 4061116 : Hook for browser to close modal
  3392. * dialogs.
  3393. */
  3394. if (id == WindowEvent.WINDOW_CLOSING && !e.isConsumed()) {
  3395. if (toolkit instanceof WindowClosingListener) {
  3396. windowClosingException =
  3397. ((WindowClosingListener)toolkit).
  3398. windowClosingDelivered((WindowEvent)e);
  3399. if (checkWindowClosingException()) {
  3400. return;
  3401. }
  3402. }
  3403. }
  3404. /*
  3405. * 9. Allow the peer to process the event.
  3406. * Except KeyEvents, they will be processed by peer after
  3407. * all KeyEventPostProcessors
  3408. * (see DefaultKeyboardFocusManager.dispatchKeyEvent())
  3409. */
  3410. if (!(e instanceof KeyEvent) && (peer != null)) {
  3411. peer.handleEvent(e);
  3412. }
  3413. } // dispatchEventImpl()
  3414. /*
  3415. * If newEventsOnly is false, method is called so that ScrollPane can
  3416. * override it and handle common-case mouse wheel scrolling. NOP
  3417. * for Component.
  3418. */
  3419. void autoProcessMouseWheel(MouseWheelEvent e) {}
  3420. /*
  3421. * Dispatch given MouseWheelEvent to the first ancestor for which
  3422. * MouseWheelEvents are enabled.
  3423. *
  3424. * Returns whether or not event was dispatched to an ancestor
  3425. */
  3426. boolean dispatchMouseWheelToAncestor(MouseWheelEvent e) {
  3427. int newX, newY;
  3428. newX = e.getX() + getX(); // Coordinates take into account at least
  3429. newY = e.getY() + getY(); // the cursor's position relative to this
  3430. // Component (e.getX()), and this Component's
  3431. // position relative to its parent.
  3432. MouseWheelEvent newMWE;
  3433. if (dbg.on) {
  3434. dbg.println("Component.dispatchMouseWheelToAncestor");
  3435. dbg.println("orig event src is of " + e.getSource().getClass());
  3436. }
  3437. /* parent field for Window refers to the owning Window.
  3438. * MouseWheelEvents should NOT be propagated into owning Windows
  3439. */
  3440. synchronized (getTreeLock()) {
  3441. Container anc = getParent();
  3442. while (anc != null && !anc.eventEnabled(e)) {
  3443. // fix coordinates to be relative to new event source
  3444. newX += anc.getX();
  3445. newY += anc.getY();
  3446. if (!(anc instanceof Window)) {
  3447. anc = anc.getParent();
  3448. }
  3449. else {
  3450. break;
  3451. }
  3452. }
  3453. if (dbg.on) dbg.println("new event src is " + anc.getClass());
  3454. if (anc != null && anc.eventEnabled(e)) {
  3455. // Change event to be from new source, with new x,y
  3456. // For now, just create a new event - yucky
  3457. newMWE = new MouseWheelEvent(anc, // new source
  3458. e.getID(),
  3459. e.getWhen(),
  3460. e.getModifiers(),
  3461. newX, // x relative to new source
  3462. newY, // y relative to new source
  3463. e.getClickCount(),
  3464. e.isPopupTrigger(),
  3465. e.getScrollType(),
  3466. e.getScrollAmount(),
  3467. e.getWheelRotation());
  3468. ((AWTEvent)e).copyPrivateDataInto(newMWE);
  3469. anc.dispatchEventImpl(newMWE);
  3470. }
  3471. }
  3472. return true;
  3473. }
  3474. boolean checkWindowClosingException() {
  3475. if (windowClosingException != null) {
  3476. if (this instanceof Dialog) {
  3477. ((Dialog)this).interruptBlocking();
  3478. } else {
  3479. windowClosingException.fillInStackTrace();
  3480. windowClosingException.printStackTrace();
  3481. windowClosingException = null;
  3482. }
  3483. return true;
  3484. }
  3485. return false;
  3486. }
  3487. boolean areInputMethodsEnabled() {
  3488. // in 1.2, we assume input method support is required for all
  3489. // components that handle key events, but components can turn off
  3490. // input methods by calling enableInputMethods(false).
  3491. return ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0) &&
  3492. ((eventMask & AWTEvent.KEY_EVENT_MASK) != 0 || keyListener != null);
  3493. }
  3494. // REMIND: remove when filtering is handled at lower level
  3495. boolean eventEnabled(AWTEvent e) {
  3496. return eventTypeEnabled(e.id);
  3497. }
  3498. boolean eventTypeEnabled(int type) {
  3499. switch(type) {
  3500. case ComponentEvent.COMPONENT_MOVED:
  3501. case ComponentEvent.COMPONENT_RESIZED:
  3502. case ComponentEvent.COMPONENT_SHOWN:
  3503. case ComponentEvent.COMPONENT_HIDDEN:
  3504. if ((eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
  3505. componentListener != null) {
  3506. return true;
  3507. }
  3508. break;
  3509. case FocusEvent.FOCUS_GAINED:
  3510. case FocusEvent.FOCUS_LOST:
  3511. if ((eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0 ||
  3512. focusListener != null) {
  3513. return true;
  3514. }
  3515. break;
  3516. case KeyEvent.KEY_PRESSED:
  3517. case KeyEvent.KEY_RELEASED:
  3518. case KeyEvent.KEY_TYPED:
  3519. if ((eventMask & AWTEvent.KEY_EVENT_MASK) != 0 ||
  3520. keyListener != null) {
  3521. return true;
  3522. }
  3523. break;
  3524. case MouseEvent.MOUSE_PRESSED:
  3525. case MouseEvent.MOUSE_RELEASED:
  3526. case MouseEvent.MOUSE_ENTERED:
  3527. case MouseEvent.MOUSE_EXITED:
  3528. case MouseEvent.MOUSE_CLICKED:
  3529. if ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0 ||
  3530. mouseListener != null) {
  3531. return true;
  3532. }
  3533. break;
  3534. case MouseEvent.MOUSE_MOVED:
  3535. case MouseEvent.MOUSE_DRAGGED:
  3536. if ((eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0 ||
  3537. mouseMotionListener != null) {
  3538. return true;
  3539. }
  3540. break;
  3541. case MouseEvent.MOUSE_WHEEL:
  3542. if ((eventMask & AWTEvent.MOUSE_WHEEL_EVENT_MASK) != 0 ||
  3543. mouseWheelListener != null) {
  3544. return true;
  3545. }
  3546. break;
  3547. case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
  3548. case InputMethodEvent.CARET_POSITION_CHANGED:
  3549. if ((eventMask & AWTEvent.INPUT_METHOD_EVENT_MASK) != 0 ||
  3550. inputMethodListener != null) {
  3551. return true;
  3552. }
  3553. break;
  3554. case HierarchyEvent.HIERARCHY_CHANGED:
  3555. if ((eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0 ||
  3556. hierarchyListener != null) {
  3557. return true;
  3558. }
  3559. break;
  3560. case HierarchyEvent.ANCESTOR_MOVED:
  3561. case HierarchyEvent.ANCESTOR_RESIZED:
  3562. if ((eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0 ||
  3563. hierarchyBoundsListener != null) {
  3564. return true;
  3565. }
  3566. break;
  3567. case ActionEvent.ACTION_PERFORMED:
  3568. if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0) {
  3569. return true;
  3570. }
  3571. break;
  3572. case TextEvent.TEXT_VALUE_CHANGED:
  3573. if ((eventMask & AWTEvent.TEXT_EVENT_MASK) != 0) {
  3574. return true;
  3575. }
  3576. break;
  3577. case ItemEvent.ITEM_STATE_CHANGED:
  3578. if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0) {
  3579. return true;
  3580. }
  3581. break;
  3582. case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
  3583. if ((eventMask & AWTEvent.ADJUSTMENT_EVENT_MASK) != 0) {
  3584. return true;
  3585. }
  3586. break;
  3587. default:
  3588. break;
  3589. }
  3590. //
  3591. // Always pass on events defined by external programs.
  3592. //
  3593. if (type > AWTEvent.RESERVED_ID_MAX) {
  3594. return true;
  3595. }
  3596. return false;
  3597. }
  3598. /**
  3599. * @deprecated As of JDK version 1.1,
  3600. * replaced by dispatchEvent(AWTEvent).
  3601. */
  3602. public boolean postEvent(Event e) {
  3603. ComponentPeer peer = this.peer;
  3604. if (handleEvent(e)) {
  3605. e.consume();
  3606. return true;
  3607. }
  3608. Component parent = this.parent;
  3609. int eventx = e.x;
  3610. int eventy = e.y;
  3611. if (parent != null) {
  3612. e.translate(x, y);
  3613. if (parent.postEvent(e)) {
  3614. e.consume();
  3615. return true;
  3616. }
  3617. // restore coords
  3618. e.x = eventx;
  3619. e.y = eventy;
  3620. }
  3621. return false;
  3622. }
  3623. // Event source interfaces
  3624. /**
  3625. * Adds the specified component listener to receive component events from
  3626. * this component.
  3627. * If listener <code>l</code> is <code>null</code>,
  3628. * no exception is thrown and no action is performed.
  3629. * @param l the component listene.
  3630. * @see java.awt.event.ComponentEvent
  3631. * @see java.awt.event.ComponentListener
  3632. * @see #removeComponentListener
  3633. * @see #getComponentListeners
  3634. * @since JDK1.1
  3635. */
  3636. public synchronized void addComponentListener(ComponentListener l) {
  3637. if (l == null) {
  3638. return;
  3639. }
  3640. componentListener = AWTEventMulticaster.add(componentListener, l);
  3641. newEventsOnly = true;
  3642. }
  3643. /**
  3644. * Removes the specified component listener so that it no longer
  3645. * receives component events from this component. This method performs
  3646. * no function, nor does it throw an exception, if the listener
  3647. * specified by the argument was not previously added to this component.
  3648. * If listener <code>l</code> is <code>null</code>,
  3649. * no exception is thrown and no action is performed.
  3650. * @param l the component listener
  3651. * @see java.awt.event.ComponentEvent
  3652. * @see java.awt.event.ComponentListener
  3653. * @see #addComponentListener
  3654. * @see #getComponentListeners
  3655. * @since JDK1.1
  3656. */
  3657. public synchronized void removeComponentListener(ComponentListener l) {
  3658. if (l == null) {
  3659. return;
  3660. }
  3661. componentListener = AWTEventMulticaster.remove(componentListener, l);
  3662. }
  3663. /**
  3664. * Returns an array of all the component listeners
  3665. * registered on this component.
  3666. *
  3667. * @return all of this comonent's <code>ComponentListener</code>s
  3668. * or an empty array if no component
  3669. * listeners are currently registered
  3670. *
  3671. * @see #addComponentListener
  3672. * @see #removeComponentListener
  3673. * @since 1.4
  3674. */
  3675. public synchronized ComponentListener[] getComponentListeners() {
  3676. return (ComponentListener[]) (getListeners(ComponentListener.class));
  3677. }
  3678. /**
  3679. * Adds the specified focus listener to receive focus events from
  3680. * this component when this component gains input focus.
  3681. * If listener <code>l</code> is <code>null</code>,
  3682. * no exception is thrown and no action is performed.
  3683. *
  3684. * @param l the focus listener
  3685. * @see java.awt.event.FocusEvent
  3686. * @see java.awt.event.FocusListener
  3687. * @see #removeFocusListener
  3688. * @see #getFocusListeners
  3689. * @since JDK1.1
  3690. */
  3691. public synchronized void addFocusListener(FocusListener l) {
  3692. if (l == null) {
  3693. return;
  3694. }
  3695. focusListener = AWTEventMulticaster.add(focusListener, l);
  3696. newEventsOnly = true;
  3697. // if this is a lightweight component, enable focus events
  3698. // in the native container.
  3699. if (peer instanceof LightweightPeer) {
  3700. parent.proxyEnableEvents(AWTEvent.FOCUS_EVENT_MASK);
  3701. }
  3702. }
  3703. /**
  3704. * Removes the specified focus listener so that it no longer
  3705. * receives focus events from this component. This method performs
  3706. * no function, nor does it throw an exception, if the listener
  3707. * specified by the argument was not previously added to this component.
  3708. * If listener <code>l</code> is <code>null</code>,
  3709. * no exception is thrown and no action is performed.
  3710. *
  3711. * @param l the focus listener
  3712. * @see java.awt.event.FocusEvent
  3713. * @see java.awt.event.FocusListener
  3714. * @see #addFocusListener
  3715. * @see #getFocusListeners
  3716. * @since JDK1.1
  3717. */
  3718. public synchronized void removeFocusListener(FocusListener l) {
  3719. if (l == null) {
  3720. return;
  3721. }
  3722. focusListener = AWTEventMulticaster.remove(focusListener, l);
  3723. }
  3724. /**
  3725. * Returns an array of all the focus listeners
  3726. * registered on this component.
  3727. *
  3728. * @return all of this component's <code>FocusListener</code>s
  3729. * or an empty array if no component
  3730. * listeners are currently registered
  3731. *
  3732. * @see #addFocusListener
  3733. * @see #removeFocusListener
  3734. * @since 1.4
  3735. */
  3736. public synchronized FocusListener[] getFocusListeners() {
  3737. return (FocusListener[]) (getListeners(FocusListener.class));
  3738. }
  3739. /**
  3740. * Adds the specified hierarchy listener to receive hierarchy changed
  3741. * events from this component when the hierarchy to which this container
  3742. * belongs changes.
  3743. * If listener <code>l</code> is <code>null</code>,
  3744. * no exception is thrown and no action is performed.
  3745. *
  3746. * @param l the hierarchy listener
  3747. * @see java.awt.event.HierarchyEvent
  3748. * @see java.awt.event.HierarchyListener
  3749. * @see #removeHierarchyListener
  3750. * @see #getHierarchyListeners
  3751. * @since 1.3
  3752. */
  3753. public void addHierarchyListener(HierarchyListener l) {
  3754. if (l == null) {
  3755. return;
  3756. }
  3757. boolean notifyAncestors;
  3758. synchronized (this) {
  3759. notifyAncestors =
  3760. (hierarchyListener == null &&
  3761. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) == 0);
  3762. hierarchyListener = AWTEventMulticaster.add(hierarchyListener, l);
  3763. notifyAncestors = (notifyAncestors && hierarchyListener != null);
  3764. newEventsOnly = true;
  3765. }
  3766. if (notifyAncestors) {
  3767. synchronized (getTreeLock()) {
  3768. adjustListeningChildrenOnParent(AWTEvent.HIERARCHY_EVENT_MASK,
  3769. 1);
  3770. }
  3771. }
  3772. }
  3773. /**
  3774. * Removes the specified hierarchy listener so that it no longer
  3775. * receives hierarchy changed events from this component. This method
  3776. * performs no function, nor does it throw an exception, if the listener
  3777. * specified by the argument was not previously added to this component.
  3778. * If listener <code>l</code> is <code>null</code>,
  3779. * no exception is thrown and no action is performed.
  3780. *
  3781. * @param l the hierarchy listener
  3782. * @see java.awt.event.HierarchyEvent
  3783. * @see java.awt.event.HierarchyListener
  3784. * @see #addHierarchyListener
  3785. * @see #getHierarchyListeners
  3786. * @since 1.3
  3787. */
  3788. public void removeHierarchyListener(HierarchyListener l) {
  3789. if (l == null) {
  3790. return;
  3791. }
  3792. boolean notifyAncestors;
  3793. synchronized (this) {
  3794. notifyAncestors =
  3795. (hierarchyListener != null &&
  3796. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) == 0);
  3797. hierarchyListener =
  3798. AWTEventMulticaster.remove(hierarchyListener, l);
  3799. notifyAncestors = (notifyAncestors && hierarchyListener == null);
  3800. }
  3801. if (notifyAncestors) {
  3802. synchronized (getTreeLock()) {
  3803. adjustListeningChildrenOnParent(AWTEvent.HIERARCHY_EVENT_MASK,
  3804. -1);
  3805. }
  3806. }
  3807. }
  3808. /**
  3809. * Returns an array of all the hierarchy listeners
  3810. * registered on this component.
  3811. *
  3812. * @return all of this component's <code>HierarchyListener</code>s
  3813. * or an empty array if no hierarchy
  3814. * listeners are currently registered
  3815. *
  3816. * @see #addHierarchyListener
  3817. * @see #removeHierarchyListener
  3818. * @since 1.4
  3819. */
  3820. public synchronized HierarchyListener[] getHierarchyListeners() {
  3821. return (HierarchyListener[])(getListeners(HierarchyListener.class));
  3822. }
  3823. /**
  3824. * Adds the specified hierarchy bounds listener to receive hierarchy
  3825. * bounds events from this component when the hierarchy to which this
  3826. * container belongs changes.
  3827. * If listener <code>l</code> is <code>null</code>,
  3828. * no exception is thrown and no action is performed.
  3829. *
  3830. * @param l the hierarchy bounds listener
  3831. * @see java.awt.event.HierarchyEvent
  3832. * @see java.awt.event.HierarchyBoundsListener
  3833. * @see #removeHierarchyBoundsListener
  3834. * @see #getHierarchyBoundsListeners
  3835. * @since 1.3
  3836. */
  3837. public void addHierarchyBoundsListener(HierarchyBoundsListener l) {
  3838. if (l == null) {
  3839. return;
  3840. }
  3841. boolean notifyAncestors;
  3842. synchronized (this) {
  3843. notifyAncestors =
  3844. (hierarchyBoundsListener == null &&
  3845. (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) == 0);
  3846. hierarchyBoundsListener =
  3847. AWTEventMulticaster.add(hierarchyBoundsListener, l);
  3848. notifyAncestors = (notifyAncestors &&
  3849. hierarchyBoundsListener != null);
  3850. newEventsOnly = true;
  3851. }
  3852. if (notifyAncestors) {
  3853. synchronized (getTreeLock()) {
  3854. adjustListeningChildrenOnParent(
  3855. AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, 1);
  3856. }
  3857. }
  3858. }
  3859. /**
  3860. * Removes the specified hierarchy bounds listener so that it no longer
  3861. * receives hierarchy bounds events from this component. This method
  3862. * performs no function, nor does it throw an exception, if the listener
  3863. * specified by the argument was not previously added to this component.
  3864. * If listener <code>l</code> is <code>null</code>,
  3865. * no exception is thrown and no action is performed.
  3866. *
  3867. * @param l the hierarchy bounds listener
  3868. * @see java.awt.event.HierarchyEvent
  3869. * @see java.awt.event.HierarchyBoundsListener
  3870. * @see #addHierarchyBoundsListener
  3871. * @see #getHierarchyBoundsListeners
  3872. * @since 1.3
  3873. */
  3874. public void removeHierarchyBoundsListener(HierarchyBoundsListener l) {
  3875. if (l == null) {
  3876. return;
  3877. }
  3878. boolean notifyAncestors;
  3879. synchronized (this) {
  3880. notifyAncestors =
  3881. (hierarchyBoundsListener != null &&
  3882. (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) == 0);
  3883. hierarchyBoundsListener =
  3884. AWTEventMulticaster.remove(hierarchyBoundsListener, l);
  3885. notifyAncestors = (notifyAncestors &&
  3886. hierarchyBoundsListener == null);
  3887. }
  3888. if (notifyAncestors) {
  3889. synchronized (getTreeLock()) {
  3890. adjustListeningChildrenOnParent(
  3891. AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, -1);
  3892. }
  3893. }
  3894. }
  3895. // Should only be called while holding the tree lock
  3896. int numListening(long mask) {
  3897. if (dbg.on) {
  3898. // One mask or the other, but not neither or both.
  3899. dbg.assertion(mask == AWTEvent.HIERARCHY_EVENT_MASK ||
  3900. mask == AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK);
  3901. }
  3902. if ((mask == AWTEvent.HIERARCHY_EVENT_MASK &&
  3903. (hierarchyListener != null ||
  3904. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0)) ||
  3905. (mask == AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK &&
  3906. (hierarchyBoundsListener != null ||
  3907. (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0))) {
  3908. return 1;
  3909. } else {
  3910. return 0;
  3911. }
  3912. }
  3913. // Should only be called while holding tree lock
  3914. int countHierarchyMembers() {
  3915. return 1;
  3916. }
  3917. // Should only be called while holding the tree lock
  3918. int createHierarchyEvents(int id, Component changed,
  3919. Container changedParent, long changeFlags,
  3920. boolean enabledOnToolkit) {
  3921. switch (id) {
  3922. case HierarchyEvent.HIERARCHY_CHANGED:
  3923. if (hierarchyListener != null ||
  3924. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0 ||
  3925. enabledOnToolkit) {
  3926. HierarchyEvent e = new HierarchyEvent(this, id, changed,
  3927. changedParent,
  3928. changeFlags);
  3929. dispatchEvent(e);
  3930. return 1;
  3931. }
  3932. break;
  3933. case HierarchyEvent.ANCESTOR_MOVED:
  3934. case HierarchyEvent.ANCESTOR_RESIZED:
  3935. if (dbg.on) {
  3936. dbg.assertion(changeFlags == 0);
  3937. }
  3938. if (hierarchyBoundsListener != null ||
  3939. (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0 ||
  3940. enabledOnToolkit) {
  3941. HierarchyEvent e = new HierarchyEvent(this, id, changed,
  3942. changedParent);
  3943. dispatchEvent(e);
  3944. return 1;
  3945. }
  3946. break;
  3947. default:
  3948. if (dbg.on) {
  3949. dbg.assertion(false);
  3950. }
  3951. break;
  3952. }
  3953. return 0;
  3954. }
  3955. /**
  3956. * Returns an array of all the hierarchy bounds listeners
  3957. * registered on this component.
  3958. *
  3959. * @return all of this component's <code>HierarchyBoundsListener</code>s
  3960. * or an empty array if no hierarchy bounds
  3961. * listeners are currently registered
  3962. *
  3963. * @see #addHierarchyBoundsListener
  3964. * @see #removeHierarchyBoundsListener
  3965. * @since 1.4
  3966. */
  3967. public synchronized HierarchyBoundsListener[] getHierarchyBoundsListeners() {
  3968. return (HierarchyBoundsListener[])
  3969. (getListeners(HierarchyBoundsListener.class));
  3970. }
  3971. // Since a Component has no children, this function does nothing
  3972. void createChildHierarchyEvents(int id, long changeFlags,
  3973. boolean enabledOnToolkit) {
  3974. }
  3975. /*
  3976. * Should only be called while holding the tree lock.
  3977. * It's added only for overriding in java.awt.Window
  3978. * because parent in Window is owner.
  3979. */
  3980. void adjustListeningChildrenOnParent(long mask, int num) {
  3981. if (parent != null) {
  3982. parent.adjustListeningChildren(mask, num);
  3983. }
  3984. }
  3985. /**
  3986. * Adds the specified key listener to receive key events from
  3987. * this component.
  3988. * If l is null, no exception is thrown and no action is performed.
  3989. *
  3990. * @param l the key listener.
  3991. * @see java.awt.event.KeyEvent
  3992. * @see java.awt.event.KeyListener
  3993. * @see #removeKeyListener
  3994. * @see #getKeyListeners
  3995. * @since JDK1.1
  3996. */
  3997. public synchronized void addKeyListener(KeyListener l) {
  3998. if (l == null) {
  3999. return;
  4000. }
  4001. keyListener = AWTEventMulticaster.add(keyListener, l);
  4002. newEventsOnly = true;
  4003. // if this is a lightweight component, enable key events
  4004. // in the native container.
  4005. if (peer instanceof LightweightPeer) {
  4006. parent.proxyEnableEvents(AWTEvent.KEY_EVENT_MASK);
  4007. }
  4008. }
  4009. /**
  4010. * Removes the specified key listener so that it no longer
  4011. * receives key events from this component. This method performs
  4012. * no function, nor does it throw an exception, if the listener
  4013. * specified by the argument was not previously added to this component.
  4014. * If listener <code>l</code> is <code>null</code>,
  4015. * no exception is thrown and no action is performed.
  4016. *
  4017. * @param l the key listener
  4018. * @see java.awt.event.KeyEvent
  4019. * @see java.awt.event.KeyListener
  4020. * @see #addKeyListener
  4021. * @see #getKeyListeners
  4022. * @since JDK1.1
  4023. */
  4024. public synchronized void removeKeyListener(KeyListener l) {
  4025. if (l == null) {
  4026. return;
  4027. }
  4028. keyListener = AWTEventMulticaster.remove(keyListener, l);
  4029. }
  4030. /**
  4031. * Returns an array of all the key listeners
  4032. * registered on this component.
  4033. *
  4034. * @return all of this component's <code>KeyListener</code>s
  4035. * or an empty array if no key
  4036. * listeners are currently registered
  4037. *
  4038. * @see #addKeyListener
  4039. * @see #removeKeyListener
  4040. * @since 1.4
  4041. */
  4042. public synchronized KeyListener[] getKeyListeners() {
  4043. return (KeyListener[]) (getListeners(KeyListener.class));
  4044. }
  4045. /**
  4046. * Adds the specified mouse listener to receive mouse events from
  4047. * this component.
  4048. * If listener <code>l</code> is <code>null</code>,
  4049. * no exception is thrown and no action is performed.
  4050. *
  4051. * @param l the mouse listener
  4052. * @see java.awt.event.MouseEvent
  4053. * @see java.awt.event.MouseListener
  4054. * @see #removeMouseListener
  4055. * @see #getMouseListeners
  4056. * @since JDK1.1
  4057. */
  4058. public synchronized void addMouseListener(MouseListener l) {
  4059. if (l == null) {
  4060. return;
  4061. }
  4062. mouseListener = AWTEventMulticaster.add(mouseListener,l);
  4063. newEventsOnly = true;
  4064. // if this is a lightweight component, enable mouse events
  4065. // in the native container.
  4066. if (peer instanceof LightweightPeer) {
  4067. parent.proxyEnableEvents(AWTEvent.MOUSE_EVENT_MASK);
  4068. }
  4069. }
  4070. /**
  4071. * Removes the specified mouse listener so that it no longer
  4072. * receives mouse events from this component. This method performs
  4073. * no function, nor does it throw an exception, if the listener
  4074. * specified by the argument was not previously added to this component.
  4075. * If listener <code>l</code> is <code>null</code>,
  4076. * no exception is thrown and no action is performed.
  4077. *
  4078. * @param l the mouse listener
  4079. * @see java.awt.event.MouseEvent
  4080. * @see java.awt.event.MouseListener
  4081. * @see #addMouseListener
  4082. * @see #getMouseListeners
  4083. * @since JDK1.1
  4084. */
  4085. public synchronized void removeMouseListener(MouseListener l) {
  4086. if (l == null) {
  4087. return;
  4088. }
  4089. mouseListener = AWTEventMulticaster.remove(mouseListener, l);
  4090. }
  4091. /**
  4092. * Returns an array of all the mouse listeners
  4093. * registered on this component.
  4094. *
  4095. * @return all of this component's <code>MouseListener</code>s
  4096. * or an empty array if no mouse
  4097. * listeners are currently registered
  4098. *
  4099. * @see #addMouseListener
  4100. * @see #removeMouseListener
  4101. * @since 1.4
  4102. */
  4103. public synchronized MouseListener[] getMouseListeners() {
  4104. return (MouseListener[]) (getListeners(MouseListener.class));
  4105. }
  4106. /**
  4107. * Adds the specified mouse motion listener to receive mouse motion
  4108. * events from this component.
  4109. * If listener <code>l</code> is <code>null</code>,
  4110. * no exception is thrown and no action is performed.
  4111. *
  4112. * @param l the mouse motion listener
  4113. * @see java.awt.event.MouseEvent
  4114. * @see java.awt.event.MouseMotionListener
  4115. * @see #removeMouseMotionListener
  4116. * @see #getMouseMotionListeners
  4117. * @since JDK1.1
  4118. */
  4119. public synchronized void addMouseMotionListener(MouseMotionListener l) {
  4120. if (l == null) {
  4121. return;
  4122. }
  4123. mouseMotionListener = AWTEventMulticaster.add(mouseMotionListener,l);
  4124. newEventsOnly = true;
  4125. // if this is a lightweight component, enable mouse events
  4126. // in the native container.
  4127. if (peer instanceof LightweightPeer) {
  4128. parent.proxyEnableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
  4129. }
  4130. }
  4131. /**
  4132. * Removes the specified mouse motion listener so that it no longer
  4133. * receives mouse motion events from this component. This method performs
  4134. * no function, nor does it throw an exception, if the listener
  4135. * specified by the argument was not previously added to this component.
  4136. * If listener <code>l</code> is <code>null</code>,
  4137. * no exception is thrown and no action is performed.
  4138. *
  4139. * @param l the mouse motion listener
  4140. * @see java.awt.event.MouseEvent
  4141. * @see java.awt.event.MouseMotionListener
  4142. * @see #addMouseMotionListener
  4143. * @see #getMouseMotionListeners
  4144. * @since JDK1.1
  4145. */
  4146. public synchronized void removeMouseMotionListener(MouseMotionListener l) {
  4147. if (l == null) {
  4148. return;
  4149. }
  4150. mouseMotionListener = AWTEventMulticaster.remove(mouseMotionListener, l);
  4151. }
  4152. /**
  4153. * Returns an array of all the mouse motion listeners
  4154. * registered on this component.
  4155. *
  4156. * @return all of this component's <code>MouseMotionListener</code>s
  4157. * or an empty array if no mouse motion
  4158. * listeners are currently registered
  4159. *
  4160. * @see #addMouseMotionListener
  4161. * @see #removeMouseMotionListener
  4162. * @since 1.4
  4163. */
  4164. public synchronized MouseMotionListener[] getMouseMotionListeners() {
  4165. return (MouseMotionListener[]) (getListeners(MouseMotionListener.class));
  4166. }
  4167. /**
  4168. * Adds the specified mouse wheel listener to receive mouse wheel events
  4169. * from this component. Containers also receive mouse wheel events from
  4170. * sub-components.
  4171. * If l is null, no exception is thrown and no action is performed.
  4172. *
  4173. * @param l the mouse wheel listener.
  4174. * @see java.awt.event.MouseWheelEvent
  4175. * @see java.awt.event.MouseWheelListener
  4176. * @see #removeMouseWheelListener
  4177. * @see #getMouseWheelListeners
  4178. * @since 1.4
  4179. */
  4180. public synchronized void addMouseWheelListener(MouseWheelListener l) {
  4181. if (l == null) {
  4182. return;
  4183. }
  4184. mouseWheelListener = AWTEventMulticaster.add(mouseWheelListener,l);
  4185. newEventsOnly = true;
  4186. dbg.println("Component.addMouseWheelListener(): newEventsOnly = " + newEventsOnly);
  4187. // if this is a lightweight component, enable mouse events
  4188. // in the native container.
  4189. if (peer instanceof LightweightPeer) {
  4190. parent.proxyEnableEvents(AWTEvent.MOUSE_WHEEL_EVENT_MASK);
  4191. }
  4192. }
  4193. /**
  4194. * Removes the specified mouse wheel listener so that it no longer
  4195. * receives mouse wheel events from this component. This method performs
  4196. * no function, nor does it throw an exception, if the listener
  4197. * specified by the argument was not previously added to this component.
  4198. * If l is null, no exception is thrown and no action is performed.
  4199. *
  4200. * @param l the mouse wheel listener.
  4201. * @see java.awt.event.MouseWheelEvent
  4202. * @see java.awt.event.MouseWheelListener
  4203. * @see #addMouseWheelListener
  4204. * @see #getMouseWheelListeners
  4205. * @since 1.4
  4206. */
  4207. public synchronized void removeMouseWheelListener(MouseWheelListener l) {
  4208. if (l == null) {
  4209. return;
  4210. }
  4211. mouseWheelListener = AWTEventMulticaster.remove(mouseWheelListener, l);
  4212. }
  4213. /**
  4214. * Returns an array of all the mouse wheel listeners
  4215. * registered on this component.
  4216. *
  4217. * @return all of this component's <code>MouseWheelListener</code>s
  4218. * or an empty array if no mouse wheel
  4219. * listeners are currently registered
  4220. *
  4221. * @see #addMouseWheelListener
  4222. * @see #removeMouseWheelListener
  4223. * @since 1.4
  4224. */
  4225. public synchronized MouseWheelListener[] getMouseWheelListeners() {
  4226. return (MouseWheelListener[]) (getListeners(MouseWheelListener.class));
  4227. }
  4228. /**
  4229. * Adds the specified input method listener to receive
  4230. * input method events from this component. A component will
  4231. * only receive input method events from input methods
  4232. * if it also overrides <code>getInputMethodRequests</code> to return an
  4233. * <code>InputMethodRequests</code> instance.
  4234. * If listener <code>l</code> is <code>null</code>,
  4235. * no exception is thrown and no action is performed.
  4236. *
  4237. * @param l the input method listener
  4238. * @see java.awt.event.InputMethodEvent
  4239. * @see java.awt.event.InputMethodListener
  4240. * @see #removeInputMethodListener
  4241. * @see #getInputMethodListeners
  4242. * @see #getInputMethodRequests
  4243. * @since 1.2
  4244. */
  4245. public synchronized void addInputMethodListener(InputMethodListener l) {
  4246. if (l == null) {
  4247. return;
  4248. }
  4249. inputMethodListener = AWTEventMulticaster.add(inputMethodListener, l);
  4250. newEventsOnly = true;
  4251. }
  4252. /**
  4253. * Removes the specified input method listener so that it no longer
  4254. * receives input method events from this component. This method performs
  4255. * no function, nor does it throw an exception, if the listener
  4256. * specified by the argument was not previously added to this component.
  4257. * If listener <code>l</code> is <code>null</code>,
  4258. * no exception is thrown and no action is performed.
  4259. *
  4260. * @param l the input method listener
  4261. * @see java.awt.event.InputMethodEvent
  4262. * @see java.awt.event.InputMethodListener
  4263. * @see #addInputMethodListener
  4264. * @see #getInputMethodListeners
  4265. * @since 1.2
  4266. */
  4267. public synchronized void removeInputMethodListener(InputMethodListener l) {
  4268. if (l == null) {
  4269. return;
  4270. }
  4271. inputMethodListener = AWTEventMulticaster.remove(inputMethodListener, l);
  4272. }
  4273. /**
  4274. * Returns an array of all the input method listeners
  4275. * registered on this component.
  4276. *
  4277. * @return all of this component's <code>InputMethodListener</code>s
  4278. * or an empty array if no input method
  4279. * listeners are currently registered
  4280. *
  4281. * @see #addInputMethodListener
  4282. * @see #removeInputMethodListener
  4283. * @since 1.4
  4284. */
  4285. public synchronized InputMethodListener[] getInputMethodListeners() {
  4286. return (InputMethodListener[]) (getListeners(InputMethodListener.class));
  4287. }
  4288. /**
  4289. * Returns an array of all the objects currently registered
  4290. * as <code><em>Foo</em>Listener</code>s
  4291. * upon this <code>Component</code>.
  4292. * <code><em>Foo</em>Listener</code>s are registered using the
  4293. * <code>add<em>Foo</em>Listener</code> method.
  4294. *
  4295. * <p>
  4296. * You can specify the <code>listenerType</code> argument
  4297. * with a class literal, such as
  4298. * <code><em>Foo</em>Listener.class</code>.
  4299. * For example, you can query a
  4300. * <code>Component</code> <code>c</code>
  4301. * for its mouse listeners with the following code:
  4302. *
  4303. * <pre>MouseListener[] mls = (MouseListener[])(c.getListeners(MouseListener.class));</pre>
  4304. *
  4305. * If no such listeners exist, this method returns an empty array.
  4306. *
  4307. * @param listenerType the type of listeners requested; this parameter
  4308. * should specify an interface that descends from
  4309. * <code>java.util.EventListener</code>
  4310. * @return an array of all objects registered as
  4311. * <code><em>Foo</em>Listener</code>s on this component,
  4312. * or an empty array if no such listeners have been added
  4313. * @exception ClassCastException if <code>listenerType</code>
  4314. * doesn't specify a class or interface that implements
  4315. * <code>java.util.EventListener</code>
  4316. *
  4317. * @see #getComponentListeners
  4318. * @see #getFocusListeners
  4319. * @see #getHierarchyListeners
  4320. * @see #getHierarchyBoundsListeners
  4321. * @see #getKeyListeners
  4322. * @see #getMouseListeners
  4323. * @see #getMouseMotionListeners
  4324. * @see #getMouseWheelListeners
  4325. * @see #getInputMethodListeners
  4326. * @see #getPropertyChangeListeners
  4327. *
  4328. * @since 1.3
  4329. */
  4330. public EventListener[] getListeners(Class listenerType) {
  4331. EventListener l = null;
  4332. if (listenerType == ComponentListener.class) {
  4333. l = componentListener;
  4334. } else if (listenerType == FocusListener.class) {
  4335. l = focusListener;
  4336. } else if (listenerType == HierarchyListener.class) {
  4337. l = hierarchyListener;
  4338. } else if (listenerType == HierarchyBoundsListener.class) {
  4339. l = hierarchyBoundsListener;
  4340. } else if (listenerType == KeyListener.class) {
  4341. l = keyListener;
  4342. } else if (listenerType == MouseListener.class) {
  4343. l = mouseListener;
  4344. } else if (listenerType == MouseMotionListener.class) {
  4345. l = mouseMotionListener;
  4346. } else if (listenerType == MouseWheelListener.class) {
  4347. l = mouseWheelListener;
  4348. } else if (listenerType == InputMethodListener.class) {
  4349. l = inputMethodListener;
  4350. } else if (listenerType == PropertyChangeListener.class) {
  4351. return getPropertyChangeListeners();
  4352. }
  4353. return AWTEventMulticaster.getListeners(l, listenerType);
  4354. }
  4355. /**
  4356. * Gets the input method request handler which supports
  4357. * requests from input methods for this component. A component
  4358. * that supports on-the-spot text input must override this
  4359. * method to return an <code>InputMethodRequests</code> instance.
  4360. * At the same time, it also has to handle input method events.
  4361. *
  4362. * @return the input method request handler for this component,
  4363. * <code>null</code> by default
  4364. * @see #addInputMethodListener
  4365. * @since 1.2
  4366. */
  4367. public InputMethodRequests getInputMethodRequests() {
  4368. return null;
  4369. }
  4370. /**
  4371. * Gets the input context used by this component for handling
  4372. * the communication with input methods when text is entered
  4373. * in this component. By default, the input context used for
  4374. * the parent component is returned. Components may
  4375. * override this to return a private input context.
  4376. *
  4377. * @return the input context used by this component;
  4378. * <code>null</code> if no context can be determined
  4379. * @since 1.2
  4380. */
  4381. public InputContext getInputContext() {
  4382. Container parent = this.parent;
  4383. if (parent == null) {
  4384. return null;
  4385. } else {
  4386. return parent.getInputContext();
  4387. }
  4388. }
  4389. /**
  4390. * Enables the events defined by the specified event mask parameter
  4391. * to be delivered to this component.
  4392. * <p>
  4393. * Event types are automatically enabled when a listener for
  4394. * that event type is added to the component.
  4395. * <p>
  4396. * This method only needs to be invoked by subclasses of
  4397. * <code>Component</code> which desire to have the specified event
  4398. * types delivered to <code>processEvent</code> regardless of whether
  4399. * or not a listener is registered.
  4400. * @param eventsToEnable the event mask defining the event types
  4401. * @see #processEvent
  4402. * @see #disableEvents
  4403. * @see AWTEvent
  4404. * @since JDK1.1
  4405. */
  4406. protected final void enableEvents(long eventsToEnable) {
  4407. long notifyAncestors = 0;
  4408. synchronized (this) {
  4409. if ((eventsToEnable & AWTEvent.HIERARCHY_EVENT_MASK) != 0 &&
  4410. hierarchyListener == null &&
  4411. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) == 0) {
  4412. notifyAncestors |= AWTEvent.HIERARCHY_EVENT_MASK;
  4413. }
  4414. if ((eventsToEnable & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0 &&
  4415. hierarchyBoundsListener == null &&
  4416. (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) == 0) {
  4417. notifyAncestors |= AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK;
  4418. }
  4419. eventMask |= eventsToEnable;
  4420. newEventsOnly = true;
  4421. }
  4422. // if this is a lightweight component, enable mouse events
  4423. // in the native container.
  4424. if (peer instanceof LightweightPeer) {
  4425. parent.proxyEnableEvents(eventMask);
  4426. }
  4427. if (notifyAncestors != 0) {
  4428. synchronized (getTreeLock()) {
  4429. adjustListeningChildrenOnParent(notifyAncestors, 1);
  4430. }
  4431. }
  4432. }
  4433. /**
  4434. * Disables the events defined by the specified event mask parameter
  4435. * from being delivered to this component.
  4436. * @param eventsToDisable the event mask defining the event types
  4437. * @see #enableEvents
  4438. * @since JDK1.1
  4439. */
  4440. protected final void disableEvents(long eventsToDisable) {
  4441. long notifyAncestors = 0;
  4442. synchronized (this) {
  4443. if ((eventsToDisable & AWTEvent.HIERARCHY_EVENT_MASK) != 0 &&
  4444. hierarchyListener == null &&
  4445. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0) {
  4446. notifyAncestors |= AWTEvent.HIERARCHY_EVENT_MASK;
  4447. }
  4448. if ((eventsToDisable & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK)!=0 &&
  4449. hierarchyBoundsListener == null &&
  4450. (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0) {
  4451. notifyAncestors |= AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK;
  4452. }
  4453. eventMask &= ~eventsToDisable;
  4454. }
  4455. if (notifyAncestors != 0) {
  4456. synchronized (getTreeLock()) {
  4457. adjustListeningChildrenOnParent(notifyAncestors, -1);
  4458. }
  4459. }
  4460. }
  4461. /**
  4462. * Potentially coalesce an event being posted with an existing
  4463. * event. This method is called by <code>EventQueue.postEvent</code>
  4464. * if an event with the same ID as the event to be posted is found in
  4465. * the queue (both events must have this component as their source).
  4466. * This method either returns a coalesced event which replaces
  4467. * the existing event (and the new event is then discarded), or
  4468. * <code>null</code> to indicate that no combining should be done
  4469. * (add the second event to the end of the queue). Either event
  4470. * parameter may be modified and returned, as the other one is discarded
  4471. * unless <code>null</code> is returned.
  4472. * <p>
  4473. * This implementation of <code>coalesceEvents</code> coalesces
  4474. * two event types: mouse move (and drag) events,
  4475. * and paint (and update) events.
  4476. * For mouse move events the last event is always returned, causing
  4477. * intermediate moves to be discarded. For paint events, the new
  4478. * event is coalesced into a complex <code>RepaintArea</code> in the peer.
  4479. * The new <code>AWTEvent</code> is always returned.
  4480. *
  4481. * @param existingEvent the event already on the <code>EventQueue</code>
  4482. * @param newEvent the event being posted to the
  4483. * <code>EventQueue</code>
  4484. * @return a coalesced event, or <code>null</code> indicating that no
  4485. * coalescing was done
  4486. */
  4487. protected AWTEvent coalesceEvents(AWTEvent existingEvent,
  4488. AWTEvent newEvent) {
  4489. int id = existingEvent.getID();
  4490. if (dbg.on) {
  4491. dbg.assertion(id == newEvent.getID() &&
  4492. existingEvent.getSource().equals(newEvent.getSource()));
  4493. }
  4494. switch (id) {
  4495. case Event.MOUSE_MOVE:
  4496. case Event.MOUSE_DRAG: {
  4497. MouseEvent e = (MouseEvent)existingEvent;
  4498. if (e.getModifiers() == ((MouseEvent)newEvent).getModifiers()) {
  4499. // Just return the newEvent, causing the old to be
  4500. // discarded.
  4501. return newEvent;
  4502. }
  4503. break;
  4504. }
  4505. case PaintEvent.PAINT:
  4506. case PaintEvent.UPDATE: {
  4507. if(peer != null && !(peer instanceof LightweightPeer)) {
  4508. // EventQueue.postEvent should use peer.coalescePaintEvent
  4509. return newEvent;
  4510. }
  4511. // This approach to coalescing paint events seems to be
  4512. // better than any heuristic for unioning rectangles.
  4513. PaintEvent existingPaintEvent = (PaintEvent) existingEvent;
  4514. PaintEvent newPaintEvent = (PaintEvent) newEvent;
  4515. Rectangle existingRect = existingPaintEvent.getUpdateRect();
  4516. Rectangle newRect = newPaintEvent.getUpdateRect();
  4517. if (dbg.on) {
  4518. dbg.println("Component::coalesceEvents : newEvent : nullPeer : x = " +
  4519. newRect.x + " y = " + newRect.y + " width = " + newRect.width +
  4520. " height = " + newRect.height);
  4521. }
  4522. if (existingRect.contains(newRect)) {
  4523. return existingEvent;
  4524. }
  4525. if (newRect.contains(existingRect)) {
  4526. return newEvent;
  4527. }
  4528. break;
  4529. }
  4530. }
  4531. return null;
  4532. }
  4533. /**
  4534. * Processes events occurring on this component. By default this
  4535. * method calls the appropriate
  4536. * <code>process<event type>Event</code>
  4537. * method for the given class of event.
  4538. * <p>Note that if the event parameter is <code>null</code>
  4539. * the behavior is unspecified and may result in an
  4540. * exception.
  4541. *
  4542. * @param e the event
  4543. * @see #processComponentEvent
  4544. * @see #processFocusEvent
  4545. * @see #processKeyEvent
  4546. * @see #processMouseEvent
  4547. * @see #processMouseMotionEvent
  4548. * @see #processInputMethodEvent
  4549. * @see #processHierarchyEvent
  4550. * @see #processMouseWheelEvent
  4551. * @since JDK1.1
  4552. */
  4553. protected void processEvent(AWTEvent e) {
  4554. if (e instanceof FocusEvent) {
  4555. processFocusEvent((FocusEvent)e);
  4556. } else if (e instanceof MouseEvent) {
  4557. switch(e.getID()) {
  4558. case MouseEvent.MOUSE_PRESSED:
  4559. case MouseEvent.MOUSE_RELEASED:
  4560. case MouseEvent.MOUSE_CLICKED:
  4561. case MouseEvent.MOUSE_ENTERED:
  4562. case MouseEvent.MOUSE_EXITED:
  4563. processMouseEvent((MouseEvent)e);
  4564. break;
  4565. case MouseEvent.MOUSE_MOVED:
  4566. case MouseEvent.MOUSE_DRAGGED:
  4567. processMouseMotionEvent((MouseEvent)e);
  4568. break;
  4569. case MouseEvent.MOUSE_WHEEL:
  4570. processMouseWheelEvent((MouseWheelEvent)e);
  4571. break;
  4572. }
  4573. } else if (e instanceof KeyEvent) {
  4574. processKeyEvent((KeyEvent)e);
  4575. } else if (e instanceof ComponentEvent) {
  4576. processComponentEvent((ComponentEvent)e);
  4577. } else if (e instanceof InputMethodEvent) {
  4578. processInputMethodEvent((InputMethodEvent)e);
  4579. } else if (e instanceof HierarchyEvent) {
  4580. switch (e.getID()) {
  4581. case HierarchyEvent.HIERARCHY_CHANGED:
  4582. processHierarchyEvent((HierarchyEvent)e);
  4583. break;
  4584. case HierarchyEvent.ANCESTOR_MOVED:
  4585. case HierarchyEvent.ANCESTOR_RESIZED:
  4586. processHierarchyBoundsEvent((HierarchyEvent)e);
  4587. break;
  4588. }
  4589. }
  4590. }
  4591. /**
  4592. * Processes component events occurring on this component by
  4593. * dispatching them to any registered
  4594. * <code>ComponentListener</code> objects.
  4595. * <p>
  4596. * This method is not called unless component events are
  4597. * enabled for this component. Component events are enabled
  4598. * when one of the following occurs:
  4599. * <p><ul>
  4600. * <li>A <code>ComponentListener</code> object is registered
  4601. * via <code>addComponentListener</code>.
  4602. * <li>Component events are enabled via <code>enableEvents</code>.
  4603. * </ul>
  4604. * <p>Note that if the event parameter is <code>null</code>
  4605. * the behavior is unspecified and may result in an
  4606. * exception.
  4607. *
  4608. * @param e the component event
  4609. * @see java.awt.event.ComponentEvent
  4610. * @see java.awt.event.ComponentListener
  4611. * @see #addComponentListener
  4612. * @see #enableEvents
  4613. * @since JDK1.1
  4614. */
  4615. protected void processComponentEvent(ComponentEvent e) {
  4616. ComponentListener listener = componentListener;
  4617. if (listener != null) {
  4618. int id = e.getID();
  4619. switch(id) {
  4620. case ComponentEvent.COMPONENT_RESIZED:
  4621. listener.componentResized(e);
  4622. break;
  4623. case ComponentEvent.COMPONENT_MOVED:
  4624. listener.componentMoved(e);
  4625. break;
  4626. case ComponentEvent.COMPONENT_SHOWN:
  4627. listener.componentShown(e);
  4628. break;
  4629. case ComponentEvent.COMPONENT_HIDDEN:
  4630. listener.componentHidden(e);
  4631. break;
  4632. }
  4633. }
  4634. }
  4635. /**
  4636. * Processes focus events occurring on this component by
  4637. * dispatching them to any registered
  4638. * <code>FocusListener</code> objects.
  4639. * <p>
  4640. * This method is not called unless focus events are
  4641. * enabled for this component. Focus events are enabled
  4642. * when one of the following occurs:
  4643. * <p><ul>
  4644. * <li>A <code>FocusListener</code> object is registered
  4645. * via <code>addFocusListener</code>.
  4646. * <li>Focus events are enabled via <code>enableEvents</code>.
  4647. * </ul>
  4648. * <p>Note that if the event parameter is <code>null</code>
  4649. * the behavior is unspecified and may result in an
  4650. * exception.
  4651. *
  4652. * @param e the focus event
  4653. * @see java.awt.event.FocusEvent
  4654. * @see java.awt.event.FocusListener
  4655. * @see #addFocusListener
  4656. * @see #enableEvents
  4657. * @since JDK1.1
  4658. */
  4659. protected void processFocusEvent(FocusEvent e) {
  4660. FocusListener listener = focusListener;
  4661. if (listener != null) {
  4662. int id = e.getID();
  4663. switch(id) {
  4664. case FocusEvent.FOCUS_GAINED:
  4665. listener.focusGained(e);
  4666. break;
  4667. case FocusEvent.FOCUS_LOST:
  4668. listener.focusLost(e);
  4669. break;
  4670. }
  4671. }
  4672. }
  4673. /**
  4674. * Processes key events occurring on this component by
  4675. * dispatching them to any registered
  4676. * <code>KeyListener</code> objects.
  4677. * <p>
  4678. * This method is not called unless key events are
  4679. * enabled for this component. Key events are enabled
  4680. * when one of the following occurs:
  4681. * <p><ul>
  4682. * <li>A <code>KeyListener</code> object is registered
  4683. * via <code>addKeyListener</code>.
  4684. * <li>Key events are enabled via <code>enableEvents</code>.
  4685. * </ul>
  4686. *
  4687. * <p>Note that this method is not called by the event
  4688. * dispatch thread if the component is not the focus
  4689. * owner of if the component is not showing. This method
  4690. * is called when key events are
  4691. * registered via the <code>addKeyListener</code> or
  4692. * <code>enableEvents</code> methods but, as of release 1.4,
  4693. * the implementation of the AWT event dispatching thread
  4694. * redirects <code>KeyEvent</code> to the focus owner.
  4695. * Please see the <a href="doc-files/FocusSpec.html">
  4696. * Focus Specification</a> for further information.
  4697. *
  4698. * <p>If the event parameter is <code>null</code>
  4699. * the behavior is unspecified and may result in an
  4700. * exception.
  4701. *
  4702. * @param e the key event
  4703. * @see java.awt.event.KeyEvent
  4704. * @see java.awt.event.KeyListener
  4705. * @see #addKeyListener
  4706. * @see #enableEvents
  4707. * @see #isShowing
  4708. * @since JDK1.1
  4709. */
  4710. protected void processKeyEvent(KeyEvent e) {
  4711. KeyListener listener = keyListener;
  4712. if (listener != null) {
  4713. int id = e.getID();
  4714. switch(id) {
  4715. case KeyEvent.KEY_TYPED:
  4716. listener.keyTyped(e);
  4717. break;
  4718. case KeyEvent.KEY_PRESSED:
  4719. listener.keyPressed(e);
  4720. break;
  4721. case KeyEvent.KEY_RELEASED:
  4722. listener.keyReleased(e);
  4723. break;
  4724. }
  4725. }
  4726. }
  4727. /**
  4728. * Processes mouse events occurring on this component by
  4729. * dispatching them to any registered
  4730. * <code>MouseListener</code> objects.
  4731. * <p>
  4732. * This method is not called unless mouse events are
  4733. * enabled for this component. Mouse events are enabled
  4734. * when one of the following occurs:
  4735. * <p><ul>
  4736. * <li>A <code>MouseListener</code> object is registered
  4737. * via <code>addMouseListener</code>.
  4738. * <li>Mouse events are enabled via <code>enableEvents</code>.
  4739. * </ul>
  4740. * <p>Note that if the event parameter is <code>null</code>
  4741. * the behavior is unspecified and may result in an
  4742. * exception.
  4743. *
  4744. * @param e the mouse event
  4745. * @see java.awt.event.MouseEvent
  4746. * @see java.awt.event.MouseListener
  4747. * @see #addMouseListener
  4748. * @see #enableEvents
  4749. * @since JDK1.1
  4750. */
  4751. protected void processMouseEvent(MouseEvent e) {
  4752. MouseListener listener = mouseListener;
  4753. if (listener != null) {
  4754. int id = e.getID();
  4755. switch(id) {
  4756. case MouseEvent.MOUSE_PRESSED:
  4757. listener.mousePressed(e);
  4758. break;
  4759. case MouseEvent.MOUSE_RELEASED:
  4760. listener.mouseReleased(e);
  4761. break;
  4762. case MouseEvent.MOUSE_CLICKED:
  4763. listener.mouseClicked(e);
  4764. break;
  4765. case MouseEvent.MOUSE_EXITED:
  4766. listener.mouseExited(e);
  4767. break;
  4768. case MouseEvent.MOUSE_ENTERED:
  4769. listener.mouseEntered(e);
  4770. break;
  4771. }
  4772. }
  4773. }
  4774. /**
  4775. * Processes mouse motion events occurring on this component by
  4776. * dispatching them to any registered
  4777. * <code>MouseMotionListener</code> objects.
  4778. * <p>
  4779. * This method is not called unless mouse motion events are
  4780. * enabled for this component. Mouse motion events are enabled
  4781. * when one of the following occurs:
  4782. * <p><ul>
  4783. * <li>A <code>MouseMotionListener</code> object is registered
  4784. * via <code>addMouseMotionListener</code>.
  4785. * <li>Mouse motion events are enabled via <code>enableEvents</code>.
  4786. * </ul>
  4787. * <p>Note that if the event parameter is <code>null</code>
  4788. * the behavior is unspecified and may result in an
  4789. * exception.
  4790. *
  4791. * @param e the mouse motion event
  4792. * @see java.awt.event.MouseEvent
  4793. * @see java.awt.event.MouseMotionListener
  4794. * @see #addMouseMotionListener
  4795. * @see #enableEvents
  4796. * @since JDK1.1
  4797. */
  4798. protected void processMouseMotionEvent(MouseEvent e) {
  4799. MouseMotionListener listener = mouseMotionListener;
  4800. if (listener != null) {
  4801. int id = e.getID();
  4802. switch(id) {
  4803. case MouseEvent.MOUSE_MOVED:
  4804. listener.mouseMoved(e);
  4805. break;
  4806. case MouseEvent.MOUSE_DRAGGED:
  4807. listener.mouseDragged(e);
  4808. break;
  4809. }
  4810. }
  4811. }
  4812. /**
  4813. * Processes mouse wheel events occurring on this component by
  4814. * dispatching them to any registered
  4815. * <code>MouseWheelListener</code> objects.
  4816. * <p>
  4817. * This method is not called unless mouse wheel events are
  4818. * enabled for this component. Mouse wheel events are enabled
  4819. * when one of the following occurs:
  4820. * <p><ul>
  4821. * <li>A <code>MouseWheelListener</code> object is registered
  4822. * via <code>addMouseWheelListener</code>.
  4823. * <li>Mouse wheel events are enabled via <code>enableEvents</code>.
  4824. * </ul>
  4825. * <p>Note that if the event parameter is <code>null</code>
  4826. * the behavior is unspecified and may result in an
  4827. * exception.
  4828. *
  4829. * @param e the mouse wheel event.
  4830. * @see java.awt.event.MouseWheelEvent
  4831. * @see java.awt.event.MouseWheelListener
  4832. * @see #addMouseWheelListener
  4833. * @see #enableEvents
  4834. * @since 1.4
  4835. */
  4836. protected void processMouseWheelEvent(MouseWheelEvent e) {
  4837. MouseWheelListener listener = mouseWheelListener;
  4838. if (listener != null) {
  4839. int id = e.getID();
  4840. switch(id) {
  4841. case MouseEvent.MOUSE_WHEEL:
  4842. listener.mouseWheelMoved(e);
  4843. break;
  4844. }
  4845. }
  4846. }
  4847. boolean postsOldMouseEvents() {
  4848. return false;
  4849. }
  4850. /**
  4851. * Processes input method events occurring on this component by
  4852. * dispatching them to any registered
  4853. * <code>InputMethodListener</code> objects.
  4854. * <p>
  4855. * This method is not called unless input method events
  4856. * are enabled for this component. Input method events are enabled
  4857. * when one of the following occurs:
  4858. * <p><ul>
  4859. * <li>An <code>InputMethodListener</code> object is registered
  4860. * via <code>addInputMethodListener</code>.
  4861. * <li>Input method events are enabled via <code>enableEvents</code>.
  4862. * </ul>
  4863. * <p>Note that if the event parameter is <code>null</code>
  4864. * the behavior is unspecified and may result in an
  4865. * exception.
  4866. *
  4867. * @param e the input method event
  4868. * @see java.awt.event.InputMethodEvent
  4869. * @see java.awt.event.InputMethodListener
  4870. * @see #addInputMethodListener
  4871. * @see #enableEvents
  4872. * @since 1.2
  4873. */
  4874. protected void processInputMethodEvent(InputMethodEvent e) {
  4875. InputMethodListener listener = inputMethodListener;
  4876. if (listener != null) {
  4877. int id = e.getID();
  4878. switch (id) {
  4879. case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
  4880. listener.inputMethodTextChanged(e);
  4881. break;
  4882. case InputMethodEvent.CARET_POSITION_CHANGED:
  4883. listener.caretPositionChanged(e);
  4884. break;
  4885. }
  4886. }
  4887. }
  4888. /**
  4889. * Processes hierarchy events occurring on this component by
  4890. * dispatching them to any registered
  4891. * <code>HierarchyListener</code> objects.
  4892. * <p>
  4893. * This method is not called unless hierarchy events
  4894. * are enabled for this component. Hierarchy events are enabled
  4895. * when one of the following occurs:
  4896. * <p><ul>
  4897. * <li>An <code>HierarchyListener</code> object is registered
  4898. * via <code>addHierarchyListener</code>.
  4899. * <li>Hierarchy events are enabled via <code>enableEvents</code>.
  4900. * </ul>
  4901. * <p>Note that if the event parameter is <code>null</code>
  4902. * the behavior is unspecified and may result in an
  4903. * exception.
  4904. *
  4905. * @param e the hierarchy event
  4906. * @see java.awt.event.HierarchyEvent
  4907. * @see java.awt.event.HierarchyListener
  4908. * @see #addHierarchyListener
  4909. * @see #enableEvents
  4910. * @since 1.3
  4911. */
  4912. protected void processHierarchyEvent(HierarchyEvent e) {
  4913. HierarchyListener listener = hierarchyListener;
  4914. if (listener != null) {
  4915. int id = e.getID();
  4916. switch (id) {
  4917. case HierarchyEvent.HIERARCHY_CHANGED:
  4918. listener.hierarchyChanged(e);
  4919. break;
  4920. }
  4921. }
  4922. }
  4923. /**
  4924. * Processes hierarchy bounds events occurring on this component by
  4925. * dispatching them to any registered
  4926. * <code>HierarchyBoundsListener</code> objects.
  4927. * <p>
  4928. * This method is not called unless hierarchy bounds events
  4929. * are enabled for this component. Hierarchy bounds events are enabled
  4930. * when one of the following occurs:
  4931. * <p><ul>
  4932. * <li>An <code>HierarchyBoundsListener</code> object is registered
  4933. * via <code>addHierarchyBoundsListener</code>.
  4934. * <li>Hierarchy bounds events are enabled via <code>enableEvents</code>.
  4935. * </ul>
  4936. * <p>Note that if the event parameter is <code>null</code>
  4937. * the behavior is unspecified and may result in an
  4938. * exception.
  4939. *
  4940. * @param e the hierarchy event
  4941. * @see java.awt.event.HierarchyEvent
  4942. * @see java.awt.event.HierarchyBoundsListener
  4943. * @see #addHierarchyBoundsListener
  4944. * @see #enableEvents
  4945. * @since 1.3
  4946. */
  4947. protected void processHierarchyBoundsEvent(HierarchyEvent e) {
  4948. HierarchyBoundsListener listener = hierarchyBoundsListener;
  4949. if (listener != null) {
  4950. int id = e.getID();
  4951. switch (id) {
  4952. case HierarchyEvent.ANCESTOR_MOVED:
  4953. listener.ancestorMoved(e);
  4954. break;
  4955. case HierarchyEvent.ANCESTOR_RESIZED:
  4956. listener.ancestorResized(e);
  4957. break;
  4958. }
  4959. }
  4960. }
  4961. /**
  4962. * @deprecated As of JDK version 1.1
  4963. * replaced by processEvent(AWTEvent).
  4964. */
  4965. public boolean handleEvent(Event evt) {
  4966. switch (evt.id) {
  4967. case Event.MOUSE_ENTER:
  4968. return mouseEnter(evt, evt.x, evt.y);
  4969. case Event.MOUSE_EXIT:
  4970. return mouseExit(evt, evt.x, evt.y);
  4971. case Event.MOUSE_MOVE:
  4972. return mouseMove(evt, evt.x, evt.y);
  4973. case Event.MOUSE_DOWN:
  4974. return mouseDown(evt, evt.x, evt.y);
  4975. case Event.MOUSE_DRAG:
  4976. return mouseDrag(evt, evt.x, evt.y);
  4977. case Event.MOUSE_UP:
  4978. return mouseUp(evt, evt.x, evt.y);
  4979. case Event.KEY_PRESS:
  4980. case Event.KEY_ACTION:
  4981. return keyDown(evt, evt.key);
  4982. case Event.KEY_RELEASE:
  4983. case Event.KEY_ACTION_RELEASE:
  4984. return keyUp(evt, evt.key);
  4985. case Event.ACTION_EVENT:
  4986. return action(evt, evt.arg);
  4987. case Event.GOT_FOCUS:
  4988. return gotFocus(evt, evt.arg);
  4989. case Event.LOST_FOCUS:
  4990. return lostFocus(evt, evt.arg);
  4991. }
  4992. return false;
  4993. }
  4994. /**
  4995. * @deprecated As of JDK version 1.1,
  4996. * replaced by processMouseEvent(MouseEvent).
  4997. */
  4998. public boolean mouseDown(Event evt, int x, int y) {
  4999. return false;
  5000. }
  5001. /**
  5002. * @deprecated As of JDK version 1.1,
  5003. * replaced by processMouseMotionEvent(MouseEvent).
  5004. */
  5005. public boolean mouseDrag(Event evt, int x, int y) {
  5006. return false;
  5007. }
  5008. /**
  5009. * @deprecated As of JDK version 1.1,
  5010. * replaced by processMouseEvent(MouseEvent).
  5011. */
  5012. public boolean mouseUp(Event evt, int x, int y) {
  5013. return false;
  5014. }
  5015. /**
  5016. * @deprecated As of JDK version 1.1,
  5017. * replaced by processMouseMotionEvent(MouseEvent).
  5018. */
  5019. public boolean mouseMove(Event evt, int x, int y) {
  5020. return false;
  5021. }
  5022. /**
  5023. * @deprecated As of JDK version 1.1,
  5024. * replaced by processMouseEvent(MouseEvent).
  5025. */
  5026. public boolean mouseEnter(Event evt, int x, int y) {
  5027. return false;
  5028. }
  5029. /**
  5030. * @deprecated As of JDK version 1.1,
  5031. * replaced by processMouseEvent(MouseEvent).
  5032. */
  5033. public boolean mouseExit(Event evt, int x, int y) {
  5034. return false;
  5035. }
  5036. /**
  5037. * @deprecated As of JDK version 1.1,
  5038. * replaced by processKeyEvent(KeyEvent).
  5039. */
  5040. public boolean keyDown(Event evt, int key) {
  5041. return false;
  5042. }
  5043. /**
  5044. * @deprecated As of JDK version 1.1,
  5045. * replaced by processKeyEvent(KeyEvent).
  5046. */
  5047. public boolean keyUp(Event evt, int key) {
  5048. return false;
  5049. }
  5050. /**
  5051. * @deprecated As of JDK version 1.1,
  5052. * should register this component as ActionListener on component
  5053. * which fires action events.
  5054. */
  5055. public boolean action(Event evt, Object what) {
  5056. return false;
  5057. }
  5058. /**
  5059. * Makes this <code>Component</code> displayable by connecting it to a
  5060. * native screen resource.
  5061. * This method is called internally by the toolkit and should
  5062. * not be called directly by programs.
  5063. * @see #isDisplayable
  5064. * @see #removeNotify
  5065. * @since JDK1.0
  5066. */
  5067. public void addNotify() {
  5068. synchronized (getTreeLock()) {
  5069. ComponentPeer peer = this.peer;
  5070. if (peer == null || peer instanceof LightweightPeer){
  5071. if (peer == null) {
  5072. // Update both the Component's peer variable and the local
  5073. // variable we use for thread safety.
  5074. this.peer = peer = getToolkit().createComponent(this);
  5075. }
  5076. // This is a lightweight component which means it won't be
  5077. // able to get window-related events by itself. If any
  5078. // have been enabled, then the nearest native container must
  5079. // be enabled.
  5080. if (parent != null) {
  5081. long mask = 0;
  5082. if ((mouseListener != null) || ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0)) {
  5083. mask |= AWTEvent.MOUSE_EVENT_MASK;
  5084. }
  5085. if ((mouseMotionListener != null) ||
  5086. ((eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0)) {
  5087. mask |= AWTEvent.MOUSE_MOTION_EVENT_MASK;
  5088. }
  5089. if ((mouseWheelListener != null ) ||
  5090. ((eventMask & AWTEvent.MOUSE_WHEEL_EVENT_MASK) != 0)) {
  5091. mask |= AWTEvent.MOUSE_WHEEL_EVENT_MASK;
  5092. }
  5093. if (focusListener != null || (eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0) {
  5094. mask |= AWTEvent.FOCUS_EVENT_MASK;
  5095. }
  5096. if (keyListener != null || (eventMask & AWTEvent.KEY_EVENT_MASK) != 0) {
  5097. mask |= AWTEvent.KEY_EVENT_MASK;
  5098. }
  5099. if (mask != 0) {
  5100. parent.proxyEnableEvents(mask);
  5101. }
  5102. }
  5103. } else {
  5104. // It's native. If the parent is lightweight it
  5105. // will need some help.
  5106. Container parent = this.parent;
  5107. if (parent != null && parent.peer instanceof LightweightPeer) {
  5108. new NativeInLightFixer();
  5109. }
  5110. }
  5111. invalidate();
  5112. int npopups = (popups != null? popups.size() : 0);
  5113. for (int i = 0 ; i < npopups ; i++) {
  5114. PopupMenu popup = (PopupMenu)popups.elementAt(i);
  5115. popup.addNotify();
  5116. }
  5117. if (dropTarget != null) dropTarget.addNotify(peer);
  5118. peerFont = getFont();
  5119. if (hierarchyListener != null ||
  5120. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0 ||
  5121. Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK)) {
  5122. HierarchyEvent e =
  5123. new HierarchyEvent(this, HierarchyEvent.HIERARCHY_CHANGED,
  5124. this, parent,
  5125. HierarchyEvent.DISPLAYABILITY_CHANGED |
  5126. ((isRecursivelyVisible())
  5127. ? HierarchyEvent.SHOWING_CHANGED
  5128. : 0));
  5129. dispatchEvent(e);
  5130. }
  5131. }
  5132. }
  5133. /**
  5134. * Makes this <code>Component</code> undisplayable by destroying it native
  5135. * screen resource.
  5136. * This method is called by the toolkit internally and should
  5137. * not be called directly by programs.
  5138. * @see #isDisplayable
  5139. * @see #addNotify
  5140. * @since JDK1.0
  5141. */
  5142. public void removeNotify() {
  5143. KeyboardFocusManager.clearMostRecentFocusOwner(this);
  5144. if (KeyboardFocusManager.getCurrentKeyboardFocusManager().
  5145. getPermanentFocusOwner() == this)
  5146. {
  5147. KeyboardFocusManager.getCurrentKeyboardFocusManager().
  5148. setGlobalPermanentFocusOwner(null);
  5149. }
  5150. synchronized (getTreeLock()) {
  5151. if (isFocusOwner() && !nextFocusHelper()) {
  5152. KeyboardFocusManager.getCurrentKeyboardFocusManager().
  5153. clearGlobalFocusOwner();
  5154. }
  5155. int npopups = (popups != null? popups.size() : 0);
  5156. for (int i = 0 ; i < npopups ; i++) {
  5157. PopupMenu popup = (PopupMenu)popups.elementAt(i);
  5158. popup.removeNotify();
  5159. }
  5160. // If there is any input context for this component, notify
  5161. // that this component is being removed. (This has to be done
  5162. // before hiding peer.)
  5163. if (areInputMethodsEnabled()) {
  5164. InputContext inputContext = getInputContext();
  5165. if (inputContext != null) {
  5166. inputContext.removeNotify(this);
  5167. }
  5168. }
  5169. ComponentPeer p = peer;
  5170. if (p != null) {
  5171. if (bufferStrategy instanceof FlipBufferStrategy) {
  5172. ((FlipBufferStrategy)bufferStrategy).destroyBuffers();
  5173. }
  5174. if (dropTarget != null) dropTarget.removeNotify(peer);
  5175. // Hide peer first to stop system events such as cursor moves.
  5176. if (visible) {
  5177. p.hide();
  5178. }
  5179. peer = null; // Stop peer updates.
  5180. peerFont = null;
  5181. Toolkit.getEventQueue().removeSourceEvents(this, false);
  5182. KeyboardFocusManager.getCurrentKeyboardFocusManager().
  5183. discardKeyEvents(this);
  5184. p.dispose();
  5185. }
  5186. if (hierarchyListener != null ||
  5187. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0 ||
  5188. Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK)) {
  5189. HierarchyEvent e =
  5190. new HierarchyEvent(this, HierarchyEvent.HIERARCHY_CHANGED,
  5191. this, parent,
  5192. HierarchyEvent.DISPLAYABILITY_CHANGED |
  5193. ((isRecursivelyVisible())
  5194. ? HierarchyEvent.SHOWING_CHANGED
  5195. : 0));
  5196. dispatchEvent(e);
  5197. }
  5198. }
  5199. }
  5200. /**
  5201. * @deprecated As of JDK version 1.1,
  5202. * replaced by processFocusEvent(FocusEvent).
  5203. */
  5204. public boolean gotFocus(Event evt, Object what) {
  5205. return false;
  5206. }
  5207. /**
  5208. * @deprecated As of JDK version 1.1,
  5209. * replaced by processFocusEvent(FocusEvent).
  5210. */
  5211. public boolean lostFocus(Event evt, Object what) {
  5212. return false;
  5213. }
  5214. /**
  5215. * Returns whether this Component can be focused.
  5216. *
  5217. * @return <code>true</code> if this Component is focusable;
  5218. * <code>false</code> otherwise.
  5219. * @see #setFocusable
  5220. * @since JDK1.1
  5221. */
  5222. /**
  5223. * Returns whether this <code>Component</code> can become the focus
  5224. * owner.
  5225. *
  5226. * @return <code>true</code> if this <code>Component</code> is
  5227. * focusable; <code>false</code> otherwise
  5228. * @see #setFocusable
  5229. * @since JDK1.1
  5230. * @deprecated As of 1.4, replaced by <code>isFocusable()</code>.
  5231. */
  5232. public boolean isFocusTraversable() {
  5233. if (isFocusTraversableOverridden == FOCUS_TRAVERSABLE_UNKNOWN) {
  5234. isFocusTraversableOverridden = FOCUS_TRAVERSABLE_DEFAULT;
  5235. }
  5236. return focusable;
  5237. }
  5238. /**
  5239. * Returns whether this Component can be focused.
  5240. *
  5241. * @return <code>true</code> if this Component is focusable;
  5242. * <code>false</code> otherwise.
  5243. * @see #setFocusable
  5244. * @since 1.4
  5245. */
  5246. public boolean isFocusable() {
  5247. return isFocusTraversable();
  5248. }
  5249. /**
  5250. * Sets the focusable state of this Component to the specified value. This
  5251. * value overrides the Component's default focusability.
  5252. *
  5253. * @param focusable indicates whether this Component is focusable
  5254. * @see #isFocusable
  5255. * @since 1.4
  5256. * @beaninfo
  5257. * bound: true
  5258. */
  5259. public void setFocusable(boolean focusable) {
  5260. boolean oldFocusable;
  5261. synchronized (this) {
  5262. oldFocusable = this.focusable;
  5263. this.focusable = focusable;
  5264. }
  5265. isFocusTraversableOverridden = FOCUS_TRAVERSABLE_SET;
  5266. firePropertyChange("focusable", oldFocusable, focusable);
  5267. if (oldFocusable && !focusable) {
  5268. if (isFocusOwner()) {
  5269. autoTransferFocus(true);
  5270. }
  5271. KeyboardFocusManager.clearMostRecentFocusOwner(this);
  5272. }
  5273. }
  5274. final boolean isFocusTraversableOverridden() {
  5275. return (isFocusTraversableOverridden != FOCUS_TRAVERSABLE_DEFAULT);
  5276. }
  5277. /**
  5278. * Sets the focus traversal keys for a given traversal operation for this
  5279. * Component.
  5280. * <p>
  5281. * The default values for a Component's focus traversal keys are
  5282. * implementation-dependent. Sun recommends that all implementations for a
  5283. * particular native platform use the same default values. The
  5284. * recommendations for Windows and Unix are listed below. These
  5285. * recommendations are used in the Sun AWT implementations.
  5286. *
  5287. * <table border=1 summary="Recommended default values for a Component's focus traversal keys">
  5288. * <tr>
  5289. * <th>Identifier</th>
  5290. * <th>Meaning</th>
  5291. * <th>Default</th>
  5292. * </tr>
  5293. * <tr>
  5294. * <td>KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS</td>
  5295. * <td>Normal forward keyboard traversal</td>
  5296. * <td>TAB on KEY_PRESSED, CTRL-TAB on KEY_PRESSED</td>
  5297. * </tr>
  5298. * <tr>
  5299. * <td>KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS</td>
  5300. * <td>Normal reverse keyboard traversal</td>
  5301. * <td>SHIFT-TAB on KEY_PRESSED, CTRL-SHIFT-TAB on KEY_PRESSED</td>
  5302. * </tr>
  5303. * <tr>
  5304. * <td>KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS</td>
  5305. * <td>Go up one focus traversal cycle</td>
  5306. * <td>none</td>
  5307. * </tr>
  5308. * </table>
  5309. *
  5310. * To disable a traversal key, use an empty Set; Collections.EMPTY_SET is
  5311. * recommended.
  5312. * <p>
  5313. * Using the AWTKeyStroke API, client code can specify on which of two
  5314. * specific KeyEvents, KEY_PRESSED or KEY_RELEASED, the focus traversal
  5315. * operation will occur. Regardless of which KeyEvent is specified,
  5316. * however, all KeyEvents related to the focus traversal key, including the
  5317. * associated KEY_TYPED event, will be consumed, and will not be dispatched
  5318. * to any Component. It is a runtime error to specify a KEY_TYPED event as
  5319. * mapping to a focus traversal operation, or to map the same event to
  5320. * multiple default focus traversal operations.
  5321. * <p>
  5322. * If a value of null is specified for the Set, this Component inherits the
  5323. * Set from its parent. If all ancestors of this Component have null
  5324. * specified for the Set, then the current KeyboardFocusManager's default
  5325. * Set is used.
  5326. *
  5327. * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  5328. * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, or
  5329. * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS
  5330. * @param keystrokes the Set of AWTKeyStroke for the specified operation
  5331. * @see #getFocusTraversalKeys
  5332. * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
  5333. * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
  5334. * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
  5335. * @throws IllegalArgumentException if id is not one of
  5336. * KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  5337. * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, or
  5338. * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or if keystrokes
  5339. * contains null, or if any Object in keystrokes is not an
  5340. * AWTKeyStroke, or if any keystroke represents a KEY_TYPED event,
  5341. * or if any keystroke already maps to another focus traversal
  5342. * operation for this Component
  5343. * @since 1.4
  5344. * @beaninfo
  5345. * bound: true
  5346. */
  5347. public void setFocusTraversalKeys(int id, Set keystrokes) {
  5348. if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH - 1) {
  5349. throw new IllegalArgumentException("invalid focus traversal key identifier");
  5350. }
  5351. setFocusTraversalKeys_NoIDCheck(id, keystrokes);
  5352. }
  5353. /**
  5354. * Returns the Set of focus traversal keys for a given traversal operation
  5355. * for this Component. (See
  5356. * <code>setFocusTraversalKeys</code> for a full description of each key.)
  5357. * <p>
  5358. * If a Set of traversal keys has not been explicitly defined for this
  5359. * Component, then this Component's parent's Set is returned. If no Set
  5360. * has been explicitly defined for any of this Component's ancestors, then
  5361. * the current KeyboardFocusManager's default Set is returned.
  5362. *
  5363. * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  5364. * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, or
  5365. * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS
  5366. * @return the Set of AWTKeyStrokes for the specified operation. The Set
  5367. * will be unmodifiable, and may be empty. null will never be
  5368. * returned.
  5369. * @see #setFocusTraversalKeys
  5370. * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
  5371. * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
  5372. * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
  5373. * @throws IllegalArgumentException if id is not one of
  5374. * KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  5375. * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, or
  5376. * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS
  5377. * @since 1.4
  5378. */
  5379. public Set getFocusTraversalKeys(int id) {
  5380. if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH - 1) {
  5381. throw new IllegalArgumentException("invalid focus traversal key identifier");
  5382. }
  5383. return getFocusTraversalKeys_NoIDCheck(id);
  5384. }
  5385. // We define these methods so that Container does not need to repeat this
  5386. // code. Container cannot call super.<method> because Container allows
  5387. // DOWN_CYCLE_TRAVERSAL_KEY while Component does not. The Component method
  5388. // would erroneously generate an IllegalArgumentException for
  5389. // DOWN_CYCLE_TRAVERSAL_KEY.
  5390. final void setFocusTraversalKeys_NoIDCheck(int id, Set keystrokes) {
  5391. Set oldKeys;
  5392. synchronized (this) {
  5393. if (focusTraversalKeys == null) {
  5394. initializeFocusTraversalKeys();
  5395. }
  5396. if (keystrokes != null) {
  5397. for (Iterator iter = keystrokes.iterator(); iter.hasNext(); ) {
  5398. Object obj = iter.next();
  5399. if (obj == null) {
  5400. throw new IllegalArgumentException("cannot set null focus traversal key");
  5401. }
  5402. // Generates a ClassCastException if the element is not an
  5403. // AWTKeyStroke. This is desirable.
  5404. AWTKeyStroke keystroke = (AWTKeyStroke)obj;
  5405. if (keystroke.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
  5406. throw new IllegalArgumentException("focus traversal keys cannot map to KEY_TYPED events");
  5407. }
  5408. for (int i = 0; i < focusTraversalKeys.length; i++) {
  5409. if (i == id) {
  5410. continue;
  5411. }
  5412. if (getFocusTraversalKeys_NoIDCheck(i).contains(keystroke))
  5413. {
  5414. throw new IllegalArgumentException("focus traversal keys must be unique for a Component");
  5415. }
  5416. }
  5417. }
  5418. }
  5419. oldKeys = focusTraversalKeys[id];
  5420. focusTraversalKeys[id] = (keystrokes != null)
  5421. ? Collections.unmodifiableSet(new HashSet(keystrokes))
  5422. : null;
  5423. }
  5424. firePropertyChange(focusTraversalKeyPropertyNames[id], oldKeys,
  5425. keystrokes);
  5426. }
  5427. final Set getFocusTraversalKeys_NoIDCheck(int id) {
  5428. // Okay to return Set directly because it is an unmodifiable view
  5429. Set keystrokes = (focusTraversalKeys != null)
  5430. ? focusTraversalKeys[id]
  5431. : null;
  5432. if (keystrokes != null) {
  5433. return keystrokes;
  5434. } else {
  5435. Container parent = this.parent;
  5436. if (parent != null) {
  5437. return parent.getFocusTraversalKeys(id);
  5438. } else {
  5439. return KeyboardFocusManager.getCurrentKeyboardFocusManager().
  5440. getDefaultFocusTraversalKeys(id);
  5441. }
  5442. }
  5443. }
  5444. /**
  5445. * Returns whether the Set of focus traversal keys for the given focus
  5446. * traversal operation has been explicitly defined for this Component. If
  5447. * this method returns <code>false</code>, this Component is inheriting the
  5448. * Set from an ancestor, or from the current KeyboardFocusManager.
  5449. *
  5450. * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  5451. * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, or
  5452. * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS
  5453. * @return <code>true</code> if the the Set of focus traversal keys for the
  5454. * given focus traversal operation has been explicitly defined for
  5455. * this Component; <code>false</code> otherwise.
  5456. * @throws IllegalArgumentException if id is not one of
  5457. * KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  5458. * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, or
  5459. * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS
  5460. * @since 1.4
  5461. */
  5462. public boolean areFocusTraversalKeysSet(int id) {
  5463. if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH - 1) {
  5464. throw new IllegalArgumentException("invalid focus traversal key identifier");
  5465. }
  5466. return (focusTraversalKeys != null && focusTraversalKeys[id] != null);
  5467. }
  5468. /**
  5469. * Sets whether focus traversal keys are enabled for this Component.
  5470. * Components for which focus traversal keys are disabled receive key
  5471. * events for focus traversal keys. Components for which focus traversal
  5472. * keys are enabled do not see these events; instead, the events are
  5473. * automatically converted to traversal operations.
  5474. *
  5475. * @param focusTraversalKeysEnabled whether focus traversal keys are
  5476. * enabled for this Component
  5477. * @see #getFocusTraversalKeysEnabled
  5478. * @see #setFocusTraversalKeys
  5479. * @see #getFocusTraversalKeys
  5480. * @since 1.4
  5481. * @beaninfo
  5482. * bound: true
  5483. */
  5484. public void setFocusTraversalKeysEnabled(boolean
  5485. focusTraversalKeysEnabled) {
  5486. boolean oldFocusTraversalKeysEnabled;
  5487. synchronized (this) {
  5488. oldFocusTraversalKeysEnabled = this.focusTraversalKeysEnabled;
  5489. this.focusTraversalKeysEnabled = focusTraversalKeysEnabled;
  5490. }
  5491. firePropertyChange("focusTraversalKeysEnabled",
  5492. oldFocusTraversalKeysEnabled,
  5493. focusTraversalKeysEnabled);
  5494. }
  5495. /**
  5496. * Returns whether focus traversal keys are enabled for this Component.
  5497. * Components for which focus traversal keys are disabled receive key
  5498. * events for focus traversal keys. Components for which focus traversal
  5499. * keys are enabled do not see these events; instead, the events are
  5500. * automatically converted to traversal operations.
  5501. *
  5502. * @return whether focus traversal keys are enabled for this Component
  5503. * @see #setFocusTraversalKeysEnabled
  5504. * @see #setFocusTraversalKeys
  5505. * @see #getFocusTraversalKeys
  5506. * @since 1.4
  5507. */
  5508. public boolean getFocusTraversalKeysEnabled() {
  5509. return focusTraversalKeysEnabled;
  5510. }
  5511. /**
  5512. * Requests that this Component get the input focus, and that this
  5513. * Component's top-level ancestor become the focused Window. This component
  5514. * must be displayable, visible, and focusable for the request to be
  5515. * granted. Every effort will be made to honor the request; however, in
  5516. * some cases it may be impossible to do so. Developers must never assume
  5517. * that this Component is the focus owner until this Component receives a
  5518. * FOCUS_GAINED event. If this request is denied because this Component's
  5519. * top-level Window cannot become the focused Window, the request will be
  5520. * remembered and will be granted when the Window is later focused by the
  5521. * user.
  5522. * <p>
  5523. * This method cannot be used to set the focus owner to no Component at
  5524. * all. Use <code>KeyboardFocusManager.clearGlobalFocusOwner()</code>
  5525. * instead.
  5526. * <p>
  5527. * Because the focus behavior of this method is platform-dependent,
  5528. * developers are strongly encouraged to use
  5529. * <code>requestFocusInWindow</code> when possible.
  5530. *
  5531. * @see #requestFocusInWindow
  5532. * @see java.awt.event.FocusEvent
  5533. * @see #addFocusListener
  5534. * @see #isFocusable
  5535. * @see #isDisplayable
  5536. * @see KeyboardFocusManager#clearGlobalFocusOwner
  5537. * @since JDK1.0
  5538. */
  5539. public void requestFocus() {
  5540. requestFocusHelper(false, true);
  5541. }
  5542. /**
  5543. * Requests that this Component get the input focus, and that this
  5544. * Component's top-level ancestor become the focused Window. This component
  5545. * must be displayable, visible, and focusable for the request to be
  5546. * granted. Every effort will be made to honor the request; however, in
  5547. * some cases it may be impossible to do so. Developers must never assume
  5548. * that this Component is the focus owner until this Component receives a
  5549. * FOCUS_GAINED event. If this request is denied because this Component's
  5550. * top-level Window cannot become the focused Window, the request will be
  5551. * remembered and will be granted when the Window is later focused by the
  5552. * user.
  5553. * <p>
  5554. * This method returns a boolean value. If <code>false</code> is returned,
  5555. * the request is <b>guaranteed to fail</b>. If <code>true</code> is
  5556. * returned, the request will succeed <b>unless</b> it is vetoed, or an
  5557. * extraordinary event, such as disposal of the Component's peer, occurs
  5558. * before the request can be granted by the native windowing system. Again,
  5559. * while a return value of <code>true</code> indicates that the request is
  5560. * likely to succeed, developers must never assume that this Component is
  5561. * the focus owner until this Component receives a FOCUS_GAINED event.
  5562. * <p>
  5563. * This method cannot be used to set the focus owner to no Component at
  5564. * all. Use <code>KeyboardFocusManager.clearGlobalFocusOwner()</code>
  5565. * instead.
  5566. * <p>
  5567. * Because the focus behavior of this method is platform-dependent,
  5568. * developers are strongly encouraged to use
  5569. * <code>requestFocusInWindow</code> when possible.
  5570. * <p>
  5571. * Every effort will be made to ensure that FocusEvents generated as a
  5572. * result of this request will have the specified temporary value. However,
  5573. * because specifying an arbitrary temporary state may not be implementable
  5574. * on all native windowing systems, correct behavior for this method can be
  5575. * guaranteed only for lightweight Components. This method is not intended
  5576. * for general use, but exists instead as a hook for lightweight Component
  5577. * libraries, such as Swing.
  5578. *
  5579. * @return <code>false</code> if the focus change request is guaranteed to
  5580. * fail; <code>true</code> if it is likely to succeed
  5581. * @see java.awt.event.FocusEvent
  5582. * @see #addFocusListener
  5583. * @see #isFocusable
  5584. * @see #isDisplayable
  5585. * @see KeyboardFocusManager#clearGlobalFocusOwner
  5586. * @since 1.4
  5587. */
  5588. protected boolean requestFocus(boolean temporary) {
  5589. return requestFocusHelper(temporary, true);
  5590. }
  5591. /**
  5592. * Requests that this Component get the input focus, if this Component's
  5593. * top-level ancestor is already the focused Window. This component must be
  5594. * displayable, visible, and focusable for the request to be granted. Every
  5595. * effort will be made to honor the request; however, in some cases it may
  5596. * be impossible to do so. Developers must never assume that this Component
  5597. * is the focus owner until this Component receives a FOCUS_GAINED event.
  5598. * <p>
  5599. * This method returns a boolean value. If <code>false</code> is returned,
  5600. * the request is <b>guaranteed to fail</b>. If <code>true</code> is
  5601. * returned, the request will succeed <b>unless</b> it is vetoed, or an
  5602. * extraordinary event, such as disposal of the Component's peer, occurs
  5603. * before the request can be granted by the native windowing system. Again,
  5604. * while a return value of <code>true</code> indicates that the request is
  5605. * likely to succeed, developers must never assume that this Component is
  5606. * the focus owner until this Component receives a FOCUS_GAINED event.
  5607. * <p>
  5608. * This method cannot be used to set the focus owner to no Component at
  5609. * all. Use <code>KeyboardFocusManager.clearGlobalFocusOwner()</code>
  5610. * instead.
  5611. * <p>
  5612. * The focus behavior of this method can be implemented uniformly across
  5613. * platforms, and thus developers are strongly encouraged to use this
  5614. * method over <code>requestFocus</code> when possible. Code which relies
  5615. * on <code>requestFocus</code> may exhibit different focus behavior on
  5616. * different platforms.
  5617. *
  5618. * @return <code>false</code> if the focus change request is guaranteed to
  5619. * fail; <code>true</code> if it is likely to succeed
  5620. * @see #requestFocus
  5621. * @see java.awt.event.FocusEvent
  5622. * @see #addFocusListener
  5623. * @see #isFocusable
  5624. * @see #isDisplayable
  5625. * @see KeyboardFocusManager#clearGlobalFocusOwner
  5626. * @since 1.4
  5627. */
  5628. public boolean requestFocusInWindow() {
  5629. return requestFocusHelper(false, false);
  5630. }
  5631. /**
  5632. * Requests that this Component get the input focus, if this Component's
  5633. * top-level ancestor is already the focused Window. This component must be
  5634. * displayable, visible, and focusable for the request to be granted. Every
  5635. * effort will be made to honor the request; however, in some cases it may
  5636. * be impossible to do so. Developers must never assume that this Component
  5637. * is the focus owner until this Component receives a FOCUS_GAINED event.
  5638. * <p>
  5639. * This method returns a boolean value. If <code>false</code> is returned,
  5640. * the request is <b>guaranteed to fail</b>. If <code>true</code> is
  5641. * returned, the request will succeed <b>unless</b> it is vetoed, or an
  5642. * extraordinary event, such as disposal of the Component's peer, occurs
  5643. * before the request can be granted by the native windowing system. Again,
  5644. * while a return value of <code>true</code> indicates that the request is
  5645. * likely to succeed, developers must never assume that this Component is
  5646. * the focus owner until this Component receives a FOCUS_GAINED event.
  5647. * <p>
  5648. * This method cannot be used to set the focus owner to no Component at
  5649. * all. Use <code>KeyboardFocusManager.clearGlobalFocusOwner()</code>
  5650. * instead.
  5651. * <p>
  5652. * The focus behavior of this method can be implemented uniformly across
  5653. * platforms, and thus developers are strongly encouraged to use this
  5654. * method over <code>requestFocus</code> when possible. Code which relies
  5655. * on <code>requestFocus</code> may exhibit different focus behavior on
  5656. * different platforms.
  5657. * <p>
  5658. * Every effort will be made to ensure that FocusEvents generated as a
  5659. * result of this request will have the specified temporary value. However,
  5660. * because specifying an arbitrary temporary state may not be implementable
  5661. * on all native windowing systems, correct behavior for this method can be
  5662. * guaranteed only for lightweight Components. This method is not intended
  5663. * for general use, but exists instead as a hook for lightweight Component
  5664. * libraries, such as Swing.
  5665. *
  5666. * @return <code>false</code> if the focus change request is guaranteed to
  5667. * fail; <code>true</code> if it is likely to succeed
  5668. * @see #requestFocus
  5669. * @see java.awt.event.FocusEvent
  5670. * @see #addFocusListener
  5671. * @see #isFocusable
  5672. * @see #isDisplayable
  5673. * @see KeyboardFocusManager#clearGlobalFocusOwner
  5674. * @since 1.4
  5675. */
  5676. protected boolean requestFocusInWindow(boolean temporary) {
  5677. return requestFocusHelper(temporary, false);
  5678. }
  5679. final boolean requestFocusHelper(boolean temporary,
  5680. boolean focusedWindowChangeAllowed) {
  5681. if (isFocusable() && isVisible()) {
  5682. ComponentPeer peer = this.peer;
  5683. if (peer != null) {
  5684. Component window = this;
  5685. while (!(window instanceof Window)) {
  5686. window = window.parent;
  5687. }
  5688. if (window == null || !((Window)window).isFocusableWindow()) {
  5689. return false;
  5690. }
  5691. // Update most-recent map
  5692. KeyboardFocusManager.setMostRecentFocusOwner(this);
  5693. Component heavyweight = (peer instanceof LightweightPeer)
  5694. ? getNativeContainer() : this;
  5695. if (heavyweight == null || !heavyweight.isVisible()) {
  5696. return false;
  5697. }
  5698. peer = heavyweight.peer;
  5699. if (peer == null) {
  5700. return false;
  5701. }
  5702. // Focus this Component
  5703. long time = EventQueue.getMostRecentEventTime();
  5704. boolean success = peer.requestFocus
  5705. (this, temporary, focusedWindowChangeAllowed, time);
  5706. if (!success) {
  5707. KeyboardFocusManager.getCurrentKeyboardFocusManager
  5708. (SunToolkit.targetToAppContext(this)).
  5709. dequeueKeyEvents(time, this);
  5710. }
  5711. return success;
  5712. }
  5713. }
  5714. return false;
  5715. }
  5716. final void autoTransferFocus(boolean clearOnFailure) {
  5717. Component toTest = KeyboardFocusManager.
  5718. getCurrentKeyboardFocusManager().getFocusOwner();
  5719. if (toTest != this) {
  5720. if (toTest != null) {
  5721. toTest.autoTransferFocus(clearOnFailure);
  5722. }
  5723. return;
  5724. }
  5725. // the following code will execute only if this Component is the focus
  5726. // owner
  5727. if (!(isDisplayable() && isVisible() && isEnabled() && isFocusable())) {
  5728. doAutoTransfer(clearOnFailure);
  5729. return;
  5730. }
  5731. toTest = getParent();
  5732. while (toTest != null && !(toTest instanceof Window)) {
  5733. if (!(toTest.isDisplayable() && toTest.isVisible() &&
  5734. (toTest.isEnabled() || toTest.isLightweight()))) {
  5735. doAutoTransfer(clearOnFailure);
  5736. return;
  5737. }
  5738. toTest = toTest.getParent();
  5739. }
  5740. }
  5741. private void doAutoTransfer(boolean clearOnFailure) {
  5742. if (clearOnFailure) {
  5743. if (!nextFocusHelper()) {
  5744. KeyboardFocusManager.getCurrentKeyboardFocusManager().
  5745. clearGlobalFocusOwner();
  5746. }
  5747. } else {
  5748. transferFocus();
  5749. }
  5750. }
  5751. /**
  5752. * Transfers the focus to the next component, as though this Component were
  5753. * the focus owner.
  5754. * @see #requestFocus()
  5755. * @since JDK1.1
  5756. */
  5757. public void transferFocus() {
  5758. nextFocus();
  5759. }
  5760. /**
  5761. * Returns the Container which is the focus cycle root of this Component's
  5762. * focus traversal cycle. Each focus traversal cycle has only a single
  5763. * focus cycle root and each Component which is not a Container belongs to
  5764. * only a single focus traversal cycle. Containers which are focus cycle
  5765. * roots belong to two cycles: one rooted at the Container itself, and one
  5766. * rooted at the Container's nearest focus-cycle-root ancestor. For such
  5767. * Containers, this method will return the Container's nearest focus-cycle-
  5768. * root ancestor.
  5769. *
  5770. * @return this Component's nearest focus-cycle-root ancestor
  5771. * @see Container#isFocusCycleRoot()
  5772. * @since 1.4
  5773. */
  5774. public Container getFocusCycleRootAncestor() {
  5775. Container rootAncestor = this.parent;
  5776. while (rootAncestor != null && !rootAncestor.isFocusCycleRoot()) {
  5777. rootAncestor = rootAncestor.parent;
  5778. }
  5779. return rootAncestor;
  5780. }
  5781. /**
  5782. * Returns whether the specified Container is the focus cycle root of this
  5783. * Component's focus traversal cycle. Each focus traversal cycle has only
  5784. * a single focus cycle root and each Component which is not a Container
  5785. * belongs to only a single focus traversal cycle.
  5786. *
  5787. * @param container the Container to be tested
  5788. * @return <code>true</code> if the specified Container is a focus-cycle-
  5789. * root of this Component; <code>false</code> otherwise
  5790. * @see Container#isFocusCycleRoot()
  5791. * @since 1.4
  5792. */
  5793. public boolean isFocusCycleRoot(Container container) {
  5794. Container rootAncestor = getFocusCycleRootAncestor();
  5795. return (rootAncestor == container);
  5796. }
  5797. /**
  5798. * @deprecated As of JDK version 1.1,
  5799. * replaced by transferFocus().
  5800. */
  5801. public void nextFocus() {
  5802. nextFocusHelper();
  5803. }
  5804. boolean nextFocusHelper() {
  5805. Container rootAncestor = getFocusCycleRootAncestor();
  5806. Component comp = this;
  5807. while (rootAncestor != null &&
  5808. !(rootAncestor.isShowing() &&
  5809. rootAncestor.isFocusable() &&
  5810. rootAncestor.isEnabled()))
  5811. {
  5812. comp = rootAncestor;
  5813. rootAncestor = comp.getFocusCycleRootAncestor();
  5814. }
  5815. if (rootAncestor != null) {
  5816. FocusTraversalPolicy policy =
  5817. rootAncestor.getFocusTraversalPolicy();
  5818. Component toFocus = policy.getComponentAfter(rootAncestor, comp);
  5819. if (toFocus == null) {
  5820. toFocus = policy.getDefaultComponent(rootAncestor);
  5821. }
  5822. if (toFocus != null) {
  5823. return toFocus.requestFocus(false);
  5824. }
  5825. }
  5826. return false;
  5827. }
  5828. /**
  5829. * Transfers the focus to the previous component, as though this Component
  5830. * were the focus owner.
  5831. * @see #requestFocus()
  5832. * @since 1.4
  5833. */
  5834. public void transferFocusBackward() {
  5835. Container rootAncestor = getFocusCycleRootAncestor();
  5836. Component comp = this;
  5837. while (rootAncestor != null &&
  5838. !(rootAncestor.isShowing() &&
  5839. rootAncestor.isFocusable() &&
  5840. rootAncestor.isEnabled()))
  5841. {
  5842. comp = rootAncestor;
  5843. rootAncestor = comp.getFocusCycleRootAncestor();
  5844. }
  5845. if (rootAncestor != null) {
  5846. FocusTraversalPolicy policy =
  5847. rootAncestor.getFocusTraversalPolicy();
  5848. Component toFocus = policy.getComponentBefore(rootAncestor, comp);
  5849. if (toFocus == null) {
  5850. toFocus = policy.getDefaultComponent(rootAncestor);
  5851. }
  5852. if (toFocus != null) {
  5853. toFocus.requestFocus();
  5854. }
  5855. }
  5856. }
  5857. /**
  5858. * Transfers the focus up one focus traversal cycle. Typically, the focus
  5859. * owner is set to this Component's focus cycle root, and the current focus
  5860. * cycle root is set to the new focus owner's focus cycle root. If,
  5861. * however, this Component's focus cycle root is a Window, then the focus
  5862. * owner is set to the focus cycle root's default Component to focus, and
  5863. * the current focus cycle root is unchanged.
  5864. *
  5865. * @see #requestFocus()
  5866. * @see Container#isFocusCycleRoot()
  5867. * @see Container#setFocusCycleRoot(boolean)
  5868. * @since 1.4
  5869. */
  5870. public void transferFocusUpCycle() {
  5871. Container rootAncestor;
  5872. for (rootAncestor = getFocusCycleRootAncestor();
  5873. rootAncestor != null && !(rootAncestor.isShowing() &&
  5874. rootAncestor.isFocusable() &&
  5875. rootAncestor.isEnabled());
  5876. rootAncestor = rootAncestor.getFocusCycleRootAncestor()) {
  5877. }
  5878. if (rootAncestor != null) {
  5879. Container rootAncestorRootAncestor =
  5880. rootAncestor.getFocusCycleRootAncestor();
  5881. KeyboardFocusManager.getCurrentKeyboardFocusManager().
  5882. setGlobalCurrentFocusCycleRoot(
  5883. (rootAncestorRootAncestor != null)
  5884. ? rootAncestorRootAncestor
  5885. : rootAncestor);
  5886. rootAncestor.requestFocus();
  5887. } else {
  5888. Container window =
  5889. (this instanceof Container) ? ((Container)this) : getParent();
  5890. while (window != null && !(window instanceof Window)) {
  5891. window = window.getParent();
  5892. }
  5893. if (window != null) {
  5894. Component toFocus = window.getFocusTraversalPolicy().
  5895. getDefaultComponent(window);
  5896. if (toFocus != null) {
  5897. KeyboardFocusManager.getCurrentKeyboardFocusManager().
  5898. setGlobalCurrentFocusCycleRoot(window);
  5899. toFocus.requestFocus();
  5900. }
  5901. }
  5902. }
  5903. }
  5904. /**
  5905. * Returns <code>true</code> if this <code>Component</code> is the
  5906. * focus owner. This method is obsolete, and has been replaced by
  5907. * <code>isFocusOwner()</code>.
  5908. *
  5909. * @return <code>true</code> if this <code>Component</code> is the
  5910. * focus owner; <code>false</code> otherwise
  5911. * @since 1.2
  5912. */
  5913. public boolean hasFocus() {
  5914. return (KeyboardFocusManager.getCurrentKeyboardFocusManager().
  5915. getFocusOwner() == this);
  5916. }
  5917. /**
  5918. * Returns <code>true</code> if this <code>Component</code> is the
  5919. * focus owner.
  5920. *
  5921. * @return <code>true</code> if this <code>Component</code> is the
  5922. * focus owner; <code>false</code> otherwise
  5923. * @since 1.4
  5924. */
  5925. public boolean isFocusOwner() {
  5926. return hasFocus();
  5927. }
  5928. /**
  5929. * Adds the specified popup menu to the component.
  5930. * @param popup the popup menu to be added to the component.
  5931. * @see #remove(MenuComponent)
  5932. * @since JDK1.1
  5933. */
  5934. public synchronized void add(PopupMenu popup) {
  5935. if (popup.parent != null) {
  5936. popup.parent.remove(popup);
  5937. }
  5938. if (popups == null) {
  5939. popups = new Vector();
  5940. }
  5941. popups.addElement(popup);
  5942. popup.parent = this;
  5943. if (peer != null) {
  5944. if (popup.peer == null) {
  5945. popup.addNotify();
  5946. }
  5947. }
  5948. }
  5949. /**
  5950. * Removes the specified popup menu from the component.
  5951. * @param popup the popup menu to be removed
  5952. * @see #add(PopupMenu)
  5953. * @since JDK1.1
  5954. */
  5955. public synchronized void remove(MenuComponent popup) {
  5956. if (popups != null) {
  5957. int index = popups.indexOf(popup);
  5958. if (index >= 0) {
  5959. PopupMenu pmenu = (PopupMenu)popup;
  5960. if (pmenu.peer != null) {
  5961. pmenu.removeNotify();
  5962. }
  5963. pmenu.parent = null;
  5964. popups.removeElementAt(index);
  5965. if (popups.size() == 0) {
  5966. popups = null;
  5967. }
  5968. }
  5969. }
  5970. }
  5971. /**
  5972. * Returns a string representing the state of this component. This
  5973. * method is intended to be used only for debugging purposes, and the
  5974. * content and format of the returned string may vary between
  5975. * implementations. The returned string may be empty but may not be
  5976. * <code>null</code>.
  5977. *
  5978. * @return a string representation of this component's state
  5979. * @since JDK1.0
  5980. */
  5981. protected String paramString() {
  5982. String thisName = getName();
  5983. String str = (thisName != null? thisName : "") + "," + x + "," + y + "," + width + "x" + height;
  5984. if (!valid) {
  5985. str += ",invalid";
  5986. }
  5987. if (!visible) {
  5988. str += ",hidden";
  5989. }
  5990. if (!enabled) {
  5991. str += ",disabled";
  5992. }
  5993. return str;
  5994. }
  5995. /**
  5996. * Returns a string representation of this component and its values.
  5997. * @return a string representation of this component
  5998. * @since JDK1.0
  5999. */
  6000. public String toString() {
  6001. return getClass().getName() + "[" + paramString() + "]";
  6002. }
  6003. /**
  6004. * Prints a listing of this component to the standard system output
  6005. * stream <code>System.out</code>.
  6006. * @see java.lang.System#out
  6007. * @since JDK1.0
  6008. */
  6009. public void list() {
  6010. list(System.out, 0);
  6011. }
  6012. /**
  6013. * Prints a listing of this component to the specified output
  6014. * stream.
  6015. * @param out a print stream
  6016. * @since JDK1.0
  6017. */
  6018. public void list(PrintStream out) {
  6019. list(out, 0);
  6020. }
  6021. /**
  6022. * Prints out a list, starting at the specified indentation, to the
  6023. * specified print stream.
  6024. * @param out a print stream
  6025. * @param indent number of spaces to indent
  6026. * @see java.io.PrintStream#println(java.lang.Object)
  6027. * @since JDK1.0
  6028. */
  6029. public void list(PrintStream out, int indent) {
  6030. for (int i = 0 ; i < indent ; i++) {
  6031. out.print(" ");
  6032. }
  6033. out.println(this);
  6034. }
  6035. /**
  6036. * Prints a listing to the specified print writer.
  6037. * @param out the print writer to print to
  6038. * @since JDK1.1
  6039. */
  6040. public void list(PrintWriter out) {
  6041. list(out, 0);
  6042. }
  6043. /**
  6044. * Prints out a list, starting at the specified indentation, to
  6045. * the specified print writer.
  6046. * @param out the print writer to print to
  6047. * @param indent the number of spaces to indent
  6048. * @see java.io.PrintStream#println(java.lang.Object)
  6049. * @since JDK1.1
  6050. */
  6051. public void list(PrintWriter out, int indent) {
  6052. for (int i = 0 ; i < indent ; i++) {
  6053. out.print(" ");
  6054. }
  6055. out.println(this);
  6056. }
  6057. /*
  6058. * Fetches the native container somewhere higher up in the component
  6059. * tree that contains this component.
  6060. */
  6061. Container getNativeContainer() {
  6062. Container p = parent;
  6063. while (p != null && p.peer instanceof LightweightPeer) {
  6064. p = p.getParent();
  6065. }
  6066. return p;
  6067. }
  6068. /**
  6069. * Adds a PropertyChangeListener to the listener list. The listener is
  6070. * registered for all bound properties of this class, including the
  6071. * following:
  6072. * <ul>
  6073. * <li>this Component's font ("font")</li>
  6074. * <li>this Component's background color ("background")</li>
  6075. * <li>this Component's foreground color ("foreground")</li>
  6076. * <li>this Component's focusability ("focusable")</li>
  6077. * <li>this Component's focus traversal keys enabled state
  6078. * ("focusTraversalKeysEnabled")</li>
  6079. * <li>this Component's Set of FORWARD_TRAVERSAL_KEYS
  6080. * ("forwardFocusTraversalKeys")</li>
  6081. * <li>this Component's Set of BACKWARD_TRAVERSAL_KEYS
  6082. * ("backwardFocusTraversalKeys")</li>
  6083. * <li>this Component's Set of UP_CYCLE_TRAVERSAL_KEYS
  6084. * ("upCycleFocusTraversalKeys")</li>
  6085. * </ul>
  6086. * Note that if this Component is inheriting a bound property, then no
  6087. * event will be fired in response to a change in the inherited property.
  6088. * <p>
  6089. * If listener is null, no exception is thrown and no action is performed.
  6090. *
  6091. * @param listener the PropertyChangeListener to be added
  6092. *
  6093. * @see #removePropertyChangeListener
  6094. * @see #getPropertyChangeListeners
  6095. * @see #addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
  6096. */
  6097. public synchronized void addPropertyChangeListener(
  6098. PropertyChangeListener listener) {
  6099. if (listener == null) {
  6100. return;
  6101. }
  6102. if (changeSupport == null) {
  6103. changeSupport = new java.beans.PropertyChangeSupport(this);
  6104. }
  6105. changeSupport.addPropertyChangeListener(listener);
  6106. }
  6107. /**
  6108. * Removes a PropertyChangeListener from the listener list. This method
  6109. * should be used to remove PropertyChangeListeners that were registered
  6110. * for all bound properties of this class.
  6111. * <p>
  6112. * If listener is null, no exception is thrown and no action is performed.
  6113. *
  6114. * @param listener the PropertyChangeListener to be removed
  6115. *
  6116. * @see #addPropertyChangeListener
  6117. * @see #getPropertyChangeListeners
  6118. * @see #removePropertyChangeListener(java.lang.String,java.beans.PropertyChangeListener)
  6119. */
  6120. public synchronized void removePropertyChangeListener(
  6121. PropertyChangeListener listener) {
  6122. if (listener == null || changeSupport == null) {
  6123. return;
  6124. }
  6125. changeSupport.removePropertyChangeListener(listener);
  6126. }
  6127. /**
  6128. * Returns an array of all the property change listeners
  6129. * registered on this component.
  6130. *
  6131. * @return all of this component's <code>PropertyChangeListener</code>s
  6132. * or an empty array if no property change
  6133. * listeners are currently registered
  6134. *
  6135. * @see #addPropertyChangeListener
  6136. * @see #removePropertyChangeListener
  6137. * @see #getPropertyChangeListeners(java.lang.String)
  6138. * @see java.beans.PropertyChangeSupport#getPropertyChangeListeners
  6139. * @since 1.4
  6140. */
  6141. public synchronized PropertyChangeListener[] getPropertyChangeListeners() {
  6142. if (changeSupport == null) {
  6143. return new PropertyChangeListener[0];
  6144. }
  6145. return changeSupport.getPropertyChangeListeners();
  6146. }
  6147. /**
  6148. * Adds a PropertyChangeListener to the listener list for a specific
  6149. * property. The specified property may be user-defined, or one of the
  6150. * following:
  6151. * <ul>
  6152. * <li>this Component's font ("font")</li>
  6153. * <li>this Component's background color ("background")</li>
  6154. * <li>this Component's foreground color ("foreground")</li>
  6155. * <li>this Component's focusability ("focusable")</li>
  6156. * <li>this Component's focus traversal keys enabled state
  6157. * ("focusTraversalKeysEnabled")</li>
  6158. * <li>this Component's Set of FORWARD_TRAVERSAL_KEYS
  6159. * ("forwardFocusTraversalKeys")</li>
  6160. * <li>this Component's Set of BACKWARD_TRAVERSAL_KEYS
  6161. * ("backwardFocusTraversalKeys")</li>
  6162. * <li>this Component's Set of UP_CYCLE_TRAVERSAL_KEYS
  6163. * ("upCycleFocusTraversalKeys")</li>
  6164. * </ul>
  6165. * Note that if this Component is inheriting a bound property, then no
  6166. * event will be fired in response to a change in the inherited property.
  6167. * <p>
  6168. * If listener is null, no exception is thrown and no action is performed.
  6169. *
  6170. * @param propertyName one of the property names listed above
  6171. * @param listener the PropertyChangeListener to be added
  6172. *
  6173. * @see #removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
  6174. * @see #getPropertyChangeListeners(java.lang.String)
  6175. * @see #addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
  6176. */
  6177. public synchronized void addPropertyChangeListener(
  6178. String propertyName,
  6179. PropertyChangeListener listener) {
  6180. if (listener == null) {
  6181. return;
  6182. }
  6183. if (changeSupport == null) {
  6184. changeSupport = new java.beans.PropertyChangeSupport(this);
  6185. }
  6186. changeSupport.addPropertyChangeListener(propertyName, listener);
  6187. }
  6188. /**
  6189. * Removes a PropertyChangeListener from the listener list for a specific
  6190. * property. This method should be used to remove PropertyChangeListeners
  6191. * that were registered for a specific bound property.
  6192. * <p>
  6193. * If listener is null, no exception is thrown and no action is performed.
  6194. *
  6195. * @param propertyName a valid property name
  6196. * @param listener the PropertyChangeListener to be removed
  6197. *
  6198. * @see #addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
  6199. * @see #getPropertyChangeListeners(java.lang.String)
  6200. * @see #removePropertyChangeListener(java.beans.PropertyChangeListener)
  6201. */
  6202. public synchronized void removePropertyChangeListener(
  6203. String propertyName,
  6204. PropertyChangeListener listener) {
  6205. if (listener == null || changeSupport == null) {
  6206. return;
  6207. }
  6208. changeSupport.removePropertyChangeListener(propertyName, listener);
  6209. }
  6210. /**
  6211. * Returns an array of all the listeners which have been associated
  6212. * with the named property.
  6213. *
  6214. * @return all of the <code>PropertyChangeListeners</code> associated with
  6215. * the named property or an empty array if no listeners have
  6216. * been added
  6217. *
  6218. * @see #addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
  6219. * @see #removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
  6220. * @see #getPropertyChangeListeners
  6221. * @since 1.4
  6222. */
  6223. public synchronized PropertyChangeListener[] getPropertyChangeListeners(
  6224. String propertyName) {
  6225. if (changeSupport == null) {
  6226. return new PropertyChangeListener[0];
  6227. }
  6228. return changeSupport.getPropertyChangeListeners(propertyName);
  6229. }
  6230. /**
  6231. * Support for reporting bound property changes for Object properties.
  6232. * This method can be called when a bound property has changed and it will
  6233. * send the appropriate PropertyChangeEvent to any registered
  6234. * PropertyChangeListeners.
  6235. *
  6236. * @param propertyName the property whose value has changed
  6237. * @param oldValue the property's previous value
  6238. * @param newValue the property's new value
  6239. */
  6240. protected void firePropertyChange(String propertyName,
  6241. Object oldValue, Object newValue) {
  6242. java.beans.PropertyChangeSupport changeSupport = this.changeSupport;
  6243. if (changeSupport == null) {
  6244. return;
  6245. }
  6246. changeSupport.firePropertyChange(propertyName, oldValue, newValue);
  6247. }
  6248. /**
  6249. * Support for reporting bound property changes for boolean properties.
  6250. * This method can be called when a bound property has changed and it will
  6251. * send the appropriate PropertyChangeEvent to any registered
  6252. * PropertyChangeListeners.
  6253. *
  6254. * @param propertyName the property whose value has changed
  6255. * @param oldValue the property's previous value
  6256. * @param newValue the property's new value
  6257. */
  6258. protected void firePropertyChange(String propertyName,
  6259. boolean oldValue, boolean newValue) {
  6260. java.beans.PropertyChangeSupport changeSupport = this.changeSupport;
  6261. if (changeSupport == null) {
  6262. return;
  6263. }
  6264. changeSupport.firePropertyChange(propertyName, oldValue, newValue);
  6265. }
  6266. /**
  6267. * Support for reporting bound property changes for integer properties.
  6268. * This method can be called when a bound property has changed and it will
  6269. * send the appropriate PropertyChangeEvent to any registered
  6270. * PropertyChangeListeners.
  6271. *
  6272. * @param propertyName the property whose value has changed
  6273. * @param oldValue the property's previous value
  6274. * @param newValue the property's new value
  6275. */
  6276. protected void firePropertyChange(String propertyName,
  6277. int oldValue, int newValue) {
  6278. java.beans.PropertyChangeSupport changeSupport = this.changeSupport;
  6279. if (changeSupport == null) {
  6280. return;
  6281. }
  6282. changeSupport.firePropertyChange(propertyName, oldValue, newValue);
  6283. }
  6284. // Serialization support.
  6285. /**
  6286. * Component Serialized Data Version.
  6287. *
  6288. * @serial
  6289. */
  6290. private int componentSerializedDataVersion = 4;
  6291. /**
  6292. * Writes default serializable fields to stream. Writes
  6293. * a variety of serializable listeners as optional data.
  6294. * The non-serializable listeners are detected and
  6295. * no attempt is made to serialize them.
  6296. *
  6297. * @param s the <code>ObjectOutputStream</code> to write
  6298. * @serialData <code>null</code> terminated sequence of
  6299. * 0 or more pairs; the pair consists of a <code>String</code>
  6300. * and an <code>Object</code> the <code>String</code> indicates
  6301. * the type of object and is one of the following (as of 1.4):
  6302. * <code>componentListenerK</code> indicating an
  6303. * <code>ComponentListener</code> object;
  6304. * <code>focusListenerK</code> indicating an
  6305. * <code>FocusListener</code> object;
  6306. * <code>keyListenerK</code> indicating an
  6307. * <code>KeyListener</code> object;
  6308. * <code>mouseListenerK</code> indicating an
  6309. * <code>MouseListener</code> object;
  6310. * <code>mouseMotionListenerK</code> indicating an
  6311. * <code>MouseMotionListener</code> object;
  6312. * <code>inputListenerK</code> indicating an
  6313. * <code>InputListener</code> object;
  6314. * <code>hierarchyListenerK</code> indicating an
  6315. * <code>HierarchyListener</code> object;
  6316. * <code>hierarchyBoundsListenerK</code> indicating an
  6317. * <code>HierarchyBoundsListener</code> object;
  6318. * <code>mouseWheelListenerK</code> indicating an
  6319. * <code>MouseWheelListener</code> object
  6320. * @serialData an optional <code>ComponentOrientation</code>
  6321. * (after <code>inputMethodListener</code>, as of 1.2)
  6322. *
  6323. * @see AWTEventMulticaster#save(java.io.ObjectOutputStream, java.lang.String, java.util.EventListener)
  6324. * @see #componentListenerK
  6325. * @see #focusListenerK
  6326. * @see #keyListenerK
  6327. * @see #mouseListenerK
  6328. * @see #mouseMotionListenerK
  6329. * @see #inputListenerK
  6330. * @see #hierarchyListenerK
  6331. * @see #hierarchyBoundsListenerK
  6332. * @see #mouseWheelListenerK
  6333. * @see #readObject(ObjectInputStream)
  6334. */
  6335. private void writeObject(ObjectOutputStream s)
  6336. throws IOException
  6337. {
  6338. s.defaultWriteObject();
  6339. AWTEventMulticaster.save(s, componentListenerK, componentListener);
  6340. AWTEventMulticaster.save(s, focusListenerK, focusListener);
  6341. AWTEventMulticaster.save(s, keyListenerK, keyListener);
  6342. AWTEventMulticaster.save(s, mouseListenerK, mouseListener);
  6343. AWTEventMulticaster.save(s, mouseMotionListenerK, mouseMotionListener);
  6344. AWTEventMulticaster.save(s, inputMethodListenerK, inputMethodListener);
  6345. s.writeObject(null);
  6346. s.writeObject(componentOrientation);
  6347. AWTEventMulticaster.save(s, hierarchyListenerK, hierarchyListener);
  6348. AWTEventMulticaster.save(s, hierarchyBoundsListenerK,
  6349. hierarchyBoundsListener);
  6350. s.writeObject(null);
  6351. AWTEventMulticaster.save(s, mouseWheelListenerK, mouseWheelListener);
  6352. s.writeObject(null);
  6353. }
  6354. /**
  6355. * Reads the <code>ObjectInputStream</code> and if it isn't
  6356. * <code>null</code> adds a listener to receive a variety
  6357. * of events fired by the component.
  6358. * Unrecognized keys or values will be ignored.
  6359. *
  6360. * @param s the <code>ObjectInputStream</code> to read
  6361. * @see #writeObject(ObjectOutputStream)
  6362. */
  6363. private void readObject(ObjectInputStream s)
  6364. throws ClassNotFoundException, IOException
  6365. {
  6366. s.defaultReadObject();
  6367. privateKey = new Object();
  6368. appContext = AppContext.getAppContext();
  6369. SunToolkit.insertTargetMapping(this, appContext);
  6370. if (componentSerializedDataVersion < 4) {
  6371. // These fields are non-transient and rely on default
  6372. // serialization. However, the default values are insufficient,
  6373. // so we need to set them explicitly for object data streams prior
  6374. // to 1.4.
  6375. focusable = true;
  6376. isFocusTraversableOverridden = FOCUS_TRAVERSABLE_UNKNOWN;
  6377. initializeFocusTraversalKeys();
  6378. focusTraversalKeysEnabled = true;
  6379. }
  6380. Object keyOrNull;
  6381. while(null != (keyOrNull = s.readObject())) {
  6382. String key = ((String)keyOrNull).intern();
  6383. if (componentListenerK == key)
  6384. addComponentListener((ComponentListener)(s.readObject()));
  6385. else if (focusListenerK == key)
  6386. addFocusListener((FocusListener)(s.readObject()));
  6387. else if (keyListenerK == key)
  6388. addKeyListener((KeyListener)(s.readObject()));
  6389. else if (mouseListenerK == key)
  6390. addMouseListener((MouseListener)(s.readObject()));
  6391. else if (mouseMotionListenerK == key)
  6392. addMouseMotionListener((MouseMotionListener)(s.readObject()));
  6393. else if (inputMethodListenerK == key)
  6394. addInputMethodListener((InputMethodListener)(s.readObject()));
  6395. else // skip value for unrecognized key
  6396. s.readObject();
  6397. }
  6398. // Read the component's orientation if it's present
  6399. Object orient = null;
  6400. try {
  6401. orient = s.readObject();
  6402. } catch (java.io.OptionalDataException e) {
  6403. // JDK 1.1 instances will not have this optional data.
  6404. // e.eof will be true to indicate that there is no more
  6405. // data available for this object.
  6406. // If e.eof is not true, throw the exception as it
  6407. // might have been caused by reasons unrelated to
  6408. // componentOrientation.
  6409. if (!e.eof) {
  6410. throw (e);
  6411. }
  6412. }
  6413. if (orient != null) {
  6414. componentOrientation = (ComponentOrientation)orient;
  6415. } else {
  6416. componentOrientation = ComponentOrientation.UNKNOWN;
  6417. }
  6418. try {
  6419. while(null != (keyOrNull = s.readObject())) {
  6420. String key = ((String)keyOrNull).intern();
  6421. if (hierarchyListenerK == key) {
  6422. addHierarchyListener((HierarchyListener)(s.readObject()));
  6423. }
  6424. else if (hierarchyBoundsListenerK == key) {
  6425. addHierarchyBoundsListener((HierarchyBoundsListener)
  6426. (s.readObject()));
  6427. }
  6428. else {
  6429. // skip value for unrecognized key
  6430. s.readObject();
  6431. }
  6432. }
  6433. } catch (java.io.OptionalDataException e) {
  6434. // JDK 1.1/1.2 instances will not have this optional data.
  6435. // e.eof will be true to indicate that there is no more
  6436. // data available for this object.
  6437. // If e.eof is not true, throw the exception as it
  6438. // might have been caused by reasons unrelated to
  6439. // hierarchy and hierarchyBounds listeners.
  6440. if (!e.eof) {
  6441. throw (e);
  6442. }
  6443. }
  6444. try {
  6445. while (null != (keyOrNull = s.readObject())) {
  6446. String key = ((String)keyOrNull).intern();
  6447. if (mouseWheelListenerK == key) {
  6448. addMouseWheelListener((MouseWheelListener)(s.readObject()));
  6449. }
  6450. else {
  6451. // skip value for unrecognized key
  6452. s.readObject();
  6453. }
  6454. }
  6455. } catch (java.io.OptionalDataException e) {
  6456. // pre-1.3 instances will not have this optional data.
  6457. // e.eof will be true to indicate that there is no more
  6458. // data available for this object.
  6459. // If e.eof is not true, throw the exception as it
  6460. // might have been caused by reasons unrelated to
  6461. // mouse wheel listeners
  6462. if (!e.eof) {
  6463. throw (e);
  6464. }
  6465. }
  6466. if (popups != null) {
  6467. int npopups = popups.size();
  6468. for (int i = 0 ; i < npopups ; i++) {
  6469. PopupMenu popup = (PopupMenu)popups.elementAt(i);
  6470. popup.parent = this;
  6471. }
  6472. }
  6473. }
  6474. /**
  6475. * Sets the language-sensitive orientation that is to be used to order
  6476. * the elements or text within this component. Language-sensitive
  6477. * <code>LayoutManager</code> and <code>Component</code>
  6478. * subclasses will use this property to
  6479. * determine how to lay out and draw components.
  6480. * <p>
  6481. * At construction time, a component's orientation is set to
  6482. * <code>ComponentOrientation.UNKNOWN</code>,
  6483. * indicating that it has not been specified
  6484. * explicitly. The UNKNOWN orientation behaves the same as
  6485. * <code>ComponentOrientation.LEFT_TO_RIGHT</code>.
  6486. * <p>
  6487. * To set the orientation of a single component, use this method.
  6488. * To set the orientation of an entire component
  6489. * hierarchy, use
  6490. * {@link #applyComponentOrientation applyComponentOrientation}.
  6491. *
  6492. * @see ComponentOrientation
  6493. *
  6494. * @author Laura Werner, IBM
  6495. * @beaninfo
  6496. * bound: true
  6497. */
  6498. public void setComponentOrientation(ComponentOrientation o) {
  6499. ComponentOrientation oldValue = componentOrientation;
  6500. componentOrientation = o;
  6501. // This is a bound property, so report the change to
  6502. // any registered listeners. (Cheap if there are none.)
  6503. firePropertyChange("componentOrientation", oldValue, o);
  6504. // This could change the preferred size of the Component.
  6505. if (valid) {
  6506. invalidate();
  6507. }
  6508. }
  6509. /**
  6510. * Retrieves the language-sensitive orientation that is to be used to order
  6511. * the elements or text within this component. <code>LayoutManager</code>
  6512. * and <code>Component</code>
  6513. * subclasses that wish to respect orientation should call this method to
  6514. * get the component's orientation before performing layout or drawing.
  6515. *
  6516. * @see ComponentOrientation
  6517. *
  6518. * @author Laura Werner, IBM
  6519. */
  6520. public ComponentOrientation getComponentOrientation() {
  6521. return componentOrientation;
  6522. }
  6523. /**
  6524. * Sets the <code>ComponentOrientation</code> property of this component
  6525. * and all components contained within it.
  6526. *
  6527. * @param orientation the new component orientation of this component and
  6528. * the components contained within it.
  6529. * @exception NullPointerException if <code>orientation</code> is null.
  6530. * @see #setComponentOrientation
  6531. * @see #getComponentOrientation
  6532. * @since 1.4
  6533. */
  6534. public void applyComponentOrientation(ComponentOrientation orientation) {
  6535. if (orientation == null) {
  6536. throw new NullPointerException();
  6537. }
  6538. setComponentOrientation(orientation);
  6539. }
  6540. /**
  6541. * This odd class is to help out a native component that has been
  6542. * embedded in a lightweight component. Moving lightweight
  6543. * components around and changing their visibility is not seen
  6544. * by the native window system. This is a feature for lightweights,
  6545. * but a problem for native components that depend upon the
  6546. * lightweights. An instance of this class listens to the lightweight
  6547. * parents of an associated native component (the outer class).
  6548. *
  6549. * @author Timothy Prinzing
  6550. */
  6551. private final class NativeInLightFixer implements ComponentListener, ContainerListener {
  6552. NativeInLightFixer() {
  6553. lightParents = new Vector();
  6554. Container p = parent;
  6555. // stash a reference to the components that are being observed so that
  6556. // we can reliably remove ourself as a listener later.
  6557. for (; p.peer instanceof LightweightPeer; p = p.parent) {
  6558. // register listeners and stash a reference
  6559. p.addComponentListener(this);
  6560. p.addContainerListener(this);
  6561. lightParents.addElement(p);
  6562. }
  6563. // register with the native host (native parent of associated native)
  6564. // to get notified if the top-level lightweight is removed.
  6565. nativeHost = p;
  6566. p.addContainerListener(this);
  6567. // kick start the fixup. Since the event isn't looked at
  6568. // we can simulate movement notification.
  6569. componentMoved(null);
  6570. }
  6571. // --- ComponentListener -------------------------------------------
  6572. /**
  6573. * Invoked when one of the lightweight parents has been resized.
  6574. * This doesn't change the position of the native child so it
  6575. * is ignored.
  6576. */
  6577. public void componentResized(ComponentEvent e) {
  6578. }
  6579. /**
  6580. * Invoked when one of the lightweight parents has been moved.
  6581. * The native peer must be told of the new position which is
  6582. * relative to the native container that is hosting the
  6583. * lightweight components.
  6584. */
  6585. public void componentMoved(ComponentEvent e) {
  6586. synchronized (getTreeLock()) {
  6587. int nativeX = x;
  6588. int nativeY = y;
  6589. for(Component c = parent; (c != null) &&
  6590. (c.peer instanceof LightweightPeer);
  6591. c = c.parent) {
  6592. nativeX += c.x;
  6593. nativeY += c.y;
  6594. }
  6595. if (peer != null) {
  6596. peer.setBounds(nativeX, nativeY, width, height);
  6597. }
  6598. }
  6599. }
  6600. /**
  6601. * Invoked when a lightweight parent component has been
  6602. * shown. The associated native component must also be
  6603. * shown if it hasn't had an overriding hide done on it.
  6604. */
  6605. public void componentShown(ComponentEvent e) {
  6606. if (isShowing()) {
  6607. synchronized (getTreeLock()) {
  6608. if (peer != null) {
  6609. peer.show();
  6610. }
  6611. }
  6612. }
  6613. }
  6614. /**
  6615. * Invoked when component has been hidden.
  6616. */
  6617. public void componentHidden(ComponentEvent e) {
  6618. if (visible) {
  6619. synchronized (getTreeLock()) {
  6620. if (peer != null) {
  6621. peer.hide();
  6622. }
  6623. }
  6624. }
  6625. }
  6626. // --- ContainerListener ------------------------------------
  6627. /**
  6628. * Invoked when a component has been added to a lightweight
  6629. * parent. This doesn't effect the native component.
  6630. */
  6631. public void componentAdded(ContainerEvent e) {
  6632. }
  6633. /**
  6634. * Invoked when a lightweight parent has been removed.
  6635. * This means the services of this listener are no longer
  6636. * required and it should remove all references (ie
  6637. * registered listeners).
  6638. */
  6639. public void componentRemoved(ContainerEvent e) {
  6640. Component c = e.getChild();
  6641. if (c == Component.this) {
  6642. removeReferences();
  6643. } else {
  6644. int n = lightParents.size();
  6645. for (int i = 0; i < n; i++) {
  6646. Container p = (Container) lightParents.elementAt(i);
  6647. if (p == c) {
  6648. removeReferences();
  6649. break;
  6650. }
  6651. }
  6652. }
  6653. }
  6654. /**
  6655. * Removes references to this object so it can be
  6656. * garbage collected.
  6657. */
  6658. void removeReferences() {
  6659. int n = lightParents.size();
  6660. for (int i = 0; i < n; i++) {
  6661. Container c = (Container) lightParents.elementAt(i);
  6662. c.removeComponentListener(this);
  6663. c.removeContainerListener(this);
  6664. }
  6665. nativeHost.removeContainerListener(this);
  6666. }
  6667. Vector lightParents;
  6668. Container nativeHost;
  6669. }
  6670. /**
  6671. * Initialize JNI field and method IDs
  6672. */
  6673. private static native void initIDs();
  6674. /*
  6675. * --- Accessibility Support ---
  6676. *
  6677. * Component will contain all of the methods in interface Accessible,
  6678. * though it won't actually implement the interface - that will be up
  6679. * to the individual objects which extend Component.
  6680. */
  6681. AccessibleContext accessibleContext = null;
  6682. /**
  6683. * Gets the <code>AccessibleContext</code> associated
  6684. * with this <code>Component</code>.
  6685. * The method implemented by this base
  6686. * class returns null. Classes that extend <code>Component</code>
  6687. * should implement this method to return the
  6688. * <code>AccessibleContext</code> associated with the subclass.
  6689. *
  6690. *
  6691. * @return the <code>AccessibleContext</code> of this
  6692. * <code>Component</code>
  6693. */
  6694. public AccessibleContext getAccessibleContext() {
  6695. return accessibleContext;
  6696. }
  6697. /**
  6698. * Inner class of Component used to provide default support for
  6699. * accessibility. This class is not meant to be used directly by
  6700. * application developers, but is instead meant only to be
  6701. * subclassed by component developers.
  6702. * <p>
  6703. * The class used to obtain the accessible role for this object.
  6704. */
  6705. protected abstract class AccessibleAWTComponent extends AccessibleContext
  6706. implements Serializable, AccessibleComponent {
  6707. /**
  6708. * Though the class is abstract, this should be called by
  6709. * all sub-classes.
  6710. */
  6711. protected AccessibleAWTComponent() {
  6712. }
  6713. protected ComponentListener accessibleAWTComponentHandler = null;
  6714. protected FocusListener accessibleAWTFocusHandler = null;
  6715. /**
  6716. * Fire PropertyChange listener, if one is registered,
  6717. * when shown/hidden..
  6718. */
  6719. protected class AccessibleAWTComponentHandler implements ComponentListener {
  6720. public void componentHidden(ComponentEvent e) {
  6721. if (accessibleContext != null) {
  6722. accessibleContext.firePropertyChange(
  6723. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  6724. AccessibleState.VISIBLE, null);
  6725. }
  6726. }
  6727. public void componentShown(ComponentEvent e) {
  6728. if (accessibleContext != null) {
  6729. accessibleContext.firePropertyChange(
  6730. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  6731. null, AccessibleState.VISIBLE);
  6732. }
  6733. }
  6734. public void componentMoved(ComponentEvent e) {
  6735. }
  6736. public void componentResized(ComponentEvent e) {
  6737. }
  6738. } // inner class AccessibleAWTComponentHandler
  6739. /**
  6740. * Fire PropertyChange listener, if one is registered,
  6741. * when focus events happen
  6742. */
  6743. protected class AccessibleAWTFocusHandler implements FocusListener {
  6744. public void focusGained(FocusEvent event) {
  6745. if (accessibleContext != null) {
  6746. accessibleContext.firePropertyChange(
  6747. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  6748. null, AccessibleState.FOCUSED);
  6749. }
  6750. }
  6751. public void focusLost(FocusEvent event) {
  6752. if (accessibleContext != null) {
  6753. accessibleContext.firePropertyChange(
  6754. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  6755. AccessibleState.FOCUSED, null);
  6756. }
  6757. }
  6758. } // inner class AccessibleAWTFocusHandler
  6759. /**
  6760. * Add a PropertyChangeListener to the listener list.
  6761. *
  6762. * @param listener The PropertyChangeListener to be added
  6763. */
  6764. public void addPropertyChangeListener(PropertyChangeListener listener) {
  6765. if (accessibleAWTComponentHandler == null) {
  6766. accessibleAWTComponentHandler = new AccessibleAWTComponentHandler();
  6767. Component.this.addComponentListener(accessibleAWTComponentHandler);
  6768. }
  6769. if (accessibleAWTFocusHandler == null) {
  6770. accessibleAWTFocusHandler = new AccessibleAWTFocusHandler();
  6771. Component.this.addFocusListener(accessibleAWTFocusHandler);
  6772. }
  6773. super.addPropertyChangeListener(listener);
  6774. }
  6775. /**
  6776. * Remove a PropertyChangeListener from the listener list.
  6777. * This removes a PropertyChangeListener that was registered
  6778. * for all properties.
  6779. *
  6780. * @param listener The PropertyChangeListener to be removed
  6781. */
  6782. public void removePropertyChangeListener(PropertyChangeListener listener) {
  6783. if (accessibleAWTComponentHandler != null) {
  6784. Component.this.removeComponentListener(accessibleAWTComponentHandler);
  6785. accessibleAWTComponentHandler = null;
  6786. }
  6787. if (accessibleAWTFocusHandler != null) {
  6788. Component.this.removeFocusListener(accessibleAWTFocusHandler);
  6789. accessibleAWTFocusHandler = null;
  6790. }
  6791. super.removePropertyChangeListener(listener);
  6792. }
  6793. // AccessibleContext methods
  6794. //
  6795. /**
  6796. * Gets the accessible name of this object. This should almost never
  6797. * return <code>java.awt.Component.getName()</code>,
  6798. * as that generally isn't a localized name,
  6799. * and doesn't have meaning for the user. If the
  6800. * object is fundamentally a text object (e.g. a menu item), the
  6801. * accessible name should be the text of the object (e.g. "save").
  6802. * If the object has a tooltip, the tooltip text may also be an
  6803. * appropriate String to return.
  6804. *
  6805. * @return the localized name of the object -- can be
  6806. * <code>null</code> if this
  6807. * object does not have a name
  6808. * @see javax.accessibility.AccessibleContext#setAccessibleName
  6809. */
  6810. public String getAccessibleName() {
  6811. return accessibleName;
  6812. }
  6813. /**
  6814. * Gets the accessible description of this object. This should be
  6815. * a concise, localized description of what this object is - what
  6816. * is its meaning to the user. If the object has a tooltip, the
  6817. * tooltip text may be an appropriate string to return, assuming
  6818. * it contains a concise description of the object (instead of just
  6819. * the name of the object - e.g. a "Save" icon on a toolbar that
  6820. * had "save" as the tooltip text shouldn't return the tooltip
  6821. * text as the description, but something like "Saves the current
  6822. * text document" instead).
  6823. *
  6824. * @return the localized description of the object -- can be
  6825. * <code>null</code> if this object does not have a description
  6826. * @see javax.accessibility.AccessibleContext#setAccessibleDescription
  6827. */
  6828. public String getAccessibleDescription() {
  6829. return accessibleDescription;
  6830. }
  6831. /**
  6832. * Gets the role of this object.
  6833. *
  6834. * @return an instance of <code>AccessibleRole</code>
  6835. * describing the role of the object
  6836. * @see javax.accessibility.AccessibleRole
  6837. */
  6838. public AccessibleRole getAccessibleRole() {
  6839. return AccessibleRole.AWT_COMPONENT;
  6840. }
  6841. /**
  6842. * Gets the state of this object.
  6843. *
  6844. * @return an instance of <code>AccessibleStateSet</code>
  6845. * containing the current state set of the object
  6846. * @see javax.accessibility.AccessibleState
  6847. */
  6848. public AccessibleStateSet getAccessibleStateSet() {
  6849. return Component.this.getAccessibleStateSet();
  6850. }
  6851. /**
  6852. * Gets the <code>Accessible</code> parent of this object.
  6853. * If the parent of this object implements <code>Accessible</code>,
  6854. * this method should simply return <code>getParent</code>.
  6855. *
  6856. * @return the <code>Accessible</code> parent of this
  6857. * object -- can be <code>null</code> if this
  6858. * object does not have an <code>Accessible</code> parent
  6859. */
  6860. public Accessible getAccessibleParent() {
  6861. if (accessibleParent != null) {
  6862. return accessibleParent;
  6863. } else {
  6864. Container parent = getParent();
  6865. if (parent instanceof Accessible) {
  6866. return (Accessible) parent;
  6867. }
  6868. }
  6869. return null;
  6870. }
  6871. /**
  6872. * Gets the index of this object in its accessible parent.
  6873. *
  6874. * @return the index of this object in its parent; or -1 if this
  6875. * object does not have an accessible parent
  6876. * @see #getAccessibleParent
  6877. */
  6878. public int getAccessibleIndexInParent() {
  6879. return Component.this.getAccessibleIndexInParent();
  6880. }
  6881. /**
  6882. * Returns the number of accessible children in the object. If all
  6883. * of the children of this object implement <code>Accessible</code>,
  6884. * then this method should return the number of children of this object.
  6885. *
  6886. * @return the number of accessible children in the object
  6887. */
  6888. public int getAccessibleChildrenCount() {
  6889. return 0; // Components don't have children
  6890. }
  6891. /**
  6892. * Returns the nth <code>Accessible</code> child of the object.
  6893. *
  6894. * @param i zero-based index of child
  6895. * @return the nth <code>Accessible</code> child of the object
  6896. */
  6897. public Accessible getAccessibleChild(int i) {
  6898. return null; // Components don't have children
  6899. }
  6900. /**
  6901. * Returns the locale of this object.
  6902. *
  6903. * @return the locale of this object
  6904. */
  6905. public Locale getLocale() {
  6906. return Component.this.getLocale();
  6907. }
  6908. /**
  6909. * Gets the <code>AccessibleComponent</code> associated
  6910. * with this object if one exists.
  6911. * Otherwise return <code>null</code>.
  6912. *
  6913. * @return the component
  6914. */
  6915. public AccessibleComponent getAccessibleComponent() {
  6916. return this;
  6917. }
  6918. // AccessibleComponent methods
  6919. //
  6920. /**
  6921. * Gets the background color of this object.
  6922. *
  6923. * @return the background color, if supported, of the object;
  6924. * otherwise, <code>null</code>
  6925. */
  6926. public Color getBackground() {
  6927. return Component.this.getBackground();
  6928. }
  6929. /**
  6930. * Sets the background color of this object.
  6931. * (For transparency, see <code>isOpaque</code>.)
  6932. *
  6933. * @param c the new <code>Color</code> for the background
  6934. * @see Component#isOpaque
  6935. */
  6936. public void setBackground(Color c) {
  6937. Component.this.setBackground(c);
  6938. }
  6939. /**
  6940. * Gets the foreground color of this object.
  6941. *
  6942. * @return the foreground color, if supported, of the object;
  6943. * otherwise, <code>null</code>
  6944. */
  6945. public Color getForeground() {
  6946. return Component.this.getForeground();
  6947. }
  6948. /**
  6949. * Sets the foreground color of this object.
  6950. *
  6951. * @param c the new <code>Color</code> for the foreground
  6952. */
  6953. public void setForeground(Color c) {
  6954. Component.this.setForeground(c);
  6955. }
  6956. /**
  6957. * Gets the <code>Cursor</code> of this object.
  6958. *
  6959. * @return the <code>Cursor</code>, if supported,
  6960. * of the object; otherwise, <code>null</code>
  6961. */
  6962. public Cursor getCursor() {
  6963. return Component.this.getCursor();
  6964. }
  6965. /**
  6966. * Sets the <code>Cursor</code> of this object.
  6967. *
  6968. * @param cursor the new <code>Cursor</code> for the object
  6969. */
  6970. public void setCursor(Cursor cursor) {
  6971. Component.this.setCursor(cursor);
  6972. }
  6973. /**
  6974. * Gets the <code>Font</code> of this object.
  6975. *
  6976. * @return the <code>Font</code>, if supported,
  6977. * for the object; otherwise, <code>null</code>
  6978. */
  6979. public Font getFont() {
  6980. return Component.this.getFont();
  6981. }
  6982. /**
  6983. * Sets the <code>Font</code> of this object.
  6984. *
  6985. * @param f the new <code>Font</code> for the object
  6986. */
  6987. public void setFont(Font f) {
  6988. Component.this.setFont(f);
  6989. }
  6990. /**
  6991. * Gets the <code>FontMetrics</code> of this object.
  6992. *
  6993. * @param f the <code>Font</code>
  6994. * @return the <code>FontMetrics</code>, if supported,
  6995. * the object; otherwise, <code>null</code>
  6996. * @see #getFont
  6997. */
  6998. public FontMetrics getFontMetrics(Font f) {
  6999. if (f == null) {
  7000. return null;
  7001. } else {
  7002. return Component.this.getFontMetrics(f);
  7003. }
  7004. }
  7005. /**
  7006. * Determines if the object is enabled.
  7007. *
  7008. * @return true if object is enabled; otherwise, false
  7009. */
  7010. public boolean isEnabled() {
  7011. return Component.this.isEnabled();
  7012. }
  7013. /**
  7014. * Sets the enabled state of the object.
  7015. *
  7016. * @param b if true, enables this object; otherwise, disables it
  7017. */
  7018. public void setEnabled(boolean b) {
  7019. boolean old = Component.this.isEnabled();
  7020. Component.this.setEnabled(b);
  7021. if (b != old) {
  7022. if (accessibleContext != null) {
  7023. if (b) {
  7024. accessibleContext.firePropertyChange(
  7025. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  7026. null, AccessibleState.ENABLED);
  7027. } else {
  7028. accessibleContext.firePropertyChange(
  7029. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  7030. AccessibleState.ENABLED, null);
  7031. }
  7032. }
  7033. }
  7034. }
  7035. /**
  7036. * Determines if the object is visible. Note: this means that the
  7037. * object intends to be visible; however, it may not in fact be
  7038. * showing on the screen because one of the objects that this object
  7039. * is contained by is not visible. To determine if an object is
  7040. * showing on the screen, use <code>isShowing</code>.
  7041. *
  7042. * @return true if object is visible; otherwise, false
  7043. */
  7044. public boolean isVisible() {
  7045. return Component.this.isVisible();
  7046. }
  7047. /**
  7048. * Sets the visible state of the object.
  7049. *
  7050. * @param b if true, shows this object; otherwise, hides it
  7051. */
  7052. public void setVisible(boolean b) {
  7053. boolean old = Component.this.isVisible();
  7054. Component.this.setVisible(b);
  7055. if (b != old) {
  7056. if (accessibleContext != null) {
  7057. if (b) {
  7058. accessibleContext.firePropertyChange(
  7059. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  7060. null, AccessibleState.VISIBLE);
  7061. } else {
  7062. accessibleContext.firePropertyChange(
  7063. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  7064. AccessibleState.VISIBLE, null);
  7065. }
  7066. }
  7067. }
  7068. }
  7069. /**
  7070. * Determines if the object is showing. This is determined by checking
  7071. * the visibility of the object and ancestors of the object. Note:
  7072. * this will return true even if the object is obscured by another
  7073. * (for example, it happens to be underneath a menu that was pulled
  7074. * down).
  7075. *
  7076. * @return true if object is showing; otherwise, false
  7077. */
  7078. public boolean isShowing() {
  7079. return Component.this.isShowing();
  7080. }
  7081. /**
  7082. * Checks whether the specified point is within this object's bounds,
  7083. * where the point's x and y coordinates are defined to be relative to
  7084. * the coordinate system of the object.
  7085. *
  7086. * @param p the <code>Point</code> relative to the
  7087. * coordinate system of the object
  7088. * @return true if object contains <code>Point</code> otherwise false
  7089. */
  7090. public boolean contains(Point p) {
  7091. return Component.this.contains(p);
  7092. }
  7093. /**
  7094. * Returns the location of the object on the screen.
  7095. *
  7096. * @return location of object on screen -- can be
  7097. * <code>null</code> if this object is not on the screen
  7098. */
  7099. public Point getLocationOnScreen() {
  7100. synchronized (Component.this.getTreeLock()) {
  7101. if (Component.this.isShowing()) {
  7102. return Component.this.getLocationOnScreen();
  7103. } else {
  7104. return null;
  7105. }
  7106. }
  7107. }
  7108. /**
  7109. * Gets the location of the object relative to the parent in the form
  7110. * of a point specifying the object's top-left corner in the screen's
  7111. * coordinate space.
  7112. *
  7113. * @return an instance of Point representing the top-left corner of
  7114. * the object's bounds in the coordinate space of the screen;
  7115. * <code>null</code> if this object or its parent are not on the screen
  7116. */
  7117. public Point getLocation() {
  7118. return Component.this.getLocation();
  7119. }
  7120. /**
  7121. * Sets the location of the object relative to the parent.
  7122. * @param p the coordinates of the object
  7123. */
  7124. public void setLocation(Point p) {
  7125. Component.this.setLocation(p);
  7126. }
  7127. /**
  7128. * Gets the bounds of this object in the form of a Rectangle object.
  7129. * The bounds specify this object's width, height, and location
  7130. * relative to its parent.
  7131. *
  7132. * @return a rectangle indicating this component's bounds;
  7133. * <code>null</code> if this object is not on the screen
  7134. */
  7135. public Rectangle getBounds() {
  7136. return Component.this.getBounds();
  7137. }
  7138. /**
  7139. * Sets the bounds of this object in the form of a
  7140. * <code>Rectangle</code> object.
  7141. * The bounds specify this object's width, height, and location
  7142. * relative to its parent.
  7143. *
  7144. * @param r a rectangle indicating this component's bounds
  7145. */
  7146. public void setBounds(Rectangle r) {
  7147. Component.this.setBounds(r);
  7148. }
  7149. /**
  7150. * Returns the size of this object in the form of a
  7151. * <code>Dimension</code> object. The height field of the
  7152. * <code>Dimension</code> object contains this objects's
  7153. * height, and the width field of the <code>Dimension</code>
  7154. * object contains this object's width.
  7155. *
  7156. * @return a <code>Dimension</code> object that indicates
  7157. * the size of this component; <code>null</code> if
  7158. * this object is not on the screen
  7159. */
  7160. public Dimension getSize() {
  7161. return Component.this.getSize();
  7162. }
  7163. /**
  7164. * Resizes this object so that it has width width and height.
  7165. *
  7166. * @param d - the dimension specifying the new size of the object
  7167. */
  7168. public void setSize(Dimension d) {
  7169. Component.this.setSize(d);
  7170. }
  7171. /**
  7172. * Returns the <code>Accessible</code> child,
  7173. * if one exists, contained at the local
  7174. * coordinate <code>Point</code>. Otherwise returns
  7175. * <code>null</code>.
  7176. *
  7177. * @param p the point defining the top-left corner of
  7178. * the <code>Accessible</code>, given in the
  7179. * coordinate space of the object's parent
  7180. * @return the <code>Accessible</code>, if it exists,
  7181. * at the specified location; else <code>null</code>
  7182. */
  7183. public Accessible getAccessibleAt(Point p) {
  7184. return null; // Components don't have children
  7185. }
  7186. /**
  7187. * Returns whether this object can accept focus or not.
  7188. *
  7189. * @return true if object can accept focus; otherwise false
  7190. */
  7191. public boolean isFocusTraversable() {
  7192. return Component.this.isFocusTraversable();
  7193. }
  7194. /**
  7195. * Requests focus for this object.
  7196. */
  7197. public void requestFocus() {
  7198. Component.this.requestFocus();
  7199. }
  7200. /**
  7201. * Adds the specified focus listener to receive focus events from this
  7202. * component.
  7203. *
  7204. * @param l the focus listener
  7205. */
  7206. public void addFocusListener(FocusListener l) {
  7207. Component.this.addFocusListener(l);
  7208. }
  7209. /**
  7210. * Removes the specified focus listener so it no longer receives focus
  7211. * events from this component.
  7212. *
  7213. * @param l the focus listener
  7214. */
  7215. public void removeFocusListener(FocusListener l) {
  7216. Component.this.removeFocusListener(l);
  7217. }
  7218. } // inner class AccessibleAWTComponent
  7219. /**
  7220. * Gets the index of this object in its accessible parent.
  7221. * If this object does not have an accessible parent, returns
  7222. * -1.
  7223. *
  7224. * @return the index of this object in its accessible parent
  7225. */
  7226. int getAccessibleIndexInParent() {
  7227. synchronized (getTreeLock()) {
  7228. int index = -1;
  7229. Container parent = this.getParent();
  7230. if (parent != null && parent instanceof Accessible) {
  7231. Component ca[] = parent.getComponents();
  7232. for (int i = 0; i < ca.length; i++) {
  7233. if (ca[i] instanceof Accessible) {
  7234. index++;
  7235. }
  7236. if (this.equals(ca[i])) {
  7237. return index;
  7238. }
  7239. }
  7240. }
  7241. return -1;
  7242. }
  7243. }
  7244. /**
  7245. * Gets the current state set of this object.
  7246. *
  7247. * @return an instance of <code>AccessibleStateSet</code>
  7248. * containing the current state set of the object
  7249. * @see AccessibleState
  7250. */
  7251. AccessibleStateSet getAccessibleStateSet() {
  7252. synchronized (getTreeLock()) {
  7253. AccessibleStateSet states = new AccessibleStateSet();
  7254. if (this.isEnabled()) {
  7255. states.add(AccessibleState.ENABLED);
  7256. }
  7257. if (this.isFocusTraversable()) {
  7258. states.add(AccessibleState.FOCUSABLE);
  7259. }
  7260. if (this.isVisible()) {
  7261. states.add(AccessibleState.VISIBLE);
  7262. }
  7263. if (this.isShowing()) {
  7264. states.add(AccessibleState.SHOWING);
  7265. }
  7266. if (this.isFocusOwner()) {
  7267. states.add(AccessibleState.FOCUSED);
  7268. }
  7269. if (this instanceof Accessible) {
  7270. AccessibleContext ac = ((Accessible) this).getAccessibleContext();
  7271. if (ac != null) {
  7272. Accessible ap = ac.getAccessibleParent();
  7273. if (ap != null) {
  7274. AccessibleContext pac = ap.getAccessibleContext();
  7275. if (pac != null) {
  7276. AccessibleSelection as = pac.getAccessibleSelection();
  7277. if (as != null) {
  7278. states.add(AccessibleState.SELECTABLE);
  7279. int i = ac.getAccessibleIndexInParent();
  7280. if (i >= 0) {
  7281. if (as.isAccessibleChildSelected(i)) {
  7282. states.add(AccessibleState.SELECTED);
  7283. }
  7284. }
  7285. }
  7286. }
  7287. }
  7288. }
  7289. }
  7290. if (this instanceof javax.swing.JComponent) {
  7291. if (((javax.swing.JComponent) this).isOpaque()) {
  7292. states.add(AccessibleState.OPAQUE);
  7293. }
  7294. }
  7295. return states;
  7296. }
  7297. }
  7298. }