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