1. /*
  2. * @(#)Component.java 1.265 00/02/28
  3. *
  4. * Copyright 1995-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.awt;
  11. import java.io.PrintStream;
  12. import java.io.PrintWriter;
  13. import java.util.Vector;
  14. import java.util.Locale;
  15. import java.util.EventListener;
  16. import java.awt.peer.ComponentPeer;
  17. import java.awt.image.ImageObserver;
  18. import java.awt.image.ImageProducer;
  19. import java.awt.image.ColorModel;
  20. import java.awt.event.*;
  21. import java.awt.datatransfer.Transferable;
  22. import java.awt.dnd.DnDConstants;
  23. import java.awt.dnd.DragSource;
  24. import java.awt.dnd.DragSourceContext;
  25. import java.awt.dnd.DragSourceListener;
  26. import java.awt.dnd.InvalidDnDOperationException;
  27. import java.io.Serializable;
  28. import java.io.ObjectOutputStream;
  29. import java.io.ObjectInputStream;
  30. import java.io.IOException;
  31. import java.beans.PropertyChangeListener;
  32. import java.awt.event.InputMethodListener;
  33. import java.awt.event.InputMethodEvent;
  34. import java.awt.im.InputContext;
  35. import java.awt.im.InputMethodRequests;
  36. import java.awt.dnd.DropTarget;
  37. import javax.accessibility.*;
  38. import java.awt.GraphicsConfiguration;
  39. import javax.accessibility.*;
  40. import sun.security.action.GetPropertyAction;
  41. import sun.awt.AppContext;
  42. import sun.awt.SunToolkit;
  43. import sun.awt.ConstrainableGraphics;
  44. import sun.awt.DebugHelper;
  45. import sun.awt.WindowClosingListener;
  46. import sun.awt.WindowClosingSupport;
  47. import sun.awt.GlobalCursorManager;
  48. import sun.awt.im.CompositionArea;
  49. /**
  50. * A <em>component</em> is an object having a graphical representation
  51. * that can be displayed on the screen and that can interact with the
  52. * user. Examples of components are the buttons, checkboxes, and scrollbars
  53. * of a typical graphical user interface. <p>
  54. * The <code>Component</code> class is the abstract superclass of
  55. * the nonmenu-related Abstract Window Toolkit components. Class
  56. * <code>Component</code> can also be extended directly to create a
  57. * lightweight component. A lightweight component is a component that is
  58. * not associated with a native opaque window.
  59. *
  60. * @version 1.265, 02/28/00
  61. * @author Arthur van Hoff
  62. * @author Sami Shaio
  63. */
  64. public abstract class Component implements ImageObserver, MenuContainer,
  65. Serializable
  66. {
  67. /**
  68. * The peer of the component. The peer implements the component's
  69. * behaviour. The peer is set when the Component is added to a
  70. * container that also is a peer.
  71. * @see #addNotify
  72. * @see #removeNotify
  73. */
  74. transient ComponentPeer peer;
  75. /**
  76. * The parent of the object. It may be null for top-level components.
  77. * @see #getParent
  78. */
  79. transient Container parent;
  80. /**
  81. * The AppContext of the component. This is set in the constructor
  82. * and never changes.
  83. */
  84. transient AppContext appContext;
  85. /**
  86. * The x position of the component in the parent's coordinate system.
  87. *
  88. * @serial
  89. * @see #getLocation
  90. */
  91. int x;
  92. /**
  93. * The y position of the component in the parent's coordinate system.
  94. *
  95. * @serial
  96. * @see #getLocation
  97. */
  98. int y;
  99. /**
  100. * The width of the component.
  101. *
  102. * @serial
  103. * @see #getSize
  104. */
  105. int width;
  106. /**
  107. * The height of the component.
  108. *
  109. * @serial
  110. * @see #getSize
  111. */
  112. int height;
  113. /**
  114. * The foreground color for this component.
  115. * foreground color can be null.
  116. *
  117. * @serial
  118. * @see #getForeground
  119. * @see #setForeground
  120. */
  121. Color foreground;
  122. /**
  123. * The background color for this component.
  124. * background can be null.
  125. *
  126. * @serial
  127. * @see #getBackground
  128. * @see #setBackground
  129. */
  130. Color background;
  131. /**
  132. * The font used by this component.
  133. * The font can be null.
  134. *
  135. * @serial
  136. * @see #getFont
  137. * @see #setFont
  138. */
  139. Font font;
  140. /**
  141. * The font which the peer is currently using. (null if no peer exists.)
  142. */
  143. Font peerFont;
  144. /**
  145. * The cursor displayed when pointer is over this component.
  146. * This value can be null.
  147. *
  148. * @serial
  149. * @see #getCursor
  150. * @see #setCursor
  151. */
  152. Cursor cursor;
  153. /**
  154. * The locale for the component.
  155. *
  156. * @serial
  157. * @see #getLocale
  158. * @see #setLocale
  159. */
  160. Locale locale;
  161. /**
  162. * A reference to a GraphicsConfiguration object
  163. * used to describe the characteristics of a graphics
  164. * destination.
  165. * This value can be null.
  166. *
  167. * @since 1.3
  168. * @serial
  169. * @see java.awt.GraphicsConfiguration
  170. * @see #getGraphicsConfiguration
  171. */
  172. transient GraphicsConfiguration graphicsConfig = null;
  173. /**
  174. * True when the object is visible. An object that is not
  175. * visible is not drawn on the screen.
  176. *
  177. * @serial
  178. * @see #isVisible
  179. * @see #setVisible
  180. */
  181. boolean visible = true;
  182. /**
  183. * True when the object is enabled. An object that is not
  184. * enabled does not interact with the user.
  185. *
  186. * @serial
  187. * @see #isEnabled
  188. * @see #setEnabled
  189. */
  190. boolean enabled = true;
  191. /**
  192. * True when the object is valid. An invalid object needs to
  193. * be layed out. This flag is set to false when the object
  194. * size is changed.
  195. *
  196. * @serial
  197. * @see #isValid
  198. * @see #validate
  199. * @see #invalidate
  200. */
  201. boolean valid = false;
  202. /**
  203. * The DropTarget associated with this Component.
  204. *
  205. * @since 1.2
  206. * @serial
  207. * @see #setDropTarget
  208. * @see #getDropTarget
  209. */
  210. DropTarget dropTarget;
  211. /**
  212. * True if this component has enabled focus events and currently
  213. * has the focus.
  214. *
  215. * @serial
  216. * @see #hasFocus
  217. * @see #processFocusEvent
  218. */
  219. boolean hasFocus = false;
  220. /**
  221. * @serial
  222. * @see add()
  223. */
  224. Vector popups;
  225. /**
  226. * A components name.
  227. * This field can be null.
  228. *
  229. * @serial
  230. * @see getName()
  231. * @see setName(String)
  232. */
  233. private String name;
  234. /**
  235. * A bool to determine whether the name has
  236. * been set explicitly. nameExplicitlySet will
  237. * be false if the name has not been set and
  238. * true if it has.
  239. *
  240. * @serial
  241. * @see getName()
  242. * @see setName(String)
  243. */
  244. private boolean nameExplicitlySet = false;
  245. /**
  246. * The locking object for AWT component-tree and layout operations.
  247. *
  248. * @see #getTreeLock
  249. */
  250. static final Object LOCK = new AWTTreeLock();
  251. static class AWTTreeLock {}
  252. /**
  253. * Internal, cached size information.
  254. * (This field perhaps should have been transient).
  255. *
  256. * @serial
  257. */
  258. Dimension minSize;
  259. /** Internal, cached size information
  260. * (This field perhaps should have been transient).
  261. *
  262. * @serial
  263. */
  264. Dimension prefSize;
  265. /**
  266. * The orientation for this component.
  267. * @see #getComponentOrientation
  268. * @see #setComponentOrientation
  269. */
  270. transient ComponentOrientation componentOrientation
  271. = ComponentOrientation.UNKNOWN;
  272. /**
  273. * newEventsOnly will be true if the event is
  274. * one of the event types enabled for the component.
  275. * It will then allow for normal processing to
  276. * continue. If it is false the event is passed
  277. * to the components parent and up the ancestor
  278. * tree until the event has been consumed.
  279. *
  280. * @serial
  281. * @see dispatchEvent()
  282. */
  283. boolean newEventsOnly = false;
  284. transient ComponentListener componentListener;
  285. transient FocusListener focusListener;
  286. transient HierarchyListener hierarchyListener;
  287. transient HierarchyBoundsListener hierarchyBoundsListener;
  288. transient KeyListener keyListener;
  289. transient MouseListener mouseListener;
  290. transient MouseMotionListener mouseMotionListener;
  291. transient InputMethodListener inputMethodListener;
  292. transient RuntimeException windowClosingException = null;
  293. /** Internal, constants for serialization */
  294. final static String actionListenerK = "actionL";
  295. final static String adjustmentListenerK = "adjustmentL";
  296. final static String componentListenerK = "componentL";
  297. final static String containerListenerK = "containerL";
  298. final static String focusListenerK = "focusL";
  299. final static String itemListenerK = "itemL";
  300. final static String keyListenerK = "keyL";
  301. final static String mouseListenerK = "mouseL";
  302. final static String mouseMotionListenerK = "mouseMotionL";
  303. final static String textListenerK = "textL";
  304. final static String ownedWindowK = "ownedL";
  305. final static String windowListenerK = "windowL";
  306. final static String inputMethodListenerK = "inputMethodL";
  307. final static String hierarchyListenerK = "hierarchyL";
  308. final static String hierarchyBoundsListenerK = "hierarchyBoundsL";
  309. /**
  310. * The eventMask is ONLY set by subclasses via enableEvents.
  311. * The mask should NOT be set when listeners are registered
  312. * so that we can distinguish the difference between when
  313. * listeners request events and subclasses request them.
  314. * One bit is used to indicate whether input methods are
  315. * enabled; this bit is set by enableInputMethods and is
  316. * on by default.
  317. *
  318. * @serial
  319. * @see enableInputMethods()
  320. */
  321. long eventMask = AWTEvent.INPUT_METHODS_ENABLED_MASK;
  322. private static final DebugHelper dbg = DebugHelper.create(Component.class);
  323. /**
  324. * Static properties for incremental drawing.
  325. * @see #imageUpdate
  326. */
  327. static boolean isInc;
  328. static int incRate;
  329. static {
  330. /* ensure that the necessary native libraries are loaded */
  331. Toolkit.loadLibraries();
  332. /* initialize JNI field and method ids */
  333. initIDs();
  334. String s = (String) java.security.AccessController.doPrivileged(
  335. new GetPropertyAction("awt.image.incrementaldraw"));
  336. isInc = (s == null || s.equals("true"));
  337. s = (String) java.security.AccessController.doPrivileged(
  338. new GetPropertyAction("awt.image.redrawrate"));
  339. incRate = (s != null) ? Integer.parseInt(s) : 100;
  340. }
  341. /**
  342. * Ease-of-use constant for <code>getAlignmentY()</code>. Specifies an
  343. * alignment to the top of the component.
  344. * @see #getAlignmentY
  345. */
  346. public static final float TOP_ALIGNMENT = 0.0f;
  347. /**
  348. * Ease-of-use constant for <code>getAlignmentY</code> and
  349. * <code>getAlignmentX</code>. Specifies an alignment to
  350. * the center of the component
  351. * @see #getAlignmentX
  352. * @see #getAlignmentY
  353. */
  354. public static final float CENTER_ALIGNMENT = 0.5f;
  355. /**
  356. * Ease-of-use constant for <code>getAlignmentY</code>. Specifies an
  357. * alignment to the bottom of the component.
  358. * @see #getAlignmentY
  359. */
  360. public static final float BOTTOM_ALIGNMENT = 1.0f;
  361. /**
  362. * Ease-of-use constant for <code>getAlignmentX</code>. Specifies an
  363. * alignment to the left side of the component.
  364. * @see #getAlignmentX
  365. */
  366. public static final float LEFT_ALIGNMENT = 0.0f;
  367. /**
  368. * Ease-of-use constant for <code>getAlignmentX</code>. Specifies an
  369. * alignment to the right side of the component.
  370. * @see #getAlignmentX
  371. */
  372. public static final float RIGHT_ALIGNMENT = 1.0f;
  373. /*
  374. * JDK 1.1 serialVersionUID
  375. */
  376. private static final long serialVersionUID = -7644114512714619750L;
  377. /**
  378. * If any PropertyChangeListeners have been registered, the
  379. * changeSupport field describes them.
  380. *
  381. * @serial
  382. * @since 1.2
  383. * @see addPropertyChangeListener
  384. * @see removePropertyChangeListener
  385. * @see firePropertyChange
  386. */
  387. private java.beans.PropertyChangeSupport changeSupport;
  388. boolean isPacked = false;
  389. /**
  390. * This object is used as a key for internal hashtables.
  391. */
  392. transient private Object privateKey = new Object();
  393. /**
  394. * Constructs a new component. Class <code>Component</code> can be
  395. * extended directly to create a lightweight component that does not
  396. * utilize an opaque native window. A lightweight component must be
  397. * hosted by a native container somewhere higher up in the component
  398. * tree (for example, by a <code>Frame</code> object).
  399. */
  400. protected Component() {
  401. appContext = AppContext.getAppContext();
  402. SunToolkit.insertTargetMapping(this, appContext);
  403. }
  404. /**
  405. * Construct a name for this component. Called by getName() when the
  406. * name is null.
  407. */
  408. String constructComponentName() {
  409. return null; // For strict compliance with prior platform versions, a Component
  410. // that doesn't set its name should return null from
  411. // getName()
  412. }
  413. /**
  414. * Gets the name of the component.
  415. * @return This component's name.
  416. * @see #setName
  417. * @since JDK1.1
  418. */
  419. public String getName() {
  420. if (name == null && !nameExplicitlySet) {
  421. synchronized(this) {
  422. if (name == null && !nameExplicitlySet)
  423. name = constructComponentName();
  424. }
  425. }
  426. return name;
  427. }
  428. /**
  429. * Sets the name of the component to the specified string.
  430. * @param name The string that is to be this
  431. * component's name.
  432. * @see #getName
  433. * @since JDK1.1
  434. */
  435. public void setName(String name) {
  436. synchronized(this) {
  437. this.name = name;
  438. nameExplicitlySet = true;
  439. }
  440. }
  441. /**
  442. * Gets the parent of this component.
  443. * @return The parent container of this component.
  444. * @since JDK1.0
  445. */
  446. public Container getParent() {
  447. return getParent_NoClientCode();
  448. }
  449. // NOTE: This method may be called by privileged threads.
  450. // This functionality is implemented in a package-private method
  451. // to insure that it cannot be overridden by client subclasses.
  452. // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  453. final Container getParent_NoClientCode() {
  454. return parent;
  455. }
  456. /**
  457. * @deprecated As of JDK version 1.1,
  458. * programs should not directly manipulate peers.
  459. * replaced by <code>boolean isDisplayable()</code>.
  460. */
  461. public ComponentPeer getPeer() {
  462. return peer;
  463. }
  464. /**
  465. * Associate a DropTarget with this Component. The Component will
  466. * receive drops only if it is enabled.
  467. *
  468. * @see #isEnabled
  469. * @param dt The DropTarget
  470. */
  471. public synchronized void setDropTarget(DropTarget dt) {
  472. if (dt == dropTarget || (dropTarget != null && dropTarget.equals(dt)))
  473. return;
  474. DropTarget old;
  475. if ((old = dropTarget) != null) {
  476. if (peer != null) dropTarget.removeNotify(peer);
  477. DropTarget t = dropTarget;
  478. dropTarget = null;
  479. try {
  480. t.setComponent(null);
  481. } catch (IllegalArgumentException iae) {
  482. // ignore it.
  483. }
  484. }
  485. // if we have a new one, and we have a peer, add it!
  486. if ((dropTarget = dt) != null) {
  487. try {
  488. dropTarget.setComponent(this);
  489. if (peer != null) dropTarget.addNotify(peer);
  490. } catch (IllegalArgumentException iae) {
  491. if (old != null) {
  492. try {
  493. old.setComponent(this);
  494. if (peer != null) dropTarget.addNotify(peer);
  495. } catch (IllegalArgumentException iae1) {
  496. // ignore it!
  497. }
  498. }
  499. }
  500. }
  501. }
  502. /**
  503. * Get the DropTarget associated with this Component
  504. */
  505. public synchronized DropTarget getDropTarget() { return dropTarget; }
  506. /**
  507. * Get the <code>GraphicsConfiguration</code> associated with this
  508. * <code>Component</code>.
  509. * If the <code>Component</code> has not been assigned a specific
  510. * <code>GraphicsConfiguration</code>,
  511. * the <code>GraphicsConfiguration</code> of the
  512. * <code>Component</code> object's top-level container is
  513. * returned.
  514. * If the <code>Component</code> has been created, but not yet added
  515. * to a <code>Container</code>, this method returns <code>null</code>.
  516. * @return the <code>GraphicsConfiguration</code> used by this
  517. * <code>Component</code> or <code>null</code>
  518. * @since 1.3
  519. */
  520. public GraphicsConfiguration getGraphicsConfiguration() {
  521. synchronized(getTreeLock()) {
  522. if (graphicsConfig != null) {
  523. return graphicsConfig;
  524. } else if (getParent() != null) {
  525. return getParent().getGraphicsConfiguration();
  526. } else {
  527. return null;
  528. }
  529. }
  530. }
  531. /**
  532. * Reset this Componenet's GraphicsConfiguration back to a default
  533. * value. For most Componenets, this is null.
  534. * Called from the Toolkit thread, so NO CLIENT CODE.
  535. */
  536. void resetGC() {
  537. synchronized(getTreeLock()) {
  538. graphicsConfig = null;
  539. }
  540. }
  541. /**
  542. * Checks that this Component's GraphicsDevice idString matches
  543. * the String argument
  544. */
  545. void checkGD(String stringID) {
  546. if (graphicsConfig != null) {
  547. if (!graphicsConfig.getDevice().getIDstring().equals(stringID)) {
  548. throw new IllegalArgumentException(
  549. "adding a container to a container on a different GraphicsDevice");
  550. }
  551. }
  552. }
  553. /**
  554. * Gets the locking object for AWT component-tree and layout
  555. * Gets this component's locking object (the object that owns the thread
  556. * sychronization monitor) for AWT component-tree and layout
  557. * operations.
  558. * @return This component's locking object.
  559. */
  560. public final Object getTreeLock() {
  561. return LOCK;
  562. }
  563. /**
  564. * Gets the toolkit of this component. Note that
  565. * the frame that contains a component controls which
  566. * toolkit is used by that component. Therefore if the component
  567. * is moved from one frame to another, the toolkit it uses may change.
  568. * @return The toolkit of this component.
  569. * @since JDK1.0
  570. */
  571. public Toolkit getToolkit() {
  572. return getToolkitImpl();
  573. }
  574. /*
  575. * This is called by the native code, so client code can't
  576. * be called on the toolkit thread.
  577. */
  578. final Toolkit getToolkitImpl() {
  579. ComponentPeer peer = this.peer;
  580. if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)){
  581. return peer.getToolkit();
  582. }
  583. Container parent = this.parent;
  584. if (parent != null) {
  585. return parent.getToolkitImpl();
  586. }
  587. return Toolkit.getDefaultToolkit();
  588. }
  589. /**
  590. * Determines whether this component is valid. A component is valid
  591. * when it is correctly sized and positioned within its parent
  592. * container and all its children are also valid. Components are
  593. * invalidated when they are first shown on the screen.
  594. * @return <code>true</code> if the component is valid; <code>false</code>
  595. * otherwise.
  596. * @see #validate
  597. * @see #invalidate
  598. * @since JDK1.0
  599. */
  600. public boolean isValid() {
  601. return (peer != null) && valid;
  602. }
  603. /**
  604. * Determines whether this component is displayable. A component is
  605. * displayable when it is connected to a native screen resource.
  606. * <p>
  607. * A component is made displayable either when it is added to
  608. * a displayable containment hierarchy or when its containment
  609. * hierarchy is made displayable.
  610. * A containment hierarchy is made displayable when its ancestor
  611. * window is either packed or made visible.
  612. * <p>
  613. * A component is made undisplayable either when it is removed from
  614. * a displayable containment hierarchy or when its containment hierarchy
  615. * is made undisplayable. A containment hierarchy is made
  616. * undisplayable when its ancestor window is disposed.
  617. *
  618. * @return <code>true</code> if the component is displayable;
  619. * <code>false</code> otherwise.
  620. * @see java.awt.Container#add(java.awt.Component)
  621. * @see java.awt.Window#pack
  622. * @see java.awt.Window#show
  623. * @see java.awt.Container#remove(java.awt.Component)
  624. * @see java.awt.Window#dispose
  625. * @since 1.2
  626. */
  627. public boolean isDisplayable() {
  628. return getPeer() != null;
  629. }
  630. /**
  631. * Determines whether this component should be visible when its
  632. * parent is visible. Components are
  633. * initially visible, with the exception of top level components such
  634. * as <code>Frame</code> objects.
  635. * @return <code>true</code> if the component is visible;
  636. * <code>false</code> otherwise.
  637. * @see #setVisible
  638. * @since JDK1.0
  639. */
  640. public boolean isVisible() {
  641. return visible;
  642. }
  643. /**
  644. * Determines whether this component will be displayed on the screen
  645. * if it's displayable.
  646. * @return <code>true</code> if the component and all of its ancestors
  647. * are visible; <code>false</code> otherwise.
  648. */
  649. boolean isRecursivelyVisible() {
  650. return visible && (parent == null || parent.isRecursivelyVisible());
  651. }
  652. /**
  653. * Determines whether this component is showing on screen. This means
  654. * that the component must be visible, and it must be in a container
  655. * that is visible and showing.
  656. * @return <code>true</code> if the component is showing;
  657. * <code>false</code> otherwise.
  658. * @see #setVisible
  659. * @since JDK1.0
  660. */
  661. public boolean isShowing() {
  662. if (visible && (peer != null)) {
  663. Container parent = this.parent;
  664. return (parent == null) || parent.isShowing();
  665. }
  666. return false;
  667. }
  668. /**
  669. * Determines whether this component is enabled. An enabled component
  670. * can respond to user input and generate events. Components are
  671. * enabled initially by default. A component may be enabled or disabled by
  672. * calling its <code>setEnabled</code> method.
  673. * @return <code>true</code> if the component is enabled;
  674. * <code>false</code> otherwise.
  675. * @see #setEnabled
  676. * @since JDK1.0
  677. */
  678. public boolean isEnabled() {
  679. return isEnabledImpl();
  680. }
  681. /*
  682. * This is called by the native code, so client code can't
  683. * be called on the toolkit thread.
  684. */
  685. final boolean isEnabledImpl() {
  686. return enabled;
  687. }
  688. /**
  689. * Enables or disables this component, depending on the value of the
  690. * parameter <code>b</code>. An enabled component can respond to user
  691. * input and generate events. Components are enabled initially by default.
  692. * @param b If <code>true</code>, this component is
  693. * enabled; otherwise this component is disabled.
  694. * @see #isEnabled
  695. * @since JDK1.1
  696. */
  697. public void setEnabled(boolean b) {
  698. enable(b);
  699. }
  700. /**
  701. * @deprecated As of JDK version 1.1,
  702. * replaced by <code>setEnabled(boolean)</code>.
  703. */
  704. public void enable() {
  705. if (enabled != true) {
  706. synchronized (getTreeLock()) {
  707. enabled = true;
  708. ComponentPeer peer = this.peer;
  709. if (peer != null) {
  710. peer.enable();
  711. if (visible) {
  712. GlobalCursorManager.updateCursorImmediately();
  713. }
  714. }
  715. }
  716. if (accessibleContext != null) {
  717. accessibleContext.firePropertyChange(
  718. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  719. null, AccessibleState.ENABLED);
  720. }
  721. }
  722. }
  723. /**
  724. * @deprecated As of JDK version 1.1,
  725. * replaced by <code>setEnabled(boolean)</code>.
  726. */
  727. public void enable(boolean b) {
  728. if (b) {
  729. enable();
  730. } else {
  731. disable();
  732. }
  733. }
  734. /**
  735. * @deprecated As of JDK version 1.1,
  736. * replaced by <code>setEnabled(boolean)</code>.
  737. */
  738. public void disable() {
  739. if (enabled != false) {
  740. synchronized (getTreeLock()) {
  741. enabled = false;
  742. ComponentPeer peer = this.peer;
  743. if (peer != null) {
  744. peer.disable();
  745. if (visible) {
  746. GlobalCursorManager.updateCursorImmediately();
  747. }
  748. }
  749. }
  750. if (accessibleContext != null) {
  751. accessibleContext.firePropertyChange(
  752. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  753. null, AccessibleState.ENABLED);
  754. }
  755. }
  756. }
  757. /**
  758. * Returns true if this component is painted to an offscreen image
  759. * ("buffer") that's copied to the screen later. Component
  760. * subclasses that support double buffering should override this
  761. * method to return true if double buffering is enabled.
  762. *
  763. * @return false by default
  764. */
  765. public boolean isDoubleBuffered() {
  766. return false;
  767. }
  768. /**
  769. * Enables or disables input method support for this component. If input
  770. * method support is enabled and the component also processes key events,
  771. * incoming events are offered to
  772. * the current input method and will only be processed by the component or
  773. * dispatched to its listeners if the input method does not consume them.
  774. * By default, input method support is enabled.
  775. *
  776. * @param enable true to enable, false to disable.
  777. * @see java.awt.Component#processKeyEvent
  778. * @since 1.2
  779. */
  780. public void enableInputMethods(boolean enable) {
  781. if (enable) {
  782. if ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0)
  783. return;
  784. // If this component already has focus, then activate the
  785. // input method by dispatching a synthesized focus gained
  786. // event.
  787. if (hasFocus() == true) {
  788. InputContext inputContext = getInputContext();
  789. if (inputContext != null) {
  790. FocusEvent focusGainedEvent = new FocusEvent(this,
  791. FocusEvent.FOCUS_GAINED);
  792. inputContext.dispatchEvent(focusGainedEvent);
  793. }
  794. }
  795. eventMask |= AWTEvent.INPUT_METHODS_ENABLED_MASK;
  796. } else {
  797. if (areInputMethodsEnabled()) {
  798. InputContext inputContext = getInputContext();
  799. if (inputContext != null) {
  800. inputContext.endComposition();
  801. inputContext.removeNotify(this);
  802. }
  803. }
  804. eventMask &= ~AWTEvent.INPUT_METHODS_ENABLED_MASK;
  805. }
  806. }
  807. /**
  808. * Shows or hides this component depending on the value of parameter
  809. * <code>b</code>.
  810. * @param b If <code>true</code>, shows this component;
  811. * otherwise, hides this component.
  812. * @see #isVisible
  813. * @since JDK1.1
  814. */
  815. public void setVisible(boolean b) {
  816. show(b);
  817. }
  818. /**
  819. * @deprecated As of JDK version 1.1,
  820. * replaced by <code>setVisible(boolean)</code>.
  821. */
  822. public void show() {
  823. if (!visible) {
  824. synchronized (getTreeLock()) {
  825. visible = true;
  826. ComponentPeer peer = this.peer;
  827. if (peer != null) {
  828. peer.show();
  829. createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
  830. this, parent,
  831. HierarchyEvent.SHOWING_CHANGED);
  832. if (peer instanceof java.awt.peer.LightweightPeer) {
  833. repaint();
  834. }
  835. GlobalCursorManager.updateCursorImmediately();
  836. }
  837. if (componentListener != null ||
  838. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
  839. ComponentEvent e = new ComponentEvent(this,
  840. ComponentEvent.COMPONENT_SHOWN);
  841. Toolkit.getEventQueue().postEvent(e);
  842. }
  843. }
  844. Container parent = this.parent;
  845. if (parent != null) {
  846. parent.invalidate();
  847. }
  848. }
  849. }
  850. /**
  851. * @deprecated As of JDK version 1.1,
  852. * replaced by <code>setVisible(boolean)</code>.
  853. */
  854. public void show(boolean b) {
  855. if (b) {
  856. show();
  857. } else {
  858. hide();
  859. }
  860. }
  861. /**
  862. * @deprecated As of JDK version 1.1,
  863. * replaced by <code>setVisible(boolean)</code>.
  864. */
  865. public void hide() {
  866. if (visible) {
  867. synchronized (getTreeLock()) {
  868. visible = false;
  869. ComponentPeer peer = this.peer;
  870. if (peer != null) {
  871. peer.hide();
  872. createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
  873. this, parent,
  874. HierarchyEvent.SHOWING_CHANGED);
  875. if (peer instanceof java.awt.peer.LightweightPeer) {
  876. repaint();
  877. }
  878. GlobalCursorManager.updateCursorImmediately();
  879. }
  880. if (componentListener != null ||
  881. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
  882. ComponentEvent e = new ComponentEvent(this,
  883. ComponentEvent.COMPONENT_HIDDEN);
  884. Toolkit.getEventQueue().postEvent(e);
  885. }
  886. }
  887. Container parent = this.parent;
  888. if (parent != null) {
  889. parent.invalidate();
  890. }
  891. }
  892. }
  893. /**
  894. * Gets the foreground color of this component.
  895. * @return This component's foreground color. If this component does
  896. * not have a foreground color, the foreground color of its parent
  897. * is returned.
  898. * @see #setForeground
  899. * @since JDK1.0
  900. */
  901. public Color getForeground() {
  902. Color foreground = this.foreground;
  903. if (foreground != null) {
  904. return foreground;
  905. }
  906. Container parent = this.parent;
  907. return (parent != null) ? parent.getForeground() : null;
  908. }
  909. /**
  910. * Sets the foreground color of this component.
  911. * @param c The color to become this component's
  912. * foreground color.
  913. * If this parameter is null then this component will inherit
  914. * the foreground color of its parent.
  915. * @see #getForeground
  916. * @since JDK1.0
  917. */
  918. public void setForeground(Color c) {
  919. Color oldColor = foreground;
  920. ComponentPeer peer = this.peer;
  921. foreground = c;
  922. if (peer != null) {
  923. c = getForeground();
  924. if (c != null) {
  925. peer.setForeground(c);
  926. }
  927. }
  928. // This is a bound property, so report the change to
  929. // any registered listeners. (Cheap if there are none.)
  930. firePropertyChange("foreground", oldColor, c);
  931. }
  932. /**
  933. * Gets the background color of this component.
  934. * @return This component's background color. If this component does
  935. * not have a background color, the background color of its parent
  936. * is returned.
  937. * @see java.awt.Component#setBackground(java.awt.Color)
  938. * @since JDK1.0
  939. */
  940. public Color getBackground() {
  941. Color background = this.background;
  942. if (background != null) {
  943. return background;
  944. }
  945. Container parent = this.parent;
  946. return (parent != null) ? parent.getBackground() : null;
  947. }
  948. /**
  949. * Sets the background color of this component.
  950. * @param c The color to become this component's color.
  951. * If this parameter is null then this component will inherit
  952. * the background color of its parent.
  953. * background color.
  954. * @see #getBackground
  955. * @since JDK1.0
  956. */
  957. public void setBackground(Color c) {
  958. Color oldColor = background;
  959. ComponentPeer peer = this.peer;
  960. background = c;
  961. if (peer != null) {
  962. c = getBackground();
  963. if (c != null) {
  964. peer.setBackground(c);
  965. }
  966. }
  967. // This is a bound property, so report the change to
  968. // any registered listeners. (Cheap if there are none.)
  969. firePropertyChange("background", oldColor, c);
  970. }
  971. /**
  972. * Gets the font of this component.
  973. * @return This component's font. If a font has not been set
  974. * for this component, the font of its parent is returned.
  975. * @see #setFont
  976. * @since JDK1.0
  977. */
  978. public Font getFont() {
  979. return getFont_NoClientCode();
  980. }
  981. // NOTE: This method may be called by privileged threads.
  982. // This functionality is implemented in a package-private method
  983. // to insure that it cannot be overridden by client subclasses.
  984. // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  985. final Font getFont_NoClientCode() {
  986. Font font = this.font;
  987. if (font != null) {
  988. return font;
  989. }
  990. Container parent = this.parent;
  991. return (parent != null) ? parent.getFont_NoClientCode() : null;
  992. }
  993. /**
  994. * Sets the font of this component.
  995. * @param f The font to become this component's font.
  996. * If this parameter is null then this component will inherit
  997. * the font of its parent.
  998. * @see #getFont
  999. * @since JDK1.0
  1000. */
  1001. public void setFont(Font f) {
  1002. Font oldFont, newFont;
  1003. synchronized (this) {
  1004. oldFont = font;
  1005. ComponentPeer peer = this.peer;
  1006. newFont = font = f;
  1007. if (peer != null) {
  1008. f = getFont();
  1009. if (f != null) {
  1010. peer.setFont(f);
  1011. peerFont = f;
  1012. }
  1013. }
  1014. }
  1015. // This is a bound property, so report the change to
  1016. // any registered listeners. (Cheap if there are none.)
  1017. firePropertyChange("font", oldFont, newFont);
  1018. // This could change the preferred size of the Component.
  1019. if (valid) {
  1020. invalidate();
  1021. }
  1022. }
  1023. /**
  1024. * Gets the locale of this component.
  1025. * @return This component's locale. If this component does not
  1026. * have a locale, the locale of its parent is returned.
  1027. * @see #setLocale
  1028. * @exception IllegalComponentStateException If the Component
  1029. * does not have its own locale and has not yet been added to
  1030. * a containment hierarchy such that the locale can be determined
  1031. * from the containing parent.
  1032. * @since JDK1.1
  1033. */
  1034. public Locale getLocale() {
  1035. Locale locale = this.locale;
  1036. if (locale != null) {
  1037. return locale;
  1038. }
  1039. Container parent = this.parent;
  1040. if (parent == null) {
  1041. throw new IllegalComponentStateException("This component must have a parent in order to determine its locale");
  1042. } else {
  1043. return parent.getLocale();
  1044. }
  1045. }
  1046. /**
  1047. * Sets the locale of this component.
  1048. * @param l The locale to become this component's locale.
  1049. * @see #getLocale
  1050. * @since JDK1.1
  1051. */
  1052. public void setLocale(Locale l) {
  1053. locale = l;
  1054. // This could change the preferred size of the Component.
  1055. if (valid) {
  1056. invalidate();
  1057. }
  1058. }
  1059. /**
  1060. * Gets the instance of <code>ColorModel</code> used to display
  1061. * the component on the output device.
  1062. * @return The color model used by this component.
  1063. * @see java.awt.image.ColorModel
  1064. * @see java.awt.peer.ComponentPeer#getColorModel()
  1065. * @see java.awt.Toolkit#getColorModel()
  1066. * @since JDK1.0
  1067. */
  1068. public ColorModel getColorModel() {
  1069. ComponentPeer peer = this.peer;
  1070. if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)) {
  1071. return peer.getColorModel();
  1072. }
  1073. return getToolkit().getColorModel();
  1074. }
  1075. /**
  1076. * Gets the location of this component in the form of a
  1077. * point specifying the component's top-left corner.
  1078. * The location will be relative to the parent's coordinate space.
  1079. * <p>
  1080. * Due to the asynchronous nature of native event handling, this
  1081. * method can return outdated values (for instance, after several calls
  1082. * of <code>setLocation()</code> in rapid succession). For this
  1083. * reason, the recommended method of obtaining a Component's position is
  1084. * within <code>java.awt.event.ComponentListener.componentMoved()</code>,
  1085. * which is called after the operating system has finished moving the
  1086. * Component.
  1087. * </p>
  1088. * @return An instance of <code>Point</code> representing
  1089. * the top-left corner of the component's bounds in the coordinate
  1090. * space of the component's parent.
  1091. * @see #setLocation
  1092. * @see #getLocationOnScreen
  1093. * @since JDK1.1
  1094. */
  1095. public Point getLocation() {
  1096. return location();
  1097. }
  1098. /**
  1099. * Gets the location of this component in the form of a point
  1100. * specifying the component's top-left corner in the screen's
  1101. * coordinate space.
  1102. * @return An instance of <code>Point</code> representing
  1103. * the top-left corner of the component's bounds in the
  1104. * coordinate space of the screen.
  1105. * @see #setLocation
  1106. * @see #getLocation
  1107. */
  1108. public Point getLocationOnScreen() {
  1109. synchronized (getTreeLock()) {
  1110. return getLocationOnScreen_NoTreeLock();
  1111. }
  1112. }
  1113. /*
  1114. * a package private version of getLocationOnScreen
  1115. * used by GlobalCursormanager to update cursor
  1116. */
  1117. final Point getLocationOnScreen_NoTreeLock() {
  1118. if (peer != null && isShowing()) {
  1119. if (peer instanceof java.awt.peer.LightweightPeer) {
  1120. // lightweight component location needs to be translated
  1121. // relative to a native component.
  1122. Container host = getNativeContainer();
  1123. Point pt = host.peer.getLocationOnScreen();
  1124. for(Component c = this; c != host; c = c.getParent()) {
  1125. pt.x += c.x;
  1126. pt.y += c.y;
  1127. }
  1128. return pt;
  1129. } else {
  1130. Point pt = peer.getLocationOnScreen();
  1131. return pt;
  1132. }
  1133. } else {
  1134. throw new IllegalComponentStateException("component must be showing on the screen to determine its location");
  1135. }
  1136. }
  1137. /**
  1138. * @deprecated As of JDK version 1.1,
  1139. * replaced by <code>getLocation()</code>.
  1140. */
  1141. public Point location() {
  1142. return new Point(x, y);
  1143. }
  1144. /**
  1145. * Moves this component to a new location. The top-left corner of
  1146. * the new location is specified by the <code>x</code> and <code>y</code>
  1147. * parameters in the coordinate space of this component's parent.
  1148. * @param x The <i>x</i>-coordinate of the new location's
  1149. * top-left corner in the parent's coordinate space.
  1150. * @param y The <i>y</i>-coordinate of the new location's
  1151. * top-left corner in the parent's coordinate space.
  1152. * @see #getLocation
  1153. * @see #setBounds
  1154. * @since JDK1.1
  1155. */
  1156. public void setLocation(int x, int y) {
  1157. move(x, y);
  1158. }
  1159. /**
  1160. * @deprecated As of JDK version 1.1,
  1161. * replaced by <code>setLocation(int, int)</code>.
  1162. */
  1163. public void move(int x, int y) {
  1164. setBounds(x, y, width, height);
  1165. }
  1166. /**
  1167. * Moves this component to a new location. The top-left corner of
  1168. * the new location is specified by point <code>p</code>. Point
  1169. * <code>p</code> is given in the parent's coordinate space.
  1170. * @param p The point defining the top-left corner
  1171. * of the new location, given in the coordinate space of this
  1172. * component's parent.
  1173. * @see #getLocation
  1174. * @see #setBounds
  1175. * @since JDK1.1
  1176. */
  1177. public void setLocation(Point p) {
  1178. setLocation(p.x, p.y);
  1179. }
  1180. /**
  1181. * Returns the size of this component in the form of a
  1182. * <code>Dimension</code> object. The <code>height</code>
  1183. * field of the <code>Dimension</code> object contains
  1184. * this component's height, and the <code>width</code>
  1185. * field of the <code>Dimension</code> object contains
  1186. * this component's width.
  1187. * @return A <code>Dimension</code> object that indicates the
  1188. * size of this component.
  1189. * @see #setSize
  1190. * @since JDK1.1
  1191. */
  1192. public Dimension getSize() {
  1193. return size();
  1194. }
  1195. /**
  1196. * @deprecated As of JDK version 1.1,
  1197. * replaced by <code>getSize()</code>.
  1198. */
  1199. public Dimension size() {
  1200. return new Dimension(width, height);
  1201. }
  1202. /**
  1203. * Resizes this component so that it has width <code>width</code>
  1204. * and <code>height</code>.
  1205. * @param width The new width of this component in pixels.
  1206. * @param height The new height of this component in pixels.
  1207. * @see #getSize
  1208. * @see #setBounds
  1209. * @since JDK1.1
  1210. */
  1211. public void setSize(int width, int height) {
  1212. resize(width, height);
  1213. }
  1214. /**
  1215. * @deprecated As of JDK version 1.1,
  1216. * replaced by <code>setSize(int, int)</code>.
  1217. */
  1218. public void resize(int width, int height) {
  1219. setBounds(x, y, width, height);
  1220. }
  1221. /**
  1222. * Resizes this component so that it has width <code>d.width</code>
  1223. * and height <code>d.height</code>.
  1224. * @param d The dimension specifying the new size
  1225. * of this component.
  1226. * @see #setSize
  1227. * @see #setBounds
  1228. * @since JDK1.1
  1229. */
  1230. public void setSize(Dimension d) {
  1231. resize(d);
  1232. }
  1233. /**
  1234. * @deprecated As of JDK version 1.1,
  1235. * replaced by <code>setSize(Dimension)</code>.
  1236. */
  1237. public void resize(Dimension d) {
  1238. setSize(d.width, d.height);
  1239. }
  1240. /**
  1241. * Gets the bounds of this component in the form of a
  1242. * <code>Rectangle</code> object. The bounds specify this
  1243. * component's width, height, and location relative to
  1244. * its parent.
  1245. * @return A rectangle indicating this component's bounds.
  1246. * @see #setBounds
  1247. * @see #getLocation
  1248. * @see #getSize
  1249. */
  1250. public Rectangle getBounds() {
  1251. return bounds();
  1252. }
  1253. /**
  1254. * @deprecated As of JDK version 1.1,
  1255. * replaced by <code>getBounds()</code>.
  1256. */
  1257. public Rectangle bounds() {
  1258. return new Rectangle(x, y, width, height);
  1259. }
  1260. /**
  1261. * Moves and resizes this component. The new location of the top-left
  1262. * corner is specified by <code>x</code> and <code>y</code>, and the
  1263. * new size is specified by <code>width</code> and <code>height</code>.
  1264. * @param x The new <i>x</i>-coordinate of this component.
  1265. * @param y The new <i>y</i>-coordinate of this component.
  1266. * @param width The new <code>width</code> of this component.
  1267. * @param height The new <code>height</code> of this
  1268. * component.
  1269. * @see java.awt.Component#getBounds
  1270. * @see java.awt.Component#setLocation(int, int)
  1271. * @see java.awt.Component#setLocation(java.awt.Point)
  1272. * @see java.awt.Component#setSize(int, int)
  1273. * @see java.awt.Component#setSize(java.awt.Dimension)
  1274. * @JDK1.1
  1275. */
  1276. public void setBounds(int x, int y, int width, int height) {
  1277. reshape(x, y, width, height);
  1278. }
  1279. /**
  1280. * @deprecated As of JDK version 1.1,
  1281. * replaced by <code>setBounds(int, int, int, int)</code>.
  1282. */
  1283. public void reshape(int x, int y, int width, int height) {
  1284. synchronized (getTreeLock()) {
  1285. boolean resized = (this.width != width) || (this.height != height);
  1286. boolean moved = (this.x != x) || (this.y != y);
  1287. boolean isLightweight = peer instanceof java.awt.peer.LightweightPeer;
  1288. if (resized) {
  1289. isPacked = false;
  1290. }
  1291. if (resized || moved) {
  1292. if (isLightweight && visible) {
  1293. // Have the parent redraw the area this component occupied.
  1294. repaint();
  1295. }
  1296. this.x = x;
  1297. this.y = y;
  1298. this.width = width;
  1299. this.height = height;
  1300. if (peer != null) {
  1301. if (isLightweight) {
  1302. peer.setBounds(x, y, width, height);
  1303. } else {
  1304. // native peer might be offset by more than direct
  1305. // parent since parent might be lightweight.
  1306. int nativeX = x;
  1307. int nativeY = y;
  1308. for(Component c = parent; (c != null) &&
  1309. (c.peer instanceof java.awt.peer.LightweightPeer);
  1310. c = c.parent) {
  1311. nativeX += c.x;
  1312. nativeY += c.y;
  1313. }
  1314. peer.setBounds(nativeX, nativeY, width, height);
  1315. }
  1316. if (resized) {
  1317. invalidate();
  1318. }
  1319. if (parent != null && parent.valid) {
  1320. parent.invalidate();
  1321. }
  1322. }
  1323. if (resized) {
  1324. if (componentListener != null ||
  1325. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
  1326. ComponentEvent e = new ComponentEvent(this,
  1327. ComponentEvent.COMPONENT_RESIZED);
  1328. Toolkit.getEventQueue().postEvent(e);
  1329. // Container.dispatchEventImpl will create
  1330. // HierarchyEvents
  1331. } else {
  1332. createChildHierarchyEvents(
  1333. HierarchyEvent.ANCESTOR_RESIZED,
  1334. 0);
  1335. }
  1336. }
  1337. if (moved) {
  1338. if (componentListener != null ||
  1339. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0){
  1340. ComponentEvent e = new ComponentEvent(this,
  1341. ComponentEvent.COMPONENT_MOVED);
  1342. Toolkit.getEventQueue().postEvent(e);
  1343. // Container.dispatchEventImpl will create
  1344. // HierarchyEvents
  1345. } else {
  1346. createChildHierarchyEvents(
  1347. HierarchyEvent.ANCESTOR_MOVED,
  1348. 0);
  1349. }
  1350. }
  1351. if (isLightweight && visible) {
  1352. // Have the parent redraw the area this component *now* occupies.
  1353. repaint();
  1354. }
  1355. }
  1356. }
  1357. }
  1358. /**
  1359. * Moves and resizes this component to conform to the new
  1360. * bounding rectangle <code>r</code>. This component's new
  1361. * position is specified by <code>r.x</code> and <code>r.y</code>,
  1362. * and its new size is specified by <code>r.width</code> and
  1363. * <code>r.height</code>
  1364. * @param r The new bounding rectangle for this component.
  1365. * @see java.awt.Component#getBounds
  1366. * @see java.awt.Component#setLocation(int, int)
  1367. * @see java.awt.Component#setLocation(java.awt.Point)
  1368. * @see java.awt.Component#setSize(int, int)
  1369. * @see java.awt.Component#setSize(java.awt.Dimension)
  1370. * @since JDK1.1
  1371. */
  1372. public void setBounds(Rectangle r) {
  1373. setBounds(r.x, r.y, r.width, r.height);
  1374. }
  1375. /**
  1376. * Return the current x coordinate of the components origin.
  1377. * This method is preferable to writing component.getBounds().x,
  1378. * or component.getLocation().x because it doesn't cause any
  1379. * heap allocations.
  1380. *
  1381. * @return the current x coordinate of the components origin.
  1382. * @since 1.2
  1383. */
  1384. public int getX() {
  1385. return x;
  1386. }
  1387. /**
  1388. * Return the current y coordinate of the components origin.
  1389. * This method is preferable to writing component.getBounds().y,
  1390. * or component.getLocation().y because it doesn't cause any
  1391. * heap allocations.
  1392. *
  1393. * @return the current y coordinate of the components origin.
  1394. * @since 1.2
  1395. */
  1396. public int getY() {
  1397. return y;
  1398. }
  1399. /**
  1400. * Return the current width of this component.
  1401. * This method is preferable to writing component.getBounds().width,
  1402. * or component.getSize().width because it doesn't cause any
  1403. * heap allocations.
  1404. *
  1405. * @return the current width of this component.
  1406. * @since 1.2
  1407. */
  1408. public int getWidth() {
  1409. return width;
  1410. }
  1411. /**
  1412. * Return the current height of this component.
  1413. * This method is preferable to writing component.getBounds().height,
  1414. * or component.getSize().height because it doesn't cause any
  1415. * heap allocations.
  1416. *
  1417. * @return the current height of this component.
  1418. * @since 1.2
  1419. */
  1420. public int getHeight() {
  1421. return height;
  1422. }
  1423. /**
  1424. * Store the bounds of this component into "return value" <b>rv</b> and
  1425. * return <b>rv</b>. If rv is null a new Rectangle is allocated.
  1426. * This version of getBounds() is useful if the caller
  1427. * wants to avoid allocating a new Rectangle object on the heap.
  1428. *
  1429. * @param rv the return value, modified to the components bounds
  1430. * @return rv
  1431. */
  1432. public Rectangle getBounds(Rectangle rv) {
  1433. if (rv == null) {
  1434. return new Rectangle(getX(), getY(), getWidth(), getHeight());
  1435. }
  1436. else {
  1437. rv.setBounds(getX(), getY(), getWidth(), getHeight());
  1438. return rv;
  1439. }
  1440. }
  1441. /**
  1442. * Store the width/height of this component into "return value" <b>rv</b>
  1443. * and return <b>rv</b>. If rv is null a new Dimension object is
  1444. * allocated. This version of getSize() is useful if the
  1445. * caller wants to avoid allocating a new Dimension object on the heap.
  1446. *
  1447. * @param rv the return value, modified to the components size
  1448. * @return rv
  1449. */
  1450. public Dimension getSize(Dimension rv) {
  1451. if (rv == null) {
  1452. return new Dimension(getWidth(), getHeight());
  1453. }
  1454. else {
  1455. rv.setSize(getWidth(), getHeight());
  1456. return rv;
  1457. }
  1458. }
  1459. /**
  1460. * Store the x,y origin of this component into "return value" <b>rv</b>
  1461. * and return <b>rv</b>. If rv is null a new Point is allocated.
  1462. * This version of getLocation() is useful if the
  1463. * caller wants to avoid allocating a new Point object on the heap.
  1464. *
  1465. * @param rv the return value, modified to the components location
  1466. * @return rv
  1467. */
  1468. public Point getLocation(Point rv) {
  1469. if (rv == null) {
  1470. return new Point(getX(), getY());
  1471. }
  1472. else {
  1473. rv.setLocation(getX(), getY());
  1474. return rv;
  1475. }
  1476. }
  1477. /**
  1478. * Returns true if this component is completely opaque, returns
  1479. * false by default.
  1480. * <p>
  1481. * An opaque component paints every pixel within its
  1482. * rectangular region. A non-opaque component paints only some of
  1483. * its pixels, allowing the pixels underneath it to "show through".
  1484. * A component that does not fully paint its pixels therefore
  1485. * provides a degree of transparency. Only lightweight
  1486. * components can be transparent.
  1487. * <p>
  1488. * Subclasses that guarantee to always completely paint their
  1489. * contents should override this method and return true. All
  1490. * of the "heavyweight" AWT components are opaque.
  1491. *
  1492. * @return true if this component is completely opaque.
  1493. * @see #isLightweight
  1494. * @since 1.2
  1495. */
  1496. public boolean isOpaque() {
  1497. return !isLightweight();
  1498. }
  1499. /**
  1500. * A lightweight component doesn't have a native toolkit peer.
  1501. * Subclasses of Component and Container, other than the ones
  1502. * defined in this package like Button or Scrollbar, are lightweight.
  1503. * All of the Swing components are lightweights.
  1504. *
  1505. * This method will always return <code>false</code> if this Component
  1506. * is not displayable because it is impossible to determine the
  1507. * weight of an undisplayable Component.
  1508. *
  1509. * @return true if this component has a lightweight peer; false if
  1510. * it has a native peer or no peer.
  1511. * @see #isDisplayable
  1512. * @since 1.2
  1513. */
  1514. public boolean isLightweight() {
  1515. return getPeer() instanceof java.awt.peer.LightweightPeer;
  1516. }
  1517. /**
  1518. * Gets the preferred size of this component.
  1519. * @return A dimension object indicating this component's preferred size.
  1520. * @see #getMinimumSize
  1521. * @see java.awt.LayoutManager
  1522. */
  1523. public Dimension getPreferredSize() {
  1524. return preferredSize();
  1525. }
  1526. /**
  1527. * @deprecated As of JDK version 1.1,
  1528. * replaced by <code>getPreferredSize()</code>.
  1529. */
  1530. public Dimension preferredSize() {
  1531. /* Avoid grabbing the lock if a reasonable cached size value
  1532. * is available.
  1533. */
  1534. Dimension dim = prefSize;
  1535. if (dim != null && isValid()) {
  1536. return dim;
  1537. }
  1538. synchronized (getTreeLock()) {
  1539. prefSize = (peer != null) ?
  1540. peer.preferredSize() :
  1541. getMinimumSize();
  1542. return prefSize;
  1543. }
  1544. }
  1545. /**
  1546. * Gets the mininimum size of this component.
  1547. * @return A dimension object indicating this component's minimum size.
  1548. * @see #getPreferredSize
  1549. * @see java.awtLayoutManager
  1550. */
  1551. public Dimension getMinimumSize() {
  1552. return minimumSize();
  1553. }
  1554. /**
  1555. * @deprecated As of JDK version 1.1,
  1556. * replaced by <code>getMinimumSize()</code>.
  1557. */
  1558. public Dimension minimumSize() {
  1559. /* Avoid grabbing the lock if a reasonable cached size value
  1560. * is available.
  1561. */
  1562. Dimension dim = minSize;
  1563. if (dim != null && isValid()) {
  1564. return dim;
  1565. }
  1566. synchronized (getTreeLock()) {
  1567. minSize = (peer != null) ?
  1568. peer.minimumSize() :
  1569. size();
  1570. return minSize;
  1571. }
  1572. }
  1573. /**
  1574. * Gets the maximum size of this component.
  1575. * @return A dimension object indicating this component's maximum size.
  1576. * @see #getMinimumSize
  1577. * @see #getPreferredSize
  1578. * @see LayoutManager
  1579. */
  1580. public Dimension getMaximumSize() {
  1581. return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
  1582. }
  1583. /**
  1584. * Returns the alignment along the x axis. This specifies how
  1585. * the component would like to be aligned relative to other
  1586. * components. The value should be a number between 0 and 1
  1587. * where 0 represents alignment along the origin, 1 is aligned
  1588. * the furthest away from the origin, 0.5 is centered, etc.
  1589. */
  1590. public float getAlignmentX() {
  1591. return CENTER_ALIGNMENT;
  1592. }
  1593. /**
  1594. * Returns the alignment along the y axis. This specifies how
  1595. * the component would like to be aligned relative to other
  1596. * components. The value should be a number between 0 and 1
  1597. * where 0 represents alignment along the origin, 1 is aligned
  1598. * the furthest away from the origin, 0.5 is centered, etc.
  1599. */
  1600. public float getAlignmentY() {
  1601. return CENTER_ALIGNMENT;
  1602. }
  1603. /**
  1604. * Prompts the layout manager to lay out this component. This is
  1605. * usually called when the component (more specifically, container)
  1606. * is validated.
  1607. * @see #validate
  1608. * @see LayoutManager
  1609. */
  1610. public void doLayout() {
  1611. layout();
  1612. }
  1613. /**
  1614. * @deprecated As of JDK version 1.1,
  1615. * replaced by <code>doLayout()</code>.
  1616. */
  1617. public void layout() {
  1618. }
  1619. /**
  1620. * Ensures that this component has a valid layout. This method is
  1621. * primarily intended to operate on instances of <code>Container</code>.
  1622. * @see java.awt.Component#invalidate
  1623. * @see java.awt.Component#doLayout()
  1624. * @see java.awt.LayoutManager
  1625. * @see java.awt.Container#validate
  1626. * @since JDK1.0
  1627. */
  1628. public void validate() {
  1629. if (!valid) {
  1630. synchronized (getTreeLock()) {
  1631. ComponentPeer peer = this.peer;
  1632. if (!valid && peer != null) {
  1633. Font newfont = getFont();
  1634. Font oldfont = peerFont;
  1635. if (newfont != oldfont && (oldfont == null
  1636. || !oldfont.equals(newfont))) {
  1637. peer.setFont(newfont);
  1638. peerFont = newfont;
  1639. }
  1640. }
  1641. }
  1642. valid = true;
  1643. }
  1644. }
  1645. /**
  1646. * Invalidates this component. This component and all parents
  1647. * above it are marked as needing to be laid out. This method can
  1648. * be called often, so it needs to execute quickly.
  1649. * @see java.awt.Component#validate
  1650. * @see java.awt.Component#doLayout
  1651. * @see java.awt.LayoutManager
  1652. * @since JDK1.0
  1653. */
  1654. public void invalidate() {
  1655. synchronized (getTreeLock()) {
  1656. /* Nullify cached layout and size information.
  1657. * For efficiency, propagate invalidate() upwards only if
  1658. * some other component hasn't already done so first.
  1659. */
  1660. valid = false;
  1661. prefSize = null;
  1662. minSize = null;
  1663. if (parent != null && parent.valid) {
  1664. parent.invalidate();
  1665. }
  1666. }
  1667. }
  1668. /**
  1669. * Creates a graphics context for this component. This method will
  1670. * return <code>null</code> if this component is currently not
  1671. * displayable.
  1672. * @return A graphics context for this component, or <code>null</code>
  1673. * if it has none.
  1674. * @see java.awt.Component#paint
  1675. * @since JDK1.0
  1676. */
  1677. public Graphics getGraphics() {
  1678. if (peer instanceof java.awt.peer.LightweightPeer) {
  1679. // This is for a lightweight component, need to
  1680. // translate coordinate spaces and clip relative
  1681. // to the parent.
  1682. if (parent == null) return null;
  1683. Graphics g = parent.getGraphics();
  1684. if (g == null) return null;
  1685. if (g instanceof ConstrainableGraphics) {
  1686. ((ConstrainableGraphics) g).constrain(x, y, width, height);
  1687. } else {
  1688. g.translate(x,y);
  1689. g.setClip(0, 0, width, height);
  1690. }
  1691. g.setFont(getFont());
  1692. return g;
  1693. } else {
  1694. ComponentPeer peer = this.peer;
  1695. return (peer != null) ? peer.getGraphics() : null;
  1696. }
  1697. }
  1698. /** saves an internal cache of FontMetrics for better performance **/
  1699. static java.util.Hashtable metrics = new java.util.Hashtable();
  1700. /**
  1701. * Gets the font metrics for the specified font.
  1702. * @param font The font for which font metrics is to be
  1703. * obtained.
  1704. * @return The font metrics for <code>font</code>.
  1705. * @param font the font.
  1706. * @return the font metrics for the specified font.
  1707. * @see java.awt.Component#getFont
  1708. * @see java.awt.Component#getPeer()
  1709. * @see java.awt.peer.ComponentPeer#getFontMetrics(java.awt.Font)
  1710. * @see java.awt.Toolkit#getFontMetrics(java.awt.Font)
  1711. * @since JDK1.0
  1712. */
  1713. public FontMetrics getFontMetrics(Font font) {
  1714. FontMetrics result = (FontMetrics) metrics.get(font);
  1715. if (result != null) {
  1716. return result;
  1717. }
  1718. if (sun.java2d.loops.RasterOutputManager.usesPlatformFont()) {
  1719. if (peer != null &&
  1720. !(peer instanceof java.awt.peer.LightweightPeer)) {
  1721. result = peer.getFontMetrics(font);
  1722. metrics.put(font, result);
  1723. return result;
  1724. }
  1725. }
  1726. if (parent != null) {
  1727. // These are the lines that cost the big dollars. Calling
  1728. // parent.getGraphics triggers the construcion (at great
  1729. // expense) of a new Graphics object that is then quickly
  1730. // discarded. - Graham
  1731. Graphics g = parent.getGraphics();
  1732. if (g != null) {
  1733. try {
  1734. result = g.getFontMetrics(font);
  1735. metrics.put(font, result);
  1736. return result;
  1737. } finally {
  1738. g.dispose();
  1739. }
  1740. }
  1741. }
  1742. result = getToolkit().getFontMetrics(font);
  1743. metrics.put(font, result);
  1744. return result;
  1745. }
  1746. /**
  1747. * Sets the cursor image to the specified cursor. This cursor
  1748. * image is displayed when the <code>contains</code> method for
  1749. * this component returns true for the current cursor location, and
  1750. * this Component is visible, displayable, and enabled. Setting the
  1751. * cursor of a <code>Container</code> causes that cursor to be displayed
  1752. * within all of the container's subcomponents, except for those
  1753. * that have a non-null cursor.
  1754. *
  1755. * @param cursor One of the constants defined
  1756. * by the <code>Cursor</code> class.
  1757. * If this parameter is null then this component will inherit
  1758. * the cursor of its parent.
  1759. * @see #isEnabled
  1760. * @see #isShowing
  1761. * @see java.awt.Component#getCursor
  1762. * @see java.awt.Component#contains
  1763. * @see java.awt.Toolkit#createCustomCursor
  1764. * @see java.awt.Cursor
  1765. * @since JDK1.1
  1766. */
  1767. public void setCursor(Cursor cursor) {
  1768. this.cursor = cursor;
  1769. if (peer != null) {
  1770. GlobalCursorManager.updateCursorImmediately();
  1771. }
  1772. }
  1773. /**
  1774. * Gets the cursor set in the component. If the component does
  1775. * not have a cursor set, the cursor of its parent is returned.
  1776. * If no Cursor is set in the entire hierarchy, Cursor.DEFAULT_CURSOR is
  1777. * returned.
  1778. * @see #setCursor
  1779. * @since JDK1.1
  1780. */
  1781. public Cursor getCursor() {
  1782. Cursor cursor = this.cursor;
  1783. if (cursor != null) {
  1784. return cursor;
  1785. }
  1786. Container parent = this.parent;
  1787. if (parent != null) {
  1788. return parent.getCursor();
  1789. } else {
  1790. return Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
  1791. }
  1792. }
  1793. /**
  1794. * Paints this component. This method is called when the contents
  1795. * of the component should be painted in response to the component
  1796. * first being shown or damage needing repair. The clip rectangle
  1797. * in the Graphics parameter will be set to the area which needs
  1798. * to be painted.
  1799. * For performance reasons, Components with zero width or height
  1800. * aren't considered to need painting when they are first shown,
  1801. * and also aren't considered to need repair.
  1802. * @param g The graphics context to use for painting.
  1803. * @see java.awt.Component#update
  1804. * @since JDK1.0
  1805. */
  1806. public void paint(Graphics g) {
  1807. }
  1808. /**
  1809. * Updates this component.
  1810. * <p>
  1811. * The AWT calls the <code>update</code> method in response to a
  1812. * call to <code>repaint</code. The appearance of the
  1813. * component on the screen has not changed since the last call to
  1814. * <code>update</code> or <code>paint</code>. You can assume that
  1815. * the background is not cleared.
  1816. * <p>
  1817. * The <code>update</code>method of <code>Component</code>
  1818. * does the following:
  1819. * <p>
  1820. * <blockquote><ul>
  1821. * <li>Clears this component by filling it
  1822. * with the background color.
  1823. * <li>Sets the color of the graphics context to be
  1824. * the foreground color of this component.
  1825. * <li>Calls this component's <code>paint</code>
  1826. * method to completely redraw this component.
  1827. * </ul></blockquote>
  1828. * <p>
  1829. * The origin of the graphics context, its
  1830. * (<code>0</code>, <code>0</code>) coordinate point, is the
  1831. * top-left corner of this component. The clipping region of the
  1832. * graphics context is the bounding rectangle of this component.
  1833. * @param g the specified context to use for updating.
  1834. * @see java.awt.Component#paint
  1835. * @see java.awt.Component#repaint()
  1836. * @since JDK1.0
  1837. */
  1838. public void update(Graphics g) {
  1839. if ((this instanceof java.awt.Canvas) ||
  1840. (this instanceof java.awt.Panel) ||
  1841. (this instanceof java.awt.Window)) {
  1842. g.clearRect(0, 0, width, height);
  1843. }
  1844. paint(g);
  1845. }
  1846. /**
  1847. * Paints this component and all of its subcomponents.
  1848. * <p>
  1849. * The origin of the graphics context, its
  1850. * (<code>0</code>, <code>0</code>) coordinate point, is the
  1851. * top-left corner of this component. The clipping region of the
  1852. * graphics context is the bounding rectangle of this component.
  1853. * @param g the graphics context to use for painting.
  1854. * @see java.awt.Component#paint
  1855. * @since JDK1.0
  1856. */
  1857. public void paintAll(Graphics g) {
  1858. if (isShowing()) {
  1859. GraphicsCallback.PeerPaintCallback.getInstance().
  1860. runOneComponent(this, new Rectangle(0, 0, width, height),
  1861. g, g.getClip(),
  1862. GraphicsCallback.LIGHTWEIGHTS |
  1863. GraphicsCallback.HEAVYWEIGHTS);
  1864. }
  1865. }
  1866. /**
  1867. * Simulates the peer callbacks into java.awt for painting of
  1868. * lightweight Components.
  1869. * @param g the graphics context to use for painting.
  1870. * @see #paintAll
  1871. */
  1872. void lightweightPaint(Graphics g) {
  1873. paint(g);
  1874. }
  1875. /**
  1876. * Paints all the heavyweight subcomponents.
  1877. */
  1878. void paintHeavyweightComponents(Graphics g) {
  1879. }
  1880. /**
  1881. * Repaints this component.
  1882. * <p>
  1883. * This method causes a call to this component's <code>update</code>
  1884. * method as soon as possible.
  1885. * @see java.awt.Component#update(java.awt.Graphics)
  1886. * @since JDK1.0
  1887. */
  1888. public void repaint() {
  1889. repaint(0, 0, 0, width, height);
  1890. }
  1891. /**
  1892. * Repaints the component. This will result in a
  1893. * call to <code>update</code> within <em>tm</em> milliseconds.
  1894. * @param tm maximum time in milliseconds before update
  1895. * @see #paint
  1896. * @see java.awt.Component#update(java.awt.Graphics)
  1897. * @since JDK1.0
  1898. */
  1899. public void repaint(long tm) {
  1900. repaint(tm, 0, 0, width, height);
  1901. }
  1902. /**
  1903. * Repaints the specified rectangle of this component.
  1904. * <p>
  1905. * This method causes a call to this component's <code>update</code>
  1906. * method as soon as possible.
  1907. * @param x the <i>x</i> coordinate.
  1908. * @param y the <i>y</i> coordinate.
  1909. * @param width the width.
  1910. * @param height the height.
  1911. * @see java.awt.Component#update(java.awt.Graphics)
  1912. * @since JDK1.0
  1913. */
  1914. public void repaint(int x, int y, int width, int height) {
  1915. repaint(0, x, y, width, height);
  1916. }
  1917. /**
  1918. * Repaints the specified rectangle of this component within
  1919. * <code>tm</code> milliseconds.
  1920. * <p>
  1921. * This method causes a call to this component's
  1922. * <code>update</code> method.
  1923. * @param tm maximum time in milliseconds before update.
  1924. * @param x the <i>x</i> coordinate.
  1925. * @param y the <i>y</i> coordinate.
  1926. * @param width the width.
  1927. * @param height the height.
  1928. * @see java.awt.Component#update(java.awt.Graphics)
  1929. * @since JDK1.0
  1930. */
  1931. public void repaint(long tm, int x, int y, int width, int height) {
  1932. if (this.peer instanceof java.awt.peer.LightweightPeer) {
  1933. // Needs to be translated to parent coordinates since
  1934. // a parent native container provides the actual repaint
  1935. // services. Additionally, the request is restricted to
  1936. // the bounds of the component.
  1937. if (parent != null) {
  1938. int px = this.x + ((x < 0) ? 0 : x);
  1939. int py = this.y + ((y < 0) ? 0 : y);
  1940. int pwidth = (width > this.width) ? this.width : width;
  1941. int pheight = (height > this.height) ? this.height : height;
  1942. parent.repaint(tm, px, py, pwidth, pheight);
  1943. }
  1944. } else {
  1945. if(!isVisible()) {
  1946. return;
  1947. }
  1948. ComponentPeer peer = this.peer;
  1949. if ((peer != null) && (width > 0) && (height > 0)) {
  1950. peer.repaint(tm, x, y, width, height);
  1951. }
  1952. }
  1953. }
  1954. /**
  1955. * Prints this component. Applications should override this method
  1956. * for components that must do special processing before being
  1957. * printed or should be printed differently than they are painted.
  1958. * <p>
  1959. * The default implementation of this method calls the
  1960. * <code>paint</code> method.
  1961. * <p>
  1962. * The origin of the graphics context, its
  1963. * (<code>0</code>, <code>0</code>) coordinate point, is the
  1964. * top-left corner of this component. The clipping region of the
  1965. * graphics context is the bounding rectangle of this component.
  1966. * @param g the graphics context to use for printing.
  1967. * @see java.awt.Component#paint(java.awt.Graphics)
  1968. * @since JDK1.0
  1969. */
  1970. public void print(Graphics g) {
  1971. paint(g);
  1972. }
  1973. /**
  1974. * Prints this component and all of its subcomponents.
  1975. * <p>
  1976. * The origin of the graphics context, its
  1977. * (<code>0</code>, <code>0</code>) coordinate point, is the
  1978. * top-left corner of this component. The clipping region of the
  1979. * graphics context is the bounding rectangle of this component.
  1980. * @param g the graphics context to use for printing.
  1981. * @see java.awt.Component#print(java.awt.Graphics)
  1982. * @since JDK1.0
  1983. */
  1984. public void printAll(Graphics g) {
  1985. if (isShowing()) {
  1986. GraphicsCallback.PeerPrintCallback.getInstance().
  1987. runOneComponent(this, new Rectangle(0, 0, width, height),
  1988. g, g.getClip(),
  1989. GraphicsCallback.LIGHTWEIGHTS |
  1990. GraphicsCallback.HEAVYWEIGHTS);
  1991. }
  1992. }
  1993. /**
  1994. * Simulates the peer callbacks into java.awt for printing of
  1995. * lightweight Components.
  1996. * @param g the graphics context to use for printing.
  1997. * @see #printAll
  1998. */
  1999. void lightweightPrint(Graphics g) {
  2000. print(g);
  2001. }
  2002. /**
  2003. * Prints all the heavyweight subcomponents.
  2004. */
  2005. void printHeavyweightComponents(Graphics g) {
  2006. }
  2007. /**
  2008. * Repaints the component when the image has changed.
  2009. * This <code>imageUpdate</code> method of an <code>ImageObserver</code>
  2010. * is called when more information about an
  2011. * image which had been previously requested using an asynchronous
  2012. * routine such as the <code>drawImage</code> method of
  2013. * <code>Graphics</code> becomes available.
  2014. * See the definition of <code>imageUpdate</code> for
  2015. * more information on this method and its arguments.
  2016. * <p>
  2017. * The <code>imageUpdate</code> method of <code>Component</code>
  2018. * incrementally draws an image on the component as more of the bits
  2019. * of the image are available.
  2020. * <p>
  2021. * If the system property <code>awt.image.incrementalDraw</code>
  2022. * is missing or has the value <code>true</code>, the image is
  2023. * incrementally drawn, If the system property has any other value,
  2024. * then the image is not drawn until it has been completely loaded.
  2025. * <p>
  2026. * Also, if incremental drawing is in effect, the value of the
  2027. * system property <code>awt.image.redrawrate</code> is interpreted
  2028. * as an integer to give the maximum redraw rate, in milliseconds. If
  2029. * the system property is missing or cannot be interpreted as an
  2030. * integer, the redraw rate is once every 100ms.
  2031. * <p>
  2032. * The interpretation of the <code>x</code>, <code>y</code>,
  2033. * <code>width</code>, and <code>height</code> arguments depends on
  2034. * the value of the <code>infoflags</code> argument.
  2035. *
  2036. * @param img the image being observed.
  2037. * @param infoflags see <code>imageUpdate</code> for more information.
  2038. * @param x the <i>x</i> coordinate.
  2039. * @param y the <i>y</i> coordinate.
  2040. * @param w the width.
  2041. * @param h the height.
  2042. * @return <code>false</code> if the infoflags indicate that the
  2043. * image is completely loaded; <code>true</code> otherwise.
  2044. *
  2045. * @see java.awt.image.ImageObserver
  2046. * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, java.awt.Color, java.awt.image.ImageObserver)
  2047. * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  2048. * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int, java.awt.Color, java.awt.image.ImageObserver)
  2049. * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int, java.awt.image.ImageObserver)
  2050. * @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
  2051. * @since JDK1.0
  2052. */
  2053. public boolean imageUpdate(Image img, int infoflags,
  2054. int x, int y, int w, int h) {
  2055. int rate = -1;
  2056. if ((infoflags & (FRAMEBITS|ALLBITS)) != 0) {
  2057. rate = 0;
  2058. } else if ((infoflags & SOMEBITS) != 0) {
  2059. if (isInc) {
  2060. try {
  2061. rate = incRate;
  2062. if (rate < 0)
  2063. rate = 0;
  2064. } catch (Exception e) {
  2065. rate = 100;
  2066. }
  2067. }
  2068. }
  2069. if (rate >= 0) {
  2070. repaint(rate, 0, 0, width, height);
  2071. }
  2072. return (infoflags & (ALLBITS|ABORT)) == 0;
  2073. }
  2074. /**
  2075. * Creates an image from the specified image producer.
  2076. * @param producer the image producer
  2077. * @return the image produced.
  2078. * @since JDK1.0
  2079. */
  2080. public Image createImage(ImageProducer producer) {
  2081. ComponentPeer peer = this.peer;
  2082. if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)) {
  2083. return peer.createImage(producer);
  2084. }
  2085. return getToolkit().createImage(producer);
  2086. }
  2087. /**
  2088. * Creates an off-screen drawable image
  2089. * to be used for double buffering.
  2090. * @param width the specified width.
  2091. * @param height the specified height.
  2092. * @return an off-screen drawable image,
  2093. * which can be used for double buffering.
  2094. * @since JDK1.0
  2095. */
  2096. public Image createImage(int width, int height) {
  2097. ComponentPeer peer = this.peer;
  2098. if (peer instanceof java.awt.peer.LightweightPeer) {
  2099. if (parent != null) { return parent.createImage(width, height); }
  2100. else { return null;}
  2101. } else {
  2102. return (peer != null) ? peer.createImage(width, height) : null;
  2103. }
  2104. }
  2105. /**
  2106. * Prepares an image for rendering on this component. The image
  2107. * data is downloaded asynchronously in another thread and the
  2108. * appropriate screen representation of the image is generated.
  2109. * @param image the <code>Image</code> for which to
  2110. * prepare a screen representation.
  2111. * @param observer the <code>ImageObserver</code> object
  2112. * to be notified as the image is being prepared.
  2113. * @return <code>true</code> if the image has already been fully prepared;
  2114. <code>false</code> otherwise.
  2115. * @since JDK1.0
  2116. */
  2117. public boolean prepareImage(Image image, ImageObserver observer) {
  2118. return prepareImage(image, -1, -1, observer);
  2119. }
  2120. /**
  2121. * Prepares an image for rendering on this component at the
  2122. * specified width and height.
  2123. * <p>
  2124. * The image data is downloaded asynchronously in another thread,
  2125. * and an appropriately scaled screen representation of the image is
  2126. * generated.
  2127. * @param image the instance of <code>Image</code>
  2128. * for which to prepare a screen representation.
  2129. * @param width the width of the desired screen representation.
  2130. * @param height the height of the desired screen representation.
  2131. * @param observer the <code>ImageObserver</code> object
  2132. * to be notified as the image is being prepared.
  2133. * @return <code>true</code> if the image has already been fully prepared;
  2134. <code>false</code> otherwise.
  2135. * @see java.awt.image.ImageObserver
  2136. * @since JDK1.0
  2137. */
  2138. public boolean prepareImage(Image image, int width, int height,
  2139. ImageObserver observer) {
  2140. ComponentPeer peer = this.peer;
  2141. if (peer instanceof java.awt.peer.LightweightPeer) {
  2142. return (parent != null)
  2143. ? parent.prepareImage(image, width, height, observer)
  2144. : getToolkit().prepareImage(image, width, height, observer);
  2145. } else {
  2146. return (peer != null)
  2147. ? peer.prepareImage(image, width, height, observer)
  2148. : getToolkit().prepareImage(image, width, height, observer);
  2149. }
  2150. }
  2151. /**
  2152. * Returns the status of the construction of a screen representation
  2153. * of the specified image.
  2154. * <p>
  2155. * This method does not cause the image to begin loading. An
  2156. * application must use the <code>prepareImage</code> method
  2157. * to force the loading of an image.
  2158. * <p>
  2159. * Information on the flags returned by this method can be found
  2160. * with the discussion of the <code>ImageObserver</code> interface.
  2161. * @param image the <code>Image</code> object whose status
  2162. * is being checked.
  2163. * @param observer the <code>ImageObserver</code>
  2164. * object to be notified as the image is being prepared.
  2165. * @return the bitwise inclusive <b>OR</b> of
  2166. * <code>ImageObserver</code> flags indicating what
  2167. * information about the image is currently available.
  2168. * @see java.awt.Component#prepareImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  2169. * @see java.awt.Toolkit#checkImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  2170. * @see java.awt.image.ImageObserver
  2171. * @since JDK1.0
  2172. */
  2173. public int checkImage(Image image, ImageObserver observer) {
  2174. return checkImage(image, -1, -1, observer);
  2175. }
  2176. /**
  2177. * Returns the status of the construction of a screen representation
  2178. * of the specified image.
  2179. * <p>
  2180. * This method does not cause the image to begin loading. An
  2181. * application must use the <code>prepareImage</code> method
  2182. * to force the loading of an image.
  2183. * <p>
  2184. * The <code>checkImage</code> method of <code>Component</code>
  2185. * calls its peer's <code>checkImage</code> method to calculate
  2186. * the flags. If this component does not yet have a peer, the
  2187. * component's toolkit's <code>checkImage</code> method is called
  2188. * instead.
  2189. * <p>
  2190. * Information on the flags returned by this method can be found
  2191. * with the discussion of the <code>ImageObserver</code> interface.
  2192. * @param image the <code>Image</code> object whose status
  2193. * is being checked.
  2194. * @param width the width of the scaled version
  2195. * whose status is to be checked.
  2196. * @param height the height of the scaled version
  2197. * whose status is to be checked.
  2198. * @param observer the <code>ImageObserver</code> object
  2199. * to be notified as the image is being prepared.
  2200. * @return the bitwise inclusive <b>OR</b> of
  2201. * <code>ImageObserver</code> flags indicating what
  2202. * information about the image is currently available.
  2203. * @see java.awt.Component#prepareImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  2204. * @see java.awt.Toolkit#checkImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  2205. * @see java.awt.image.ImageObserver
  2206. * @since JDK1.0
  2207. */
  2208. public int checkImage(Image image, int width, int height,
  2209. ImageObserver observer) {
  2210. ComponentPeer peer = this.peer;
  2211. if (peer instanceof java.awt.peer.LightweightPeer) {
  2212. return (parent != null)
  2213. ? parent.checkImage(image, width, height, observer)
  2214. : getToolkit().checkImage(image, width, height, observer);
  2215. } else {
  2216. return (peer != null)
  2217. ? peer.checkImage(image, width, height, observer)
  2218. : getToolkit().checkImage(image, width, height, observer);
  2219. }
  2220. }
  2221. /**
  2222. * Checks whether this component "contains" the specified point,
  2223. * where <code>x</code> and <code>y</code> are defined to be
  2224. * relative to the coordinate system of this component.
  2225. * @param x the <i>x</i> coordinate of the point.
  2226. * @param y the <i>y</i> coordinate of the point.
  2227. * @see java.awt.Component#getComponentAt(int, int)
  2228. * @since JDK1.1
  2229. */
  2230. public boolean contains(int x, int y) {
  2231. return inside(x, y);
  2232. }
  2233. /**
  2234. * @deprecated As of JDK version 1.1,
  2235. * replaced by contains(int, int).
  2236. */
  2237. public boolean inside(int x, int y) {
  2238. return (x >= 0) && (x < width) && (y >= 0) && (y < height);
  2239. }
  2240. /**
  2241. * Checks whether this component "contains" the specified point,
  2242. * where the point's <i>x</i> and <i>y</i> coordinates are defined
  2243. * to be relative to the coordinate system of this component.
  2244. * @param p the point.
  2245. * @see java.awt.Component#getComponentAt(java.awt.Point)
  2246. * @since JDK1.1
  2247. */
  2248. public boolean contains(Point p) {
  2249. return contains(p.x, p.y);
  2250. }
  2251. /**
  2252. * Determines if this component or one of its immediate
  2253. * subcomponents contains the (<i>x</i>, <i>y</i>) location,
  2254. * and if so, returns the containing component. This method only
  2255. * looks one level deep. If the point (<i>x</i>, <i>y</i>) is
  2256. * inside a subcomponent that itself has subcomponents, it does not
  2257. * go looking down the subcomponent tree.
  2258. * <p>
  2259. * The <code>locate</code> method of <code>Component</code> simply
  2260. * returns the component itself if the (<i>x</i>, <i>y</i>)
  2261. * coordinate location is inside its bounding box, and <code>null</code>
  2262. * otherwise.
  2263. * @param x the <i>x</i> coordinate.
  2264. * @param y the <i>y</i> coordinate.
  2265. * @return the component or subcomponent that contains the
  2266. * (<i>x</i>, <i>y</i>) location;
  2267. * <code>null</code> if the location
  2268. * is outside this component.
  2269. * @see java.awt.Component#contains(int, int)
  2270. * @since JDK1.0
  2271. */
  2272. public Component getComponentAt(int x, int y) {
  2273. return locate(x, y);
  2274. }
  2275. /**
  2276. * @deprecated As of JDK version 1.1,
  2277. * replaced by getComponentAt(int, int).
  2278. */
  2279. public Component locate(int x, int y) {
  2280. return contains(x, y) ? this : null;
  2281. }
  2282. /**
  2283. * Returns the component or subcomponent that contains the
  2284. * specified point.
  2285. * @param p the point.
  2286. * @see java.awt.Component#contains
  2287. * @since JDK1.1
  2288. */
  2289. public Component getComponentAt(Point p) {
  2290. return getComponentAt(p.x, p.y);
  2291. }
  2292. /**
  2293. * @deprecated As of JDK version 1.1,
  2294. * replaced by <code>dispatchEvent(AWTEvent e)</code>.
  2295. */
  2296. public void deliverEvent(Event e) {
  2297. postEvent(e);
  2298. }
  2299. /**
  2300. * Dispatches an event to this component or one of its sub components.
  2301. * Calls processEvent() before returning for 1.1-style events which
  2302. * have been enabled for the Component.
  2303. * @param e the event
  2304. */
  2305. public final void dispatchEvent(AWTEvent e) {
  2306. dispatchEventImpl(e);
  2307. }
  2308. void dispatchEventImpl(AWTEvent e) {
  2309. int id = e.getID();
  2310. /*
  2311. * 0. Allow the Toolkit to pass this to AWTEventListeners.
  2312. */
  2313. Toolkit toolkit = Toolkit.getDefaultToolkit();
  2314. toolkit.notifyAWTEventListeners(e);
  2315. /*
  2316. * 1. Allow input methods to process the event
  2317. */
  2318. if (areInputMethodsEnabled()
  2319. && (
  2320. // We need to pass on InputMethodEvents since some host
  2321. // input method adapters send them through the Java
  2322. // event queue instead of directly to the component,
  2323. // and the input context also handles the Java composition window
  2324. ((e instanceof InputMethodEvent) && !(this instanceof CompositionArea))
  2325. ||
  2326. // Otherwise, we only pass on input and focus events, because
  2327. // a) input methods shouldn't know about semantic or component-level events
  2328. // b) passing on the events takes time
  2329. // c) isConsumed() is always true for semantic events.
  2330. (e instanceof InputEvent) || (e instanceof FocusEvent))) {
  2331. InputContext inputContext = getInputContext();
  2332. if (inputContext != null) {
  2333. inputContext.dispatchEvent(e);
  2334. if (e.isConsumed()) {
  2335. return;
  2336. }
  2337. }
  2338. }
  2339. /*
  2340. * 2. Pre-process any special events before delivery
  2341. */
  2342. switch(id) {
  2343. // Handling of the PAINT and UPDATE events is now done in the
  2344. // peer's handleEvent() method so the background can be cleared
  2345. // selectively for non-native components on Windows only.
  2346. // - Fred.Ecks@Eng.sun.com, 5-8-98
  2347. case FocusEvent.FOCUS_GAINED:
  2348. if (parent != null && !(this instanceof Window)) {
  2349. parent.setFocusOwner(this);
  2350. }
  2351. break;
  2352. case FocusEvent.FOCUS_LOST:
  2353. break;
  2354. case KeyEvent.KEY_PRESSED:
  2355. case KeyEvent.KEY_RELEASED:
  2356. Container p = (Container)((this instanceof Container) ? this : parent);
  2357. if (p != null) {
  2358. p.preProcessKeyEvent((KeyEvent)e);
  2359. if (e.isConsumed()) {
  2360. return;
  2361. }
  2362. }
  2363. break;
  2364. /*
  2365. case MouseEvent.MOUSE_PRESSED:
  2366. if (isFocusTraversable()) {
  2367. requestFocus();
  2368. }
  2369. break;
  2370. */
  2371. case WindowEvent.WINDOW_CLOSING:
  2372. if (toolkit instanceof WindowClosingListener) {
  2373. windowClosingException = ((WindowClosingListener)
  2374. toolkit).windowClosingNotify((WindowEvent)e);
  2375. if (checkWindowClosingException()) {
  2376. return;
  2377. }
  2378. }
  2379. break;
  2380. default:
  2381. break;
  2382. }
  2383. /*
  2384. * 3. Deliver event for normal processing
  2385. */
  2386. if (newEventsOnly) {
  2387. // Filtering needs to really be moved to happen at a lower
  2388. // level in order to get maximum performance gain; it is
  2389. // here temporarily to ensure the API spec is honored.
  2390. //
  2391. if (eventEnabled(e)) {
  2392. processEvent(e);
  2393. }
  2394. } else if (!(e instanceof MouseEvent && !postsOldMouseEvents())) {
  2395. //
  2396. // backward compatibility
  2397. //
  2398. Event olde = e.convertToOld();
  2399. if (olde != null) {
  2400. int key = olde.key;
  2401. int modifiers = olde.modifiers;
  2402. postEvent(olde);
  2403. if (olde.isConsumed()) {
  2404. e.consume();
  2405. }
  2406. // if target changed key or modifier values, copy them
  2407. // back to original event
  2408. //
  2409. switch(olde.id) {
  2410. case Event.KEY_PRESS:
  2411. case Event.KEY_RELEASE:
  2412. case Event.KEY_ACTION:
  2413. case Event.KEY_ACTION_RELEASE:
  2414. if (olde.key != key) {
  2415. ((KeyEvent)e).setKeyChar(olde.getKeyEventChar());
  2416. }
  2417. if (olde.modifiers != modifiers) {
  2418. ((KeyEvent)e).setModifiers(olde.modifiers);
  2419. }
  2420. break;
  2421. default:
  2422. break;
  2423. }
  2424. }
  2425. }
  2426. /*
  2427. * 4. If no one has consumed a key event, propagate it
  2428. * up the containment hierarchy to ensure that menu shortcuts
  2429. * and keyboard traversal will work properly.
  2430. */
  2431. if (!e.isConsumed()) {
  2432. if (e instanceof java.awt.event.KeyEvent) {
  2433. Container p = (Container)((this instanceof Container) ? this : parent);
  2434. if (p != null) {
  2435. p.postProcessKeyEvent((KeyEvent)e);
  2436. }
  2437. } else {
  2438. switch(id) {
  2439. case WindowEvent.WINDOW_CLOSING:
  2440. if (toolkit instanceof WindowClosingListener) {
  2441. windowClosingException =
  2442. ((WindowClosingListener)toolkit).
  2443. windowClosingDelivered((WindowEvent)e);
  2444. if (checkWindowClosingException()) {
  2445. return;
  2446. }
  2447. }
  2448. break;
  2449. default:
  2450. break;
  2451. }
  2452. }
  2453. }
  2454. /*
  2455. * 5. Allow the peer to process the event
  2456. */
  2457. if (peer != null) {
  2458. peer.handleEvent(e);
  2459. }
  2460. } // dispatchEventImpl()
  2461. boolean checkWindowClosingException() {
  2462. if (windowClosingException != null) {
  2463. if (this instanceof Dialog) {
  2464. ((Dialog)this).interruptBlocking();
  2465. } else {
  2466. windowClosingException.fillInStackTrace();
  2467. windowClosingException.printStackTrace();
  2468. windowClosingException = null;
  2469. }
  2470. return true;
  2471. }
  2472. return false;
  2473. }
  2474. boolean areInputMethodsEnabled() {
  2475. // in 1.2, we assume input method support is required for all
  2476. // components that handle key events, but components can turn off
  2477. // input methods by calling enableInputMethods(false).
  2478. return ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0) &&
  2479. ((eventMask & AWTEvent.KEY_EVENT_MASK) != 0 || keyListener != null);
  2480. }
  2481. // REMIND: remove when filtering is handled at lower level
  2482. boolean eventEnabled(AWTEvent e) {
  2483. switch(e.id) {
  2484. case ComponentEvent.COMPONENT_MOVED:
  2485. case ComponentEvent.COMPONENT_RESIZED:
  2486. case ComponentEvent.COMPONENT_SHOWN:
  2487. case ComponentEvent.COMPONENT_HIDDEN:
  2488. if ((eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
  2489. componentListener != null) {
  2490. return true;
  2491. }
  2492. break;
  2493. case FocusEvent.FOCUS_GAINED:
  2494. case FocusEvent.FOCUS_LOST:
  2495. if ((eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0 ||
  2496. focusListener != null) {
  2497. return true;
  2498. }
  2499. break;
  2500. case KeyEvent.KEY_PRESSED:
  2501. case KeyEvent.KEY_RELEASED:
  2502. case KeyEvent.KEY_TYPED:
  2503. if ((eventMask & AWTEvent.KEY_EVENT_MASK) != 0 ||
  2504. keyListener != null) {
  2505. return true;
  2506. }
  2507. break;
  2508. case MouseEvent.MOUSE_PRESSED:
  2509. case MouseEvent.MOUSE_RELEASED:
  2510. case MouseEvent.MOUSE_ENTERED:
  2511. case MouseEvent.MOUSE_EXITED:
  2512. case MouseEvent.MOUSE_CLICKED:
  2513. if ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0 ||
  2514. mouseListener != null) {
  2515. return true;
  2516. }
  2517. break;
  2518. case MouseEvent.MOUSE_MOVED:
  2519. case MouseEvent.MOUSE_DRAGGED:
  2520. if ((eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0 ||
  2521. mouseMotionListener != null) {
  2522. return true;
  2523. }
  2524. break;
  2525. case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
  2526. case InputMethodEvent.CARET_POSITION_CHANGED:
  2527. if ((eventMask & AWTEvent.INPUT_METHOD_EVENT_MASK) != 0 ||
  2528. inputMethodListener != null) {
  2529. return true;
  2530. }
  2531. break;
  2532. case HierarchyEvent.HIERARCHY_CHANGED:
  2533. if ((eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0 ||
  2534. hierarchyListener != null) {
  2535. return true;
  2536. }
  2537. break;
  2538. case HierarchyEvent.ANCESTOR_MOVED:
  2539. case HierarchyEvent.ANCESTOR_RESIZED:
  2540. if ((eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0 ||
  2541. hierarchyBoundsListener != null) {
  2542. return true;
  2543. }
  2544. break;
  2545. default:
  2546. break;
  2547. }
  2548. //
  2549. // Always pass on events defined by external programs.
  2550. //
  2551. if (e.id > AWTEvent.RESERVED_ID_MAX) {
  2552. return true;
  2553. }
  2554. return false;
  2555. }
  2556. /**
  2557. * Returns the Window subclass that contains this object. Will
  2558. * return the object itself, if it is a window.
  2559. */
  2560. private Window getWindowForObject(Object obj) {
  2561. if (obj instanceof Component) {
  2562. while (obj != null) {
  2563. if (obj instanceof Window) {
  2564. return (Window)obj;
  2565. }
  2566. obj = ((Component)obj).getParent();
  2567. }
  2568. }
  2569. return null;
  2570. } // getWindowForObject()
  2571. /**
  2572. * @deprecated As of JDK version 1.1,
  2573. * replaced by dispatchEvent(AWTEvent).
  2574. */
  2575. public boolean postEvent(Event e) {
  2576. ComponentPeer peer = this.peer;
  2577. if (handleEvent(e)) {
  2578. e.consume();
  2579. return true;
  2580. }
  2581. Component parent = this.parent;
  2582. int eventx = e.x;
  2583. int eventy = e.y;
  2584. if (parent != null) {
  2585. e.translate(x, y);
  2586. if (parent.postEvent(e)) {
  2587. e.consume();
  2588. return true;
  2589. }
  2590. // restore coords
  2591. e.x = eventx;
  2592. e.y = eventy;
  2593. }
  2594. return false;
  2595. }
  2596. // Event source interfaces
  2597. /**
  2598. * Adds the specified component listener to receive component events from
  2599. * this component.
  2600. * If l is null, no exception is thrown and no action is performed.
  2601. * @param l the component listener.
  2602. * @see java.awt.event.ComponentEvent
  2603. * @see java.awt.event.ComponentListener
  2604. * @see java.awt.Component#removeComponentListener
  2605. * @since JDK1.1
  2606. */
  2607. public synchronized void addComponentListener(ComponentListener l) {
  2608. if (l == null) {
  2609. return;
  2610. }
  2611. componentListener = AWTEventMulticaster.add(componentListener, l);
  2612. newEventsOnly = true;
  2613. }
  2614. /**
  2615. * Removes the specified component listener so that it no longer
  2616. * receives component events from this component. This method performs
  2617. * no function, nor does it throw an exception, if the listener
  2618. * specified by the argument was not previously added to this component.
  2619. * If l is null, no exception is thrown and no action is performed.
  2620. * @param l the component listener.
  2621. * @see java.awt.event.ComponentEvent
  2622. * @see java.awt.event.ComponentListener
  2623. * @see java.awt.Component#addComponentListener
  2624. * @since JDK1.1
  2625. */
  2626. public synchronized void removeComponentListener(ComponentListener l) {
  2627. if (l == null) {
  2628. return;
  2629. }
  2630. componentListener = AWTEventMulticaster.remove(componentListener, l);
  2631. }
  2632. /**
  2633. * Adds the specified focus listener to receive focus events from
  2634. * this component when this component gains input focus.
  2635. * If l is null, no exception is thrown and no action is performed.
  2636. *
  2637. * @param l the focus listener.
  2638. * @see java.awt.event.FocusEvent
  2639. * @see java.awt.event.FocusListener
  2640. * @see java.awt.Component#removeFocusListener
  2641. * @since JDK1.1
  2642. */
  2643. public synchronized void addFocusListener(FocusListener l) {
  2644. if (l == null) {
  2645. return;
  2646. }
  2647. focusListener = AWTEventMulticaster.add(focusListener, l);
  2648. newEventsOnly = true;
  2649. // if this is a lightweight component, enable focus events
  2650. // in the native container.
  2651. if (peer instanceof java.awt.peer.LightweightPeer) {
  2652. parent.proxyEnableEvents(AWTEvent.FOCUS_EVENT_MASK);
  2653. }
  2654. }
  2655. /**
  2656. * Removes the specified focus listener so that it no longer
  2657. * receives focus events from this component. This method performs
  2658. * no function, nor does it throw an exception, if the listener
  2659. * specified by the argument was not previously added to this component.
  2660. * If l is null, no exception is thrown and no action is performed.
  2661. *
  2662. * @param l the focus listener.
  2663. * @see java.awt.event.FocusEvent
  2664. * @see java.awt.event.FocusListener
  2665. * @see java.awt.Component#addFocusListener
  2666. * @since JDK1.1
  2667. */
  2668. public synchronized void removeFocusListener(FocusListener l) {
  2669. if (l == null) {
  2670. return;
  2671. }
  2672. focusListener = AWTEventMulticaster.remove(focusListener, l);
  2673. }
  2674. /**
  2675. * Adds the specified hierarchy listener to receive hierarchy changed
  2676. * events from this component when the hierarchy to which this container
  2677. * belongs changes.
  2678. * If l is null, no exception is thrown and no action is performed.
  2679. *
  2680. * @param l the hierarchy listener.
  2681. * @see java.awt.event.HierarchyEvent
  2682. * @see java.awt.event.HierarchyListener
  2683. * @see java.awt.Component#removeHierarchyListener
  2684. * @since 1.3
  2685. */
  2686. public void addHierarchyListener(HierarchyListener l) {
  2687. if (l == null) {
  2688. return;
  2689. }
  2690. boolean notifyAncestors;
  2691. synchronized (this) {
  2692. notifyAncestors =
  2693. (hierarchyListener == null &&
  2694. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) == 0);
  2695. hierarchyListener = AWTEventMulticaster.add(hierarchyListener, l);
  2696. notifyAncestors = (notifyAncestors && hierarchyListener != null);
  2697. newEventsOnly = true;
  2698. }
  2699. if (notifyAncestors) {
  2700. synchronized (getTreeLock()) {
  2701. if (parent != null) {
  2702. parent.adjustListeningChildren(
  2703. AWTEvent.HIERARCHY_EVENT_MASK, 1);
  2704. }
  2705. }
  2706. }
  2707. }
  2708. /**
  2709. * Removes the specified hierarchy listener so that it no longer
  2710. * receives hierarchy changed events from this component. This method
  2711. * performs no function, nor does it throw an exception, if the listener
  2712. * specified by the argument was not previously added to this component.
  2713. * If l is null, no exception is thrown and no action is performed.
  2714. *
  2715. * @param l the hierarchy listener.
  2716. * @see java.awt.event.HierarchyEvent
  2717. * @see java.awt.event.HierarchyListener
  2718. * @see java.awt.Component#addHierarchyListener
  2719. * @since 1.3
  2720. */
  2721. public void removeHierarchyListener(HierarchyListener l) {
  2722. if (l == null) {
  2723. return;
  2724. }
  2725. boolean notifyAncestors;
  2726. synchronized (this) {
  2727. notifyAncestors =
  2728. (hierarchyListener != null &&
  2729. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) == 0);
  2730. hierarchyListener =
  2731. AWTEventMulticaster.remove(hierarchyListener, l);
  2732. notifyAncestors = (notifyAncestors && hierarchyListener == null);
  2733. }
  2734. if (notifyAncestors) {
  2735. synchronized (getTreeLock()) {
  2736. if (parent != null) {
  2737. parent.adjustListeningChildren(
  2738. AWTEvent.HIERARCHY_EVENT_MASK, -1);
  2739. }
  2740. }
  2741. }
  2742. }
  2743. /**
  2744. * Adds the specified hierarchy bounds listener to receive hierarchy
  2745. * bounds events from this component when the hierarchy to which this
  2746. * container belongs changes.
  2747. * If l is null, no exception is thrown and no action is performed.
  2748. *
  2749. * @param l the hierarchy bounds listener.
  2750. * @see java.awt.event.HierarchyEvent
  2751. * @see java.awt.event.HierarchyBoundsListener
  2752. * @see java.awt.Component#removeHierarchyBoundsListener
  2753. * @since 1.3
  2754. */
  2755. public void addHierarchyBoundsListener(HierarchyBoundsListener l) {
  2756. if (l == null) {
  2757. return;
  2758. }
  2759. boolean notifyAncestors;
  2760. synchronized (this) {
  2761. notifyAncestors =
  2762. (hierarchyBoundsListener == null &&
  2763. (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) == 0);
  2764. hierarchyBoundsListener =
  2765. AWTEventMulticaster.add(hierarchyBoundsListener, l);
  2766. notifyAncestors = (notifyAncestors &&
  2767. hierarchyBoundsListener != null);
  2768. newEventsOnly = true;
  2769. }
  2770. if (notifyAncestors) {
  2771. synchronized (getTreeLock()) {
  2772. if (parent != null) {
  2773. parent.adjustListeningChildren(
  2774. AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, 1);
  2775. }
  2776. }
  2777. }
  2778. }
  2779. /**
  2780. * Removes the specified hierarchy bounds listener so that it no longer
  2781. * receives hierarchy bounds events from this component. This method
  2782. * performs no function, nor does it throw an exception, if the listener
  2783. * specified by the argument was not previously added to this component.
  2784. * If l is null, no exception is thrown and no action is performed.
  2785. *
  2786. * @param l the hierarchy bounds listener.
  2787. * @see java.awt.event.HierarchyEvent
  2788. * @see java.awt.event.HierarchyBoundsListener
  2789. * @see java.awt.Component#addHierarchyBoundsListener
  2790. * @since 1.3
  2791. */
  2792. public void removeHierarchyBoundsListener(HierarchyBoundsListener l) {
  2793. if (l == null) {
  2794. return;
  2795. }
  2796. boolean notifyAncestors;
  2797. synchronized (this) {
  2798. notifyAncestors =
  2799. (hierarchyBoundsListener != null &&
  2800. (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) == 0);
  2801. hierarchyBoundsListener =
  2802. AWTEventMulticaster.remove(hierarchyBoundsListener, l);
  2803. notifyAncestors = (notifyAncestors &&
  2804. hierarchyBoundsListener == null);
  2805. }
  2806. if (notifyAncestors) {
  2807. synchronized (getTreeLock()) {
  2808. if (parent != null) {
  2809. parent.adjustListeningChildren(
  2810. AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, -1);
  2811. }
  2812. }
  2813. }
  2814. }
  2815. // Should only be called while holding the tree lock
  2816. int numListening(long mask) {
  2817. if (dbg.on) {
  2818. // One mask or the other, but not neither or both.
  2819. dbg.assert(mask == AWTEvent.HIERARCHY_EVENT_MASK ||
  2820. mask == AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK);
  2821. }
  2822. if ((mask == AWTEvent.HIERARCHY_EVENT_MASK &&
  2823. (hierarchyListener != null ||
  2824. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0)) ||
  2825. (mask == AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK &&
  2826. (hierarchyBoundsListener != null ||
  2827. (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0))) {
  2828. return 1;
  2829. } else {
  2830. return 0;
  2831. }
  2832. }
  2833. // Should only be called while holding the tree lock
  2834. int createHierarchyEvents(int id, Component changed,
  2835. Container changedParent, long changeFlags) {
  2836. switch (id) {
  2837. case HierarchyEvent.HIERARCHY_CHANGED:
  2838. if (hierarchyListener != null ||
  2839. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0) {
  2840. HierarchyEvent e = new HierarchyEvent(this, id, changed,
  2841. changedParent,
  2842. changeFlags);
  2843. dispatchEvent(e);
  2844. return 1;
  2845. }
  2846. break;
  2847. case HierarchyEvent.ANCESTOR_MOVED:
  2848. case HierarchyEvent.ANCESTOR_RESIZED:
  2849. if (dbg.on) {
  2850. dbg.assert(changeFlags == 0);
  2851. }
  2852. if (hierarchyBoundsListener != null ||
  2853. (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0) {
  2854. HierarchyEvent e = new HierarchyEvent(this, id, changed,
  2855. changedParent);
  2856. dispatchEvent(e);
  2857. return 1;
  2858. }
  2859. break;
  2860. default:
  2861. if (dbg.on) {
  2862. dbg.assert(false);
  2863. }
  2864. break;
  2865. }
  2866. return 0;
  2867. }
  2868. // Since a Component has no children, this funciton does nothing
  2869. void createChildHierarchyEvents(int id, long changeFlags) {}
  2870. /**
  2871. * Adds the specified key listener to receive key events from
  2872. * this component.
  2873. * If l is null, no exception is thrown and no action is performed.
  2874. *
  2875. * @param l the key listener.
  2876. * @see java.awt.event.KeyEvent
  2877. * @see java.awt.event.KeyListener
  2878. * @see java.awt.Component#removeKeyListener
  2879. * @since JDK1.1
  2880. */
  2881. public synchronized void addKeyListener(KeyListener l) {
  2882. if (l == null) {
  2883. return;
  2884. }
  2885. keyListener = AWTEventMulticaster.add(keyListener, l);
  2886. newEventsOnly = true;
  2887. // if this is a lightweight component, enable key events
  2888. // in the native container.
  2889. if (peer instanceof java.awt.peer.LightweightPeer) {
  2890. parent.proxyEnableEvents(AWTEvent.KEY_EVENT_MASK);
  2891. }
  2892. }
  2893. /**
  2894. * Removes the specified key listener so that it no longer
  2895. * receives key events from this component. This method performs
  2896. * no function, nor does it throw an exception, if the listener
  2897. * specified by the argument was not previously added to this component.
  2898. * If l is null, no exception is thrown and no action is performed.
  2899. *
  2900. * @param l the key listener.
  2901. * @see java.awt.event.KeyEvent
  2902. * @see java.awt.event.KeyListener
  2903. * @see java.awt.Component#addKeyListener
  2904. * @since JDK1.1
  2905. */
  2906. public synchronized void removeKeyListener(KeyListener l) {
  2907. if (l == null) {
  2908. return;
  2909. }
  2910. keyListener = AWTEventMulticaster.remove(keyListener, l);
  2911. }
  2912. /**
  2913. * Adds the specified mouse listener to receive mouse events from
  2914. * this component.
  2915. * If l is null, no exception is thrown and no action is performed.
  2916. *
  2917. * @param l the mouse listener.
  2918. * @see java.awt.event.MouseEvent
  2919. * @see java.awt.event.MouseListener
  2920. * @see java.awt.Component#removeMouseListener
  2921. * @since JDK1.1
  2922. */
  2923. public synchronized void addMouseListener(MouseListener l) {
  2924. if (l == null) {
  2925. return;
  2926. }
  2927. mouseListener = AWTEventMulticaster.add(mouseListener,l);
  2928. newEventsOnly = true;
  2929. // if this is a lightweight component, enable mouse events
  2930. // in the native container.
  2931. if (peer instanceof java.awt.peer.LightweightPeer) {
  2932. parent.proxyEnableEvents(AWTEvent.MOUSE_EVENT_MASK);
  2933. }
  2934. }
  2935. /**
  2936. * Removes the specified mouse listener so that it no longer
  2937. * receives mouse events from this component. This method performs
  2938. * no function, nor does it throw an exception, if the listener
  2939. * specified by the argument was not previously added to this component.
  2940. * If l is null, no exception is thrown and no action is performed.
  2941. *
  2942. * @param l the mouse listener.
  2943. * @see java.awt.event.MouseEvent
  2944. * @see java.awt.event.MouseListener
  2945. * @see java.awt.Component#addMouseListener
  2946. * @since JDK1.1
  2947. */
  2948. public synchronized void removeMouseListener(MouseListener l) {
  2949. if (l == null) {
  2950. return;
  2951. }
  2952. mouseListener = AWTEventMulticaster.remove(mouseListener, l);
  2953. }
  2954. /**
  2955. * Adds the specified mouse motion listener to receive mouse motion events from
  2956. * this component.
  2957. * If l is null, no exception is thrown and no action is performed.
  2958. *
  2959. * @param l the mouse motion listener.
  2960. * @see java.awt.event.MouseMotionEvent
  2961. * @see java.awt.event.MouseMotionListener
  2962. * @see java.awt.Component#removeMouseMotionListener
  2963. * @since JDK1.1
  2964. */
  2965. public synchronized void addMouseMotionListener(MouseMotionListener l) {
  2966. if (l == null) {
  2967. return;
  2968. }
  2969. mouseMotionListener = AWTEventMulticaster.add(mouseMotionListener,l);
  2970. newEventsOnly = true;
  2971. // if this is a lightweight component, enable mouse events
  2972. // in the native container.
  2973. if (peer instanceof java.awt.peer.LightweightPeer) {
  2974. parent.proxyEnableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
  2975. }
  2976. }
  2977. /**
  2978. * Removes the specified mouse motion listener so that it no longer
  2979. * receives mouse motion events from this component. This method performs
  2980. * no function, nor does it throw an exception, if the listener
  2981. * specified by the argument was not previously added to this component.
  2982. * If l is null, no exception is thrown and no action is performed.
  2983. *
  2984. * @param l the mouse motion listener.
  2985. * @see java.awt.event.MouseMotionEvent
  2986. * @see java.awt.event.MouseMotionListener
  2987. * @see java.awt.Component#addMouseMotionListener
  2988. * @since JDK1.1
  2989. */
  2990. public synchronized void removeMouseMotionListener(MouseMotionListener l) {
  2991. if (l == null) {
  2992. return;
  2993. }
  2994. mouseMotionListener = AWTEventMulticaster.remove(mouseMotionListener, l);
  2995. }
  2996. /**
  2997. * Adds the specified input method listener to receive
  2998. * input method events from this component. A component will
  2999. * only receive input method events from input methods
  3000. * if it also overrides getInputMethodRequests to return an
  3001. * InputMethodRequests instance.
  3002. * If l is null, no exception is thrown and no action is performed.
  3003. *
  3004. * @param l the input method listener.
  3005. * @see java.awt.event.InputMethodEvent
  3006. * @see java.awt.event.InputMethodListener
  3007. * @see java.awt.Component#removeInputMethodListener
  3008. * @see java.awt.Component#getInputMethodRequests
  3009. * @since 1.2
  3010. */
  3011. public synchronized void addInputMethodListener(InputMethodListener l) {
  3012. if (l == null) {
  3013. return;
  3014. }
  3015. inputMethodListener = AWTEventMulticaster.add(inputMethodListener, l);
  3016. newEventsOnly = true;
  3017. }
  3018. /**
  3019. * Removes the specified input method listener so that it no longer receives
  3020. * input method events from this component. This method performs
  3021. * no function, nor does it throw an exception, if the listener
  3022. * specified by the argument was not previously added to this component.
  3023. * If l is null, no exception is thrown and no action is performed.
  3024. *
  3025. * @param l the input method listener.
  3026. * @see java.awt.event.InputMethodEvent
  3027. * @see java.awt.event.InputMethodListener
  3028. * @see java.awt.Component#addInputMethodListener
  3029. * @since 1.2
  3030. */
  3031. public synchronized void removeInputMethodListener(InputMethodListener l) {
  3032. if (l == null) {
  3033. return;
  3034. }
  3035. inputMethodListener = AWTEventMulticaster.remove(inputMethodListener, l);
  3036. }
  3037. /**
  3038. * Return an array of all the listeners that were added to the Component
  3039. * with addXXXListener(), where XXX is the name of the <code>listenerType</code>
  3040. * argument. For example, to get all of the MouseListeners for the
  3041. * given Component <code>c</code>, one would write:
  3042. * <pre>
  3043. * MouseListener[] mls = (MouseListener[])(c.getListeners(MouseListener.class))
  3044. * </pre>
  3045. * If no such listener list exists, then an empty array is returned.
  3046. *
  3047. * @param listenerType Type of listeners requested. This parameter must be
  3048. * a <tt>java.util.EventListener</tt> or subclass.
  3049. *
  3050. * @returns an array of all listeners added to this Component using
  3051. * addXXXListener, or an empty array if no such
  3052. * listeners have been added to this Component.
  3053. *
  3054. * @throws <tt>ClassCastException</tt> if the <tt>listenerType</tt>
  3055. * parameter is not a <tt>java.util.EventListener</tt> or subclass.
  3056. *
  3057. * @since 1.3
  3058. */
  3059. public EventListener[] getListeners(Class listenerType) {
  3060. EventListener l = null;
  3061. if (listenerType == ComponentListener.class) {
  3062. l = componentListener;
  3063. } else if (listenerType == FocusListener.class) {
  3064. l = focusListener;
  3065. } else if (listenerType == HierarchyListener.class) {
  3066. l = hierarchyListener;
  3067. } else if (listenerType == HierarchyBoundsListener.class) {
  3068. l = hierarchyBoundsListener;
  3069. } else if (listenerType == KeyListener.class) {
  3070. l = keyListener;
  3071. } else if (listenerType == MouseListener.class) {
  3072. l = mouseListener;
  3073. } else if (listenerType == MouseMotionListener.class) {
  3074. l = mouseMotionListener;
  3075. } else if (listenerType == InputMethodListener.class) {
  3076. l = inputMethodListener;
  3077. }
  3078. return AWTEventMulticaster.getListeners(l, listenerType);
  3079. }
  3080. /**
  3081. * Gets the input method request handler which supports
  3082. * requests from input methods for this component. A component
  3083. * that supports on-the-spot text input must override this
  3084. * method to return an InputMethodRequests instance. At the same
  3085. * time, it also has to handle input method events.
  3086. *
  3087. * @return the input method request handler for this component,
  3088. * null by default.
  3089. * @see #addInputMethodListener
  3090. * @since 1.2
  3091. */
  3092. public InputMethodRequests getInputMethodRequests() {
  3093. return null;
  3094. }
  3095. /**
  3096. * Gets the input context used by this component for handling the communication
  3097. * with input methods when text is entered in this component. By default, the
  3098. * input context used for the parent component is returned. Components may
  3099. * override this to return a private input context.
  3100. *
  3101. * @return The input context used by this component. Null if no context can
  3102. * be determined.
  3103. * @since 1.2
  3104. */
  3105. public InputContext getInputContext() {
  3106. Container parent = this.parent;
  3107. if (parent == null) {
  3108. return null;
  3109. } else {
  3110. return parent.getInputContext();
  3111. }
  3112. }
  3113. /**
  3114. * Enables the events defined by the specified event mask parameter
  3115. * to be delivered to this component.
  3116. * <p>
  3117. * Event types are automatically enabled when a listener for
  3118. * that event type is added to the component.
  3119. * <p>
  3120. * This method only needs to be invoked by subclasses of
  3121. * <code>Component</code> which desire to have the specified event
  3122. * types delivered to <code>processEvent</code> regardless of whether
  3123. * or not a listener is registered.
  3124. * @param eventsToEnable the event mask defining the event types.
  3125. * @see java.awt.Component#processEvent
  3126. * @see java.awt.Component#disableEvents
  3127. * @since JDK1.1
  3128. */
  3129. protected final void enableEvents(long eventsToEnable) {
  3130. long notifyAncestors = 0;
  3131. synchronized (this) {
  3132. if ((eventsToEnable & AWTEvent.HIERARCHY_EVENT_MASK) != 0 &&
  3133. hierarchyListener == null &&
  3134. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) == 0) {
  3135. notifyAncestors |= AWTEvent.HIERARCHY_EVENT_MASK;
  3136. }
  3137. if ((eventsToEnable & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0 &&
  3138. hierarchyBoundsListener == null &&
  3139. (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) == 0) {
  3140. notifyAncestors |= AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK;
  3141. }
  3142. eventMask |= eventsToEnable;
  3143. newEventsOnly = true;
  3144. }
  3145. // if this is a lightweight component, enable mouse events
  3146. // in the native container.
  3147. if (peer instanceof java.awt.peer.LightweightPeer) {
  3148. parent.proxyEnableEvents(eventMask);
  3149. }
  3150. if (notifyAncestors != 0) {
  3151. synchronized (getTreeLock()) {
  3152. if (parent != null) {
  3153. parent.adjustListeningChildren(notifyAncestors, 1);
  3154. }
  3155. }
  3156. }
  3157. }
  3158. /**
  3159. * Disables the events defined by the specified event mask parameter
  3160. * from being delivered to this component.
  3161. * @param eventsToDisable the event mask defining the event types
  3162. * @see java.awt.Component#enableEvents
  3163. * @since JDK1.1
  3164. */
  3165. protected final void disableEvents(long eventsToDisable) {
  3166. long notifyAncestors = 0;
  3167. synchronized (this) {
  3168. if ((eventsToDisable & AWTEvent.HIERARCHY_EVENT_MASK) != 0 &&
  3169. hierarchyListener == null &&
  3170. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0) {
  3171. notifyAncestors |= AWTEvent.HIERARCHY_EVENT_MASK;
  3172. }
  3173. if ((eventsToDisable & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK)!=0 &&
  3174. hierarchyBoundsListener == null &&
  3175. (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0) {
  3176. notifyAncestors |= AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK;
  3177. }
  3178. eventMask &= ~eventsToDisable;
  3179. }
  3180. if (notifyAncestors != 0) {
  3181. synchronized (getTreeLock()) {
  3182. if (parent != null) {
  3183. parent.adjustListeningChildren(notifyAncestors, -1);
  3184. }
  3185. }
  3186. }
  3187. }
  3188. /**
  3189. * Potentially coalesce an event being posted with an existing
  3190. * event. This method is called by EventQueue.postEvent if an
  3191. * event with the same ID as the event to be posted is found in
  3192. * the queue (both events must have this component as their source).
  3193. * This method either returns a coalesced event which replaces
  3194. * the existing event (and the new event is then discarded), or
  3195. * null to indicate that no combining should be done (add the
  3196. * second event to the end of the queue). Either event parameter
  3197. * may be modified and returned, as the other one is discarded
  3198. * unless null is returned.
  3199. * <p>
  3200. * This implementation of coalesceEvents coalesces two event types:
  3201. * mouse move (and drag) events, and paint (and update) events.
  3202. * For mouse move events the last event is always returned, causing
  3203. * intermediate moves to be discarded. For paint events, the new
  3204. * event is coalesced into a complex RepaintArea in the peer. The
  3205. * new Event is always returned.
  3206. *
  3207. * @param existingEvent the event already on the EventQueue.
  3208. * @param newEvent the event being posted to the EventQueue.
  3209. * @return a coalesced event, or null indicating that no coalescing
  3210. * was done.
  3211. */
  3212. protected AWTEvent coalesceEvents(AWTEvent existingEvent,
  3213. AWTEvent newEvent) {
  3214. int id = existingEvent.getID();
  3215. if (dbg.on) {
  3216. dbg.assert(id == newEvent.getID() &&
  3217. existingEvent.getSource().equals(newEvent.getSource()));
  3218. }
  3219. switch (id) {
  3220. case Event.MOUSE_MOVE:
  3221. case Event.MOUSE_DRAG: {
  3222. MouseEvent e = (MouseEvent)existingEvent;
  3223. if (e.getModifiers() == ((MouseEvent)newEvent).getModifiers()) {
  3224. // Just return the newEvent, causing the old to be
  3225. // discarded.
  3226. return newEvent;
  3227. }
  3228. break;
  3229. }
  3230. case PaintEvent.PAINT:
  3231. case PaintEvent.UPDATE: {
  3232. // We now use non-rectangular clip regions, so all heavyweight paint
  3233. // events are coalesced. We just union the update rectangle for the
  3234. // paint event with the updateArea.
  3235. if (peer != null) {
  3236. peer.coalescePaintEvent((PaintEvent)newEvent);
  3237. if (dbg.on) {
  3238. Rectangle newrect = ((PaintEvent)newEvent).getUpdateRect();
  3239. dbg.println("Component::coalesceEvents : newEvent : Peered : x = " +
  3240. newrect.x + " y = " + newrect.y + " width = " + newrect.width +
  3241. " height = " + newrect.height);
  3242. }
  3243. return newEvent;
  3244. } else {
  3245. // This approach to coalescing paint events seems to be
  3246. // better than any heuristic for unioning rectangles.
  3247. PaintEvent existingPaintEvent = (PaintEvent) existingEvent;
  3248. PaintEvent newPaintEvent = (PaintEvent) newEvent;
  3249. Rectangle existingRect = existingPaintEvent.getUpdateRect();
  3250. Rectangle newRect = newPaintEvent.getUpdateRect();
  3251. if (dbg.on) {
  3252. dbg.println("Component::coalesceEvents : newEvent : nullPeer : x = " +
  3253. newRect.x + " y = " + newRect.y + " width = " + newRect.width +
  3254. " height = " + newRect.height);
  3255. }
  3256. if (existingRect.contains(newRect)) {
  3257. return existingEvent;
  3258. }
  3259. if (newRect.contains(existingRect)) {
  3260. return newEvent;
  3261. }
  3262. break;
  3263. }
  3264. }
  3265. }
  3266. return null;
  3267. }
  3268. /**
  3269. * Processes events occurring on this component. By default this
  3270. * method calls the appropriate
  3271. * <code>process<event type>Event</code>
  3272. * method for the given class of event.
  3273. * @param e the event.
  3274. * @see java.awt.Component#processComponentEvent
  3275. * @see java.awt.Component#processFocusEvent
  3276. * @see java.awt.Component#processKeyEvent
  3277. * @see java.awt.Component#processMouseEvent
  3278. * @see java.awt.Component#processMouseMotionEvent
  3279. * @see java.awt.Component#processInputMethodEvent
  3280. * @see java.awt.Component#processHierarchyEvent
  3281. * @since JDK1.1
  3282. */
  3283. protected void processEvent(AWTEvent e) {
  3284. if (e instanceof FocusEvent) {
  3285. processFocusEvent((FocusEvent)e);
  3286. } else if (e instanceof MouseEvent) {
  3287. switch(e.getID()) {
  3288. case MouseEvent.MOUSE_PRESSED:
  3289. case MouseEvent.MOUSE_RELEASED:
  3290. case MouseEvent.MOUSE_CLICKED:
  3291. case MouseEvent.MOUSE_ENTERED:
  3292. case MouseEvent.MOUSE_EXITED:
  3293. processMouseEvent((MouseEvent)e);
  3294. break;
  3295. case MouseEvent.MOUSE_MOVED:
  3296. case MouseEvent.MOUSE_DRAGGED:
  3297. processMouseMotionEvent((MouseEvent)e);
  3298. break;
  3299. }
  3300. } else if (e instanceof KeyEvent) {
  3301. processKeyEvent((KeyEvent)e);
  3302. } else if (e instanceof ComponentEvent) {
  3303. processComponentEvent((ComponentEvent)e);
  3304. } else if (e instanceof InputMethodEvent) {
  3305. processInputMethodEvent((InputMethodEvent)e);
  3306. } else if (e instanceof HierarchyEvent) {
  3307. switch (e.getID()) {
  3308. case HierarchyEvent.HIERARCHY_CHANGED:
  3309. processHierarchyEvent((HierarchyEvent)e);
  3310. break;
  3311. case HierarchyEvent.ANCESTOR_MOVED:
  3312. case HierarchyEvent.ANCESTOR_RESIZED:
  3313. processHierarchyBoundsEvent((HierarchyEvent)e);
  3314. break;
  3315. }
  3316. }
  3317. }
  3318. /**
  3319. * Processes component events occurring on this component by
  3320. * dispatching them to any registered
  3321. * <code>ComponentListener</code> objects.
  3322. * <p>
  3323. * This method is not called unless component events are
  3324. * enabled for this component. Component events are enabled
  3325. * when one of the following occurs:
  3326. * <p><ul>
  3327. * <li>A <code>ComponentListener</code> object is registered
  3328. * via <code>addComponentListener</code>.
  3329. * <li>Component events are enabled via <code>enableEvents</code>.
  3330. * </ul>
  3331. * @param e the component event.
  3332. * @see java.awt.event.ComponentEvent
  3333. * @see java.awt.event.ComponentListener
  3334. * @see java.awt.Component#addComponentListener
  3335. * @see java.awt.Component#enableEvents
  3336. * @since JDK1.1
  3337. */
  3338. protected void processComponentEvent(ComponentEvent e) {
  3339. ComponentListener listener = componentListener;
  3340. if (listener != null) {
  3341. int id = e.getID();
  3342. switch(id) {
  3343. case ComponentEvent.COMPONENT_RESIZED:
  3344. listener.componentResized(e);
  3345. break;
  3346. case ComponentEvent.COMPONENT_MOVED:
  3347. listener.componentMoved(e);
  3348. break;
  3349. case ComponentEvent.COMPONENT_SHOWN:
  3350. listener.componentShown(e);
  3351. break;
  3352. case ComponentEvent.COMPONENT_HIDDEN:
  3353. listener.componentHidden(e);
  3354. break;
  3355. }
  3356. }
  3357. }
  3358. /**
  3359. * Processes focus events occurring on this component by
  3360. * dispatching them to any registered
  3361. * <code>FocusListener</code> objects.
  3362. * <p>
  3363. * This method is not called unless focus events are
  3364. * enabled for this component. Focus events are enabled
  3365. * when one of the following occurs:
  3366. * <p><ul>
  3367. * <li>A <code>FocusListener</code> object is registered
  3368. * via <code>addFocusListener</code>.
  3369. * <li>Focus events are enabled via <code>enableEvents</code>.
  3370. * </ul>
  3371. * @param e the focus event.
  3372. * @see java.awt.event.FocusEvent
  3373. * @see java.awt.event.FocusListener
  3374. * @see java.awt.Component#addFocusListener
  3375. * @see java.awt.Component#enableEvents
  3376. * @since JDK1.1
  3377. */
  3378. protected void processFocusEvent(FocusEvent e) {
  3379. FocusListener listener = focusListener;
  3380. if (listener != null) {
  3381. int id = e.getID();
  3382. switch(id) {
  3383. case FocusEvent.FOCUS_GAINED:
  3384. listener.focusGained(e);
  3385. break;
  3386. case FocusEvent.FOCUS_LOST:
  3387. listener.focusLost(e);
  3388. break;
  3389. }
  3390. }
  3391. }
  3392. /**
  3393. * Processes key events occurring on this component by
  3394. * dispatching them to any registered
  3395. * <code>KeyListener</code> objects.
  3396. * <p>
  3397. * This method is not called unless key events are
  3398. * enabled for this component. Key events are enabled
  3399. * when one of the following occurs:
  3400. * <p><ul>
  3401. * <li>A <code>KeyListener</code> object is registered
  3402. * via <code>addKeyListener</code>.
  3403. * <li>Key events are enabled via <code>enableEvents</code>.
  3404. * </ul>
  3405. * @param e the key event.
  3406. * @see java.awt.event.KeyEvent
  3407. * @see java.awt.event.KeyListener
  3408. * @see java.awt.Component#addKeyListener
  3409. * @see java.awt.Component#enableEvents
  3410. * @since JDK1.1
  3411. */
  3412. protected void processKeyEvent(KeyEvent e) {
  3413. KeyListener listener = keyListener;
  3414. if (listener != null) {
  3415. int id = e.getID();
  3416. switch(id) {
  3417. case KeyEvent.KEY_TYPED:
  3418. listener.keyTyped(e);
  3419. break;
  3420. case KeyEvent.KEY_PRESSED:
  3421. listener.keyPressed(e);
  3422. break;
  3423. case KeyEvent.KEY_RELEASED:
  3424. listener.keyReleased(e);
  3425. break;
  3426. }
  3427. }
  3428. }
  3429. /**
  3430. * Processes mouse events occurring on this component by
  3431. * dispatching them to any registered
  3432. * <code>MouseListener</code> objects.
  3433. * <p>
  3434. * This method is not called unless mouse events are
  3435. * enabled for this component. Mouse events are enabled
  3436. * when one of the following occurs:
  3437. * <p><ul>
  3438. * <li>A <code>MouseListener</code> object is registered
  3439. * via <code>addMouseListener</code>.
  3440. * <li>Mouse events are enabled via <code>enableEvents</code>.
  3441. * </ul>
  3442. * @param e the mouse event.
  3443. * @see java.awt.event.MouseEvent
  3444. * @see java.awt.event.MouseListener
  3445. * @see java.awt.Component#addMouseListener
  3446. * @see java.awt.Component#enableEvents
  3447. * @since JDK1.1
  3448. */
  3449. protected void processMouseEvent(MouseEvent e) {
  3450. MouseListener listener = mouseListener;
  3451. if (listener != null) {
  3452. int id = e.getID();
  3453. switch(id) {
  3454. case MouseEvent.MOUSE_PRESSED:
  3455. listener.mousePressed(e);
  3456. break;
  3457. case MouseEvent.MOUSE_RELEASED:
  3458. listener.mouseReleased(e);
  3459. break;
  3460. case MouseEvent.MOUSE_CLICKED:
  3461. listener.mouseClicked(e);
  3462. break;
  3463. case MouseEvent.MOUSE_EXITED:
  3464. listener.mouseExited(e);
  3465. break;
  3466. case MouseEvent.MOUSE_ENTERED:
  3467. listener.mouseEntered(e);
  3468. break;
  3469. }
  3470. }
  3471. }
  3472. /**
  3473. * Processes mouse motion events occurring on this component by
  3474. * dispatching them to any registered
  3475. * <code>MouseMotionListener</code> objects.
  3476. * <p>
  3477. * This method is not called unless mouse motion events are
  3478. * enabled for this component. Mouse motion events are enabled
  3479. * when one of the following occurs:
  3480. * <p><ul>
  3481. * <li>A <code>MouseMotionListener</code> object is registered
  3482. * via <code>addMouseMotionListener</code>.
  3483. * <li>Mouse motion events are enabled via <code>enableEvents</code>.
  3484. * </ul>
  3485. * @param e the mouse motion event.
  3486. * @see java.awt.event.MouseMotionEvent
  3487. * @see java.awt.event.MouseMotionListener
  3488. * @see java.awt.Component#addMouseMotionListener
  3489. * @see java.awt.Component#enableEvents
  3490. * @since JDK1.1
  3491. */
  3492. protected void processMouseMotionEvent(MouseEvent e) {
  3493. MouseMotionListener listener = mouseMotionListener;
  3494. if (listener != null) {
  3495. int id = e.getID();
  3496. switch(id) {
  3497. case MouseEvent.MOUSE_MOVED:
  3498. listener.mouseMoved(e);
  3499. break;
  3500. case MouseEvent.MOUSE_DRAGGED:
  3501. listener.mouseDragged(e);
  3502. break;
  3503. }
  3504. }
  3505. }
  3506. boolean postsOldMouseEvents() {
  3507. return false;
  3508. }
  3509. /**
  3510. * Processes input method events occurring on this component by
  3511. * dispatching them to any registered
  3512. * <code>InputMethodListener</code> objects.
  3513. * <p>
  3514. * This method is not called unless input method events
  3515. * are enabled for this component. Input method events are enabled
  3516. * when one of the following occurs:
  3517. * <p><ul>
  3518. * <li>An <code>InputMethodListener</code> object is registered
  3519. * via <code>addInputMethodListener</code>.
  3520. * <li>Input method events are enabled via <code>enableEvents</code>.
  3521. * </ul>
  3522. * @param e the input method event
  3523. * @see java.awt.event.InputMethodEvent
  3524. * @see java.awt.event.InputMethodListener
  3525. * @see java.awt.Component#addInputMethodListener
  3526. * @see java.awt.Component#enableEvents
  3527. * @since 1.2
  3528. */
  3529. protected void processInputMethodEvent(InputMethodEvent e) {
  3530. InputMethodListener listener = inputMethodListener;
  3531. if (listener != null) {
  3532. int id = e.getID();
  3533. switch (id) {
  3534. case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
  3535. listener.inputMethodTextChanged(e);
  3536. break;
  3537. case InputMethodEvent.CARET_POSITION_CHANGED:
  3538. listener.caretPositionChanged(e);
  3539. break;
  3540. }
  3541. }
  3542. }
  3543. /**
  3544. * Processes hierarchy events occurring on this component by
  3545. * dispatching them to any registered
  3546. * <code>HierarchyListener</code> objects.
  3547. * <p>
  3548. * This method is not called unless hierarchy events
  3549. * are enabled for this component. Hierarchy events are enabled
  3550. * when one of the following occurs:
  3551. * <p><ul>
  3552. * <li>An <code>HierarchyListener</code> object is registered
  3553. * via <code>addHierarchyListener</code>.
  3554. * <li>Hierarchy events are enabled via <code>enableEvents</code>.
  3555. * </ul>
  3556. * @param e the hierarchy event
  3557. * @see java.awt.event.HierarchyEvent
  3558. * @see java.awt.event.HierarchyListener
  3559. * @see java.awt.Component#addHierarchyListener
  3560. * @see java.awt.Component#enableEvents
  3561. * @since 1.3
  3562. */
  3563. protected void processHierarchyEvent(HierarchyEvent e) {
  3564. HierarchyListener listener = hierarchyListener;
  3565. if (listener != null) {
  3566. int id = e.getID();
  3567. switch (id) {
  3568. case HierarchyEvent.HIERARCHY_CHANGED:
  3569. listener.hierarchyChanged(e);
  3570. break;
  3571. }
  3572. }
  3573. }
  3574. /**
  3575. * Processes hierarchy bounds events occurring on this component by
  3576. * dispatching them to any registered
  3577. * <code>HierarchyBoundsListener</code> objects.
  3578. * <p>
  3579. * This method is not called unless hierarchy bounds events
  3580. * are enabled for this component. Hierarchy bounds events are enabled
  3581. * when one of the following occurs:
  3582. * <p><ul>
  3583. * <li>An <code>HierarchyBoundsListener</code> object is registered
  3584. * via <code>addHierarchyBoundsListener</code>.
  3585. * <li>Hierarchy bounds events are enabled via <code>enableEvents</code>.
  3586. * </ul>
  3587. * @param e the hierarchy event
  3588. * @see java.awt.event.HierarchyEvent
  3589. * @see java.awt.event.HierarchyBoundsListener
  3590. * @see java.awt.Component#addHierarchyBoundsListener
  3591. * @see java.awt.Component#enableEvents
  3592. * @since 1.3
  3593. */
  3594. protected void processHierarchyBoundsEvent(HierarchyEvent e) {
  3595. HierarchyBoundsListener listener = hierarchyBoundsListener;
  3596. if (listener != null) {
  3597. int id = e.getID();
  3598. switch (id) {
  3599. case HierarchyEvent.ANCESTOR_MOVED:
  3600. listener.ancestorMoved(e);
  3601. break;
  3602. case HierarchyEvent.ANCESTOR_RESIZED:
  3603. listener.ancestorResized(e);
  3604. break;
  3605. }
  3606. }
  3607. }
  3608. /**
  3609. * @deprecated As of JDK version 1.1
  3610. * replaced by processEvent(AWTEvent).
  3611. */
  3612. public boolean handleEvent(Event evt) {
  3613. switch (evt.id) {
  3614. case Event.MOUSE_ENTER:
  3615. return mouseEnter(evt, evt.x, evt.y);
  3616. case Event.MOUSE_EXIT:
  3617. return mouseExit(evt, evt.x, evt.y);
  3618. case Event.MOUSE_MOVE:
  3619. return mouseMove(evt, evt.x, evt.y);
  3620. case Event.MOUSE_DOWN:
  3621. return mouseDown(evt, evt.x, evt.y);
  3622. case Event.MOUSE_DRAG:
  3623. return mouseDrag(evt, evt.x, evt.y);
  3624. case Event.MOUSE_UP:
  3625. return mouseUp(evt, evt.x, evt.y);
  3626. case Event.KEY_PRESS:
  3627. case Event.KEY_ACTION:
  3628. return keyDown(evt, evt.key);
  3629. case Event.KEY_RELEASE:
  3630. case Event.KEY_ACTION_RELEASE:
  3631. return keyUp(evt, evt.key);
  3632. case Event.ACTION_EVENT:
  3633. return action(evt, evt.arg);
  3634. case Event.GOT_FOCUS:
  3635. return gotFocus(evt, evt.arg);
  3636. case Event.LOST_FOCUS:
  3637. return lostFocus(evt, evt.arg);
  3638. }
  3639. return false;
  3640. }
  3641. /**
  3642. * @deprecated As of JDK version 1.1,
  3643. * replaced by processMouseEvent(MouseEvent).
  3644. */
  3645. public boolean mouseDown(Event evt, int x, int y) {
  3646. return false;
  3647. }
  3648. /**
  3649. * @deprecated As of JDK version 1.1,
  3650. * replaced by processMouseMotionEvent(MouseEvent).
  3651. */
  3652. public boolean mouseDrag(Event evt, int x, int y) {
  3653. return false;
  3654. }
  3655. /**
  3656. * @deprecated As of JDK version 1.1,
  3657. * replaced by processMouseEvent(MouseEvent).
  3658. */
  3659. public boolean mouseUp(Event evt, int x, int y) {
  3660. return false;
  3661. }
  3662. /**
  3663. * @deprecated As of JDK version 1.1,
  3664. * replaced by processMouseMotionEvent(MouseEvent).
  3665. */
  3666. public boolean mouseMove(Event evt, int x, int y) {
  3667. return false;
  3668. }
  3669. /**
  3670. * @deprecated As of JDK version 1.1,
  3671. * replaced by processMouseEvent(MouseEvent).
  3672. */
  3673. public boolean mouseEnter(Event evt, int x, int y) {
  3674. return false;
  3675. }
  3676. /**
  3677. * @deprecated As of JDK version 1.1,
  3678. * replaced by processMouseEvent(MouseEvent).
  3679. */
  3680. public boolean mouseExit(Event evt, int x, int y) {
  3681. return false;
  3682. }
  3683. /**
  3684. * @deprecated As of JDK version 1.1,
  3685. * replaced by processKeyEvent(KeyEvent).
  3686. */
  3687. public boolean keyDown(Event evt, int key) {
  3688. return false;
  3689. }
  3690. /**
  3691. * @deprecated As of JDK version 1.1,
  3692. * replaced by processKeyEvent(KeyEvent).
  3693. */
  3694. public boolean keyUp(Event evt, int key) {
  3695. return false;
  3696. }
  3697. /**
  3698. * @deprecated As of JDK version 1.1,
  3699. * should register this component as ActionListener on component
  3700. * which fires action events.
  3701. */
  3702. public boolean action(Event evt, Object what) {
  3703. return false;
  3704. }
  3705. /**
  3706. * Makes this Component displayable by connecting it to a
  3707. * native screen resource.
  3708. * This method is called internally by the toolkit and should
  3709. * not be called directly by programs.
  3710. * @see java.awt.Component#isDisplayable
  3711. * @see java.awt.Component#removeNotify
  3712. * @since JDK1.0
  3713. */
  3714. public void addNotify() {
  3715. synchronized (getTreeLock()) {
  3716. ComponentPeer peer = this.peer;
  3717. if (peer == null || peer instanceof java.awt.peer.LightweightPeer){
  3718. if (peer == null) {
  3719. // Update both the Component's peer variable and the local
  3720. // variable we use for thread safety.
  3721. this.peer = peer = getToolkit().createComponent(this);
  3722. }
  3723. // This is a lightweight component which means it won't be
  3724. // able to get window-related events by itself. If any
  3725. // have been enabled, then the nearest native container must
  3726. // be enabled.
  3727. if (parent != null) {
  3728. long mask = 0;
  3729. if ((mouseListener != null) || ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0)) {
  3730. mask |= AWTEvent.MOUSE_EVENT_MASK;
  3731. }
  3732. if ((mouseMotionListener != null) ||
  3733. ((eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0)) {
  3734. mask |= AWTEvent.MOUSE_MOTION_EVENT_MASK;
  3735. }
  3736. if (focusListener != null || (eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0) {
  3737. mask |= AWTEvent.FOCUS_EVENT_MASK;
  3738. }
  3739. if (keyListener != null || (eventMask & AWTEvent.KEY_EVENT_MASK) != 0) {
  3740. mask |= AWTEvent.KEY_EVENT_MASK;
  3741. }
  3742. if (mask != 0) {
  3743. parent.proxyEnableEvents(mask);
  3744. }
  3745. }
  3746. } else {
  3747. // It's native. If the parent is lightweight it
  3748. // will need some help.
  3749. Container parent = this.parent;
  3750. if (parent != null && parent.peer instanceof java.awt.peer.LightweightPeer) {
  3751. new NativeInLightFixer();
  3752. }
  3753. }
  3754. invalidate();
  3755. int npopups = (popups != null? popups.size() : 0);
  3756. for (int i = 0 ; i < npopups ; i++) {
  3757. PopupMenu popup = (PopupMenu)popups.elementAt(i);
  3758. popup.addNotify();
  3759. }
  3760. if (dropTarget != null) dropTarget.addNotify(peer);
  3761. peerFont = getFont();
  3762. if (hierarchyListener != null ||
  3763. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0) {
  3764. HierarchyEvent e =
  3765. new HierarchyEvent(this, HierarchyEvent.HIERARCHY_CHANGED,
  3766. this, parent,
  3767. HierarchyEvent.DISPLAYABILITY_CHANGED |
  3768. ((isRecursivelyVisible())
  3769. ? HierarchyEvent.SHOWING_CHANGED
  3770. : 0));
  3771. dispatchEvent(e);
  3772. }
  3773. }
  3774. }
  3775. /**
  3776. * Makes this Component undisplayable by destroying it native
  3777. * screen resource.
  3778. * This method is called by the toolkit internally and should
  3779. * not be called directly by programs.
  3780. * @see java.awt.Component#isDisplayable
  3781. * @see java.awt.Component#addNotify
  3782. * @since JDK1.0
  3783. */
  3784. public void removeNotify() {
  3785. synchronized (getTreeLock()) {
  3786. int npopups = (popups != null? popups.size() : 0);
  3787. for (int i = 0 ; i < npopups ; i++) {
  3788. PopupMenu popup = (PopupMenu)popups.elementAt(i);
  3789. popup.removeNotify();
  3790. }
  3791. // If there is any input context for this component, notify
  3792. // that this component is being removed. (This has to be done
  3793. // before hiding peer.)
  3794. if (areInputMethodsEnabled()) {
  3795. InputContext inputContext = getInputContext();
  3796. if (inputContext != null) {
  3797. inputContext.removeNotify(this);
  3798. }
  3799. }
  3800. ComponentPeer p = peer;
  3801. if (p != null) {
  3802. if (dropTarget != null) dropTarget.removeNotify(peer);
  3803. // Hide peer first to stop system events such as cursor moves.
  3804. if (visible) {
  3805. p.hide();
  3806. }
  3807. peer = null; // Stop peer updates.
  3808. peerFont = null;
  3809. Toolkit.getEventQueue().removeSourceEvents(this);
  3810. p.dispose();
  3811. }
  3812. if ((p instanceof java.awt.peer.LightweightPeer || p == null) && hasFocus()) {
  3813. dispatchEvent(new FocusEvent(this, FocusEvent.FOCUS_LOST));
  3814. }
  3815. if (hierarchyListener != null ||
  3816. (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0) {
  3817. HierarchyEvent e =
  3818. new HierarchyEvent(this, HierarchyEvent.HIERARCHY_CHANGED,
  3819. this, parent,
  3820. HierarchyEvent.DISPLAYABILITY_CHANGED |
  3821. ((isRecursivelyVisible())
  3822. ? HierarchyEvent.SHOWING_CHANGED
  3823. : 0));
  3824. dispatchEvent(e);
  3825. }
  3826. }
  3827. }
  3828. /**
  3829. * @deprecated As of JDK version 1.1,
  3830. * replaced by processFocusEvent(FocusEvent).
  3831. */
  3832. public boolean gotFocus(Event evt, Object what) {
  3833. return false;
  3834. }
  3835. /**
  3836. * @deprecated As of JDK version 1.1,
  3837. * replaced by processFocusEvent(FocusEvent).
  3838. */
  3839. public boolean lostFocus(Event evt, Object what) {
  3840. return false;
  3841. }
  3842. /**
  3843. * Returns the value of a flag that indicates whether
  3844. * this component can be traversed using
  3845. * Tab or Shift-Tab keyboard focus traversal. If this method
  3846. * returns "false", this component may still request the keyboard
  3847. * focus using <code>requestFocus()</code>, but it will not automatically
  3848. * be assigned focus during tab traversal.
  3849. * @return <code>true</code> if this component is
  3850. * focus-traverable; <code>false</code> otherwise.
  3851. * @since JDK1.1
  3852. */
  3853. public boolean isFocusTraversable() {
  3854. ComponentPeer peer = this.peer;
  3855. if (peer != null) {
  3856. return peer.isFocusTraversable();
  3857. }
  3858. return false;
  3859. }
  3860. /**
  3861. * Requests that this component get the input focus.
  3862. * The component must be visible
  3863. * on the screen for this request to be granted
  3864. * @see FocusEvent
  3865. * @see #addFocusListener
  3866. * @see #processFocusEvent
  3867. * @see #isFocusTraversable
  3868. * @since JDK1.0
  3869. */
  3870. public void requestFocus() {
  3871. ComponentPeer peer = this.peer;
  3872. if (peer != null) {
  3873. if (peer instanceof java.awt.peer.LightweightPeer) {
  3874. if (parent != null) { parent.proxyRequestFocus(this); }
  3875. } else {
  3876. peer.requestFocus();
  3877. Toolkit.getEventQueue().changeKeyEventFocus(this);
  3878. }
  3879. }
  3880. }
  3881. /**
  3882. * Transfers the focus to the next component.
  3883. * @see java.awt.Component#requestFocus
  3884. * @since JDK1.1s
  3885. */
  3886. public void transferFocus() {
  3887. nextFocus();
  3888. }
  3889. /**
  3890. * @deprecated As of JDK version 1.1,
  3891. * replaced by transferFocus().
  3892. */
  3893. public void nextFocus() {
  3894. Container parent = this.parent;
  3895. if (parent != null) {
  3896. parent.transferFocus(this);
  3897. }
  3898. }
  3899. /**
  3900. * Returns true if this Component has the keyboard focus.
  3901. *
  3902. * @return true if this Component has the keyboard focus.
  3903. * @since 1.2
  3904. */
  3905. public boolean hasFocus() {
  3906. if ((eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0) {
  3907. return hasFocus;
  3908. }
  3909. else {
  3910. for (Container p = getParent(); p != null; p = p.getParent()) {
  3911. if (p instanceof Window) {
  3912. return ((Window)p).getFocusOwner() == this;
  3913. }
  3914. }
  3915. return false;
  3916. }
  3917. }
  3918. /**
  3919. * Adds the specified popup menu to the component.
  3920. * @param popup the popup menu to be added to the component.
  3921. * @see java.awt.Component#remove(java.awt.MenuComponent)
  3922. * @since JDK1.1
  3923. */
  3924. public synchronized void add(PopupMenu popup) {
  3925. if (popup.parent != null) {
  3926. popup.parent.remove(popup);
  3927. }
  3928. if (popups == null) {
  3929. popups = new Vector();
  3930. }
  3931. popups.addElement(popup);
  3932. popup.parent = this;
  3933. if (peer != null) {
  3934. if (popup.peer == null) {
  3935. popup.addNotify();
  3936. }
  3937. }
  3938. }
  3939. /**
  3940. * Removes the specified popup menu from the component.
  3941. * @param popup the popup menu to be removed.
  3942. * @see java.awt.Component#add(java.awt.PopupMenu)
  3943. * @since JDK1.1
  3944. */
  3945. public synchronized void remove(MenuComponent popup) {
  3946. if (popups != null) {
  3947. int index = popups.indexOf(popup);
  3948. if (index >= 0) {
  3949. PopupMenu pmenu = (PopupMenu)popup;
  3950. if (pmenu.peer != null) {
  3951. pmenu.removeNotify();
  3952. }
  3953. pmenu.parent = null;
  3954. popups.removeElementAt(index);
  3955. if (popups.size() == 0) {
  3956. popups = null;
  3957. }
  3958. }
  3959. }
  3960. }
  3961. /**
  3962. * Returns a string representing the state of this component. This
  3963. * method is intended to be used only for debugging purposes, and the
  3964. * content and format of the returned string may vary between
  3965. * implementations. The returned string may be empty but may not be
  3966. * <code>null</code>.
  3967. *
  3968. * @return a string representation of this component's state.
  3969. * @since JDK1.0
  3970. */
  3971. protected String paramString() {
  3972. String thisName = getName();
  3973. String str = (thisName != null? thisName : "") + "," + x + "," + y + "," + width + "x" + height;
  3974. if (!valid) {
  3975. str += ",invalid";
  3976. }
  3977. if (!visible) {
  3978. str += ",hidden";
  3979. }
  3980. if (!enabled) {
  3981. str += ",disabled";
  3982. }
  3983. return str;
  3984. }
  3985. /**
  3986. * Returns a string representation of this component and its values.
  3987. * @return a string representation of this component.
  3988. * @since JDK1.0
  3989. */
  3990. public String toString() {
  3991. return getClass().getName() + "[" + paramString() + "]";
  3992. }
  3993. /**
  3994. * Prints a listing of this component to the standard system output
  3995. * stream <code>System.out</code>.
  3996. * @see java.lang.System#out
  3997. * @since JDK1.0
  3998. */
  3999. public void list() {
  4000. list(System.out, 0);
  4001. }
  4002. /**
  4003. * Prints a listing of this component to the specified output
  4004. * stream.
  4005. * @param out a print stream.
  4006. * @since JDK1.0
  4007. */
  4008. public void list(PrintStream out) {
  4009. list(out, 0);
  4010. }
  4011. /**
  4012. * Prints out a list, starting at the specified indention, to the
  4013. * specified print stream.
  4014. * @param out a print stream.
  4015. * @param indent number of spaces to indent.
  4016. * @see java.io.PrintStream#println(java.lang.Object)
  4017. * @since JDK1.0
  4018. */
  4019. public void list(PrintStream out, int indent) {
  4020. for (int i = 0 ; i < indent ; i++) {
  4021. out.print(" ");
  4022. }
  4023. out.println(this);
  4024. }
  4025. /**
  4026. * Prints a listing to the specified print writer.
  4027. * @param out The print writer to print to.
  4028. * @since JDK1.1
  4029. */
  4030. public void list(PrintWriter out) {
  4031. list(out, 0);
  4032. }
  4033. /**
  4034. * Prints out a list, starting at the specified indention, to
  4035. * the specified print writer.
  4036. * @param out The print writer to print to.
  4037. * @param indent The number of spaces to indent.
  4038. * @see java.io.PrintStream#println(java.lang.Object)
  4039. * @since JDK1.1
  4040. */
  4041. public void list(PrintWriter out, int indent) {
  4042. for (int i = 0 ; i < indent ; i++) {
  4043. out.print(" ");
  4044. }
  4045. out.println(this);
  4046. }
  4047. /*
  4048. * Fetch the native container somewhere higher up in the component
  4049. * tree that contains this component.
  4050. */
  4051. Container getNativeContainer() {
  4052. Container p = parent;
  4053. while (p != null && p.peer instanceof java.awt.peer.LightweightPeer) {
  4054. p = p.getParent();
  4055. }
  4056. return p;
  4057. }
  4058. /**
  4059. * Add a PropertyChangeListener to the listener list.
  4060. * The listener is registered for all properties.
  4061. * <p>
  4062. * A PropertyChangeEvent will get fired in response to an
  4063. * explicit setFont, setBackground, or SetForeground on the
  4064. * current component. Note that if the current component is
  4065. * inheriting its foreground, background, or font from its
  4066. * container, then no event will be fired in response to a
  4067. * change in the inherited property.
  4068. *
  4069. * If listener is null, no exception is thrown and no action is performed.
  4070. *
  4071. * @param listener The PropertyChangeListener to be added
  4072. */
  4073. public synchronized void addPropertyChangeListener(
  4074. PropertyChangeListener listener) {
  4075. if (listener == null) {
  4076. return;
  4077. }
  4078. if (changeSupport == null) {
  4079. changeSupport = new java.beans.PropertyChangeSupport(this);
  4080. }
  4081. changeSupport.addPropertyChangeListener(listener);
  4082. }
  4083. /**
  4084. * Remove a PropertyChangeListener from the listener list.
  4085. * This removes a PropertyChangeListener that was registered
  4086. * for all properties.
  4087. *
  4088. * If listener is null, no exception is thrown and no action is performed.
  4089. *
  4090. * @param listener The PropertyChangeListener to be removed
  4091. */
  4092. public synchronized void removePropertyChangeListener(
  4093. PropertyChangeListener listener) {
  4094. if (listener == null) {
  4095. return;
  4096. }
  4097. if (changeSupport == null) {
  4098. return;
  4099. }
  4100. changeSupport.removePropertyChangeListener(listener);
  4101. }
  4102. /**
  4103. * Add a PropertyChangeListener for a specific property. The listener
  4104. * will be invoked only when a call on firePropertyChange names that
  4105. * specific property.
  4106. *
  4107. * If listener is null, no exception is thrown and no action is performed.
  4108. *
  4109. * @param propertyName The name of the property to listen on.
  4110. * @param listener The PropertyChangeListener to be added
  4111. */
  4112. public synchronized void addPropertyChangeListener(
  4113. String propertyName,
  4114. PropertyChangeListener listener) {
  4115. if (listener == null) {
  4116. return;
  4117. }
  4118. if (changeSupport == null) {
  4119. changeSupport = new java.beans.PropertyChangeSupport(this);
  4120. }
  4121. changeSupport.addPropertyChangeListener(propertyName, listener);
  4122. }
  4123. /**
  4124. * Remove a PropertyChangeListener for a specific property.
  4125. * If listener is null, no exception is thrown and no action is performed.
  4126. *
  4127. * @param propertyName The name of the property that was listened on.
  4128. * @param listener The PropertyChangeListener to be removed
  4129. */
  4130. public synchronized void removePropertyChangeListener(
  4131. String propertyName,
  4132. PropertyChangeListener listener) {
  4133. if (listener == null) {
  4134. return;
  4135. }
  4136. if (changeSupport == null) {
  4137. return;
  4138. }
  4139. changeSupport.removePropertyChangeListener(propertyName, listener);
  4140. }
  4141. /**
  4142. * Support for reporting bound property changes. This method can be called
  4143. * when a bound property has changed and it will send the appropriate
  4144. * PropertyChangeEvent to any registered PropertyChangeListeners.
  4145. */
  4146. protected void firePropertyChange(String propertyName,
  4147. Object oldValue, Object newValue) {
  4148. java.beans.PropertyChangeSupport changeSupport = this.changeSupport;
  4149. if (changeSupport == null) {
  4150. return;
  4151. }
  4152. changeSupport.firePropertyChange(propertyName, oldValue, newValue);
  4153. }
  4154. /* Serialization support.
  4155. */
  4156. /**
  4157. * Component Serialized Data Version.
  4158. *
  4159. * @serial
  4160. */
  4161. private int componentSerializedDataVersion = 3;
  4162. /**
  4163. * Writes default serializable fields to stream. Writes
  4164. * a list of serializable ItemListener(s) as optional data.
  4165. * The non-serializable ItemListener(s) are detected and
  4166. * no attempt is made to serialize them.
  4167. *
  4168. * @serialData Null terminated sequence of 0 or more pairs.
  4169. * The pair consists of a String and Object.
  4170. * The String indicates the type of object and
  4171. * is one of the following :
  4172. * itemListenerK indicating and ItemListener object.
  4173. *
  4174. * @see AWTEventMulticaster.save(ObjectOutputStream, String, EventListener)
  4175. * @see java.awt.Component.itemListenerK
  4176. */
  4177. private void writeObject(ObjectOutputStream s)
  4178. throws IOException
  4179. {
  4180. s.defaultWriteObject();
  4181. AWTEventMulticaster.save(s, componentListenerK, componentListener);
  4182. AWTEventMulticaster.save(s, focusListenerK, focusListener);
  4183. AWTEventMulticaster.save(s, keyListenerK, keyListener);
  4184. AWTEventMulticaster.save(s, mouseListenerK, mouseListener);
  4185. AWTEventMulticaster.save(s, mouseMotionListenerK, mouseMotionListener);
  4186. AWTEventMulticaster.save(s, inputMethodListenerK, inputMethodListener);
  4187. s.writeObject(null);
  4188. s.writeObject(componentOrientation);
  4189. AWTEventMulticaster.save(s, hierarchyListenerK, hierarchyListener);
  4190. AWTEventMulticaster.save(s, hierarchyBoundsListenerK,
  4191. hierarchyBoundsListener);
  4192. s.writeObject(null);
  4193. }
  4194. /**
  4195. * Read the ObjectInputStream and if it isnt null
  4196. * add a listener to receive item events fired
  4197. * by the components.
  4198. * Unrecognised keys or values will be Ignored.
  4199. *
  4200. * @see removeActionListener()
  4201. * @see addActionListener()
  4202. */
  4203. private void readObject(ObjectInputStream s)
  4204. throws ClassNotFoundException, IOException
  4205. {
  4206. s.defaultReadObject();
  4207. privateKey = new Object();
  4208. appContext = AppContext.getAppContext();
  4209. SunToolkit.insertTargetMapping(this, appContext);
  4210. Object keyOrNull;
  4211. while(null != (keyOrNull = s.readObject())) {
  4212. String key = ((String)keyOrNull).intern();
  4213. if (componentListenerK == key)
  4214. addComponentListener((ComponentListener)(s.readObject()));
  4215. else if (focusListenerK == key)
  4216. addFocusListener((FocusListener)(s.readObject()));
  4217. else if (keyListenerK == key)
  4218. addKeyListener((KeyListener)(s.readObject()));
  4219. else if (mouseListenerK == key)
  4220. addMouseListener((MouseListener)(s.readObject()));
  4221. else if (mouseMotionListenerK == key)
  4222. addMouseMotionListener((MouseMotionListener)(s.readObject()));
  4223. else if (inputMethodListenerK == key)
  4224. addInputMethodListener((InputMethodListener)(s.readObject()));
  4225. else // skip value for unrecognized key
  4226. s.readObject();
  4227. }
  4228. // Read the component's orientation if it's present
  4229. Object orient = null;
  4230. try {
  4231. orient = s.readObject();
  4232. } catch (java.io.OptionalDataException e) {
  4233. // JDK 1.1 instances will not have this optional data.
  4234. // e.eof will be true to indicate that there is no more
  4235. // data available for this object.
  4236. // If e.eof is not true, throw the exception as it
  4237. // might have been caused by reasons unrelated to
  4238. // componentOrientation.
  4239. if (!e.eof) {
  4240. throw (e);
  4241. }
  4242. }
  4243. if (orient != null) {
  4244. componentOrientation = (ComponentOrientation)orient;
  4245. } else {
  4246. componentOrientation = ComponentOrientation.UNKNOWN;
  4247. }
  4248. try {
  4249. while(null != (keyOrNull = s.readObject())) {
  4250. String key = ((String)keyOrNull).intern();
  4251. if (hierarchyListenerK == key) {
  4252. addHierarchyListener((HierarchyListener)(s.readObject()));
  4253. }
  4254. else if (hierarchyBoundsListenerK == key) {
  4255. addHierarchyBoundsListener((HierarchyBoundsListener)
  4256. (s.readObject()));
  4257. }
  4258. else {
  4259. // skip value for unrecognized key
  4260. s.readObject();
  4261. }
  4262. }
  4263. } catch (java.io.OptionalDataException e) {
  4264. // JDK 1.1/1.2 instances will not have this optional data.
  4265. // e.eof will be true to indicate that there is no more
  4266. // data available for this object.
  4267. // If e.eof is not true, throw the exception as it
  4268. // might have been caused by reasons unrelated to
  4269. // hierarchy and hierarchyBounds listeners.
  4270. if (!e.eof) {
  4271. throw (e);
  4272. }
  4273. }
  4274. if (popups != null) {
  4275. int npopups = popups.size();
  4276. for (int i = 0 ; i < npopups ; i++) {
  4277. PopupMenu popup = (PopupMenu)popups.elementAt(i);
  4278. popup.parent = this;
  4279. }
  4280. }
  4281. }
  4282. /**
  4283. * Set the language-sensitive orientation that is to be used to order
  4284. * the elements or text within this component. Language-sensitive
  4285. * LayoutManager and Component subclasses will use this property to
  4286. * determine how to lay out and draw components.
  4287. * <p>
  4288. * At construction time, a component's orientation is set to
  4289. * ComponentOrientation.UNKNOWN, indicating that it has not been specified
  4290. * explicitly. The UNKNOWN orientation behaves the same as
  4291. * ComponentOrientation.LEFT_TO_RIGHT.
  4292. * <p>
  4293. * To set the orientation of a single component, use this method.
  4294. * To apply a ResourceBundle's orientation to an entire component
  4295. * hierarchy, use java.awt.Window.applyResourceBundle.
  4296. *
  4297. * @see java.awt.ComponentOrientation
  4298. * @see java.awt.Window#applyResourceBundle(java.util.ResourceBundle)
  4299. *
  4300. * @author Laura Werner, IBM
  4301. */
  4302. public void setComponentOrientation(ComponentOrientation o) {
  4303. ComponentOrientation oldValue = componentOrientation;
  4304. componentOrientation = o;
  4305. // This is a bound property, so report the change to
  4306. // any registered listeners. (Cheap if there are none.)
  4307. firePropertyChange("componentOrientation", oldValue, o);
  4308. // This could change the preferred size of the Component.
  4309. if (valid) {
  4310. invalidate();
  4311. }
  4312. }
  4313. /**
  4314. * Retrieve the language-sensitive orientation that is to be used to order
  4315. * the elements or text within this component. LayoutManager and Component
  4316. * subclasses that wish to respect orientation should call this method to
  4317. * get the component's orientation before performing layout or drawing.
  4318. *
  4319. * @see java.awt.ComponentOrientation
  4320. *
  4321. * @author Laura Werner, IBM
  4322. */
  4323. public ComponentOrientation getComponentOrientation() {
  4324. return componentOrientation;
  4325. }
  4326. /**
  4327. * This odd class is to help out a native component that has been
  4328. * embedded in a lightweight component. Moving lightweight
  4329. * components around and changing their visibility is not seen
  4330. * by the native window system. This is a feature for lightweights,
  4331. * but a problem for native components that depend upon the
  4332. * lightweights. An instance of this class listens to the lightweight
  4333. * parents of an associated native component (the outer class).
  4334. *
  4335. * @author Timothy Prinzing
  4336. */
  4337. private final class NativeInLightFixer implements ComponentListener, ContainerListener {
  4338. NativeInLightFixer() {
  4339. lightParents = new Vector();
  4340. Container p = parent;
  4341. // stash a reference to the components that are being observed so that
  4342. // we can reliably remove ourself as a listener later.
  4343. for (; p.peer instanceof java.awt.peer.LightweightPeer; p = p.parent) {
  4344. // register listeners and stash a reference
  4345. p.addComponentListener(this);
  4346. p.addContainerListener(this);
  4347. lightParents.addElement(p);
  4348. }
  4349. // register with the native host (native parent of associated native)
  4350. // to get notified if the top-level lightweight is removed.
  4351. nativeHost = p;
  4352. p.addContainerListener(this);
  4353. // kick start the fixup. Since the event isn't looked at
  4354. // we can simulate movement notification.
  4355. componentMoved(null);
  4356. }
  4357. // --- ComponentListener -------------------------------------------
  4358. /**
  4359. * Invoked when one of the lightweight parents has been resized.
  4360. * This doesn't change the position of the native child so it
  4361. * is ignored.
  4362. */
  4363. public void componentResized(ComponentEvent e) {
  4364. }
  4365. /**
  4366. * Invoked when one of the lightweight parents has been moved.
  4367. * The native peer must be told of the new position which is
  4368. * relative to the native container that is hosting the
  4369. * lightweight components.
  4370. */
  4371. public void componentMoved(ComponentEvent e) {
  4372. synchronized (getTreeLock()) {
  4373. int nativeX = x;
  4374. int nativeY = y;
  4375. for(Component c = parent; (c != null) &&
  4376. (c.peer instanceof java.awt.peer.LightweightPeer);
  4377. c = c.parent) {
  4378. nativeX += c.x;
  4379. nativeY += c.y;
  4380. }
  4381. if (peer != null) {
  4382. peer.setBounds(nativeX, nativeY, width, height);
  4383. }
  4384. }
  4385. }
  4386. /**
  4387. * Invoked when a lightweight parent component has been
  4388. * shown. The associated native component must also be
  4389. * shown if it hasn't had an overriding hide done on it.
  4390. */
  4391. public void componentShown(ComponentEvent e) {
  4392. if (isShowing()) {
  4393. synchronized (getTreeLock()) {
  4394. if (peer != null) {
  4395. peer.show();
  4396. }
  4397. }
  4398. }
  4399. }
  4400. /**
  4401. * Invoked when component has been hidden.
  4402. */
  4403. public void componentHidden(ComponentEvent e) {
  4404. if (visible) {
  4405. synchronized (getTreeLock()) {
  4406. if (peer != null) {
  4407. peer.hide();
  4408. }
  4409. }
  4410. }
  4411. }
  4412. // --- ContainerListener ------------------------------------
  4413. /**
  4414. * Invoked when a component has been added to a lightweight
  4415. * parent. This doesn't effect the native component.
  4416. */
  4417. public void componentAdded(ContainerEvent e) {
  4418. }
  4419. /**
  4420. * Invoked when a lightweight parent has been removed.
  4421. * This means the services of this listener are no longer
  4422. * required and it should remove all references (ie
  4423. * registered listeners).
  4424. */
  4425. public void componentRemoved(ContainerEvent e) {
  4426. Component c = e.getChild();
  4427. if (c == Component.this) {
  4428. removeReferences();
  4429. } else {
  4430. int n = lightParents.size();
  4431. for (int i = 0; i < n; i++) {
  4432. Container p = (Container) lightParents.elementAt(i);
  4433. if (p == c) {
  4434. removeReferences();
  4435. break;
  4436. }
  4437. }
  4438. }
  4439. }
  4440. /**
  4441. * Remove references to this object so it can be
  4442. * garbage collected.
  4443. */
  4444. void removeReferences() {
  4445. int n = lightParents.size();
  4446. for (int i = 0; i < n; i++) {
  4447. Container c = (Container) lightParents.elementAt(i);
  4448. c.removeComponentListener(this);
  4449. c.removeContainerListener(this);
  4450. }
  4451. nativeHost.removeContainerListener(this);
  4452. }
  4453. Vector lightParents;
  4454. Container nativeHost;
  4455. }
  4456. /**
  4457. * Initialize JNI field and method IDs
  4458. */
  4459. private static native void initIDs();
  4460. /*
  4461. * --- Accessibility Support ---
  4462. *
  4463. * Component will contain all of the methods in interface Accessible,
  4464. * though it won't actually implement the interface - that will be up
  4465. * to the individual objects which extend Component.
  4466. */
  4467. AccessibleContext accessibleContext = null;
  4468. /**
  4469. * Get the AccessibleContext associated with this Component
  4470. *
  4471. * @return the AccessibleContext of this Component
  4472. */
  4473. public AccessibleContext getAccessibleContext() {
  4474. return accessibleContext;
  4475. }
  4476. /**
  4477. * Inner class of Component used to provide default support for
  4478. * accessibility. This class is not meant to be used directly by
  4479. * application developers, but is instead meant only to be
  4480. * subclassed by component developers.
  4481. * <p>
  4482. * The class used to obtain the accessible role for this object.
  4483. */
  4484. protected abstract class AccessibleAWTComponent extends AccessibleContext
  4485. implements Serializable, AccessibleComponent {
  4486. /**
  4487. * Though the class is abstract, this should be called by
  4488. * all sub-classes.
  4489. */
  4490. protected AccessibleAWTComponent() {
  4491. }
  4492. protected ComponentListener accessibleAWTComponentHandler = null;
  4493. protected FocusListener accessibleAWTFocusHandler = null;
  4494. /**
  4495. * Fire PropertyChange listener, if one is registered,
  4496. * when shown/hidden..
  4497. */
  4498. protected class AccessibleAWTComponentHandler implements ComponentListener {
  4499. public void componentHidden(ComponentEvent e) {
  4500. if (accessibleContext != null) {
  4501. accessibleContext.firePropertyChange(
  4502. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  4503. AccessibleState.VISIBLE, null);
  4504. }
  4505. }
  4506. public void componentShown(ComponentEvent e) {
  4507. if (accessibleContext != null) {
  4508. accessibleContext.firePropertyChange(
  4509. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  4510. null, AccessibleState.VISIBLE);
  4511. }
  4512. }
  4513. public void componentMoved(ComponentEvent e) {
  4514. }
  4515. public void componentResized(ComponentEvent e) {
  4516. }
  4517. } // inner class AccessibleAWTComponentHandler
  4518. /**
  4519. * Fire PropertyChange listener, if one is registered,
  4520. * when focus events happen
  4521. */
  4522. protected class AccessibleAWTFocusHandler implements FocusListener {
  4523. public void focusGained(FocusEvent event) {
  4524. if (accessibleContext != null) {
  4525. accessibleContext.firePropertyChange(
  4526. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  4527. null, AccessibleState.FOCUSED);
  4528. }
  4529. }
  4530. public void focusLost(FocusEvent event) {
  4531. if (accessibleContext != null) {
  4532. accessibleContext.firePropertyChange(
  4533. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  4534. AccessibleState.FOCUSED, null);
  4535. }
  4536. }
  4537. } // inner class AccessibleAWTFocusHandler
  4538. /**
  4539. * Add a PropertyChangeListener to the listener list.
  4540. *
  4541. * @param listener The PropertyChangeListener to be added
  4542. */
  4543. public void addPropertyChangeListener(PropertyChangeListener listener) {
  4544. if (accessibleAWTComponentHandler == null) {
  4545. accessibleAWTComponentHandler = new AccessibleAWTComponentHandler();
  4546. Component.this.addComponentListener(accessibleAWTComponentHandler);
  4547. }
  4548. if (accessibleAWTFocusHandler == null) {
  4549. accessibleAWTFocusHandler = new AccessibleAWTFocusHandler();
  4550. Component.this.addFocusListener(accessibleAWTFocusHandler);
  4551. }
  4552. super.addPropertyChangeListener(listener);
  4553. }
  4554. /**
  4555. * Remove a PropertyChangeListener from the listener list.
  4556. * This removes a PropertyChangeListener that was registered
  4557. * for all properties.
  4558. *
  4559. * @param listener The PropertyChangeListener to be removed
  4560. */
  4561. public void removePropertyChangeListener(PropertyChangeListener listener) {
  4562. if (accessibleAWTComponentHandler != null) {
  4563. Component.this.removeComponentListener(accessibleAWTComponentHandler);
  4564. accessibleAWTComponentHandler = null;
  4565. }
  4566. if (accessibleAWTFocusHandler != null) {
  4567. Component.this.removeFocusListener(accessibleAWTFocusHandler);
  4568. accessibleAWTFocusHandler = null;
  4569. }
  4570. super.removePropertyChangeListener(listener);
  4571. }
  4572. // AccessibleContext methods
  4573. //
  4574. /**
  4575. * Get the accessible name of this object. This should almost never
  4576. * return java.awt.Component.getName(), as that generally isn't
  4577. * a localized name, and doesn't have meaning for the user. If the
  4578. * object is fundamentally a text object (e.g. a menu item), the
  4579. * accessible name should be the text of the object (e.g. "save").
  4580. * If the object has a tooltip, the tooltip text may also be an
  4581. * appropriate String to return.
  4582. *
  4583. * @return the localized name of the object -- can be null if this
  4584. * object does not have a name
  4585. * @see AccessibleContext#setAccessibleName
  4586. */
  4587. public String getAccessibleName() {
  4588. return accessibleName;
  4589. }
  4590. /**
  4591. * Get the accessible description of this object. This should be
  4592. * a concise, localized description of what this object is - what
  4593. * is its meaning to the user. If the object has a tooltip, the
  4594. * tooltip text may be an appropriate string to return, assuming
  4595. * it contains a concise description of the object (instead of just
  4596. * the name of the object - e.g. a "Save" icon on a toolbar that
  4597. * had "save" as the tooltip text shouldn't return the tooltip
  4598. * text as the description, but something like "Saves the current
  4599. * text document" instead).
  4600. *
  4601. * @return the localized description of the object -- can be null if
  4602. * this object does not have a description
  4603. * @see AccessibleContext#setAccessibleDescription
  4604. */
  4605. public String getAccessibleDescription() {
  4606. return accessibleDescription;
  4607. }
  4608. /**
  4609. * Get the role of this object.
  4610. *
  4611. * @return an instance of AccessibleRole describing the role of the
  4612. * object
  4613. * @see AccessibleRole
  4614. */
  4615. public AccessibleRole getAccessibleRole() {
  4616. return AccessibleRole.AWT_COMPONENT;
  4617. }
  4618. /**
  4619. * Get the state of this object.
  4620. *
  4621. * @return an instance of AccessibleStateSet containing the current
  4622. * state set of the object
  4623. * @see AccessibleState
  4624. */
  4625. public AccessibleStateSet getAccessibleStateSet() {
  4626. return Component.this.getAccessibleStateSet();
  4627. }
  4628. /**
  4629. * Get the Accessible parent of this object. If the parent of this
  4630. * object implements Accessible, this method should simply return
  4631. * getParent().
  4632. *
  4633. * @return the Accessible parent of this object -- can be null if this
  4634. * object does not have an Accessible parent
  4635. */
  4636. public Accessible getAccessibleParent() {
  4637. if (accessibleParent != null) {
  4638. return accessibleParent;
  4639. } else {
  4640. Container parent = getParent();
  4641. if (parent instanceof Accessible) {
  4642. return (Accessible) parent;
  4643. }
  4644. }
  4645. return null;
  4646. }
  4647. /**
  4648. * Get the index of this object in its accessible parent.
  4649. *
  4650. * @return the index of this object in its parent; -1 if this
  4651. * object does not have an accessible parent.
  4652. * @see #getAccessibleParent
  4653. */
  4654. public int getAccessibleIndexInParent() {
  4655. return Component.this.getAccessibleIndexInParent();
  4656. }
  4657. /**
  4658. * Returns the number of accessible children in the object. If all
  4659. * of the children of this object implement Accessible, than this
  4660. * method should return the number of children of this object.
  4661. *
  4662. * @return the number of accessible children in the object.
  4663. */
  4664. public int getAccessibleChildrenCount() {
  4665. return 0; // Components don't have children
  4666. }
  4667. /**
  4668. * Return the nth Accessible child of the object.
  4669. *
  4670. * @param i zero-based index of child
  4671. * @return the nth Accessible child of the object
  4672. */
  4673. public Accessible getAccessibleChild(int i) {
  4674. return null; // Components don't have children
  4675. }
  4676. /**
  4677. * Return the locale of this object.
  4678. *
  4679. * @return the locale of this object
  4680. */
  4681. public Locale getLocale() {
  4682. return Component.this.getLocale();
  4683. }
  4684. /**
  4685. * Get the AccessibleComponent associated with this object if one
  4686. * exists. Otherwise return null.
  4687. *
  4688. * @return the component
  4689. */
  4690. public AccessibleComponent getAccessibleComponent() {
  4691. return this;
  4692. }
  4693. // AccessibleComponent methods
  4694. //
  4695. /**
  4696. * Get the background color of this object.
  4697. *
  4698. * @return the background color, if supported, of the object;
  4699. * otherwise, null
  4700. */
  4701. public Color getBackground() {
  4702. return Component.this.getBackground();
  4703. }
  4704. /**
  4705. * Set the background color of this object.
  4706. * (For transparency, see <code>isOpaque</code>.)
  4707. *
  4708. * @param c the new Color for the background
  4709. * @see Component#isOpaque
  4710. */
  4711. public void setBackground(Color c) {
  4712. Component.this.setBackground(c);
  4713. }
  4714. /**
  4715. * Get the foreground color of this object.
  4716. *
  4717. * @return the foreground color, if supported, of the object;
  4718. * otherwise, null
  4719. */
  4720. public Color getForeground() {
  4721. return Component.this.getForeground();
  4722. }
  4723. /**
  4724. * Set the foreground color of this object.
  4725. *
  4726. * @param c the new Color for the foreground
  4727. */
  4728. public void setForeground(Color c) {
  4729. Component.this.setForeground(c);
  4730. }
  4731. /**
  4732. * Get the Cursor of this object.
  4733. *
  4734. * @return the Cursor, if supported, of the object; otherwise, null
  4735. */
  4736. public Cursor getCursor() {
  4737. return Component.this.getCursor();
  4738. }
  4739. /**
  4740. * Set the Cursor of this object.
  4741. *
  4742. * @param c the new Cursor for the object
  4743. */
  4744. public void setCursor(Cursor cursor) {
  4745. Component.this.setCursor(cursor);
  4746. }
  4747. /**
  4748. * Get the Font of this object.
  4749. *
  4750. * @return the Font,if supported, for the object; otherwise, null
  4751. */
  4752. public Font getFont() {
  4753. return Component.this.getFont();
  4754. }
  4755. /**
  4756. * Set the Font of this object.
  4757. *
  4758. * @param f the new Font for the object
  4759. */
  4760. public void setFont(Font f) {
  4761. Component.this.setFont(f);
  4762. }
  4763. /**
  4764. * Get the FontMetrics of this object.
  4765. *
  4766. * @param f the Font
  4767. * @return the FontMetrics, if supported, the object; otherwise, null
  4768. * @see #getFont
  4769. */
  4770. public FontMetrics getFontMetrics(Font f) {
  4771. if (f == null) {
  4772. return null;
  4773. } else {
  4774. return Component.this.getFontMetrics(f);
  4775. }
  4776. }
  4777. /**
  4778. * Determine if the object is enabled.
  4779. *
  4780. * @return true if object is enabled; otherwise, false
  4781. */
  4782. public boolean isEnabled() {
  4783. return Component.this.isEnabled();
  4784. }
  4785. /**
  4786. * Set the enabled state of the object.
  4787. *
  4788. * @param b if true, enables this object; otherwise, disables it
  4789. */
  4790. public void setEnabled(boolean b) {
  4791. boolean old = Component.this.isEnabled();
  4792. Component.this.setEnabled(b);
  4793. if (b != old) {
  4794. if (accessibleContext != null) {
  4795. if (b) {
  4796. accessibleContext.firePropertyChange(
  4797. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  4798. null, AccessibleState.ENABLED);
  4799. } else {
  4800. accessibleContext.firePropertyChange(
  4801. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  4802. AccessibleState.ENABLED, null);
  4803. }
  4804. }
  4805. }
  4806. }
  4807. /**
  4808. * Determine if the object is visible. Note: this means that the
  4809. * object intends to be visible; however, it may not in fact be
  4810. * showing on the screen because one of the objects that this object
  4811. * is contained by is not visible. To determine if an object is
  4812. * showing on the screen, use isShowing().
  4813. *
  4814. * @return true if object is visible; otherwise, false
  4815. */
  4816. public boolean isVisible() {
  4817. return Component.this.isVisible();
  4818. }
  4819. /**
  4820. * Set the visible state of the object.
  4821. *
  4822. * @param b if true, shows this object; otherwise, hides it
  4823. */
  4824. public void setVisible(boolean b) {
  4825. boolean old = Component.this.isVisible();
  4826. Component.this.setVisible(b);
  4827. if (b != old) {
  4828. if (accessibleContext != null) {
  4829. if (b) {
  4830. accessibleContext.firePropertyChange(
  4831. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  4832. null, AccessibleState.VISIBLE);
  4833. } else {
  4834. accessibleContext.firePropertyChange(
  4835. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  4836. AccessibleState.VISIBLE, null);
  4837. }
  4838. }
  4839. }
  4840. }
  4841. /**
  4842. * Determine if the object is showing. This is determined by checking
  4843. * the visibility of the object and ancestors of the object. Note:
  4844. * this will return true even if the object is obscured by another
  4845. * (for example, it happens to be underneath a menu that was pulled
  4846. * down).
  4847. *
  4848. * @return true if object is showing; otherwise, false
  4849. */
  4850. public boolean isShowing() {
  4851. return Component.this.isShowing();
  4852. }
  4853. /**
  4854. * Checks whether the specified point is within this object's bounds,
  4855. * where the point's x and y coordinates are defined to be relative to
  4856. * the coordinate system of the object.
  4857. *
  4858. * @param p the Point relative to the coordinate system of the object
  4859. * @return true if object contains Point; otherwise false
  4860. */
  4861. public boolean contains(Point p) {
  4862. return Component.this.contains(p);
  4863. }
  4864. /**
  4865. * Returns the location of the object on the screen.
  4866. *
  4867. * @return location of object on screen -- can be null if this object
  4868. * is not on the screen
  4869. */
  4870. public Point getLocationOnScreen() {
  4871. synchronized (Component.this.getTreeLock()) {
  4872. if (Component.this.isShowing()) {
  4873. return Component.this.getLocationOnScreen();
  4874. } else {
  4875. return null;
  4876. }
  4877. }
  4878. }
  4879. /**
  4880. * Gets the location of the object relative to the parent in the form
  4881. * of a point specifying the object's top-left corner in the screen's
  4882. * coordinate space.
  4883. *
  4884. * @return An instance of Point representing the top-left corner of
  4885. * the objects's bounds in the coordinate space of the screen; null if
  4886. * this object or its parent are not on the screen
  4887. */
  4888. public Point getLocation() {
  4889. return Component.this.getLocation();
  4890. }
  4891. /**
  4892. * Sets the location of the object relative to the parent.
  4893. */
  4894. public void setLocation(Point p) {
  4895. Component.this.setLocation(p);
  4896. }
  4897. /**
  4898. * Gets the bounds of this object in the form of a Rectangle object.
  4899. * The bounds specify this object's width, height, and location
  4900. * relative to its parent.
  4901. *
  4902. * @return A rectangle indicating this component's bounds; null if
  4903. * this object is not on the screen.
  4904. */
  4905. public Rectangle getBounds() {
  4906. return Component.this.getBounds();
  4907. }
  4908. /**
  4909. * Sets the bounds of this object in the form of a Rectangle object.
  4910. * The bounds specify this object's width, height, and location
  4911. * relative to its parent.
  4912. *
  4913. * @param A rectangle indicating this component's bounds
  4914. */
  4915. public void setBounds(Rectangle r) {
  4916. Component.this.setBounds(r);
  4917. }
  4918. /**
  4919. * Returns the size of this object in the form of a Dimension object.
  4920. * The height field of the Dimension object contains this objects's
  4921. * height, and the width field of the Dimension object contains this
  4922. * object's width.
  4923. *
  4924. * @return A Dimension object that indicates the size of this
  4925. * component; null if this object is not on the screen
  4926. */
  4927. public Dimension getSize() {
  4928. return Component.this.getSize();
  4929. }
  4930. /**
  4931. * Resizes this object so that it has width width and height.
  4932. *
  4933. * @param d - The dimension specifying the new size of the object.
  4934. */
  4935. public void setSize(Dimension d) {
  4936. Component.this.setSize(d);
  4937. }
  4938. /**
  4939. * Returns the Accessible child, if one exists, contained at the local
  4940. * coordinate Point.
  4941. *
  4942. * @param p The point defining the top-left corner of the Accessible,
  4943. * given in the coordinate space of the object's parent.
  4944. * @return the Accessible, if it exists, at the specified location;
  4945. * else null
  4946. */
  4947. public Accessible getAccessibleAt(Point p) {
  4948. return null; // Components don't have children
  4949. }
  4950. /**
  4951. * Returns whether this object can accept focus or not.
  4952. *
  4953. * @return true if object can accept focus; otherwise false
  4954. */
  4955. public boolean isFocusTraversable() {
  4956. return Component.this.isFocusTraversable();
  4957. }
  4958. /**
  4959. * Requests focus for this object.
  4960. */
  4961. public void requestFocus() {
  4962. Component.this.requestFocus();
  4963. }
  4964. /**
  4965. * Adds the specified focus listener to receive focus events from this
  4966. * component.
  4967. *
  4968. * @param l the focus listener
  4969. */
  4970. public void addFocusListener(FocusListener l) {
  4971. Component.this.addFocusListener(l);
  4972. }
  4973. /**
  4974. * Removes the specified focus listener so it no longer receives focus
  4975. * events from this component.
  4976. *
  4977. * @param l the focus listener
  4978. */
  4979. public void removeFocusListener(FocusListener l) {
  4980. Component.this.removeFocusListener(l);
  4981. }
  4982. } // inner class AccessibleAWTComponent
  4983. /**
  4984. * Get the index of this object in its accessible parent.
  4985. *
  4986. * @return -1 of this object does not have an accessible parent.
  4987. * Otherwise, the index of the child in its accessible parent.
  4988. */
  4989. int getAccessibleIndexInParent() {
  4990. synchronized (getTreeLock()) {
  4991. int index = -1;
  4992. Container parent = this.getParent();
  4993. if (parent != null && parent instanceof Accessible) {
  4994. Component ca[] = parent.getComponents();
  4995. for (int i = 0; i < ca.length; i++) {
  4996. if (ca[i] instanceof Accessible) {
  4997. index++;
  4998. }
  4999. if (this.equals(ca[i])) {
  5000. return index;
  5001. }
  5002. }
  5003. }
  5004. return -1;
  5005. }
  5006. }
  5007. /**
  5008. * Get the state of this object.
  5009. *
  5010. * @return an instance of AccessibleStateSet containing the current state
  5011. * set of the object
  5012. * @see AccessibleState
  5013. */
  5014. AccessibleStateSet getAccessibleStateSet() {
  5015. synchronized (getTreeLock()) {
  5016. AccessibleStateSet states = new AccessibleStateSet();
  5017. if (this.isEnabled()) {
  5018. states.add(AccessibleState.ENABLED);
  5019. }
  5020. if (this.isFocusTraversable()) {
  5021. states.add(AccessibleState.FOCUSABLE);
  5022. }
  5023. if (this.isVisible()) {
  5024. states.add(AccessibleState.VISIBLE);
  5025. }
  5026. if (this.isShowing()) {
  5027. states.add(AccessibleState.SHOWING);
  5028. }
  5029. if (this.hasFocus()) {
  5030. states.add(AccessibleState.FOCUSED);
  5031. }
  5032. if (this instanceof Accessible) {
  5033. AccessibleContext ac = ((Accessible) this).getAccessibleContext();
  5034. if (ac != null) {
  5035. Accessible ap = ac.getAccessibleParent();
  5036. if (ap != null) {
  5037. AccessibleContext pac = ap.getAccessibleContext();
  5038. if (pac != null) {
  5039. AccessibleSelection as = pac.getAccessibleSelection();
  5040. if (as != null) {
  5041. states.add(AccessibleState.SELECTABLE);
  5042. int i = ac.getAccessibleIndexInParent();
  5043. if (i >= 0) {
  5044. if (as.isAccessibleChildSelected(i)) {
  5045. states.add(AccessibleState.SELECTED);
  5046. }
  5047. }
  5048. }
  5049. }
  5050. }
  5051. }
  5052. }
  5053. if (this instanceof javax.swing.JComponent) {
  5054. if (((javax.swing.JComponent) this).isOpaque()) {
  5055. states.add(AccessibleState.OPAQUE);
  5056. }
  5057. }
  5058. return states;
  5059. }
  5060. }
  5061. }