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 = (LookAndFeel) cls.newInstance();

 530                     if (!laf.isSupportedLookAndFeel()) {
 531                         break;
 532                     }
 533                     return laf;
 534                 }
 535             }
 536         } catch (InstantiationException | IllegalAccessException ignore) {

 537         }
 538 
 539         throw new UnsupportedLookAndFeelException(name);
 540     }
 541 
 542     /**
 543      * Sets the current look and feel to {@code newLookAndFeel}.
 544      * If the current look and feel is {@code non-null} {@code
 545      * uninitialize} is invoked on it. If {@code newLookAndFeel} is
 546      * {@code non-null}, {@code initialize} is invoked on it followed
 547      * by {@code getDefaults}. The defaults returned from {@code
 548      * newLookAndFeel.getDefaults()} replace those of the defaults
 549      * from the previous look and feel. If the {@code newLookAndFeel} is
 550      * {@code null}, the look and feel defaults are set to {@code null}.
 551      * <p>
 552      * A value of {@code null} can be used to set the look and feel
 553      * to {@code null}. As the {@code LookAndFeel} is required for
 554      * most of Swing to function, setting the {@code LookAndFeel} to
 555      * {@code null} is strongly discouraged.
 556      * <p>
 557      * This is a JavaBeans bound property.
 558      *
 559      * @param newLookAndFeel {@code LookAndFeel} to install
 560      * @throws UnsupportedLookAndFeelException if
 561      *          {@code newLookAndFeel} is {@code non-null} and
 562      *          {@code newLookAndFeel.isSupportedLookAndFeel()} returns
 563      *          {@code false}
 564      * @see #getLookAndFeel
 565      */
 566     public static void setLookAndFeel(LookAndFeel newLookAndFeel)
 567         throws UnsupportedLookAndFeelException
 568     {
 569         if ((newLookAndFeel != null) && !newLookAndFeel.isSupportedLookAndFeel()) {
 570             String s = newLookAndFeel.toString() + " not supported on this platform";
 571             throw new UnsupportedLookAndFeelException(s);
 572         }
 573 
 574         LAFState lafState = getLAFState();
 575         LookAndFeel oldLookAndFeel = lafState.lookAndFeel;
 576         if (oldLookAndFeel != null) {
 577             oldLookAndFeel.uninitialize();
 578         }
 579 
 580         lafState.lookAndFeel = newLookAndFeel;
 581         if (newLookAndFeel != null) {
 582             sun.swing.DefaultLookup.setDefaultLookup(null);
 583             newLookAndFeel.initialize();
 584             lafState.setLookAndFeelDefaults(newLookAndFeel.getDefaults());
 585         }
 586         else {
 587             lafState.setLookAndFeelDefaults(null);
 588         }
 589 
 590         SwingPropertyChangeSupport changeSupport = lafState.
 591                                          getPropertyChangeSupport(false);
 592         if (changeSupport != null) {
 593             changeSupport.firePropertyChange("lookAndFeel", oldLookAndFeel,
 594                                              newLookAndFeel);
 595         }
 596     }
 597 
 598 
 599     /**
 600      * Loads the {@code LookAndFeel} specified by the given class
 601      * name, using the current thread's context class loader, and
 602      * passes it to {@code setLookAndFeel(LookAndFeel)}.
 603      *
 604      * @param className  a string specifying the name of the class that implements
 605      *        the look and feel
 606      * @exception ClassNotFoundException if the <code>LookAndFeel</code>
 607      *           class could not be found
 608      * @exception InstantiationException if a new instance of the class
 609      *          couldn't be created
 610      * @exception IllegalAccessException if the class or initializer isn't accessible
 611      * @exception UnsupportedLookAndFeelException if
 612      *          <code>lnf.isSupportedLookAndFeel()</code> is false
 613      * @throws ClassCastException if {@code className} does not identify
 614      *         a class that extends {@code LookAndFeel}
 615      */
 616     public static void setLookAndFeel(String className)
 617         throws ClassNotFoundException,
 618                InstantiationException,
 619                IllegalAccessException,
 620                UnsupportedLookAndFeelException
 621     {
 622         if ("javax.swing.plaf.metal.MetalLookAndFeel".equals(className)) {
 623             // Avoid reflection for the common case of metal.
 624             setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
 625         }
 626         else {
 627             Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
 628             setLookAndFeel((LookAndFeel)(lnfClass.newInstance()));









 629         }
 630     }
 631 
 632     /**
 633      * Returns the name of the <code>LookAndFeel</code> class that implements
 634      * the native system look and feel if there is one, otherwise
 635      * the name of the default cross platform <code>LookAndFeel</code>
 636      * class. This value can be overriden by setting the
 637      * <code>swing.systemlaf</code> system property.
 638      *
 639      * @return the <code>String</code> of the <code>LookAndFeel</code>
 640      *          class
 641      *
 642      * @see #setLookAndFeel
 643      * @see #getCrossPlatformLookAndFeelClassName
 644      */
 645     public static String getSystemLookAndFeelClassName() {
 646         String systemLAF = AccessController.doPrivileged(
 647                              new GetPropertyAction("swing.systemlaf"));
 648         if (systemLAF != null) {
 649             return systemLAF;
 650         }
 651         OSInfo.OSType osType = AccessController.doPrivileged(OSInfo.getOSTypeAction());
 652         if (osType == OSInfo.OSType.WINDOWS) {
 653             return "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
 654         } else {
 655             String desktop = AccessController.doPrivileged(new GetPropertyAction("sun.desktop"));
 656             Toolkit toolkit = Toolkit.getDefaultToolkit();
 657             if ("gnome".equals(desktop) &&
 658                     toolkit instanceof SunToolkit &&
 659                     ((SunToolkit) toolkit).isNativeGTKAvailable()) {
 660                 // May be set on Linux and Solaris boxs.
 661                 return "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
 662             }
 663             if (osType == OSInfo.OSType.MACOSX) {
 664                 if (toolkit.getClass() .getName()
 665                                        .equals("sun.lwawt.macosx.LWCToolkit")) {
 666                     return "com.apple.laf.AquaLookAndFeel";
 667                 }
 668             }
 669             if (osType == OSInfo.OSType.SOLARIS) {
 670                 return "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
 671             }
 672         }
 673         return getCrossPlatformLookAndFeelClassName();
 674     }
 675 
 676 
 677     /**
 678      * Returns the name of the <code>LookAndFeel</code> class that implements
 679      * the default cross platform look and feel -- the Java
 680      * Look and Feel (JLF).  This value can be overriden by setting the
 681      * <code>swing.crossplatformlaf</code> system property.
 682      *
 683      * @return  a string with the JLF implementation-class
 684      * @see #setLookAndFeel
 685      * @see #getSystemLookAndFeelClassName
 686      */
 687     public static String getCrossPlatformLookAndFeelClassName() {
 688         String laf = AccessController.doPrivileged(
 689                              new GetPropertyAction("swing.crossplatformlaf"));
 690         if (laf != null) {
 691             return laf;
 692         }
 693         return "javax.swing.plaf.metal.MetalLookAndFeel";
 694     }
 695 
 696 
 697     /**
 698      * Returns the defaults. The returned defaults resolve using the
 699      * logic specified in the class documentation.
 700      *
 701      * @return a <code>UIDefaults</code> object containing the default values
 702      */
 703     public static UIDefaults getDefaults() {
 704         maybeInitialize();
 705         return getLAFState().multiUIDefaults;
 706     }
 707 
 708     /**
 709      * Returns a font from the defaults. If the value for {@code key} is
 710      * not a {@code Font}, {@code null} is returned.
 711      *
 712      * @param key  an <code>Object</code> specifying the font
 713      * @return the <code>Font</code> object
 714      * @throws NullPointerException if {@code key} is {@code null}
 715      */
 716     public static Font getFont(Object key) {
 717         return getDefaults().getFont(key);
 718     }
 719 
 720     /**
 721      * Returns a font from the defaults that is appropriate
 722      * for the given locale. If the value for {@code key} is
 723      * not a {@code Font}, {@code null} is returned.
 724      *
 725      * @param key  an <code>Object</code> specifying the font
 726      * @param l the <code>Locale</code> for which the font is desired; refer
 727      *        to {@code UIDefaults} for details on how a {@code null}
 728      *        {@code Locale} is handled
 729      * @return the <code>Font</code> object
 730      * @throws NullPointerException if {@code key} is {@code null}
 731      * @since 1.4
 732      */
 733     public static Font getFont(Object key, Locale l) {
 734         return getDefaults().getFont(key,l);
 735     }
 736 
 737     /**
 738      * Returns a color from the defaults. If the value for {@code key} is
 739      * not a {@code Color}, {@code null} is returned.
 740      *
 741      * @param key  an <code>Object</code> specifying the color
 742      * @return the <code>Color</code> object
 743      * @throws NullPointerException if {@code key} is {@code null}
 744      */
 745     public static Color getColor(Object key) {
 746         return getDefaults().getColor(key);
 747     }
 748 
 749     /**
 750      * Returns a color from the defaults that is appropriate
 751      * for the given locale. If the value for {@code key} is
 752      * not a {@code Color}, {@code null} is returned.
 753      *
 754      * @param key  an <code>Object</code> specifying the color
 755      * @param l the <code>Locale</code> for which the color is desired; refer
 756      *        to {@code UIDefaults} for details on how a {@code null}
 757      *        {@code Locale} is handled
 758      * @return the <code>Color</code> object
 759      * @throws NullPointerException if {@code key} is {@code null}
 760      * @since 1.4
 761      */
 762     public static Color getColor(Object key, Locale l) {
 763         return getDefaults().getColor(key,l);
 764     }
 765 
 766     /**
 767      * Returns an <code>Icon</code> from the defaults. If the value for
 768      * {@code key} is not an {@code Icon}, {@code null} is returned.
 769      *
 770      * @param key  an <code>Object</code> specifying the icon
 771      * @return the <code>Icon</code> object
 772      * @throws NullPointerException if {@code key} is {@code null}
 773      */
 774     public static Icon getIcon(Object key) {
 775         return getDefaults().getIcon(key);
 776     }
 777 
 778     /**
 779      * Returns an <code>Icon</code> from the defaults that is appropriate
 780      * for the given locale. If the value for
 781      * {@code key} is not an {@code Icon}, {@code null} is returned.
 782      *
 783      * @param key  an <code>Object</code> specifying the icon
 784      * @param l the <code>Locale</code> for which the icon is desired; refer
 785      *        to {@code UIDefaults} for details on how a {@code null}
 786      *        {@code Locale} is handled
 787      * @return the <code>Icon</code> object
 788      * @throws NullPointerException if {@code key} is {@code null}
 789      * @since 1.4
 790      */
 791     public static Icon getIcon(Object key, Locale l) {
 792         return getDefaults().getIcon(key,l);
 793     }
 794 
 795     /**
 796      * Returns a border from the defaults. If the value for
 797      * {@code key} is not a {@code Border}, {@code null} is returned.
 798      *
 799      * @param key  an <code>Object</code> specifying the border
 800      * @return the <code>Border</code> object
 801      * @throws NullPointerException if {@code key} is {@code null}
 802      */
 803     public static Border getBorder(Object key) {
 804         return getDefaults().getBorder(key);
 805     }
 806 
 807     /**
 808      * Returns a border from the defaults that is appropriate
 809      * for the given locale.  If the value for
 810      * {@code key} is not a {@code Border}, {@code null} is returned.
 811      *
 812      * @param key  an <code>Object</code> specifying the border
 813      * @param l the <code>Locale</code> for which the border is desired; refer
 814      *        to {@code UIDefaults} for details on how a {@code null}
 815      *        {@code Locale} is handled
 816      * @return the <code>Border</code> object
 817      * @throws NullPointerException if {@code key} is {@code null}
 818      * @since 1.4
 819      */
 820     public static Border getBorder(Object key, Locale l) {
 821         return getDefaults().getBorder(key,l);
 822     }
 823 
 824     /**
 825      * Returns a string from the defaults. If the value for
 826      * {@code key} is not a {@code String}, {@code null} is returned.
 827      *
 828      * @param key  an <code>Object</code> specifying the string
 829      * @return the <code>String</code>
 830      * @throws NullPointerException if {@code key} is {@code null}
 831      */
 832     public static String getString(Object key) {
 833         return getDefaults().getString(key);
 834     }
 835 
 836     /**
 837      * Returns a string from the defaults that is appropriate for the
 838      * given locale.  If the value for
 839      * {@code key} is not a {@code String}, {@code null} is returned.
 840      *
 841      * @param key  an <code>Object</code> specifying the string
 842      * @param l the <code>Locale</code> for which the string is desired; refer
 843      *        to {@code UIDefaults} for details on how a {@code null}
 844      *        {@code Locale} is handled
 845      * @return the <code>String</code>
 846      * @since 1.4
 847      * @throws NullPointerException if {@code key} is {@code null}
 848      */
 849     public static String getString(Object key, Locale l) {
 850         return getDefaults().getString(key,l);
 851     }
 852 
 853     /**
 854      * Returns a string from the defaults that is appropriate for the
 855      * given locale.  If the value for
 856      * {@code key} is not a {@code String}, {@code null} is returned.
 857      *
 858      * @param key  an <code>Object</code> specifying the string
 859      * @param c {@code Component} used to determine the locale;
 860      *          {@code null} implies the default locale as
 861      *          returned by {@code Locale.getDefault()}
 862      * @return the <code>String</code>
 863      * @throws NullPointerException if {@code key} is {@code null}
 864      */
 865     static String getString(Object key, Component c) {
 866         Locale l = (c == null) ? Locale.getDefault() : c.getLocale();
 867         return getString(key, l);
 868     }
 869 
 870     /**
 871      * Returns an integer from the defaults. If the value for
 872      * {@code key} is not an {@code Integer}, or does not exist,
 873      * {@code 0} is returned.
 874      *
 875      * @param key  an <code>Object</code> specifying the int
 876      * @return the int
 877      * @throws NullPointerException if {@code key} is {@code null}
 878      */
 879     public static int getInt(Object key) {
 880         return getDefaults().getInt(key);
 881     }
 882 
 883     /**
 884      * Returns an integer from the defaults that is appropriate
 885      * for the given locale. If the value for
 886      * {@code key} is not an {@code Integer}, or does not exist,
 887      * {@code 0} is returned.
 888      *
 889      * @param key  an <code>Object</code> specifying the int
 890      * @param l the <code>Locale</code> for which the int is desired; refer
 891      *        to {@code UIDefaults} for details on how a {@code null}
 892      *        {@code Locale} is handled
 893      * @return the int
 894      * @throws NullPointerException if {@code key} is {@code null}
 895      * @since 1.4
 896      */
 897     public static int getInt(Object key, Locale l) {
 898         return getDefaults().getInt(key,l);
 899     }
 900 
 901     /**
 902      * Returns a boolean from the defaults which is associated with
 903      * the key value. If the key is not found or the key doesn't represent
 904      * a boolean value then {@code false} is returned.
 905      *
 906      * @param key  an <code>Object</code> specifying the key for the desired boolean value
 907      * @return the boolean value corresponding to the key
 908      * @throws NullPointerException if {@code key} is {@code null}
 909      * @since 1.4
 910      */
 911     public static boolean getBoolean(Object key) {
 912         return getDefaults().getBoolean(key);
 913     }
 914 
 915     /**
 916      * Returns a boolean from the defaults which is associated with
 917      * the key value and the given <code>Locale</code>. If the key is not
 918      * found or the key doesn't represent
 919      * a boolean value then {@code false} will be returned.
 920      *
 921      * @param key  an <code>Object</code> specifying the key for the desired
 922      *             boolean value
 923      * @param l the <code>Locale</code> for which the boolean is desired; refer
 924      *        to {@code UIDefaults} for details on how a {@code null}
 925      *        {@code Locale} is handled
 926      * @return the boolean value corresponding to the key
 927      * @throws NullPointerException if {@code key} is {@code null}
 928      * @since 1.4
 929      */
 930     public static boolean getBoolean(Object key, Locale l) {
 931         return getDefaults().getBoolean(key,l);
 932     }
 933 
 934     /**
 935      * Returns an <code>Insets</code> object from the defaults. If the value
 936      * for {@code key} is not an {@code Insets}, {@code null} is returned.
 937      *
 938      * @param key  an <code>Object</code> specifying the <code>Insets</code> object
 939      * @return the <code>Insets</code> object
 940      * @throws NullPointerException if {@code key} is {@code null}
 941      */
 942     public static Insets getInsets(Object key) {
 943         return getDefaults().getInsets(key);
 944     }
 945 
 946     /**
 947      * Returns an <code>Insets</code> object from the defaults that is
 948      * appropriate for the given locale. If the value
 949      * for {@code key} is not an {@code Insets}, {@code null} is returned.
 950      *
 951      * @param key  an <code>Object</code> specifying the <code>Insets</code> object
 952      * @param l the <code>Locale</code> for which the object is desired; refer
 953      *        to {@code UIDefaults} for details on how a {@code null}
 954      *        {@code Locale} is handled
 955      * @return the <code>Insets</code> object
 956      * @throws NullPointerException if {@code key} is {@code null}
 957      * @since 1.4
 958      */
 959     public static Insets getInsets(Object key, Locale l) {
 960         return getDefaults().getInsets(key,l);
 961     }
 962 
 963     /**
 964      * Returns a dimension from the defaults. If the value
 965      * for {@code key} is not a {@code Dimension}, {@code null} is returned.
 966      *
 967      * @param key  an <code>Object</code> specifying the dimension object
 968      * @return the <code>Dimension</code> object
 969      * @throws NullPointerException if {@code key} is {@code null}
 970      */
 971     public static Dimension getDimension(Object key) {
 972         return getDefaults().getDimension(key);
 973     }
 974 
 975     /**
 976      * Returns a dimension from the defaults that is appropriate
 977      * for the given locale. If the value
 978      * for {@code key} is not a {@code Dimension}, {@code null} is returned.
 979      *
 980      * @param key  an <code>Object</code> specifying the dimension object
 981      * @param l the <code>Locale</code> for which the object is desired; refer
 982      *        to {@code UIDefaults} for details on how a {@code null}
 983      *        {@code Locale} is handled
 984      * @return the <code>Dimension</code> object
 985      * @throws NullPointerException if {@code key} is {@code null}
 986      * @since 1.4
 987      */
 988     public static Dimension getDimension(Object key, Locale l) {
 989         return getDefaults().getDimension(key,l);
 990     }
 991 
 992     /**
 993      * Returns an object from the defaults.
 994      *
 995      * @param key  an <code>Object</code> specifying the desired object
 996      * @return the <code>Object</code>
 997      * @throws NullPointerException if {@code key} is {@code null}
 998      */
 999     public static Object get(Object key) {
1000         return getDefaults().get(key);
1001     }
1002 
1003     /**
1004      * Returns an object from the defaults that is appropriate for
1005      * the given locale.
1006      *
1007      * @param key  an <code>Object</code> specifying the desired object
1008      * @param l the <code>Locale</code> for which the object is desired; refer
1009      *        to {@code UIDefaults} for details on how a {@code null}
1010      *        {@code Locale} is handled
1011      * @return the <code>Object</code>
1012      * @throws NullPointerException if {@code key} is {@code null}
1013      * @since 1.4
1014      */
1015     public static Object get(Object key, Locale l) {
1016         return getDefaults().get(key,l);
1017     }
1018 
1019     /**
1020      * Stores an object in the developer defaults. This is a cover method
1021      * for {@code getDefaults().put(key, value)}. This only effects the
1022      * developer defaults, not the system or look and feel defaults.
1023      *
1024      * @param key    an <code>Object</code> specifying the retrieval key
1025      * @param value  the <code>Object</code> to store; refer to
1026      *               {@code UIDefaults} for details on how {@code null} is
1027      *               handled
1028      * @return the <code>Object</code> returned by {@link UIDefaults#put}
1029      * @throws NullPointerException if {@code key} is {@code null}
1030      * @see UIDefaults#put
1031      */
1032     public static Object put(Object key, Object value) {
1033         return getDefaults().put(key, value);
1034     }
1035 
1036     /**
1037      * Returns the appropriate {@code ComponentUI} implementation for
1038      * {@code target}. Typically, this is a cover for
1039      * {@code getDefaults().getUI(target)}. However, if an auxiliary
1040      * look and feel has been installed, this first invokes
1041      * {@code getUI(target)} on the multiplexing look and feel's
1042      * defaults, and returns that value if it is {@code non-null}.
1043      *
1044      * @param target the <code>JComponent</code> to return the
1045      *        {@code ComponentUI} for
1046      * @return the <code>ComponentUI</code> object for {@code target}
1047      * @throws NullPointerException if {@code target} is {@code null}
1048      * @see UIDefaults#getUI
1049      */
1050     public static ComponentUI getUI(JComponent target) {
1051         maybeInitialize();
1052         maybeInitializeFocusPolicy(target);
1053         ComponentUI ui = null;
1054         LookAndFeel multiLAF = getLAFState().multiLookAndFeel;
1055         if (multiLAF != null) {
1056             // This can return null if the multiplexing look and feel
1057             // doesn't support a particular UI.
1058             ui = multiLAF.getDefaults().getUI(target);
1059         }
1060         if (ui == null) {
1061             ui = getDefaults().getUI(target);
1062         }
1063         return ui;
1064     }
1065 
1066 
1067     /**
1068      * Returns the {@code UIDefaults} from the current look and feel,
1069      * that were obtained at the time the look and feel was installed.
1070      * <p>
1071      * In general, developers should use the {@code UIDefaults} returned from
1072      * {@code getDefaults()}. As the current look and feel may expect
1073      * certain values to exist, altering the {@code UIDefaults} returned
1074      * from this method could have unexpected results.
1075      *
1076      * @return <code>UIDefaults</code> from the current look and feel
1077      * @see #getDefaults
1078      * @see #setLookAndFeel(LookAndFeel)
1079      * @see LookAndFeel#getDefaults
1080      */
1081     public static UIDefaults getLookAndFeelDefaults() {
1082         maybeInitialize();
1083         return getLAFState().getLookAndFeelDefaults();
1084     }
1085 
1086     /**
1087      * Finds the Multiplexing <code>LookAndFeel</code>.
1088      */
1089     private static LookAndFeel getMultiLookAndFeel() {
1090         LookAndFeel multiLookAndFeel = getLAFState().multiLookAndFeel;
1091         if (multiLookAndFeel == null) {
1092             String defaultName = "javax.swing.plaf.multi.MultiLookAndFeel";
1093             String className = getLAFState().swingProps.getProperty(multiplexingLAFKey, defaultName);
1094             try {
1095                 Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
1096                 multiLookAndFeel = (LookAndFeel)lnfClass.newInstance();

1097             } catch (Exception exc) {
1098                 System.err.println("UIManager: failed loading " + className);
1099             }
1100         }
1101         return multiLookAndFeel;
1102     }
1103 
1104     /**
1105      * Adds a <code>LookAndFeel</code> to the list of auxiliary look and feels.
1106      * The auxiliary look and feels tell the multiplexing look and feel what
1107      * other <code>LookAndFeel</code> classes for a component instance are to be used
1108      * in addition to the default <code>LookAndFeel</code> class when creating a
1109      * multiplexing UI.  The change will only take effect when a new
1110      * UI class is created or when the default look and feel is changed
1111      * on a component instance.
1112      * <p>Note these are not the same as the installed look and feels.
1113      *
1114      * @param laf the <code>LookAndFeel</code> object
1115      * @see #removeAuxiliaryLookAndFeel
1116      * @see #setLookAndFeel
1117      * @see #getAuxiliaryLookAndFeels
1118      * @see #getInstalledLookAndFeels
1119      */
1120     public static void addAuxiliaryLookAndFeel(LookAndFeel laf) {
1121         maybeInitialize();
1122 
1123         if (!laf.isSupportedLookAndFeel()) {
1124             // Ideally we would throw an exception here, but it's too late
1125             // for that.
1126             return;
1127         }
1128         Vector<LookAndFeel> v = getLAFState().auxLookAndFeels;
1129         if (v == null) {
1130             v = new Vector<LookAndFeel>();
1131         }
1132 
1133         if (!v.contains(laf)) {
1134             v.addElement(laf);
1135             laf.initialize();
1136             getLAFState().auxLookAndFeels = v;
1137 
1138             if (getLAFState().multiLookAndFeel == null) {
1139                 getLAFState().multiLookAndFeel = getMultiLookAndFeel();
1140             }
1141         }
1142     }
1143 
1144     /**
1145      * Removes a <code>LookAndFeel</code> from the list of auxiliary look and feels.
1146      * The auxiliary look and feels tell the multiplexing look and feel what
1147      * other <code>LookAndFeel</code> classes for a component instance are to be used
1148      * in addition to the default <code>LookAndFeel</code> class when creating a
1149      * multiplexing UI.  The change will only take effect when a new
1150      * UI class is created or when the default look and feel is changed
1151      * on a component instance.
1152      * <p>Note these are not the same as the installed look and feels.
1153      *
1154      * @param laf the {@code LookAndFeel} to be removed
1155      * @return true if the <code>LookAndFeel</code> was removed from the list
1156      * @see #removeAuxiliaryLookAndFeel
1157      * @see #getAuxiliaryLookAndFeels
1158      * @see #setLookAndFeel
1159      * @see #getInstalledLookAndFeels
1160      */
1161     public static boolean removeAuxiliaryLookAndFeel(LookAndFeel laf) {
1162         maybeInitialize();
1163 
1164         boolean result;
1165 
1166         Vector<LookAndFeel> v = getLAFState().auxLookAndFeels;
1167         if ((v == null) || (v.size() == 0)) {
1168             return false;
1169         }
1170 
1171         result = v.removeElement(laf);
1172         if (result) {
1173             if (v.size() == 0) {
1174                 getLAFState().auxLookAndFeels = null;
1175                 getLAFState().multiLookAndFeel = null;
1176             } else {
1177                 getLAFState().auxLookAndFeels = v;
1178             }
1179         }
1180         laf.uninitialize();
1181 
1182         return result;
1183     }
1184 
1185     /**
1186      * Returns the list of auxiliary look and feels (can be <code>null</code>).
1187      * The auxiliary look and feels tell the multiplexing look and feel what
1188      * other <code>LookAndFeel</code> classes for a component instance are
1189      * to be used in addition to the default LookAndFeel class when creating a
1190      * multiplexing UI.
1191      * <p>Note these are not the same as the installed look and feels.
1192      *
1193      * @return list of auxiliary <code>LookAndFeel</code>s or <code>null</code>
1194      * @see #addAuxiliaryLookAndFeel
1195      * @see #removeAuxiliaryLookAndFeel
1196      * @see #setLookAndFeel
1197      * @see #getInstalledLookAndFeels
1198      */
1199     public static LookAndFeel[] getAuxiliaryLookAndFeels() {
1200         maybeInitialize();
1201 
1202         Vector<LookAndFeel> v = getLAFState().auxLookAndFeels;
1203         if ((v == null) || (v.size() == 0)) {
1204             return null;
1205         }
1206         else {
1207             LookAndFeel[] rv = new LookAndFeel[v.size()];
1208             for (int i = 0; i < rv.length; i++) {
1209                 rv[i] = v.elementAt(i);
1210             }
1211             return rv;
1212         }
1213     }
1214 
1215 
1216     /**
1217      * Adds a <code>PropertyChangeListener</code> to the listener list.
1218      * The listener is registered for all properties.
1219      *
1220      * @param listener  the <code>PropertyChangeListener</code> to be added
1221      * @see java.beans.PropertyChangeSupport
1222      */
1223     public static void addPropertyChangeListener(PropertyChangeListener listener)
1224     {
1225         synchronized (classLock) {
1226             getLAFState().getPropertyChangeSupport(true).
1227                              addPropertyChangeListener(listener);
1228         }
1229     }
1230 
1231 
1232     /**
1233      * Removes a <code>PropertyChangeListener</code> from the listener list.
1234      * This removes a <code>PropertyChangeListener</code> that was registered
1235      * for all properties.
1236      *
1237      * @param listener  the <code>PropertyChangeListener</code> to be removed
1238      * @see java.beans.PropertyChangeSupport
1239      */
1240     public static void removePropertyChangeListener(PropertyChangeListener listener)
1241     {
1242         synchronized (classLock) {
1243             getLAFState().getPropertyChangeSupport(true).
1244                           removePropertyChangeListener(listener);
1245         }
1246     }
1247 
1248 
1249     /**
1250      * Returns an array of all the <code>PropertyChangeListener</code>s added
1251      * to this UIManager with addPropertyChangeListener().
1252      *
1253      * @return all of the <code>PropertyChangeListener</code>s added or an empty
1254      *         array if no listeners have been added
1255      * @since 1.4
1256      */
1257     public static PropertyChangeListener[] getPropertyChangeListeners() {
1258         synchronized(classLock) {
1259             return getLAFState().getPropertyChangeSupport(true).
1260                       getPropertyChangeListeners();
1261         }
1262     }
1263 
1264     private static Properties loadSwingProperties()
1265     {
1266         /* Don't bother checking for Swing properties if untrusted, as
1267          * there's no way to look them up without triggering SecurityExceptions.
1268          */
1269         if (UIManager.class.getClassLoader() != null) {
1270             return new Properties();
1271         }
1272         else {
1273             final Properties props = new Properties();
1274 
1275             java.security.AccessController.doPrivileged(
1276                 new java.security.PrivilegedAction<Object>() {
1277                 public Object run() {
1278                     OSInfo.OSType osType = AccessController.doPrivileged(OSInfo.getOSTypeAction());
1279                     if (osType == OSInfo.OSType.MACOSX) {
1280                         props.put(defaultLAFKey, getSystemLookAndFeelClassName());
1281                     }
1282 
1283                     try {
1284                         File file = new File(makeSwingPropertiesFilename());
1285 
1286                         if (file.exists()) {
1287                             // InputStream has been buffered in Properties
1288                             // class
1289                             FileInputStream ins = new FileInputStream(file);
1290                             props.load(ins);
1291                             ins.close();
1292                         }
1293                     }
1294                     catch (Exception e) {
1295                         // No such file, or file is otherwise non-readable.
1296                     }
1297 
1298                     // Check whether any properties were overridden at the
1299                     // command line.
1300                     checkProperty(props, defaultLAFKey);
1301                     checkProperty(props, auxiliaryLAFsKey);
1302                     checkProperty(props, multiplexingLAFKey);
1303                     checkProperty(props, installedLAFsKey);
1304                     checkProperty(props, disableMnemonicKey);
1305                     // Don't care about return value.
1306                     return null;
1307                 }
1308             });
1309             return props;
1310         }
1311     }
1312 
1313     private static void checkProperty(Properties props, String key) {
1314         // No need to do catch the SecurityException here, this runs
1315         // in a doPrivileged.
1316         String value = System.getProperty(key);
1317         if (value != null) {
1318             props.put(key, value);
1319         }
1320     }
1321 
1322 
1323     /**
1324      * If a <code>swing.properties</code> file exist and it has a
1325      * <code>swing.installedlafs</code> property
1326      * then initialize the <code>installedLAFs</code> field.
1327      *
1328      * @see #getInstalledLookAndFeels
1329      */
1330     private static void initializeInstalledLAFs(Properties swingProps)
1331     {
1332         String ilafsString = swingProps.getProperty(installedLAFsKey);
1333         if (ilafsString == null) {
1334             return;
1335         }
1336 
1337         /* Create a vector that contains the value of the swing.installedlafs
1338          * property.  For example given "swing.installedlafs=motif,windows"
1339          * lafs = {"motif", "windows"}.
1340          */
1341         Vector<String> lafs = new Vector<String>();
1342         StringTokenizer st = new StringTokenizer(ilafsString, ",", false);
1343         while (st.hasMoreTokens()) {
1344             lafs.addElement(st.nextToken());
1345         }
1346 
1347         /* Look up the name and class for each name in the "swing.installedlafs"
1348          * list.  If they both exist then add a LookAndFeelInfo to
1349          * the installedLafs array.
1350          */
1351         Vector<LookAndFeelInfo> ilafs = new Vector<LookAndFeelInfo>(lafs.size());
1352         for (String laf : lafs) {
1353             String name = swingProps.getProperty(makeInstalledLAFKey(laf, "name"), laf);
1354             String cls = swingProps.getProperty(makeInstalledLAFKey(laf, "class"));
1355             if (cls != null) {
1356                 ilafs.addElement(new LookAndFeelInfo(name, cls));
1357             }
1358         }
1359 
1360         LookAndFeelInfo[] installedLAFs = new LookAndFeelInfo[ilafs.size()];
1361         for(int i = 0; i < ilafs.size(); i++) {
1362             installedLAFs[i] = ilafs.elementAt(i);
1363         }
1364         getLAFState().installedLAFs = installedLAFs;
1365     }
1366 
1367 
1368     /**
1369      * If the user has specified a default look and feel, use that.
1370      * Otherwise use the look and feel that's native to this platform.
1371      * If this code is called after the application has explicitly
1372      * set it's look and feel, do nothing.
1373      *
1374      * @see #maybeInitialize
1375      */
1376     private static void initializeDefaultLAF(Properties swingProps)
1377     {
1378         if (getLAFState().lookAndFeel != null) {
1379             return;
1380         }
1381 
1382         // Try to get default LAF from system property, then from AppContext
1383         // (6653395), then use cross-platform one by default.
1384         String lafName = null;
1385         @SuppressWarnings("unchecked")
1386         HashMap<Object, String> lafData =
1387                 (HashMap) AppContext.getAppContext().remove("swing.lafdata");
1388         if (lafData != null) {
1389             lafName = lafData.remove("defaultlaf");
1390         }
1391         if (lafName == null) {
1392             lafName = getCrossPlatformLookAndFeelClassName();
1393         }
1394         lafName = swingProps.getProperty(defaultLAFKey, lafName);
1395 
1396         try {
1397             setLookAndFeel(lafName);
1398         } catch (Exception e) {
1399             throw new Error("Cannot load " + lafName);
1400         }
1401 
1402         // Set any properties passed through AppContext (6653395).
1403         if (lafData != null) {
1404             for (Object key: lafData.keySet()) {
1405                 UIManager.put(key, lafData.get(key));
1406             }
1407         }
1408     }
1409 
1410 
1411     private static void initializeAuxiliaryLAFs(Properties swingProps)
1412     {
1413         String auxLookAndFeelNames = swingProps.getProperty(auxiliaryLAFsKey);
1414         if (auxLookAndFeelNames == null) {
1415             return;
1416         }
1417 
1418         Vector<LookAndFeel> auxLookAndFeels = new Vector<LookAndFeel>();
1419 
1420         StringTokenizer p = new StringTokenizer(auxLookAndFeelNames,",");
1421         String factoryName;
1422 
1423         /* Try to load each LookAndFeel subclass in the list.
1424          */
1425 
1426         while (p.hasMoreTokens()) {
1427             String className = p.nextToken();
1428             try {
1429                 Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
1430                 LookAndFeel newLAF = (LookAndFeel)lnfClass.newInstance();

1431                 newLAF.initialize();
1432                 auxLookAndFeels.addElement(newLAF);
1433             }
1434             catch (Exception e) {
1435                 System.err.println("UIManager: failed loading auxiliary look and feel " + className);
1436             }
1437         }
1438 
1439         /* If there were problems and no auxiliary look and feels were
1440          * loaded, make sure we reset auxLookAndFeels to null.
1441          * Otherwise, we are going to use the MultiLookAndFeel to get
1442          * all component UI's, so we need to load it now.
1443          */
1444         if (auxLookAndFeels.size() == 0) {
1445             auxLookAndFeels = null;
1446         }
1447         else {
1448             getLAFState().multiLookAndFeel = getMultiLookAndFeel();
1449             if (getLAFState().multiLookAndFeel == null) {
1450                 auxLookAndFeels = null;
1451             }
1452         }
1453 
1454         getLAFState().auxLookAndFeels = auxLookAndFeels;
1455     }
1456 
1457 
1458     private static void initializeSystemDefaults(Properties swingProps) {
1459         getLAFState().swingProps = swingProps;
1460     }
1461 
1462 
1463     /*
1464      * This method is called before any code that depends on the
1465      * <code>AppContext</code> specific LAFState object runs.  When the AppContext
1466      * corresponds to a set of applets it's possible for this method
1467      * to be re-entered, which is why we grab a lock before calling
1468      * initialize().
1469      */
1470     private static void maybeInitialize() {
1471         synchronized (classLock) {
1472             if (!getLAFState().initialized) {
1473                 getLAFState().initialized = true;
1474                 initialize();
1475             }
1476         }
1477     }
1478 
1479     /*
1480      * Sets default swing focus traversal policy.
1481      */
1482     @SuppressWarnings("deprecation")
1483     private static void maybeInitializeFocusPolicy(JComponent comp) {
1484         // Check for JRootPane which indicates that a swing toplevel
1485         // is coming, in which case a swing default focus policy
1486         // should be instatiated. See 7125044.
1487         if (comp instanceof JRootPane) {
1488             synchronized (classLock) {
1489                 if (!getLAFState().focusPolicyInitialized) {
1490                     getLAFState().focusPolicyInitialized = true;
1491 
1492                     if (FocusManager.isFocusManagerEnabled()) {
1493                         KeyboardFocusManager.getCurrentKeyboardFocusManager().
1494                             setDefaultFocusTraversalPolicy(
1495                                 new LayoutFocusTraversalPolicy());
1496                     }
1497                 }
1498             }
1499         }
1500     }
1501 
1502     /*
1503      * Only called by maybeInitialize().
1504      */
1505     private static void initialize() {
1506         Properties swingProps = loadSwingProperties();
1507         initializeSystemDefaults(swingProps);
1508         initializeDefaultLAF(swingProps);
1509         initializeAuxiliaryLAFs(swingProps);
1510         initializeInstalledLAFs(swingProps);
1511 
1512         // Install Swing's PaintEventDispatcher
1513         if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) {
1514             sun.awt.PaintEventDispatcher.setPaintEventDispatcher(
1515                                         new SwingPaintEventDispatcher());
1516         }
1517         // Install a hook that will be invoked if no one consumes the
1518         // KeyEvent.  If the source isn't a JComponent this will process
1519         // key bindings, if the source is a JComponent it implies that
1520         // processKeyEvent was already invoked and thus no need to process
1521         // the bindings again, unless the Component is disabled, in which
1522         // case KeyEvents will no longer be dispatched to it so that we
1523         // handle it here.
1524         KeyboardFocusManager.getCurrentKeyboardFocusManager().
1525                 addKeyEventPostProcessor(new KeyEventPostProcessor() {
1526                     public boolean postProcessKeyEvent(KeyEvent e) {
1527                         Component c = e.getComponent();
1528 
1529                         if ((!(c instanceof JComponent) ||
1530                              (c != null && !c.isEnabled())) &&
1531                                 JComponent.KeyboardState.shouldProcess(e) &&
1532                                 SwingUtilities.processKeyBindings(e)) {
1533                             e.consume();
1534                             return true;
1535                         }
1536                         return false;
1537                     }
1538                 });
1539         AWTAccessor.getComponentAccessor().
1540             setRequestFocusController(JComponent.focusController);
1541     }
1542 }
--- EOF ---