1 /* 2 * Copyright (c) 1997, 2010, 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 import java.awt.Component; 28 import java.awt.Font; 29 import java.awt.Color; 30 import java.awt.Insets; 31 import java.awt.Dimension; 32 import java.awt.KeyboardFocusManager; 33 import java.awt.KeyEventPostProcessor; 34 import java.awt.Toolkit; 35 36 import java.awt.event.KeyEvent; 37 38 import java.security.AccessController; 39 40 import javax.swing.plaf.ComponentUI; 41 import javax.swing.border.Border; 42 43 import javax.swing.event.SwingPropertyChangeSupport; 44 import java.beans.PropertyChangeListener; 45 46 import java.io.Serializable; 47 import java.io.File; 48 import java.io.FileInputStream; 49 50 import java.util.ArrayList; 51 import java.util.Properties; 52 import java.util.StringTokenizer; 53 import java.util.Vector; 54 import java.util.Locale; 55 56 import sun.awt.SunToolkit; 57 import sun.awt.OSInfo; 58 import sun.security.action.GetPropertyAction; 59 import sun.swing.SwingUtilities2; 60 import java.lang.reflect.Method; 61 62 63 /** 64 * {@code UIManager} manages the current look and feel, the set of 65 * available look and feels, {@code PropertyChangeListeners} that 66 * are notified when the look and feel changes, look and feel defaults, and 67 * convenience methods for obtaining various default values. 68 * 69 * <h3>Specifying the look and feel</h3> 70 * 71 * The look and feel can be specified in two distinct ways: by 72 * specifying the fully qualified name of the class for the look and 73 * feel, or by creating an instance of {@code LookAndFeel} and passing 74 * it to {@code setLookAndFeel}. The following example illustrates 75 * setting the look and feel to the system look and feel: 76 * <pre> 77 * UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 78 * </pre> 79 * The following example illustrates setting the look and feel based on 80 * class name: 81 * <pre> 82 * UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 83 * </pre> 84 * Once the look and feel has been changed it is imperative to invoke 85 * {@code updateUI} on all {@code JComponents}. The method {@link 86 * SwingUtilities#updateComponentTreeUI} makes it easy to apply {@code 87 * updateUI} to a containment hierarchy. Refer to it for 88 * details. The exact behavior of not invoking {@code 89 * updateUI} after changing the look and feel is 90 * unspecified. It is very possible to receive unexpected exceptions, 91 * painting problems, or worse. 92 * 93 * <h3>Default look and feel</h3> 94 * 95 * The class used for the default look and feel is chosen in the following 96 * manner: 97 * <ol> 98 * <li>If the system property <code>swing.defaultlaf</code> is 99 * {@code non-null}, use its value as the default look and feel class 100 * name. 101 * <li>If the {@link java.util.Properties} file <code>swing.properties</code> 102 * exists and contains the key <code>swing.defaultlaf</code>, 103 * use its value as the default look and feel class name. The location 104 * that is checked for <code>swing.properties</code> may vary depending 105 * upon the implementation of the Java platform. In Sun's implementation 106 * the location is <code>${java.home}/lib/swing.properties</code>. 107 * Refer to the release notes of the implementation being used for 108 * further details. 109 * <li>Otherwise use the cross platform look and feel. 110 * </ol> 111 * 112 * <h3>Defaults</h3> 113 * 114 * {@code UIManager} manages three sets of {@code UIDefaults}. In order, they 115 * are: 116 * <ol> 117 * <li>Developer defaults. With few exceptions Swing does not 118 * alter the developer defaults; these are intended to be modified 119 * and used by the developer. 120 * <li>Look and feel defaults. The look and feel defaults are 121 * supplied by the look and feel at the time it is installed as the 122 * current look and feel ({@code setLookAndFeel()} is invoked). The 123 * look and feel defaults can be obtained using the {@code 124 * getLookAndFeelDefaults()} method. 125 * <li>Sytem defaults. The system defaults are provided by Swing. 126 * </ol> 127 * Invoking any of the various {@code get} methods 128 * results in checking each of the defaults, in order, returning 129 * the first {@code non-null} value. For example, invoking 130 * {@code UIManager.getString("Table.foreground")} results in first 131 * checking developer defaults. If the developer defaults contain 132 * a value for {@code "Table.foreground"} it is returned, otherwise 133 * the look and feel defaults are checked, followed by the system defaults. 134 * <p> 135 * It's important to note that {@code getDefaults} returns a custom 136 * instance of {@code UIDefaults} with this resolution logic built into it. 137 * For example, {@code UIManager.getDefaults().getString("Table.foreground")} 138 * is equivalent to {@code UIManager.getString("Table.foreground")}. Both 139 * resolve using the algorithm just described. In many places the 140 * documentation uses the word defaults to refer to the custom instance 141 * of {@code UIDefaults} with the resolution logic as previously described. 142 * <p> 143 * When the look and feel is changed, {@code UIManager} alters only the 144 * look and feel defaults; the developer and system defaults are not 145 * altered by the {@code UIManager} in any way. 146 * <p> 147 * The set of defaults a particular look and feel supports is defined 148 * and documented by that look and feel. In addition, each look and 149 * feel, or {@code ComponentUI} provided by a look and feel, may 150 * access the defaults at different times in their life cycle. Some 151 * look and feels may agressively look up defaults, so that changing a 152 * default may not have an effect after installing the look and feel. 153 * Other look and feels may lazily access defaults so that a change to 154 * the defaults may effect an existing look and feel. Finally, other look 155 * and feels might not configure themselves from the defaults table in 156 * any way. None-the-less it is usually the case that a look and feel 157 * expects certain defaults, so that in general 158 * a {@code ComponentUI} provided by one look and feel will not 159 * work with another look and feel. 160 * <p> 161 * <strong>Warning:</strong> 162 * Serialized objects of this class will not be compatible with 163 * future Swing releases. The current serialization support is 164 * appropriate for short term storage or RMI between applications running 165 * the same version of Swing. As of 1.4, support for long term storage 166 * of all JavaBeans<sup><font size="-2">TM</font></sup> 167 * has been added to the <code>java.beans</code> package. 168 * Please see {@link java.beans.XMLEncoder}. 169 * 170 * @author Thomas Ball 171 * @author Hans Muller 172 */ 173 public class UIManager implements Serializable 174 { 175 /** 176 * This class defines the state managed by the <code>UIManager</code>. For 177 * Swing applications the fields in this class could just as well 178 * be static members of <code>UIManager</code> however we give them 179 * "AppContext" 180 * scope instead so that applets (and potentially multiple lightweight 181 * applications running in a single VM) have their own state. For example, 182 * an applet can alter its look and feel, see <code>setLookAndFeel</code>. 183 * Doing so has no affect on other applets (or the browser). 184 */ 185 private static class LAFState 186 { 187 Properties swingProps; 188 private UIDefaults[] tables = new UIDefaults[2]; 189 190 boolean initialized = false; 191 MultiUIDefaults multiUIDefaults = new MultiUIDefaults(tables); 192 LookAndFeel lookAndFeel; 193 LookAndFeel multiLookAndFeel = null; 194 Vector auxLookAndFeels = null; 195 SwingPropertyChangeSupport changeSupport; 196 197 LookAndFeelInfo[] installedLAFs; 198 199 UIDefaults getLookAndFeelDefaults() { return tables[0]; } 200 void setLookAndFeelDefaults(UIDefaults x) { tables[0] = x; } 201 202 UIDefaults getSystemDefaults() { return tables[1]; } 203 void setSystemDefaults(UIDefaults x) { tables[1] = x; } 204 205 /** 206 * Returns the SwingPropertyChangeSupport for the current 207 * AppContext. If <code>create</code> is a true, a non-null 208 * <code>SwingPropertyChangeSupport</code> will be returned, if 209 * <code>create</code> is false and this has not been invoked 210 * with true, null will be returned. 211 */ 212 public synchronized SwingPropertyChangeSupport 213 getPropertyChangeSupport(boolean create) { 214 if (create && changeSupport == null) { 215 changeSupport = new SwingPropertyChangeSupport( 216 UIManager.class); 217 } 218 return changeSupport; 219 } 220 } 221 222 223 224 225 /* Lock object used in place of class object for synchronization. (4187686) 226 */ 227 private static final Object classLock = new Object(); 228 229 /** 230 * Return the <code>LAFState</code> object, lazily create one if necessary. 231 * All access to the <code>LAFState</code> fields is done via this method, 232 * for example: 233 * <pre> 234 * getLAFState().initialized = true; 235 * </pre> 236 */ 237 private static LAFState getLAFState() { 238 LAFState rv = (LAFState)SwingUtilities.appContextGet( 239 SwingUtilities2.LAF_STATE_KEY); 240 if (rv == null) { 241 synchronized (classLock) { 242 rv = (LAFState)SwingUtilities.appContextGet( 243 SwingUtilities2.LAF_STATE_KEY); 244 if (rv == null) { 245 SwingUtilities.appContextPut( 246 SwingUtilities2.LAF_STATE_KEY, 247 (rv = new LAFState())); 248 } 249 } 250 } 251 return rv; 252 } 253 254 255 /* Keys used for the properties file in <java.home>/lib/swing.properties. 256 * See loadUserProperties(), initialize(). 257 */ 258 259 private static final String defaultLAFKey = "swing.defaultlaf"; 260 private static final String auxiliaryLAFsKey = "swing.auxiliarylaf"; 261 private static final String multiplexingLAFKey = "swing.plaf.multiplexinglaf"; 262 private static final String installedLAFsKey = "swing.installedlafs"; 263 private static final String disableMnemonicKey = "swing.disablenavaids"; 264 265 /** 266 * Return a swing.properties file key for the attribute of specified 267 * look and feel. The attr is either "name" or "class", a typical 268 * key would be: "swing.installedlaf.windows.name" 269 */ 270 private static String makeInstalledLAFKey(String laf, String attr) { 271 return "swing.installedlaf." + laf + "." + attr; 272 } 273 274 /** 275 * The filename for swing.properties is a path like this (Unix version): 276 * <java.home>/lib/swing.properties. This method returns a bogus 277 * filename if java.home isn't defined. 278 */ 279 private static String makeSwingPropertiesFilename() { 280 String sep = File.separator; 281 // No need to wrap this in a doPrivileged as it's called from 282 // a doPrivileged. 283 String javaHome = System.getProperty("java.home"); 284 if (javaHome == null) { 285 javaHome = "<java.home undefined>"; 286 } 287 return javaHome + sep + "lib" + sep + "swing.properties"; 288 } 289 290 291 /** 292 * Provides a little information about an installed 293 * <code>LookAndFeel</code> for the sake of configuring a menu or 294 * for initial application set up. 295 * 296 * @see UIManager#getInstalledLookAndFeels 297 * @see LookAndFeel 298 */ 299 public static class LookAndFeelInfo { 300 private String name; 301 private String className; 302 303 /** 304 * Constructs a <code>UIManager</code>s 305 * <code>LookAndFeelInfo</code> object. 306 * 307 * @param name a <code>String</code> specifying the name of 308 * the look and feel 309 * @param className a <code>String</code> specifiying the name of 310 * the class that implements the look and feel 311 */ 312 public LookAndFeelInfo(String name, String className) { 313 this.name = name; 314 this.className = className; 315 } 316 317 /** 318 * Returns the name of the look and feel in a form suitable 319 * for a menu or other presentation 320 * @return a <code>String</code> containing the name 321 * @see LookAndFeel#getName 322 */ 323 public String getName() { 324 return name; 325 } 326 327 /** 328 * Returns the name of the class that implements this look and feel. 329 * @return the name of the class that implements this 330 * <code>LookAndFeel</code> 331 * @see LookAndFeel 332 */ 333 public String getClassName() { 334 return className; 335 } 336 337 /** 338 * Returns a string that displays and identifies this 339 * object's properties. 340 * 341 * @return a <code>String</code> representation of this object 342 */ 343 public String toString() { 344 return getClass().getName() + "[" + getName() + " " + getClassName() + "]"; 345 } 346 } 347 348 349 /** 350 * The default value of <code>installedLAFS</code> is used when no 351 * swing.properties 352 * file is available or if the file doesn't contain a "swing.installedlafs" 353 * property. 354 * 355 * @see #initializeInstalledLAFs 356 */ 357 private static LookAndFeelInfo[] installedLAFs; 358 359 static { 360 ArrayList iLAFs = new ArrayList(4); 361 iLAFs.add(new LookAndFeelInfo( 362 "Metal", "javax.swing.plaf.metal.MetalLookAndFeel")); 363 iLAFs.add(new LookAndFeelInfo( 364 "Nimbus", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel")); 365 iLAFs.add(new LookAndFeelInfo("CDE/Motif", 366 "com.sun.java.swing.plaf.motif.MotifLookAndFeel")); 367 368 // Only include windows on Windows boxs. 369 OSInfo.OSType osType = AccessController.doPrivileged(OSInfo.getOSTypeAction()); 370 if (osType == OSInfo.OSType.WINDOWS) { 371 iLAFs.add(new LookAndFeelInfo("Windows", 372 "com.sun.java.swing.plaf.windows.WindowsLookAndFeel")); 373 if (Toolkit.getDefaultToolkit().getDesktopProperty( 374 "win.xpstyle.themeActive") != null) { 375 iLAFs.add(new LookAndFeelInfo("Windows Classic", 376 "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel")); 377 } 378 } 379 else { 380 // GTK is not shipped on Windows. 381 iLAFs.add(new LookAndFeelInfo("GTK+", 382 "com.sun.java.swing.plaf.gtk.GTKLookAndFeel")); 383 } 384 installedLAFs = (LookAndFeelInfo[])iLAFs.toArray( 385 new LookAndFeelInfo[iLAFs.size()]); 386 } 387 388 389 /** 390 * Returns an array of {@code LookAndFeelInfo}s representing the 391 * {@code LookAndFeel} implementations currently available. The 392 * <code>LookAndFeelInfo</code> objects can be used by an 393 * application to construct a menu of look and feel options for 394 * the user, or to determine which look and feel to set at startup 395 * time. To avoid the penalty of creating numerous {@code 396 * LookAndFeel} objects, {@code LookAndFeelInfo} maintains the 397 * class name of the {@code LookAndFeel} class, not the actual 398 * {@code LookAndFeel} instance. 399 * <p> 400 * The following example illustrates setting the current look and feel 401 * from an instance of {@code LookAndFeelInfo}: 402 * <pre> 403 * UIManager.setLookAndFeel(info.getClassName()); 404 * </pre> 405 * 406 * @return an array of <code>LookAndFeelInfo</code> objects 407 * @see #setLookAndFeel 408 */ 409 public static LookAndFeelInfo[] getInstalledLookAndFeels() { 410 maybeInitialize(); 411 LookAndFeelInfo[] ilafs = getLAFState().installedLAFs; 412 if (ilafs == null) { 413 ilafs = installedLAFs; 414 } 415 LookAndFeelInfo[] rv = new LookAndFeelInfo[ilafs.length]; 416 System.arraycopy(ilafs, 0, rv, 0, ilafs.length); 417 return rv; 418 } 419 420 421 /** 422 * Sets the set of available look and feels. While this method does 423 * not check to ensure all of the {@code LookAndFeelInfos} are 424 * {@code non-null}, it is strongly recommended that only {@code non-null} 425 * values are supplied in the {@code infos} array. 426 * 427 * @param infos set of <code>LookAndFeelInfo</code> objects specifying 428 * the available look and feels 429 * 430 * @see #getInstalledLookAndFeels 431 * @throws NullPointerException if {@code infos} is {@code null} 432 */ 433 public static void setInstalledLookAndFeels(LookAndFeelInfo[] infos) 434 throws SecurityException 435 { 436 maybeInitialize(); 437 LookAndFeelInfo[] newInfos = new LookAndFeelInfo[infos.length]; 438 System.arraycopy(infos, 0, newInfos, 0, infos.length); 439 getLAFState().installedLAFs = newInfos; 440 } 441 442 443 /** 444 * Adds the specified look and feel to the set of available look 445 * and feels. While this method allows a {@code null} {@code info}, 446 * it is strongly recommended that a {@code non-null} value be used. 447 * 448 * @param info a <code>LookAndFeelInfo</code> object that names the 449 * look and feel and identifies the class that implements it 450 * @see #setInstalledLookAndFeels 451 */ 452 public static void installLookAndFeel(LookAndFeelInfo info) { 453 LookAndFeelInfo[] infos = getInstalledLookAndFeels(); 454 LookAndFeelInfo[] newInfos = new LookAndFeelInfo[infos.length + 1]; 455 System.arraycopy(infos, 0, newInfos, 0, infos.length); 456 newInfos[infos.length] = info; 457 setInstalledLookAndFeels(newInfos); 458 } 459 460 461 /** 462 * Adds the specified look and feel to the set of available look 463 * and feels. While this method does not check the 464 * arguments in any way, it is strongly recommended that {@code 465 * non-null} values be supplied. 466 * 467 * @param name descriptive name of the look and feel 468 * @param className name of the class that implements the look and feel 469 * @see #setInstalledLookAndFeels 470 */ 471 public static void installLookAndFeel(String name, String className) { 472 installLookAndFeel(new LookAndFeelInfo(name, className)); 473 } 474 475 476 /** 477 * Returns the current look and feel or <code>null</code>. 478 * 479 * @return current look and feel, or <code>null</code> 480 * @see #setLookAndFeel 481 */ 482 public static LookAndFeel getLookAndFeel() { 483 maybeInitialize(); 484 return getLAFState().lookAndFeel; 485 } 486 487 488 /** 489 * Sets the current look and feel to {@code newLookAndFeel}. 490 * If the current look and feel is {@code non-null} {@code 491 * uninitialize} is invoked on it. If {@code newLookAndFeel} is 492 * {@code non-null}, {@code initialize} is invoked on it followed 493 * by {@code getDefaults}. The defaults returned from {@code 494 * newLookAndFeel.getDefaults()} replace those of the defaults 495 * from the previous look and feel. If the {@code newLookAndFeel} is 496 * {@code null}, the look and feel defaults are set to {@code null}. 497 * <p> 498 * A value of {@code null} can be used to set the look and feel 499 * to {@code null}. As the {@code LookAndFeel} is required for 500 * most of Swing to function, setting the {@code LookAndFeel} to 501 * {@code null} is strongly discouraged. 502 * <p> 503 * This is a JavaBeans bound property. 504 * 505 * @param newLookAndFeel {@code LookAndFeel} to install 506 * @throws UnsupportedLookAndFeelException if 507 * {@code newLookAndFeel} is {@code non-null} and 508 * {@code newLookAndFeel.isSupportedLookAndFeel()} returns 509 * {@code false} 510 * @see #getLookAndFeel 511 */ 512 public static void setLookAndFeel(LookAndFeel newLookAndFeel) 513 throws UnsupportedLookAndFeelException 514 { 515 if ((newLookAndFeel != null) && !newLookAndFeel.isSupportedLookAndFeel()) { 516 String s = newLookAndFeel.toString() + " not supported on this platform"; 517 throw new UnsupportedLookAndFeelException(s); 518 } 519 520 LAFState lafState = getLAFState(); 521 LookAndFeel oldLookAndFeel = lafState.lookAndFeel; 522 if (oldLookAndFeel != null) { 523 oldLookAndFeel.uninitialize(); 524 } 525 526 lafState.lookAndFeel = newLookAndFeel; 527 if (newLookAndFeel != null) { 528 sun.swing.DefaultLookup.setDefaultLookup(null); 529 newLookAndFeel.initialize(); 530 lafState.setLookAndFeelDefaults(newLookAndFeel.getDefaults()); 531 } 532 else { 533 lafState.setLookAndFeelDefaults(null); 534 } 535 536 SwingPropertyChangeSupport changeSupport = lafState. 537 getPropertyChangeSupport(false); 538 if (changeSupport != null) { 539 changeSupport.firePropertyChange("lookAndFeel", oldLookAndFeel, 540 newLookAndFeel); 541 } 542 } 543 544 545 /** 546 * Loads the {@code LookAndFeel} specified by the given class 547 * name, using the current thread's context class loader, and 548 * passes it to {@code setLookAndFeel(LookAndFeel)}. 549 * 550 * @param className a string specifying the name of the class that implements 551 * the look and feel 552 * @exception ClassNotFoundException if the <code>LookAndFeel</code> 553 * class could not be found 554 * @exception InstantiationException if a new instance of the class 555 * couldn't be created 556 * @exception IllegalAccessException if the class or initializer isn't accessible 557 * @exception UnsupportedLookAndFeelException if 558 * <code>lnf.isSupportedLookAndFeel()</code> is false 559 * @throws ClassCastException if {@code className} does not identify 560 * a class that extends {@code LookAndFeel} 561 */ 562 public static void setLookAndFeel(String className) 563 throws ClassNotFoundException, 564 InstantiationException, 565 IllegalAccessException, 566 UnsupportedLookAndFeelException 567 { 568 if ("javax.swing.plaf.metal.MetalLookAndFeel".equals(className)) { 569 // Avoid reflection for the common case of metal. 570 setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel()); 571 } 572 else { 573 Class lnfClass = SwingUtilities.loadSystemClass(className); 574 setLookAndFeel((LookAndFeel)(lnfClass.newInstance())); 575 } 576 } 577 578 /** 579 * Returns the name of the <code>LookAndFeel</code> class that implements 580 * the native system look and feel if there is one, otherwise 581 * the name of the default cross platform <code>LookAndFeel</code> 582 * class. This value can be overriden by setting the 583 * <code>swing.systemlaf</code> system property. 584 * 585 * @return the <code>String</code> of the <code>LookAndFeel</code> 586 * class 587 * 588 * @see #setLookAndFeel 589 * @see #getCrossPlatformLookAndFeelClassName 590 */ 591 public static String getSystemLookAndFeelClassName() { 592 String systemLAF = AccessController.doPrivileged( 593 new GetPropertyAction("swing.systemlaf")); 594 if (systemLAF != null) { 595 return systemLAF; 596 } 597 OSInfo.OSType osType = AccessController.doPrivileged(OSInfo.getOSTypeAction()); 598 if (osType == OSInfo.OSType.WINDOWS) { 599 return "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; 600 } else { 601 String desktop = AccessController.doPrivileged(new GetPropertyAction("sun.desktop")); 602 Toolkit toolkit = Toolkit.getDefaultToolkit(); 603 if ("gnome".equals(desktop) && 604 toolkit instanceof SunToolkit && 605 ((SunToolkit) toolkit).isNativeGTKAvailable()) { 606 // May be set on Linux and Solaris boxs. 607 return "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; 608 } 609 if (osType == OSInfo.OSType.SOLARIS) { 610 return "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; 611 } 612 } 613 return getCrossPlatformLookAndFeelClassName(); 614 } 615 616 617 /** 618 * Returns the name of the <code>LookAndFeel</code> class that implements 619 * the default cross platform look and feel -- the Java 620 * Look and Feel (JLF). This value can be overriden by setting the 621 * <code>swing.crossplatformlaf</code> system property. 622 * 623 * @return a string with the JLF implementation-class 624 * @see #setLookAndFeel 625 * @see #getSystemLookAndFeelClassName 626 */ 627 public static String getCrossPlatformLookAndFeelClassName() { 628 String laf = (String)AccessController.doPrivileged( 629 new GetPropertyAction("swing.crossplatformlaf")); 630 if (laf != null) { 631 return laf; 632 } 633 return "javax.swing.plaf.metal.MetalLookAndFeel"; 634 } 635 636 637 /** 638 * Returns the defaults. The returned defaults resolve using the 639 * logic specified in the class documentation. 640 * 641 * @return a <code>UIDefaults</code> object containing the default values 642 */ 643 public static UIDefaults getDefaults() { 644 maybeInitialize(); 645 return getLAFState().multiUIDefaults; 646 } 647 648 /** 649 * Returns a font from the defaults. If the value for {@code key} is 650 * not a {@code Font}, {@code null} is returned. 651 * 652 * @param key an <code>Object</code> specifying the font 653 * @return the <code>Font</code> object 654 * @throws NullPointerException if {@code key} is {@code null} 655 */ 656 public static Font getFont(Object key) { 657 return getDefaults().getFont(key); 658 } 659 660 /** 661 * Returns a font from the defaults that is appropriate 662 * for the given locale. If the value for {@code key} is 663 * not a {@code Font}, {@code null} is returned. 664 * 665 * @param key an <code>Object</code> specifying the font 666 * @param l the <code>Locale</code> for which the font is desired; refer 667 * to {@code UIDefaults} for details on how a {@code null} 668 * {@code Locale} is handled 669 * @return the <code>Font</code> object 670 * @throws NullPointerException if {@code key} is {@code null} 671 * @since 1.4 672 */ 673 public static Font getFont(Object key, Locale l) { 674 return getDefaults().getFont(key,l); 675 } 676 677 /** 678 * Returns a color from the defaults. If the value for {@code key} is 679 * not a {@code Color}, {@code null} is returned. 680 * 681 * @param key an <code>Object</code> specifying the color 682 * @return the <code>Color</code> object 683 * @throws NullPointerException if {@code key} is {@code null} 684 */ 685 public static Color getColor(Object key) { 686 return getDefaults().getColor(key); 687 } 688 689 /** 690 * Returns a color from the defaults that is appropriate 691 * for the given locale. If the value for {@code key} is 692 * not a {@code Color}, {@code null} is returned. 693 * 694 * @param key an <code>Object</code> specifying the color 695 * @param l the <code>Locale</code> for which the color is desired; refer 696 * to {@code UIDefaults} for details on how a {@code null} 697 * {@code Locale} is handled 698 * @return the <code>Color</code> object 699 * @throws NullPointerException if {@code key} is {@code null} 700 * @since 1.4 701 */ 702 public static Color getColor(Object key, Locale l) { 703 return getDefaults().getColor(key,l); 704 } 705 706 /** 707 * Returns an <code>Icon</code> from the defaults. If the value for 708 * {@code key} is not an {@code Icon}, {@code null} is returned. 709 * 710 * @param key an <code>Object</code> specifying the icon 711 * @return the <code>Icon</code> object 712 * @throws NullPointerException if {@code key} is {@code null} 713 */ 714 public static Icon getIcon(Object key) { 715 return getDefaults().getIcon(key); 716 } 717 718 /** 719 * Returns an <code>Icon</code> from the defaults that is appropriate 720 * for the given locale. If the value for 721 * {@code key} is not an {@code Icon}, {@code null} is returned. 722 * 723 * @param key an <code>Object</code> specifying the icon 724 * @param l the <code>Locale</code> for which the icon is desired; refer 725 * to {@code UIDefaults} for details on how a {@code null} 726 * {@code Locale} is handled 727 * @return the <code>Icon</code> object 728 * @throws NullPointerException if {@code key} is {@code null} 729 * @since 1.4 730 */ 731 public static Icon getIcon(Object key, Locale l) { 732 return getDefaults().getIcon(key,l); 733 } 734 735 /** 736 * Returns a border from the defaults. If the value for 737 * {@code key} is not a {@code Border}, {@code null} is returned. 738 * 739 * @param key an <code>Object</code> specifying the border 740 * @return the <code>Border</code> object 741 * @throws NullPointerException if {@code key} is {@code null} 742 */ 743 public static Border getBorder(Object key) { 744 return getDefaults().getBorder(key); 745 } 746 747 /** 748 * Returns a border from the defaults that is appropriate 749 * for the given locale. If the value for 750 * {@code key} is not a {@code Border}, {@code null} is returned. 751 * 752 * @param key an <code>Object</code> specifying the border 753 * @param l the <code>Locale</code> for which the border is desired; refer 754 * to {@code UIDefaults} for details on how a {@code null} 755 * {@code Locale} is handled 756 * @return the <code>Border</code> object 757 * @throws NullPointerException if {@code key} is {@code null} 758 * @since 1.4 759 */ 760 public static Border getBorder(Object key, Locale l) { 761 return getDefaults().getBorder(key,l); 762 } 763 764 /** 765 * Returns a string from the defaults. If the value for 766 * {@code key} is not a {@code String}, {@code null} is returned. 767 * 768 * @param key an <code>Object</code> specifying the string 769 * @return the <code>String</code> 770 * @throws NullPointerException if {@code key} is {@code null} 771 */ 772 public static String getString(Object key) { 773 return getDefaults().getString(key); 774 } 775 776 /** 777 * Returns a string from the defaults that is appropriate for the 778 * given locale. If the value for 779 * {@code key} is not a {@code String}, {@code null} is returned. 780 * 781 * @param key an <code>Object</code> specifying the string 782 * @param l the <code>Locale</code> for which the string is desired; refer 783 * to {@code UIDefaults} for details on how a {@code null} 784 * {@code Locale} is handled 785 * @return the <code>String</code> 786 * @since 1.4 787 * @throws NullPointerException if {@code key} is {@code null} 788 */ 789 public static String getString(Object key, Locale l) { 790 return getDefaults().getString(key,l); 791 } 792 793 /** 794 * Returns a string from the defaults that is appropriate for the 795 * given locale. If the value for 796 * {@code key} is not a {@code String}, {@code null} is returned. 797 * 798 * @param key an <code>Object</code> specifying the string 799 * @param c {@code Component} used to determine the locale; 800 * {@code null} implies the default locale as 801 * returned by {@code Locale.getDefault()} 802 * @return the <code>String</code> 803 * @throws NullPointerException if {@code key} is {@code null} 804 */ 805 static String getString(Object key, Component c) { 806 Locale l = (c == null) ? Locale.getDefault() : c.getLocale(); 807 return getString(key, l); 808 } 809 810 /** 811 * Returns an integer from the defaults. If the value for 812 * {@code key} is not an {@code Integer}, or does not exist, 813 * {@code 0} is returned. 814 * 815 * @param key an <code>Object</code> specifying the int 816 * @return the int 817 * @throws NullPointerException if {@code key} is {@code null} 818 */ 819 public static int getInt(Object key) { 820 return getDefaults().getInt(key); 821 } 822 823 /** 824 * Returns an integer from the defaults that is appropriate 825 * for the given locale. If the value for 826 * {@code key} is not an {@code Integer}, or does not exist, 827 * {@code 0} is returned. 828 * 829 * @param key an <code>Object</code> specifying the int 830 * @param l the <code>Locale</code> for which the int is desired; refer 831 * to {@code UIDefaults} for details on how a {@code null} 832 * {@code Locale} is handled 833 * @return the int 834 * @throws NullPointerException if {@code key} is {@code null} 835 * @since 1.4 836 */ 837 public static int getInt(Object key, Locale l) { 838 return getDefaults().getInt(key,l); 839 } 840 841 /** 842 * Returns a boolean from the defaults which is associated with 843 * the key value. If the key is not found or the key doesn't represent 844 * a boolean value then {@code false} is returned. 845 * 846 * @param key an <code>Object</code> specifying the key for the desired boolean value 847 * @return the boolean value corresponding to the key 848 * @throws NullPointerException if {@code key} is {@code null} 849 * @since 1.4 850 */ 851 public static boolean getBoolean(Object key) { 852 return getDefaults().getBoolean(key); 853 } 854 855 /** 856 * Returns a boolean from the defaults which is associated with 857 * the key value and the given <code>Locale</code>. If the key is not 858 * found or the key doesn't represent 859 * a boolean value then {@code false} will be returned. 860 * 861 * @param key an <code>Object</code> specifying the key for the desired 862 * boolean value 863 * @param l the <code>Locale</code> for which the boolean is desired; refer 864 * to {@code UIDefaults} for details on how a {@code null} 865 * {@code Locale} is handled 866 * @return the boolean value corresponding to the key 867 * @throws NullPointerException if {@code key} is {@code null} 868 * @since 1.4 869 */ 870 public static boolean getBoolean(Object key, Locale l) { 871 return getDefaults().getBoolean(key,l); 872 } 873 874 /** 875 * Returns an <code>Insets</code> object from the defaults. If the value 876 * for {@code key} is not an {@code Insets}, {@code null} is returned. 877 * 878 * @param key an <code>Object</code> specifying the <code>Insets</code> object 879 * @return the <code>Insets</code> object 880 * @throws NullPointerException if {@code key} is {@code null} 881 */ 882 public static Insets getInsets(Object key) { 883 return getDefaults().getInsets(key); 884 } 885 886 /** 887 * Returns an <code>Insets</code> object from the defaults that is 888 * appropriate for the given locale. If the value 889 * for {@code key} is not an {@code Insets}, {@code null} is returned. 890 * 891 * @param key an <code>Object</code> specifying the <code>Insets</code> object 892 * @param l the <code>Locale</code> for which the object is desired; refer 893 * to {@code UIDefaults} for details on how a {@code null} 894 * {@code Locale} is handled 895 * @return the <code>Insets</code> object 896 * @throws NullPointerException if {@code key} is {@code null} 897 * @since 1.4 898 */ 899 public static Insets getInsets(Object key, Locale l) { 900 return getDefaults().getInsets(key,l); 901 } 902 903 /** 904 * Returns a dimension from the defaults. If the value 905 * for {@code key} is not a {@code Dimension}, {@code null} is returned. 906 * 907 * @param key an <code>Object</code> specifying the dimension object 908 * @return the <code>Dimension</code> object 909 * @throws NullPointerException if {@code key} is {@code null} 910 */ 911 public static Dimension getDimension(Object key) { 912 return getDefaults().getDimension(key); 913 } 914 915 /** 916 * Returns a dimension from the defaults that is appropriate 917 * for the given locale. If the value 918 * for {@code key} is not a {@code Dimension}, {@code null} is returned. 919 * 920 * @param key an <code>Object</code> specifying the dimension object 921 * @param l the <code>Locale</code> for which the object is desired; refer 922 * to {@code UIDefaults} for details on how a {@code null} 923 * {@code Locale} is handled 924 * @return the <code>Dimension</code> object 925 * @throws NullPointerException if {@code key} is {@code null} 926 * @since 1.4 927 */ 928 public static Dimension getDimension(Object key, Locale l) { 929 return getDefaults().getDimension(key,l); 930 } 931 932 /** 933 * Returns an object from the defaults. 934 * 935 * @param key an <code>Object</code> specifying the desired object 936 * @return the <code>Object</code> 937 * @throws NullPointerException if {@code key} is {@code null} 938 */ 939 public static Object get(Object key) { 940 return getDefaults().get(key); 941 } 942 943 /** 944 * Returns an object from the defaults that is appropriate for 945 * the given locale. 946 * 947 * @param key an <code>Object</code> specifying the desired object 948 * @param l the <code>Locale</code> for which the object is desired; refer 949 * to {@code UIDefaults} for details on how a {@code null} 950 * {@code Locale} is handled 951 * @return the <code>Object</code> 952 * @throws NullPointerException if {@code key} is {@code null} 953 * @since 1.4 954 */ 955 public static Object get(Object key, Locale l) { 956 return getDefaults().get(key,l); 957 } 958 959 /** 960 * Stores an object in the developer defaults. This is a cover method 961 * for {@code getDefaults().put(key, value)}. This only effects the 962 * developer defaults, not the system or look and feel defaults. 963 * 964 * @param key an <code>Object</code> specifying the retrieval key 965 * @param value the <code>Object</code> to store; refer to 966 * {@code UIDefaults} for details on how {@code null} is 967 * handled 968 * @return the <code>Object</code> returned by {@link UIDefaults#put} 969 * @throws NullPointerException if {@code key} is {@code null} 970 * @see UIDefaults#put 971 */ 972 public static Object put(Object key, Object value) { 973 return getDefaults().put(key, value); 974 } 975 976 /** 977 * Returns the appropriate {@code ComponentUI} implementation for 978 * {@code target}. Typically, this is a cover for 979 * {@code getDefaults().getUI(target)}. However, if an auxiliary 980 * look and feel has been installed, this first invokes 981 * {@code getUI(target)} on the multiplexing look and feel's 982 * defaults, and returns that value if it is {@code non-null}. 983 * 984 * @param target the <code>JComponent</code> to return the 985 * {@code ComponentUI} for 986 * @return the <code>ComponentUI</code> object for {@code target} 987 * @throws NullPointerException if {@code target} is {@code null} 988 * @see UIDefaults#getUI 989 */ 990 public static ComponentUI getUI(JComponent target) { 991 maybeInitialize(); 992 ComponentUI ui = null; 993 LookAndFeel multiLAF = getLAFState().multiLookAndFeel; 994 if (multiLAF != null) { 995 // This can return null if the multiplexing look and feel 996 // doesn't support a particular UI. 997 ui = multiLAF.getDefaults().getUI(target); 998 } 999 if (ui == null) { 1000 ui = getDefaults().getUI(target); 1001 } 1002 return ui; 1003 } 1004 1005 1006 /** 1007 * Returns the {@code UIDefaults} from the current look and feel, 1008 * that were obtained at the time the look and feel was installed. 1009 * <p> 1010 * In general, developers should use the {@code UIDefaults} returned from 1011 * {@code getDefaults()}. As the current look and feel may expect 1012 * certain values to exist, altering the {@code UIDefaults} returned 1013 * from this method could have unexpected results. 1014 * 1015 * @return <code>UIDefaults</code> from the current look and feel 1016 * @see #getDefaults 1017 * @see #setLookAndFeel(LookAndFeel) 1018 * @see LookAndFeel#getDefaults 1019 */ 1020 public static UIDefaults getLookAndFeelDefaults() { 1021 maybeInitialize(); 1022 return getLAFState().getLookAndFeelDefaults(); 1023 } 1024 1025 /** 1026 * Finds the Multiplexing <code>LookAndFeel</code>. 1027 */ 1028 private static LookAndFeel getMultiLookAndFeel() { 1029 LookAndFeel multiLookAndFeel = getLAFState().multiLookAndFeel; 1030 if (multiLookAndFeel == null) { 1031 String defaultName = "javax.swing.plaf.multi.MultiLookAndFeel"; 1032 String className = getLAFState().swingProps.getProperty(multiplexingLAFKey, defaultName); 1033 try { 1034 Class lnfClass = SwingUtilities.loadSystemClass(className); 1035 multiLookAndFeel = (LookAndFeel)lnfClass.newInstance(); 1036 } catch (Exception exc) { 1037 System.err.println("UIManager: failed loading " + className); 1038 } 1039 } 1040 return multiLookAndFeel; 1041 } 1042 1043 /** 1044 * Adds a <code>LookAndFeel</code> to the list of auxiliary look and feels. 1045 * The auxiliary look and feels tell the multiplexing look and feel what 1046 * other <code>LookAndFeel</code> classes for a component instance are to be used 1047 * in addition to the default <code>LookAndFeel</code> class when creating a 1048 * multiplexing UI. The change will only take effect when a new 1049 * UI class is created or when the default look and feel is changed 1050 * on a component instance. 1051 * <p>Note these are not the same as the installed look and feels. 1052 * 1053 * @param laf the <code>LookAndFeel</code> object 1054 * @see #removeAuxiliaryLookAndFeel 1055 * @see #setLookAndFeel 1056 * @see #getAuxiliaryLookAndFeels 1057 * @see #getInstalledLookAndFeels 1058 */ 1059 static public void addAuxiliaryLookAndFeel(LookAndFeel laf) { 1060 maybeInitialize(); 1061 1062 if (!laf.isSupportedLookAndFeel()) { 1063 // Ideally we would throw an exception here, but it's too late 1064 // for that. 1065 return; 1066 } 1067 Vector v = getLAFState().auxLookAndFeels; 1068 if (v == null) { 1069 v = new Vector(); 1070 } 1071 1072 if (!v.contains(laf)) { 1073 v.addElement(laf); 1074 laf.initialize(); 1075 getLAFState().auxLookAndFeels = v; 1076 1077 if (getLAFState().multiLookAndFeel == null) { 1078 getLAFState().multiLookAndFeel = getMultiLookAndFeel(); 1079 } 1080 } 1081 } 1082 1083 /** 1084 * Removes a <code>LookAndFeel</code> from the list of auxiliary look and feels. 1085 * The auxiliary look and feels tell the multiplexing look and feel what 1086 * other <code>LookAndFeel</code> classes for a component instance are to be used 1087 * in addition to the default <code>LookAndFeel</code> class when creating a 1088 * multiplexing UI. The change will only take effect when a new 1089 * UI class is created or when the default look and feel is changed 1090 * on a component instance. 1091 * <p>Note these are not the same as the installed look and feels. 1092 * @return true if the <code>LookAndFeel</code> was removed from the list 1093 * @see #removeAuxiliaryLookAndFeel 1094 * @see #getAuxiliaryLookAndFeels 1095 * @see #setLookAndFeel 1096 * @see #getInstalledLookAndFeels 1097 */ 1098 static public boolean removeAuxiliaryLookAndFeel(LookAndFeel laf) { 1099 maybeInitialize(); 1100 1101 boolean result; 1102 1103 Vector v = getLAFState().auxLookAndFeels; 1104 if ((v == null) || (v.size() == 0)) { 1105 return false; 1106 } 1107 1108 result = v.removeElement(laf); 1109 if (result) { 1110 if (v.size() == 0) { 1111 getLAFState().auxLookAndFeels = null; 1112 getLAFState().multiLookAndFeel = null; 1113 } else { 1114 getLAFState().auxLookAndFeels = v; 1115 } 1116 } 1117 laf.uninitialize(); 1118 1119 return result; 1120 } 1121 1122 /** 1123 * Returns the list of auxiliary look and feels (can be <code>null</code>). 1124 * The auxiliary look and feels tell the multiplexing look and feel what 1125 * other <code>LookAndFeel</code> classes for a component instance are 1126 * to be used in addition to the default LookAndFeel class when creating a 1127 * multiplexing UI. 1128 * <p>Note these are not the same as the installed look and feels. 1129 * 1130 * @return list of auxiliary <code>LookAndFeel</code>s or <code>null</code> 1131 * @see #addAuxiliaryLookAndFeel 1132 * @see #removeAuxiliaryLookAndFeel 1133 * @see #setLookAndFeel 1134 * @see #getInstalledLookAndFeels 1135 */ 1136 static public LookAndFeel[] getAuxiliaryLookAndFeels() { 1137 maybeInitialize(); 1138 1139 Vector v = getLAFState().auxLookAndFeels; 1140 if ((v == null) || (v.size() == 0)) { 1141 return null; 1142 } 1143 else { 1144 LookAndFeel[] rv = new LookAndFeel[v.size()]; 1145 for (int i = 0; i < rv.length; i++) { 1146 rv[i] = (LookAndFeel)v.elementAt(i); 1147 } 1148 return rv; 1149 } 1150 } 1151 1152 1153 /** 1154 * Adds a <code>PropertyChangeListener</code> to the listener list. 1155 * The listener is registered for all properties. 1156 * 1157 * @param listener the <code>PropertyChangeListener</code> to be added 1158 * @see java.beans.PropertyChangeSupport 1159 */ 1160 public static void addPropertyChangeListener(PropertyChangeListener listener) 1161 { 1162 synchronized (classLock) { 1163 getLAFState().getPropertyChangeSupport(true). 1164 addPropertyChangeListener(listener); 1165 } 1166 } 1167 1168 1169 /** 1170 * Removes a <code>PropertyChangeListener</code> from the listener list. 1171 * This removes a <code>PropertyChangeListener</code> that was registered 1172 * for all properties. 1173 * 1174 * @param listener the <code>PropertyChangeListener</code> to be removed 1175 * @see java.beans.PropertyChangeSupport 1176 */ 1177 public static void removePropertyChangeListener(PropertyChangeListener listener) 1178 { 1179 synchronized (classLock) { 1180 getLAFState().getPropertyChangeSupport(true). 1181 removePropertyChangeListener(listener); 1182 } 1183 } 1184 1185 1186 /** 1187 * Returns an array of all the <code>PropertyChangeListener</code>s added 1188 * to this UIManager with addPropertyChangeListener(). 1189 * 1190 * @return all of the <code>PropertyChangeListener</code>s added or an empty 1191 * array if no listeners have been added 1192 * @since 1.4 1193 */ 1194 public static PropertyChangeListener[] getPropertyChangeListeners() { 1195 synchronized(classLock) { 1196 return getLAFState().getPropertyChangeSupport(true). 1197 getPropertyChangeListeners(); 1198 } 1199 } 1200 1201 private static Properties loadSwingProperties() 1202 { 1203 /* Don't bother checking for Swing properties if untrusted, as 1204 * there's no way to look them up without triggering SecurityExceptions. 1205 */ 1206 if (UIManager.class.getClassLoader() != null) { 1207 return new Properties(); 1208 } 1209 else { 1210 final Properties props = new Properties(); 1211 1212 java.security.AccessController.doPrivileged( 1213 new java.security.PrivilegedAction() { 1214 public Object run() { 1215 try { 1216 File file = new File(makeSwingPropertiesFilename()); 1217 1218 if (file.exists()) { 1219 // InputStream has been buffered in Properties 1220 // class 1221 FileInputStream ins = new FileInputStream(file); 1222 props.load(ins); 1223 ins.close(); 1224 } 1225 } 1226 catch (Exception e) { 1227 // No such file, or file is otherwise non-readable. 1228 } 1229 1230 // Check whether any properties were overridden at the 1231 // command line. 1232 checkProperty(props, defaultLAFKey); 1233 checkProperty(props, auxiliaryLAFsKey); 1234 checkProperty(props, multiplexingLAFKey); 1235 checkProperty(props, installedLAFsKey); 1236 checkProperty(props, disableMnemonicKey); 1237 // Don't care about return value. 1238 return null; 1239 } 1240 }); 1241 return props; 1242 } 1243 } 1244 1245 private static void checkProperty(Properties props, String key) { 1246 // No need to do catch the SecurityException here, this runs 1247 // in a doPrivileged. 1248 String value = System.getProperty(key); 1249 if (value != null) { 1250 props.put(key, value); 1251 } 1252 } 1253 1254 1255 /** 1256 * If a swing.properties file exist and it has a swing.installedlafs property 1257 * then initialize the <code>installedLAFs</code> field. 1258 * 1259 * @see #getInstalledLookAndFeels 1260 */ 1261 private static void initializeInstalledLAFs(Properties swingProps) 1262 { 1263 String ilafsString = swingProps.getProperty(installedLAFsKey); 1264 if (ilafsString == null) { 1265 return; 1266 } 1267 1268 /* Create a vector that contains the value of the swing.installedlafs 1269 * property. For example given "swing.installedlafs=motif,windows" 1270 * lafs = {"motif", "windows"}. 1271 */ 1272 Vector lafs = new Vector(); 1273 StringTokenizer st = new StringTokenizer(ilafsString, ",", false); 1274 while (st.hasMoreTokens()) { 1275 lafs.addElement(st.nextToken()); 1276 } 1277 1278 /* Look up the name and class for each name in the "swing.installedlafs" 1279 * list. If they both exist then add a LookAndFeelInfo to 1280 * the installedLafs array. 1281 */ 1282 Vector ilafs = new Vector(lafs.size()); 1283 for(int i = 0; i < lafs.size(); i++) { 1284 String laf = (String)lafs.elementAt(i); 1285 String name = swingProps.getProperty(makeInstalledLAFKey(laf, "name"), laf); 1286 String cls = swingProps.getProperty(makeInstalledLAFKey(laf, "class")); 1287 if (cls != null) { 1288 ilafs.addElement(new LookAndFeelInfo(name, cls)); 1289 } 1290 } 1291 1292 LookAndFeelInfo[] installedLAFs = new LookAndFeelInfo[ilafs.size()]; 1293 for(int i = 0; i < ilafs.size(); i++) { 1294 installedLAFs[i] = (LookAndFeelInfo)(ilafs.elementAt(i)); 1295 } 1296 getLAFState().installedLAFs = installedLAFs; 1297 } 1298 1299 1300 /** 1301 * If the user has specified a default look and feel, use that. 1302 * Otherwise use the look and feel that's native to this platform. 1303 * If this code is called after the application has explicitly 1304 * set it's look and feel, do nothing. 1305 * 1306 * @see #maybeInitialize 1307 */ 1308 private static void initializeDefaultLAF(Properties swingProps) 1309 { 1310 if (getLAFState().lookAndFeel != null) { 1311 return; 1312 } 1313 1314 String metalLnf = getCrossPlatformLookAndFeelClassName(); 1315 String lnfDefault = metalLnf; 1316 1317 String lnfName = "<undefined>" ; 1318 try { 1319 lnfName = swingProps.getProperty(defaultLAFKey, lnfDefault); 1320 setLookAndFeel(lnfName); 1321 } catch (Exception e) { 1322 try { 1323 lnfName = swingProps.getProperty(defaultLAFKey, metalLnf); 1324 setLookAndFeel(lnfName); 1325 } catch (Exception e2) { 1326 throw new Error("can't load " + lnfName); 1327 } 1328 } 1329 } 1330 1331 1332 private static void initializeAuxiliaryLAFs(Properties swingProps) 1333 { 1334 String auxLookAndFeelNames = swingProps.getProperty(auxiliaryLAFsKey); 1335 if (auxLookAndFeelNames == null) { 1336 return; 1337 } 1338 1339 Vector auxLookAndFeels = new Vector(); 1340 1341 StringTokenizer p = new StringTokenizer(auxLookAndFeelNames,","); 1342 String factoryName; 1343 1344 /* Try to load each LookAndFeel subclass in the list. 1345 */ 1346 1347 while (p.hasMoreTokens()) { 1348 String className = p.nextToken(); 1349 try { 1350 Class lnfClass = SwingUtilities.loadSystemClass(className); 1351 LookAndFeel newLAF = (LookAndFeel)lnfClass.newInstance(); 1352 newLAF.initialize(); 1353 auxLookAndFeels.addElement(newLAF); 1354 } 1355 catch (Exception e) { 1356 System.err.println("UIManager: failed loading auxiliary look and feel " + className); 1357 } 1358 } 1359 1360 /* If there were problems and no auxiliary look and feels were 1361 * loaded, make sure we reset auxLookAndFeels to null. 1362 * Otherwise, we are going to use the MultiLookAndFeel to get 1363 * all component UI's, so we need to load it now. 1364 */ 1365 if (auxLookAndFeels.size() == 0) { 1366 auxLookAndFeels = null; 1367 } 1368 else { 1369 getLAFState().multiLookAndFeel = getMultiLookAndFeel(); 1370 if (getLAFState().multiLookAndFeel == null) { 1371 auxLookAndFeels = null; 1372 } 1373 } 1374 1375 getLAFState().auxLookAndFeels = auxLookAndFeels; 1376 } 1377 1378 1379 private static void initializeSystemDefaults(Properties swingProps) { 1380 getLAFState().swingProps = swingProps; 1381 } 1382 1383 1384 /* 1385 * This method is called before any code that depends on the 1386 * <code>AppContext</code> specific LAFState object runs. When the AppContext 1387 * corresponds to a set of applets it's possible for this method 1388 * to be re-entered, which is why we grab a lock before calling 1389 * initialize(). 1390 */ 1391 private static void maybeInitialize() { 1392 synchronized (classLock) { 1393 if (!getLAFState().initialized) { 1394 getLAFState().initialized = true; 1395 initialize(); 1396 } 1397 } 1398 } 1399 1400 1401 /* 1402 * Only called by maybeInitialize(). 1403 */ 1404 private static void initialize() { 1405 Properties swingProps = loadSwingProperties(); 1406 initializeSystemDefaults(swingProps); 1407 initializeDefaultLAF(swingProps); 1408 initializeAuxiliaryLAFs(swingProps); 1409 initializeInstalledLAFs(swingProps); 1410 1411 // Enable the Swing default LayoutManager. 1412 String toolkitName = Toolkit.getDefaultToolkit().getClass().getName(); 1413 // don't set default policy if this is XAWT. 1414 if (!"sun.awt.X11.XToolkit".equals(toolkitName)) { 1415 if (FocusManager.isFocusManagerEnabled()) { 1416 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 1417 setDefaultFocusTraversalPolicy( 1418 new LayoutFocusTraversalPolicy()); 1419 } 1420 } 1421 1422 // Install Swing's PaintEventDispatcher 1423 if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) { 1424 sun.awt.PaintEventDispatcher.setPaintEventDispatcher( 1425 new SwingPaintEventDispatcher()); 1426 } 1427 // Install a hook that will be invoked if no one consumes the 1428 // KeyEvent. If the source isn't a JComponent this will process 1429 // key bindings, if the source is a JComponent it implies that 1430 // processKeyEvent was already invoked and thus no need to process 1431 // the bindings again, unless the Component is disabled, in which 1432 // case KeyEvents will no longer be dispatched to it so that we 1433 // handle it here. 1434 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 1435 addKeyEventPostProcessor(new KeyEventPostProcessor() { 1436 public boolean postProcessKeyEvent(KeyEvent e) { 1437 Component c = e.getComponent(); 1438 1439 if ((!(c instanceof JComponent) || 1440 (c != null && !((JComponent)c).isEnabled())) && 1441 JComponent.KeyboardState.shouldProcess(e) && 1442 SwingUtilities.processKeyBindings(e)) { 1443 e.consume(); 1444 return true; 1445 } 1446 return false; 1447 } 1448 }); 1449 try { 1450 Method setRequestFocusControllerM = java.security.AccessController.doPrivileged( 1451 new java.security.PrivilegedExceptionAction<Method>() { 1452 public Method run() throws Exception { 1453 Method method = 1454 Component.class.getDeclaredMethod("setRequestFocusController", 1455 sun.awt.RequestFocusController.class); 1456 method.setAccessible(true); 1457 return method; 1458 } 1459 }); 1460 setRequestFocusControllerM.invoke(null, JComponent.focusController); 1461 } catch (Exception e) { 1462 // perhaps we should log this 1463 assert false; 1464 } 1465 } 1466 }