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