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