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