1. /*
  2. * @(#)JComponent.java 2.245 04/06/28
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.util.Hashtable;
  9. import java.util.Dictionary;
  10. import java.util.Enumeration;
  11. import java.util.Locale;
  12. import java.util.Vector;
  13. import java.util.EventListener;
  14. import java.util.Set;
  15. import java.util.TreeSet;
  16. import java.util.Map;
  17. import java.util.HashMap;
  18. import java.awt.*;
  19. import java.awt.event.*;
  20. import java.awt.image.VolatileImage;
  21. import java.awt.Graphics2D;
  22. import java.awt.peer.LightweightPeer;
  23. import java.awt.dnd.DropTarget;
  24. import java.awt.font.FontRenderContext;
  25. import java.beans.*;
  26. import java.applet.Applet;
  27. import java.io.Serializable;
  28. import java.io.ObjectOutputStream;
  29. import java.io.ObjectInputStream;
  30. import java.io.IOException;
  31. import java.io.ObjectInputValidation;
  32. import java.io.InvalidObjectException;
  33. import javax.swing.border.*;
  34. import javax.swing.event.*;
  35. import javax.swing.plaf.*;
  36. import javax.accessibility.*;
  37. import com.sun.java.swing.SwingUtilities2;
  38. import sun.font.FontDesignMetrics;
  39. /**
  40. * The base class for all Swing components except top-level containers.
  41. * To use a component that inherits from <code>JComponent</code>,
  42. * you must place the component in a containment hierarchy
  43. * whose root is a top-level Swing container.
  44. * Top-level Swing containers --
  45. * such as <code>JFrame</code>, <code>JDialog</code>,
  46. * and <code>JApplet</code> --
  47. * are specialized components
  48. * that provide a place for other Swing components to paint themselves.
  49. * For an explanation of containment hierarchies, see
  50. * <a
  51. href="http://java.sun.com/docs/books/tutorial/uiswing/overview/hierarchy.html">Swing Components and the Containment Hierarchy</a>,
  52. * a section in <em>The Java Tutorial</em>.
  53. *
  54. * <p>
  55. * The <code>JComponent</code> class provides:
  56. * <ul>
  57. * <li>The base class for both standard and custom components
  58. * that use the Swing architecture.
  59. * <li>A "pluggable look and feel" (L&F) that can be specified by the
  60. * programmer or (optionally) selected by the user at runtime.
  61. * The look and feel for each component is provided by a
  62. * <em>UI delegate</em> -- an object that descends from
  63. * {@link javax.swing.plaf.ComponentUI}.
  64. * See <a
  65. * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/plaf.html">How
  66. * to Set the Look and Feel</a>
  67. * in <em>The Java Tutorial</em>
  68. * for more information.
  69. * <li>Comprehensive keystroke handling.
  70. * See the document <a
  71. * href="http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html">Keyboard
  72. * Bindings in Swing</a>,
  73. * an article in <em>The Swing Connection</em>,
  74. * for more information.
  75. * <li>Support for tool tips --
  76. * short descriptions that pop up when the cursor lingers
  77. * over a component.
  78. * See <a
  79. * href="http://java.sun.com/docs/books/tutorial/uiswing/components/tooltip.html">How
  80. * to Use Tool Tips</a>
  81. * in <em>The Java Tutorial</em>
  82. * for more information.
  83. * <li>Support for accessibility.
  84. * <code>JComponent</code> contains all of the methods in the
  85. * <code>Accessible</code> interface,
  86. * but it doesn't actually implement the interface. That is the
  87. * responsibility of the individual classes
  88. * that extend <code>JComponent</code>.
  89. * <li>Support for component-specific properties.
  90. * With the {@link #putClientProperty}
  91. * and {@link #getClientProperty} methods,
  92. * you can associate name-object pairs
  93. * with any object that descends from <code>JComponent</code>.
  94. * <li>An infrastructure for painting
  95. * that includes double buffering and support for borders.
  96. * For more information see <a
  97. * href="http://java.sun.com/docs/books/tutorial/uiswing/overview/draw.html">Painting</a> and
  98. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/border.html">How
  99. * to Use Borders</a>,
  100. * both of which are sections in <em>The Java Tutorial</em>.
  101. * </ul>
  102. * For more information on these subjects, see the
  103. * <a href="package-summary.html#package_description">Swing package description</a>
  104. * and <em>The Java Tutorial</em> section
  105. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/jcomponent.html">The JComponent Class</a>.
  106. * <p>
  107. * <code>JComponent</code> and its subclasses document default values
  108. * for certain properties. For example, <code>JTable</code> documents the
  109. * default row height as 16. Each <code>JComponent</code> subclass
  110. * that has a <code>ComponentUI</code> will create the
  111. * <code>ComponentUI</code> as part of its constructor. In order
  112. * to provide a particular look and feel each
  113. * <code>ComponentUI</code> may set properties back on the
  114. * <code>JComponent</code> that created it. For example, a custom
  115. * look and feel may require <code>JTable</code>s to have a row
  116. * height of 24. The documented defaults are the value of a property
  117. * BEFORE the <code>ComponentUI</code> has been installed. If you
  118. * need a specific value for a particular property you should
  119. * explicitly set it.
  120. * <p>
  121. * In release 1.4, the focus subsystem was rearchitected.
  122. * For more information, see
  123. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
  124. * How to Use the Focus Subsystem</a>,
  125. * a section in <em>The Java Tutorial</em>.
  126. * <p>
  127. * <strong>Warning:</strong>
  128. * Serialized objects of this class will not be compatible with
  129. * future Swing releases. The current serialization support is
  130. * appropriate for short term storage or RMI between applications running
  131. * the same version of Swing. As of 1.4, support for long term storage
  132. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  133. * has been added to the <code>java.beans</code> package.
  134. * Please see {@link java.beans.XMLEncoder}.
  135. *
  136. * @see KeyStroke
  137. * @see Action
  138. * @see #setBorder
  139. * @see #registerKeyboardAction
  140. * @see JOptionPane
  141. * @see #setDebugGraphicsOptions
  142. * @see #setToolTipText
  143. * @see #setAutoscrolls
  144. *
  145. * @version 2.130 07/09/99
  146. * @author Hans Muller
  147. * @author Arnaud Weber
  148. */
  149. public abstract class JComponent extends Container implements Serializable
  150. {
  151. /**
  152. * @see #getUIClassID
  153. * @see #writeObject
  154. */
  155. private static final String uiClassID = "ComponentUI";
  156. /**
  157. * Key used in client properties for the AncestorNotifier.
  158. */
  159. private static final StringBuffer ANCESTOR_NOTIFIER_KEY = new StringBuffer(
  160. "AncestorNotifier");
  161. /**
  162. * Key used in client properties for the TransferHandler.
  163. */
  164. private static final StringBuffer TRANSFER_HANDLER_KEY = new StringBuffer(
  165. "TransferHandler");
  166. /**
  167. * Key used in client properties for the InputVerifier.
  168. */
  169. private static final StringBuffer INPUT_VERIFIER_KEY = new StringBuffer(
  170. "InputVerifier");
  171. /**
  172. * @see #readObject
  173. */
  174. private static final Hashtable readObjectCallbacks = new Hashtable(1);
  175. /**
  176. * Keys to use for forward focus traversal when the JComponent is
  177. * managing focus.
  178. */
  179. private static Set managingFocusForwardTraversalKeys;
  180. /**
  181. * Keys to use for backward focus traversal when the JComponent is
  182. * managing focus.
  183. */
  184. private static Set managingFocusBackwardTraversalKeys;
  185. /**
  186. * Indicates if we should register a <code>DropTarget</code> for a
  187. * non-null <code>TransferHandler</code>. Use
  188. * <code>getSuppressDropTarget</code> to check.
  189. */
  190. private static boolean suppressDropSupport;
  191. /**
  192. * Indiciates if we've checked the system property for suppressing
  193. * drop support.
  194. */
  195. private static boolean checkedSuppressDropSupport;
  196. // Following are the possible return values from getObscuredState.
  197. private static final int NOT_OBSCURED = 0;
  198. private static final int PARTIALLY_OBSCURED = 1;
  199. private static final int COMPLETELY_OBSCURED = 2;
  200. /**
  201. * Set to true when DebugGraphics has been loaded.
  202. */
  203. static boolean DEBUG_GRAPHICS_LOADED;
  204. /**
  205. * Anti-aliased FontMetrics.
  206. */
  207. private static final Map<Font,FontMetrics> aaFontMap;
  208. /* The following fields support set methods for the corresponding
  209. * java.awt.Component properties.
  210. */
  211. private boolean isAlignmentXSet;
  212. private float alignmentX;
  213. private boolean isAlignmentYSet;
  214. private float alignmentY;
  215. /**
  216. * Backing store for JComponent properties and listeners
  217. */
  218. /** The look and feel delegate for this component. */
  219. protected transient ComponentUI ui;
  220. /** A list of event listeners for this component. */
  221. protected EventListenerList listenerList = new EventListenerList();
  222. private transient ArrayTable clientProperties;
  223. private VetoableChangeSupport vetoableChangeSupport;
  224. /**
  225. * Whether or not autoscroll has been enabled.
  226. */
  227. private boolean autoscrolls;
  228. private Border border;
  229. private int flags;
  230. /* Input verifier for this component */
  231. private InputVerifier inputVerifier = null;
  232. private boolean verifyInputWhenFocusTarget = true;
  233. /**
  234. * Set in <code>_paintImmediately</code>.
  235. * Will indicate the child that initiated the painting operation.
  236. * If <code>paintingChild</code> is opaque, no need to paint
  237. * any child components after <code>paintingChild</code>.
  238. * Test used in <code>paintChildren</code>.
  239. */
  240. transient Component paintingChild;
  241. /**
  242. * Constant used for <code>registerKeyboardAction</code> that
  243. * means that the command should be invoked when
  244. * the component has the focus.
  245. */
  246. public static final int WHEN_FOCUSED = 0;
  247. /**
  248. * Constant used for <code>registerKeyboardAction</code> that
  249. * means that the command should be invoked when the receiving
  250. * component is an ancestor of the focused component or is
  251. * itself the focused component.
  252. */
  253. public static final int WHEN_ANCESTOR_OF_FOCUSED_COMPONENT = 1;
  254. /**
  255. * Constant used for <code>registerKeyboardAction</code> that
  256. * means that the command should be invoked when
  257. * the receiving component is in the window that has the focus
  258. * or is itself the focused component.
  259. */
  260. public static final int WHEN_IN_FOCUSED_WINDOW = 2;
  261. /**
  262. * Constant used by some of the APIs to mean that no condition is defined.
  263. */
  264. public static final int UNDEFINED_CONDITION = -1;
  265. /**
  266. * The key used by <code>JComponent</code> to access keyboard bindings.
  267. */
  268. private static final String KEYBOARD_BINDINGS_KEY = "_KeyboardBindings";
  269. /**
  270. * An array of <code>KeyStroke</code>s used for
  271. * <code>WHEN_IN_FOCUSED_WINDOW</code> are stashed
  272. * in the client properties under this string.
  273. */
  274. private static final String WHEN_IN_FOCUSED_WINDOW_BINDINGS = "_WhenInFocusedWindow";
  275. /**
  276. * The comment to display when the cursor is over the component,
  277. * also known as a "value tip", "flyover help", or "flyover label".
  278. */
  279. public static final String TOOL_TIP_TEXT_KEY = "ToolTipText";
  280. private static final String NEXT_FOCUS = "nextFocus";
  281. /**
  282. * <code>JPopupMenu<code> assigned to this component
  283. * and all of its childrens
  284. */
  285. private JPopupMenu popupMenu;
  286. /** Private flags **/
  287. private static final int IS_DOUBLE_BUFFERED = 0;
  288. private static final int ANCESTOR_USING_BUFFER = 1;
  289. private static final int IS_PAINTING_TILE = 2;
  290. private static final int IS_OPAQUE = 3;
  291. private static final int KEY_EVENTS_ENABLED = 4;
  292. private static final int FOCUS_INPUTMAP_CREATED = 5;
  293. private static final int ANCESTOR_INPUTMAP_CREATED = 6;
  294. private static final int WIF_INPUTMAP_CREATED = 7;
  295. private static final int ACTIONMAP_CREATED = 8;
  296. private static final int CREATED_DOUBLE_BUFFER = 9;
  297. // bit 10 is free
  298. private static final int IS_PRINTING = 11;
  299. private static final int IS_PRINTING_ALL = 12;
  300. private static final int IS_REPAINTING = 13;
  301. /** Bits 14-21 are used to handle nested writeObject calls. **/
  302. private static final int WRITE_OBJ_COUNTER_FIRST = 14;
  303. private static final int RESERVED_1 = 15;
  304. private static final int RESERVED_2 = 16;
  305. private static final int RESERVED_3 = 17;
  306. private static final int RESERVED_4 = 18;
  307. private static final int RESERVED_5 = 19;
  308. private static final int RESERVED_6 = 20;
  309. private static final int WRITE_OBJ_COUNTER_LAST = 21;
  310. private static final int REQUEST_FOCUS_DISABLED = 22;
  311. private static final int INHERITS_POPUP_MENU = 23;
  312. private static final int OPAQUE_SET = 24;
  313. private static final int AUTOSCROLLS_SET = 25;
  314. private static final int FOCUS_TRAVERSAL_KEYS_FORWARD_SET = 26;
  315. private static final int FOCUS_TRAVERSAL_KEYS_BACKWARD_SET = 27;
  316. /**
  317. * Whether or not some JComponent is calling into an InputVerifier.
  318. */
  319. private static boolean inInputVerifier;
  320. /**
  321. * Temporary rectangles.
  322. */
  323. private static java.util.List tempRectangles = new java.util.ArrayList(11);
  324. /** Used for <code>WHEN_FOCUSED</code> bindings. */
  325. private InputMap focusInputMap;
  326. /** Used for <code>WHEN_ANCESTOR_OF_FOCUSED_COMPONENT</code> bindings. */
  327. private InputMap ancestorInputMap;
  328. /** Used for <code>WHEN_IN_FOCUSED_KEY</code> bindings. */
  329. private ComponentInputMap windowInputMap;
  330. /** ActionMap. */
  331. private ActionMap actionMap;
  332. /** Key used to store the default locale in an AppContext **/
  333. private static final String defaultLocale = "JComponent.defaultLocale";
  334. /**
  335. * Whether or not this component should turn on anti-aliased text
  336. * rendering.
  337. */
  338. private boolean aaText;
  339. static {
  340. aaFontMap = new HashMap<Font,FontMetrics>();
  341. }
  342. /**
  343. * Returns the Set of <code>KeyStroke</code>s to use if the component
  344. * is managing focus for forward focus traversal.
  345. */
  346. static Set<KeyStroke> getManagingFocusForwardTraversalKeys() {
  347. if (managingFocusForwardTraversalKeys == null) {
  348. managingFocusForwardTraversalKeys = new TreeSet();
  349. managingFocusForwardTraversalKeys.add(
  350. KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_MASK));
  351. }
  352. return managingFocusForwardTraversalKeys;
  353. }
  354. /**
  355. * Returns the Set of <code>KeyStroke</code>s to use if the component
  356. * is managing focus for backward focus traversal.
  357. */
  358. static Set<KeyStroke> getManagingFocusBackwardTraversalKeys() {
  359. if (managingFocusBackwardTraversalKeys == null) {
  360. managingFocusBackwardTraversalKeys = new TreeSet();
  361. managingFocusBackwardTraversalKeys.add(
  362. KeyStroke.getKeyStroke(KeyEvent.VK_TAB,
  363. InputEvent.SHIFT_MASK |
  364. InputEvent.CTRL_MASK));
  365. }
  366. return managingFocusBackwardTraversalKeys;
  367. }
  368. /**
  369. * Returns true if <code>setTransferHandler</code> should install
  370. * a <code>DropTarget</code>.
  371. */
  372. private static boolean getSuppressDropTarget() {
  373. if (!checkedSuppressDropSupport) {
  374. Boolean b = (Boolean)java.security.AccessController.doPrivileged(
  375. new java.security.PrivilegedAction() {
  376. public Object run() {
  377. String value = System.getProperty(
  378. "suppressSwingDropSupport");
  379. if (value != null) {
  380. return Boolean.valueOf(value);
  381. }
  382. return Boolean.FALSE;
  383. }
  384. }
  385. );
  386. suppressDropSupport = b.booleanValue();
  387. checkedSuppressDropSupport = true;
  388. }
  389. return suppressDropSupport;
  390. }
  391. private static Rectangle fetchRectangle() {
  392. synchronized(tempRectangles) {
  393. Rectangle rect;
  394. int size = tempRectangles.size();
  395. if (size > 0) {
  396. rect = (Rectangle)tempRectangles.remove(size - 1);
  397. }
  398. else {
  399. rect = new Rectangle(0, 0, 0, 0);
  400. }
  401. return rect;
  402. }
  403. }
  404. private static void recycleRectangle(Rectangle rect) {
  405. synchronized(tempRectangles) {
  406. tempRectangles.add(rect);
  407. }
  408. }
  409. /**
  410. * Sets whether or not <code>getComponentPopupMenu</code> should delegate
  411. * to the parent if this component does not have a <code>JPopupMenu</code>
  412. * assigned to it.
  413. * <p>
  414. * The default value for this is false, but some <code>JComponent</code>
  415. * subclasses that are implemented as a number of <code>JComponent</code>s
  416. * may set this to true.
  417. * <p>
  418. * This is a bound property.
  419. *
  420. * @param value whether or not the JPopupMenu is inherited
  421. * @see #setComponentPopupMenu
  422. * @beaninfo
  423. * bound: true
  424. * description: Whether or not the JPopupMenu is inherited
  425. * @since 1.5
  426. */
  427. public void setInheritsPopupMenu(boolean value) {
  428. setFlag(INHERITS_POPUP_MENU, value);
  429. }
  430. /**
  431. * Returns true if the JPopupMenu should be inherited from the parent.
  432. *
  433. * @see #setComponentPopupMenu
  434. * @since 1.5
  435. */
  436. public boolean getInheritsPopupMenu() {
  437. return getFlag(INHERITS_POPUP_MENU);
  438. }
  439. /**
  440. * Sets the <code>JPopupMenu</code> for this <code>JComponent</code>.
  441. * The UI is responsible for registering bindings and adding the necessary
  442. * listeners such that the <code>JPopupMenu</code> will be shown at
  443. * the appropriate time. When the <code>JPopupMenu</code> is shown
  444. * depends upon the look and feel: some may show it on a mouse event,
  445. * some may enable a key binding.
  446. * <p>
  447. * If <code>popup</code> is null, and <code>getInheritsPopupMenu</code>
  448. * returns true, then <code>getComponentPopupMenu</code> will be delegated
  449. * to the parent. This provides for a way to make all child components
  450. * inherit the popupmenu of the parent.
  451. * <p>
  452. * This is a bound property.
  453. *
  454. * @param popup - the popup that will be assigned to this component
  455. * may be null
  456. * @see #getComponentPopupMenu
  457. * @beaninfo
  458. * bound: true
  459. * preferred: true
  460. * description: Popup to show
  461. * @since 1.5
  462. */
  463. public void setComponentPopupMenu(JPopupMenu popup) {
  464. if(popup != null && isLightweight()) {
  465. enableEvents(AWTEvent.MOUSE_EVENT_MASK);
  466. }
  467. this.popupMenu = popup;
  468. }
  469. /**
  470. * Returns <code>JPopupMenu</code> that assigned for this component.
  471. * If this component does not have a <code>JPopupMenu</code> assigned
  472. * to it and <code>getInheritsPopupMenu</code> is true, this
  473. * will return <code>getParent().getComponentPopupMenu()</code> (assuming
  474. * the parent is valid.)
  475. *
  476. * @return <code>JPopupMenu</code> assigned for this component
  477. * or <code>null</code> if no popup assigned
  478. * @see #setComponentPopupMenu
  479. * @since 1.5
  480. */
  481. public JPopupMenu getComponentPopupMenu() {
  482. if(!getInheritsPopupMenu()) {
  483. return popupMenu;
  484. }
  485. if(popupMenu == null) {
  486. // Search parents for its popup
  487. Container parent = getParent();
  488. while (parent != null) {
  489. if(parent instanceof JComponent) {
  490. return ((JComponent)parent).getComponentPopupMenu();
  491. }
  492. if(parent instanceof Window ||
  493. parent instanceof Applet) {
  494. // Reached toplevel, break and return null
  495. break;
  496. }
  497. parent = parent.getParent();
  498. }
  499. return null;
  500. }
  501. return popupMenu;
  502. }
  503. /**
  504. * Default <code>JComponent</code> constructor. This constructor does
  505. * very little initialization beyond calling the <code>Container</code>
  506. * constructor. For example, the initial layout manager is
  507. * <code>null</code>. It does, however, set the component's locale
  508. * property to the value returned by
  509. * <code>JComponent.getDefaultLocale</code>.
  510. *
  511. * @see #getDefaultLocale
  512. */
  513. public JComponent() {
  514. super();
  515. // We enable key events on all JComponents so that accessibility
  516. // bindings will work everywhere. This is a partial fix to BugID
  517. // 4282211.
  518. enableEvents(AWTEvent.KEY_EVENT_MASK);
  519. if (isManagingFocus()) {
  520. LookAndFeel.installProperty(this,
  521. "focusTraversalKeysForward",
  522. getManagingFocusForwardTraversalKeys());
  523. LookAndFeel.installProperty(this,
  524. "focusTraversalKeysBackward",
  525. getManagingFocusBackwardTraversalKeys());
  526. }
  527. super.setLocale( JComponent.getDefaultLocale() );
  528. }
  529. /**
  530. * Resets the UI property to a value from the current look and feel.
  531. * <code>JComponent</code> subclasses must override this method
  532. * like this:
  533. * <pre>
  534. * public void updateUI() {
  535. * setUI((SliderUI)UIManager.getUI(this);
  536. * }
  537. * </pre>
  538. *
  539. * @see #setUI
  540. * @see UIManager#getLookAndFeel
  541. * @see UIManager#getUI
  542. */
  543. public void updateUI() {}
  544. /**
  545. * Sets the look and feel delegate for this component.
  546. * <code>JComponent</code> subclasses generally override this method
  547. * to narrow the argument type. For example, in <code>JSlider</code>:
  548. * <pre>
  549. * public void setUI(SliderUI newUI) {
  550. * super.setUI(newUI);
  551. * }
  552. * </pre>
  553. * <p>
  554. * Additionally <code>JComponent</code> subclasses must provide a
  555. * <code>getUI</code> method that returns the correct type. For example:
  556. * <pre>
  557. * public SliderUI getUI() {
  558. * return (SliderUI)ui;
  559. * }
  560. * </pre>
  561. *
  562. * @param newUI the new UI delegate
  563. * @see #updateUI
  564. * @see UIManager#getLookAndFeel
  565. * @see UIManager#getUI
  566. * @beaninfo
  567. * bound: true
  568. * hidden: true
  569. * attribute: visualUpdate true
  570. * description: The component's look and feel delegate.
  571. */
  572. protected void setUI(ComponentUI newUI) {
  573. /* We do not check that the UI instance is different
  574. * before allowing the switch in order to enable the
  575. * same UI instance *with different default settings*
  576. * to be installed.
  577. */
  578. if (ui != null) {
  579. ui.uninstallUI(this);
  580. }
  581. // aaText shouldn't persist between look and feels, reset it.
  582. aaText = false;
  583. ComponentUI oldUI = ui;
  584. ui = newUI;
  585. if (ui != null) {
  586. ui.installUI(this);
  587. }
  588. firePropertyChange("UI", oldUI, newUI);
  589. revalidate();
  590. repaint();
  591. }
  592. /**
  593. * Returns the <code>UIDefaults</code> key used to
  594. * look up the name of the <code>swing.plaf.ComponentUI</code>
  595. * class that defines the look and feel
  596. * for this component. Most applications will never need to
  597. * call this method. Subclasses of <code>JComponent</code> that support
  598. * pluggable look and feel should override this method to
  599. * return a <code>UIDefaults</code> key that maps to the
  600. * <code>ComponentUI</code> subclass that defines their look and feel.
  601. *
  602. * @return the <code>UIDefaults</code> key for a
  603. * <code>ComponentUI</code> subclass
  604. * @see UIDefaults#getUI
  605. * @beaninfo
  606. * expert: true
  607. * description: UIClassID
  608. */
  609. public String getUIClassID() {
  610. return uiClassID;
  611. }
  612. /**
  613. * Returns the graphics object used to paint this component.
  614. * If <code>DebugGraphics</code> is turned on we create a new
  615. * <code>DebugGraphics</code> object if necessary.
  616. * Otherwise we just configure the
  617. * specified graphics object's foreground and font.
  618. *
  619. * @param g the original <code>Graphics</code> object
  620. * @return a <code>Graphics</code> object configured for this component
  621. */
  622. protected Graphics getComponentGraphics(Graphics g) {
  623. Graphics componentGraphics = g;
  624. if (ui != null && DEBUG_GRAPHICS_LOADED) {
  625. if ((DebugGraphics.debugComponentCount() != 0) &&
  626. (shouldDebugGraphics() != 0) &&
  627. !(g instanceof DebugGraphics)) {
  628. componentGraphics = new DebugGraphics(g,this);
  629. }
  630. }
  631. componentGraphics.setColor(getForeground());
  632. componentGraphics.setFont(getFont());
  633. return componentGraphics;
  634. }
  635. /**
  636. * Calls the UI delegate's paint method, if the UI delegate
  637. * is non-<code>null</code>. We pass the delegate a copy of the
  638. * <code>Graphics</code> object to protect the rest of the
  639. * paint code from irrevocable changes
  640. * (for example, <code>Graphics.translate</code>).
  641. * <p>
  642. * If you override this in a subclass you should not make permanent
  643. * changes to the passed in <code>Graphics</code>. For example, you
  644. * should not alter the clip <code>Rectangle</code> or modify the
  645. * transform. If you need to do these operations you may find it
  646. * easier to create a new <code>Graphics</code> from the passed in
  647. * <code>Graphics</code> and manipulate it. Further, if you do not
  648. * invoker super's implementation you must honor the opaque property,
  649. * that is
  650. * if this component is opaque, you must completely fill in the background
  651. * in a non-opaque color. If you do not honor the opaque property you
  652. * will likely see visual artifacts.
  653. * <p>
  654. * The passed in <code>Graphics</code> object might
  655. * have a transform other than the identify transform
  656. * installed on it. In this case, you might get
  657. * unexpected results if you cumulatively apply
  658. * another transform.
  659. *
  660. * @param g the <code>Graphics</code> object to protect
  661. * @see #paint
  662. * @see ComponentUI
  663. */
  664. protected void paintComponent(Graphics g) {
  665. if (ui != null) {
  666. Graphics scratchGraphics = (g == null) ? null : g.create();
  667. try {
  668. ui.update(scratchGraphics, this);
  669. }
  670. finally {
  671. scratchGraphics.dispose();
  672. }
  673. }
  674. }
  675. /**
  676. * Paints this component's children.
  677. * If <code>shouldUseBuffer</code> is true,
  678. * no component ancestor has a buffer and
  679. * the component children can use a buffer if they have one.
  680. * Otherwise, one ancestor has a buffer currently in use and children
  681. * should not use a buffer to paint.
  682. * @param g the <code>Graphics</code> context in which to paint
  683. * @see #paint
  684. * @see java.awt.Container#paint
  685. */
  686. protected void paintChildren(Graphics g) {
  687. boolean isJComponent;
  688. Graphics sg = null;
  689. try {
  690. synchronized(getTreeLock()) {
  691. int i = getComponentCount() - 1;
  692. if (i < 0) {
  693. return;
  694. }
  695. sg = (g == null) ? null : g.create();
  696. // If we are only to paint to a specific child, determine
  697. // its index.
  698. if (paintingChild != null &&
  699. (paintingChild instanceof JComponent) &&
  700. ((JComponent)paintingChild).isOpaque()) {
  701. for (; i >= 0; i--) {
  702. if (getComponent(i) == paintingChild){
  703. break;
  704. }
  705. }
  706. }
  707. Rectangle tmpRect = fetchRectangle();
  708. boolean checkSiblings = (!isOptimizedDrawingEnabled() &&
  709. checkIfChildObscuredBySibling());
  710. Rectangle clipBounds = null;
  711. if (checkSiblings) {
  712. clipBounds = sg.getClipBounds();
  713. if (clipBounds == null) {
  714. clipBounds = new Rectangle(0, 0, getWidth(),
  715. getHeight());
  716. }
  717. }
  718. boolean printing = getFlag(IS_PRINTING);
  719. for (; i >= 0 ; i--) {
  720. Component comp = getComponent(i);
  721. if (comp != null &&
  722. (printing || isLightweightComponent(comp)) &&
  723. (comp.isVisible() == true)) {
  724. Rectangle cr;
  725. isJComponent = (comp instanceof JComponent);
  726. cr = comp.getBounds(tmpRect);
  727. boolean hitClip = g.hitClip(cr.x, cr.y, cr.width, cr.height);
  728. if (hitClip) {
  729. if (checkSiblings && i > 0) {
  730. int x = cr.x;
  731. int y = cr.y;
  732. int width = cr.width;
  733. int height = cr.height;
  734. SwingUtilities.computeIntersection
  735. (clipBounds.x, clipBounds.y,
  736. clipBounds.width, clipBounds.height, cr);
  737. if(getObscuredState(i, cr.x, cr.y, cr.width,
  738. cr.height) == COMPLETELY_OBSCURED) {
  739. continue;
  740. }
  741. cr.x = x;
  742. cr.y = y;
  743. cr.width = width;
  744. cr.height = height;
  745. }
  746. Graphics cg = sg.create(cr.x, cr.y, cr.width,
  747. cr.height);
  748. cg.setColor(comp.getForeground());
  749. cg.setFont(comp.getFont());
  750. boolean shouldSetFlagBack = false;
  751. try {
  752. if(isJComponent) {
  753. if(getFlag(ANCESTOR_USING_BUFFER)) {
  754. ((JComponent)comp).setFlag(ANCESTOR_USING_BUFFER,true);
  755. shouldSetFlagBack = true;
  756. }
  757. if(getFlag(IS_PAINTING_TILE)) {
  758. ((JComponent)comp).setFlag(IS_PAINTING_TILE,true);
  759. shouldSetFlagBack = true;
  760. }
  761. if(!printing) {
  762. ((JComponent)comp).paint(cg);
  763. }
  764. else {
  765. if (!getFlag(IS_PRINTING_ALL)) {
  766. comp.print(cg);
  767. }
  768. else {
  769. comp.printAll(cg);
  770. }
  771. }
  772. } else {
  773. if (!printing) {
  774. comp.paint(cg);
  775. }
  776. else {
  777. if (!getFlag(IS_PRINTING_ALL)) {
  778. comp.print(cg);
  779. }
  780. else {
  781. comp.printAll(cg);
  782. }
  783. }
  784. }
  785. } finally {
  786. cg.dispose();
  787. if(shouldSetFlagBack) {
  788. ((JComponent)comp).setFlag(ANCESTOR_USING_BUFFER,false);
  789. ((JComponent)comp).setFlag(IS_PAINTING_TILE,false);
  790. }
  791. }
  792. }
  793. }
  794. }
  795. recycleRectangle(tmpRect);
  796. }
  797. } finally {
  798. if (sg != null) {
  799. sg.dispose();
  800. }
  801. }
  802. }
  803. /**
  804. * Paints the component's border.
  805. * <p>
  806. * If you override this in a subclass you should not make permanent
  807. * changes to the passed in <code>Graphics</code>. For example, you
  808. * should not alter the clip <code>Rectangle</code> or modify the
  809. * transform. If you need to do these operations you may find it
  810. * easier to create a new <code>Graphics</code> from the passed in
  811. * <code>Graphics</code> and manipulate it.
  812. *
  813. * @param g the <code>Graphics</code> context in which to paint
  814. *
  815. * @see #paint
  816. * @see #setBorder
  817. */
  818. protected void paintBorder(Graphics g) {
  819. Border border = getBorder();
  820. if (border != null) {
  821. border.paintBorder(this, g, 0, 0, getWidth(), getHeight());
  822. }
  823. }
  824. /**
  825. * Calls <code>paint</code>. Doesn't clear the background but see
  826. * <code>ComponentUI.update</code>, which is called by
  827. * <code>paintComponent</code>.
  828. *
  829. * @param g the <code>Graphics</code> context in which to paint
  830. * @see #paint
  831. * @see #paintComponent
  832. * @see javax.swing.plaf.ComponentUI
  833. */
  834. public void update(Graphics g) {
  835. paint(g);
  836. }
  837. /**
  838. * Invoked by Swing to draw components.
  839. * Applications should not invoke <code>paint</code> directly,
  840. * but should instead use the <code>repaint</code> method to
  841. * schedule the component for redrawing.
  842. * <p>
  843. * This method actually delegates the work of painting to three
  844. * protected methods: <code>paintComponent</code>,
  845. * <code>paintBorder</code>,
  846. * and <code>paintChildren</code>. They're called in the order
  847. * listed to ensure that children appear on top of component itself.
  848. * Generally speaking, the component and its children should not
  849. * paint in the insets area allocated to the border. Subclasses can
  850. * just override this method, as always. A subclass that just
  851. * wants to specialize the UI (look and feel) delegate's
  852. * <code>paint</code> method should just override
  853. * <code>paintComponent</code>.
  854. *
  855. * @param g the <code>Graphics</code> context in which to paint
  856. * @see #paintComponent
  857. * @see #paintBorder
  858. * @see #paintChildren
  859. * @see #getComponentGraphics
  860. * @see #repaint
  861. */
  862. public void paint(Graphics g) {
  863. boolean shouldClearPaintFlags = false;
  864. boolean paintCompleted = false;
  865. if ((getWidth() <= 0) || (getHeight() <= 0)) {
  866. return;
  867. }
  868. Graphics componentGraphics = getComponentGraphics(g);
  869. Graphics co = (componentGraphics == null) ? null :
  870. componentGraphics.create();
  871. try {
  872. RepaintManager repaintManager = RepaintManager.currentManager(this);
  873. Rectangle clipRect = co.getClipBounds();
  874. int clipX;
  875. int clipY;
  876. int clipW;
  877. int clipH;
  878. if (clipRect == null) {
  879. clipX = clipY = 0;
  880. clipW = getWidth();
  881. clipH = getHeight();
  882. }
  883. else {
  884. clipX = clipRect.x;
  885. clipY = clipRect.y;
  886. clipW = clipRect.width;
  887. clipH = clipRect.height;
  888. }
  889. if(clipW > getWidth()) {
  890. clipW = getWidth();
  891. }
  892. if(clipH > getHeight()) {
  893. clipH = getHeight();
  894. }
  895. if(getParent() != null && !(getParent() instanceof JComponent)) {
  896. adjustPaintFlags();
  897. shouldClearPaintFlags = true;
  898. }
  899. int bw,bh;
  900. boolean printing = getFlag(IS_PRINTING);
  901. if(!printing && repaintManager.isDoubleBufferingEnabled() &&
  902. !getFlag(ANCESTOR_USING_BUFFER) && isDoubleBuffered()) {
  903. paintCompleted = paintDoubleBuffered(this, this, co, clipX, clipY, clipW, clipH);
  904. }
  905. if (!paintCompleted) {
  906. // Will ocassionaly happen in 1.2, especially when printing.
  907. if (clipRect == null) {
  908. co.setClip(clipX, clipY, clipW, clipH);
  909. }
  910. if (!rectangleIsObscured(clipX,clipY,clipW,clipH)) {
  911. if (!printing) {
  912. paintComponent(co);
  913. paintBorder(co);
  914. }
  915. else {
  916. printComponent(co);
  917. printBorder(co);
  918. }
  919. }
  920. if (!printing) {
  921. paintChildren(co);
  922. }
  923. else {
  924. printChildren(co);
  925. }
  926. }
  927. } finally {
  928. co.dispose();
  929. if(shouldClearPaintFlags) {
  930. setFlag(ANCESTOR_USING_BUFFER,false);
  931. setFlag(IS_PAINTING_TILE,false);
  932. setFlag(IS_PRINTING,false);
  933. setFlag(IS_PRINTING_ALL,false);
  934. }
  935. }
  936. }
  937. /**
  938. * Returns true if this component, or any of it's ancestors, are in
  939. * the processing of painting.
  940. */
  941. boolean isPainting() {
  942. Container component = this;
  943. while (component != null) {
  944. if (component instanceof JComponent &&
  945. ((JComponent)component).getFlag(ANCESTOR_USING_BUFFER)) {
  946. return true;
  947. }
  948. component = component.getParent();
  949. }
  950. return false;
  951. }
  952. private void adjustPaintFlags() {
  953. JComponent jparent = null;
  954. Container parent;
  955. for(parent = getParent() ; parent != null ; parent =
  956. parent.getParent()) {
  957. if(parent instanceof JComponent) {
  958. jparent = (JComponent) parent;
  959. if(jparent.getFlag(ANCESTOR_USING_BUFFER))
  960. setFlag(ANCESTOR_USING_BUFFER, true);
  961. if(jparent.getFlag(IS_PAINTING_TILE))
  962. setFlag(IS_PAINTING_TILE, true);
  963. if(jparent.getFlag(IS_PRINTING))
  964. setFlag(IS_PRINTING, true);
  965. if(jparent.getFlag(IS_PRINTING_ALL))
  966. setFlag(IS_PRINTING_ALL, true);
  967. break;
  968. }
  969. }
  970. }
  971. /**
  972. * Invoke this method to print the component. This method invokes
  973. * <code>print</code> on the component.
  974. *
  975. * @param g the <code>Graphics</code> context in which to paint
  976. * @see #print
  977. * @see #printComponent
  978. * @see #printBorder
  979. * @see #printChildren
  980. */
  981. public void printAll(Graphics g) {
  982. setFlag(IS_PRINTING_ALL, true);
  983. try {
  984. print(g);
  985. }
  986. finally {
  987. setFlag(IS_PRINTING_ALL, false);
  988. }
  989. }
  990. /**
  991. * Invoke this method to print the component. This method will
  992. * result in invocations to <code>printComponent</code>,
  993. * <code>printBorder</code> and <code>printChildren</code>. It is
  994. * not recommended that you override this method, instead override
  995. * one of the previously mentioned methods. This method sets the
  996. * component's state such that the double buffer will not be used, eg
  997. * painting will be done directly on the passed in <code>Graphics</code>.
  998. *
  999. * @param g the <code>Graphics</code> context in which to paint
  1000. * @see #printComponent
  1001. * @see #printBorder
  1002. * @see #printChildren
  1003. */
  1004. public void print(Graphics g) {
  1005. setFlag(IS_PRINTING, true);
  1006. try {
  1007. paint(g);
  1008. }
  1009. finally {
  1010. setFlag(IS_PRINTING, false);
  1011. }
  1012. }
  1013. /**
  1014. * This is invoked during a printing operation. This is implemented to
  1015. * invoke <code>paintComponent</code> on the component. Override this
  1016. * if you wish to add special painting behavior when printing.
  1017. *
  1018. * @param g the <code>Graphics</code> context in which to paint
  1019. * @see #print
  1020. * @since 1.3
  1021. */
  1022. protected void printComponent(Graphics g) {
  1023. paintComponent(g);
  1024. }
  1025. /**
  1026. * Prints this component's children. This is implemented to invoke
  1027. * <code>paintChildren</code> on the component. Override this if you
  1028. * wish to print the children differently than painting.
  1029. *
  1030. * @param g the <code>Graphics</code> context in which to paint
  1031. * @see #print
  1032. * @since 1.3
  1033. */
  1034. protected void printChildren(Graphics g) {
  1035. paintChildren(g);
  1036. }
  1037. /**
  1038. * Prints the component's border. This is implemented to invoke
  1039. * <code>paintBorder</code> on the component. Override this if you
  1040. * wish to print the border differently that it is painted.
  1041. *
  1042. * @param g the <code>Graphics</code> context in which to paint
  1043. * @see #print
  1044. * @since 1.3
  1045. */
  1046. protected void printBorder(Graphics g) {
  1047. paintBorder(g);
  1048. }
  1049. /**
  1050. * Returns true if the component is currently painting a tile.
  1051. * If this method returns true, paint will be called again for another
  1052. * tile. This method returns false if you are not painting a tile or
  1053. * if the last tile is painted.
  1054. * Use this method to keep some state you might need between tiles.
  1055. *
  1056. * @return true if the component is currently painting a tile,
  1057. * false otherwise
  1058. */
  1059. public boolean isPaintingTile() {
  1060. return getFlag(IS_PAINTING_TILE);
  1061. }
  1062. /**
  1063. * In release 1.4, the focus subsystem was rearchitected.
  1064. * For more information, see
  1065. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
  1066. * How to Use the Focus Subsystem</a>,
  1067. * a section in <em>The Java Tutorial</em>.
  1068. * <p>
  1069. * Changes this <code>JComponent</code>'s focus traversal keys to
  1070. * CTRL+TAB and CTRL+SHIFT+TAB. Also prevents
  1071. * <code>SortingFocusTraversalPolicy</code> from considering descendants
  1072. * of this JComponent when computing a focus traversal cycle.
  1073. *
  1074. * @see java.awt.Component#setFocusTraversalKeys
  1075. * @see SortingFocusTraversalPolicy
  1076. * @deprecated As of 1.4, replaced by
  1077. * <code>Component.setFocusTraversalKeys(int, Set)</code> and
  1078. * <code>Container.setFocusCycleRoot(boolean)</code>.
  1079. */
  1080. @Deprecated
  1081. public boolean isManagingFocus() {
  1082. return false;
  1083. }
  1084. private void registerNextFocusableComponent() {
  1085. registerNextFocusableComponent(getNextFocusableComponent());
  1086. }
  1087. private void registerNextFocusableComponent(Component
  1088. nextFocusableComponent) {
  1089. if (nextFocusableComponent == null) {
  1090. return;
  1091. }
  1092. Container nearestRoot =
  1093. (isFocusCycleRoot()) ? this : getFocusCycleRootAncestor();
  1094. FocusTraversalPolicy policy = nearestRoot.getFocusTraversalPolicy();
  1095. if (!(policy instanceof LegacyGlueFocusTraversalPolicy)) {
  1096. policy = new LegacyGlueFocusTraversalPolicy(policy);
  1097. nearestRoot.setFocusTraversalPolicy(policy);
  1098. }
  1099. ((LegacyGlueFocusTraversalPolicy)policy).
  1100. setNextFocusableComponent(this, nextFocusableComponent);
  1101. }
  1102. private void deregisterNextFocusableComponent() {
  1103. Component nextFocusableComponent = getNextFocusableComponent();
  1104. if (nextFocusableComponent == null) {
  1105. return;
  1106. }
  1107. Container nearestRoot =
  1108. (isFocusCycleRoot()) ? this : getFocusCycleRootAncestor();
  1109. if (nearestRoot == null) {
  1110. return;
  1111. }
  1112. FocusTraversalPolicy policy = nearestRoot.getFocusTraversalPolicy();
  1113. if (policy instanceof LegacyGlueFocusTraversalPolicy) {
  1114. ((LegacyGlueFocusTraversalPolicy)policy).
  1115. unsetNextFocusableComponent(this, nextFocusableComponent);
  1116. }
  1117. }
  1118. /**
  1119. * In release 1.4, the focus subsystem was rearchitected.
  1120. * For more information, see
  1121. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
  1122. * How to Use the Focus Subsystem</a>,
  1123. * a section in <em>The Java Tutorial</em>.
  1124. * <p>
  1125. * Overrides the default <code>FocusTraversalPolicy</code> for this
  1126. * <code>JComponent</code>'s focus traversal cycle by unconditionally
  1127. * setting the specified <code>Component</code> as the next
  1128. * <code>Component</code> in the cycle, and this <code>JComponent</code>
  1129. * as the specified <code>Component</code>'s previous
  1130. * <code>Component</code> in the cycle.
  1131. *
  1132. * @param aComponent the <code>Component</code> that should follow this
  1133. * <code>JComponent</code> in the focus traversal cycle
  1134. *
  1135. * @see #getNextFocusableComponent
  1136. * @see java.awt.FocusTraversalPolicy
  1137. * @deprecated As of 1.4, replaced by <code>FocusTraversalPolicy</code>
  1138. */
  1139. @Deprecated
  1140. public void setNextFocusableComponent(Component aComponent) {
  1141. boolean displayable = isDisplayable();
  1142. if (displayable) {
  1143. deregisterNextFocusableComponent();
  1144. }
  1145. putClientProperty(NEXT_FOCUS, aComponent);
  1146. if (displayable) {
  1147. registerNextFocusableComponent(aComponent);
  1148. }
  1149. }
  1150. /**
  1151. * In release 1.4, the focus subsystem was rearchitected.
  1152. * For more information, see
  1153. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
  1154. * How to Use the Focus Subsystem</a>,
  1155. * a section in <em>The Java Tutorial</em>.
  1156. * <p>
  1157. * Returns the <code>Component</code> set by a prior call to
  1158. * <code>setNextFocusableComponent(Component)</code> on this
  1159. * <code>JComponent</code>.
  1160. *
  1161. * @return the <code>Component</code> that will follow this
  1162. * <code>JComponent</code> in the focus traversal cycle, or
  1163. * <code>null</code> if none has been explicitly specified
  1164. *
  1165. * @see #setNextFocusableComponent
  1166. * @deprecated As of 1.4, replaced by <code>FocusTraversalPolicy</code>.
  1167. */
  1168. @Deprecated
  1169. public Component getNextFocusableComponent() {
  1170. return (Component)getClientProperty(NEXT_FOCUS);
  1171. }
  1172. /**
  1173. * Provides a hint as to whether or not this <code>JComponent</code>
  1174. * should get focus. This is only a hint, and it is up to consumers that
  1175. * are requesting focus to honor this property. This is typically honored
  1176. * for mouse operations, but not keyboard operations. For example, look
  1177. * and feels could verify this property is true before requesting focus
  1178. * during a mouse operation. This would often times be used if you did
  1179. * not want a mouse press on a <code>JComponent</code> to steal focus,
  1180. * but did want the <code>JComponent</code> to be traversable via the
  1181. * keyboard. If you do not want this <code>JComponent</code> focusable at
  1182. * all, use the <code>setFocusable</code> method instead.
  1183. * <p>
  1184. * Please see
  1185. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
  1186. * How to Use the Focus Subsystem</a>,
  1187. * a section in <em>The Java Tutorial</em>,
  1188. * for more information.
  1189. *
  1190. * @param requestFocusEnabled indicates whether you want this
  1191. * <code>JComponent</code> to be focusable or not
  1192. * @see <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
  1193. * @see java.awt.Component#setFocusable
  1194. */
  1195. public void setRequestFocusEnabled(boolean requestFocusEnabled) {
  1196. setFlag(REQUEST_FOCUS_DISABLED, !requestFocusEnabled);
  1197. }
  1198. /**
  1199. * Returns <code>true</code> if this <code>JComponent</code> should
  1200. * get focus; otherwise returns <code>false</code>.
  1201. * <p>
  1202. * Please see
  1203. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
  1204. * How to Use the Focus Subsystem</a>,
  1205. * a section in <em>The Java Tutorial</em>,
  1206. * for more information.
  1207. *
  1208. * @return <code>true</code> if this component should get focus,
  1209. * otherwise returns <code>false</code>
  1210. * @see #setRequestFocusEnabled
  1211. * @see <a href="../../java/awt/doc-files/FocusSpec.html">Focus
  1212. * Specification</a>
  1213. * @see java.awt.Component#isFocusable
  1214. */
  1215. public boolean isRequestFocusEnabled() {
  1216. return !getFlag(REQUEST_FOCUS_DISABLED);
  1217. }
  1218. private boolean runInputVerifier() {
  1219. if (inInputVerifier) {
  1220. // We're already running the InputVerifier, assume the
  1221. // developer knows what they're doing.
  1222. return true;
  1223. }
  1224. Component focusOwner =
  1225. KeyboardFocusManager.getCurrentKeyboardFocusManager().
  1226. getFocusOwner();
  1227. if (focusOwner == this) {
  1228. return true;
  1229. }
  1230. if (!getVerifyInputWhenFocusTarget()) {
  1231. return true;
  1232. }
  1233. if (focusOwner == null || !(focusOwner instanceof JComponent)) {
  1234. return true;
  1235. }
  1236. JComponent jFocusOwner = (JComponent)focusOwner;
  1237. InputVerifier iv = jFocusOwner.getInputVerifier();
  1238. if (iv == null) {
  1239. return true;
  1240. } else {
  1241. inInputVerifier = true;
  1242. try {
  1243. return iv.shouldYieldFocus(jFocusOwner);
  1244. } finally {
  1245. inInputVerifier = false;
  1246. }
  1247. }
  1248. }
  1249. /**
  1250. * Requests that this <code>Component</code> gets the input focus.
  1251. * Refer to {@link java.awt.Component#requestFocus()
  1252. * Component.requestFocus()} for a complete description of
  1253. * this method.
  1254. * <p>
  1255. * Note that the use of this method is discouraged because
  1256. * its behavior is platform dependent. Instead we recommend the
  1257. * use of {@link #requestFocusInWindow() requestFocusInWindow()}.
  1258. * If you would like more information on focus, see
  1259. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
  1260. *
  1261. * @see java.awt.Component#requestFocusInWindow()
  1262. * @see java.awt.Component#requestFocusInWindow(boolean)
  1263. * @since 1.4
  1264. */
  1265. public void requestFocus() {
  1266. if (runInputVerifier()) {
  1267. super.requestFocus();
  1268. }
  1269. }
  1270. /**
  1271. * Requests that this <code>Component</code> gets the input focus.
  1272. * Refer to {@link java.awt.Component#requestFocus(boolean)
  1273. * Component.requestFocus(boolean)} for a complete description of
  1274. * this method.
  1275. * <p>
  1276. * Note that the use of this method is discouraged because
  1277. * its behavior is platform dependent. Instead we recommend the
  1278. * use of {@link #requestFocusInWindow(boolean)
  1279. * requestFocusInWindow(boolean)}.
  1280. * If you would like more information on focus, see
  1281. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
  1282. * How to Use the Focus Subsystem</a>,
  1283. * a section in <em>The Java Tutorial</em>.
  1284. *
  1285. * @param temporary boolean indicating if the focus change is temporary
  1286. * @return <code>false</code> if the focus change request is guaranteed to
  1287. * fail; <code>true</code> if it is likely to succeed
  1288. * @see java.awt.Component#requestFocusInWindow()
  1289. * @see java.awt.Component#requestFocusInWindow(boolean)
  1290. * @since 1.4
  1291. */
  1292. public boolean requestFocus(boolean temporary) {
  1293. return (runInputVerifier())
  1294. ? super.requestFocus(temporary)
  1295. : false;
  1296. }
  1297. /**
  1298. * Requests that this <code>Component</code> gets the input focus.
  1299. * Refer to {@link java.awt.Component#requestFocusInWindow()
  1300. * Component.requestFocusInWindow()} for a complete description of
  1301. * this method.
  1302. * <p>
  1303. * If you would like more information on focus, see
  1304. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
  1305. * How to Use the Focus Subsystem</a>,
  1306. * a section in <em>The Java Tutorial</em>.
  1307. *
  1308. * @return <code>false</code> if the focus change request is guaranteed to
  1309. * fail; <code>true</code> if it is likely to succeed
  1310. * @see java.awt.Component#requestFocusInWindow()
  1311. * @see java.awt.Component#requestFocusInWindow(boolean)
  1312. * @since 1.4
  1313. */
  1314. public boolean requestFocusInWindow() {
  1315. return (runInputVerifier())
  1316. ? super.requestFocusInWindow()
  1317. : false;
  1318. }
  1319. /**
  1320. * Requests that this <code>Component</code> gets the input focus.
  1321. * Refer to {@link java.awt.Component#requestFocusInWindow(boolean)
  1322. * Component.requestFocusInWindow(boolean)} for a complete description of
  1323. * this method.
  1324. * <p>
  1325. * If you would like more information on focus, see
  1326. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
  1327. *
  1328. * @param temporary boolean indicating if the focus change is temporary
  1329. * @return <code>false</code> if the focus change request is guaranteed to
  1330. * fail; <code>true</code> if it is likely to succeed
  1331. * @see java.awt.Component#requestFocusInWindow()
  1332. * @see java.awt.Component#requestFocusInWindow(boolean)
  1333. * @since 1.4
  1334. */
  1335. protected boolean requestFocusInWindow(boolean temporary) {
  1336. return (runInputVerifier())
  1337. ? super.requestFocusInWindow(temporary)
  1338. : false;
  1339. }
  1340. /**
  1341. * Requests that this Component get the input focus, and that this
  1342. * Component's top-level ancestor become the focused Window. This component
  1343. * must be displayable, visible, and focusable for the request to be
  1344. * granted.
  1345. * <p>
  1346. * This method is intended for use by focus implementations. Client code
  1347. * should not use this method; instead, it should use
  1348. * <code>requestFocusInWindow()</code>.
  1349. *
  1350. * @see #requestFocusInWindow()
  1351. */
  1352. public void grabFocus() {
  1353. requestFocus();
  1354. }
  1355. /**
  1356. * Sets the value to indicate whether input verifier for the
  1357. * current focus owner will be called before this component requests
  1358. * focus. The default is true. Set to false on components such as a
  1359. * Cancel button or a scrollbar, which should activate even if the
  1360. * input in the current focus owner is not "passed" by the input
  1361. * verifier for that component.
  1362. *
  1363. * @param verifyInputWhenFocusTarget value for the
  1364. * <code>verifyInputWhenFocusTarget</code> property
  1365. * @see InputVerifier
  1366. * @see #setInputVerifier
  1367. * @see #getInputVerifier
  1368. * @see #getVerifyInputWhenFocusTarget
  1369. *
  1370. * @since 1.3
  1371. * @beaninfo
  1372. * bound: true
  1373. * description: Whether the Component verifies input before accepting
  1374. * focus.
  1375. */
  1376. public void setVerifyInputWhenFocusTarget(boolean
  1377. verifyInputWhenFocusTarget) {
  1378. boolean oldVerifyInputWhenFocusTarget =
  1379. this.verifyInputWhenFocusTarget;
  1380. this.verifyInputWhenFocusTarget = verifyInputWhenFocusTarget;
  1381. firePropertyChange("verifyInputWhenFocusTarget",
  1382. oldVerifyInputWhenFocusTarget,
  1383. verifyInputWhenFocusTarget);
  1384. }
  1385. /**
  1386. * Returns the value that indicates whether the input verifier for the
  1387. * current focus owner will be called before this component requests
  1388. * focus.
  1389. *
  1390. * @return value of the <code>verifyInputWhenFocusTarget</code> property
  1391. *
  1392. * @see InputVerifier
  1393. * @see #setInputVerifier
  1394. * @see #getInputVerifier
  1395. * @see #setVerifyInputWhenFocusTarget
  1396. *
  1397. * @since 1.3
  1398. */
  1399. public boolean getVerifyInputWhenFocusTarget() {
  1400. return verifyInputWhenFocusTarget;
  1401. }
  1402. /**
  1403. * Gets the <code>FontMetrics</code> for the specified <code>Font</code>.
  1404. *
  1405. * @param font the font for which font metrics is to be
  1406. * obtained
  1407. * @return the font metrics for <code>font</code>
  1408. * @throws NullPointerException if <code>font</code> is null
  1409. * @since 1.5
  1410. */
  1411. public FontMetrics getFontMetrics(Font font) {
  1412. if (font != null && SwingUtilities2.drawTextAntialiased(aaText)) {
  1413. synchronized(aaFontMap) {
  1414. FontMetrics aaMetrics = aaFontMap.get(font);
  1415. if (aaMetrics == null) {
  1416. aaMetrics = new FontDesignMetrics(
  1417. font, SwingUtilities2.AA_FRC);
  1418. aaFontMap.put(font, aaMetrics);
  1419. }
  1420. return aaMetrics;
  1421. }
  1422. }
  1423. return super.getFontMetrics(font);
  1424. }
  1425. /**
  1426. * Sets the preferred size of this component.
  1427. * If <code>preferredSize</code> is <code>null</code>, the UI will
  1428. * be asked for the preferred size.
  1429. * @beaninfo
  1430. * preferred: true
  1431. * bound: true
  1432. * description: The preferred size of the component.
  1433. */
  1434. public void setPreferredSize(Dimension preferredSize) {
  1435. super.setPreferredSize(preferredSize);
  1436. }
  1437. /**
  1438. * If the <code>preferredSize</code> has been set to a
  1439. * non-<code>null</code> value just returns it.
  1440. * If the UI delegate's <code>getPreferredSize</code>
  1441. * method returns a non <code>null</code> value then return that;
  1442. * otherwise defer to the component's layout manager.
  1443. *
  1444. * @return the value of the <code>preferredSize</code> property
  1445. * @see #setPreferredSize
  1446. * @see ComponentUI
  1447. */
  1448. public Dimension getPreferredSize() {
  1449. if (isPreferredSizeSet()) {
  1450. return super.getPreferredSize();
  1451. }
  1452. Dimension size = null;
  1453. if (ui != null) {
  1454. size = ui.getPreferredSize(this);
  1455. }
  1456. return (size != null) ? size : super.getPreferredSize();
  1457. }
  1458. /**
  1459. * Sets the maximum size of this component to a constant
  1460. * value. Subsequent calls to <code>getMaximumSize</code> will always
  1461. * return this value; the component's UI will not be asked
  1462. * to compute it. Setting the maximum size to <code>null</code>
  1463. * restores the default behavior.
  1464. *
  1465. * @param maximumSize a <code>Dimension</code> containing the
  1466. * desired maximum allowable size
  1467. * @see #getMaximumSize
  1468. * @beaninfo
  1469. * bound: true
  1470. * description: The maximum size of the component.
  1471. */
  1472. public void setMaximumSize(Dimension maximumSize) {
  1473. super.setMaximumSize(maximumSize);
  1474. }
  1475. /**
  1476. * If the maximum size has been set to a non-<code>null</code> value
  1477. * just returns it. If the UI delegate's <code>getMaximumSize</code>
  1478. * method returns a non-<code>null</code> value then return that;
  1479. * otherwise defer to the component's layout manager.
  1480. *
  1481. * @return the value of the <code>maximumSize</code> property
  1482. * @see #setMaximumSize
  1483. * @see ComponentUI
  1484. */
  1485. public Dimension getMaximumSize() {
  1486. if (isMaximumSizeSet()) {
  1487. return super.getMaximumSize();
  1488. }
  1489. Dimension size = null;
  1490. if (ui != null) {
  1491. size = ui.getMaximumSize(this);
  1492. }
  1493. return (size != null) ? size : super.getMaximumSize();
  1494. }
  1495. /**
  1496. * Sets the minimum size of this component to a constant
  1497. * value. Subsequent calls to <code>getMinimumSize</code> will always
  1498. * return this value; the component's UI will not be asked
  1499. * to compute it. Setting the minimum size to <code>null</code>
  1500. * restores the default behavior.
  1501. *
  1502. * @param minimumSize the new minimum size of this component
  1503. * @see #getMinimumSize
  1504. * @beaninfo
  1505. * bound: true
  1506. * description: The minimum size of the component.
  1507. */
  1508. public void setMinimumSize(Dimension minimumSize) {
  1509. super.setMinimumSize(minimumSize);
  1510. }
  1511. /**
  1512. * If the minimum size has been set to a non-<code>null</code> value
  1513. * just returns it. If the UI delegate's <code>getMinimumSize</code>
  1514. * method returns a non-<code>null</code> value then return that; otherwise
  1515. * defer to the component's layout manager.
  1516. *
  1517. * @return the value of the <code>minimumSize</code> property
  1518. * @see #setMinimumSize
  1519. * @see ComponentUI
  1520. */
  1521. public Dimension getMinimumSize() {
  1522. if (isMinimumSizeSet()) {
  1523. return super.getMinimumSize();
  1524. }
  1525. Dimension size = null;
  1526. if (ui != null) {
  1527. size = ui.getMinimumSize(this);
  1528. }
  1529. return (size != null) ? size : super.getMinimumSize();
  1530. }
  1531. /**
  1532. * Gives the UI delegate an opportunity to define the precise
  1533. * shape of this component for the sake of mouse processing.
  1534. *
  1535. * @return true if this component logically contains x,y
  1536. * @see java.awt.Component#contains(int, int)
  1537. * @see ComponentUI
  1538. */
  1539. public boolean contains(int x, int y) {
  1540. return (ui != null) ? ui.contains(this, x, y) : super.contains(x, y);
  1541. }
  1542. /**
  1543. * Sets the border of this component. The <code>Border</code> object is
  1544. * responsible for defining the insets for the component
  1545. * (overriding any insets set directly on the component) and
  1546. * for optionally rendering any border decorations within the
  1547. * bounds of those insets. Borders should be used (rather
  1548. * than insets) for creating both decorative and non-decorative
  1549. * (such as margins and padding) regions for a swing component.
  1550. * Compound borders can be used to nest multiple borders within a
  1551. * single component.
  1552. * <p>
  1553. * Although technically you can set the border on any object
  1554. * that inherits from <code>JComponent</ocde>, the look and
  1555. * feel implementation of many standard Swing components
  1556. * doesn't work well with user-set borders. In general,
  1557. * when you want to set a border on a standard Swing
  1558. * component other than <code>JPanel</code> or <code>JLabel</code>,
  1559. * we recommend that you put the component in a <code>JPanel</code>
  1560. * and set the border on the <code>JPanel</code>.
  1561. * <p>
  1562. * This is a bound property.
  1563. *
  1564. * @param border the border to be rendered for this component
  1565. * @see Border
  1566. * @see CompoundBorder
  1567. * @beaninfo
  1568. * bound: true
  1569. * preferred: true
  1570. * attribute: visualUpdate true
  1571. * description: The component's border.
  1572. */
  1573. public void setBorder(Border border) {
  1574. Border oldBorder = this.border;
  1575. this.border = border;
  1576. firePropertyChange("border", oldBorder, border);
  1577. if (border != oldBorder) {
  1578. if (border == null || oldBorder == null ||
  1579. !(border.getBorderInsets(this).equals(oldBorder.getBorderInsets(this)))) {
  1580. revalidate();
  1581. }
  1582. repaint();
  1583. }
  1584. }
  1585. /**
  1586. * Returns the border of this component or <code>null</code> if no
  1587. * border is currently set.
  1588. *
  1589. * @return the border object for this component
  1590. * @see #setBorder
  1591. */
  1592. public Border getBorder() {
  1593. return border;
  1594. }
  1595. /**
  1596. * If a border has been set on this component, returns the
  1597. * border's insets; otherwise calls <code>super.getInsets</code>.
  1598. *
  1599. * @return the value of the insets property
  1600. * @see #setBorder
  1601. */
  1602. public Insets getInsets() {
  1603. if (border != null) {
  1604. return border.getBorderInsets(this);
  1605. }
  1606. return super.getInsets();
  1607. }
  1608. /**
  1609. * Returns an <code>Insets</code> object containing this component's inset
  1610. * values. The passed-in <code>Insets</code> object will be reused
  1611. * if possible.
  1612. * Calling methods cannot assume that the same object will be returned,
  1613. * however. All existing values within this object are overwritten.
  1614. * If <code>insets</code> is null, this will allocate a new one.
  1615. *
  1616. * @param insets the <code>Insets</code> object, which can be reused
  1617. * @return the <code>Insets</code> object
  1618. * @see #getInsets
  1619. * @beaninfo
  1620. * expert: true
  1621. */
  1622. public Insets getInsets(Insets insets) {
  1623. if (insets == null) {
  1624. insets = new Insets(0, 0, 0, 0);
  1625. }
  1626. if (border != null) {
  1627. if (border instanceof AbstractBorder) {
  1628. return ((AbstractBorder)border).getBorderInsets(this, insets);
  1629. } else {
  1630. // Can't reuse border insets because the Border interface
  1631. // can't be enhanced.
  1632. return border.getBorderInsets(this);
  1633. }
  1634. } else {
  1635. // super.getInsets() always returns an Insets object with
  1636. // all of its value zeroed. No need for a new object here.
  1637. insets.left = insets.top = insets.right = insets.bottom = 0;
  1638. return insets;
  1639. }
  1640. }
  1641. /**
  1642. * Overrides <code>Container.getAlignmentY</code> to return
  1643. * the horizontal alignment.
  1644. *
  1645. * @return the value of the <code>alignmentY</code> property
  1646. * @see #setAlignmentY
  1647. * @see java.awt.Component#getAlignmentY
  1648. */
  1649. public float getAlignmentY() {
  1650. if (isAlignmentYSet) {
  1651. return alignmentY;
  1652. }
  1653. return super.getAlignmentY();
  1654. }
  1655. /**
  1656. * Sets the the horizontal alignment.
  1657. *
  1658. * @param alignmentY the new horizontal alignment
  1659. * @see #getAlignmentY
  1660. * @beaninfo
  1661. * description: The preferred vertical alignment of the component.
  1662. */
  1663. public void setAlignmentY(float alignmentY) {
  1664. this.alignmentY = alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f : alignmentY;
  1665. isAlignmentYSet = true;
  1666. }
  1667. /**
  1668. * Overrides <code>Container.getAlignmentX</code> to return
  1669. * the vertical alignment.
  1670. *
  1671. * @return the value of the <code>alignmentX</code> property
  1672. * @see #setAlignmentX
  1673. * @see java.awt.Component#getAlignmentX
  1674. */
  1675. public float getAlignmentX() {
  1676. if (isAlignmentXSet) {
  1677. return alignmentX;
  1678. }
  1679. return super.getAlignmentX();
  1680. }
  1681. /**
  1682. * Sets the the vertical alignment.
  1683. *
  1684. * @param alignmentX the new vertical alignment
  1685. * @see #getAlignmentX
  1686. * @beaninfo
  1687. * description: The preferred horizontal alignment of the component.
  1688. */
  1689. public void setAlignmentX(float alignmentX) {
  1690. this.alignmentX = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f : alignmentX;
  1691. isAlignmentXSet = true;
  1692. }
  1693. /**
  1694. * Sets the input verifier for this component.
  1695. *
  1696. * @param inputVerifier the new input verifier
  1697. * @since 1.3
  1698. * @see InputVerifier
  1699. * @beaninfo
  1700. * bound: true
  1701. * description: The component's input verifier.
  1702. */
  1703. public void setInputVerifier(InputVerifier inputVerifier) {
  1704. InputVerifier oldInputVerifier = (InputVerifier)getClientProperty(
  1705. INPUT_VERIFIER_KEY);
  1706. putClientProperty(INPUT_VERIFIER_KEY, inputVerifier);
  1707. firePropertyChange("inputVerifier", oldInputVerifier, inputVerifier);
  1708. }
  1709. /**
  1710. * Returns the input verifier for this component.
  1711. *
  1712. * @return the <code>inputVerifier</code> property
  1713. * @since 1.3
  1714. * @see InputVerifier
  1715. */
  1716. public InputVerifier getInputVerifier() {
  1717. return (InputVerifier)getClientProperty(INPUT_VERIFIER_KEY);
  1718. }
  1719. /**
  1720. * Returns this component's graphics context, which lets you draw
  1721. * on a component. Use this method get a <code>Graphics</code> object and
  1722. * then invoke operations on that object to draw on the component.
  1723. * @return this components graphics context
  1724. */
  1725. public Graphics getGraphics() {
  1726. if (DEBUG_GRAPHICS_LOADED && shouldDebugGraphics() != 0) {
  1727. DebugGraphics graphics = new DebugGraphics(super.getGraphics(),
  1728. this);
  1729. return graphics;
  1730. }
  1731. return super.getGraphics();
  1732. }
  1733. /** Enables or disables diagnostic information about every graphics
  1734. * operation performed within the component or one of its children.
  1735. *
  1736. * @param debugOptions determines how the component should display
  1737. * the information; one of the following options:
  1738. * <ul>
  1739. * <li>DebugGraphics.LOG_OPTION - causes a text message to be printed.
  1740. * <li>DebugGraphics.FLASH_OPTION - causes the drawing to flash several
  1741. * times.
  1742. * <li>DebugGraphics.BUFFERED_OPTION - creates an
  1743. * <code>ExternalWindow</code> that displays the operations
  1744. * performed on the View's offscreen buffer.
  1745. * <li>DebugGraphics.NONE_OPTION disables debugging.
  1746. * <li>A value of 0 causes no changes to the debugging options.
  1747. * </ul>
  1748. * <code>debugOptions</code> is bitwise OR'd into the current value
  1749. *
  1750. * @beaninfo
  1751. * preferred: true
  1752. * enum: NONE_OPTION DebugGraphics.NONE_OPTION
  1753. * LOG_OPTION DebugGraphics.LOG_OPTION
  1754. * FLASH_OPTION DebugGraphics.FLASH_OPTION
  1755. * BUFFERED_OPTION DebugGraphics.BUFFERED_OPTION
  1756. * description: Diagnostic options for graphics operations.
  1757. */
  1758. public void setDebugGraphicsOptions(int debugOptions) {
  1759. DebugGraphics.setDebugOptions(this, debugOptions);
  1760. }
  1761. /** Returns the state of graphics debugging.
  1762. *
  1763. * @return a bitwise OR'd flag of zero or more of the following options:
  1764. * <ul>
  1765. * <li>DebugGraphics.LOG_OPTION - causes a text message to be printed.
  1766. * <li>DebugGraphics.FLASH_OPTION - causes the drawing to flash several
  1767. * times.
  1768. * <li>DebugGraphics.BUFFERED_OPTION - creates an
  1769. * <code>ExternalWindow</code> that displays the operations
  1770. * performed on the View's offscreen buffer.
  1771. * <li>DebugGraphics.NONE_OPTION disables debugging.
  1772. * <li>A value of 0 causes no changes to the debugging options.
  1773. * </ul>
  1774. * @see #setDebugGraphicsOptions
  1775. */
  1776. public int getDebugGraphicsOptions() {
  1777. return DebugGraphics.getDebugOptions(this);
  1778. }
  1779. /**
  1780. * Returns true if debug information is enabled for this
  1781. * <code>JComponent</code> or one of its parents.
  1782. */
  1783. int shouldDebugGraphics() {
  1784. return DebugGraphics.shouldComponentDebug(this);
  1785. }
  1786. /**
  1787. * This method is now obsolete, please use a combination of
  1788. * <code>getActionMap()</code> and <code>getInputMap()</code> for
  1789. * similiar behavior. For example, to bind the <code>KeyStroke</code>
  1790. * <code>aKeyStroke</code> to the <code>Action</code> <code>anAction</code>
  1791. * now use:
  1792. * <pre>
  1793. * component.getInputMap().put(aKeyStroke, aCommand);
  1794. * component.getActionMap().put(aCommmand, anAction);
  1795. * </pre>
  1796. * The above assumes you want the binding to be applicable for
  1797. * <code>WHEN_FOCUSED</code>. To register bindings for other focus
  1798. * states use the <code>getInputMap</code> method that takes an integer.
  1799. * <p>
  1800. * Register a new keyboard action.
  1801. * <code>anAction</code> will be invoked if a key event matching
  1802. * <code>aKeyStroke</code> occurs and <code>aCondition</code> is verified.
  1803. * The <code>KeyStroke</code> object defines a
  1804. * particular combination of a keyboard key and one or more modifiers
  1805. * (alt, shift, ctrl, meta).
  1806. * <p>
  1807. * The <code>aCommand</code> will be set in the delivered event if
  1808. * specified.
  1809. * <p>
  1810. * The <code>aCondition</code> can be one of:
  1811. * <blockquote>
  1812. * <DL>
  1813. * <DT>WHEN_FOCUSED
  1814. * <DD>The action will be invoked only when the keystroke occurs
  1815. * while the component has the focus.
  1816. * <DT>WHEN_IN_FOCUSED_WINDOW
  1817. * <DD>The action will be invoked when the keystroke occurs while
  1818. * the component has the focus or if the component is in the
  1819. * window that has the focus. Note that the component need not
  1820. * be an immediate descendent of the window -- it can be
  1821. * anywhere in the window's containment hierarchy. In other
  1822. * words, whenever <em>any</em> component in the window has the focus,
  1823. * the action registered with this component is invoked.
  1824. * <DT>WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
  1825. * <DD>The action will be invoked when the keystroke occurs while the
  1826. * component has the focus or if the component is an ancestor of
  1827. * the component that has the focus.
  1828. * </DL>
  1829. * </blockquote>
  1830. * <p>
  1831. * The combination of keystrokes and conditions lets you define high
  1832. * level (semantic) action events for a specified keystroke+modifier
  1833. * combination (using the KeyStroke class) and direct to a parent or
  1834. * child of a component that has the focus, or to the component itself.
  1835. * In other words, in any hierarchical structure of components, an
  1836. * arbitrary key-combination can be immediately directed to the
  1837. * appropriate component in the hierarchy, and cause a specific method
  1838. * to be invoked (usually by way of adapter objects).
  1839. * <p>
  1840. * If an action has already been registered for the receiving
  1841. * container, with the same charCode and the same modifiers,
  1842. * <code>anAction</code> will replace the action.
  1843. *
  1844. * @param anAction the <code>Action</code> to be registered
  1845. * @param aCommand the command to be set in the delivered event
  1846. * @param aKeyStroke the <code>KeyStroke</code> to bind to the action
  1847. * @param aCondition the condition that needs to be met, see above
  1848. * @see KeyStroke
  1849. */
  1850. public void registerKeyboardAction(ActionListener anAction,String aCommand,KeyStroke aKeyStroke,int aCondition) {
  1851. InputMap inputMap = getInputMap(aCondition, true);
  1852. if (inputMap != null) {
  1853. ActionMap actionMap = getActionMap(true);
  1854. ActionStandin action = new ActionStandin(anAction, aCommand);
  1855. inputMap.put(aKeyStroke, action);
  1856. if (actionMap != null) {
  1857. actionMap.put(action, action);
  1858. }
  1859. }
  1860. }
  1861. /**
  1862. * Registers any bound <code>WHEN_IN_FOCUSED_WINDOW</code> actions with
  1863. * the <code>KeyboardManager</code>. If <code>onlyIfNew</code>
  1864. * is true only actions that haven't been registered are pushed
  1865. * to the <code>KeyboardManager</code>
  1866. * otherwise all actions are pushed to the <code>KeyboardManager</code>.
  1867. *
  1868. * @param onlyIfNew if true, only actions that haven't been registered
  1869. * are pushed to the <code>KeyboardManager</code>
  1870. */
  1871. private void registerWithKeyboardManager(boolean onlyIfNew) {
  1872. InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW, false);
  1873. KeyStroke[] strokes;
  1874. Hashtable registered = (Hashtable)getClientProperty
  1875. (WHEN_IN_FOCUSED_WINDOW_BINDINGS);
  1876. if (inputMap != null) {
  1877. // Push any new KeyStrokes to the KeyboardManager.
  1878. strokes = inputMap.allKeys();
  1879. if (strokes != null) {
  1880. for (int counter = strokes.length - 1; counter >= 0;
  1881. counter--) {
  1882. if (!onlyIfNew || registered == null ||
  1883. registered.get(strokes[counter]) == null) {
  1884. registerWithKeyboardManager(strokes[counter]);
  1885. }
  1886. if (registered != null) {
  1887. registered.remove(strokes[counter]);
  1888. }
  1889. }
  1890. }
  1891. }
  1892. else {
  1893. strokes = null;
  1894. }
  1895. // Remove any old ones.
  1896. if (registered != null && registered.size() > 0) {
  1897. Enumeration keys = registered.keys();
  1898. while (keys.hasMoreElements()) {
  1899. KeyStroke ks = (KeyStroke)keys.nextElement();
  1900. unregisterWithKeyboardManager(ks);
  1901. }
  1902. registered.clear();
  1903. }
  1904. // Updated the registered Hashtable.
  1905. if (strokes != null && strokes.length > 0) {
  1906. if (registered == null) {
  1907. registered = new Hashtable(strokes.length);
  1908. putClientProperty(WHEN_IN_FOCUSED_WINDOW_BINDINGS, registered);
  1909. }
  1910. for (int counter = strokes.length - 1; counter >= 0; counter--) {
  1911. registered.put(strokes[counter], strokes[counter]);
  1912. }
  1913. }
  1914. else {
  1915. putClientProperty(WHEN_IN_FOCUSED_WINDOW_BINDINGS, null);
  1916. }
  1917. }
  1918. /**
  1919. * Unregisters all the previously registered
  1920. * <code>WHEN_IN_FOCUSED_WINDOW</code> <code>KeyStroke</code> bindings.
  1921. */
  1922. private void unregisterWithKeyboardManager() {
  1923. Hashtable registered = (Hashtable)getClientProperty
  1924. (WHEN_IN_FOCUSED_WINDOW_BINDINGS);
  1925. if (registered != null && registered.size() > 0) {
  1926. Enumeration keys = registered.keys();
  1927. while (keys.hasMoreElements()) {
  1928. KeyStroke ks = (KeyStroke)keys.nextElement();
  1929. unregisterWithKeyboardManager(ks);
  1930. }
  1931. }
  1932. putClientProperty(WHEN_IN_FOCUSED_WINDOW_BINDINGS, null);
  1933. }
  1934. /**
  1935. * Invoked from <code>ComponentInputMap</code> when its bindings change.
  1936. * If <code>inputMap</code> is the current <code>windowInputMap</code>
  1937. * (or a parent of the window <code>InputMap</code>)
  1938. * the <code>KeyboardManager</code> is notified of the new bindings.
  1939. *
  1940. * @param inputMap the map containing the new bindings
  1941. */
  1942. void componentInputMapChanged(ComponentInputMap inputMap) {
  1943. InputMap km = getInputMap(WHEN_IN_FOCUSED_WINDOW, false);
  1944. while (km != inputMap && km != null) {
  1945. km = (ComponentInputMap)km.getParent();
  1946. }
  1947. if (km != null) {
  1948. registerWithKeyboardManager(false);
  1949. }
  1950. }
  1951. private void registerWithKeyboardManager(KeyStroke aKeyStroke) {
  1952. KeyboardManager.getCurrentManager().registerKeyStroke(aKeyStroke,this);
  1953. }
  1954. private void unregisterWithKeyboardManager(KeyStroke aKeyStroke) {
  1955. KeyboardManager.getCurrentManager().unregisterKeyStroke(aKeyStroke,
  1956. this);
  1957. }
  1958. /**
  1959. * This method is now obsolete, please use a combination of
  1960. * <code>getActionMap()</code> and <code>getInputMap()</code> for
  1961. * similiar behavior.
  1962. */
  1963. public void registerKeyboardAction(ActionListener anAction,KeyStroke aKeyStroke,int aCondition) {
  1964. registerKeyboardAction(anAction,null,aKeyStroke,aCondition);
  1965. }
  1966. /**
  1967. * This method is now obsolete. To unregister an existing binding
  1968. * you can either remove the binding from the
  1969. * <code>ActionMap/InputMap</code>, or place a dummy binding the
  1970. * <code>InputMap</code>. Removing the binding from the
  1971. * <code>InputMap</code> allows bindings in parent <code>InputMap</code>s
  1972. * to be active, whereas putting a dummy binding in the
  1973. * <code>InputMap</code> effectively disables
  1974. * the binding from ever happening.
  1975. * <p>
  1976. * Unregisters a keyboard action.
  1977. * This will remove the binding from the <code>ActionMap</code>
  1978. * (if it exists) as well as the <code>InputMap</code>s.
  1979. */
  1980. public void unregisterKeyboardAction(KeyStroke aKeyStroke) {
  1981. ActionMap am = getActionMap(false);
  1982. for (int counter = 0; counter < 3; counter++) {
  1983. InputMap km = getInputMap(counter, false);
  1984. if (km != null) {
  1985. Object actionID = km.get(aKeyStroke);
  1986. if (am != null && actionID != null) {
  1987. am.remove(actionID);
  1988. }
  1989. km.remove(aKeyStroke);
  1990. }
  1991. }
  1992. }
  1993. /**
  1994. * Returns the <code>KeyStrokes</code> that will initiate
  1995. * registered actions.
  1996. *
  1997. * @return an array of <code>KeyStroke</code> objects
  1998. * @see #registerKeyboardAction
  1999. */
  2000. public KeyStroke[] getRegisteredKeyStrokes() {
  2001. int[] counts = new int[3];
  2002. KeyStroke[][] strokes = new KeyStroke[3][];
  2003. for (int counter = 0; counter < 3; counter++) {
  2004. InputMap km = getInputMap(counter, false);
  2005. strokes[counter] = (km != null) ? km.allKeys() : null;
  2006. counts[counter] = (strokes[counter] != null) ?
  2007. strokes[counter].length : 0;
  2008. }
  2009. KeyStroke[] retValue = new KeyStroke[counts[0] + counts[1] +
  2010. counts[2]];
  2011. for (int counter = 0, last = 0; counter < 3; counter++) {
  2012. if (counts[counter] > 0) {
  2013. System.arraycopy(strokes[counter], 0, retValue, last,
  2014. counts[counter]);
  2015. last += counts[counter];
  2016. }
  2017. }
  2018. return retValue;
  2019. }
  2020. /**
  2021. * Returns the condition that determines whether a registered action
  2022. * occurs in response to the specified keystroke.
  2023. * <p>
  2024. * For Java 2 platform v1.3, a <code>KeyStroke</code> can be associated
  2025. * with more than one condition.
  2026. * For example, 'a' could be bound for the two
  2027. * conditions <code>WHEN_FOCUSED</code> and
  2028. * <code>WHEN_IN_FOCUSED_WINDOW</code> condition.
  2029. *
  2030. * @return the action-keystroke condition
  2031. */
  2032. public int getConditionForKeyStroke(KeyStroke aKeyStroke) {
  2033. for (int counter = 0; counter < 3; counter++) {
  2034. InputMap inputMap = getInputMap(counter, false);
  2035. if (inputMap != null && inputMap.get(aKeyStroke) != null) {
  2036. return counter;
  2037. }
  2038. }
  2039. return UNDEFINED_CONDITION;
  2040. }
  2041. /**
  2042. * Returns the object that will perform the action registered for a
  2043. * given keystroke.
  2044. *
  2045. * @return the <code>ActionListener</code>
  2046. * object invoked when the keystroke occurs
  2047. */
  2048. public ActionListener getActionForKeyStroke(KeyStroke aKeyStroke) {
  2049. ActionMap am = getActionMap(false);
  2050. if (am == null) {
  2051. return null;
  2052. }
  2053. for (int counter = 0; counter < 3; counter++) {
  2054. InputMap inputMap = getInputMap(counter, false);
  2055. if (inputMap != null) {
  2056. Object actionBinding = inputMap.get(aKeyStroke);
  2057. if (actionBinding != null) {
  2058. Action action = am.get(actionBinding);
  2059. if (action instanceof ActionStandin) {
  2060. return ((ActionStandin)action).actionListener;
  2061. }
  2062. return action;
  2063. }
  2064. }
  2065. }
  2066. return null;
  2067. }
  2068. /**
  2069. * Unregisters all the bindings in the first tier <code>InputMaps</code>
  2070. * and <code>ActionMap</code>. This has the effect of removing any
  2071. * local bindings, and allowing the bindings defined in parent
  2072. * <code>InputMap/ActionMaps</code>
  2073. * (the UI is usually defined in the second tier) to persist.
  2074. */
  2075. public void resetKeyboardActions() {
  2076. // Keys
  2077. for (int counter = 0; counter < 3; counter++) {
  2078. InputMap inputMap = getInputMap(counter, false);
  2079. if (inputMap != null) {
  2080. inputMap.clear();
  2081. }
  2082. }
  2083. // Actions
  2084. ActionMap am = getActionMap(false);
  2085. if (am != null) {
  2086. am.clear();
  2087. }
  2088. }
  2089. /**
  2090. * Sets the <code>InputMap</code> to use under the condition
  2091. * <code>condition</code> to
  2092. * <code>map</code>. A <code>null</code> value implies you
  2093. * do not want any bindings to be used, even from the UI. This will
  2094. * not reinstall the UI <code>InputMap</code> (if there was one).
  2095. * <code>condition</code> has one of the following values:
  2096. * <ul>
  2097. * <li><code>WHEN_IN_FOCUSED_WINDOW</code>
  2098. * <li><code>WHEN_FOCUSED</code>
  2099. * <li><code>WHEN_ANCESTOR_OF_FOCUSED_COMPONENT</code>
  2100. * </ul>
  2101. * If <code>condition</code> is <code>WHEN_IN_FOCUSED_WINDOW</code>
  2102. * and <code>map</code> is not a <code>ComponentInputMap</code>, an
  2103. * <code>IllegalArgumentException</code> will be thrown.
  2104. * Similarly, if <code>condition</code> is not one of the values
  2105. * listed, an <code>IllegalArgumentException</code> will be thrown.
  2106. *
  2107. * @param condition one of the values listed above
  2108. * @param map the <code>InputMap</code> to use for the given condition
  2109. * @exception IllegalArgumentException if <code>condition</code> is
  2110. * <code>WHEN_IN_FOCUSED_WINDOW</code> and <code>map</code>
  2111. * is not an instance of <code>ComponentInputMap</code> or
  2112. * if <code>condition</code> is not one of the legal values
  2113. * specified above
  2114. * @since 1.3
  2115. */
  2116. public final void setInputMap(int condition, InputMap map) {
  2117. switch (condition) {
  2118. case WHEN_IN_FOCUSED_WINDOW:
  2119. if (map != null && !(map instanceof ComponentInputMap)) {
  2120. throw new IllegalArgumentException("WHEN_IN_FOCUSED_WINDOW InputMaps must be of type ComponentInputMap");
  2121. }
  2122. windowInputMap = (ComponentInputMap)map;
  2123. setFlag(WIF_INPUTMAP_CREATED, true);
  2124. registerWithKeyboardManager(false);
  2125. break;
  2126. case WHEN_ANCESTOR_OF_FOCUSED_COMPONENT:
  2127. ancestorInputMap = map;
  2128. setFlag(ANCESTOR_INPUTMAP_CREATED, true);
  2129. break;
  2130. case WHEN_FOCUSED:
  2131. focusInputMap = map;
  2132. setFlag(FOCUS_INPUTMAP_CREATED, true);
  2133. break;
  2134. default:
  2135. throw new IllegalArgumentException("condition must be one of JComponent.WHEN_IN_FOCUSED_WINDOW, JComponent.WHEN_FOCUSED or JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT");
  2136. }
  2137. }
  2138. /**
  2139. * Returns the <code>InputMap</code> that is used during
  2140. * <code>condition</code>.
  2141. *
  2142. * @param condition one of WHEN_IN_FOCUSED_WINDOW, WHEN_FOCUSED,
  2143. * WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
  2144. * @return the <code>InputMap</code> for the specified
  2145. * <code>condition</code>
  2146. * @since 1.3
  2147. */
  2148. public final InputMap getInputMap(int condition) {
  2149. return getInputMap(condition, true);
  2150. }
  2151. /**
  2152. * Returns the <code>InputMap</code> that is used when the
  2153. * component has focus.
  2154. * This is convenience method for <code>getInputMap(WHEN_FOCUSED)</code>.
  2155. *
  2156. * @return the <code>InputMap</code> used when the component has focus
  2157. * @since JDK1.3
  2158. */
  2159. public final InputMap getInputMap() {
  2160. return getInputMap(WHEN_FOCUSED, true);
  2161. }
  2162. /**
  2163. * Sets the <code>ActionMap</code> to <code>am</code>. This does not set
  2164. * the parent of the <code>am</code> to be the <code>ActionMap</code>
  2165. * from the UI (if there was one), it is up to the caller to have done this.
  2166. *
  2167. * @param am the new <code>ActionMap</code>
  2168. * @since 1.3
  2169. */
  2170. public final void setActionMap(ActionMap am) {
  2171. actionMap = am;
  2172. setFlag(ACTIONMAP_CREATED, true);
  2173. }
  2174. /**
  2175. * Returns the <code>ActionMap</code> used to determine what
  2176. * <code>Action</code> to fire for particular <code>KeyStroke</code>
  2177. * binding. The returned <code>ActionMap</code>, unless otherwise
  2178. * set, will have the <code>ActionMap</code> from the UI set as the parent.
  2179. *
  2180. * @return the <code>ActionMap</code> containing the key/action bindings
  2181. * @since 1.3
  2182. */
  2183. public final ActionMap getActionMap() {
  2184. return getActionMap(true);
  2185. }
  2186. /**
  2187. * Returns the <code>InputMap</code> to use for condition
  2188. * <code>condition</code>. If the <code>InputMap</code> hasn't
  2189. * been created, and <code>create</code> is
  2190. * true, it will be created.
  2191. *
  2192. * @param condition one of the following values:
  2193. * <ul>
  2194. * <li>JComponent.FOCUS_INPUTMAP_CREATED
  2195. * <li>JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
  2196. * <li>JComponent.WHEN_IN_FOCUSED_WINDOW
  2197. * </ul>
  2198. * @param create if true, create the <code>InputMap</code> if it
  2199. * is not already created
  2200. * @return the <code>InputMap</code> for the given <code>condition</code>
  2201. * if <code>create</code> is false and the <code>InputMap</code>
  2202. * hasn't been created, returns <code>null</code>
  2203. * @exception IllegalArgumentException if <code>condition</code>
  2204. * is not one of the legal values listed above
  2205. */
  2206. final InputMap getInputMap(int condition, boolean create) {
  2207. switch (condition) {
  2208. case WHEN_FOCUSED:
  2209. if (getFlag(FOCUS_INPUTMAP_CREATED)) {
  2210. return focusInputMap;
  2211. }
  2212. // Hasn't been created yet.
  2213. if (create) {
  2214. InputMap km = new InputMap();
  2215. setInputMap(condition, km);
  2216. return km;
  2217. }
  2218. break;
  2219. case WHEN_ANCESTOR_OF_FOCUSED_COMPONENT:
  2220. if (getFlag(ANCESTOR_INPUTMAP_CREATED)) {
  2221. return ancestorInputMap;
  2222. }
  2223. // Hasn't been created yet.
  2224. if (create) {
  2225. InputMap km = new InputMap();
  2226. setInputMap(condition, km);
  2227. return km;
  2228. }
  2229. break;
  2230. case WHEN_IN_FOCUSED_WINDOW:
  2231. if (getFlag(WIF_INPUTMAP_CREATED)) {
  2232. return windowInputMap;
  2233. }
  2234. // Hasn't been created yet.
  2235. if (create) {
  2236. ComponentInputMap km = new ComponentInputMap(this);
  2237. setInputMap(condition, km);
  2238. return km;
  2239. }
  2240. break;
  2241. default:
  2242. throw new IllegalArgumentException("condition must be one of JComponent.WHEN_IN_FOCUSED_WINDOW, JComponent.WHEN_FOCUSED or JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT");
  2243. }
  2244. return null;
  2245. }
  2246. /**
  2247. * Finds and returns the appropriate <code>ActionMap</code>.
  2248. *
  2249. * @param create if true, create the <code>ActionMap</code> if it
  2250. * is not already created
  2251. * @return the <code>ActionMap</code> for this component; if the
  2252. * <code>create</code> flag is false and there is no
  2253. * current <code>ActionMap</code>, returns <code>null</code>
  2254. */
  2255. final ActionMap getActionMap(boolean create) {
  2256. if (getFlag(ACTIONMAP_CREATED)) {
  2257. return actionMap;
  2258. }
  2259. // Hasn't been created.
  2260. if (create) {
  2261. ActionMap am = new ActionMap();
  2262. setActionMap(am);
  2263. return am;
  2264. }
  2265. return null;
  2266. }
  2267. /**
  2268. * In release 1.4, the focus subsystem was rearchitected.
  2269. * For more information, see
  2270. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
  2271. * How to Use the Focus Subsystem</a>,
  2272. * a section in <em>The Java Tutorial</em>.
  2273. * <p>
  2274. * Requests focus on this <code>JComponent</code>'s
  2275. * <code>FocusTraversalPolicy</code>'s default <code>Component</code>.
  2276. * If this <code>JComponent</code> is a focus cycle root, then its
  2277. * <code>FocusTraversalPolicy</code> is used. Otherwise, the
  2278. * <code>FocusTraversalPolicy</code> of this <code>JComponent</code>'s
  2279. * focus-cycle-root ancestor is used.
  2280. *
  2281. * @see java.awt.FocusTraversalPolicy#getDefaultComponent
  2282. * @deprecated As of 1.4, replaced by
  2283. * <code>FocusTraversalPolicy.getDefaultComponent(Container).requestFocus()</code>
  2284. */
  2285. @Deprecated
  2286. public boolean requestDefaultFocus() {
  2287. Container nearestRoot =
  2288. (isFocusCycleRoot()) ? this : getFocusCycleRootAncestor();
  2289. if (nearestRoot == null) {
  2290. return false;
  2291. }
  2292. Component comp = nearestRoot.getFocusTraversalPolicy().
  2293. getDefaultComponent(nearestRoot);
  2294. if (comp != null) {
  2295. comp.requestFocus();
  2296. return true;
  2297. } else {
  2298. return false;
  2299. }
  2300. }
  2301. /**
  2302. * Makes the component visible or invisible.
  2303. * Overrides <code>Component.setVisible</code>.
  2304. *
  2305. * @param aFlag true to make the component visible; false to
  2306. * make it invisible
  2307. *
  2308. * @beaninfo
  2309. * attribute: visualUpdate true
  2310. */
  2311. public void setVisible(boolean aFlag) {
  2312. if(aFlag != isVisible()) {
  2313. super.setVisible(aFlag);
  2314. Container parent = getParent();
  2315. if(parent != null) {
  2316. Rectangle r = getBounds();
  2317. parent.repaint(r.x,r.y,r.width,r.height);
  2318. }
  2319. // Some (all should) LayoutManagers do not consider components
  2320. // that are not visible. As such we need to revalidate when the
  2321. // visible bit changes.
  2322. revalidate();
  2323. }
  2324. }
  2325. /**
  2326. * Sets whether or not this component is enabled.
  2327. * A component that is enabled may respond to user input,
  2328. * while a component that is not enabled cannot respond to
  2329. * user input. Some components may alter their visual
  2330. * representation when they are disabled in order to
  2331. * provide feedback to the user that they cannot take input.
  2332. * <p>Note: Disabling a component does not disable it's children.
  2333. *
  2334. * <p>Note: Disabling a lightweight component does not prevent it from
  2335. * receiving MouseEvents.
  2336. *
  2337. * @param enabled true if this component should be enabled, false otherwise
  2338. * @see java.awt.Component#isEnabled
  2339. * @see java.awt.Component#isLightweight
  2340. *
  2341. * @beaninfo
  2342. * preferred: true
  2343. * bound: true
  2344. * attribute: visualUpdate true
  2345. * description: The enabled state of the component.
  2346. */
  2347. public void setEnabled(boolean enabled) {
  2348. boolean oldEnabled = isEnabled();
  2349. super.setEnabled(enabled);
  2350. firePropertyChange("enabled", oldEnabled, enabled);
  2351. if (enabled != oldEnabled) {
  2352. repaint();
  2353. }
  2354. }
  2355. /**
  2356. * Sets the foreground color of this component.
  2357. *
  2358. * @param fg the desired foreground <code>Color</code>
  2359. * @see java.awt.Component#getForeground
  2360. *
  2361. * @beaninfo
  2362. * preferred: true
  2363. * bound: true
  2364. * attribute: visualUpdate true
  2365. * description: The foreground color of the component.
  2366. */
  2367. public void setForeground(Color fg) {
  2368. Color oldFg = getForeground();
  2369. super.setForeground(fg);
  2370. if ((oldFg != null) ? !oldFg.equals(fg) : ((fg != null) && !fg.equals(oldFg))) {
  2371. // foreground already bound in AWT1.2
  2372. repaint();
  2373. }
  2374. }
  2375. /**
  2376. * Sets the background color of this component.
  2377. *
  2378. * @param bg the desired background <code>Color</code>
  2379. * @see java.awt.Component#getBackground
  2380. *
  2381. * @beaninfo
  2382. * preferred: true
  2383. * bound: true
  2384. * attribute: visualUpdate true
  2385. * description: The background color of the component.
  2386. */
  2387. public void setBackground(Color bg) {
  2388. Color oldBg = getBackground();
  2389. super.setBackground(bg);
  2390. if ((oldBg != null) ? !oldBg.equals(bg) : ((bg != null) && !bg.equals(oldBg))) {
  2391. // background already bound in AWT1.2
  2392. repaint();
  2393. }
  2394. }
  2395. /**
  2396. * Sets the font for this component.
  2397. *
  2398. * @param font the desired <code>Font</code> for this component
  2399. * @see java.awt.Component#getFont
  2400. *
  2401. * @beaninfo
  2402. * preferred: true
  2403. * bound: true
  2404. * attribute: visualUpdate true
  2405. * description: The font for the component.
  2406. */
  2407. public void setFont(Font font) {
  2408. Font oldFont = getFont();
  2409. super.setFont(font);
  2410. // font already bound in AWT1.2
  2411. if (font != oldFont) {
  2412. revalidate();
  2413. repaint();
  2414. }
  2415. }
  2416. /**
  2417. * Returns the default locale used to initialize each JComponent's
  2418. * locale property upon creation.
  2419. *
  2420. * The default locale has "AppContext" scope so that applets (and
  2421. * potentially multiple lightweight applications running in a single VM)
  2422. * can have their own setting. An applet can safely alter its default
  2423. * locale because it will have no affect on other applets (or the browser).
  2424. *
  2425. * @return the default <code>Locale</code>.
  2426. * @see #setDefaultLocale
  2427. * @see java.awt.Component#getLocale
  2428. * @see #setLocale
  2429. * @since 1.4
  2430. */
  2431. static public Locale getDefaultLocale() {
  2432. Locale l = (Locale) SwingUtilities.appContextGet(defaultLocale);
  2433. if( l == null ) {
  2434. //REMIND(bcb) choosing the default value is more complicated
  2435. //than this.
  2436. l = Locale.getDefault();
  2437. JComponent.setDefaultLocale( l );
  2438. }
  2439. return l;
  2440. }
  2441. /**
  2442. * Sets the default locale used to initialize each JComponent's locale
  2443. * property upon creation. The initial value is the VM's default locale.
  2444. *
  2445. * The default locale has "AppContext" scope so that applets (and
  2446. * potentially multiple lightweight applications running in a single VM)
  2447. * can have their own setting. An applet can safely alter its default
  2448. * locale because it will have no affect on other applets (or the browser).
  2449. *
  2450. * @param l the desired default <code>Locale</code> for new components.
  2451. * @see #getDefaultLocale
  2452. * @see java.awt.Component#getLocale
  2453. * @see #setLocale
  2454. * @since 1.4
  2455. */
  2456. static public void setDefaultLocale( Locale l ) {
  2457. SwingUtilities.appContextPut(defaultLocale, l);
  2458. }
  2459. /**
  2460. * Processes any key events that the component itself
  2461. * recognizes. This is called after the focus
  2462. * manager and any interested listeners have been
  2463. * given a chance to steal away the event. This
  2464. * method is called only if the event has not
  2465. * yet been consumed. This method is called prior
  2466. * to the keyboard UI logic.
  2467. * <p>
  2468. * This method is implemented to do nothing. Subclasses would
  2469. * normally override this method if they process some
  2470. * key events themselves. If the event is processed,
  2471. * it should be consumed.
  2472. */
  2473. protected void processComponentKeyEvent(KeyEvent e) {
  2474. }
  2475. /** Overrides <code>processKeyEvent</code> to process events. **/
  2476. protected void processKeyEvent(KeyEvent e) {
  2477. boolean result;
  2478. boolean shouldProcessKey;
  2479. // This gives the key event listeners a crack at the event
  2480. super.processKeyEvent(e);
  2481. // give the component itself a crack at the event
  2482. if (! e.isConsumed()) {
  2483. processComponentKeyEvent(e);
  2484. }
  2485. shouldProcessKey = KeyboardState.shouldProcess(e);
  2486. if(e.isConsumed()) {
  2487. return;
  2488. }
  2489. if (shouldProcessKey && processKeyBindings(e, e.getID() ==
  2490. KeyEvent.KEY_PRESSED)) {
  2491. e.consume();
  2492. }
  2493. }
  2494. /**
  2495. * Invoked to process the key bindings for <code>ks</code> as the result
  2496. * of the <code>KeyEvent</code> <code>e</code>. This obtains
  2497. * the appropriate <code>InputMap</code>,
  2498. * gets the binding, gets the action from the <code>ActionMap</code>,
  2499. * and then (if the action is found and the component
  2500. * is enabled) invokes <code>notifyAction</code> to notify the action.
  2501. *
  2502. * @param ks the <code>KeyStroke</code> queried
  2503. * @param e the <code>KeyEvent</code>
  2504. * @param condition one of the following values:
  2505. * <ul>
  2506. * <li>JComponent.WHEN_FOCUSED
  2507. * <li>JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
  2508. * <li>JComponent.WHEN_IN_FOCUSED_WINDOW
  2509. * </ul>
  2510. * @param pressed true if the key is pressed
  2511. * @return true if there was a binding to an action, and the action
  2512. * was enabled
  2513. *
  2514. * @since 1.3
  2515. */
  2516. protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,
  2517. int condition, boolean pressed) {
  2518. InputMap map = getInputMap(condition, false);
  2519. ActionMap am = getActionMap(false);
  2520. if(map != null && am != null && isEnabled()) {
  2521. Object binding = map.get(ks);
  2522. Action action = (binding == null) ? null : am.get(binding);
  2523. if (action != null) {
  2524. return SwingUtilities.notifyAction(action, ks, e, this,
  2525. e.getModifiers());
  2526. }
  2527. }
  2528. return false;
  2529. }
  2530. /**
  2531. * This is invoked as the result of a <code>KeyEvent</code>
  2532. * that was not consumed by the <code>FocusManager</code>,
  2533. * <code>KeyListeners</code>, or the component. It will first try
  2534. * <code>WHEN_FOCUSED</code> bindings,
  2535. * then <code>WHEN_ANCESTOR_OF_FOCUSED_COMPONENT</code> bindings,
  2536. * and finally <code>WHEN_IN_FOCUSED_WINDOW</code> bindings.
  2537. *
  2538. * @param e the unconsumed <code>KeyEvent</code>
  2539. * @param pressed true if the key is pressed
  2540. * @return true if there is a key binding for <code>e</code>
  2541. */
  2542. boolean processKeyBindings(KeyEvent e, boolean pressed) {
  2543. if (!SwingUtilities.isValidKeyEventForKeyBindings(e)) {
  2544. return false;
  2545. }
  2546. // Get the KeyStroke
  2547. KeyStroke ks;
  2548. if (e.getID() == KeyEvent.KEY_TYPED) {
  2549. ks = KeyStroke.getKeyStroke(e.getKeyChar());
  2550. }
  2551. else {
  2552. ks = KeyStroke.getKeyStroke(e.getKeyCode(),e.getModifiers(),
  2553. (pressed ? false:true));
  2554. }
  2555. /* Do we have a key binding for e? */
  2556. if(processKeyBinding(ks, e, WHEN_FOCUSED, pressed))
  2557. return true;
  2558. /* We have no key binding. Let's try the path from our parent to the
  2559. * window excluded. We store the path components so we can avoid
  2560. * asking the same component twice.
  2561. */
  2562. Container parent = this;
  2563. while (parent != null && !(parent instanceof Window) &&
  2564. !(parent instanceof Applet)) {
  2565. if(parent instanceof JComponent) {
  2566. if(((JComponent)parent).processKeyBinding(ks, e,
  2567. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, pressed))
  2568. return true;
  2569. }
  2570. // This is done so that the children of a JInternalFrame are
  2571. // given precedence for WHEN_IN_FOCUSED_WINDOW bindings before
  2572. // other components WHEN_IN_FOCUSED_WINDOW bindings. This also gives
  2573. // more precedence to the WHEN_IN_FOCUSED_WINDOW bindings of the
  2574. // JInternalFrame's children vs the
  2575. // WHEN_ANCESTOR_OF_FOCUSED_COMPONENT bindings of the parents.
  2576. // maybe generalize from JInternalFrame (like isFocusCycleRoot).
  2577. if ((parent instanceof JInternalFrame) &&
  2578. JComponent.processKeyBindingsForAllComponents(e,parent,pressed)){
  2579. return true;
  2580. }
  2581. parent = parent.getParent();
  2582. }
  2583. /* No components between the focused component and the window is
  2584. * actually interested by the key event. Let's try the other
  2585. * JComponent in this window.
  2586. */
  2587. if(parent != null) {
  2588. return JComponent.processKeyBindingsForAllComponents(e,parent,pressed);
  2589. }
  2590. return false;
  2591. }
  2592. static boolean processKeyBindingsForAllComponents(KeyEvent e,
  2593. Container container, boolean pressed) {
  2594. while (true) {
  2595. if (KeyboardManager.getCurrentManager().fireKeyboardAction(
  2596. e, pressed, container)) {
  2597. return true;
  2598. }
  2599. if (container instanceof Popup.HeavyWeightWindow) {
  2600. container = ((Window)container).getOwner();
  2601. }
  2602. else {
  2603. return false;
  2604. }
  2605. }
  2606. }
  2607. /**
  2608. * Registers the text to display in a tool tip.
  2609. * The text displays when the cursor lingers over the component.
  2610. * <p>
  2611. * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/tooltip.html">How to Use Tool Tips</a>
  2612. * in <em>The Java Tutorial</em>
  2613. * for further documentation.
  2614. *
  2615. * @param text the string to display; if the text is <code>null</code>,
  2616. * the tool tip is turned off for this component
  2617. * @see #TOOL_TIP_TEXT_KEY
  2618. * @beaninfo
  2619. * preferred: true
  2620. * description: The text to display in a tool tip.
  2621. */
  2622. public void setToolTipText(String text) {
  2623. String oldText = getToolTipText();
  2624. putClientProperty(TOOL_TIP_TEXT_KEY, text);
  2625. ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
  2626. if (text != null) {
  2627. if (oldText == null) {
  2628. toolTipManager.registerComponent(this);
  2629. }
  2630. } else {
  2631. toolTipManager.unregisterComponent(this);
  2632. }
  2633. }
  2634. /**
  2635. * Returns the tooltip string that has been set with
  2636. * <code>setToolTipText</code>.
  2637. *
  2638. * @return the text of the tool tip
  2639. * @see #TOOL_TIP_TEXT_KEY
  2640. */
  2641. public String getToolTipText() {
  2642. return (String)getClientProperty(TOOL_TIP_TEXT_KEY);
  2643. }
  2644. /**
  2645. * Returns the string to be used as the tooltip for <i>event</i>.
  2646. * By default this returns any string set using
  2647. * <code>setToolTipText</code>. If a component provides
  2648. * more extensive API to support differing tooltips at different locations,
  2649. * this method should be overridden.
  2650. */
  2651. public String getToolTipText(MouseEvent event) {
  2652. return getToolTipText();
  2653. }
  2654. /**
  2655. * Returns the tooltip location in this component's coordinate system.
  2656. * If <code>null</code> is returned, Swing will choose a location.
  2657. * The default implementation returns <code>null</code>.
  2658. *
  2659. * @param event the <code>MouseEvent</code> that caused the
  2660. * <code>ToolTipManager</code> to show the tooltip
  2661. * @return always returns <code>null</code>
  2662. */
  2663. public Point getToolTipLocation(MouseEvent event) {
  2664. return null;
  2665. }
  2666. /**
  2667. * Returns the preferred location to display the popup menu in this
  2668. * component's coordinate system. It is up to the look and feel to
  2669. * honor this propery, some may choose to ignore it. If <code>null</code>
  2670. * is truend the look and feel will choose a suitable location.
  2671. *
  2672. * @param event the <code>MouseEvent</code> that triggered the popup
  2673. * to be shown, or null if popup was is not being shown as the
  2674. * result of a mouse event
  2675. * @return Locatino to display the JPopupMenu.
  2676. * @since 1.5
  2677. */
  2678. public Point getPopupLocation(MouseEvent event) {
  2679. return null;
  2680. }
  2681. /**
  2682. * Returns the instance of <code>JToolTip</code> that should be used
  2683. * to display the tooltip.
  2684. * Components typically would not override this method,
  2685. * but it can be used to
  2686. * cause different tooltips to be displayed differently.
  2687. *
  2688. * @return the <code>JToolTip</code> used to display this toolTip
  2689. */
  2690. public JToolTip createToolTip() {
  2691. JToolTip tip = new JToolTip();
  2692. tip.setComponent(this);
  2693. return tip;
  2694. }
  2695. /**
  2696. * Forwards the <code>scrollRectToVisible()</code> message to the
  2697. * <code>JComponent</code>'s parent. Components that can service
  2698. * the request, such as <code>JViewport</code>,
  2699. * override this method and perform the scrolling.
  2700. *
  2701. * @param aRect the visible <code>Rectangle</code>
  2702. * @see JViewport
  2703. */
  2704. public void scrollRectToVisible(Rectangle aRect) {
  2705. Container parent;
  2706. int dx = getX(), dy = getY();
  2707. for (parent = getParent();
  2708. !(parent == null) &&
  2709. !(parent instanceof JComponent) &&
  2710. !(parent instanceof CellRendererPane);
  2711. parent = parent.getParent()) {
  2712. Rectangle bounds = parent.getBounds();
  2713. dx += bounds.x;
  2714. dy += bounds.y;
  2715. }
  2716. if (!(parent == null) && !(parent instanceof CellRendererPane)) {
  2717. aRect.x += dx;
  2718. aRect.y += dy;
  2719. ((JComponent)parent).scrollRectToVisible(aRect);
  2720. aRect.x -= dx;
  2721. aRect.y -= dy;
  2722. }
  2723. }
  2724. /**
  2725. * Sets the <code>autoscrolls</code> property.
  2726. * If <code>true</code> mouse dragged events will be
  2727. * synthetically generated when the mouse is dragged
  2728. * outside of the component's bounds and mouse motion
  2729. * has paused (while the button continues to be held
  2730. * down). The synthetic events make it appear that the
  2731. * drag gesture has resumed in the direction established when
  2732. * the component's boundary was crossed. Components that
  2733. * support autoscrolling must handle <code>mouseDragged</code>
  2734. * events by calling <code>scrollRectToVisible</code> with a
  2735. * rectangle that contains the mouse event's location. All of
  2736. * the Swing components that support item selection and are
  2737. * typically displayed in a <code>JScrollPane</code>
  2738. * (<code>JTable</code>, <code>JList</code>, <code>JTree</code>,
  2739. * <code>JTextArea</code>, and <code>JEditorPane</code>)
  2740. * already handle mouse dragged events in this way. To enable
  2741. * autoscrolling in any other component, add a mouse motion
  2742. * listener that calls <code>scrollRectToVisible</code>.
  2743. * For example, given a <code>JPanel</code>, <code>myPanel</code>:
  2744. * <pre>
  2745. * MouseMotionListener doScrollRectToVisible = new MouseMotionAdapter() {
  2746. * public void mouseDragged(MouseEvent e) {
  2747. * Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1);
  2748. * ((JPanel)e.getSource()).scrollRectToVisible(r);
  2749. * }
  2750. * };
  2751. * myPanel.addMouseMotionListener(doScrollRectToVisible);
  2752. * </pre>
  2753. * The default value of the <code>autoScrolls</code>
  2754. * property is <code>false</code>.
  2755. *
  2756. * @param autoscrolls if true, synthetic mouse dragged events
  2757. * are generated when the mouse is dragged outside of a component's
  2758. * bounds and the mouse button continues to be held down; otherwise
  2759. * false
  2760. * @see #getAutoscrolls
  2761. * @see JViewport
  2762. * @see JScrollPane
  2763. *
  2764. * @beaninfo
  2765. * expert: true
  2766. * description: Determines if this component automatically scrolls its contents when dragged.
  2767. */
  2768. public void setAutoscrolls(boolean autoscrolls) {
  2769. setFlag(AUTOSCROLLS_SET, true);
  2770. if (this.autoscrolls != autoscrolls) {
  2771. this.autoscrolls = autoscrolls;
  2772. if (autoscrolls) {
  2773. enableEvents(AWTEvent.MOUSE_EVENT_MASK);
  2774. enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
  2775. }
  2776. else {
  2777. Autoscroller.stop(this);
  2778. }
  2779. }
  2780. }
  2781. /**
  2782. * Gets the <code>autoscrolls</code> property.
  2783. *
  2784. * @return the value of the <code>autoscrolls</code> property
  2785. * @see JViewport
  2786. * @see #setAutoscrolls
  2787. */
  2788. public boolean getAutoscrolls() {
  2789. return autoscrolls;
  2790. }
  2791. /**
  2792. * Sets the <code>transferHandler</code> property,
  2793. * which is <code>null</code> if the component does
  2794. * not support data transfer operations.
  2795. * <p>
  2796. * If <code>newHandler</code> is not <code>null</code>,
  2797. * and the system property
  2798. * <code>suppressSwingDropSupport</code> is not true, this will
  2799. * install a <code>DropTarget</code> on the <code>JComponent</code>.
  2800. * The default for the system property is false, so that a
  2801. * <code>DropTarget</code> will be added.
  2802. * <p>
  2803. * Please see
  2804. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/dnd.html">
  2805. * How to Use Drag and Drop and Data Transfer</a>,
  2806. * a section in <em>The Java Tutorial</em>, for more information.
  2807. *
  2808. * @param newHandler mechanism for transfer of data to
  2809. * and from the component
  2810. *
  2811. * @see TransferHandler
  2812. * @see #getTransferHandler
  2813. * @since 1.4
  2814. * @beaninfo
  2815. * bound: true
  2816. * hidden: true
  2817. * description: Mechanism for transfer of data to and from the component
  2818. */
  2819. public void setTransferHandler(TransferHandler newHandler) {
  2820. TransferHandler oldHandler = (TransferHandler)getClientProperty(
  2821. TRANSFER_HANDLER_KEY);
  2822. putClientProperty(TRANSFER_HANDLER_KEY, newHandler);
  2823. if (! getSuppressDropTarget()) {
  2824. DropTarget dropHandler = getDropTarget();
  2825. if ((dropHandler == null) || (dropHandler instanceof UIResource)) {
  2826. if (newHandler == null) {
  2827. setDropTarget(null);
  2828. } else if (!GraphicsEnvironment.isHeadless()) {
  2829. setDropTarget(new TransferHandler.SwingDropTarget(this));
  2830. }
  2831. }
  2832. }
  2833. firePropertyChange("transferHandler", oldHandler, newHandler);
  2834. }
  2835. /**
  2836. * Gets the <code>transferHandler</code> property.
  2837. *
  2838. * @return the value of the <code>transferHandler</code> property
  2839. *
  2840. * @see TransferHandler
  2841. * @see #setTransferHandler
  2842. * @since 1.4
  2843. */
  2844. public TransferHandler getTransferHandler() {
  2845. return (TransferHandler)getClientProperty(TRANSFER_HANDLER_KEY);
  2846. }
  2847. /**
  2848. * Processes mouse events occurring on this component by
  2849. * dispatching them to any registered
  2850. * <code>MouseListener</code> objects, refer to
  2851. * {@link java.awt.Component#processMouseEvent(MouseEvent)}
  2852. * for a complete description of this method.
  2853. *
  2854. * @param e the mouse event
  2855. * @see java.awt.Component#processMouseEvent
  2856. * @since 1.5
  2857. */
  2858. protected void processMouseEvent(MouseEvent e) {
  2859. if (autoscrolls && e.getID() == MouseEvent.MOUSE_RELEASED) {
  2860. Autoscroller.stop(this);
  2861. }
  2862. super.processMouseEvent(e);
  2863. }
  2864. /**
  2865. * Processes mouse motion events, such as MouseEvent.MOUSE_DRAGGED.
  2866. *
  2867. * @param e the <code>MouseEvent</code>
  2868. * @see MouseEvent
  2869. */
  2870. protected void processMouseMotionEvent(MouseEvent e) {
  2871. boolean dispatch = true;
  2872. if (autoscrolls && e.getID() == MouseEvent.MOUSE_DRAGGED) {
  2873. // We don't want to do the drags when the mouse moves if we're
  2874. // autoscrolling. It makes it feel spastic.
  2875. dispatch = !Autoscroller.isRunning(this);
  2876. Autoscroller.processMouseDragged(e);
  2877. }
  2878. if (dispatch) {
  2879. super.processMouseMotionEvent(e);
  2880. }
  2881. }
  2882. // Inner classes can't get at this method from a super class
  2883. void superProcessMouseMotionEvent(MouseEvent e) {
  2884. super.processMouseMotionEvent(e);
  2885. }
  2886. /**
  2887. * This is invoked by the <code>RepaintManager</code> if
  2888. * <code>createImage</code> is called on the component.
  2889. *
  2890. * @param newValue true if the double buffer image was created from this component
  2891. */
  2892. void setCreatedDoubleBuffer(boolean newValue) {
  2893. setFlag(CREATED_DOUBLE_BUFFER, newValue);
  2894. }
  2895. /**
  2896. * Returns true if the <code>RepaintManager</code>
  2897. * created the double buffer image from the component.
  2898. *
  2899. * @return true if this component had a double buffer image, false otherwise
  2900. */
  2901. boolean getCreatedDoubleBuffer() {
  2902. return getFlag(CREATED_DOUBLE_BUFFER);
  2903. }
  2904. /**
  2905. * <code>ActionStandin</code> is used as a standin for
  2906. * <code>ActionListeners</code> that are
  2907. * added via <code>registerKeyboardAction</code>.
  2908. */
  2909. final class ActionStandin implements Action {
  2910. private final ActionListener actionListener;
  2911. private final String command;
  2912. // This will be non-null if actionListener is an Action.
  2913. private final Action action;
  2914. ActionStandin(ActionListener actionListener, String command) {
  2915. this.actionListener = actionListener;
  2916. if (actionListener instanceof Action) {
  2917. this.action = (Action)actionListener;
  2918. }
  2919. else {
  2920. this.action = null;
  2921. }
  2922. this.command = command;
  2923. }
  2924. public Object getValue(String key) {
  2925. if (key != null) {
  2926. if (key.equals(Action.ACTION_COMMAND_KEY)) {
  2927. return command;
  2928. }
  2929. if (action != null) {
  2930. return action.getValue(key);
  2931. }
  2932. if (key.equals(NAME)) {
  2933. return "ActionStandin";
  2934. }
  2935. }
  2936. return null;
  2937. }
  2938. public boolean isEnabled() {
  2939. if (actionListener == null) {
  2940. // This keeps the old semantics where
  2941. // registerKeyboardAction(null) would essentialy remove
  2942. // the binding. We don't remove the binding from the
  2943. // InputMap as that would still allow parent InputMaps
  2944. // bindings to be accessed.
  2945. return false;
  2946. }
  2947. if (action == null) {
  2948. return true;
  2949. }
  2950. return action.isEnabled();
  2951. }
  2952. public void actionPerformed(ActionEvent ae) {
  2953. if (actionListener != null) {
  2954. actionListener.actionPerformed(ae);
  2955. }
  2956. }
  2957. // We don't allow any values to be added.
  2958. public void putValue(String key, Object value) {}
  2959. // Does nothing, our enabledness is determiend from our asociated
  2960. // action.
  2961. public void setEnabled(boolean b) { }
  2962. public void addPropertyChangeListener
  2963. (PropertyChangeListener listener) {}
  2964. public void removePropertyChangeListener
  2965. (PropertyChangeListener listener) {}
  2966. }
  2967. // This class is used by the KeyboardState class to provide a single
  2968. // instance that can be stored in the AppContext.
  2969. static final class IntVector {
  2970. int array[] = null;
  2971. int count = 0;
  2972. int capacity = 0;
  2973. int size() {
  2974. return count;
  2975. }
  2976. int elementAt(int index) {
  2977. return array[index];
  2978. }
  2979. void addElement(int value) {
  2980. if (count == capacity) {
  2981. capacity = (capacity + 2) * 2;
  2982. int[] newarray = new int[capacity];
  2983. if (count > 0) {
  2984. System.arraycopy(array, 0, newarray, 0, count);
  2985. }
  2986. array = newarray;
  2987. }
  2988. array[count++] = value;
  2989. }
  2990. void setElementAt(int value, int index) {
  2991. array[index] = value;
  2992. }
  2993. }
  2994. static class KeyboardState implements Serializable {
  2995. private static final Object keyCodesKey =
  2996. JComponent.KeyboardState.class;
  2997. // Get the array of key codes from the AppContext.
  2998. static IntVector getKeyCodeArray() {
  2999. IntVector iv =
  3000. (IntVector)SwingUtilities.appContextGet(keyCodesKey);
  3001. if (iv == null) {
  3002. iv = new IntVector();
  3003. SwingUtilities.appContextPut(keyCodesKey, iv);
  3004. }
  3005. return iv;
  3006. }
  3007. static void registerKeyPressed(int keyCode) {
  3008. IntVector kca = getKeyCodeArray();
  3009. int count = kca.size();
  3010. int i;
  3011. for(i=0;i<count;i++) {
  3012. if(kca.elementAt(i) == -1){
  3013. kca.setElementAt(keyCode, i);
  3014. return;
  3015. }
  3016. }
  3017. kca.addElement(keyCode);
  3018. }
  3019. static void registerKeyReleased(int keyCode) {
  3020. IntVector kca = getKeyCodeArray();
  3021. int count = kca.size();
  3022. int i;
  3023. for(i=0;i<count;i++) {
  3024. if(kca.elementAt(i) == keyCode) {
  3025. kca.setElementAt(-1, i);
  3026. return;
  3027. }
  3028. }
  3029. }
  3030. static boolean keyIsPressed(int keyCode) {
  3031. IntVector kca = getKeyCodeArray();
  3032. int count = kca.size();
  3033. int i;
  3034. for(i=0;i<count;i++) {
  3035. if(kca.elementAt(i) == keyCode) {
  3036. return true;
  3037. }
  3038. }
  3039. return false;
  3040. }
  3041. /**
  3042. * Updates internal state of the KeyboardState and returns true
  3043. * if the event should be processed further.
  3044. */
  3045. static boolean shouldProcess(KeyEvent e) {
  3046. switch (e.getID()) {
  3047. case KeyEvent.KEY_PRESSED:
  3048. if (!keyIsPressed(e.getKeyCode())) {
  3049. registerKeyPressed(e.getKeyCode());
  3050. }
  3051. return true;
  3052. case KeyEvent.KEY_RELEASED:
  3053. // We are forced to process VK_PRINTSCREEN separately because
  3054. // the Windows doesn't generate the key pressed event for
  3055. // printscreen and it block the processing of key release
  3056. // event for printscreen.
  3057. if (keyIsPressed(e.getKeyCode()) || e.getKeyCode()==KeyEvent.VK_PRINTSCREEN) {
  3058. registerKeyReleased(e.getKeyCode());
  3059. return true;
  3060. }
  3061. return false;
  3062. case KeyEvent.KEY_TYPED:
  3063. return true;
  3064. default:
  3065. // Not a known KeyEvent type, bail.
  3066. return false;
  3067. }
  3068. }
  3069. }
  3070. /*
  3071. * --- Accessibility Support ---
  3072. */
  3073. /**
  3074. * @deprecated As of JDK version 1.1,
  3075. * replaced by <code>java.awt.Component.setEnabled(boolean)</code>.
  3076. */
  3077. @Deprecated
  3078. public void enable() {
  3079. if (isEnabled() != true) {
  3080. super.enable();
  3081. if (accessibleContext != null) {
  3082. accessibleContext.firePropertyChange(
  3083. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  3084. null, AccessibleState.ENABLED);
  3085. }
  3086. }
  3087. }
  3088. /**
  3089. * @deprecated As of JDK version 1.1,
  3090. * replaced by <code>java.awt.Component.setEnabled(boolean)</code>.
  3091. */
  3092. @Deprecated
  3093. public void disable() {
  3094. if (isEnabled() != false) {
  3095. super.disable();
  3096. if (accessibleContext != null) {
  3097. accessibleContext.firePropertyChange(
  3098. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  3099. AccessibleState.ENABLED, null);
  3100. }
  3101. }
  3102. }
  3103. /**
  3104. * The <code>AccessibleContext</code> associated with this
  3105. * <code>JComponent</code>.
  3106. */
  3107. protected AccessibleContext accessibleContext = null;
  3108. /**
  3109. * Returns the <code>AccessibleContext</code> associated with this
  3110. * <code>JComponent</code>. The method implemented by this base
  3111. * class returns null. Classes that extend <code>JComponent</code>
  3112. * should implement this method to return the
  3113. * <code>AccessibleContext</code> associated with the subclass.
  3114. *
  3115. * @return the <code>AccessibleContext</code> of this
  3116. * <code>JComponent</code>
  3117. */
  3118. public AccessibleContext getAccessibleContext() {
  3119. return accessibleContext;
  3120. }
  3121. /**
  3122. * Inner class of JComponent used to provide default support for
  3123. * accessibility. This class is not meant to be used directly by
  3124. * application developers, but is instead meant only to be
  3125. * subclassed by component developers.
  3126. * <p>
  3127. * <strong>Warning:</strong>
  3128. * Serialized objects of this class will not be compatible with
  3129. * future Swing releases. The current serialization support is
  3130. * appropriate for short term storage or RMI between applications running
  3131. * the same version of Swing. As of 1.4, support for long term storage
  3132. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  3133. * has been added to the <code>java.beans</code> package.
  3134. * Please see {@link java.beans.XMLEncoder}.
  3135. */
  3136. public abstract class AccessibleJComponent extends AccessibleAWTContainer
  3137. implements AccessibleExtendedComponent
  3138. {
  3139. /**
  3140. * Though the class is abstract, this should be called by
  3141. * all sub-classes.
  3142. */
  3143. protected AccessibleJComponent() {
  3144. super();
  3145. }
  3146. protected ContainerListener accessibleContainerHandler = null;
  3147. protected FocusListener accessibleFocusHandler = null;
  3148. /**
  3149. * Fire PropertyChange listener, if one is registered,
  3150. * when children added/removed.
  3151. */
  3152. protected class AccessibleContainerHandler
  3153. implements ContainerListener {
  3154. public void componentAdded(ContainerEvent e) {
  3155. Component c = e.getChild();
  3156. if (c != null && c instanceof Accessible) {
  3157. AccessibleJComponent.this.firePropertyChange(
  3158. AccessibleContext.ACCESSIBLE_CHILD_PROPERTY,
  3159. null, ((Accessible) c).getAccessibleContext());
  3160. }
  3161. }
  3162. public void componentRemoved(ContainerEvent e) {
  3163. Component c = e.getChild();
  3164. if (c != null && c instanceof Accessible) {
  3165. AccessibleJComponent.this.firePropertyChange(
  3166. AccessibleContext.ACCESSIBLE_CHILD_PROPERTY,
  3167. ((Accessible) c).getAccessibleContext(), null);
  3168. }
  3169. }
  3170. }
  3171. /**
  3172. * Fire PropertyChange listener, if one is registered,
  3173. * when focus events happen
  3174. */
  3175. protected class AccessibleFocusHandler implements FocusListener {
  3176. public void focusGained(FocusEvent event) {
  3177. if (accessibleContext != null) {
  3178. accessibleContext.firePropertyChange(
  3179. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  3180. null, AccessibleState.FOCUSED);
  3181. }
  3182. }
  3183. public void focusLost(FocusEvent event) {
  3184. if (accessibleContext != null) {
  3185. accessibleContext.firePropertyChange(
  3186. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  3187. AccessibleState.FOCUSED, null);
  3188. }
  3189. }
  3190. } // inner class AccessibleFocusHandler
  3191. /**
  3192. * Adds a PropertyChangeListener to the listener list.
  3193. *
  3194. * @param listener the PropertyChangeListener to be added
  3195. */
  3196. public void addPropertyChangeListener(PropertyChangeListener listener) {
  3197. if (accessibleFocusHandler == null) {
  3198. accessibleFocusHandler = new AccessibleFocusHandler();
  3199. JComponent.this.addFocusListener(accessibleFocusHandler);
  3200. }
  3201. if (accessibleContainerHandler == null) {
  3202. accessibleContainerHandler = new AccessibleContainerHandler();
  3203. JComponent.this.addContainerListener(accessibleContainerHandler);
  3204. }
  3205. super.addPropertyChangeListener(listener);
  3206. }
  3207. /**
  3208. * Removes a PropertyChangeListener from the listener list.
  3209. * This removes a PropertyChangeListener that was registered
  3210. * for all properties.
  3211. *
  3212. * @param listener the PropertyChangeListener to be removed
  3213. */
  3214. public void removePropertyChangeListener(PropertyChangeListener listener) {
  3215. if (accessibleFocusHandler != null) {
  3216. JComponent.this.removeFocusListener(accessibleFocusHandler);
  3217. accessibleFocusHandler = null;
  3218. }
  3219. super.removePropertyChangeListener(listener);
  3220. }
  3221. /**
  3222. * Recursively search through the border hierarchy (if it exists)
  3223. * for a TitledBorder with a non-null title. This does a depth
  3224. * first search on first the inside borders then the outside borders.
  3225. * The assumption is that titles make really pretty inside borders
  3226. * but not very pretty outside borders in compound border situations.
  3227. * It's rather arbitrary, but hopefully decent UI programmers will
  3228. * not create multiple titled borders for the same component.
  3229. */
  3230. protected String getBorderTitle(Border b) {
  3231. String s;
  3232. if (b instanceof TitledBorder) {
  3233. return ((TitledBorder) b).getTitle();
  3234. } else if (b instanceof CompoundBorder) {
  3235. s = getBorderTitle(((CompoundBorder) b).getInsideBorder());
  3236. if (s == null) {
  3237. s = getBorderTitle(((CompoundBorder) b).getOutsideBorder());
  3238. }
  3239. return s;
  3240. } else {
  3241. return null;
  3242. }
  3243. }
  3244. // AccessibleContext methods
  3245. //
  3246. /**
  3247. * Gets the accessible name of this object. This should almost never
  3248. * return java.awt.Component.getName(), as that generally isn't
  3249. * a localized name, and doesn't have meaning for the user. If the
  3250. * object is fundamentally a text object (such as a menu item), the
  3251. * accessible name should be the text of the object (for example,
  3252. * "save").
  3253. * If the object has a tooltip, the tooltip text may also be an
  3254. * appropriate String to return.
  3255. *
  3256. * @return the localized name of the object -- can be null if this
  3257. * object does not have a name
  3258. * @see AccessibleContext#setAccessibleName
  3259. */
  3260. public String getAccessibleName() {
  3261. String name = accessibleName;
  3262. // fallback to the titled border if it exists
  3263. //
  3264. if (name == null) {
  3265. name = getBorderTitle(getBorder());
  3266. }
  3267. // fallback to the label labeling us if it exists
  3268. //
  3269. if (name == null) {
  3270. Object o = getClientProperty(JLabel.LABELED_BY_PROPERTY);
  3271. if (o instanceof Accessible) {
  3272. AccessibleContext ac = ((Accessible) o).getAccessibleContext();
  3273. if (ac != null) {
  3274. name = ac.getAccessibleName();
  3275. }
  3276. }
  3277. }
  3278. return name;
  3279. }
  3280. /**
  3281. * Gets the accessible description of this object. This should be
  3282. * a concise, localized description of what this object is - what
  3283. * is its meaning to the user. If the object has a tooltip, the
  3284. * tooltip text may be an appropriate string to return, assuming
  3285. * it contains a concise description of the object (instead of just
  3286. * the name of the object - for example a "Save" icon on a toolbar that
  3287. * had "save" as the tooltip text shouldn't return the tooltip
  3288. * text as the description, but something like "Saves the current
  3289. * text document" instead).
  3290. *
  3291. * @return the localized description of the object -- can be null if
  3292. * this object does not have a description
  3293. * @see AccessibleContext#setAccessibleDescription
  3294. */
  3295. public String getAccessibleDescription() {
  3296. String description = accessibleDescription;
  3297. // fallback to the tool tip text if it exists
  3298. //
  3299. if (description == null) {
  3300. try {
  3301. description = getToolTipText();
  3302. } catch (Exception e) {
  3303. // Just in case the subclass overrode the
  3304. // getToolTipText method and actually
  3305. // requires a MouseEvent.
  3306. // [[[FIXME: WDW - we probably should require this
  3307. // method to take a MouseEvent and just pass it on
  3308. // to getToolTipText. The swing-feedback traffic
  3309. // leads me to believe getToolTipText might change,
  3310. // though, so I was hesitant to make this change at
  3311. // this time.]]]
  3312. }
  3313. }
  3314. // fallback to the label labeling us if it exists
  3315. //
  3316. if (description == null) {
  3317. Object o = getClientProperty(JLabel.LABELED_BY_PROPERTY);
  3318. if (o instanceof Accessible) {
  3319. AccessibleContext ac = ((Accessible) o).getAccessibleContext();
  3320. if (ac != null) {
  3321. description = ac.getAccessibleDescription();
  3322. }
  3323. }
  3324. }
  3325. return description;
  3326. }
  3327. /**
  3328. * Gets the role of this object.
  3329. *
  3330. * @return an instance of AccessibleRole describing the role of the
  3331. * object
  3332. * @see AccessibleRole
  3333. */
  3334. public AccessibleRole getAccessibleRole() {
  3335. return AccessibleRole.SWING_COMPONENT;
  3336. }
  3337. /**
  3338. * Gets the state of this object.
  3339. *
  3340. * @return an instance of AccessibleStateSet containing the current
  3341. * state set of the object
  3342. * @see AccessibleState
  3343. */
  3344. public AccessibleStateSet getAccessibleStateSet() {
  3345. AccessibleStateSet states = super.getAccessibleStateSet();
  3346. if (JComponent.this.isOpaque()) {
  3347. states.add(AccessibleState.OPAQUE);
  3348. }
  3349. return states;
  3350. }
  3351. /**
  3352. * Returns the number of accessible children in the object. If all
  3353. * of the children of this object implement Accessible, than this
  3354. * method should return the number of children of this object.
  3355. *
  3356. * @return the number of accessible children in the object.
  3357. */
  3358. public int getAccessibleChildrenCount() {
  3359. return super.getAccessibleChildrenCount();
  3360. }
  3361. /**
  3362. * Returns the nth Accessible child of the object.
  3363. *
  3364. * @param i zero-based index of child
  3365. * @return the nth Accessible child of the object
  3366. */
  3367. public Accessible getAccessibleChild(int i) {
  3368. return super.getAccessibleChild(i);
  3369. }
  3370. // ----- AccessibleExtendedComponent
  3371. /**
  3372. * Returns the AccessibleExtendedComponent
  3373. *
  3374. * @return the AccessibleExtendedComponent
  3375. */
  3376. AccessibleExtendedComponent getAccessibleExtendedComponent() {
  3377. return this;
  3378. }
  3379. /**
  3380. * Returns the tool tip text
  3381. *
  3382. * @return the tool tip text, if supported, of the object;
  3383. * otherwise, null
  3384. */
  3385. public String getToolTipText() {
  3386. return null;
  3387. }
  3388. /**
  3389. * Returns the titled border text
  3390. *
  3391. * @return the titled border text, if supported, of the object;
  3392. * otherwise, null
  3393. */
  3394. public String getTitledBorderText() {
  3395. Border border = JComponent.this.getBorder();
  3396. if (border instanceof TitledBorder) {
  3397. return ((TitledBorder)border).getTitle();
  3398. } else {
  3399. return null;
  3400. }
  3401. }
  3402. /**
  3403. * Returns key bindings associated with this object
  3404. *
  3405. * @return the key bindings, if supported, of the object;
  3406. * otherwise, null
  3407. * @see AccessibleKeyBinding
  3408. */
  3409. public AccessibleKeyBinding getAccessibleKeyBinding() {
  3410. return null;
  3411. }
  3412. } // inner class AccessibleJComponent
  3413. /**
  3414. * Returns an <code>ArrayTable</code> used for
  3415. * key/value "client properties" for this component. If the
  3416. * <code>clientProperties</code> table doesn't exist, an empty one
  3417. * will be created.
  3418. *
  3419. * @return an ArrayTable
  3420. * @see #putClientProperty
  3421. * @see #getClientProperty
  3422. */
  3423. private ArrayTable getClientProperties() {
  3424. if (clientProperties == null) {
  3425. clientProperties = new ArrayTable();
  3426. }
  3427. return clientProperties;
  3428. }
  3429. /**
  3430. * Returns the value of the property with the specified key. Only
  3431. * properties added with <code>putClientProperty</code> will return
  3432. * a non-<code>null</code> value.
  3433. *
  3434. * @param key the being queried
  3435. * @return the value of this property or <code>null</code>
  3436. * @see #putClientProperty
  3437. */
  3438. public final Object getClientProperty(Object key) {
  3439. if (key == SwingUtilities2.AA_TEXT_PROPERTY_KEY) {
  3440. return Boolean.valueOf(aaText);
  3441. }
  3442. if(clientProperties == null) {
  3443. return null;
  3444. } else {
  3445. synchronized(clientProperties) {
  3446. return clientProperties.get(key);
  3447. }
  3448. }
  3449. }
  3450. /**
  3451. * Adds an arbitrary key/value "client property" to this component.
  3452. * <p>
  3453. * The <code>get/putClientProperty</code> methods provide access to
  3454. * a small per-instance hashtable. Callers can use get/putClientProperty
  3455. * to annotate components that were created by another module.
  3456. * For example, a
  3457. * layout manager might store per child constraints this way. For example:
  3458. * <pre>
  3459. * componentA.putClientProperty("to the left of", componentB);
  3460. * </pre>
  3461. * If value is <code>null</code> this method will remove the property.
  3462. * Changes to client properties are reported with
  3463. * <code>PropertyChange</code> events.
  3464. * The name of the property (for the sake of PropertyChange
  3465. * events) is <code>key.toString()</code>.
  3466. * <p>
  3467. * The <code>clientProperty</code> dictionary is not intended to
  3468. * support large
  3469. * scale extensions to JComponent nor should be it considered an
  3470. * alternative to subclassing when designing a new component.
  3471. *
  3472. * @param key the new client property key
  3473. * @param value the new client property value; if <code>null</code>
  3474. * this method will remove the property
  3475. * @see #getClientProperty
  3476. * @see #addPropertyChangeListener
  3477. */
  3478. public final void putClientProperty(Object key, Object value) {
  3479. if (value == null && clientProperties == null) {
  3480. // Both the value and ArrayTable are null, implying we don't
  3481. // have to do anything.
  3482. return;
  3483. }
  3484. if (key == SwingUtilities2.AA_TEXT_PROPERTY_KEY) {
  3485. if (value instanceof Boolean) {
  3486. aaText = ((Boolean)value).booleanValue();
  3487. }
  3488. return;
  3489. }
  3490. ArrayTable clientProperties = getClientProperties();
  3491. Object oldValue;
  3492. synchronized(clientProperties) {
  3493. oldValue = clientProperties.get(key);
  3494. if (value != null) {
  3495. clientProperties.put(key, value);
  3496. } else if (oldValue != null) {
  3497. clientProperties.remove(key);
  3498. } else {
  3499. // old == new == null
  3500. return;
  3501. }
  3502. }
  3503. firePropertyChange(key.toString(), oldValue, value);
  3504. }
  3505. /*
  3506. * Sets the property with the specified name to the specified value if
  3507. * the property has not already been set by the client program.
  3508. * This method is used primarily to set UI defaults for properties
  3509. * with primitive types, where the values cannot be marked with
  3510. * UIResource.
  3511. * @see LookAndFeel#installProperty
  3512. * @param propertyName String containing the name of the property
  3513. * @param value Object containing the property value
  3514. */
  3515. void setUIProperty(String propertyName, Object value) {
  3516. if (propertyName == "opaque") {
  3517. if (!getFlag(OPAQUE_SET)) {
  3518. setOpaque(((Boolean)value).booleanValue());
  3519. setFlag(OPAQUE_SET, false);
  3520. }
  3521. } else if (propertyName == "autoscrolls") {
  3522. if (!getFlag(AUTOSCROLLS_SET)) {
  3523. setAutoscrolls(((Boolean)value).booleanValue());
  3524. setFlag(AUTOSCROLLS_SET, false);
  3525. }
  3526. } else if (propertyName == "focusTraversalKeysForward") {
  3527. if (!getFlag(FOCUS_TRAVERSAL_KEYS_FORWARD_SET)) {
  3528. super.setFocusTraversalKeys(KeyboardFocusManager.
  3529. FORWARD_TRAVERSAL_KEYS,
  3530. (Set)value);
  3531. }
  3532. } else if (propertyName == "focusTraversalKeysBackward") {
  3533. if (!getFlag(FOCUS_TRAVERSAL_KEYS_BACKWARD_SET)) {
  3534. super.setFocusTraversalKeys(KeyboardFocusManager.
  3535. BACKWARD_TRAVERSAL_KEYS,
  3536. (Set)value);
  3537. }
  3538. } else {
  3539. throw new IllegalArgumentException("property \""+
  3540. propertyName+ "\" cannot be set using this method");
  3541. }
  3542. }
  3543. /**
  3544. * Sets the focus traversal keys for a given traversal operation for this
  3545. * Component.
  3546. * Refer to
  3547. * {@link java.awt.Component#setFocusTraversalKeys}
  3548. * for a complete description of this method.
  3549. *
  3550. * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  3551. * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, or
  3552. * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS
  3553. * @param keystrokes the Set of AWTKeyStroke for the specified operation
  3554. * @see java.awt.KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
  3555. * @see java.awt.KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
  3556. * @see java.awt.KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
  3557. * @throws IllegalArgumentException if id is not one of
  3558. * KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  3559. * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, or
  3560. * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or if keystrokes
  3561. * contains null, or if any Object in keystrokes is not an
  3562. * AWTKeyStroke, or if any keystroke represents a KEY_TYPED event,
  3563. * or if any keystroke already maps to another focus traversal
  3564. * operation for this Component
  3565. * @since 1.5
  3566. * @beaninfo
  3567. * bound: true
  3568. */
  3569. public void
  3570. setFocusTraversalKeys(int id, Set<? extends AWTKeyStroke> keystrokes)
  3571. {
  3572. if (id == KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS) {
  3573. setFlag(FOCUS_TRAVERSAL_KEYS_FORWARD_SET,true);
  3574. } else if (id == KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS) {
  3575. setFlag(FOCUS_TRAVERSAL_KEYS_BACKWARD_SET,true);
  3576. }
  3577. super.setFocusTraversalKeys(id,keystrokes);
  3578. }
  3579. /* --- Transitional java.awt.Component Support ---
  3580. * The methods and fields in this section will migrate to
  3581. * java.awt.Component in the next JDK release.
  3582. */
  3583. /**
  3584. * Returns true if this component is lightweight, that is, if it doesn't
  3585. * have a native window system peer.
  3586. *
  3587. * @return true if this component is lightweight
  3588. */
  3589. public static boolean isLightweightComponent(Component c) {
  3590. return c.getPeer() instanceof LightweightPeer;
  3591. }
  3592. /**
  3593. * @deprecated As of JDK 5,
  3594. * replaced by <code>Component.setBounds(int, int, int, int)</code>.
  3595. * <p>
  3596. * Moves and resizes this component.
  3597. *
  3598. * @param x the new horizontal location
  3599. * @param y the new vertical location
  3600. * @param w the new width
  3601. * @param h the new height
  3602. * @see java.awt.Component#setBounds
  3603. */
  3604. @Deprecated
  3605. public void reshape(int x, int y, int w, int h) {
  3606. super.reshape(x, y, w, h);
  3607. }
  3608. /**
  3609. * Stores the bounds of this component into "return value"
  3610. * <code>rv</code> and returns <code>rv</code>.
  3611. * If <code>rv</code> is <code>null</code> a new <code>Rectangle</code>
  3612. * is allocated. This version of <code>getBounds</code> is useful
  3613. * if the caller wants to avoid allocating a new <code>Rectangle</code>
  3614. * object on the heap.
  3615. *
  3616. * @param rv the return value, modified to the component's bounds
  3617. * @return <code>rv</code> if <code>rv</code> is <code>null</code>
  3618. * return a newly created <code>Rectangle</code> with this
  3619. * component's bounds
  3620. */
  3621. public Rectangle getBounds(Rectangle rv) {
  3622. if (rv == null) {
  3623. return new Rectangle(getX(), getY(), getWidth(), getHeight());
  3624. }
  3625. else {
  3626. rv.setBounds(getX(), getY(), getWidth(), getHeight());
  3627. return rv;
  3628. }
  3629. }
  3630. /**
  3631. * Stores the width/height of this component into "return value"
  3632. * <code>rv</code> and returns <code>rv</code>.
  3633. * If <code>rv</code> is <code>null</code> a new <code>Dimension</code>
  3634. * object is allocated. This version of <code>getSize</code>
  3635. * is useful if the caller wants to avoid allocating a new
  3636. * <code>Dimension</code> object on the heap.
  3637. *
  3638. * @param rv the return value, modified to the component's size
  3639. * @return <code>rv</code>
  3640. */
  3641. public Dimension getSize(Dimension rv) {
  3642. if (rv == null) {
  3643. return new Dimension(getWidth(), getHeight());
  3644. }
  3645. else {
  3646. rv.setSize(getWidth(), getHeight());
  3647. return rv;
  3648. }
  3649. }
  3650. /**
  3651. * Stores the x,y origin of this component into "return value"
  3652. * <code>rv</code> and returns <code>rv</code>.
  3653. * If <code>rv</code> is <code>null</code> a new <code>Point</code>
  3654. * is allocated. This version of <code>getLocation</code> is useful
  3655. * if the caller wants to avoid allocating a new <code>Point</code>
  3656. * object on the heap.
  3657. *
  3658. * @param rv the return value, modified to the component's location
  3659. * @return <code>rv</code>
  3660. */
  3661. public Point getLocation(Point rv) {
  3662. if (rv == null) {
  3663. return new Point(getX(), getY());
  3664. }
  3665. else {
  3666. rv.setLocation(getX(), getY());
  3667. return rv;
  3668. }
  3669. }
  3670. /**
  3671. * Returns the current x coordinate of the component's origin.
  3672. * This method is preferable to writing
  3673. * <code>component.getBounds().x</code>, or
  3674. * <code>component.getLocation().x</code> because it doesn't cause any
  3675. * heap allocations.
  3676. *
  3677. * @return the current x coordinate of the component's origin
  3678. */
  3679. public int getX() { return super.getX(); }
  3680. /**
  3681. * Returns the current y coordinate of the component's origin.
  3682. * This method is preferable to writing
  3683. * <code>component.getBounds().y</code>, or
  3684. * <code>component.getLocation().y</code> because it doesn't cause any
  3685. * heap allocations.
  3686. *
  3687. * @return the current y coordinate of the component's origin
  3688. */
  3689. public int getY() { return super.getY(); }
  3690. /**
  3691. * Returns the current width of this component.
  3692. * This method is preferable to writing
  3693. * <code>component.getBounds().width</code>, or
  3694. * <code>component.getSize().width</code> because it doesn't cause any
  3695. * heap allocations.
  3696. *
  3697. * @return the current width of this component
  3698. */
  3699. public int getWidth() { return super.getWidth(); }
  3700. /**
  3701. * Returns the current height of this component.
  3702. * This method is preferable to writing
  3703. * <code>component.getBounds().height</code>, or
  3704. * <code>component.getSize().height</code> because it doesn't cause any
  3705. * heap allocations.
  3706. *
  3707. * @return the current height of this component
  3708. */
  3709. public int getHeight() { return super.getHeight(); }
  3710. /**
  3711. * Returns true if this component is completely opaque.
  3712. * <p>
  3713. * An opaque component paints every pixel within its
  3714. * rectangular bounds. A non-opaque component paints only a subset of
  3715. * its pixels or none at all, allowing the pixels underneath it to
  3716. * "show through". Therefore, a component that does not fully paint
  3717. * its pixels provides a degree of transparency.
  3718. * <p>
  3719. * Subclasses that guarantee to always completely paint their contents
  3720. * should override this method and return true.
  3721. *
  3722. * @return true if this component is completely opaque
  3723. * @see #setOpaque
  3724. */
  3725. public boolean isOpaque() {
  3726. return getFlag(IS_OPAQUE);
  3727. }
  3728. /**
  3729. * If true the component paints every pixel within its bounds.
  3730. * Otherwise, the component may not paint some or all of its
  3731. * pixels, allowing the underlying pixels to show through.
  3732. * <p>
  3733. * The default value of this property is false for <code>JComponent</code>.
  3734. * However, the default value for this property on most standard
  3735. * <code>JComponent</code> subclasses (such as <code>JButton</code> and
  3736. * <code>JTree</code>) is look-and-feel dependent.
  3737. *
  3738. * @param isOpaque true if this component should be opaque
  3739. * @see #isOpaque
  3740. * @beaninfo
  3741. * bound: true
  3742. * expert: true
  3743. * description: The component's opacity
  3744. */
  3745. public void setOpaque(boolean isOpaque) {
  3746. boolean oldValue = getFlag(IS_OPAQUE);
  3747. setFlag(IS_OPAQUE, isOpaque);
  3748. setFlag(OPAQUE_SET, true);
  3749. firePropertyChange("opaque", oldValue, isOpaque);
  3750. }
  3751. /**
  3752. * If the specified rectangle is completely obscured by any of this
  3753. * component's opaque children then returns true. Only direct children
  3754. * are considered, more distant descendants are ignored. A
  3755. * <code>JComponent</code> is opaque if
  3756. * <code>JComponent.isOpaque()</code> returns true, other lightweight
  3757. * components are always considered transparent, and heavyweight components
  3758. * are always considered opaque.
  3759. *
  3760. * @param x x value of specified rectangle
  3761. * @param y y value of specified rectangle
  3762. * @param width width of specified rectangle
  3763. * @param height height of specified rectangle
  3764. * @return true if the specified rectangle is obscured by an opaque child
  3765. */
  3766. boolean rectangleIsObscured(int x,int y,int width,int height)
  3767. {
  3768. int numChildren = getComponentCount();
  3769. for(int i = 0; i < numChildren; i++) {
  3770. Component child = getComponent(i);
  3771. int cx, cy, cw, ch;
  3772. cx = child.getX();
  3773. cy = child.getY();
  3774. cw = child.getWidth();
  3775. ch = child.getHeight();
  3776. if (x >= cx && (x + width) <= (cx + cw) &&
  3777. y >= cy && (y + height) <= (cy + ch) && child.isVisible()) {
  3778. if(child instanceof JComponent) {
  3779. // System.out.println("A) checking opaque: " + ((JComponent)child).isOpaque() + " " + child);
  3780. // System.out.print("B) ");
  3781. // Thread.dumpStack();
  3782. return ((JComponent)child).isOpaque();
  3783. } else {
  3784. /** Sometimes a heavy weight can have a bound larger than its peer size
  3785. * so we should always draw under heavy weights
  3786. */
  3787. return false;
  3788. }
  3789. }
  3790. }
  3791. return false;
  3792. }
  3793. /**
  3794. * Returns the <code>Component</code>'s "visible rect rectangle" - the
  3795. * intersection of the visible rectangles for the component <code>c</code>
  3796. * and all of its ancestors. The return value is stored in
  3797. * <code>visibleRect</code>.
  3798. *
  3799. * @param c the component
  3800. * @param visibleRect a <code>Rectangle</code> computed as the
  3801. * intersection of all visible rectangles for the component
  3802. * <code>c</code> and all of its ancestors -- this is the
  3803. * return value for this method
  3804. * @see #getVisibleRect
  3805. */
  3806. static final void computeVisibleRect(Component c, Rectangle visibleRect) {
  3807. Container p = c.getParent();
  3808. Rectangle bounds = c.getBounds();
  3809. if (p == null || p instanceof Window || p instanceof Applet) {
  3810. visibleRect.setBounds(0, 0, bounds.width, bounds.height);
  3811. } else {
  3812. computeVisibleRect(p, visibleRect);
  3813. visibleRect.x -= bounds.x;
  3814. visibleRect.y -= bounds.y;
  3815. SwingUtilities.computeIntersection(0,0,bounds.width,bounds.height,visibleRect);
  3816. }
  3817. }
  3818. /**
  3819. * Returns the <code>Component</code>'s "visible rect rectangle" - the
  3820. * intersection of the visible rectangles for this component
  3821. * and all of its ancestors. The return value is stored in
  3822. * <code>visibleRect</code>.
  3823. *
  3824. * @param visibleRect a <code>Rectangle</code> computed as the
  3825. * intersection of all visible rectangles for this
  3826. * component and all of its ancestors -- this is the return
  3827. * value for this method
  3828. * @see #getVisibleRect
  3829. */
  3830. public void computeVisibleRect(Rectangle visibleRect) {
  3831. computeVisibleRect(this, visibleRect);
  3832. }
  3833. /**
  3834. * Returns the <code>Component</code>'s "visible rectangle" - the
  3835. * intersection of this component's visible rectangle,
  3836. * <code>new Rectangle(0, 0, getWidth(), getHeight())</code>,
  3837. * and all of its ancestors' visible rectangles.
  3838. *
  3839. * @return the visible rectangle
  3840. */
  3841. public Rectangle getVisibleRect() {
  3842. Rectangle visibleRect = new Rectangle();
  3843. computeVisibleRect(visibleRect);
  3844. return visibleRect;
  3845. }
  3846. /**
  3847. * Support for reporting bound property changes for boolean properties.
  3848. * This method can be called when a bound property has changed and it will
  3849. * send the appropriate PropertyChangeEvent to any registered
  3850. * PropertyChangeListeners.
  3851. *
  3852. * @param propertyName the property whose value has changed
  3853. * @param oldValue the property's previous value
  3854. * @param newValue the property's new value
  3855. */
  3856. public void firePropertyChange(String propertyName,
  3857. boolean oldValue, boolean newValue) {
  3858. super.firePropertyChange(propertyName, oldValue, newValue);
  3859. }
  3860. /**
  3861. * Support for reporting bound property changes for integer properties.
  3862. * This method can be called when a bound property has changed and it will
  3863. * send the appropriate PropertyChangeEvent to any registered
  3864. * PropertyChangeListeners.
  3865. *
  3866. * @param propertyName the property whose value has changed
  3867. * @param oldValue the property's previous value
  3868. * @param newValue the property's new value
  3869. */
  3870. public void firePropertyChange(String propertyName,
  3871. int oldValue, int newValue) {
  3872. super.firePropertyChange(propertyName, oldValue, newValue);
  3873. }
  3874. // XXX This method is implemented as a workaround to a JLS issue with ambiguous
  3875. // methods. This should be removed once 4758654 is resolved.
  3876. public void firePropertyChange(String propertyName, char oldValue, char newValue) {
  3877. super.firePropertyChange(propertyName, oldValue, newValue);
  3878. }
  3879. /**
  3880. * Supports reporting constrained property changes.
  3881. * This method can be called when a constrained property has changed
  3882. * and it will send the appropriate <code>PropertyChangeEvent</code>
  3883. * to any registered <code>VetoableChangeListeners</code>.
  3884. *
  3885. * @param propertyName the name of the property that was listened on
  3886. * @param oldValue the old value of the property
  3887. * @param newValue the new value of the property
  3888. * @exception PropertyVetoException when the attempt to set the
  3889. * property is vetoed by the component
  3890. */
  3891. protected void fireVetoableChange(String propertyName, Object oldValue, Object newValue)
  3892. throws java.beans.PropertyVetoException
  3893. {
  3894. if (vetoableChangeSupport == null) {
  3895. return;
  3896. }
  3897. vetoableChangeSupport.fireVetoableChange(propertyName, oldValue, newValue);
  3898. }
  3899. /**
  3900. * Adds a <code>VetoableChangeListener</code> to the listener list.
  3901. * The listener is registered for all properties.
  3902. *
  3903. * @param listener the <code>VetoableChangeListener</code> to be added
  3904. */
  3905. public synchronized void addVetoableChangeListener(VetoableChangeListener listener) {
  3906. if (vetoableChangeSupport == null) {
  3907. vetoableChangeSupport = new java.beans.VetoableChangeSupport(this);
  3908. }
  3909. vetoableChangeSupport.addVetoableChangeListener(listener);
  3910. }
  3911. /**
  3912. * Removes a <code>VetoableChangeListener</code> from the listener list.
  3913. * This removes a <code>VetoableChangeListener</code> that was registered
  3914. * for all properties.
  3915. *
  3916. * @param listener the <code>VetoableChangeListener</code> to be removed
  3917. */
  3918. public synchronized void removeVetoableChangeListener(VetoableChangeListener listener) {
  3919. if (vetoableChangeSupport == null) {
  3920. return;
  3921. }
  3922. vetoableChangeSupport.removeVetoableChangeListener(listener);
  3923. }
  3924. /**
  3925. * Returns an array of all the vetoable change listeners
  3926. * registered on this component.
  3927. *
  3928. * @return all of the component's <code>VetoableChangeListener</code>s
  3929. * or an empty
  3930. * array if no vetoable change listeners are currently registered
  3931. *
  3932. * @see #addVetoableChangeListener
  3933. * @see #removeVetoableChangeListener
  3934. *
  3935. * @since 1.4
  3936. */
  3937. public synchronized VetoableChangeListener[] getVetoableChangeListeners() {
  3938. if (vetoableChangeSupport == null) {
  3939. return new VetoableChangeListener[0];
  3940. }
  3941. return vetoableChangeSupport.getVetoableChangeListeners();
  3942. }
  3943. /**
  3944. * Returns the top-level ancestor of this component (either the
  3945. * containing <code>Window</code> or <code>Applet</code>),
  3946. * or <code>null</code> if this component has not
  3947. * been added to any container.
  3948. *
  3949. * @return the top-level <code>Container</code> that this component is in,
  3950. * or <code>null</code> if not in any container
  3951. */
  3952. public Container getTopLevelAncestor() {
  3953. for(Container p = this; p != null; p = p.getParent()) {
  3954. if(p instanceof Window || p instanceof Applet) {
  3955. return p;
  3956. }
  3957. }
  3958. return null;
  3959. }
  3960. private AncestorNotifier getAncestorNotifier() {
  3961. return (AncestorNotifier)getClientProperty(ANCESTOR_NOTIFIER_KEY);
  3962. }
  3963. /**
  3964. * Registers <code>listener</code> so that it will receive
  3965. * <code>AncestorEvents</code> when it or any of its ancestors
  3966. * move or are made visible or invisible.
  3967. * Events are also sent when the component or its ancestors are added
  3968. * or removed from the containment hierarchy.
  3969. *
  3970. * @param listener the <code>AncestorListener</code> to register
  3971. * @see AncestorEvent
  3972. */
  3973. public void addAncestorListener(AncestorListener listener) {
  3974. AncestorNotifier ancestorNotifier = getAncestorNotifier();
  3975. if (ancestorNotifier == null) {
  3976. ancestorNotifier = new AncestorNotifier(this);
  3977. putClientProperty(ANCESTOR_NOTIFIER_KEY, ancestorNotifier);
  3978. }
  3979. ancestorNotifier.addAncestorListener(listener);
  3980. }
  3981. /**
  3982. * Unregisters <code>listener</code> so that it will no longer receive
  3983. * <code>AncestorEvents</code>.
  3984. *
  3985. * @param listener the <code>AncestorListener</code> to be removed
  3986. * @see #addAncestorListener
  3987. */
  3988. public void removeAncestorListener(AncestorListener listener) {
  3989. AncestorNotifier ancestorNotifier = getAncestorNotifier();
  3990. if (ancestorNotifier == null) {
  3991. return;
  3992. }
  3993. ancestorNotifier.removeAncestorListener(listener);
  3994. if (ancestorNotifier.listenerList.getListenerList().length == 0) {
  3995. ancestorNotifier.removeAllListeners();
  3996. putClientProperty(ANCESTOR_NOTIFIER_KEY, null);
  3997. }
  3998. }
  3999. /**
  4000. * Returns an array of all the ancestor listeners
  4001. * registered on this component.
  4002. *
  4003. * @return all of the component's <code>AncestorListener</code>s
  4004. * or an empty
  4005. * array if no ancestor listeners are currently registered
  4006. *
  4007. * @see #addAncestorListener
  4008. * @see #removeAncestorListener
  4009. *
  4010. * @since 1.4
  4011. */
  4012. public AncestorListener[] getAncestorListeners() {
  4013. AncestorNotifier ancestorNotifier = getAncestorNotifier();
  4014. if (ancestorNotifier == null) {
  4015. return new AncestorListener[0];
  4016. }
  4017. return ancestorNotifier.getAncestorListeners();
  4018. }
  4019. /**
  4020. * Returns an array of all the objects currently registered
  4021. * as <code><em>Foo</em>Listener</code>s
  4022. * upon this <code>JComponent</code>.
  4023. * <code><em>Foo</em>Listener</code>s are registered using the
  4024. * <code>add<em>Foo</em>Listener</code> method.
  4025. *
  4026. * <p>
  4027. *
  4028. * You can specify the <code>listenerType</code> argument
  4029. * with a class literal,
  4030. * such as
  4031. * <code><em>Foo</em>Listener.class</code>.
  4032. * For example, you can query a
  4033. * <code>JComponent</code> <code>c</code>
  4034. * for its mouse listeners with the following code:
  4035. * <pre>MouseListener[] mls = (MouseListener[])(c.getListeners(MouseListener.class));</pre>
  4036. * If no such listeners exist, this method returns an empty array.
  4037. *
  4038. * @param listenerType the type of listeners requested; this parameter
  4039. * should specify an interface that descends from
  4040. * <code>java.util.EventListener</code>
  4041. * @return an array of all objects registered as
  4042. * <code><em>Foo</em>Listener</code>s on this component,
  4043. * or an empty array if no such
  4044. * listeners have been added
  4045. * @exception ClassCastException if <code>listenerType</code>
  4046. * doesn't specify a class or interface that implements
  4047. * <code>java.util.EventListener</code>
  4048. *
  4049. * @since 1.3
  4050. *
  4051. * @see #getVetoableChangeListeners
  4052. * @see #getAncestorListeners
  4053. */
  4054. public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
  4055. T[] result;
  4056. if (listenerType == AncestorListener.class) {
  4057. // AncestorListeners are handled by the AncestorNotifier
  4058. result = (T[])getAncestorListeners();
  4059. }
  4060. else if (listenerType == VetoableChangeListener.class) {
  4061. // VetoableChangeListeners are handled by VetoableChangeSupport
  4062. result = (T[])getVetoableChangeListeners();
  4063. }
  4064. else if (listenerType == PropertyChangeListener.class) {
  4065. // PropertyChangeListeners are handled by PropertyChangeSupport
  4066. result = (T[])getPropertyChangeListeners();
  4067. }
  4068. else {
  4069. result = (T[])listenerList.getListeners(listenerType);
  4070. }
  4071. if (result.length == 0) {
  4072. return super.getListeners(listenerType);
  4073. }
  4074. return result;
  4075. }
  4076. /**
  4077. * Notifies this component that it now has a parent component.
  4078. * When this method is invoked, the chain of parent components is
  4079. * set up with <code>KeyboardAction</code> event listeners.
  4080. *
  4081. * @see #registerKeyboardAction
  4082. */
  4083. public void addNotify() {
  4084. super.addNotify();
  4085. firePropertyChange("ancestor", null, getParent());
  4086. registerWithKeyboardManager(false);
  4087. registerNextFocusableComponent();
  4088. }
  4089. /**
  4090. * Notifies this component that it no longer has a parent component.
  4091. * When this method is invoked, any <code>KeyboardAction</code>s
  4092. * set up in the the chain of parent components are removed.
  4093. *
  4094. * @see #registerKeyboardAction
  4095. */
  4096. public void removeNotify() {
  4097. super.removeNotify();
  4098. // This isn't strictly correct. The event shouldn't be
  4099. // fired until *after* the parent is set to null. But
  4100. // we only get notified before that happens
  4101. firePropertyChange("ancestor", getParent(), null);
  4102. unregisterWithKeyboardManager();
  4103. deregisterNextFocusableComponent();
  4104. if (getCreatedDoubleBuffer()) {
  4105. RepaintManager.currentManager(this).resetDoubleBuffer();
  4106. setCreatedDoubleBuffer(false);
  4107. }
  4108. if (autoscrolls) {
  4109. Autoscroller.stop(this);
  4110. }
  4111. }
  4112. /**
  4113. * Adds the specified region to the dirty region list if the component
  4114. * is showing. The component will be repainted after all of the
  4115. * currently pending events have been dispatched.
  4116. *
  4117. * @param tm this parameter is not used
  4118. * @param x the x value of the dirty region
  4119. * @param y the y value of the dirty region
  4120. * @param width the width of the dirty region
  4121. * @param height the height of the dirty region
  4122. * @see java.awt.Component#isShowing
  4123. * @see RepaintManager#addDirtyRegion
  4124. */
  4125. public void repaint(long tm, int x, int y, int width, int height) {
  4126. RepaintManager.currentManager(this).addDirtyRegion(this, x, y, width, height);
  4127. }
  4128. /**
  4129. * Adds the specified region to the dirty region list if the component
  4130. * is showing. The component will be repainted after all of the
  4131. * currently pending events have been dispatched.
  4132. *
  4133. * @param r a <code>Rectangle</code> containing the dirty region
  4134. * @see java.awt.Component#isShowing
  4135. * @see RepaintManager#addDirtyRegion
  4136. */
  4137. public void repaint(Rectangle r) {
  4138. repaint(0,r.x,r.y,r.width,r.height);
  4139. }
  4140. /**
  4141. * Supports deferred automatic layout.
  4142. * <p>
  4143. * Calls <code>invalidate</code> and then adds this component's
  4144. * <code>validateRoot</code> to a list of components that need to be
  4145. * validated. Validation will occur after all currently pending
  4146. * events have been dispatched. In other words after this method
  4147. * is called, the first validateRoot (if any) found when walking
  4148. * up the containment hierarchy of this component will be validated.
  4149. * By default, <code>JRootPane</code>, <code>JScrollPane</code>,
  4150. * and <code>JTextField</code> return true
  4151. * from <code>isValidateRoot</code>.
  4152. * <p>
  4153. * This method will automatically be called on this component
  4154. * when a property value changes such that size, location, or
  4155. * internal layout of this component has been affected. This automatic
  4156. * updating differs from the AWT because programs generally no
  4157. * longer need to invoke <code>validate</code> to get the contents of the
  4158. * GUI to update.
  4159. * <p>
  4160. *
  4161. * @see java.awt.Component#invalidate
  4162. * @see java.awt.Container#validate
  4163. * @see #isValidateRoot
  4164. * @see RepaintManager#addInvalidComponent
  4165. */
  4166. public void revalidate() {
  4167. if (getParent() == null) {
  4168. // Note: We don't bother invalidating here as once added
  4169. // to a valid parent invalidate will be invoked (addImpl
  4170. // invokes addNotify which will invoke invalidate on the
  4171. // new Component). Also, if we do add a check to isValid
  4172. // here it can potentially be called before the constructor
  4173. // which was causing some people grief.
  4174. return;
  4175. }
  4176. if (SwingUtilities.isEventDispatchThread()) {
  4177. invalidate();
  4178. RepaintManager.currentManager(this).addInvalidComponent(this);
  4179. }
  4180. else {
  4181. Runnable callRevalidate = new Runnable() {
  4182. public void run() {
  4183. revalidate();
  4184. }
  4185. };
  4186. SwingUtilities.invokeLater(callRevalidate);
  4187. }
  4188. }
  4189. /**
  4190. * If this method returns true, <code>revalidate</code> calls by
  4191. * descendants of this component will cause the entire tree
  4192. * beginning with this root to be validated.
  4193. * Returns false by default. <code>JScrollPane</code> overrides
  4194. * this method and returns true.
  4195. *
  4196. * @return always returns false
  4197. * @see #revalidate
  4198. * @see java.awt.Component#invalidate
  4199. * @see java.awt.Container#validate
  4200. */
  4201. public boolean isValidateRoot() {
  4202. return false;
  4203. }
  4204. /**
  4205. * Returns true if this component tiles its children -- that is, if
  4206. * it can guarantee that the children will not overlap. The
  4207. * repainting system is substantially more efficient in this
  4208. * common case. <code>JComponent</code> subclasses that can't make this
  4209. * guarantee, such as <code>JLayeredPane</code>,
  4210. * should override this method to return false.
  4211. *
  4212. * @return always returns true
  4213. */
  4214. public boolean isOptimizedDrawingEnabled() {
  4215. return true;
  4216. }
  4217. /**
  4218. * Returns true if a paint triggered on a child component should cause
  4219. * painting to originate from this Component, or one of its ancestors.
  4220. *
  4221. * @return true if painting should originate from this Component or
  4222. * one of its ancestors.
  4223. */
  4224. boolean isPaintingOrigin() {
  4225. return false;
  4226. }
  4227. /**
  4228. * Paints the specified region in this component and all of its
  4229. * descendants that overlap the region, immediately.
  4230. * <p>
  4231. * It's rarely necessary to call this method. In most cases it's
  4232. * more efficient to call repaint, which defers the actual painting
  4233. * and can collapse redundant requests into a single paint call.
  4234. * This method is useful if one needs to update the display while
  4235. * the current event is being dispatched.
  4236. *
  4237. * @param x the x value of the region to be painted
  4238. * @param y the y value of the region to be painted
  4239. * @param w the width of the region to be painted
  4240. * @param h the height of the region to be painted
  4241. * @see #repaint
  4242. */
  4243. public void paintImmediately(int x,int y,int w, int h) {
  4244. Component c = this;
  4245. Component parent;
  4246. if(!isShowing()) {
  4247. return;
  4248. }
  4249. while(!((JComponent)c).isOpaque()) {
  4250. parent = c.getParent();
  4251. if(parent != null) {
  4252. x += c.getX();
  4253. y += c.getY();
  4254. c = parent;
  4255. } else {
  4256. break;
  4257. }
  4258. if(!(c instanceof JComponent)) {
  4259. break;
  4260. }
  4261. }
  4262. if(c instanceof JComponent) {
  4263. ((JComponent)c)._paintImmediately(x,y,w,h);
  4264. } else {
  4265. c.repaint(x,y,w,h);
  4266. }
  4267. }
  4268. /**
  4269. * Paints the specified region now.
  4270. *
  4271. * @param r a <code>Rectangle</code> containing the region to be painted
  4272. */
  4273. public void paintImmediately(Rectangle r) {
  4274. paintImmediately(r.x,r.y,r.width,r.height);
  4275. }
  4276. /**
  4277. * Returns whether this component should be guaranteed to be on top.
  4278. * For example, it would make no sense for <code>Menu</code>s to pop up
  4279. * under another component, so they would always return true.
  4280. * Most components will want to return false, hence that is the default.
  4281. *
  4282. * @return always returns false
  4283. */
  4284. // package private
  4285. boolean alwaysOnTop() {
  4286. return false;
  4287. }
  4288. void setPaintingChild(Component paintingChild) {
  4289. this.paintingChild = paintingChild;
  4290. }
  4291. void _paintImmediately(int x, int y, int w, int h) {
  4292. Graphics g;
  4293. Container c;
  4294. Rectangle b;
  4295. int tmpX, tmpY, tmpWidth, tmpHeight;
  4296. int offsetX=0,offsetY=0;
  4297. boolean hasBuffer = false;
  4298. JComponent bufferedComponent = null;
  4299. JComponent paintingComponent = this;
  4300. RepaintManager repaintManager = RepaintManager.currentManager(this);
  4301. // parent Container's up to Window or Applet. First container is
  4302. // the direct parent. Note that in testing it was faster to
  4303. // alloc a new Vector vs keeping a stack of them around, and gc
  4304. // seemed to have a minimal effect on this.
  4305. Vector path = new Vector(7);
  4306. int pIndex = -1;
  4307. int pCount = 0;
  4308. tmpX = tmpY = tmpWidth = tmpHeight = 0;
  4309. Rectangle paintImmediatelyClip = fetchRectangle();
  4310. paintImmediatelyClip.x = x;
  4311. paintImmediatelyClip.y = y;
  4312. paintImmediatelyClip.width = w;
  4313. paintImmediatelyClip.height = h;
  4314. // System.out.println("1) ************* in _paintImmediately for " + this);
  4315. boolean ontop = alwaysOnTop() && isOpaque();
  4316. Component child;
  4317. for (c = this, child = null;
  4318. c != null && !(c instanceof Window) && !(c instanceof Applet);
  4319. child = c, c = c.getParent()) {
  4320. JComponent jc = (c instanceof JComponent) ? (JComponent)c :
  4321. null;
  4322. path.addElement(c);
  4323. if(!ontop && jc != null && !jc.isOptimizedDrawingEnabled()) {
  4324. boolean resetPC;
  4325. // Children of c may overlap, three possible cases for the
  4326. // painting region:
  4327. // . Completely obscured by an opaque sibling, in which
  4328. // case there is no need to paint.
  4329. // . Partially obscured by a sibling: need to start
  4330. // painting from c.
  4331. // . Otherwise we aren't obscured and thus don't need to
  4332. // start painting from parent.
  4333. if (c != this) {
  4334. if (jc.isPaintingOrigin()) {
  4335. resetPC = true;
  4336. }
  4337. else {
  4338. Component[] children = c.getComponents();
  4339. int i = 0;
  4340. for (; i<children.length; i++) {
  4341. if (children[i] == child) break;
  4342. }
  4343. switch (jc.getObscuredState(i,
  4344. paintImmediatelyClip.x,
  4345. paintImmediatelyClip.y,
  4346. paintImmediatelyClip.width,
  4347. paintImmediatelyClip.height)) {
  4348. case NOT_OBSCURED:
  4349. resetPC = false;
  4350. break;
  4351. case COMPLETELY_OBSCURED:
  4352. recycleRectangle(paintImmediatelyClip);
  4353. return;
  4354. default:
  4355. resetPC = true;
  4356. break;
  4357. }
  4358. }
  4359. }
  4360. else {
  4361. resetPC = false;
  4362. }
  4363. if (resetPC) {
  4364. // Get rid of any buffer since we draw from here and
  4365. // we might draw something larger
  4366. paintingComponent = jc;
  4367. pIndex = pCount;
  4368. offsetX = offsetY = 0;
  4369. hasBuffer = false;
  4370. }
  4371. }
  4372. pCount++;
  4373. // look to see if the parent (and therefor this component)
  4374. // is double buffered
  4375. if(repaintManager.isDoubleBufferingEnabled() && jc != null &&
  4376. jc.isDoubleBuffered()) {
  4377. hasBuffer = true;
  4378. bufferedComponent = jc;
  4379. }
  4380. // if we aren't on top, include the parent's clip
  4381. if (!ontop) {
  4382. int bx = c.getX();
  4383. int by = c.getY();
  4384. tmpWidth = c.getWidth();
  4385. tmpHeight = c.getHeight();
  4386. SwingUtilities.computeIntersection(tmpX,tmpY,tmpWidth,tmpHeight,paintImmediatelyClip);
  4387. paintImmediatelyClip.x += bx;
  4388. paintImmediatelyClip.y += by;
  4389. offsetX += bx;
  4390. offsetY += by;
  4391. }
  4392. }
  4393. // If the clip width or height is negative, don't bother painting
  4394. if(c == null || c.getPeer() == null ||
  4395. paintImmediatelyClip.width <= 0 ||
  4396. paintImmediatelyClip.height <= 0) {
  4397. recycleRectangle(paintImmediatelyClip);
  4398. return;
  4399. }
  4400. paintingComponent.setFlag(IS_REPAINTING, true);
  4401. paintImmediatelyClip.x -= offsetX;
  4402. paintImmediatelyClip.y -= offsetY;
  4403. // Notify the Components that are going to be painted of the
  4404. // child component to paint to.
  4405. if(paintingComponent != this) {
  4406. Component comp;
  4407. int i = pIndex;
  4408. for(; i > 0 ; i--) {
  4409. comp = (Component) path.elementAt(i);
  4410. if(comp instanceof JComponent) {
  4411. ((JComponent)comp).setPaintingChild
  4412. ((Component)path.elementAt(i-1));
  4413. }
  4414. }
  4415. }
  4416. try {
  4417. try {
  4418. Graphics pcg = paintingComponent.getGraphics();
  4419. g = (pcg == null) ? null : pcg.create();
  4420. pcg.dispose();
  4421. } catch(NullPointerException e) {
  4422. g = null;
  4423. e.printStackTrace();
  4424. }
  4425. if (g == null) {
  4426. System.err.println("In paintImmediately null graphics");
  4427. return;
  4428. }
  4429. try {
  4430. boolean paintCompleted = false;
  4431. if (hasBuffer) {
  4432. paintCompleted = paintDoubleBuffered(paintingComponent, bufferedComponent, g,
  4433. paintImmediatelyClip.x,
  4434. paintImmediatelyClip.y,
  4435. paintImmediatelyClip.width,
  4436. paintImmediatelyClip.height);
  4437. }
  4438. if (!paintCompleted) {
  4439. //System.out.println("has no buffer");
  4440. g.setClip(paintImmediatelyClip.x,paintImmediatelyClip.y,
  4441. paintImmediatelyClip.width,paintImmediatelyClip.height);
  4442. paintingComponent.paint(g);
  4443. }
  4444. } finally {
  4445. g.dispose();
  4446. }
  4447. }
  4448. finally {
  4449. // Reset the painting child for the parent components.
  4450. if(paintingComponent != this) {
  4451. Component comp;
  4452. int i = pIndex;
  4453. for(; i > 0 ; i--) {
  4454. comp = (Component) path.elementAt(i);
  4455. if(comp instanceof JComponent) {
  4456. ((JComponent)comp).setPaintingChild(null);
  4457. }
  4458. }
  4459. }
  4460. path.removeAllElements();
  4461. paintingComponent.setFlag(IS_REPAINTING, false);
  4462. }
  4463. recycleRectangle(paintImmediatelyClip);
  4464. }
  4465. private boolean paintDoubleBuffered(JComponent paintingComponent, Component bufferComponent,
  4466. Graphics g, int clipX, int clipY, int clipW, int clipH) {
  4467. RepaintManager repaintManager = RepaintManager.currentManager(paintingComponent);
  4468. boolean paintCompleted = false;
  4469. Image offscreen = null;
  4470. // First attempt to use VolatileImage buffer for performance. If this fails
  4471. // (which should rarely occur), fallback to a standard Image buffer.
  4472. //
  4473. if (repaintManager.useVolatileDoubleBuffer() &&
  4474. (offscreen = repaintManager.getVolatileOffscreenBuffer(
  4475. bufferComponent,clipW,clipH)) != null &&
  4476. (offscreen.getWidth(null) > 0 && offscreen.getHeight(null) > 0)) {
  4477. VolatileImage vImage = (java.awt.image.VolatileImage)offscreen;
  4478. GraphicsConfiguration gc = bufferComponent.
  4479. getGraphicsConfiguration();
  4480. for (int i = 0; !paintCompleted && i < RepaintManager.VOLATILE_LOOP_MAX; i++) {
  4481. if (vImage.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE) {
  4482. repaintManager.resetVolatileDoubleBuffer(gc);
  4483. offscreen = repaintManager.getVolatileOffscreenBuffer(bufferComponent,clipW, clipH);
  4484. vImage = (java.awt.image.VolatileImage)offscreen;
  4485. }
  4486. paintWithOffscreenBuffer(paintingComponent, g, clipX, clipY, clipW, clipH, offscreen);
  4487. paintCompleted = !vImage.contentsLost();
  4488. }
  4489. }
  4490. if (!paintCompleted) {
  4491. // VolatileImage painting loop failed, fallback to regular offscreen buffer
  4492. if ((offscreen =
  4493. repaintManager.getOffscreenBuffer(bufferComponent, clipW, clipH)) != null &&
  4494. (offscreen.getWidth(null) > 0 && offscreen.getHeight(null) > 0)) {
  4495. paintWithOffscreenBuffer(paintingComponent, g, clipX, clipY, clipW, clipH, offscreen);
  4496. paintCompleted = true;
  4497. }
  4498. }
  4499. return paintCompleted;
  4500. }
  4501. private void paintWithOffscreenBuffer(JComponent paintingComponent, Graphics g,
  4502. int clipX, int clipY, int clipW, int clipH,
  4503. Image offscreen) {
  4504. Graphics og = offscreen.getGraphics();
  4505. Graphics osg = (og == null) ? null : og.create();
  4506. og.dispose();
  4507. int bw = offscreen.getWidth(null);
  4508. int bh = offscreen.getHeight(null);
  4509. int x,y,maxx,maxy;
  4510. if (bw > clipW) {
  4511. bw = clipW;
  4512. }
  4513. if (bh > clipH) {
  4514. bh = clipH;
  4515. }
  4516. try {
  4517. paintingComponent.setFlag(ANCESTOR_USING_BUFFER,true);
  4518. paintingComponent.setFlag(IS_PAINTING_TILE,true);
  4519. for(x = clipX, maxx = clipX+clipW; x < maxx ; x += bw ) {
  4520. for(y=clipY, maxy = clipY + clipH; y < maxy ; y += bh) {
  4521. if ((y+bh) >= maxy && (x+bw) >= maxx) {
  4522. paintingComponent.setFlag(IS_PAINTING_TILE,false);
  4523. }
  4524. osg.translate(-x,-y);
  4525. osg.setClip(x,y,bw,bh);
  4526. if (paintingComponent.getFlag(IS_REPAINTING)) {
  4527. // Called from paintImmediately (RepaintManager) to fill
  4528. // repaint request
  4529. paintingComponent.paint(osg);
  4530. } else {
  4531. // Called from paint() (AWT) to repair damage
  4532. if(!paintingComponent.rectangleIsObscured(clipX,clipY,bw,bh)) {
  4533. paintingComponent.paintComponent(osg);
  4534. paintingComponent.paintBorder(osg);
  4535. }
  4536. paintingComponent.paintChildren(osg);
  4537. }
  4538. g.setClip(x,y,bw,bh);
  4539. g.drawImage(offscreen,x,y,paintingComponent);
  4540. osg.translate(x,y);
  4541. }
  4542. }
  4543. } finally {
  4544. paintingComponent.setFlag(ANCESTOR_USING_BUFFER,false);
  4545. paintingComponent.setFlag(IS_PAINTING_TILE,false);
  4546. osg.dispose();
  4547. }
  4548. }
  4549. /**
  4550. * Returns whether or not the region of the specified component is
  4551. * obscured by a sibling.
  4552. *
  4553. * @return NOT_OBSCURED if non of the siblings above the Component obscure
  4554. * it, COMPLETELY_OBSCURED if one of the siblings completely
  4555. * obscures the Component or PARTIALLY_OBSCURED if the Comonent is
  4556. * only partially obscured.
  4557. */
  4558. private int getObscuredState(int compIndex, int x, int y, int width,
  4559. int height) {
  4560. int retValue = NOT_OBSCURED;
  4561. Rectangle tmpRect = fetchRectangle();
  4562. for (int i = compIndex - 1 ; i >= 0 ; i--) {
  4563. Component sibling = getComponent(i);
  4564. if (!sibling.isVisible()) {
  4565. continue;
  4566. }
  4567. Rectangle siblingRect;
  4568. boolean opaque;
  4569. if (sibling instanceof JComponent) {
  4570. opaque = ((JComponent)sibling).isOpaque();
  4571. if (!opaque) {
  4572. if (retValue == PARTIALLY_OBSCURED) {
  4573. continue;
  4574. }
  4575. }
  4576. }
  4577. else {
  4578. opaque = true;
  4579. }
  4580. siblingRect = sibling.getBounds(tmpRect);
  4581. if (opaque && x >= siblingRect.x && (x + width) <=
  4582. (siblingRect.x + siblingRect.width) &&
  4583. y >= siblingRect.y && (y + height) <=
  4584. (siblingRect.y + siblingRect.height)) {
  4585. recycleRectangle(tmpRect);
  4586. return COMPLETELY_OBSCURED;
  4587. }
  4588. else if (retValue == NOT_OBSCURED &&
  4589. !((x + width <= siblingRect.x) ||
  4590. (y + height <= siblingRect.y) ||
  4591. (x >= siblingRect.x + siblingRect.width) ||
  4592. (y >= siblingRect.y + siblingRect.height))) {
  4593. retValue = PARTIALLY_OBSCURED;
  4594. }
  4595. }
  4596. recycleRectangle(tmpRect);
  4597. return retValue;
  4598. }
  4599. /**
  4600. * Returns true, which implies that before checking if a child should
  4601. * be painted it is first check that the child is not obscured by another
  4602. * sibling. This is only checked if <code>isOptimizedDrawingEnabled</code>
  4603. * returns false.
  4604. *
  4605. * @return always returns true
  4606. */
  4607. boolean checkIfChildObscuredBySibling() {
  4608. return true;
  4609. }
  4610. private void setFlag(int aFlag, boolean aValue) {
  4611. if(aValue) {
  4612. flags |= (1 << aFlag);
  4613. } else {
  4614. flags &= ~(1 << aFlag);
  4615. }
  4616. }
  4617. private boolean getFlag(int aFlag) {
  4618. int mask = (1 << aFlag);
  4619. return ((flags & mask) == mask);
  4620. }
  4621. // These functions must be static so that they can be called from
  4622. // subclasses inside the package, but whose inheritance hierarhcy includes
  4623. // classes outside of the package below JComponent (e.g., JTextArea).
  4624. static void setWriteObjCounter(JComponent comp, byte count) {
  4625. comp.flags = (comp.flags & ~(0xFF << WRITE_OBJ_COUNTER_FIRST)) |
  4626. (count << WRITE_OBJ_COUNTER_FIRST);
  4627. }
  4628. static byte getWriteObjCounter(JComponent comp) {
  4629. return (byte)((comp.flags >> WRITE_OBJ_COUNTER_FIRST) & 0xFF);
  4630. }
  4631. /** Buffering **/
  4632. /**
  4633. * Sets whether the this component should use a buffer to paint.
  4634. * If set to true, all the drawing from this component will be done
  4635. * in an offscreen painting buffer. The offscreen painting buffer will
  4636. * the be copied onto the screen.
  4637. * Swings painting system always uses a maximum of one double buffer.
  4638. * If a <code>Component</code> is buffered and one of its ancestor
  4639. * is also buffered, the ancestor buffer will be used.
  4640. *
  4641. * @param aFlag if true, set this component to be double buffered
  4642. */
  4643. public void setDoubleBuffered(boolean aFlag) {
  4644. setFlag(IS_DOUBLE_BUFFERED,aFlag);
  4645. }
  4646. /**
  4647. * Returns whether this component should use a buffer to paint.
  4648. *
  4649. * @return true if this component is double buffered, otherwise false
  4650. */
  4651. public boolean isDoubleBuffered() {
  4652. return getFlag(IS_DOUBLE_BUFFERED);
  4653. }
  4654. /**
  4655. * Returns the <code>JRootPane</code> ancestor for this component.
  4656. *
  4657. * @return the <code>JRootPane</code> that contains this component,
  4658. * or <code>null</code> if no <code>JRootPane</code> is found
  4659. */
  4660. public JRootPane getRootPane() {
  4661. return SwingUtilities.getRootPane(this);
  4662. }
  4663. /** Serialization **/
  4664. /**
  4665. * This is called from Component by way of reflection. Do NOT change
  4666. * the name unless you change the code in Component as well.
  4667. */
  4668. void compWriteObjectNotify() {
  4669. byte count = JComponent.getWriteObjCounter(this);
  4670. JComponent.setWriteObjCounter(this, (byte)(count + 1));
  4671. if (count != 0) {
  4672. return;
  4673. }
  4674. if (ui != null) {
  4675. ui.uninstallUI(this);
  4676. }
  4677. /* JTableHeader is in a separate package, which prevents it from
  4678. * being able to override this package-private method the way the
  4679. * other components can. We don't want to make this method protected
  4680. * because it would introduce public-api for a less-than-desirable
  4681. * serialization scheme, so we compromise with this 'instanceof' hack
  4682. * for now.
  4683. */
  4684. if (getToolTipText() != null ||
  4685. this instanceof javax.swing.table.JTableHeader) {
  4686. ToolTipManager.sharedInstance().unregisterComponent(JComponent.this);
  4687. }
  4688. }
  4689. /**
  4690. * This object is the <code>ObjectInputStream</code> callback
  4691. * that's called after a complete graph of objects (including at least
  4692. * one <code>JComponent</code>) has been read.
  4693. * It sets the UI property of each Swing component
  4694. * that was read to the current default with <code>updateUI</code>.
  4695. * <p>
  4696. * As each component is read in we keep track of the current set of
  4697. * root components here, in the roots vector. Note that there's only one
  4698. * <code>ReadObjectCallback</code> per <code>ObjectInputStream</code>,
  4699. * they're stored in the static <code>readObjectCallbacks</code>
  4700. * hashtable.
  4701. *
  4702. * @see java.io.ObjectInputStream#registerValidation
  4703. * @see SwingUtilities#updateComponentTreeUI
  4704. */
  4705. private class ReadObjectCallback implements ObjectInputValidation
  4706. {
  4707. private final Vector roots = new Vector(1);
  4708. private final ObjectInputStream inputStream;
  4709. ReadObjectCallback(ObjectInputStream s) throws Exception {
  4710. inputStream = s;
  4711. s.registerValidation(this, 0);
  4712. }
  4713. /**
  4714. * This is the method that's called after the entire graph
  4715. * of objects has been read in. It initializes
  4716. * the UI property of all of the copmonents with
  4717. * <code>SwingUtilities.updateComponentTreeUI</code>.
  4718. */
  4719. public void validateObject() throws InvalidObjectException {
  4720. try {
  4721. for(int i = 0; i < roots.size(); i++) {
  4722. JComponent root = (JComponent)(roots.elementAt(i));
  4723. SwingUtilities.updateComponentTreeUI(root);
  4724. }
  4725. }
  4726. finally {
  4727. readObjectCallbacks.remove(inputStream);
  4728. }
  4729. }
  4730. /**
  4731. * If <code>c</code> isn't a descendant of a component we've already
  4732. * seen, then add it to the roots <code>Vector</code>.
  4733. *
  4734. * @param c the <code>JComponent</code> to add
  4735. */
  4736. private void registerComponent(JComponent c)
  4737. {
  4738. /* If the Component c is a descendant of one of the
  4739. * existing roots (or it IS an existing root), we're done.
  4740. */
  4741. for(int i = 0; i < roots.size(); i++) {
  4742. JComponent root = (JComponent)roots.elementAt(i);
  4743. for(Component p = c; p != null; p = p.getParent()) {
  4744. if (p == root) {
  4745. return;
  4746. }
  4747. }
  4748. }
  4749. /* Otherwise: if Component c is an ancestor of any of the
  4750. * existing roots then remove them and add c (the "new root")
  4751. * to the roots vector.
  4752. */
  4753. for(int i = 0; i < roots.size(); i++) {
  4754. JComponent root = (JComponent)roots.elementAt(i);
  4755. for(Component p = root.getParent(); p != null; p = p.getParent()) {
  4756. if (p == c) {
  4757. roots.removeElementAt(i--); // !!
  4758. break;
  4759. }
  4760. }
  4761. }
  4762. roots.addElement(c);
  4763. }
  4764. }
  4765. /**
  4766. * We use the <code>ObjectInputStream</code> "registerValidation"
  4767. * callback to update the UI for the entire tree of components
  4768. * after they've all been read in.
  4769. *
  4770. * @param s the <code>ObjectInputStream</code> from which to read
  4771. */
  4772. private void readObject(ObjectInputStream s)
  4773. throws IOException, ClassNotFoundException
  4774. {
  4775. s.defaultReadObject();
  4776. /* If there's no ReadObjectCallback for this stream yet, that is, if
  4777. * this is the first call to JComponent.readObject() for this
  4778. * graph of objects, then create a callback and stash it
  4779. * in the readObjectCallbacks table. Note that the ReadObjectCallback
  4780. * constructor takes care of calling s.registerValidation().
  4781. */
  4782. ReadObjectCallback cb = (ReadObjectCallback)(readObjectCallbacks.get(s));
  4783. if (cb == null) {
  4784. try {
  4785. readObjectCallbacks.put(s, cb = new ReadObjectCallback(s));
  4786. }
  4787. catch (Exception e) {
  4788. throw new IOException(e.toString());
  4789. }
  4790. }
  4791. cb.registerComponent(this);
  4792. if (getToolTipText() != null) {
  4793. ToolTipManager.sharedInstance().registerComponent(this);
  4794. }
  4795. // Read back the client properties.
  4796. int cpCount = s.readInt();
  4797. if (cpCount > 0) {
  4798. clientProperties = new ArrayTable();
  4799. for (int counter = 0; counter < cpCount; counter++) {
  4800. clientProperties.put(s.readObject(),
  4801. s.readObject());
  4802. }
  4803. }
  4804. setWriteObjCounter(this, (byte)0);
  4805. }
  4806. /**
  4807. * Before writing a <code>JComponent</code> to an
  4808. * <code>ObjectOutputStream</code> we temporarily uninstall its UI.
  4809. * This is tricky to do because we want to uninstall
  4810. * the UI before any of the <code>JComponent</code>'s children
  4811. * (or its <code>LayoutManager</code> etc.) are written,
  4812. * and we don't want to restore the UI until the most derived
  4813. * <code>JComponent</code> subclass has been been stored.
  4814. *
  4815. * @param s the <code>ObjectOutputStream</code> in which to write
  4816. */
  4817. private void writeObject(ObjectOutputStream s) throws IOException {
  4818. s.defaultWriteObject();
  4819. if (getUIClassID().equals(uiClassID)) {
  4820. byte count = JComponent.getWriteObjCounter(this);
  4821. JComponent.setWriteObjCounter(this, --count);
  4822. if (count == 0 && ui != null) {
  4823. ui.installUI(this);
  4824. }
  4825. }
  4826. ArrayTable.writeArrayTable(s, clientProperties);
  4827. }
  4828. /**
  4829. * Returns a string representation of this <code>JComponent</code>.
  4830. * This method
  4831. * is intended to be used only for debugging purposes, and the
  4832. * content and format of the returned string may vary between
  4833. * implementations. The returned string may be empty but may not
  4834. * be <code>null</code>.
  4835. *
  4836. * @return a string representation of this <code>JComponent</code>
  4837. */
  4838. protected String paramString() {
  4839. String preferredSizeString = (isPreferredSizeSet() ?
  4840. getPreferredSize().toString() : "");
  4841. String minimumSizeString = (isMinimumSizeSet() ?
  4842. getMinimumSize().toString() : "");
  4843. String maximumSizeString = (isMaximumSizeSet() ?
  4844. getMaximumSize().toString() : "");
  4845. String borderString = (border != null ?
  4846. border.toString() : "");
  4847. return super.paramString() +
  4848. ",alignmentX=" + alignmentX +
  4849. ",alignmentY=" + alignmentY +
  4850. ",border=" + borderString +
  4851. ",flags=" + flags + // should beef this up a bit
  4852. ",maximumSize=" + maximumSizeString +
  4853. ",minimumSize=" + minimumSizeString +
  4854. ",preferredSize=" + preferredSizeString;
  4855. }
  4856. }