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