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