1. /*
  2. * @(#)Component.java 1.239 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.io.PrintStream;
  9. import java.io.PrintWriter;
  10. import java.util.Vector;
  11. import java.util.Locale;
  12. import java.awt.peer.ComponentPeer;
  13. import java.awt.image.ImageObserver;
  14. import java.awt.image.ImageProducer;
  15. import java.awt.image.ColorModel;
  16. import java.awt.event.*;
  17. import java.awt.datatransfer.Transferable;
  18. import java.awt.dnd.DnDConstants;
  19. import java.awt.dnd.DragSource;
  20. import java.awt.dnd.DragSourceContext;
  21. import java.awt.dnd.DragSourceListener;
  22. import java.awt.dnd.InvalidDnDOperationException;
  23. import java.io.Serializable;
  24. import java.io.ObjectOutputStream;
  25. import java.io.ObjectInputStream;
  26. import java.io.IOException;
  27. import java.beans.PropertyChangeListener;
  28. import java.awt.event.InputMethodListener;
  29. import java.awt.event.InputMethodEvent;
  30. import java.awt.im.InputContext;
  31. import java.awt.im.InputMethodRequests;
  32. import java.awt.dnd.DropTarget;
  33. import sun.security.action.GetPropertyAction;
  34. import sun.awt.AppContext;
  35. import sun.awt.SunToolkit;
  36. import sun.awt.ConstrainableGraphics;
  37. /**
  38. * A <em>component</em> is an object having a graphical representation
  39. * that can be displayed on the screen and that can interact with the
  40. * user. Examples of components are the buttons, checkboxes, and scrollbars
  41. * of a typical graphical user interface. <p>
  42. * The <code>Component</code> class is the abstract superclass of
  43. * the nonmenu-related Abstract Window Toolkit components. Class
  44. * <code>Component</code> can also be extended directly to create a
  45. * lightweight component. A lightweight component is a component that is
  46. * not associated with a native opaque window.
  47. *
  48. * @version 1.226, 12/15/98
  49. * @author Arthur van Hoff
  50. * @author Sami Shaio
  51. */
  52. public abstract class Component implements ImageObserver, MenuContainer,
  53. Serializable
  54. {
  55. /**
  56. * The peer of the component. The peer implements the component's
  57. * behaviour. The peer is set when the Component is added to a
  58. * container that also is a peer.
  59. * @see #addNotify
  60. * @see #removeNotify
  61. */
  62. transient ComponentPeer peer;
  63. /**
  64. * The parent of the object. It may be null for top-level components.
  65. * @see #getParent
  66. */
  67. transient Container parent;
  68. /**
  69. * The AppContext of the component. This is set in the constructor
  70. * and never changes.
  71. */
  72. transient AppContext appContext;
  73. /**
  74. * The x position of the component in the parent's coordinate system.
  75. *
  76. * @serial
  77. * @see #getLocation
  78. */
  79. int x;
  80. /**
  81. * The y position of the component in the parent's coordinate system.
  82. *
  83. * @serial
  84. * @see #getLocation
  85. */
  86. int y;
  87. /**
  88. * The width of the component.
  89. *
  90. * @serial
  91. * @see #getSize
  92. */
  93. int width;
  94. /**
  95. * The height of the component.
  96. *
  97. * @serial
  98. * @see #getSize
  99. */
  100. int height;
  101. /**
  102. * The foreground color for this component.
  103. * foreground color can be null.
  104. *
  105. * @serial
  106. * @see #getForeground
  107. * @see #setForeground
  108. */
  109. Color foreground;
  110. /**
  111. * The background color for this component.
  112. * background can be null.
  113. *
  114. * @serial
  115. * @see #getBackground
  116. * @see #setBackground
  117. */
  118. Color background;
  119. /**
  120. * The font used by this component.
  121. * The font can be null.
  122. *
  123. * @serial
  124. * @see #getFont
  125. * @see #setFont
  126. */
  127. Font font;
  128. /**
  129. * The font which the peer is currently using. (null if no peer exists.)
  130. */
  131. Font peerFont;
  132. /**
  133. * The cursor displayed when pointer is over this component.
  134. * cursor must always be a non-null cursor image.
  135. *
  136. * @serial
  137. * @see #getCursor
  138. * @see #setCursor
  139. */
  140. Cursor cursor;
  141. /**
  142. * The locale for the component.
  143. *
  144. * @serial
  145. * @see #getLocale
  146. * @see #setLocale
  147. */
  148. Locale locale;
  149. /**
  150. * True when the object is visible. An object that is not
  151. * visible is not drawn on the screen.
  152. *
  153. * @serial
  154. * @see #isVisible
  155. * @see #setVisible
  156. */
  157. boolean visible = true;
  158. /**
  159. * True when the object is enabled. An object that is not
  160. * enabled does not interact with the user.
  161. *
  162. * @serial
  163. * @see #isEnabled
  164. * @see #setEnabled
  165. */
  166. boolean enabled = true;
  167. /**
  168. * True when the object is valid. An invalid object needs to
  169. * be layed out. This flag is set to false when the object
  170. * size is changed.
  171. *
  172. * @serial
  173. * @see #isValid
  174. * @see #validate
  175. * @see #invalidate
  176. */
  177. boolean valid = false;
  178. /**
  179. * The DropTarget associated with this Component.
  180. *
  181. * @since JDK 1.2
  182. * @serial
  183. * @see #setDropTarget
  184. * @see #getDropTarget
  185. */
  186. DropTarget dropTarget;
  187. /**
  188. * True if this component has enabled focus events and currently
  189. * has the focus.
  190. *
  191. * @serial
  192. * @see #hasFocus
  193. * @see #processFocusEvent
  194. */
  195. boolean hasFocus = false;
  196. /**
  197. * @serial
  198. * @see add()
  199. */
  200. Vector popups;
  201. /**
  202. * A components name.
  203. * This field can be null.
  204. *
  205. * @serial
  206. * @see getName()
  207. * @see setName()
  208. */
  209. private String name;
  210. /**
  211. * A bool to determine whether the name has
  212. * been set explicitly. nameExplicitlySet will
  213. * be false if the name has not been set and
  214. * true if it has.
  215. *
  216. * @serial
  217. * @see getName()
  218. * @see setName()
  219. */
  220. private boolean nameExplicitlySet = false;
  221. /**
  222. * The locking object for AWT component-tree and layout operations.
  223. *
  224. * @see #getTreeLock
  225. */
  226. static final Object LOCK = new AWTTreeLock();
  227. static class AWTTreeLock {}
  228. /**
  229. * Internal, cached size information.
  230. * (This field perhaps should have been transient).
  231. *
  232. * @serial
  233. */
  234. Dimension minSize;
  235. /** Internal, cached size information
  236. * (This field perhaps should have been transient).
  237. *
  238. * @serial
  239. */
  240. Dimension prefSize;
  241. /**
  242. * The orientation for this component.
  243. * @see #getComponentOrientation
  244. * @see #setComponentOrientation(java.awt.ComponentOrientation)
  245. */
  246. transient ComponentOrientation componentOrientation
  247. = ComponentOrientation.UNKNOWN;
  248. /**
  249. * newEventsOnly will be true if the event is
  250. * one of the event types enabled for the component.
  251. * It will then allow for normal processing to
  252. * continue. If it is false the event is passed
  253. * to the components parent and up the ancestor
  254. * tree until the event has been consumed.
  255. *
  256. * @serial
  257. * @see dispatchEvent()
  258. */
  259. boolean newEventsOnly = false;
  260. transient ComponentListener componentListener;
  261. transient FocusListener focusListener;
  262. transient KeyListener keyListener;
  263. transient MouseListener mouseListener;
  264. transient MouseMotionListener mouseMotionListener;
  265. transient InputMethodListener inputMethodListener;
  266. /** Internal, constants for serialization */
  267. final static String actionListenerK = "actionL";
  268. final static String adjustmentListenerK = "adjustmentL";
  269. final static String componentListenerK = "componentL";
  270. final static String containerListenerK = "containerL";
  271. final static String focusListenerK = "focusL";
  272. final static String itemListenerK = "itemL";
  273. final static String keyListenerK = "keyL";
  274. final static String mouseListenerK = "mouseL";
  275. final static String mouseMotionListenerK = "mouseMotionL";
  276. final static String textListenerK = "textL";
  277. final static String ownedWindowK = "ownedL";
  278. final static String windowListenerK = "windowL";
  279. final static String inputMethodListenerK = "inputMethodL";
  280. /**
  281. * The eventMask is ONLY set by subclasses via enableEvents.
  282. * The mask should NOT be set when listeners are registered
  283. * so that we can distinguish the difference between when
  284. * listeners request events and subclasses request them.
  285. * One bit is used to indicate whether input methods are
  286. * enabled; this bit is set by enableInputMethods and is
  287. * on by default.
  288. *
  289. * @serial
  290. * @see enableInputMethods()
  291. */
  292. long eventMask = AWTEvent.INPUT_METHODS_ENABLED_MASK;
  293. // enable for assertion checking
  294. private final static boolean assert = false;
  295. /**
  296. * Static properties for incremental drawing.
  297. * @see #imageUpdate
  298. */
  299. static boolean isInc;
  300. static int incRate;
  301. static {
  302. /* ensure that the necessary native libraries are loaded */
  303. Toolkit.loadLibraries();
  304. /* initialize JNI field and method ids */
  305. initIDs();
  306. String s = (String) java.security.AccessController.doPrivileged(
  307. new GetPropertyAction("awt.image.incrementaldraw"));
  308. isInc = (s == null || s.equals("true"));
  309. s = (String) java.security.AccessController.doPrivileged(
  310. new GetPropertyAction("awt.image.redrawrate"));
  311. incRate = (s != null) ? Integer.parseInt(s) : 100;
  312. }
  313. /**
  314. * Ease-of-use constant for <code>getAlignmentY()</code>. Specifies an
  315. * alignment to the top of the component.
  316. * @see #getAlignmentY
  317. */
  318. public static final float TOP_ALIGNMENT = 0.0f;
  319. /**
  320. * Ease-of-use constant for <code>getAlignmentY</code> and
  321. * <code>getAlignmentX</code>. Specifies an alignment to
  322. * the center of the component
  323. * @see #getAlignmentX
  324. * @see #getAlignmentY
  325. */
  326. public static final float CENTER_ALIGNMENT = 0.5f;
  327. /**
  328. * Ease-of-use constant for <code>getAlignmentY</code>. Specifies an
  329. * alignment to the bottom of the component.
  330. * @see #getAlignmentY
  331. */
  332. public static final float BOTTOM_ALIGNMENT = 1.0f;
  333. /**
  334. * Ease-of-use constant for <code>getAlignmentX</code>. Specifies an
  335. * alignment to the left side of the component.
  336. * @see #getAlignmentX
  337. */
  338. public static final float LEFT_ALIGNMENT = 0.0f;
  339. /**
  340. * Ease-of-use constant for <code>getAlignmentX</code>. Specifies an
  341. * alignment to the right side of the component.
  342. * @see #getAlignmentX
  343. */
  344. public static final float RIGHT_ALIGNMENT = 1.0f;
  345. /*
  346. * JDK 1.1 serialVersionUID
  347. */
  348. private static final long serialVersionUID = -7644114512714619750L;
  349. /**
  350. * If any PropertyChangeListeners have been registered, the
  351. * changeSupport field describes them.
  352. *
  353. * @serial
  354. * @since JDK 1.2
  355. * @see addPropertyChangeListener()
  356. * @see removePropertyChangeListener()
  357. * @see firePropertyChange()
  358. */
  359. private java.beans.PropertyChangeSupport changeSupport;
  360. boolean isPacked = false;
  361. /**
  362. * Constructs a new component. Class <code>Component</code> can be
  363. * extended directly to create a lightweight component that does not
  364. * utilize an opaque native window. A lightweight component must be
  365. * hosted by a native container somewhere higher up in the component
  366. * tree (for example, by a <code>Frame</code> object).
  367. */
  368. protected Component() {
  369. appContext = AppContext.getAppContext();
  370. SunToolkit.insertTargetMapping(this, appContext);
  371. }
  372. /**
  373. * Construct a name for this component. Called by getName() when the
  374. * name is null.
  375. */
  376. String constructComponentName() {
  377. return null; // For strict compliance with prior JDKs, a Component
  378. // that doesn't set its name should return null from
  379. // getName()
  380. }
  381. /**
  382. * Gets the name of the component.
  383. * @return This component's name.
  384. * @see #setName
  385. * @since JDK1.1
  386. */
  387. public String getName() {
  388. if (name == null && !nameExplicitlySet) {
  389. synchronized(this) {
  390. if (name == null && !nameExplicitlySet)
  391. name = constructComponentName();
  392. }
  393. }
  394. return name;
  395. }
  396. /**
  397. * Sets the name of the component to the specified string.
  398. * @param name The string that is to be this
  399. * component's name.
  400. * @see #getName
  401. * @since JDK1.1
  402. */
  403. public void setName(String name) {
  404. synchronized(this) {
  405. this.name = name;
  406. nameExplicitlySet = true;
  407. }
  408. }
  409. /**
  410. * Gets the parent of this component.
  411. * @return The parent container of this component.
  412. * @since JDK1.0
  413. */
  414. public Container getParent() {
  415. return getParent_NoClientCode();
  416. }
  417. // NOTE: This method may be called by privileged threads.
  418. // This functionality is implemented in a package-private method
  419. // to insure that it cannot be overridden by client subclasses.
  420. // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  421. final Container getParent_NoClientCode() {
  422. return parent;
  423. }
  424. /**
  425. * @deprecated As of JDK version 1.1,
  426. * programs should not directly manipulate peers.
  427. * replaced by <code>boolean isDisplayable()</code>.
  428. */
  429. public ComponentPeer getPeer() {
  430. return peer;
  431. }
  432. /**
  433. * Associate a DropTarget with this Component.
  434. *
  435. * @param dt The DropTarget
  436. */
  437. public synchronized void setDropTarget(DropTarget dt) {
  438. if (dt == dropTarget || (dropTarget != null && dropTarget.equals(dt)))
  439. return;
  440. DropTarget old;
  441. if ((old = dropTarget) != null) {
  442. if (peer != null) dropTarget.removeNotify(peer);
  443. DropTarget t = dropTarget;
  444. dropTarget = null;
  445. try {
  446. t.setComponent(null);
  447. } catch (IllegalArgumentException iae) {
  448. // ignore it.
  449. }
  450. }
  451. // if we have a new one, and we have a peer, add it!
  452. if ((dropTarget = dt) != null) {
  453. try {
  454. dropTarget.setComponent(this);
  455. if (peer != null) dropTarget.addNotify(peer);
  456. } catch (IllegalArgumentException iae) {
  457. if (old != null) {
  458. try {
  459. old.setComponent(this);
  460. if (peer != null) dropTarget.addNotify(peer);
  461. } catch (IllegalArgumentException iae1) {
  462. // ignore it!
  463. }
  464. }
  465. }
  466. }
  467. }
  468. /**
  469. * Get the DropTarget associated with this Component
  470. */
  471. public synchronized DropTarget getDropTarget() { return dropTarget; }
  472. /**
  473. * Gets the locking object for AWT component-tree and layout
  474. * Gets this component's locking object (the object that owns the thread
  475. * sychronization monitor) for AWT component-tree and layout
  476. * operations.
  477. * @return This component's locking object.
  478. */
  479. public final Object getTreeLock() {
  480. return LOCK;
  481. }
  482. /**
  483. * Gets the toolkit of this component. Note that
  484. * the frame that contains a component controls which
  485. * toolkit is used by that component. Therefore if the component
  486. * is moved from one frame to another, the toolkit it uses may change.
  487. * @return The toolkit of this component.
  488. * @since JDK1.0
  489. */
  490. public Toolkit getToolkit() {
  491. return getToolkitImpl();
  492. }
  493. /*
  494. * This is called by the native code, so client code can't
  495. * be called on the toolkit thread.
  496. */
  497. final Toolkit getToolkitImpl() {
  498. ComponentPeer peer = this.peer;
  499. if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)){
  500. return peer.getToolkit();
  501. }
  502. Container parent = this.parent;
  503. if (parent != null) {
  504. return parent.getToolkitImpl();
  505. }
  506. return Toolkit.getDefaultToolkit();
  507. }
  508. /**
  509. * Determines whether this component is valid. A component is valid
  510. * when it is correctly sized and positioned within its parent
  511. * container and all its children are also valid. Components are
  512. * invalidated when they are first shown on the screen.
  513. * @return <code>true</code> if the component is valid; <code>false</code>
  514. * otherwise.
  515. * @see #validate
  516. * @see #invalidate
  517. * @since JDK1.0
  518. */
  519. public boolean isValid() {
  520. return (peer != null) && valid;
  521. }
  522. /**
  523. * Determines whether this component is displayable. A component is
  524. * displayable when it is connected to a native screen resource.
  525. * <p>
  526. * A component is made displayable either when it is added to
  527. * a displayable containment hierarchy or when its containment
  528. * hierarchy is made displayable.
  529. * A containment hierarchy is made displayable when its ancestor
  530. * window is either packed or made visible.
  531. * <p>
  532. * A component is made undisplayable either when it is removed from
  533. * a displayable containment hierarchy or when its containment hierarchy
  534. * is made undisplayable. A containment hierarchy is made
  535. * undisplayable when its ancestor window is disposed.
  536. *
  537. * @return <code>true</code> if the component is displayable;
  538. * <code>false</code> otherwise.
  539. * @see java.awt.Container#add(java.awt.Component)
  540. * @see java.awt.Window#pack
  541. * @see java.awt.Window#show
  542. * @see java.awt.Container#remove(java.awt.Component)
  543. * @see java.awt.Window#dispose
  544. * @since JDK1.2
  545. */
  546. public boolean isDisplayable() {
  547. return getPeer() != null;
  548. }
  549. /**
  550. * Determines whether this component should be visible when its
  551. * parent is visible. Components are
  552. * initially visible, with the exception of top level components such
  553. * as <code>Frame</code> objects.
  554. * @return <code>true</code> if the component is visible;
  555. * <code>false</code> otherwise.
  556. * @see #setVisible
  557. * @since JDK1.0
  558. */
  559. public boolean isVisible() {
  560. return visible;
  561. }
  562. /**
  563. * Determines whether this component is showing on screen. This means
  564. * that the component must be visible, and it must be in a container
  565. * that is visible and showing.
  566. * @return <code>true</code> if the component is showing;
  567. * <code>false</code> otherwise.
  568. * @see #setVisible
  569. * @since JDK1.0
  570. */
  571. public boolean isShowing() {
  572. if (visible && (peer != null)) {
  573. Container parent = this.parent;
  574. return (parent == null) || parent.isShowing();
  575. }
  576. return false;
  577. }
  578. /**
  579. * Determines whether this component is enabled. An enabled component
  580. * can respond to user input and generate events. Components are
  581. * enabled initially by default. A component may be enabled or disabled by
  582. * calling its <code>setEnabled</code> method.
  583. * @return <code>true</code> if the component is enabled;
  584. * <code>false</code> otherwise.
  585. * @see #setEnabled
  586. * @since JDK1.0
  587. */
  588. public boolean isEnabled() {
  589. return isEnabledImpl();
  590. }
  591. /*
  592. * This is called by the native code, so client code can't
  593. * be called on the toolkit thread.
  594. */
  595. final boolean isEnabledImpl() {
  596. return enabled;
  597. }
  598. /**
  599. * Enables or disables this component, depending on the value of the
  600. * parameter <code>b</code>. An enabled component can respond to user
  601. * input and generate events. Components are enabled initially by default.
  602. * @param b If <code>true</code>, this component is
  603. * enabled; otherwise this component is disabled.
  604. * @see #isEnabled
  605. * @since JDK1.1
  606. */
  607. public void setEnabled(boolean b) {
  608. enable(b);
  609. }
  610. /**
  611. * @deprecated As of JDK version 1.1,
  612. * replaced by <code>setEnabled(boolean)</code>.
  613. */
  614. public void enable() {
  615. if (enabled != true) {
  616. synchronized (getTreeLock()) {
  617. enabled = true;
  618. ComponentPeer peer = this.peer;
  619. if (peer != null) {
  620. peer.enable();
  621. }
  622. }
  623. }
  624. }
  625. /**
  626. * @deprecated As of JDK version 1.1,
  627. * replaced by <code>setEnabled(boolean)</code>.
  628. */
  629. public void enable(boolean b) {
  630. if (b) {
  631. enable();
  632. } else {
  633. disable();
  634. }
  635. }
  636. /**
  637. * @deprecated As of JDK version 1.1,
  638. * replaced by <code>setEnabled(boolean)</code>.
  639. */
  640. public void disable() {
  641. if (enabled != false) {
  642. synchronized (getTreeLock()) {
  643. enabled = false;
  644. ComponentPeer peer = this.peer;
  645. if (peer != null) {
  646. peer.disable();
  647. }
  648. }
  649. }
  650. }
  651. /**
  652. * Returns true if this component is painted to an offscreen image
  653. * ("buffer") that's copied to the screen later. Component
  654. * subclasses that support double buffering should override this
  655. * method to return true if double buffering is enabled.
  656. *
  657. * @return false by default
  658. */
  659. public boolean isDoubleBuffered() {
  660. return false;
  661. }
  662. /**
  663. * Enables or disables input method support for this component. If input
  664. * method support is enabled and the component also processes key events,
  665. * incoming events are offered to
  666. * the current input method and will only be processed by the component or
  667. * dispatched to its listeners if the input method does not consume them.
  668. * By default, input method support is enabled.
  669. *
  670. * @param enable true to enable, false to disable.
  671. * @see java.awt.Component#processKeyEvent
  672. * @since JDK1.2
  673. */
  674. public void enableInputMethods(boolean enable) {
  675. if (enable) {
  676. if ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0)
  677. return;
  678. // If this component already has focus, then activate the
  679. // input method by dispatching a synthesized focus gained
  680. // event.
  681. if (hasFocus() == true) {
  682. InputContext inputContext = getInputContext();
  683. if (inputContext != null) {
  684. FocusEvent focusGainedEvent = new FocusEvent(this,
  685. FocusEvent.FOCUS_GAINED);
  686. inputContext.dispatchEvent(focusGainedEvent);
  687. }
  688. }
  689. eventMask |= AWTEvent.INPUT_METHODS_ENABLED_MASK;
  690. } else {
  691. if (areInputMethodsEnabled()) {
  692. InputContext inputContext = getInputContext();
  693. if (inputContext != null) {
  694. inputContext.endComposition();
  695. inputContext.removeNotify(this);
  696. }
  697. }
  698. eventMask &= ~AWTEvent.INPUT_METHODS_ENABLED_MASK;
  699. }
  700. }
  701. /**
  702. * Shows or hides this component depending on the value of parameter
  703. * <code>b</code>.
  704. * @param b If <code>true</code>, shows this component;
  705. * otherwise, hides this component.
  706. * @see #isVisible
  707. * @since JDK1.1
  708. */
  709. public void setVisible(boolean b) {
  710. show(b);
  711. }
  712. /**
  713. * @deprecated As of JDK version 1.1,
  714. * replaced by <code>setVisible(boolean)</code>.
  715. */
  716. public void show() {
  717. if (visible != true) {
  718. synchronized (getTreeLock()) {
  719. visible = true;
  720. ComponentPeer peer = this.peer;
  721. if (peer != null) {
  722. peer.show();
  723. if (peer instanceof java.awt.peer.LightweightPeer) {
  724. repaint();
  725. }
  726. }
  727. if (componentListener != null ||
  728. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
  729. ComponentEvent e = new ComponentEvent(this,
  730. ComponentEvent.COMPONENT_SHOWN);
  731. Toolkit.getEventQueue().postEvent(e);
  732. }
  733. }
  734. Container parent = this.parent;
  735. if (parent != null) {
  736. parent.invalidate();
  737. }
  738. }
  739. }
  740. /**
  741. * @deprecated As of JDK version 1.1,
  742. * replaced by <code>setVisible(boolean)</code>.
  743. */
  744. public void show(boolean b) {
  745. if (b) {
  746. show();
  747. } else {
  748. hide();
  749. }
  750. }
  751. /**
  752. * @deprecated As of JDK version 1.1,
  753. * replaced by <code>setVisible(boolean)</code>.
  754. */
  755. public void hide() {
  756. if (visible != false) {
  757. synchronized (getTreeLock()) {
  758. visible = false;
  759. ComponentPeer peer = this.peer;
  760. if (peer != null) {
  761. peer.hide();
  762. if (peer instanceof java.awt.peer.LightweightPeer) {
  763. repaint();
  764. }
  765. }
  766. if (componentListener != null ||
  767. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
  768. ComponentEvent e = new ComponentEvent(this,
  769. ComponentEvent.COMPONENT_HIDDEN);
  770. Toolkit.getEventQueue().postEvent(e);
  771. }
  772. }
  773. Container parent = this.parent;
  774. if (parent != null) {
  775. parent.invalidate();
  776. }
  777. }
  778. }
  779. /**
  780. * Gets the foreground color of this component.
  781. * @return This component's foreground color. If this component does
  782. * not have a foreground color, the foreground color of its parent
  783. * is returned.
  784. * @see #java.awt.Component#setForeground(java.awt.Color)
  785. * @since JDK1.0
  786. */
  787. public Color getForeground() {
  788. Color foreground = this.foreground;
  789. if (foreground != null) {
  790. return foreground;
  791. }
  792. Container parent = this.parent;
  793. return (parent != null) ? parent.getForeground() : null;
  794. }
  795. /**
  796. * Sets the foreground color of this component.
  797. * @param c The color to become this component's
  798. * foreground color.
  799. * If this parameter is null then this component will inherit
  800. * the foreground color of its parent.
  801. * @see #getForeground
  802. * @since JDK1.0
  803. */
  804. public void setForeground(Color c) {
  805. Color oldColor = foreground;
  806. ComponentPeer peer = this.peer;
  807. foreground = c;
  808. if (peer != null) {
  809. c = getForeground();
  810. if (c != null) {
  811. peer.setForeground(c);
  812. }
  813. }
  814. // This is a bound property, so report the change to
  815. // any registered listeners. (Cheap if there are none.)
  816. firePropertyChange("foreground", oldColor, c);
  817. }
  818. /**
  819. * Gets the background color of this component.
  820. * @return This component's background color. If this component does
  821. * not have a background color, the background color of its parent
  822. * is returned.
  823. * @see java.awt.Component#setBackground(java.awt.Color)
  824. * @since JDK1.0
  825. */
  826. public Color getBackground() {
  827. Color background = this.background;
  828. if (background != null) {
  829. return background;
  830. }
  831. Container parent = this.parent;
  832. return (parent != null) ? parent.getBackground() : null;
  833. }
  834. /**
  835. * Sets the background color of this component.
  836. * @param c The color to become this component's color.
  837. * If this parameter is null then this component will inherit
  838. * the background color of its parent.
  839. * background color.
  840. * @see #getBackground
  841. * @since JDK1.0
  842. */
  843. public void setBackground(Color c) {
  844. Color oldColor = background;
  845. ComponentPeer peer = this.peer;
  846. background = c;
  847. if (peer != null) {
  848. c = getBackground();
  849. if (c != null) {
  850. peer.setBackground(c);
  851. }
  852. }
  853. // This is a bound property, so report the change to
  854. // any registered listeners. (Cheap if there are none.)
  855. firePropertyChange("background", oldColor, c);
  856. }
  857. /**
  858. * Gets the font of this component.
  859. * @return This component's font. If a font has not been set
  860. * for this component, the font of its parent is returned.
  861. * @see #setFont
  862. * @since JDK1.0
  863. */
  864. public Font getFont() {
  865. Font font = this.font;
  866. if (font != null) {
  867. return font;
  868. }
  869. Container parent = this.parent;
  870. return (parent != null) ? parent.getFont() : null;
  871. }
  872. // NOTE: This method may be called by privileged threads.
  873. // This functionality is implemented in a package-private method
  874. // to insure that it cannot be overridden by client subclasses.
  875. // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  876. final Font getFont_NoClientCode() {
  877. Font font = this.font;
  878. if (font != null) {
  879. return font;
  880. }
  881. Container parent = this.parent;
  882. return (parent != null) ? parent.getFont_NoClientCode() : null;
  883. }
  884. /**
  885. * Sets the font of this component.
  886. * @param f The font to become this component's font.
  887. * If this parameter is null then this component will inherit
  888. * the font of its parent.
  889. * @see #getFont
  890. * @since JDK1.0
  891. */
  892. public void setFont(Font f) {
  893. synchronized (this) {
  894. Font oldFont = font;
  895. ComponentPeer peer = this.peer;
  896. font = f;
  897. if (peer != null) {
  898. f = getFont();
  899. if (f != null) {
  900. peer.setFont(f);
  901. peerFont = f;
  902. }
  903. }
  904. // This is a bound property, so report the change to
  905. // any registered listeners. (Cheap if there are none.)
  906. firePropertyChange("font", oldFont, font);
  907. }
  908. // This could change the preferred size of the Component.
  909. if (valid) {
  910. invalidate();
  911. }
  912. }
  913. /**
  914. * Gets the locale of this component.
  915. * @return This component's locale. If this component does not
  916. * have a locale, the locale of its parent is returned.
  917. * @see #setLocale
  918. * @exception IllegalComponentStateException If the Component
  919. * does not have its own locale and has not yet been added to
  920. * a containment hierarchy such that the locale can be determined
  921. * from the containing parent.
  922. * @since JDK1.1
  923. */
  924. public Locale getLocale() {
  925. Locale locale = this.locale;
  926. if (locale != null) {
  927. return locale;
  928. }
  929. Container parent = this.parent;
  930. if (parent == null) {
  931. throw new IllegalComponentStateException("This component must have a parent in order to determine its locale");
  932. } else {
  933. return parent.getLocale();
  934. }
  935. }
  936. /**
  937. * Sets the locale of this component.
  938. * @param l The locale to become this component's locale.
  939. * @see #getLocale
  940. * @since JDK1.1
  941. */
  942. public void setLocale(Locale l) {
  943. locale = l;
  944. // This could change the preferred size of the Component.
  945. if (valid) {
  946. invalidate();
  947. }
  948. }
  949. /**
  950. * Gets the instance of <code>ColorModel</code> used to display
  951. * the component on the output device.
  952. * @return The color model used by this component.
  953. * @see java.awt.image.ColorModel
  954. * @see java.awt.peer.ComponentPeer#getColorModel()
  955. * @see java.awt.Toolkit#getColorModel()
  956. * @since JDK1.0
  957. */
  958. public ColorModel getColorModel() {
  959. ComponentPeer peer = this.peer;
  960. if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)) {
  961. return peer.getColorModel();
  962. }
  963. return getToolkit().getColorModel();
  964. }
  965. /**
  966. * Gets the location of this component in the form of a
  967. * point specifying the component's top-left corner.
  968. * The location will be relative to the parent's coordinate space.
  969. * @return An instance of <code>Point</code> representing
  970. * the top-left corner of the component's bounds in the coordinate
  971. * space of the component's parent.
  972. * @see #setLocation
  973. * @see #getLocationOnScreen
  974. * @since JDK1.1
  975. */
  976. public Point getLocation() {
  977. return location();
  978. }
  979. /**
  980. * Gets the location of this component in the form of a point
  981. * specifying the component's top-left corner in the screen's
  982. * coordinate space.
  983. * @return An instance of <code>Point</code> representing
  984. * the top-left corner of the component's bounds in the
  985. * coordinate space of the screen.
  986. * @see #setLocation
  987. * @see #getLocation
  988. */
  989. public Point getLocationOnScreen() {
  990. synchronized (getTreeLock()) {
  991. if (peer != null && isShowing()) {
  992. if (peer instanceof java.awt.peer.LightweightPeer) {
  993. // lightweight component location needs to be translated
  994. // relative to a native component.
  995. Container host = getNativeContainer();
  996. Point pt = host.peer.getLocationOnScreen();
  997. for(Component c = this; c != host; c = c.getParent()) {
  998. pt.x += c.x;
  999. pt.y += c.y;
  1000. }
  1001. return pt;
  1002. } else {
  1003. Point pt = peer.getLocationOnScreen();
  1004. return pt;
  1005. }
  1006. } else {
  1007. throw new IllegalComponentStateException("component must be showing on the screen to determine its location");
  1008. }
  1009. }
  1010. }
  1011. /**
  1012. * @deprecated As of JDK version 1.1,
  1013. * replaced by <code>getLocation()</code>.
  1014. */
  1015. public Point location() {
  1016. return new Point(x, y);
  1017. }
  1018. /**
  1019. * Moves this component to a new location. The top-left corner of
  1020. * the new location is specified by the <code>x</code> and <code>y</code>
  1021. * parameters in the coordinate space of this component's parent.
  1022. * @param x The <i>x</i>-coordinate of the new location's
  1023. * top-left corner in the parent's coordinate space.
  1024. * @param y The <i>y</i>-coordinate of the new location's
  1025. * top-left corner in the parent's coordinate space.
  1026. * @see #getLocation
  1027. * @see #setBounds
  1028. * @since JDK1.1
  1029. */
  1030. public void setLocation(int x, int y) {
  1031. move(x, y);
  1032. }
  1033. /**
  1034. * @deprecated As of JDK version 1.1,
  1035. * replaced by <code>setLocation(int, int)</code>.
  1036. */
  1037. public void move(int x, int y) {
  1038. setBounds(x, y, width, height);
  1039. }
  1040. /**
  1041. * Moves this component to a new location. The top-left corner of
  1042. * the new location is specified by point <code>p</code>. Point
  1043. * <code>p</code> is given in the parent's coordinate space.
  1044. * @param p The point defining the top-left corner
  1045. * of the new location, given in the coordinate space of this
  1046. * component's parent.
  1047. * @see #getLocation
  1048. * @see #setBounds
  1049. * @since JDK1.1
  1050. */
  1051. public void setLocation(Point p) {
  1052. setLocation(p.x, p.y);
  1053. }
  1054. /**
  1055. * Returns the size of this component in the form of a
  1056. * <code>Dimension</code> object. The <code>height</code>
  1057. * field of the <code>Dimension</code> object contains
  1058. * this component's height, and the <code>width</code>
  1059. * field of the <code>Dimension</code> object contains
  1060. * this component's width.
  1061. * @return A <code>Dimension</code> object that indicates the
  1062. * size of this component.
  1063. * @see #setSize
  1064. * @since JDK1.1
  1065. */
  1066. public Dimension getSize() {
  1067. return size();
  1068. }
  1069. /**
  1070. * @deprecated As of JDK version 1.1,
  1071. * replaced by <code>getSize()</code>.
  1072. */
  1073. public Dimension size() {
  1074. return new Dimension(width, height);
  1075. }
  1076. /**
  1077. * Resizes this component so that it has width <code>width</code>
  1078. * and <code>height</code>.
  1079. * @param width The new width of this component in pixels.
  1080. * @param height The new height of this component in pixels.
  1081. * @see #getSize
  1082. * @see #setBounds
  1083. * @since JDK1.1
  1084. */
  1085. public void setSize(int width, int height) {
  1086. resize(width, height);
  1087. }
  1088. /**
  1089. * @deprecated As of JDK version 1.1,
  1090. * replaced by <code>setSize(int, int)</code>.
  1091. */
  1092. public void resize(int width, int height) {
  1093. setBounds(x, y, width, height);
  1094. }
  1095. /**
  1096. * Resizes this component so that it has width <code>d.width</code>
  1097. * and height <code>d.height</code>.
  1098. * @param d The dimension specifying the new size
  1099. * of this component.
  1100. * @see #setSize
  1101. * @see #setBounds
  1102. * @since JDK1.1
  1103. */
  1104. public void setSize(Dimension d) {
  1105. resize(d);
  1106. }
  1107. /**
  1108. * @deprecated As of JDK version 1.1,
  1109. * replaced by <code>setSize(Dimension)</code>.
  1110. */
  1111. public void resize(Dimension d) {
  1112. setSize(d.width, d.height);
  1113. }
  1114. /**
  1115. * Gets the bounds of this component in the form of a
  1116. * <code>Rectangle</code> object. The bounds specify this
  1117. * component's width, height, and location relative to
  1118. * its parent.
  1119. * @return A rectangle indicating this component's bounds.
  1120. * @see #setBounds
  1121. * @see #getLocation
  1122. * @see #getSize
  1123. */
  1124. public Rectangle getBounds() {
  1125. return bounds();
  1126. }
  1127. /**
  1128. * @deprecated As of JDK version 1.1,
  1129. * replaced by <code>getBounds()</code>.
  1130. */
  1131. public Rectangle bounds() {
  1132. return new Rectangle(x, y, width, height);
  1133. }
  1134. /**
  1135. * Moves and resizes this component. The new location of the top-left
  1136. * corner is specified by <code>x</code> and <code>y</code>, and the
  1137. * new size is specified by <code>width</code> and <code>height</code>.
  1138. * @param x The new <i>x</i>-coordinate of this component.
  1139. * @param y The new <i>y</i>-coordinate of this component.
  1140. * @param width The new <code>width</code> of this component.
  1141. * @param height The new <code>height</code> of this
  1142. * component.
  1143. * @see java.awt.Component#getBounds
  1144. * @see java.awt.Component#setLocation(int, int)
  1145. * @see java.awt.Component#setLocation(java.awt.Point)
  1146. * @see java.awt.Component#setSize(int, int)
  1147. * @see java.awt.Component#setSize(java.awt.Dimension)
  1148. * @JDK1.1
  1149. */
  1150. public void setBounds(int x, int y, int width, int height) {
  1151. reshape(x, y, width, height);
  1152. }
  1153. /**
  1154. * @deprecated As of JDK version 1.1,
  1155. * replaced by <code>setBounds(int, int, int, int)</code>.
  1156. */
  1157. public void reshape(int x, int y, int width, int height) {
  1158. synchronized (getTreeLock()) {
  1159. boolean resized = (this.width != width) || (this.height != height);
  1160. boolean moved = (this.x != x) || (this.y != y);
  1161. boolean isLightweight = peer instanceof java.awt.peer.LightweightPeer;
  1162. if (resized) {
  1163. isPacked = false;
  1164. }
  1165. if (resized || moved) {
  1166. if (isLightweight && visible) {
  1167. // Have the parent redraw the area this component occupied.
  1168. repaint();
  1169. }
  1170. this.x = x;
  1171. this.y = y;
  1172. this.width = width;
  1173. this.height = height;
  1174. if (peer != null) {
  1175. if (isLightweight) {
  1176. peer.setBounds(x, y, width, height);
  1177. } else {
  1178. // native peer might be offset by more than direct
  1179. // parent since parent might be lightweight.
  1180. int nativeX = x;
  1181. int nativeY = y;
  1182. for(Component c = parent; (c != null) &&
  1183. (c.peer instanceof java.awt.peer.LightweightPeer);
  1184. c = c.parent) {
  1185. nativeX += c.x;
  1186. nativeY += c.y;
  1187. }
  1188. peer.setBounds(nativeX, nativeY, width, height);
  1189. }
  1190. if (resized) {
  1191. invalidate();
  1192. if (componentListener != null ||
  1193. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
  1194. ComponentEvent e = new ComponentEvent(this,
  1195. ComponentEvent.COMPONENT_RESIZED);
  1196. Toolkit.getEventQueue().postEvent(e);
  1197. }
  1198. }
  1199. if (moved &&
  1200. (componentListener != null ||
  1201. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0)) {
  1202. ComponentEvent e = new ComponentEvent(this,
  1203. ComponentEvent.COMPONENT_MOVED);
  1204. Toolkit.getEventQueue().postEvent(e);
  1205. }
  1206. if (parent != null && parent.valid) {
  1207. parent.invalidate();
  1208. }
  1209. }
  1210. if (isLightweight && visible) {
  1211. // Have the parent redraw the area this component *now* occupies.
  1212. repaint();
  1213. }
  1214. }
  1215. }
  1216. }
  1217. /**
  1218. * Moves and resizes this component to conform to the new
  1219. * bounding rectangle <code>r</code>. This component's new
  1220. * position is specified by <code>r.x</code> and <code>r.y</code>,
  1221. * and its new size is specified by <code>r.width</code> and
  1222. * <code>r.height</code>
  1223. * @param r The new bounding rectangle for this component.
  1224. * @see java.awt.Component#getBounds
  1225. * @see java.awt.Component#setLocation(int, int)
  1226. * @see java.awt.Component#setLocation(java.awt.Point)
  1227. * @see java.awt.Component#setSize(int, int)
  1228. * @see java.awt.Component#setSize(java.awt.Dimension)
  1229. * @since JDK1.1
  1230. */
  1231. public void setBounds(Rectangle r) {
  1232. setBounds(r.x, r.y, r.width, r.height);
  1233. }
  1234. /**
  1235. * Return the current x coordinate of the components origin.
  1236. * This method is preferable to writing component.getBounds().x,
  1237. * or component.getLocation().x because it doesn't cause any
  1238. * heap allocations.
  1239. *
  1240. * @return the current x coordinate of the components origin.
  1241. * @since JDK1.2
  1242. */
  1243. public int getX() {
  1244. return x;
  1245. }
  1246. /**
  1247. * Return the current y coordinate of the components origin.
  1248. * This method is preferable to writing component.getBounds().y,
  1249. * or component.getLocation().y because it doesn't cause any
  1250. * heap allocations.
  1251. *
  1252. * @return the current y coordinate of the components origin.
  1253. * @since JDK1.2
  1254. */
  1255. public int getY() {
  1256. return y;
  1257. }
  1258. /**
  1259. * Return the current width of this component.
  1260. * This method is preferable to writing component.getBounds().width,
  1261. * or component.getSize().width because it doesn't cause any
  1262. * heap allocations.
  1263. *
  1264. * @return the current width of this component.
  1265. * @since JDK1.2
  1266. */
  1267. public int getWidth() {
  1268. return width;
  1269. }
  1270. /**
  1271. * Return the current height of this component.
  1272. * This method is preferable to writing component.getBounds().height,
  1273. * or component.getSize().height because it doesn't cause any
  1274. * heap allocations.
  1275. *
  1276. * @return the current height of this component.
  1277. * @since JDK1.2
  1278. */
  1279. public int getHeight() {
  1280. return height;
  1281. }
  1282. /**
  1283. * Store the bounds of this component into "return value" <b>rv</b> and
  1284. * return <b>rv</b>. If rv is null a new Rectangle is allocated.
  1285. * This version of getBounds() is useful if the caller
  1286. * wants to avoid allocating a new Rectangle object on the heap.
  1287. *
  1288. * @param rv the return value, modified to the components bounds
  1289. * @return rv
  1290. */
  1291. public Rectangle getBounds(Rectangle rv) {
  1292. if (rv == null) {
  1293. return new Rectangle(getX(), getY(), getWidth(), getHeight());
  1294. }
  1295. else {
  1296. rv.setBounds(getX(), getY(), getWidth(), getHeight());
  1297. return rv;
  1298. }
  1299. }
  1300. /**
  1301. * Store the width/height of this component into "return value" <b>rv</b>
  1302. * and return <b>rv</b>. If rv is null a new Dimension object is
  1303. * allocated. This version of getSize() is useful if the
  1304. * caller wants to avoid allocating a new Dimension object on the heap.
  1305. *
  1306. * @param rv the return value, modified to the components size
  1307. * @return rv
  1308. */
  1309. public Dimension getSize(Dimension rv) {
  1310. if (rv == null) {
  1311. return new Dimension(getWidth(), getHeight());
  1312. }
  1313. else {
  1314. rv.setSize(getWidth(), getHeight());
  1315. return rv;
  1316. }
  1317. }
  1318. /**
  1319. * Store the x,y origin of this component into "return value" <b>rv</b>
  1320. * and return <b>rv</b>. If rv is null a new Point is allocated.
  1321. * This version of getLocation() is useful if the
  1322. * caller wants to avoid allocating a new Point object on the heap.
  1323. *
  1324. * @param rv the return value, modified to the components location
  1325. * @return rv
  1326. */
  1327. public Point getLocation(Point rv) {
  1328. if (rv == null) {
  1329. return new Point(getX(), getY());
  1330. }
  1331. else {
  1332. rv.setLocation(getX(), getY());
  1333. return rv;
  1334. }
  1335. }
  1336. /**
  1337. * Returns true if this component is completely opaque, returns
  1338. * false by default.
  1339. * <p>
  1340. * An opaque component paints every pixel within its
  1341. * rectangular region. A non-opaque component paints only some of
  1342. * its pixels, allowing the pixels underneath it to "show through".
  1343. * A component that does not fully paint its pixels therefore
  1344. * provides a degree of transparency. Only lightweight
  1345. * components can be transparent.
  1346. * <p>
  1347. * Subclasses that guarantee to always completely paint their
  1348. * contents should override this method and return true. All
  1349. * of the "heavyweight" AWT components are opaque.
  1350. *
  1351. * @return true if this component is completely opaque.
  1352. * @see #isLightweight
  1353. * @since JDK1.2
  1354. */
  1355. public boolean isOpaque() {
  1356. return !isLightweight();
  1357. }
  1358. /**
  1359. * A lightweight component doesn't have a native toolkit peer.
  1360. * Subclasses of Component and Container, other than the ones
  1361. * defined in this package like Button or Scrollbar, are lightweight.
  1362. * All of the Swing components are lightweights.
  1363. *
  1364. * @return true if this component doesn't have a native peer
  1365. * @since JDK1.2
  1366. */
  1367. public boolean isLightweight() {
  1368. return getPeer() instanceof java.awt.peer.LightweightPeer;
  1369. }
  1370. /**
  1371. * Gets the preferred size of this component.
  1372. * @return A dimension object indicating this component's preferred size.
  1373. * @see #getMinimumSize
  1374. * @see java.awt.LayoutManager
  1375. */
  1376. public Dimension getPreferredSize() {
  1377. return preferredSize();
  1378. }
  1379. /**
  1380. * @deprecated As of JDK version 1.1,
  1381. * replaced by <code>getPreferredSize()</code>.
  1382. */
  1383. public Dimension preferredSize() {
  1384. /* Avoid grabbing the lock if a reasonable cached size value
  1385. * is available.
  1386. */
  1387. Dimension dim = prefSize;
  1388. if (dim != null && isValid()) {
  1389. return dim;
  1390. }
  1391. synchronized (getTreeLock()) {
  1392. prefSize = (peer != null) ?
  1393. peer.preferredSize() :
  1394. getMinimumSize();
  1395. return prefSize;
  1396. }
  1397. }
  1398. /**
  1399. * Gets the mininimum size of this component.
  1400. * @return A dimension object indicating this component's minimum size.
  1401. * @see #getPreferredSize
  1402. * @see java.awtLayoutManager
  1403. */
  1404. public Dimension getMinimumSize() {
  1405. return minimumSize();
  1406. }
  1407. /**
  1408. * @deprecated As of JDK version 1.1,
  1409. * replaced by <code>getMinimumSize()</code>.
  1410. */
  1411. public Dimension minimumSize() {
  1412. /* Avoid grabbing the lock if a reasonable cached size value
  1413. * is available.
  1414. */
  1415. Dimension dim = minSize;
  1416. if (dim != null && isValid()) {
  1417. return dim;
  1418. }
  1419. synchronized (getTreeLock()) {
  1420. minSize = (peer != null) ?
  1421. peer.minimumSize() :
  1422. size();
  1423. return minSize;
  1424. }
  1425. }
  1426. /**
  1427. * Gets the maximum size of this component.
  1428. * @return A dimension object indicating this component's maximum size.
  1429. * @see #getMinimumSize
  1430. * @see #getPreferredSize
  1431. * @see LayoutManager
  1432. */
  1433. public Dimension getMaximumSize() {
  1434. return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
  1435. }
  1436. /**
  1437. * Returns the alignment along the x axis. This specifies how
  1438. * the component would like to be aligned relative to other
  1439. * components. The value should be a number between 0 and 1
  1440. * where 0 represents alignment along the origin, 1 is aligned
  1441. * the furthest away from the origin, 0.5 is centered, etc.
  1442. */
  1443. public float getAlignmentX() {
  1444. return CENTER_ALIGNMENT;
  1445. }
  1446. /**
  1447. * Returns the alignment along the y axis. This specifies how
  1448. * the component would like to be aligned relative to other
  1449. * components. The value should be a number between 0 and 1
  1450. * where 0 represents alignment along the origin, 1 is aligned
  1451. * the furthest away from the origin, 0.5 is centered, etc.
  1452. */
  1453. public float getAlignmentY() {
  1454. return CENTER_ALIGNMENT;
  1455. }
  1456. /**
  1457. * Prompts the layout manager to lay out this component. This is
  1458. * usually called when the component (more specifically, container)
  1459. * is validated.
  1460. * @see #validate
  1461. * @see LayoutManager
  1462. */
  1463. public void doLayout() {
  1464. layout();
  1465. }
  1466. /**
  1467. * @deprecated As of JDK version 1.1,
  1468. * replaced by <code>doLayout()</code>.
  1469. */
  1470. public void layout() {
  1471. }
  1472. /**
  1473. * Ensures that this component has a valid layout. This method is
  1474. * primarily intended to operate on instances of <code>Container</code>.
  1475. * @see java.awt.Component#invalidate
  1476. * @see java.awt.Component#doLayout()
  1477. * @see java.awt.LayoutManager
  1478. * @see java.awt.Container#validate
  1479. * @since JDK1.0
  1480. */
  1481. public void validate() {
  1482. if (!valid) {
  1483. synchronized (getTreeLock()) {
  1484. ComponentPeer peer = this.peer;
  1485. if (!valid && peer != null) {
  1486. Font newfont = getFont();
  1487. Font oldfont = peerFont;
  1488. if (newfont != oldfont && (oldfont == null
  1489. || !oldfont.equals(newfont))) {
  1490. peer.setFont(newfont);
  1491. peerFont = newfont;
  1492. }
  1493. }
  1494. }
  1495. valid = true;
  1496. }
  1497. }
  1498. /**
  1499. * Invalidates this component. This component and all parents
  1500. * above it are marked as needing to be laid out. This method can
  1501. * be called often, so it needs to execute quickly.
  1502. * @see java.awt.Component#validate
  1503. * @see java.awt.Component#doLayout
  1504. * @see java.awt.LayoutManager
  1505. * @since JDK1.0
  1506. */
  1507. public void invalidate() {
  1508. synchronized (getTreeLock()) {
  1509. /* Nullify cached layout and size information.
  1510. * For efficiency, propagate invalidate() upwards only if
  1511. * some other component hasn't already done so first.
  1512. */
  1513. valid = false;
  1514. prefSize = null;
  1515. minSize = null;
  1516. if (parent != null && parent.valid) {
  1517. parent.invalidate();
  1518. }
  1519. }
  1520. }
  1521. /**
  1522. * Creates a graphics context for this component. This method will
  1523. * return <code>null</code> if this component is currently not on
  1524. * the screen.
  1525. * @return A graphics context for this component, or <code>null</code>
  1526. * if it has none.
  1527. * @see java.awt.Component#paint
  1528. * @since JDK1.0
  1529. */
  1530. public Graphics getGraphics() {
  1531. if (peer instanceof java.awt.peer.LightweightPeer) {
  1532. // This is for a lightweight component, need to
  1533. // translate coordinate spaces and clip relative
  1534. // to the parent.
  1535. Graphics g = parent.getGraphics();
  1536. if (g instanceof ConstrainableGraphics) {
  1537. ((ConstrainableGraphics) g).constrain(x, y, width, height);
  1538. } else {
  1539. g.translate(x,y);
  1540. g.setClip(0, 0, width, height);
  1541. }
  1542. g.setFont(getFont());
  1543. return g;
  1544. } else {
  1545. ComponentPeer peer = this.peer;
  1546. return (peer != null) ? peer.getGraphics() : null;
  1547. }
  1548. }
  1549. /**
  1550. * Gets the font metrics for the specified font.
  1551. * @param font The font for which font metrics is to be
  1552. * obtained.
  1553. * @return The font metrics for <code>font</code>.
  1554. * @param font the font.
  1555. * @return the font metrics for the specified font.
  1556. * @see java.awt.Component#getFont
  1557. * @see java.awt.Component#getPeer()
  1558. * @see java.awt.peer.ComponentPeer#getFontMetrics(java.awt.Font)
  1559. * @see java.awt.Toolkit#getFontMetrics(java.awt.Font)
  1560. * @since JDK1.0
  1561. */
  1562. public FontMetrics getFontMetrics(Font font) {
  1563. if (sun.java2d.loops.RasterOutputManager.usesPlatformFont()) {
  1564. if (peer != null &&
  1565. !(peer instanceof java.awt.peer.LightweightPeer)) {
  1566. return peer.getFontMetrics(font);
  1567. }
  1568. }
  1569. if (parent != null) {
  1570. Graphics g = parent.getGraphics();
  1571. if (g != null) {
  1572. return g.getFontMetrics(font);
  1573. }
  1574. }
  1575. return getToolkit().getFontMetrics(font);
  1576. }
  1577. /**
  1578. * Sets the cursor image to the specified cursor. This cursor
  1579. * image is displayed when the <code>contains</code> method for
  1580. * this component returns true for the current cursor location.
  1581. * Setting the cursor of a <code>Container</code> causes that cursor
  1582. * to be displayed within all of the container's subcomponents,
  1583. * except for those that have a non-null cursor.
  1584. *
  1585. * @param cursor One of the constants defined
  1586. * by the <code>Cursor</code> class.
  1587. * If this parameter is null then this component will inherit
  1588. * the cursor of its parent.
  1589. * @see java.awt.Component#getCursor
  1590. * @see java.awt.Component#contains
  1591. * @see java.awt.Toolkit#createCustomCursor
  1592. * @see java.awt.Cursor
  1593. * @since JDK1.1
  1594. */
  1595. public synchronized void setCursor(Cursor cursor) {
  1596. this.cursor = cursor;
  1597. ComponentPeer peer = this.peer;
  1598. if (peer instanceof java.awt.peer.LightweightPeer) {
  1599. getNativeContainer().updateCursor(this);
  1600. } else if (peer != null) {
  1601. peer.setCursor(cursor);
  1602. }
  1603. }
  1604. /**
  1605. * Gets the cursor set in the component. If the component does
  1606. * not have a cursor set, the cursor of its parent is returned.
  1607. * If no Cursor is set in the entire hierarchy, Cursor.DEFAULT_CURSOR is
  1608. * returned.
  1609. * @see #setCursor
  1610. * @since JDK1.1
  1611. */
  1612. public Cursor getCursor() {
  1613. Cursor cursor = this.cursor;
  1614. if (cursor != null) {
  1615. return cursor;
  1616. }
  1617. Container parent = this.parent;
  1618. if (parent != null) {
  1619. return parent.getCursor();
  1620. } else {
  1621. return Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
  1622. }
  1623. }
  1624. Cursor getIntrinsicCursor() {
  1625. return cursor;
  1626. }
  1627. /**
  1628. * Paints this component. This method is called when the contents
  1629. * of the component should be painted in response to the component
  1630. * first being shown or damage needing repair. The clip rectangle
  1631. * in the Graphics parameter will be set to the area which needs
  1632. * to be painted.
  1633. * @param g The graphics context to use for painting.
  1634. * @see java.awt.Component#update
  1635. * @since JDK1.0
  1636. */
  1637. public void paint(Graphics g) {
  1638. }
  1639. /**
  1640. * Updates this component.
  1641. * <p>
  1642. * The AWT calls the <code>update</code> method in response to a
  1643. * call to <code>repaint</code. The appearance of the
  1644. * component on the screen has not changed since the last call to
  1645. * <code>update</code> or <code>paint</code>. You can assume that
  1646. * the background is not cleared.
  1647. * <p>
  1648. * The <code>update</code>method of <code>Component</code>
  1649. * does the following:
  1650. * <p>
  1651. * <blockquote><ul>
  1652. * <li>Clears this component by filling it
  1653. * with the background color.
  1654. * <li>Sets the color of the graphics context to be
  1655. * the foreground color of this component.
  1656. * <li>Calls this component's <code>paint</code>
  1657. * method to completely redraw this component.
  1658. * </ul></blockquote>
  1659. * <p>
  1660. * The origin of the graphics context, its
  1661. * (<code>0</code>, <code>0</code>) coordinate point, is the
  1662. * top-left corner of this component. The clipping region of the
  1663. * graphics context is the bounding rectangle of this component.
  1664. * @param g the specified context to use for updating.
  1665. * @see java.awt.Component#paint
  1666. * @see java.awt.Component#repaint()
  1667. * @since JDK1.0
  1668. */
  1669. public void update(Graphics g) {
  1670. if ((this instanceof java.awt.Canvas) ||
  1671. (this instanceof java.awt.Panel) ||
  1672. (this instanceof java.awt.Frame) ||
  1673. (this instanceof java.awt.Dialog) ||
  1674. (this instanceof java.awt.Window)) {
  1675. g.clearRect(0, 0, width, height);
  1676. }
  1677. paint(g);
  1678. }
  1679. /**
  1680. * Paints this component and all of its subcomponents.
  1681. * <p>
  1682. * The origin of the graphics context, its
  1683. * (<code>0</code>, <code>0</code>) coordinate point, is the
  1684. * top-left corner of this component. The clipping region of the
  1685. * graphics context is the bounding rectangle of this component.
  1686. * @param g the graphics context to use for painting.
  1687. * @see java.awt.Component#paint
  1688. * @since JDK1.0
  1689. */
  1690. public void paintAll(Graphics g) {
  1691. ComponentPeer peer = this.peer;
  1692. if (visible && (peer != null)) {
  1693. validate();
  1694. if (peer instanceof java.awt.peer.LightweightPeer) {
  1695. paint(g);
  1696. } else {
  1697. peer.paint(g);
  1698. }
  1699. }
  1700. }
  1701. /**
  1702. * Repaints this component.
  1703. * <p>
  1704. * This method causes a call to this component's <code>update</code>
  1705. * method as soon as possible.
  1706. * @see java.awt.Component#update(java.awt.Graphics)
  1707. * @since JDK1.0
  1708. */
  1709. public void repaint() {
  1710. /*
  1711. getToolkit().getEventQueue().removeSourceEvents(this, PaintEvent.PAINT);
  1712. */
  1713. repaint(0, 0, 0, width, height);
  1714. }
  1715. /**
  1716. * Repaints the component. This will result in a
  1717. * call to <code>update</code> within <em>tm</em> milliseconds.
  1718. * @param tm maximum time in milliseconds before update
  1719. * @see #paint
  1720. * @see java.awt.Component#update(java.awt.Graphics)
  1721. * @since JDK1.0
  1722. */
  1723. public void repaint(long tm) {
  1724. repaint(tm, 0, 0, width, height);
  1725. }
  1726. /**
  1727. * Repaints the specified rectangle of this component.
  1728. * <p>
  1729. * This method causes a call to this component's <code>update</code>
  1730. * method as soon as possible.
  1731. * @param x the <i>x</i> coordinate.
  1732. * @param y the <i>y</i> coordinate.
  1733. * @param width the width.
  1734. * @param height the height.
  1735. * @see java.awt.Component#update(java.awt.Graphics)
  1736. * @since JDK1.0
  1737. */
  1738. public void repaint(int x, int y, int width, int height) {
  1739. repaint(0, x, y, width, height);
  1740. }
  1741. /**
  1742. * Repaints the specified rectangle of this component within
  1743. * <code>tm</code> milliseconds.
  1744. * <p>
  1745. * This method causes a call to this component's
  1746. * <code>update</code> method.
  1747. * @param tm maximum time in milliseconds before update.
  1748. * @param x the <i>x</i> coordinate.
  1749. * @param y the <i>y</i> coordinate.
  1750. * @param width the width.
  1751. * @param height the height.
  1752. * @see java.awt.Component#update(java.awt.Graphics)
  1753. * @since JDK1.0
  1754. */
  1755. public void repaint(long tm, int x, int y, int width, int height) {
  1756. if (this.peer instanceof java.awt.peer.LightweightPeer) {
  1757. // Needs to be translated to parent coordinates since
  1758. // a parent native container provides the actual repaint
  1759. // services. Additionally, the request is restricted to
  1760. // the bounds of the component.
  1761. int px = this.x + ((x < 0) ? 0 : x);
  1762. int py = this.y + ((y < 0) ? 0 : y);
  1763. int pwidth = (width > this.width) ? this.width : width;
  1764. int pheight = (height > this.height) ? this.height : height;
  1765. parent.repaint(tm, px, py, pwidth, pheight);
  1766. } else {
  1767. ComponentPeer peer = this.peer;
  1768. if ((peer != null) && (width > 0) && (height > 0)) {
  1769. peer.repaint(tm, x, y, width, height);
  1770. }
  1771. }
  1772. }
  1773. /**
  1774. * Prints this component. Applications should override this method
  1775. * for components that must do special processing before being
  1776. * printed or should be printed differently than they are painted.
  1777. * <p>
  1778. * The default implementation of this method calls the
  1779. * <code>paint</code> method.
  1780. * <p>
  1781. * The origin of the graphics context, its
  1782. * (<code>0</code>, <code>0</code>) coordinate point, is the
  1783. * top-left corner of this component. The clipping region of the
  1784. * graphics context is the bounding rectangle of this component.
  1785. * @param g the graphics context to use for printing.
  1786. * @see java.awt.Component#paint(java.awt.Graphics)
  1787. * @since JDK1.0
  1788. */
  1789. public void print(Graphics g) {
  1790. paint(g);
  1791. }
  1792. /**
  1793. * Prints this component and all of its subcomponents.
  1794. * <p>
  1795. * The origin of the graphics context, its
  1796. * (<code>0</code>, <code>0</code>) coordinate point, is the
  1797. * top-left corner of this component. The clipping region of the
  1798. * graphics context is the bounding rectangle of this component.
  1799. * @param g the graphics context to use for printing.
  1800. * @see java.awt.Component#print(java.awt.Graphics)
  1801. * @since JDK1.0
  1802. */
  1803. public void printAll(Graphics g) {
  1804. ComponentPeer peer = this.peer;
  1805. if (visible && (peer != null)) {
  1806. validate();
  1807. Graphics cg = g.create(0, 0, width, height);
  1808. cg.setFont(getFont());
  1809. try {
  1810. if (peer instanceof java.awt.peer.LightweightPeer) {
  1811. lightweightPrint(cg);
  1812. }
  1813. else {
  1814. peer.print(cg);
  1815. }
  1816. } finally {
  1817. cg.dispose();
  1818. }
  1819. }
  1820. }
  1821. /**
  1822. * Simulates the peer callbacks into java.awt for printing of
  1823. * lightweight Components.
  1824. * @param g the graphics context to use for printing.
  1825. * @see #printAll
  1826. */
  1827. void lightweightPrint(Graphics g) {
  1828. print(g);
  1829. }
  1830. /**
  1831. * Prints all the heavyweight subcomponents.
  1832. */
  1833. void printHeavyweightComponents(Graphics g) {
  1834. }
  1835. /**
  1836. * Repaints the component when the image has changed.
  1837. * This <code>imageUpdate</code> method of an <code>ImageObserver</code>
  1838. * is called when more information about an
  1839. * image which had been previously requested using an asynchronous
  1840. * routine such as the <code>drawImage</code> method of
  1841. * <code>Graphics</code> becomes available.
  1842. * See the definition of <code>imageUpdate</code> for
  1843. * more information on this method and its arguments.
  1844. * <p>
  1845. * The <code>imageUpdate</code> method of <code>Component</code>
  1846. * incrementally draws an image on the component as more of the bits
  1847. * of the image are available.
  1848. * <p>
  1849. * If the system property <code>awt.image.incrementalDraw</code>
  1850. * is missing or has the value <code>true</code>, the image is
  1851. * incrementally drawn, If the system property has any other value,
  1852. * then the image is not drawn until it has been completely loaded.
  1853. * <p>
  1854. * Also, if incremental drawing is in effect, the value of the
  1855. * system property <code>awt.image.redrawrate</code> is interpreted
  1856. * as an integer to give the maximum redraw rate, in milliseconds. If
  1857. * the system property is missing or cannot be interpreted as an
  1858. * integer, the redraw rate is once every 100ms.
  1859. * <p>
  1860. * The interpretation of the <code>x</code>, <code>y</code>,
  1861. * <code>width</code>, and <code>height</code> arguments depends on
  1862. * the value of the <code>infoflags</code> argument.
  1863. * @param img the image being observed.
  1864. * @param infoflags see <code>imageUpdate</code> for more information.
  1865. * @param x the <i>x</i> coordinate.
  1866. * @param y the <i>y</i> coordinate.
  1867. * @param width the width.
  1868. * @param height the height.
  1869. * @return <code>true</code> if the flags indicate that the
  1870. * image is completely loaded;
  1871. * <code>false</code> otherwise.
  1872. * @see java.awt.image.ImageObserver
  1873. * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, java.awt.Color, java.awt.image.ImageObserver)
  1874. * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  1875. * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int, java.awt.Color, java.awt.image.ImageObserver)
  1876. * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int, java.awt.image.ImageObserver)
  1877. * @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
  1878. * @since JDK1.0
  1879. */
  1880. public boolean imageUpdate(Image img, int flags,
  1881. int x, int y, int w, int h) {
  1882. int rate = -1;
  1883. if ((flags & (FRAMEBITS|ALLBITS)) != 0) {
  1884. rate = 0;
  1885. } else if ((flags & SOMEBITS) != 0) {
  1886. if (isInc) {
  1887. try {
  1888. rate = incRate;
  1889. if (rate < 0)
  1890. rate = 0;
  1891. } catch (Exception e) {
  1892. rate = 100;
  1893. }
  1894. }
  1895. }
  1896. if (rate >= 0) {
  1897. repaint(rate, 0, 0, width, height);
  1898. }
  1899. return (flags & (ALLBITS|ABORT)) == 0;
  1900. }
  1901. /**
  1902. * Creates an image from the specified image producer.
  1903. * @param producer the image producer
  1904. * @return the image produced.
  1905. * @since JDK1.0
  1906. */
  1907. public Image createImage(ImageProducer producer) {
  1908. ComponentPeer peer = this.peer;
  1909. if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)) {
  1910. return peer.createImage(producer);
  1911. }
  1912. return getToolkit().createImage(producer);
  1913. }
  1914. /**
  1915. * Creates an off-screen drawable image
  1916. * to be used for double buffering.
  1917. * @param width the specified width.
  1918. * @param height the specified height.
  1919. * @return an off-screen drawable image,
  1920. * which can be used for double buffering.
  1921. * @since JDK1.0
  1922. */
  1923. public Image createImage(int width, int height) {
  1924. ComponentPeer peer = this.peer;
  1925. if (peer instanceof java.awt.peer.LightweightPeer) {
  1926. return parent.createImage(width, height);
  1927. } else {
  1928. return (peer != null) ? peer.createImage(width, height) : null;
  1929. }
  1930. }
  1931. /**
  1932. * Prepares an image for rendering on this component. The image
  1933. * data is downloaded asynchronously in another thread and the
  1934. * appropriate screen representation of the image is generated.
  1935. * @param image the <code>Image</code> for which to
  1936. * prepare a screen representation.
  1937. * @param observer the <code>ImageObserver</code> object
  1938. * to be notified as the image is being prepared.
  1939. * @return <code>true</code> if the image has already been fully prepared;
  1940. <code>false</code> otherwise.
  1941. * @since JDK1.0
  1942. */
  1943. public boolean prepareImage(Image image, ImageObserver observer) {
  1944. return prepareImage(image, -1, -1, observer);
  1945. }
  1946. /**
  1947. * Prepares an image for rendering on this component at the
  1948. * specified width and height.
  1949. * <p>
  1950. * The image data is downloaded asynchronously in another thread,
  1951. * and an appropriately scaled screen representation of the image is
  1952. * generated.
  1953. * @param image the instance of <code>Image</code>
  1954. * for which to prepare a screen representation.
  1955. * @param width the width of the desired screen representation.
  1956. * @param height the height of the desired screen representation.
  1957. * @param observer the <code>ImageObserver</code> object
  1958. * to be notified as the image is being prepared.
  1959. * @return <code>true</code> if the image has already been fully prepared;
  1960. <code>false</code> otherwise.
  1961. * @see java.awt.image.ImageObserver
  1962. * @since JDK1.0
  1963. */
  1964. public boolean prepareImage(Image image, int width, int height,
  1965. ImageObserver observer) {
  1966. ComponentPeer peer = this.peer;
  1967. if (peer instanceof java.awt.peer.LightweightPeer) {
  1968. return parent.prepareImage(image, width, height, observer);
  1969. } else {
  1970. return (peer != null)
  1971. ? peer.prepareImage(image, width, height, observer)
  1972. : getToolkit().prepareImage(image, width, height, observer);
  1973. }
  1974. }
  1975. /**
  1976. * Returns the status of the construction of a screen representation
  1977. * of the specified image.
  1978. * <p>
  1979. * This method does not cause the image to begin loading. An
  1980. * application must use the <code>prepareImage</code> method
  1981. * to force the loading of an image.
  1982. * <p>
  1983. * Information on the flags returned by this method can be found
  1984. * with the discussion of the <code>ImageObserver</code> interface.
  1985. * @param image the <code>Image</code> object whose status
  1986. * is being checked.
  1987. * @param observer the <code>ImageObserver</code>
  1988. * object to be notified as the image is being prepared.
  1989. * @return the bitwise inclusive <b>OR</b> of
  1990. * <code>ImageObserver</code> flags indicating what
  1991. * information about the image is currently available.
  1992. * @see java.awt.Component#prepareImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  1993. * @see java.awt.Toolkit#checkImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  1994. * @see java.awt.image.ImageObserver
  1995. * @since JDK1.0
  1996. */
  1997. public int checkImage(Image image, ImageObserver observer) {
  1998. return checkImage(image, -1, -1, observer);
  1999. }
  2000. /**
  2001. * Returns the status of the construction of a screen representation
  2002. * of the specified image.
  2003. * <p>
  2004. * This method does not cause the image to begin loading. An
  2005. * application must use the <code>prepareImage</code> method
  2006. * to force the loading of an image.
  2007. * <p>
  2008. * The <code>checkImage</code> method of <code>Component</code>
  2009. * calls its peer's <code>checkImage</code> method to calculate
  2010. * the flags. If this component does not yet have a peer, the
  2011. * component's toolkit's <code>checkImage</code> method is called
  2012. * instead.
  2013. * <p>
  2014. * Information on the flags returned by this method can be found
  2015. * with the discussion of the <code>ImageObserver</code> interface.
  2016. * @param image the <code>Image</code> object whose status
  2017. * is being checked.
  2018. * @param width the width of the scaled version
  2019. * whose status is to be checked.
  2020. * @param height the height of the scaled version
  2021. * whose status is to be checked.
  2022. * @param observer the <code>ImageObserver</code> object
  2023. * to be notified as the image is being prepared.
  2024. * @return the bitwise inclusive <b>OR</b> of
  2025. * <code>ImageObserver</code> flags indicating what
  2026. * information about the image is currently available.
  2027. * @see java.awt.Component#prepareImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  2028. * @see java.awt.Toolkit#checkImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  2029. * @see java.awt.image.ImageObserver#_top_
  2030. * @since JDK1.0
  2031. */
  2032. public int checkImage(Image image, int width, int height,
  2033. ImageObserver observer) {
  2034. ComponentPeer peer = this.peer;
  2035. if (peer instanceof java.awt.peer.LightweightPeer) {
  2036. return parent.checkImage(image, width, height, observer);
  2037. } else {
  2038. return (peer != null)
  2039. ? peer.checkImage(image, width, height, observer)
  2040. : getToolkit().checkImage(image, width, height, observer);
  2041. }
  2042. }
  2043. /**
  2044. * Checks whether this component "contains" the specified point,
  2045. * where <code>x</code> and <code>y</code> are defined to be
  2046. * relative to the coordinate system of this component.
  2047. * @param x the <i>x</i> coordinate of the point.
  2048. * @param y the <i>y</i> coordinate of the point.
  2049. * @see java.awt.Component#getComponentAt(int, int)
  2050. * @since JDK1.1
  2051. */
  2052. public boolean contains(int x, int y) {
  2053. return inside(x, y);
  2054. }
  2055. /**
  2056. * @deprecated As of JDK version 1.1,
  2057. * replaced by contains(int, int).
  2058. */
  2059. public boolean inside(int x, int y) {
  2060. return (x >= 0) && (x < width) && (y >= 0) && (y < height);
  2061. }
  2062. /**
  2063. * Checks whether this component "contains" the specified point,
  2064. * where the point's <i>x</i> and <i>y</i> coordinates are defined
  2065. * to be relative to the coordinate system of this component.
  2066. * @param p the point.
  2067. * @see java.awt.Component#getComponentAt(java.awt.Point)
  2068. * @since JDK1.1
  2069. */
  2070. public boolean contains(Point p) {
  2071. return contains(p.x, p.y);
  2072. }
  2073. /**
  2074. * Determines if this component or one of its immediate
  2075. * subcomponents contains the (<i>x</i>, <i>y</i>) location,
  2076. * and if so, returns the containing component. This method only
  2077. * looks one level deep. If the point (<i>x</i>, <i>y</i>) is
  2078. * inside a subcomponent that itself has subcomponents, it does not
  2079. * go looking down the subcomponent tree.
  2080. * <p>
  2081. * The <code>locate</code> method of <code>Component</code> simply
  2082. * returns the component itself if the (<i>x</i>, <i>y</i>)
  2083. * coordinate location is inside its bounding box, and <code>null</code>
  2084. * otherwise.
  2085. * @param x the <i>x</i> coordinate.
  2086. * @param y the <i>y</i> coordinate.
  2087. * @return the component or subcomponent that contains the
  2088. * (<i>x</i>, <i>y</i>) location;
  2089. * <code>null</code> if the location
  2090. * is outside this component.
  2091. * @see java.awt.Component#contains(int, int)
  2092. * @since JDK1.0
  2093. */
  2094. public Component getComponentAt(int x, int y) {
  2095. return locate(x, y);
  2096. }
  2097. /**
  2098. * @deprecated As of JDK version 1.1,
  2099. * replaced by getComponentAt(int, int).
  2100. */
  2101. public Component locate(int x, int y) {
  2102. return contains(x, y) ? this : null;
  2103. }
  2104. /**
  2105. * Returns the component or subcomponent that contains the
  2106. * specified point.
  2107. * @param p the point.
  2108. * @see java.awt.Component#contains
  2109. * @since JDK1.1
  2110. */
  2111. public Component getComponentAt(Point p) {
  2112. return getComponentAt(p.x, p.y);
  2113. }
  2114. /**
  2115. * @deprecated As of JDK version 1.1,
  2116. * replaced by <code>dispatchEvent(AWTEvent e)</code>.
  2117. */
  2118. public void deliverEvent(Event e) {
  2119. postEvent(e);
  2120. }
  2121. /**
  2122. * Dispatches an event to this component or one of its sub components.
  2123. * Calls processEvent() before returning for 1.1-style events which
  2124. * have been enabled for the Component.
  2125. * @param e the event
  2126. */
  2127. public final void dispatchEvent(AWTEvent e) {
  2128. dispatchEventImpl(e);
  2129. }
  2130. void dispatchEventImpl(AWTEvent e) {
  2131. int id = e.getID();
  2132. /*
  2133. * 0. Allow the Toolkit to pass this to AWTEventListeners.
  2134. */
  2135. Toolkit.getDefaultToolkit().notifyAWTEventListeners(e);
  2136. /*
  2137. * 1. Allow input methods to process the event
  2138. */
  2139. if (areInputMethodsEnabled()
  2140. && (
  2141. // For passive clients of the input method framework
  2142. // we need to pass on InputMethodEvents since some host
  2143. // input method adapters send them through the Java
  2144. // event queue instead of directly to the component,
  2145. // and the input context also handles the Java root window
  2146. ((e instanceof InputMethodEvent) && (getInputMethodRequests() == null))
  2147. ||
  2148. // Otherwise, we only pass on low-level events, because
  2149. // a) input methods shouldn't know about semantic events
  2150. // b) passing on the events takes time
  2151. // c) isConsumed() is always true for semantic events.
  2152. // We exclude paint events since they may be numerous and shouldn't matter.
  2153. (e instanceof ComponentEvent) && !(e instanceof PaintEvent))) {
  2154. InputContext inputContext = getInputContext();
  2155. if (inputContext != null) {
  2156. inputContext.dispatchEvent(e);
  2157. if (e.isConsumed()) {
  2158. return;
  2159. }
  2160. }
  2161. }
  2162. /*
  2163. * 2. Pre-process any special events before delivery
  2164. */
  2165. switch(id) {
  2166. // Handling of the PAINT and UPDATE events is now done in the
  2167. // peer's handleEvent() method so the background can be cleared
  2168. // selectively for non-native components on Windows only.
  2169. // - Fred.Ecks@Eng.sun.com, 5-8-98
  2170. case FocusEvent.FOCUS_GAINED:
  2171. if (parent != null && !(this instanceof Window)) {
  2172. parent.setFocusOwner(this);
  2173. }
  2174. break;
  2175. case FocusEvent.FOCUS_LOST:
  2176. break;
  2177. case KeyEvent.KEY_PRESSED:
  2178. case KeyEvent.KEY_RELEASED:
  2179. Container p = (Container)((this instanceof Container) ? this : parent);
  2180. if (p != null) {
  2181. p.preProcessKeyEvent((KeyEvent)e);
  2182. if (e.isConsumed()) {
  2183. return;
  2184. }
  2185. }
  2186. break;
  2187. /*
  2188. case MouseEvent.MOUSE_PRESSED:
  2189. if (isFocusTraversable()) {
  2190. requestFocus();
  2191. }
  2192. break;
  2193. */
  2194. default:
  2195. break;
  2196. }
  2197. /*
  2198. * 3. Deliver event for normal processing
  2199. */
  2200. if (newEventsOnly) {
  2201. // Filtering needs to really be moved to happen at a lower
  2202. // level in order to get maximum performance gain; it is
  2203. // here temporarily to ensure the API spec is honored.
  2204. //
  2205. if (eventEnabled(e)) {
  2206. processEvent(e);
  2207. }
  2208. } else if (!(e instanceof MouseEvent && !postsOldMouseEvents())) {
  2209. //
  2210. // backward compatibility
  2211. //
  2212. Event olde = e.convertToOld();
  2213. if (olde != null) {
  2214. int key = olde.key;
  2215. int modifiers = olde.modifiers;
  2216. postEvent(olde);
  2217. if (olde.isConsumed()) {
  2218. e.consume();
  2219. }
  2220. // if target changed key or modifier values, copy them
  2221. // back to original event
  2222. //
  2223. switch(olde.id) {
  2224. case Event.KEY_PRESS:
  2225. case Event.KEY_RELEASE:
  2226. case Event.KEY_ACTION:
  2227. case Event.KEY_ACTION_RELEASE:
  2228. if (olde.key != key) {
  2229. ((KeyEvent)e).setKeyChar(olde.getKeyEventChar());
  2230. }
  2231. if (olde.modifiers != modifiers) {
  2232. ((KeyEvent)e).setModifiers(olde.modifiers);
  2233. }
  2234. break;
  2235. default:
  2236. break;
  2237. }
  2238. }
  2239. }
  2240. /*
  2241. * 4. If no one has consumed a key event, propagate it
  2242. * up the containment hierarchy to ensure that menu shortcuts
  2243. * and keyboard traversal will work properly.
  2244. */
  2245. if (!e.isConsumed() && e instanceof java.awt.event.KeyEvent) {
  2246. Container p = (Container)((this instanceof Container) ? this : parent);
  2247. if (p != null) {
  2248. p.postProcessKeyEvent((KeyEvent)e);
  2249. }
  2250. }
  2251. /*
  2252. * 5. Allow the peer to process the event
  2253. */
  2254. if (peer != null) {
  2255. peer.handleEvent(e);
  2256. }
  2257. } // dispatchEventImpl()
  2258. boolean areInputMethodsEnabled() {
  2259. // in 1.2, we assume input method support is required for all
  2260. // components that handle key events, but components can turn off
  2261. // input methods by calling enableInputMethods(false).
  2262. return ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0) &&
  2263. ((eventMask & AWTEvent.KEY_EVENT_MASK) != 0 || keyListener != null);
  2264. }
  2265. // REMIND: remove when filtering is handled at lower level
  2266. boolean eventEnabled(AWTEvent e) {
  2267. switch(e.id) {
  2268. case ComponentEvent.COMPONENT_MOVED:
  2269. case ComponentEvent.COMPONENT_RESIZED:
  2270. case ComponentEvent.COMPONENT_SHOWN:
  2271. case ComponentEvent.COMPONENT_HIDDEN:
  2272. if ((eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
  2273. componentListener != null) {
  2274. return true;
  2275. }
  2276. break;
  2277. case FocusEvent.FOCUS_GAINED:
  2278. case FocusEvent.FOCUS_LOST:
  2279. if ((eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0 ||
  2280. focusListener != null) {
  2281. return true;
  2282. }
  2283. break;
  2284. case KeyEvent.KEY_PRESSED:
  2285. case KeyEvent.KEY_RELEASED:
  2286. case KeyEvent.KEY_TYPED:
  2287. if ((eventMask & AWTEvent.KEY_EVENT_MASK) != 0 ||
  2288. keyListener != null) {
  2289. return true;
  2290. }
  2291. break;
  2292. case MouseEvent.MOUSE_PRESSED:
  2293. case MouseEvent.MOUSE_RELEASED:
  2294. case MouseEvent.MOUSE_ENTERED:
  2295. case MouseEvent.MOUSE_EXITED:
  2296. case MouseEvent.MOUSE_CLICKED:
  2297. if ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0 ||
  2298. mouseListener != null) {
  2299. return true;
  2300. }
  2301. break;
  2302. case MouseEvent.MOUSE_MOVED:
  2303. case MouseEvent.MOUSE_DRAGGED:
  2304. if ((eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0 ||
  2305. mouseMotionListener != null) {
  2306. return true;
  2307. }
  2308. break;
  2309. case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
  2310. case InputMethodEvent.CARET_POSITION_CHANGED:
  2311. if ((eventMask & AWTEvent.INPUT_METHOD_EVENT_MASK) != 0 ||
  2312. inputMethodListener != null) {
  2313. return true;
  2314. }
  2315. break;
  2316. default:
  2317. break;
  2318. }
  2319. //
  2320. // Always pass on events defined by external programs.
  2321. //
  2322. if (e.id > AWTEvent.RESERVED_ID_MAX) {
  2323. return true;
  2324. }
  2325. return false;
  2326. }
  2327. /**
  2328. * Returns the Window subclass that contains this object. Will
  2329. * return the object itself, if it is a window.
  2330. */
  2331. private Window getWindowForObject(Object obj) {
  2332. if (obj instanceof Component) {
  2333. while (obj != null) {
  2334. if (obj instanceof Window) {
  2335. return (Window)obj;
  2336. }
  2337. obj = ((Component)obj).getParent();
  2338. }
  2339. }
  2340. return null;
  2341. } // getWindowForObject()
  2342. /**
  2343. * @deprecated As of JDK version 1.1,
  2344. * replaced by dispatchEvent(AWTEvent).
  2345. */
  2346. public boolean postEvent(Event e) {
  2347. ComponentPeer peer = this.peer;
  2348. if (handleEvent(e)) {
  2349. e.consume();
  2350. return true;
  2351. }
  2352. Component parent = this.parent;
  2353. int eventx = e.x;
  2354. int eventy = e.y;
  2355. if (parent != null) {
  2356. e.translate(x, y);
  2357. if (parent.postEvent(e)) {
  2358. e.consume();
  2359. return true;
  2360. }
  2361. // restore coords
  2362. e.x = eventx;
  2363. e.y = eventy;
  2364. }
  2365. return false;
  2366. }
  2367. // Event source interfaces
  2368. /**
  2369. * Adds the specified component listener to receive component events from
  2370. * this component.
  2371. * If l is null, no exception is thrown and no action is performed.
  2372. * @param l the component listener.
  2373. * @see java.awt.event.ComponentEvent
  2374. * @see java.awt.event.ComponentListener
  2375. * @see java.awt.Component#removeComponentListener
  2376. * @since JDK1.1
  2377. */
  2378. public synchronized void addComponentListener(ComponentListener l) {
  2379. if (l == null) {
  2380. return;
  2381. }
  2382. componentListener = AWTEventMulticaster.add(componentListener, l);
  2383. newEventsOnly = true;
  2384. }
  2385. /**
  2386. * Removes the specified component listener so that it no longer
  2387. * receives component events from this component. This method performs
  2388. * no function, nor does it throw an exception, if the listener
  2389. * specified by the argument was not previously added to this component.
  2390. * If l is null, no exception is thrown and no action is performed.
  2391. * @param l the component listener.
  2392. * @see java.awt.event.ComponentEvent
  2393. * @see java.awt.event.ComponentListener
  2394. * @see java.awt.Component#addComponentListener
  2395. * @since JDK1.1
  2396. */
  2397. public synchronized void removeComponentListener(ComponentListener l) {
  2398. if (l == null) {
  2399. return;
  2400. }
  2401. componentListener = AWTEventMulticaster.remove(componentListener, l);
  2402. }
  2403. /**
  2404. * Adds the specified focus listener to receive focus events from
  2405. * this component when this component gains input focus.
  2406. * If l is null, no exception is thrown and no action is performed.
  2407. *
  2408. * @param l the focus listener.
  2409. * @see java.awt.event.FocusEvent
  2410. * @see java.awt.event.FocusListener
  2411. * @see java.awt.Component#removeFocusListener
  2412. * @since JDK1.1
  2413. */
  2414. public synchronized void addFocusListener(FocusListener l) {
  2415. if (l == null) {
  2416. return;
  2417. }
  2418. focusListener = AWTEventMulticaster.add(focusListener, l);
  2419. newEventsOnly = true;
  2420. // if this is a lightweight component, enable focus events
  2421. // in the native container.
  2422. if (peer instanceof java.awt.peer.LightweightPeer) {
  2423. parent.proxyEnableEvents(AWTEvent.FOCUS_EVENT_MASK);
  2424. }
  2425. }
  2426. /**
  2427. * Removes the specified focus listener so that it no longer
  2428. * receives focus events from this component. This method performs
  2429. * no function, nor does it throw an exception, if the listener
  2430. * specified by the argument was not previously added to this component.
  2431. * If l is null, no exception is thrown and no action is performed.
  2432. *
  2433. * @param l the focus listener.
  2434. * @see java.awt.event.FocusEvent
  2435. * @see java.awt.event.FocusListener
  2436. * @see java.awt.Component#addFocusListener
  2437. * @since JDK1.1
  2438. */
  2439. public synchronized void removeFocusListener(FocusListener l) {
  2440. if (l == null) {
  2441. return;
  2442. }
  2443. focusListener = AWTEventMulticaster.remove(focusListener, l);
  2444. }
  2445. /**
  2446. * Adds the specified key listener to receive key events from
  2447. * this component.
  2448. * If l is null, no exception is thrown and no action is performed.
  2449. *
  2450. * @param l the key listener.
  2451. * @see java.awt.event.KeyEvent
  2452. * @see java.awt.event.KeyListener
  2453. * @see java.awt.Component#removeKeyListener
  2454. * @since JDK1.1
  2455. */
  2456. public synchronized void addKeyListener(KeyListener l) {
  2457. if (l == null) {
  2458. return;
  2459. }
  2460. keyListener = AWTEventMulticaster.add(keyListener, l);
  2461. newEventsOnly = true;
  2462. // if this is a lightweight component, enable key events
  2463. // in the native container.
  2464. if (peer instanceof java.awt.peer.LightweightPeer) {
  2465. parent.proxyEnableEvents(AWTEvent.KEY_EVENT_MASK);
  2466. }
  2467. }
  2468. /**
  2469. * Removes the specified key listener so that it no longer
  2470. * receives key events from this component. This method performs
  2471. * no function, nor does it throw an exception, if the listener
  2472. * specified by the argument was not previously added to this component.
  2473. * If l is null, no exception is thrown and no action is performed.
  2474. *
  2475. * @param l the key listener.
  2476. * @see java.awt.event.KeyEvent
  2477. * @see java.awt.event.KeyListener
  2478. * @see java.awt.Component#addKeyListener
  2479. * @since JDK1.1
  2480. */
  2481. public synchronized void removeKeyListener(KeyListener l) {
  2482. if (l == null) {
  2483. return;
  2484. }
  2485. keyListener = AWTEventMulticaster.remove(keyListener, l);
  2486. }
  2487. /**
  2488. * Adds the specified mouse listener to receive mouse events from
  2489. * this component.
  2490. * If l is null, no exception is thrown and no action is performed.
  2491. *
  2492. * @param l the mouse listener.
  2493. * @see java.awt.event.MouseEvent
  2494. * @see java.awt.event.MouseListener
  2495. * @see java.awt.Component#removeMouseListener
  2496. * @since JDK1.1
  2497. */
  2498. public synchronized void addMouseListener(MouseListener l) {
  2499. if (l == null) {
  2500. return;
  2501. }
  2502. mouseListener = AWTEventMulticaster.add(mouseListener,l);
  2503. newEventsOnly = true;
  2504. // if this is a lightweight component, enable mouse events
  2505. // in the native container.
  2506. if (peer instanceof java.awt.peer.LightweightPeer) {
  2507. parent.proxyEnableEvents(AWTEvent.MOUSE_EVENT_MASK);
  2508. }
  2509. }
  2510. /**
  2511. * Removes the specified mouse listener so that it no longer
  2512. * receives mouse events from this component. This method performs
  2513. * no function, nor does it throw an exception, if the listener
  2514. * specified by the argument was not previously added to this component.
  2515. * If l is null, no exception is thrown and no action is performed.
  2516. *
  2517. * @param l the mouse listener.
  2518. * @see java.awt.event.MouseEvent
  2519. * @see java.awt.event.MouseListener
  2520. * @see java.awt.Component#addMouseListener
  2521. * @since JDK1.1
  2522. */
  2523. public synchronized void removeMouseListener(MouseListener l) {
  2524. if (l == null) {
  2525. return;
  2526. }
  2527. mouseListener = AWTEventMulticaster.remove(mouseListener, l);
  2528. }
  2529. /**
  2530. * Adds the specified mouse motion listener to receive mouse motion events from
  2531. * this component.
  2532. * If l is null, no exception is thrown and no action is performed.
  2533. *
  2534. * @param l the mouse motion listener.
  2535. * @see java.awt.event.MouseMotionEvent
  2536. * @see java.awt.event.MouseMotionListener
  2537. * @see java.awt.Component#removeMouseMotionListener
  2538. * @since JDK1.1
  2539. */
  2540. public synchronized void addMouseMotionListener(MouseMotionListener l) {
  2541. if (l == null) {
  2542. return;
  2543. }
  2544. mouseMotionListener = AWTEventMulticaster.add(mouseMotionListener,l);
  2545. newEventsOnly = true;
  2546. // if this is a lightweight component, enable mouse events
  2547. // in the native container.
  2548. if (peer instanceof java.awt.peer.LightweightPeer) {
  2549. parent.proxyEnableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
  2550. }
  2551. }
  2552. /**
  2553. * Removes the specified mouse motion listener so that it no longer
  2554. * receives mouse motion events from this component. This method performs
  2555. * no function, nor does it throw an exception, if the listener
  2556. * specified by the argument was not previously added to this component.
  2557. * If l is null, no exception is thrown and no action is performed.
  2558. *
  2559. * @param l the mouse motion listener.
  2560. * @see java.awt.event.MouseMotionEvent
  2561. * @see java.awt.event.MouseMotionListener
  2562. * @see java.awt.Component#addMouseMotionListener
  2563. * @since JDK1.1
  2564. */
  2565. public synchronized void removeMouseMotionListener(MouseMotionListener l) {
  2566. if (l == null) {
  2567. return;
  2568. }
  2569. mouseMotionListener = AWTEventMulticaster.remove(mouseMotionListener, l);
  2570. }
  2571. /**
  2572. * Adds the specified input method listener to receive
  2573. * input method events from this component. A component will
  2574. * only receive input method events from input methods
  2575. * if it also overrides getInputMethodRequests to return an
  2576. * InputMethodRequests instance.
  2577. * If l is null, no exception is thrown and no action is performed.
  2578. *
  2579. * @param l the input method listener.
  2580. * @see java.awt.event.InputMethodEvent
  2581. * @see java.awt.event.InputMethodListener
  2582. * @see java.awt.Component#removeInputMethodListener
  2583. * @see java.awt.Component#getInputMethodRequests
  2584. * @since JDK1.2
  2585. */
  2586. public synchronized void addInputMethodListener(InputMethodListener l) {
  2587. if (l == null) {
  2588. return;
  2589. }
  2590. inputMethodListener = AWTEventMulticaster.add(inputMethodListener, l);
  2591. newEventsOnly = true;
  2592. }
  2593. /**
  2594. * Removes the specified input method listener so that it no longer receives
  2595. * input method events from this component. This method performs
  2596. * no function, nor does it throw an exception, if the listener
  2597. * specified by the argument was not previously added to this component.
  2598. * If l is null, no exception is thrown and no action is performed.
  2599. *
  2600. * @param l the input method listener.
  2601. * @see java.awt.event.InputMethodEvent
  2602. * @see java.awt.event.InputMethodListener
  2603. * @see java.awt.Component#addInputMethodListener
  2604. * @since JDK1.2
  2605. */
  2606. public synchronized void removeInputMethodListener(InputMethodListener l) {
  2607. if (l == null) {
  2608. return;
  2609. }
  2610. inputMethodListener = AWTEventMulticaster.remove(inputMethodListener, l);
  2611. }
  2612. /**
  2613. * Gets the input method request handler which supports
  2614. * requests from input methods for this component. A component
  2615. * that supports on-the-spot text input must override this
  2616. * method to return an InputMethodRequests instance. At the same
  2617. * time, it also has to handle input method events.
  2618. *
  2619. * @return the input method request handler for this component,
  2620. * null by default.
  2621. * @see #addInputMethodListener
  2622. * @since JDK1.2
  2623. */
  2624. public InputMethodRequests getInputMethodRequests() {
  2625. return null;
  2626. }
  2627. /**
  2628. * Gets the input context used by this component for handling the communication
  2629. * with input methods when text is entered in this component. By default, the
  2630. * input context used for the parent component is returned. Components may
  2631. * override this to return a private input context.
  2632. *
  2633. * @return The input context used by this component. Null if no context can
  2634. * be determined.
  2635. * @since JDK1.2
  2636. */
  2637. public InputContext getInputContext() {
  2638. Container parent = this.parent;
  2639. if (parent == null) {
  2640. return null;
  2641. } else {
  2642. return parent.getInputContext();
  2643. }
  2644. }
  2645. /**
  2646. * Enables the events defined by the specified event mask parameter
  2647. * to be delivered to this component.
  2648. * <p>
  2649. * Event types are automatically enabled when a listener for
  2650. * that event type is added to the component.
  2651. * <p>
  2652. * This method only needs to be invoked by subclasses of
  2653. * <code>Component</code> which desire to have the specified event
  2654. * types delivered to <code>processEvent</code> regardless of whether
  2655. * or not a listener is registered.
  2656. * @param eventsToEnable the event mask defining the event types.
  2657. * @see java.awt.Component#processEvent
  2658. * @see java.awt.Component#disableEvents
  2659. * @since JDK1.1
  2660. */
  2661. protected final void enableEvents(long eventsToEnable) {
  2662. eventMask |= eventsToEnable;
  2663. newEventsOnly = true;
  2664. // if this is a lightweight component, enable mouse events
  2665. // in the native container.
  2666. if (peer instanceof java.awt.peer.LightweightPeer) {
  2667. parent.proxyEnableEvents(eventMask);
  2668. }
  2669. }
  2670. /**
  2671. * Disables the events defined by the specified event mask parameter
  2672. * from being delivered to this component.
  2673. * @param eventsToDisable the event mask defining the event types
  2674. * @see java.awt.Component#enableEvents
  2675. * @since JDK1.1
  2676. */
  2677. protected final void disableEvents(long eventsToDisable) {
  2678. eventMask &= ~eventsToDisable;
  2679. }
  2680. /**
  2681. * Potentially coalesce an event being posted with an existing
  2682. * event. This method is called by EventQueue.postEvent if an
  2683. * event with the same ID as the event to be posted is found in
  2684. * the queue (both events must have this component as their source).
  2685. * This method either returns a coalesced event which replaces
  2686. * the existing event (and the new event is then discarded), or
  2687. * null to indicate that no combining should be done (add the
  2688. * second event to the end of the queue). Either event parameter
  2689. * may be modified and returned, as the other one is discarded
  2690. * unless null is returned.
  2691. * <p>
  2692. * This implementation of coalesceEvents coalesces two event types:
  2693. * mouse move (and drag) events, and paint (and update) events.
  2694. * For mouse move events the last event is always returned, causing
  2695. * intermediate moves to be discarded. For paint events where the
  2696. * update rectangle of one paint event is completely contained within
  2697. * the update rectangle of the other paint event, the event with the
  2698. * smaller rectangle is discarded.
  2699. *
  2700. * @param existingEvent the event already on the EventQueue.
  2701. * @param newEvent the event being posted to the EventQueue.
  2702. * @return a coalesced event, or null indicating that no coalescing
  2703. * was done.
  2704. */
  2705. protected AWTEvent coalesceEvents(AWTEvent existingEvent,
  2706. AWTEvent newEvent) {
  2707. int id = existingEvent.getID();
  2708. if (assert) {
  2709. // Enable assert to perform this extra sanity check, but it's too
  2710. // expensive for normal operation.
  2711. if (id != newEvent.getID() ||
  2712. !(existingEvent.getSource().equals(newEvent.getSource()))) {
  2713. // Only coalesce events of the same type and source.
  2714. return null;
  2715. }
  2716. }
  2717. switch (id) {
  2718. case Event.MOUSE_MOVE:
  2719. case Event.MOUSE_DRAG: {
  2720. MouseEvent e = (MouseEvent)existingEvent;
  2721. if (e.getModifiers() == ((MouseEvent)newEvent).getModifiers()) {
  2722. // Just return the newEvent, causing the old to be
  2723. // discarded.
  2724. return newEvent;
  2725. }
  2726. break;
  2727. }
  2728. case PaintEvent.PAINT:
  2729. case PaintEvent.UPDATE: {
  2730. // This approach to coalescing paint events seems to be
  2731. // better than any heuristic for unioning rectangles.
  2732. PaintEvent existingPaintEvent = (PaintEvent) existingEvent;
  2733. PaintEvent newPaintEvent = (PaintEvent) newEvent;
  2734. Rectangle existingRect = existingPaintEvent.getUpdateRect();
  2735. Rectangle newRect = newPaintEvent.getUpdateRect();
  2736. if (existingRect.contains(newRect)) {
  2737. return existingEvent;
  2738. }
  2739. if (newRect.contains(existingRect)) {
  2740. return newEvent;
  2741. }
  2742. break;
  2743. }
  2744. }
  2745. return null;
  2746. }
  2747. /**
  2748. * Processes events occurring on this component. By default this
  2749. * method calls the appropriate
  2750. * <code>process<event type>Event</code>
  2751. * method for the given class of event.
  2752. * @param e the event.
  2753. * @see java.awt.Component#processComponentEvent
  2754. * @see java.awt.Component#processFocusEvent
  2755. * @see java.awt.Component#processKeyEvent
  2756. * @see java.awt.Component#processMouseEvent
  2757. * @see java.awt.Component#processMouseMotionEvent
  2758. * @see java.awt.Component#processInputMethodEvent
  2759. * @since JDK1.1
  2760. */
  2761. protected void processEvent(AWTEvent e) {
  2762. //System.err.println("Component.processNewEvent:" + e);
  2763. if (e instanceof FocusEvent) {
  2764. processFocusEvent((FocusEvent)e);
  2765. } else if (e instanceof MouseEvent) {
  2766. switch(e.getID()) {
  2767. case MouseEvent.MOUSE_PRESSED:
  2768. case MouseEvent.MOUSE_RELEASED:
  2769. case MouseEvent.MOUSE_CLICKED:
  2770. case MouseEvent.MOUSE_ENTERED:
  2771. case MouseEvent.MOUSE_EXITED:
  2772. processMouseEvent((MouseEvent)e);
  2773. break;
  2774. case MouseEvent.MOUSE_MOVED:
  2775. case MouseEvent.MOUSE_DRAGGED:
  2776. processMouseMotionEvent((MouseEvent)e);
  2777. break;
  2778. }
  2779. } else if (e instanceof KeyEvent) {
  2780. processKeyEvent((KeyEvent)e);
  2781. } else if (e instanceof ComponentEvent) {
  2782. processComponentEvent((ComponentEvent)e);
  2783. } else if (e instanceof InputMethodEvent) {
  2784. processInputMethodEvent((InputMethodEvent)e);
  2785. }
  2786. }
  2787. /**
  2788. * Processes component events occurring on this component by
  2789. * dispatching them to any registered
  2790. * <code>ComponentListener</code> objects.
  2791. * <p>
  2792. * This method is not called unless component events are
  2793. * enabled for this component. Component events are enabled
  2794. * when one of the following occurs:
  2795. * <p><ul>
  2796. * <li>A <code>ComponentListener</code> object is registered
  2797. * via <code>addComponentListener</code>.
  2798. * <li>Component events are enabled via <code>enableEvents</code>.
  2799. * </ul>
  2800. * @param e the component event.
  2801. * @see java.awt.event.ComponentEvent
  2802. * @see java.awt.event.ComponentListener
  2803. * @see java.awt.Component#addComponentListener
  2804. * @see java.awt.Component#enableEvents
  2805. * @since JDK1.1
  2806. */
  2807. protected void processComponentEvent(ComponentEvent e) {
  2808. ComponentListener listener = componentListener;
  2809. if (listener != null) {
  2810. int id = e.getID();
  2811. switch(id) {
  2812. case ComponentEvent.COMPONENT_RESIZED:
  2813. listener.componentResized(e);
  2814. break;
  2815. case ComponentEvent.COMPONENT_MOVED:
  2816. listener.componentMoved(e);
  2817. break;
  2818. case ComponentEvent.COMPONENT_SHOWN:
  2819. listener.componentShown(e);
  2820. break;
  2821. case ComponentEvent.COMPONENT_HIDDEN:
  2822. listener.componentHidden(e);
  2823. break;
  2824. }
  2825. }
  2826. }
  2827. /**
  2828. * Processes focus events occurring on this component by
  2829. * dispatching them to any registered
  2830. * <code>FocusListener</code> objects.
  2831. * <p>
  2832. * This method is not called unless focus events are
  2833. * enabled for this component. Focus events are enabled
  2834. * when one of the following occurs:
  2835. * <p><ul>
  2836. * <li>A <code>FocusListener</code> object is registered
  2837. * via <code>addFocusListener</code>.
  2838. * <li>Focus events are enabled via <code>enableEvents</code>.
  2839. * </ul>
  2840. * @param e the focus event.
  2841. * @see java.awt.event.FocusEvent
  2842. * @see java.awt.event.FocusListener
  2843. * @see java.awt.Component#addFocusListener
  2844. * @see java.awt.Component#enableEvents
  2845. * @since JDK1.1
  2846. */
  2847. protected void processFocusEvent(FocusEvent e) {
  2848. FocusListener listener = focusListener;
  2849. if (listener != null) {
  2850. int id = e.getID();
  2851. switch(id) {
  2852. case FocusEvent.FOCUS_GAINED:
  2853. listener.focusGained(e);
  2854. break;
  2855. case FocusEvent.FOCUS_LOST:
  2856. listener.focusLost(e);
  2857. break;
  2858. }
  2859. }
  2860. }
  2861. /**
  2862. * Processes key events occurring on this component by
  2863. * dispatching them to any registered
  2864. * <code>KeyListener</code> objects.
  2865. * <p>
  2866. * This method is not called unless key events are
  2867. * enabled for this component. Key events are enabled
  2868. * when one of the following occurs:
  2869. * <p><ul>
  2870. * <li>A <code>KeyListener</code> object is registered
  2871. * via <code>addKeyListener</code>.
  2872. * <li>Key events are enabled via <code>enableEvents</code>.
  2873. * </ul>
  2874. * @param e the key event.
  2875. * @see java.awt.event.KeyEvent
  2876. * @see java.awt.event.KeyListener
  2877. * @see java.awt.Component#addKeyListener
  2878. * @see java.awt.Component#enableEvents
  2879. * @since JDK1.1
  2880. */
  2881. protected void processKeyEvent(KeyEvent e) {
  2882. KeyListener listener = keyListener;
  2883. if (listener != null) {
  2884. int id = e.getID();
  2885. switch(id) {
  2886. case KeyEvent.KEY_TYPED:
  2887. listener.keyTyped(e);
  2888. break;
  2889. case KeyEvent.KEY_PRESSED:
  2890. listener.keyPressed(e);
  2891. break;
  2892. case KeyEvent.KEY_RELEASED:
  2893. listener.keyReleased(e);
  2894. break;
  2895. }
  2896. }
  2897. }
  2898. /**
  2899. * Processes mouse events occurring on this component by
  2900. * dispatching them to any registered
  2901. * <code>MouseListener</code> objects.
  2902. * <p>
  2903. * This method is not called unless mouse events are
  2904. * enabled for this component. Mouse events are enabled
  2905. * when one of the following occurs:
  2906. * <p><ul>
  2907. * <li>A <code>MouseListener</code> object is registered
  2908. * via <code>addMouseListener</code>.
  2909. * <li>Mouse events are enabled via <code>enableEvents</code>.
  2910. * </ul>
  2911. * @param e the mouse event.
  2912. * @see java.awt.event.MouseEvent
  2913. * @see java.awt.event.MouseListener
  2914. * @see java.awt.Component#addMouseListener
  2915. * @see java.awt.Component#enableEvents
  2916. * @since JDK1.1
  2917. */
  2918. protected void processMouseEvent(MouseEvent e) {
  2919. MouseListener listener = mouseListener;
  2920. if (listener != null) {
  2921. int id = e.getID();
  2922. switch(id) {
  2923. case MouseEvent.MOUSE_PRESSED:
  2924. listener.mousePressed(e);
  2925. break;
  2926. case MouseEvent.MOUSE_RELEASED:
  2927. listener.mouseReleased(e);
  2928. break;
  2929. case MouseEvent.MOUSE_CLICKED:
  2930. listener.mouseClicked(e);
  2931. break;
  2932. case MouseEvent.MOUSE_EXITED:
  2933. listener.mouseExited(e);
  2934. break;
  2935. case MouseEvent.MOUSE_ENTERED:
  2936. listener.mouseEntered(e);
  2937. break;
  2938. }
  2939. }
  2940. }
  2941. /**
  2942. * Processes mouse motion events occurring on this component by
  2943. * dispatching them to any registered
  2944. * <code>MouseMotionListener</code> objects.
  2945. * <p>
  2946. * This method is not called unless mouse motion events are
  2947. * enabled for this component. Mouse motion events are enabled
  2948. * when one of the following occurs:
  2949. * <p><ul>
  2950. * <li>A <code>MouseMotionListener</code> object is registered
  2951. * via <code>addMouseMotionListener</code>.
  2952. * <li>Mouse motion events are enabled via <code>enableEvents</code>.
  2953. * </ul>
  2954. * @param e the mouse motion event.
  2955. * @see java.awt.event.MouseMotionEvent
  2956. * @see java.awt.event.MouseMotionListener
  2957. * @see java.awt.Component#addMouseMotionListener
  2958. * @see java.awt.Component#enableEvents
  2959. * @since JDK1.1
  2960. */
  2961. protected void processMouseMotionEvent(MouseEvent e) {
  2962. MouseMotionListener listener = mouseMotionListener;
  2963. if (listener != null) {
  2964. int id = e.getID();
  2965. switch(id) {
  2966. case MouseEvent.MOUSE_MOVED:
  2967. listener.mouseMoved(e);
  2968. break;
  2969. case MouseEvent.MOUSE_DRAGGED:
  2970. listener.mouseDragged(e);
  2971. break;
  2972. }
  2973. }
  2974. }
  2975. boolean postsOldMouseEvents() {
  2976. return false;
  2977. }
  2978. /**
  2979. * Processes input method events occurring on this component by
  2980. * dispatching them to any registered
  2981. * <code>InputMethodListener</code> objects.
  2982. * <p>
  2983. * This method is not called unless input method events
  2984. * are enabled for this component. Input method events are enabled
  2985. * when one of the following occurs:
  2986. * <p><ul>
  2987. * <li>An <code>InputMethodListener</code> object is registered
  2988. * via <code>addInputMethodListener</code>.
  2989. * <li>Input method events are enabled via <code>enableEvents</code>.
  2990. * </ul>
  2991. * @param e the input method event
  2992. * @see java.awt.event.InputMethodEvent
  2993. * @see java.awt.event.InputMethodListener
  2994. * @see java.awt.Component#addInputMethodListener
  2995. * @see java.awt.Component#enableEvents
  2996. * @since JDK1.2
  2997. */
  2998. protected void processInputMethodEvent(InputMethodEvent e) {
  2999. InputMethodListener listener = inputMethodListener;
  3000. if (listener != null) {
  3001. int id = e.getID();
  3002. switch (id) {
  3003. case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
  3004. listener.inputMethodTextChanged(e);
  3005. break;
  3006. case InputMethodEvent.CARET_POSITION_CHANGED:
  3007. listener.caretPositionChanged(e);
  3008. break;
  3009. }
  3010. }
  3011. }
  3012. /**
  3013. * @deprecated As of JDK version 1.1
  3014. * replaced by processEvent(AWTEvent).
  3015. */
  3016. public boolean handleEvent(Event evt) {
  3017. switch (evt.id) {
  3018. case Event.MOUSE_ENTER:
  3019. return mouseEnter(evt, evt.x, evt.y);
  3020. case Event.MOUSE_EXIT:
  3021. return mouseExit(evt, evt.x, evt.y);
  3022. case Event.MOUSE_MOVE:
  3023. return mouseMove(evt, evt.x, evt.y);
  3024. case Event.MOUSE_DOWN:
  3025. return mouseDown(evt, evt.x, evt.y);
  3026. case Event.MOUSE_DRAG:
  3027. return mouseDrag(evt, evt.x, evt.y);
  3028. case Event.MOUSE_UP:
  3029. return mouseUp(evt, evt.x, evt.y);
  3030. case Event.KEY_PRESS:
  3031. case Event.KEY_ACTION:
  3032. return keyDown(evt, evt.key);
  3033. case Event.KEY_RELEASE:
  3034. case Event.KEY_ACTION_RELEASE:
  3035. return keyUp(evt, evt.key);
  3036. case Event.ACTION_EVENT:
  3037. return action(evt, evt.arg);
  3038. case Event.GOT_FOCUS:
  3039. return gotFocus(evt, evt.arg);
  3040. case Event.LOST_FOCUS:
  3041. return lostFocus(evt, evt.arg);
  3042. }
  3043. return false;
  3044. }
  3045. /**
  3046. * @deprecated As of JDK version 1.1,
  3047. * replaced by processMouseEvent(MouseEvent).
  3048. */
  3049. public boolean mouseDown(Event evt, int x, int y) {
  3050. return false;
  3051. }
  3052. /**
  3053. * @deprecated As of JDK version 1.1,
  3054. * replaced by processMouseMotionEvent(MouseEvent).
  3055. */
  3056. public boolean mouseDrag(Event evt, int x, int y) {
  3057. return false;
  3058. }
  3059. /**
  3060. * @deprecated As of JDK version 1.1,
  3061. * replaced by processMouseEvent(MouseEvent).
  3062. */
  3063. public boolean mouseUp(Event evt, int x, int y) {
  3064. return false;
  3065. }
  3066. /**
  3067. * @deprecated As of JDK version 1.1,
  3068. * replaced by processMouseMotionEvent(MouseEvent).
  3069. */
  3070. public boolean mouseMove(Event evt, int x, int y) {
  3071. return false;
  3072. }
  3073. /**
  3074. * @deprecated As of JDK version 1.1,
  3075. * replaced by processMouseEvent(MouseEvent).
  3076. */
  3077. public boolean mouseEnter(Event evt, int x, int y) {
  3078. return false;
  3079. }
  3080. /**
  3081. * @deprecated As of JDK version 1.1,
  3082. * replaced by processMouseEvent(MouseEvent).
  3083. */
  3084. public boolean mouseExit(Event evt, int x, int y) {
  3085. return false;
  3086. }
  3087. /**
  3088. * @deprecated As of JDK version 1.1,
  3089. * replaced by processKeyEvent(KeyEvent).
  3090. */
  3091. public boolean keyDown(Event evt, int key) {
  3092. return false;
  3093. }
  3094. /**
  3095. * @deprecated As of JDK version 1.1,
  3096. * replaced by processKeyEvent(KeyEvent).
  3097. */
  3098. public boolean keyUp(Event evt, int key) {
  3099. return false;
  3100. }
  3101. /**
  3102. * @deprecated As of JDK version 1.1,
  3103. * should register this component as ActionListener on component
  3104. * which fires action events.
  3105. */
  3106. public boolean action(Event evt, Object what) {
  3107. return false;
  3108. }
  3109. /**
  3110. * Makes this Component displayable by connecting it to a
  3111. * native screen resource.
  3112. * This method is called internally by the toolkit and should
  3113. * not be called directly by programs.
  3114. * @see java.awt.Component#isDisplayable
  3115. * @see java.awt.Component#removeNotify
  3116. * @since JDK1.0
  3117. */
  3118. public void addNotify() {
  3119. synchronized (getTreeLock()) {
  3120. ComponentPeer peer = this.peer;
  3121. if (peer == null || peer instanceof java.awt.peer.LightweightPeer){
  3122. if (peer == null) {
  3123. // Update both the Component's peer variable and the local
  3124. // variable we use for thread safety.
  3125. this.peer = peer = getToolkit().createComponent(this);
  3126. }
  3127. // This is a lightweight component which means it won't be
  3128. // able to get window-related events by itself. If any
  3129. // have been enabled, then the nearest native container must
  3130. // be enabled.
  3131. long mask = 0;
  3132. if ((mouseListener != null) || ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0)) {
  3133. mask |= AWTEvent.MOUSE_EVENT_MASK;
  3134. }
  3135. if ((mouseMotionListener != null) ||
  3136. ((eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0)) {
  3137. mask |= AWTEvent.MOUSE_MOTION_EVENT_MASK;
  3138. }
  3139. if (focusListener != null || (eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0) {
  3140. mask |= AWTEvent.FOCUS_EVENT_MASK;
  3141. }
  3142. if (keyListener != null || (eventMask & AWTEvent.KEY_EVENT_MASK) != 0) {
  3143. mask |= AWTEvent.KEY_EVENT_MASK;
  3144. }
  3145. if (mask != 0) {
  3146. parent.proxyEnableEvents(mask);
  3147. }
  3148. } else {
  3149. // It's native. If the parent is lightweight it
  3150. // will need some help.
  3151. Container parent = this.parent;
  3152. if (parent != null && parent.peer instanceof java.awt.peer.LightweightPeer) {
  3153. new NativeInLightFixer();
  3154. }
  3155. }
  3156. invalidate();
  3157. int npopups = (popups != null? popups.size() : 0);
  3158. for (int i = 0 ; i < npopups ; i++) {
  3159. PopupMenu popup = (PopupMenu)popups.elementAt(i);
  3160. popup.addNotify();
  3161. }
  3162. for (Component p = getParent(); p != null; p = p.getParent())
  3163. if (p instanceof Window) {
  3164. if (((Window)p).getWarningString() == null) {
  3165. //!CQ set newEventsOnly if appropriate/possible?
  3166. }
  3167. break;
  3168. }
  3169. if (dropTarget != null) dropTarget.addNotify(peer);
  3170. peerFont = getFont();
  3171. }
  3172. }
  3173. /**
  3174. * Makes this Component undisplayable by destroying it native
  3175. * screen resource.
  3176. * This method is called by the toolkit internally and should
  3177. * not be called directly by programs.
  3178. * @see java.awt.Component#isDisplayable
  3179. * @see java.awt.Component#addNotify
  3180. * @since JDK1.0
  3181. */
  3182. public void removeNotify() {
  3183. synchronized (getTreeLock()) {
  3184. int npopups = (popups != null? popups.size() : 0);
  3185. for (int i = 0 ; i < npopups ; i++) {
  3186. PopupMenu popup = (PopupMenu)popups.elementAt(i);
  3187. popup.removeNotify();
  3188. }
  3189. // If there is any input context for this component, notify
  3190. // that this component is being removed. (This has to be done
  3191. // before hiding peer.)
  3192. if (areInputMethodsEnabled()) {
  3193. InputContext inputContext = getInputContext();
  3194. if (inputContext != null) {
  3195. inputContext.removeNotify(this);
  3196. }
  3197. }
  3198. ComponentPeer p = peer;
  3199. if (p != null) {
  3200. if (dropTarget != null) dropTarget.removeNotify(peer);
  3201. // Hide peer first to stop system events such as cursor moves.
  3202. if (visible) {
  3203. p.hide();
  3204. }
  3205. peer = null; // Stop peer updates.
  3206. peerFont = null;
  3207. Toolkit.getEventQueue().removeSourceEvents(this);
  3208. p.dispose();
  3209. }
  3210. }
  3211. }
  3212. /**
  3213. * @deprecated As of JDK version 1.1,
  3214. * replaced by processFocusEvent(FocusEvent).
  3215. */
  3216. public boolean gotFocus(Event evt, Object what) {
  3217. return false;
  3218. }
  3219. /**
  3220. * @deprecated As of JDK version 1.1,
  3221. * replaced by processFocusEvent(FocusEvent).
  3222. */
  3223. public boolean lostFocus(Event evt, Object what) {
  3224. return false;
  3225. }
  3226. /**
  3227. * Returns the value of a flag that indicates whether
  3228. * this component can be traversed using
  3229. * Tab or Shift-Tab keyboard focus traversal. If this method
  3230. * returns "false", this component may still request the keyboard
  3231. * focus using <code>requestFocus()</code>, but it will not automatically
  3232. * be assigned focus during tab traversal.
  3233. * @return <code>true</code> if this component is
  3234. * focus-traverable; <code>false</code> otherwise.
  3235. * @since JDK1.1
  3236. */
  3237. public boolean isFocusTraversable() {
  3238. ComponentPeer peer = this.peer;
  3239. if (peer != null) {
  3240. return peer.isFocusTraversable();
  3241. }
  3242. return false;
  3243. }
  3244. /**
  3245. * Requests that this component get the input focus.
  3246. * The component must be visible
  3247. * on the screen for this request to be granted
  3248. * @see FocusEvent
  3249. * @see #addFocusListener
  3250. * @see #processFocusEvent
  3251. * @see #isFocusTraversable
  3252. * @since JDK1.0
  3253. */
  3254. public void requestFocus() {
  3255. ComponentPeer peer = this.peer;
  3256. if (peer != null) {
  3257. if (peer instanceof java.awt.peer.LightweightPeer) {
  3258. parent.proxyRequestFocus(this);
  3259. } else {
  3260. peer.requestFocus();
  3261. Toolkit.getEventQueue().changeKeyEventFocus(this);
  3262. }
  3263. }
  3264. }
  3265. /**
  3266. * Transfers the focus to the next component.
  3267. * @see java.awt.Component#requestFocus
  3268. * @since JDK1.1s
  3269. */
  3270. public void transferFocus() {
  3271. nextFocus();
  3272. }
  3273. /**
  3274. * @deprecated As of JDK version 1.1,
  3275. * replaced by transferFocus().
  3276. */
  3277. public void nextFocus() {
  3278. Container parent = this.parent;
  3279. if (parent != null) {
  3280. parent.transferFocus(this);
  3281. }
  3282. }
  3283. /**
  3284. * Returns true if this Component has the keyboard focus.
  3285. *
  3286. * @return true if this Component has the keyboard focus.
  3287. * @since JDK1.2
  3288. */
  3289. public boolean hasFocus() {
  3290. if ((eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0) {
  3291. return hasFocus;
  3292. }
  3293. else {
  3294. for (Container p = getParent(); p != null; p = p.getParent()) {
  3295. if (p instanceof Window) {
  3296. return ((Window)p).getFocusOwner() == this;
  3297. }
  3298. }
  3299. return false;
  3300. }
  3301. }
  3302. /**
  3303. * Adds the specified popup menu to the component.
  3304. * @param popup the popup menu to be added to the component.
  3305. * @see java.awt.Component#remove(java.awt.MenuComponent)
  3306. * @since JDK1.1
  3307. */
  3308. public synchronized void add(PopupMenu popup) {
  3309. if (popup.parent != null) {
  3310. popup.parent.remove(popup);
  3311. }
  3312. if (popups == null) {
  3313. popups = new Vector();
  3314. }
  3315. popups.addElement(popup);
  3316. popup.parent = this;
  3317. if (peer != null) {
  3318. if (popup.peer == null) {
  3319. popup.addNotify();
  3320. }
  3321. }
  3322. }
  3323. /**
  3324. * Removes the specified popup menu from the component.
  3325. * @param popup the popup menu to be removed.
  3326. * @see java.awt.Component#add(java.awt.PopupMenu)
  3327. * @since JDK1.1
  3328. */
  3329. public synchronized void remove(MenuComponent popup) {
  3330. if (popups != null) {
  3331. int index = popups.indexOf(popup);
  3332. if (index >= 0) {
  3333. PopupMenu pmenu = (PopupMenu)popup;
  3334. if (pmenu.peer != null) {
  3335. pmenu.removeNotify();
  3336. }
  3337. pmenu.parent = null;
  3338. popups.removeElementAt(index);
  3339. if (popups.size() == 0) {
  3340. popups = null;
  3341. }
  3342. }
  3343. }
  3344. }
  3345. /**
  3346. * Returns a string representing the state of this component. This
  3347. * method is intended to be used only for debugging purposes, and the
  3348. * content and format of the returned string may vary between
  3349. * implementations. The returned string may be empty but may not be
  3350. * <code>null</code>.
  3351. *
  3352. * @return a string representation of this component's state.
  3353. * @since JDK1.0
  3354. */
  3355. protected String paramString() {
  3356. String thisName = getName();
  3357. String str = (thisName != null? thisName : "") + "," + x + "," + y + "," + width + "x" + height;
  3358. if (!valid) {
  3359. str += ",invalid";
  3360. }
  3361. if (!visible) {
  3362. str += ",hidden";
  3363. }
  3364. if (!enabled) {
  3365. str += ",disabled";
  3366. }
  3367. return str;
  3368. }
  3369. /**
  3370. * Returns a string representation of this component and its values.
  3371. * @return a string representation of this component.
  3372. * @since JDK1.0
  3373. */
  3374. public String toString() {
  3375. return getClass().getName() + "[" + paramString() + "]";
  3376. }
  3377. /**
  3378. * Prints a listing of this component to the standard system output
  3379. * stream <code>System.out</code>.
  3380. * @see java.lang.System#out
  3381. * @since JDK1.0
  3382. */
  3383. public void list() {
  3384. list(System.out, 0);
  3385. }
  3386. /**
  3387. * Prints a listing of this component to the specified output
  3388. * stream.
  3389. * @param out a print stream.
  3390. * @since JDK1.0
  3391. */
  3392. public void list(PrintStream out) {
  3393. list(out, 0);
  3394. }
  3395. /**
  3396. * Prints out a list, starting at the specified indention, to the
  3397. * specified print stream.
  3398. * @param out a print stream.
  3399. * @param indent number of spaces to indent.
  3400. * @see java.io.PrintStream#println(java.lang.Object)
  3401. * @since JDK1.0
  3402. */
  3403. public void list(PrintStream out, int indent) {
  3404. for (int i = 0 ; i < indent ; i++) {
  3405. out.print(" ");
  3406. }
  3407. out.println(this);
  3408. }
  3409. /**
  3410. * Prints a listing to the specified print writer.
  3411. * @param out The print writer to print to.
  3412. * @since JDK1.1
  3413. */
  3414. public void list(PrintWriter out) {
  3415. list(out, 0);
  3416. }
  3417. /**
  3418. * Prints out a list, starting at the specified indention, to
  3419. * the specified print writer.
  3420. * @param out The print writer to print to.
  3421. * @param indent The number of spaces to indent.
  3422. * @see java.io.PrintStream#println(java.lang.Object)
  3423. * @since JDK1.1
  3424. */
  3425. public void list(PrintWriter out, int indent) {
  3426. for (int i = 0 ; i < indent ; i++) {
  3427. out.print(" ");
  3428. }
  3429. out.println(this);
  3430. }
  3431. /*
  3432. * Fetch the native container somewhere higher up in the component
  3433. * tree that contains this component.
  3434. */
  3435. Container getNativeContainer() {
  3436. Container p = parent;
  3437. while (p != null && p.peer instanceof java.awt.peer.LightweightPeer) {
  3438. p = p.getParent();
  3439. }
  3440. return p;
  3441. }
  3442. /**
  3443. * Add a PropertyChangeListener to the listener list.
  3444. * The listener is registered for all properties.
  3445. * <p>
  3446. * A PropertyChangeEvent will get fired in response to an
  3447. * explicit setFont, setBackground, or SetForeground on the
  3448. * current component. Note that if the current component is
  3449. * inheriting its foreground, background, or font from its
  3450. * container, then no event will be fired in response to a
  3451. * change in the inherited property.
  3452. *
  3453. * If listener is null, no exception is thrown and no action is performed.
  3454. *
  3455. * @param listener The PropertyChangeListener to be added
  3456. */
  3457. public synchronized void addPropertyChangeListener(
  3458. PropertyChangeListener listener) {
  3459. if (listener == null) {
  3460. return;
  3461. }
  3462. if (changeSupport == null) {
  3463. changeSupport = new java.beans.PropertyChangeSupport(this);
  3464. }
  3465. changeSupport.addPropertyChangeListener(listener);
  3466. }
  3467. /**
  3468. * Remove a PropertyChangeListener from the listener list.
  3469. * This removes a PropertyChangeListener that was registered
  3470. * for all properties.
  3471. *
  3472. * If listener is null, no exception is thrown and no action is performed.
  3473. *
  3474. * @param listener The PropertyChangeListener to be removed
  3475. */
  3476. public synchronized void removePropertyChangeListener(
  3477. PropertyChangeListener listener) {
  3478. if (listener == null) {
  3479. return;
  3480. }
  3481. if (changeSupport == null) {
  3482. return;
  3483. }
  3484. changeSupport.removePropertyChangeListener(listener);
  3485. }
  3486. /**
  3487. * Add a PropertyChangeListener for a specific property. The listener
  3488. * will be invoked only when a call on firePropertyChange names that
  3489. * specific property.
  3490. *
  3491. * If listener is null, no exception is thrown and no action is performed.
  3492. *
  3493. * @param propertyName The name of the property to listen on.
  3494. * @param listener The PropertyChangeListener to be added
  3495. */
  3496. public synchronized void addPropertyChangeListener(
  3497. String propertyName,
  3498. PropertyChangeListener listener) {
  3499. if (listener == null) {
  3500. return;
  3501. }
  3502. if (changeSupport == null) {
  3503. changeSupport = new java.beans.PropertyChangeSupport(this);
  3504. }
  3505. changeSupport.addPropertyChangeListener(propertyName, listener);
  3506. }
  3507. /**
  3508. * Remove a PropertyChangeListener for a specific property.
  3509. * If listener is null, no exception is thrown and no action is performed.
  3510. *
  3511. * @param propertyName The name of the property that was listened on.
  3512. * @param listener The PropertyChangeListener to be removed
  3513. */
  3514. public synchronized void removePropertyChangeListener(
  3515. String propertyName,
  3516. PropertyChangeListener listener) {
  3517. if (listener == null) {
  3518. return;
  3519. }
  3520. if (changeSupport == null) {
  3521. return;
  3522. }
  3523. changeSupport.removePropertyChangeListener(propertyName, listener);
  3524. }
  3525. /**
  3526. * Support for reporting bound property changes. This method can be called
  3527. * when a bound property has changed and it will send the appropriate
  3528. * PropertyChangeEvent to any registered PropertyChangeListeners.
  3529. */
  3530. protected void firePropertyChange(String propertyName,
  3531. Object oldValue, Object newValue) {
  3532. java.beans.PropertyChangeSupport changeSupport = this.changeSupport;
  3533. if (changeSupport == null) {
  3534. return;
  3535. }
  3536. changeSupport.firePropertyChange(propertyName, oldValue, newValue);
  3537. }
  3538. /* Serialization support.
  3539. */
  3540. /**
  3541. * Component Serialized Data Version.
  3542. *
  3543. * @serial
  3544. */
  3545. private int componentSerializedDataVersion = 2;
  3546. /**
  3547. * Writes default serializable fields to stream. Writes
  3548. * a list of serializable ItemListener(s) as optional data.
  3549. * The non-serializable ItemListener(s) are detected and
  3550. * no attempt is made to serialize them.
  3551. *
  3552. * @serialData Null terminated sequence of 0 or more pairs.
  3553. * The pair consists of a String and Object.
  3554. * The String indicates the type of object and
  3555. * is one of the following :
  3556. * itemListenerK indicating and ItemListener object.
  3557. *
  3558. * @see AWTEventMulticaster.save(ObjectOutputStream, String, EventListener)
  3559. * @see java.awt.Component.itemListenerK
  3560. */
  3561. private void writeObject(ObjectOutputStream s)
  3562. throws IOException
  3563. {
  3564. s.defaultWriteObject();
  3565. AWTEventMulticaster.save(s, componentListenerK, componentListener);
  3566. AWTEventMulticaster.save(s, focusListenerK, focusListener);
  3567. AWTEventMulticaster.save(s, keyListenerK, keyListener);
  3568. AWTEventMulticaster.save(s, mouseListenerK, mouseListener);
  3569. AWTEventMulticaster.save(s, mouseMotionListenerK, mouseMotionListener);
  3570. AWTEventMulticaster.save(s, inputMethodListenerK, inputMethodListener);
  3571. s.writeObject(null);
  3572. s.writeObject(componentOrientation);
  3573. }
  3574. /**
  3575. * Read the ObjectInputStream and if it isnt null
  3576. * add a listener to receive item events fired
  3577. * by the components.
  3578. * Unrecognised keys or values will be Ignored.
  3579. *
  3580. * @see removeActionListener()
  3581. * @see addActionListener()
  3582. */
  3583. private void readObject(ObjectInputStream s)
  3584. throws ClassNotFoundException, IOException
  3585. {
  3586. s.defaultReadObject();
  3587. appContext = AppContext.getAppContext();
  3588. SunToolkit.insertTargetMapping(this, appContext);
  3589. Object keyOrNull;
  3590. while(null != (keyOrNull = s.readObject())) {
  3591. String key = ((String)keyOrNull).intern();
  3592. if (componentListenerK == key)
  3593. addComponentListener((ComponentListener)(s.readObject()));
  3594. else if (focusListenerK == key)
  3595. addFocusListener((FocusListener)(s.readObject()));
  3596. else if (keyListenerK == key)
  3597. addKeyListener((KeyListener)(s.readObject()));
  3598. else if (mouseListenerK == key)
  3599. addMouseListener((MouseListener)(s.readObject()));
  3600. else if (mouseMotionListenerK == key)
  3601. addMouseMotionListener((MouseMotionListener)(s.readObject()));
  3602. else if (inputMethodListenerK == key)
  3603. addInputMethodListener((InputMethodListener)(s.readObject()));
  3604. else // skip value for unrecognized key
  3605. s.readObject();
  3606. }
  3607. // Read the component's orientation if it's present
  3608. Object orient = null;
  3609. try {
  3610. orient = s.readObject();
  3611. } catch (java.io.OptionalDataException e) {
  3612. // JDK 1.1 instances will not have this optional data.
  3613. // e.eof will be true to indicate that there is no more
  3614. // data available for this object.
  3615. // If e.eof is not true, throw the exception as it
  3616. // might have been caused by reasons unrelated to
  3617. // componentOrientation.
  3618. if (!e.eof) {
  3619. throw (e);
  3620. }
  3621. }
  3622. if (orient != null) {
  3623. componentOrientation = (ComponentOrientation)orient;
  3624. } else {
  3625. componentOrientation = ComponentOrientation.UNKNOWN;
  3626. }
  3627. if (popups != null) {
  3628. int npopups = popups.size();
  3629. for (int i = 0 ; i < npopups ; i++) {
  3630. PopupMenu popup = (PopupMenu)popups.elementAt(i);
  3631. popup.parent = this;
  3632. }
  3633. }
  3634. }
  3635. /**
  3636. * Set the language-sensitive orientation that is to be used to order
  3637. * the elements or text within this component. Language-sensitive
  3638. * LayoutManager and Component subclasses will use this property to
  3639. * determine how to lay out and draw components.
  3640. * <p>
  3641. * At construction time, a component's orientation is set to
  3642. * ComponentOrientation.UNKNOWN, indicating that it has not been specified
  3643. * explicitly. The UNKNOWN orientation behaves the same as
  3644. * ComponentOrientation.LEFT_TO_RIGHT.
  3645. * <p>
  3646. * To set the orientation of a single component, use this method.
  3647. * To apply a ResourceBundle's orientation to an entire component
  3648. * hierarchy, use java.awt.Window.applyResourceBundle.
  3649. *
  3650. * @see java.awt.ComponentOrientation
  3651. * @see java.awt.Window#ApplyResourceBundle(java.util.ResourceBundle)
  3652. *
  3653. * @author Laura Werner, IBM
  3654. */
  3655. public void setComponentOrientation(ComponentOrientation o) {
  3656. ComponentOrientation oldValue = componentOrientation;
  3657. componentOrientation = o;
  3658. // This is a bound property, so report the change to
  3659. // any registered listeners. (Cheap if there are none.)
  3660. firePropertyChange("componentOrientation", oldValue, o);
  3661. // This could change the preferred size of the Component.
  3662. if (valid) {
  3663. invalidate();
  3664. }
  3665. }
  3666. /**
  3667. * Retrieve the language-sensitive orientation that is to be used to order
  3668. * the elements or text within this component. LayoutManager and Component
  3669. * subclasses that wish to respect orientation should call this method to
  3670. * get the component's orientation before performing layout or drawing.
  3671. *
  3672. * @see java.awt.ComponentOrientation
  3673. *
  3674. * @author Laura Werner, IBM
  3675. */
  3676. public ComponentOrientation getComponentOrientation() {
  3677. return componentOrientation;
  3678. }
  3679. /**
  3680. * This odd class is to help out a native component that has been
  3681. * embedded in a lightweight component. Moving lightweight
  3682. * components around and changing their visibility is not seen
  3683. * by the native window system. This is a feature for lightweights,
  3684. * but a problem for native components that depend upon the
  3685. * lightweights. An instance of this class listens to the lightweight
  3686. * parents of an associated native component (the outer class).
  3687. *
  3688. * @author Timothy Prinzing
  3689. */
  3690. private final class NativeInLightFixer implements ComponentListener, ContainerListener {
  3691. NativeInLightFixer() {
  3692. lightParents = new Vector();
  3693. Container p = parent;
  3694. // stash a reference to the components that are being observed so that
  3695. // we can reliably remove ourself as a listener later.
  3696. for (; p.peer instanceof java.awt.peer.LightweightPeer; p = p.parent) {
  3697. // register listeners and stash a reference
  3698. p.addComponentListener(this);
  3699. p.addContainerListener(this);
  3700. lightParents.addElement(p);
  3701. }
  3702. // register with the native host (native parent of associated native)
  3703. // to get notified if the top-level lightweight is removed.
  3704. nativeHost = p;
  3705. p.addContainerListener(this);
  3706. // kick start the fixup. Since the event isn't looked at
  3707. // we can simulate movement notification.
  3708. componentMoved(null);
  3709. }
  3710. // --- ComponentListener -------------------------------------------
  3711. /**
  3712. * Invoked when one of the lightweight parents has been resized.
  3713. * This doesn't change the position of the native child so it
  3714. * is ignored.
  3715. */
  3716. public void componentResized(ComponentEvent e) {
  3717. }
  3718. /**
  3719. * Invoked when one of the lightweight parents has been moved.
  3720. * The native peer must be told of the new position which is
  3721. * relative to the native container that is hosting the
  3722. * lightweight components.
  3723. */
  3724. public void componentMoved(ComponentEvent e) {
  3725. synchronized (getTreeLock()) {
  3726. int nativeX = x;
  3727. int nativeY = y;
  3728. for(Component c = parent; (c != null) &&
  3729. (c.peer instanceof java.awt.peer.LightweightPeer);
  3730. c = c.parent) {
  3731. nativeX += c.x;
  3732. nativeY += c.y;
  3733. }
  3734. if (peer != null) {
  3735. peer.setBounds(nativeX, nativeY, width, height);
  3736. }
  3737. }
  3738. }
  3739. /**
  3740. * Invoked when a lightweight parent component has been
  3741. * shown. The associated native component must also be
  3742. * shown if it hasn't had an overriding hide done on it.
  3743. */
  3744. public void componentShown(ComponentEvent e) {
  3745. if (isShowing()) {
  3746. synchronized (getTreeLock()) {
  3747. if (peer != null) {
  3748. peer.show();
  3749. }
  3750. }
  3751. }
  3752. }
  3753. /**
  3754. * Invoked when component has been hidden.
  3755. */
  3756. public void componentHidden(ComponentEvent e) {
  3757. if (visible) {
  3758. synchronized (getTreeLock()) {
  3759. if (peer != null) {
  3760. peer.hide();
  3761. }
  3762. }
  3763. }
  3764. }
  3765. // --- ContainerListener ------------------------------------
  3766. /**
  3767. * Invoked when a component has been added to a lightweight
  3768. * parent. This doesn't effect the native component.
  3769. */
  3770. public void componentAdded(ContainerEvent e) {
  3771. }
  3772. /**
  3773. * Invoked when a lightweight parent has been removed.
  3774. * This means the services of this listener are no longer
  3775. * required and it should remove all references (ie
  3776. * registered listeners).
  3777. */
  3778. public void componentRemoved(ContainerEvent e) {
  3779. Component c = e.getChild();
  3780. if (c == Component.this) {
  3781. removeReferences();
  3782. } else {
  3783. int n = lightParents.size();
  3784. for (int i = 0; i < n; i++) {
  3785. Container p = (Container) lightParents.elementAt(i);
  3786. if (p == c) {
  3787. removeReferences();
  3788. break;
  3789. }
  3790. }
  3791. }
  3792. }
  3793. /**
  3794. * Remove references to this object so it can be
  3795. * garbage collected.
  3796. */
  3797. void removeReferences() {
  3798. int n = lightParents.size();
  3799. for (int i = 0; i < n; i++) {
  3800. Container c = (Container) lightParents.elementAt(i);
  3801. c.removeComponentListener(this);
  3802. c.removeContainerListener(this);
  3803. }
  3804. nativeHost.removeContainerListener(this);
  3805. }
  3806. Vector lightParents;
  3807. Container nativeHost;
  3808. }
  3809. /**
  3810. * Initialize JNI field and method IDs
  3811. */
  3812. private static native void initIDs();
  3813. }