1/*
2 * Copyright (c) 1997, 2017, 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
26package javax.swing;
27
28
29import javax.swing.plaf.ComponentUI;
30import javax.swing.border.*;
31import javax.swing.event.SwingPropertyChangeSupport;
32
33import java.io.IOException;
34import java.io.InputStream;
35import java.io.UncheckedIOException;
36import java.lang.reflect.*;
37import java.util.HashMap;
38import java.util.Map;
39import java.util.Enumeration;
40import java.util.Hashtable;
41import java.util.ResourceBundle;
42import java.util.Locale;
43import java.util.Vector;
44import java.util.MissingResourceException;
45import java.awt.Font;
46import java.awt.Color;
47import java.awt.Insets;
48import java.awt.Dimension;
49import java.beans.PropertyChangeListener;
50import java.security.AccessController;
51import java.security.AccessControlContext;
52import java.security.PrivilegedAction;
53
54import sun.reflect.misc.MethodUtil;
55import sun.reflect.misc.ReflectUtil;
56import sun.swing.SwingAccessor;
57import sun.swing.SwingUtilities2;
58
59/**
60 * A table of defaults for Swing components.  Applications can set/get
61 * default values via the <code>UIManager</code>.
62 * <p>
63 * <strong>Warning:</strong>
64 * Serialized objects of this class will not be compatible with
65 * future Swing releases. The current serialization support is
66 * appropriate for short term storage or RMI between applications running
67 * the same version of Swing.  As of 1.4, support for long term storage
68 * of all JavaBeans&trade;
69 * has been added to the <code>java.beans</code> package.
70 * Please see {@link java.beans.XMLEncoder}.
71 *
72 * @see UIManager
73 * @author Hans Muller
74 * @since 1.2
75 */
76@SuppressWarnings("serial") // Same-version serialization only
77public class UIDefaults extends Hashtable<Object,Object>
78{
79    private static final Object PENDING = new Object();
80
81    private SwingPropertyChangeSupport changeSupport;
82
83    private Vector<String> resourceBundles;
84
85    private Locale defaultLocale = Locale.getDefault();
86
87    /**
88     * Maps from a Locale to a cached Map of the ResourceBundle. This is done
89     * so as to avoid an exception being thrown when a value is asked for.
90     * Access to this should be done while holding a lock on the
91     * UIDefaults, eg synchronized(this).
92     */
93    private Map<Locale, Map<String, Object>> resourceCache;
94
95    static {
96        SwingAccessor.setUIDefaultsAccessor(UIDefaults::addInternalBundle);
97    }
98
99    /**
100     * Creates an empty defaults table.
101     */
102    public UIDefaults() {
103        this(700, .75f);
104    }
105
106    /**
107     * Creates an empty defaults table with the specified initial capacity and
108     * load factor.
109     *
110     * @param initialCapacity   the initial capacity of the defaults table
111     * @param loadFactor        the load factor of the defaults table
112     * @see java.util.Hashtable
113     * @since 1.6
114     */
115    public UIDefaults(int initialCapacity, float loadFactor) {
116        super(initialCapacity, loadFactor);
117        resourceCache = new HashMap<Locale, Map<String, Object>>();
118    }
119
120
121    /**
122     * Creates a defaults table initialized with the specified
123     * key/value pairs.  For example:
124     * <pre>
125        Object[] uiDefaults = {
126             "Font", new Font("Dialog", Font.BOLD, 12),
127            "Color", Color.red,
128             "five", Integer.valueOf(5)
129        }
130        UIDefaults myDefaults = new UIDefaults(uiDefaults);
131     * </pre>
132     * @param keyValueList  an array of objects containing the key/value
133     *          pairs
134     */
135    public UIDefaults(Object[] keyValueList) {
136        super(keyValueList.length / 2);
137        for(int i = 0; i < keyValueList.length; i += 2) {
138            super.put(keyValueList[i], keyValueList[i + 1]);
139        }
140    }
141
142    /**
143     * Returns the value for key.  If the value is a
144     * <code>UIDefaults.LazyValue</code> then the real
145     * value is computed with <code>LazyValue.createValue()</code>,
146     * the table entry is replaced, and the real value is returned.
147     * If the value is an <code>UIDefaults.ActiveValue</code>
148     * the table entry is not replaced - the value is computed
149     * with <code>ActiveValue.createValue()</code> for each
150     * <code>get()</code> call.
151     *
152     * If the key is not found in the table then it is searched for in the list
153     * of resource bundles maintained by this object.  The resource bundles are
154     * searched most recently added first using the locale returned by
155     * <code>getDefaultLocale</code>.  <code>LazyValues</code> and
156     * <code>ActiveValues</code> are not supported in the resource bundles.
157
158     *
159     * @param key the desired key
160     * @return the value for <code>key</code>
161     * @see LazyValue
162     * @see ActiveValue
163     * @see java.util.Hashtable#get
164     * @see #getDefaultLocale
165     * @see #addResourceBundle
166     * @since 1.4
167     */
168    public Object get(Object key) {
169        Object value = getFromHashtable( key );
170        return (value != null) ? value : getFromResourceBundle(key, null);
171    }
172
173    /**
174     * Looks up the given key in our Hashtable and resolves LazyValues
175     * or ActiveValues.
176     */
177    private Object getFromHashtable(final Object key) {
178        /* Quickly handle the common case, without grabbing
179         * a lock.
180         */
181        Object value = super.get(key);
182        if ((value != PENDING) &&
183            !(value instanceof ActiveValue) &&
184            !(value instanceof LazyValue)) {
185            return value;
186        }
187
188        /* If the LazyValue for key is being constructed by another
189         * thread then wait and then return the new value, otherwise drop
190         * the lock and construct the ActiveValue or the LazyValue.
191         * We use the special value PENDING to mark LazyValues that
192         * are being constructed.
193         */
194        synchronized(this) {
195            value = super.get(key);
196            if (value == PENDING) {
197                do {
198                    try {
199                        this.wait();
200                    }
201                    catch (InterruptedException e) {
202                    }
203                    value = super.get(key);
204                }
205                while(value == PENDING);
206                return value;
207            }
208            else if (value instanceof LazyValue) {
209                super.put(key, PENDING);
210            }
211            else if (!(value instanceof ActiveValue)) {
212                return value;
213            }
214        }
215
216        /* At this point we know that the value of key was
217         * a LazyValue or an ActiveValue.
218         */
219        if (value instanceof LazyValue) {
220            try {
221                /* If an exception is thrown we'll just put the LazyValue
222                 * back in the table.
223                 */
224                value = ((LazyValue)value).createValue(this);
225            }
226            finally {
227                synchronized(this) {
228                    if (value == null) {
229                        super.remove(key);
230                    }
231                    else {
232                        super.put(key, value);
233                    }
234                    this.notifyAll();
235                }
236            }
237        }
238        else {
239            value = ((ActiveValue)value).createValue(this);
240        }
241
242        return value;
243    }
244
245
246    /**
247     * Returns the value for key associated with the given locale.
248     * If the value is a <code>UIDefaults.LazyValue</code> then the real
249     * value is computed with <code>LazyValue.createValue()</code>,
250     * the table entry is replaced, and the real value is returned.
251     * If the value is an <code>UIDefaults.ActiveValue</code>
252     * the table entry is not replaced - the value is computed
253     * with <code>ActiveValue.createValue()</code> for each
254     * <code>get()</code> call.
255     *
256     * If the key is not found in the table then it is searched for in the list
257     * of resource bundles maintained by this object.  The resource bundles are
258     * searched most recently added first using the given locale.
259     * <code>LazyValues</code> and <code>ActiveValues</code> are not supported
260     * in the resource bundles.
261     *
262     * @param key the desired key
263     * @param l the desired <code>locale</code>
264     * @return the value for <code>key</code>
265     * @see LazyValue
266     * @see ActiveValue
267     * @see java.util.Hashtable#get
268     * @see #addResourceBundle
269     * @since 1.4
270     */
271    public Object get(Object key, Locale l) {
272        Object value = getFromHashtable( key );
273        return (value != null) ? value : getFromResourceBundle(key, l);
274    }
275
276    /**
277     * Looks up given key in our resource bundles.
278     */
279    private Object getFromResourceBundle(Object key, Locale l) {
280
281        if( resourceBundles == null ||
282            resourceBundles.isEmpty() ||
283            !(key instanceof String) ) {
284            return null;
285        }
286
287        // A null locale means use the default locale.
288        if( l == null ) {
289            if( defaultLocale == null )
290                return null;
291            else
292                l = defaultLocale;
293        }
294
295        synchronized(this) {
296            return getResourceCache(l).get(key);
297        }
298    }
299
300    /**
301     * Returns a Map of the known resources for the given locale.
302     */
303    private Map<String, Object> getResourceCache(Locale l) {
304        Map<String, Object> values = resourceCache.get(l);
305
306        if (values == null) {
307            values = new TextAndMnemonicHashMap();
308            for (int i=resourceBundles.size()-1; i >= 0; i--) {
309                String bundleName = resourceBundles.get(i);
310                try {
311                    ResourceBundle b;
312                    if (isDesktopResourceBundle(bundleName)) {
313                        // load resource bundle from java.desktop module
314                        b = ResourceBundle.getBundle(bundleName, l, UIDefaults.class.getModule());
315                    } else {
316                        b = ResourceBundle.getBundle(bundleName, l, ClassLoader.getSystemClassLoader());
317                    }
318                    Enumeration<String> keys = b.getKeys();
319
320                    while (keys.hasMoreElements()) {
321                        String key = keys.nextElement();
322
323                        if (values.get(key) == null) {
324                            Object value = b.getObject(key);
325
326                            values.put(key, value);
327                        }
328                    }
329                } catch( MissingResourceException mre ) {
330                    // Keep looking
331                }
332            }
333            resourceCache.put(l, values);
334        }
335        return values;
336    }
337
338    /*
339     * Test if the specified baseName of the ROOT locale is in java.desktop module.
340     * JDK always defines the resource bundle of the ROOT locale.
341     */
342    private static boolean isDesktopResourceBundle(String baseName) {
343        Module thisModule = UIDefaults.class.getModule();
344        return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
345            @Override
346            public Boolean run() {
347                Class<?> c = Class.forName(thisModule, baseName);
348                if (c != null) {
349                    return true;
350                } else {
351                    String resourceName = baseName.replace('.', '/') + ".properties";
352                    try (InputStream in = thisModule.getResourceAsStream(resourceName)) {
353                        return in != null;
354                    } catch (IOException e) {
355                        throw new UncheckedIOException(e);
356                    }
357                }
358            }
359        });
360    }
361
362    /**
363     * Sets the value of <code>key</code> to <code>value</code> for all locales.
364     * If <code>key</code> is a string and the new value isn't
365     * equal to the old one, fire a <code>PropertyChangeEvent</code>.
366     * If value is <code>null</code>, the key is removed from the table.
367     *
368     * @param key    the unique <code>Object</code> who's value will be used
369     *          to retrieve the data value associated with it
370     * @param value  the new <code>Object</code> to store as data under
371     *          that key
372     * @return the previous <code>Object</code> value, or <code>null</code>
373     * @see #putDefaults
374     * @see java.util.Hashtable#put
375     */
376    public Object put(Object key, Object value) {
377        Object oldValue = (value == null) ? super.remove(key) : super.put(key, value);
378        if (key instanceof String) {
379            firePropertyChange((String)key, oldValue, value);
380        }
381        return oldValue;
382    }
383
384
385    /**
386     * Puts all of the key/value pairs in the database and
387     * unconditionally generates one <code>PropertyChangeEvent</code>.
388     * The events oldValue and newValue will be <code>null</code> and its
389     * <code>propertyName</code> will be "UIDefaults".  The key/value pairs are
390     * added for all locales.
391     *
392     * @param keyValueList  an array of key/value pairs
393     * @see #put
394     * @see java.util.Hashtable#put
395     */
396    public void putDefaults(Object[] keyValueList) {
397        for(int i = 0, max = keyValueList.length; i < max; i += 2) {
398            Object value = keyValueList[i + 1];
399            if (value == null) {
400                super.remove(keyValueList[i]);
401            }
402            else {
403                super.put(keyValueList[i], value);
404            }
405        }
406        firePropertyChange("UIDefaults", null, null);
407    }
408
409
410    /**
411     * If the value of <code>key</code> is a <code>Font</code> return it,
412     * otherwise return <code>null</code>.
413     * @param key the desired key
414     * @return if the value for <code>key</code> is a <code>Font</code>,
415     *          return the <code>Font</code> object; otherwise return
416     *          <code>null</code>
417     */
418    public Font getFont(Object key) {
419        Object value = get(key);
420        return (value instanceof Font) ? (Font)value : null;
421    }
422
423
424    /**
425     * If the value of <code>key</code> for the given <code>Locale</code>
426     * is a <code>Font</code> return it, otherwise return <code>null</code>.
427     * @param key the desired key
428     * @param l the desired locale
429     * @return if the value for <code>key</code> and <code>Locale</code>
430     *          is a <code>Font</code>,
431     *          return the <code>Font</code> object; otherwise return
432     *          <code>null</code>
433     * @since 1.4
434     */
435    public Font getFont(Object key, Locale l) {
436        Object value = get(key,l);
437        return (value instanceof Font) ? (Font)value : null;
438    }
439
440    /**
441     * If the value of <code>key</code> is a <code>Color</code> return it,
442     * otherwise return <code>null</code>.
443     * @param key the desired key
444     * @return if the value for <code>key</code> is a <code>Color</code>,
445     *          return the <code>Color</code> object; otherwise return
446     *          <code>null</code>
447     */
448    public Color getColor(Object key) {
449        Object value = get(key);
450        return (value instanceof Color) ? (Color)value : null;
451    }
452
453
454    /**
455     * If the value of <code>key</code> for the given <code>Locale</code>
456     * is a <code>Color</code> return it, otherwise return <code>null</code>.
457     * @param key the desired key
458     * @param l the desired locale
459     * @return if the value for <code>key</code> and <code>Locale</code>
460     *          is a <code>Color</code>,
461     *          return the <code>Color</code> object; otherwise return
462     *          <code>null</code>
463     * @since 1.4
464     */
465    public Color getColor(Object key, Locale l) {
466        Object value = get(key,l);
467        return (value instanceof Color) ? (Color)value : null;
468    }
469
470
471    /**
472     * If the value of <code>key</code> is an <code>Icon</code> return it,
473     * otherwise return <code>null</code>.
474     * @param key the desired key
475     * @return if the value for <code>key</code> is an <code>Icon</code>,
476     *          return the <code>Icon</code> object; otherwise return
477     *          <code>null</code>
478     */
479    public Icon getIcon(Object key) {
480        Object value = get(key);
481        return (value instanceof Icon) ? (Icon)value : null;
482    }
483
484
485    /**
486     * If the value of <code>key</code> for the given <code>Locale</code>
487     * is an <code>Icon</code> return it, otherwise return <code>null</code>.
488     * @param key the desired key
489     * @param l the desired locale
490     * @return if the value for <code>key</code> and <code>Locale</code>
491     *          is an <code>Icon</code>,
492     *          return the <code>Icon</code> object; otherwise return
493     *          <code>null</code>
494     * @since 1.4
495     */
496    public Icon getIcon(Object key, Locale l) {
497        Object value = get(key,l);
498        return (value instanceof Icon) ? (Icon)value : null;
499    }
500
501
502    /**
503     * If the value of <code>key</code> is a <code>Border</code> return it,
504     * otherwise return <code>null</code>.
505     * @param key the desired key
506     * @return if the value for <code>key</code> is a <code>Border</code>,
507     *          return the <code>Border</code> object; otherwise return
508     *          <code>null</code>
509     */
510    public Border getBorder(Object key) {
511        Object value = get(key);
512        return (value instanceof Border) ? (Border)value : null;
513    }
514
515
516    /**
517     * If the value of <code>key</code> for the given <code>Locale</code>
518     * is a <code>Border</code> return it, otherwise return <code>null</code>.
519     * @param key the desired key
520     * @param l the desired locale
521     * @return if the value for <code>key</code> and <code>Locale</code>
522     *          is a <code>Border</code>,
523     *          return the <code>Border</code> object; otherwise return
524     *          <code>null</code>
525     * @since 1.4
526     */
527    public Border getBorder(Object key, Locale l)  {
528        Object value = get(key,l);
529        return (value instanceof Border) ? (Border)value : null;
530    }
531
532
533    /**
534     * If the value of <code>key</code> is a <code>String</code> return it,
535     * otherwise return <code>null</code>.
536     * @param key the desired key
537     * @return if the value for <code>key</code> is a <code>String</code>,
538     *          return the <code>String</code> object; otherwise return
539     *          <code>null</code>
540     */
541    public String getString(Object key) {
542        Object value = get(key);
543        return (value instanceof String) ? (String)value : null;
544    }
545
546    /**
547     * If the value of <code>key</code> for the given <code>Locale</code>
548     * is a <code>String</code> return it, otherwise return <code>null</code>.
549     * @param key the desired key
550     * @param l the desired <code>Locale</code>
551     * @return if the value for <code>key</code> for the given
552     *          <code>Locale</code> is a <code>String</code>,
553     *          return the <code>String</code> object; otherwise return
554     *          <code>null</code>
555     * @since 1.4
556     */
557    public String getString(Object key, Locale l) {
558        Object value = get(key,l);
559        return (value instanceof String) ? (String)value : null;
560    }
561
562    /**
563     * If the value of <code>key</code> is an <code>Integer</code> return its
564     * integer value, otherwise return 0.
565     * @param key the desired key
566     * @return if the value for <code>key</code> is an <code>Integer</code>,
567     *          return its value, otherwise return 0
568     */
569    public int getInt(Object key) {
570        Object value = get(key);
571        return (value instanceof Integer) ? ((Integer)value).intValue() : 0;
572    }
573
574
575    /**
576     * If the value of <code>key</code> for the given <code>Locale</code>
577     * is an <code>Integer</code> return its integer value, otherwise return 0.
578     * @param key the desired key
579     * @param l the desired locale
580     * @return if the value for <code>key</code> and <code>Locale</code>
581     *          is an <code>Integer</code>,
582     *          return its value, otherwise return 0
583     * @since 1.4
584     */
585    public int getInt(Object key, Locale l) {
586        Object value = get(key,l);
587        return (value instanceof Integer) ? ((Integer)value).intValue() : 0;
588    }
589
590
591    /**
592     * If the value of <code>key</code> is boolean, return the
593     * boolean value, otherwise return false.
594     *
595     * @param key an <code>Object</code> specifying the key for the desired boolean value
596     * @return if the value of <code>key</code> is boolean, return the
597     *         boolean value, otherwise return false.
598     * @since 1.4
599     */
600    public boolean getBoolean(Object key) {
601        Object value = get(key);
602        return (value instanceof Boolean) ? ((Boolean)value).booleanValue() : false;
603    }
604
605
606    /**
607     * If the value of <code>key</code> for the given <code>Locale</code>
608     * is boolean, return the boolean value, otherwise return false.
609     *
610     * @param key an <code>Object</code> specifying the key for the desired boolean value
611     * @param l the desired locale
612     * @return if the value for <code>key</code> and <code>Locale</code>
613     *         is boolean, return the
614     *         boolean value, otherwise return false.
615     * @since 1.4
616     */
617    public boolean getBoolean(Object key, Locale l) {
618        Object value = get(key,l);
619        return (value instanceof Boolean) ? ((Boolean)value).booleanValue() : false;
620    }
621
622
623    /**
624     * If the value of <code>key</code> is an <code>Insets</code> return it,
625     * otherwise return <code>null</code>.
626     * @param key the desired key
627     * @return if the value for <code>key</code> is an <code>Insets</code>,
628     *          return the <code>Insets</code> object; otherwise return
629     *          <code>null</code>
630     */
631    public Insets getInsets(Object key) {
632        Object value = get(key);
633        return (value instanceof Insets) ? (Insets)value : null;
634    }
635
636
637    /**
638     * If the value of <code>key</code> for the given <code>Locale</code>
639     * is an <code>Insets</code> return it, otherwise return <code>null</code>.
640     * @param key the desired key
641     * @param l the desired locale
642     * @return if the value for <code>key</code> and <code>Locale</code>
643     *          is an <code>Insets</code>,
644     *          return the <code>Insets</code> object; otherwise return
645     *          <code>null</code>
646     * @since 1.4
647     */
648    public Insets getInsets(Object key, Locale l) {
649        Object value = get(key,l);
650        return (value instanceof Insets) ? (Insets)value : null;
651    }
652
653
654    /**
655     * If the value of <code>key</code> is a <code>Dimension</code> return it,
656     * otherwise return <code>null</code>.
657     * @param key the desired key
658     * @return if the value for <code>key</code> is a <code>Dimension</code>,
659     *          return the <code>Dimension</code> object; otherwise return
660     *          <code>null</code>
661     */
662    public Dimension getDimension(Object key) {
663        Object value = get(key);
664        return (value instanceof Dimension) ? (Dimension)value : null;
665    }
666
667
668    /**
669     * If the value of <code>key</code> for the given <code>Locale</code>
670     * is a <code>Dimension</code> return it, otherwise return <code>null</code>.
671     * @param key the desired key
672     * @param l the desired locale
673     * @return if the value for <code>key</code> and <code>Locale</code>
674     *          is a <code>Dimension</code>,
675     *          return the <code>Dimension</code> object; otherwise return
676     *          <code>null</code>
677     * @since 1.4
678     */
679    public Dimension getDimension(Object key, Locale l) {
680        Object value = get(key,l);
681        return (value instanceof Dimension) ? (Dimension)value : null;
682    }
683
684
685    /**
686     * The value of <code>get(uidClassID)</code> must be the
687     * <code>String</code> name of a
688     * class that implements the corresponding <code>ComponentUI</code>
689     * class.  If the class hasn't been loaded before, this method looks
690     * up the class with <code>uiClassLoader.loadClass()</code> if a non
691     * <code>null</code>
692     * class loader is provided, <code>classForName()</code> otherwise.
693     * <p>
694     * If a mapping for <code>uiClassID</code> exists or if the specified
695     * class can't be found, return <code>null</code>.
696     * <p>
697     * This method is used by <code>getUI</code>, it's usually
698     * not necessary to call it directly.
699     *
700     * @param uiClassID  a string containing the class ID
701     * @param uiClassLoader the object which will load the class
702     * @return the value of <code>Class.forName(get(uidClassID))</code>
703     * @see #getUI
704     */
705    public Class<? extends ComponentUI>
706        getUIClass(String uiClassID, ClassLoader uiClassLoader)
707    {
708        try {
709            String className = (String)get(uiClassID);
710            if (className != null) {
711                ReflectUtil.checkPackageAccess(className);
712
713                Class<?> cls = (Class)get(className);
714                if (cls == null) {
715                    if (uiClassLoader == null) {
716                        cls = SwingUtilities.loadSystemClass(className);
717                    }
718                    else {
719                        cls = uiClassLoader.loadClass(className);
720                    }
721                    if (cls != null) {
722                        // Save lookup for future use, as forName is slow.
723                        put(className, cls);
724                    }
725                }
726                @SuppressWarnings("unchecked")
727                Class<? extends ComponentUI> tmp = (Class<? extends ComponentUI>)cls;
728                return tmp;
729            }
730        }
731        catch (ClassNotFoundException | ClassCastException e) {
732            return null;
733        }
734        return null;
735    }
736
737
738    /**
739     * Returns the L&amp;F class that renders this component.
740     *
741     * @param uiClassID a string containing the class ID
742     * @return the Class object returned by
743     *          <code>getUIClass(uiClassID, null)</code>
744     */
745    public Class<? extends ComponentUI> getUIClass(String uiClassID) {
746        return getUIClass(uiClassID, null);
747    }
748
749
750    /**
751     * If <code>getUI()</code> fails for any reason,
752     * it calls this method before returning <code>null</code>.
753     * Subclasses may choose to do more or less here.
754     *
755     * @param msg message string to print
756     * @see #getUI
757     */
758    protected void getUIError(String msg) {
759        try {
760            throw new Error(msg);
761        }
762        catch (Throwable e) {
763            e.printStackTrace();
764        }
765    }
766
767    /**
768     * Creates an <code>ComponentUI</code> implementation for the
769     * specified component.  In other words create the look
770     * and feel specific delegate object for <code>target</code>.
771     * This is done in two steps:
772     * <ul>
773     * <li> Look up the name of the <code>ComponentUI</code> implementation
774     * class under the value returned by <code>target.getUIClassID()</code>.
775     * <li> Use the implementation classes static <code>createUI()</code>
776     * method to construct a look and feel delegate.
777     * </ul>
778     * @param target  the <code>JComponent</code> which needs a UI
779     * @return the <code>ComponentUI</code> object
780     */
781    public ComponentUI getUI(JComponent target) {
782
783        Object cl = get("ClassLoader");
784        ClassLoader uiClassLoader =
785            (cl != null) ? (ClassLoader)cl : target.getClass().getClassLoader();
786        Class<? extends ComponentUI> uiClass = getUIClass(target.getUIClassID(), uiClassLoader);
787        Object uiObject = null;
788
789        if (uiClass == null) {
790            getUIError("no ComponentUI class for: " + target);
791        }
792        else {
793            try {
794                Method m = (Method)get(uiClass);
795                if (m == null) {
796                    m = uiClass.getMethod("createUI", new Class<?>[]{JComponent.class});
797                    put(uiClass, m);
798                }
799
800                if (uiClass.getModule() == ComponentUI.class.getModule()) {
801                    // uiClass is a system LAF if it's in java.desktop module
802                    uiObject = m.invoke(null, new Object[]{target});
803                } else {
804                    uiObject = MethodUtil.invoke(m, null, new Object[]{target});
805                }
806            }
807            catch (NoSuchMethodException e) {
808                getUIError("static createUI() method not found in " + uiClass);
809            }
810            catch (Exception e) {
811                getUIError("createUI() failed for " + target + " " + e);
812            }
813        }
814
815        return (ComponentUI)uiObject;
816    }
817
818    /**
819     * Adds a <code>PropertyChangeListener</code> to the listener list.
820     * The listener is registered for all properties.
821     * <p>
822     * A <code>PropertyChangeEvent</code> will get fired whenever a default
823     * is changed.
824     *
825     * @param listener  the <code>PropertyChangeListener</code> to be added
826     * @see java.beans.PropertyChangeSupport
827     */
828    public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
829        if (changeSupport == null) {
830            changeSupport = new SwingPropertyChangeSupport(this);
831        }
832        changeSupport.addPropertyChangeListener(listener);
833    }
834
835
836    /**
837     * Removes a <code>PropertyChangeListener</code> from the listener list.
838     * This removes a <code>PropertyChangeListener</code> that was registered
839     * for all properties.
840     *
841     * @param listener  the <code>PropertyChangeListener</code> to be removed
842     * @see java.beans.PropertyChangeSupport
843     */
844    public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
845        if (changeSupport != null) {
846            changeSupport.removePropertyChangeListener(listener);
847        }
848    }
849
850
851    /**
852     * Returns an array of all the <code>PropertyChangeListener</code>s added
853     * to this UIDefaults with addPropertyChangeListener().
854     *
855     * @return all of the <code>PropertyChangeListener</code>s added or an empty
856     *         array if no listeners have been added
857     * @since 1.4
858     */
859    public synchronized PropertyChangeListener[] getPropertyChangeListeners() {
860        if (changeSupport == null) {
861            return new PropertyChangeListener[0];
862        }
863        return changeSupport.getPropertyChangeListeners();
864    }
865
866
867    /**
868     * Support for reporting bound property changes.  If oldValue and
869     * newValue are not equal and the <code>PropertyChangeEvent</code>x
870     * listener list isn't empty, then fire a
871     * <code>PropertyChange</code> event to each listener.
872     *
873     * @param propertyName  the programmatic name of the property
874     *          that was changed
875     * @param oldValue  the old value of the property
876     * @param newValue  the new value of the property
877     * @see java.beans.PropertyChangeSupport
878     */
879    protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
880        if (changeSupport != null) {
881            changeSupport.firePropertyChange(propertyName, oldValue, newValue);
882        }
883    }
884
885
886    /**
887     * Adds a resource bundle to the list of resource bundles that are
888     * searched for localized values. Resource bundles are searched in
889     * the reverse order they were added, using the
890     * {@linkplain ClassLoader#getSystemClassLoader system class loader}.
891     * In other words, the most recently added bundle is searched first.
892     *
893     * @param bundleName  the base name of the resource bundle to be added
894     * @see java.util.ResourceBundle
895     * @see #removeResourceBundle
896     * @see ResourceBundle#getBundle(String, Locale, ClassLoader)
897     * @since 1.4
898     */
899    public synchronized void addResourceBundle(final String bundleName) {
900        if (bundleName == null) {
901            return;
902        }
903        if (isDesktopResourceBundle(bundleName)) {
904            // Only the java.desktop itself can register resource bundles from
905            // java.desktop module
906            return;
907        }
908        addInternalBundle(bundleName);
909    }
910
911    /**
912     * This methods should be used to register internal resource bundles from
913     * the java.desktop module.
914     *
915     * @param bundleName  the base name of the resource bundle to be added
916     * @since 9
917     */
918    private synchronized void addInternalBundle(final String bundleName) {
919        if (bundleName == null) {
920            return;
921        }
922        if (resourceBundles == null) {
923            resourceBundles = new Vector<String>(5);
924        }
925        if (!resourceBundles.contains(bundleName)) {
926            resourceBundles.add(bundleName);
927            resourceCache.clear();
928        }
929    }
930
931    /**
932     * Removes a resource bundle from the list of resource bundles that are
933     * searched for localized defaults.
934     *
935     * @param bundleName  the base name of the resource bundle to be removed
936     * @see java.util.ResourceBundle
937     * @see #addResourceBundle
938     * @since 1.4
939     */
940    public synchronized void removeResourceBundle( String bundleName ) {
941        if( resourceBundles != null ) {
942            resourceBundles.remove( bundleName );
943        }
944        resourceCache.clear();
945    }
946
947    /**
948     * Sets the default locale.  The default locale is used in retrieving
949     * localized values via <code>get</code> methods that do not take a
950     * locale argument.  As of release 1.4, Swing UI objects should retrieve
951     * localized values using the locale of their component rather than the
952     * default locale.  The default locale exists to provide compatibility with
953     * pre 1.4 behaviour.
954     *
955     * @param l the new default locale
956     * @see #getDefaultLocale
957     * @see #get(Object)
958     * @see #get(Object,Locale)
959     * @since 1.4
960     */
961    public void setDefaultLocale( Locale l ) {
962        defaultLocale = l;
963    }
964
965    /**
966     * Returns the default locale.  The default locale is used in retrieving
967     * localized values via <code>get</code> methods that do not take a
968     * locale argument.  As of release 1.4, Swing UI objects should retrieve
969     * localized values using the locale of their component rather than the
970     * default locale.  The default locale exists to provide compatibility with
971     * pre 1.4 behaviour.
972     *
973     * @return the default locale
974     * @see #setDefaultLocale
975     * @see #get(Object)
976     * @see #get(Object,Locale)
977     * @since 1.4
978     */
979    public Locale getDefaultLocale() {
980        return defaultLocale;
981    }
982
983    /**
984     * This class enables one to store an entry in the defaults
985     * table that isn't constructed until the first time it's
986     * looked up with one of the <code>getXXX(key)</code> methods.
987     * Lazy values are useful for defaults that are expensive
988     * to construct or are seldom retrieved.  The first time
989     * a <code>LazyValue</code> is retrieved its "real value" is computed
990     * by calling <code>LazyValue.createValue()</code> and the real
991     * value is used to replace the <code>LazyValue</code> in the
992     * <code>UIDefaults</code>
993     * table.  Subsequent lookups for the same key return
994     * the real value.  Here's an example of a <code>LazyValue</code>
995     * that constructs a <code>Border</code>:
996     * <pre>
997     *  Object borderLazyValue = new UIDefaults.LazyValue() {
998     *      public Object createValue(UIDefaults table) {
999     *          return new BorderFactory.createLoweredBevelBorder();
1000     *      }
1001     *  };
1002     *
1003     *  uiDefaultsTable.put("MyBorder", borderLazyValue);
1004     * </pre>
1005     *
1006     * @see UIDefaults#get
1007     */
1008    public interface LazyValue {
1009        /**
1010         * Creates the actual value retrieved from the <code>UIDefaults</code>
1011         * table. When an object that implements this interface is
1012         * retrieved from the table, this method is used to create
1013         * the real value, which is then stored in the table and
1014         * returned to the calling method.
1015         *
1016         * @param table  a <code>UIDefaults</code> table
1017         * @return the created <code>Object</code>
1018         */
1019        Object createValue(UIDefaults table);
1020    }
1021
1022
1023    /**
1024     * This class enables one to store an entry in the defaults
1025     * table that's constructed each time it's looked up with one of
1026     * the <code>getXXX(key)</code> methods. Here's an example of
1027     * an <code>ActiveValue</code> that constructs a
1028     * <code>DefaultListCellRenderer</code>:
1029     * <pre>
1030     *  Object cellRendererActiveValue = new UIDefaults.ActiveValue() {
1031     *      public Object createValue(UIDefaults table) {
1032     *          return new DefaultListCellRenderer();
1033     *      }
1034     *  };
1035     *
1036     *  uiDefaultsTable.put("MyRenderer", cellRendererActiveValue);
1037     * </pre>
1038     *
1039     * @see UIDefaults#get
1040     */
1041    public interface ActiveValue {
1042        /**
1043         * Creates the value retrieved from the <code>UIDefaults</code> table.
1044         * The object is created each time it is accessed.
1045         *
1046         * @param table  a <code>UIDefaults</code> table
1047         * @return the created <code>Object</code>
1048         */
1049        Object createValue(UIDefaults table);
1050    }
1051
1052    /**
1053     * This class provides an implementation of <code>LazyValue</code>
1054     * which can be
1055     * used to delay loading of the Class for the instance to be created.
1056     * It also avoids creation of an anonymous inner class for the
1057     * <code>LazyValue</code>
1058     * subclass.  Both of these improve performance at the time that a
1059     * a Look and Feel is loaded, at the cost of a slight performance
1060     * reduction the first time <code>createValue</code> is called
1061     * (since Reflection APIs are used).
1062     * @since 1.3
1063     */
1064    public static class ProxyLazyValue implements LazyValue {
1065        private AccessControlContext acc;
1066        private String className;
1067        private String methodName;
1068        private Object[] args;
1069
1070        /**
1071         * Creates a <code>LazyValue</code> which will construct an instance
1072         * when asked.
1073         *
1074         * @param c    a <code>String</code> specifying the classname
1075         *             of the instance to be created on demand
1076         */
1077        public ProxyLazyValue(String c) {
1078            this(c, (String)null);
1079        }
1080        /**
1081         * Creates a <code>LazyValue</code> which will construct an instance
1082         * when asked.
1083         *
1084         * @param c    a <code>String</code> specifying the classname of
1085         *              the class
1086         *              containing a static method to be called for
1087         *              instance creation
1088         * @param m    a <code>String</code> specifying the static
1089         *              method to be called on class c
1090         */
1091        public ProxyLazyValue(String c, String m) {
1092            this(c, m, null);
1093        }
1094        /**
1095         * Creates a <code>LazyValue</code> which will construct an instance
1096         * when asked.
1097         *
1098         * @param c    a <code>String</code> specifying the classname
1099         *              of the instance to be created on demand
1100         * @param o    an array of <code>Objects</code> to be passed as
1101         *              paramaters to the constructor in class c
1102         */
1103        public ProxyLazyValue(String c, Object[] o) {
1104            this(c, null, o);
1105        }
1106        /**
1107         * Creates a <code>LazyValue</code> which will construct an instance
1108         * when asked.
1109         *
1110         * @param c    a <code>String</code> specifying the classname
1111         *              of the class
1112         *              containing a static method to be called for
1113         *              instance creation.
1114         * @param m    a <code>String</code> specifying the static method
1115         *              to be called on class c
1116         * @param o    an array of <code>Objects</code> to be passed as
1117         *              paramaters to the static method in class c
1118         */
1119        public ProxyLazyValue(String c, String m, Object[] o) {
1120            acc = AccessController.getContext();
1121            className = c;
1122            methodName = m;
1123            if (o != null) {
1124                args = o.clone();
1125            }
1126        }
1127
1128        /**
1129         * Creates the value retrieved from the <code>UIDefaults</code> table.
1130         * The object is created each time it is accessed.
1131         *
1132         * @param table  a <code>UIDefaults</code> table
1133         * @return the created <code>Object</code>
1134         */
1135        public Object createValue(final UIDefaults table) {
1136            // In order to pick up the security policy in effect at the
1137            // time of creation we use a doPrivileged with the
1138            // AccessControlContext that was in place when this was created.
1139            if (acc == null && System.getSecurityManager() != null) {
1140                throw new SecurityException("null AccessControlContext");
1141            }
1142            return AccessController.doPrivileged(new PrivilegedAction<Object>() {
1143                public Object run() {
1144                    try {
1145                        Class<?> c;
1146                        Object cl;
1147                        // See if we should use a separate ClassLoader
1148                        if (table == null || !((cl = table.get("ClassLoader"))
1149                                               instanceof ClassLoader)) {
1150                            cl = Thread.currentThread().
1151                                        getContextClassLoader();
1152                            if (cl == null) {
1153                                // Fallback to the system class loader.
1154                                cl = ClassLoader.getSystemClassLoader();
1155                            }
1156                        }
1157                        ReflectUtil.checkPackageAccess(className);
1158                        c = Class.forName(className, true, (ClassLoader)cl);
1159                        SwingUtilities2.checkAccess(c.getModifiers());
1160                        if (methodName != null) {
1161                            Class<?>[] types = getClassArray(args);
1162                            Method m = c.getMethod(methodName, types);
1163                            return MethodUtil.invoke(m, c, args);
1164                        } else {
1165                            Class<?>[] types = getClassArray(args);
1166                            Constructor<?> constructor = c.getConstructor(types);
1167                            SwingUtilities2.checkAccess(constructor.getModifiers());
1168                            return constructor.newInstance(args);
1169                        }
1170                    } catch(Exception e) {
1171                        // Ideally we would throw an exception, unfortunately
1172                        // often times there are errors as an initial look and
1173                        // feel is loaded before one can be switched. Perhaps a
1174                        // flag should be added for debugging, so that if true
1175                        // the exception would be thrown.
1176                    }
1177                    return null;
1178                }
1179            }, acc);
1180        }
1181
1182        /*
1183         * Coerce the array of class types provided into one which
1184         * looks the way the Reflection APIs expect.  This is done
1185         * by substituting primitive types for their Object counterparts,
1186         * and superclasses for subclasses used to add the
1187         * <code>UIResource</code> tag.
1188         */
1189        private Class<?>[] getClassArray(Object[] args) {
1190            Class<?>[] types = null;
1191            if (args!=null) {
1192                types = new Class<?>[args.length];
1193                for (int i = 0; i< args.length; i++) {
1194                    /* PENDING(ges): At present only the primitive types
1195                       used are handled correctly; this should eventually
1196                       handle all primitive types */
1197                    if (args[i] instanceof java.lang.Integer) {
1198                        types[i]=Integer.TYPE;
1199                    } else if (args[i] instanceof java.lang.Boolean) {
1200                        types[i]=Boolean.TYPE;
1201                    } else if (args[i] instanceof javax.swing.plaf.ColorUIResource) {
1202                        /* PENDING(ges) Currently the Reflection APIs do not
1203                           search superclasses of parameters supplied for
1204                           constructor/method lookup.  Since we only have
1205                           one case where this is needed, we substitute
1206                           directly instead of adding a massive amount
1207                           of mechanism for this.  Eventually this will
1208                           probably need to handle the general case as well.
1209                           */
1210                        types[i]=java.awt.Color.class;
1211                    } else {
1212                        types[i]=args[i].getClass();
1213                    }
1214                }
1215            }
1216            return types;
1217        }
1218
1219        private String printArgs(Object[] array) {
1220            String s = "{";
1221            if (array !=null) {
1222                for (int i = 0 ; i < array.length-1; i++) {
1223                    s = s.concat(array[i] + ",");
1224                }
1225                s = s.concat(array[array.length-1] + "}");
1226            } else {
1227                s = s.concat("}");
1228            }
1229            return s;
1230        }
1231    }
1232
1233
1234    /**
1235     * <code>LazyInputMap</code> will create a <code>InputMap</code>
1236     * in its <code>createValue</code>
1237     * method. The bindings are passed in the constructor.
1238     * The bindings are an array with
1239     * the even number entries being string <code>KeyStrokes</code>
1240     * (eg "alt SPACE") and
1241     * the odd number entries being the value to use in the
1242     * <code>InputMap</code> (and the key in the <code>ActionMap</code>).
1243     * @since 1.3
1244     */
1245    public static class LazyInputMap implements LazyValue {
1246        /** Key bindings are registered under. */
1247        private Object[] bindings;
1248
1249        /**
1250         * Constructs a {@code LazyInputMap}.
1251         * @param bindings the bindings
1252         */
1253        public LazyInputMap(Object[] bindings) {
1254            this.bindings = bindings;
1255        }
1256
1257        /**
1258         * Creates an <code>InputMap</code> with the bindings that are
1259         * passed in.
1260         *
1261         * @param table a <code>UIDefaults</code> table
1262         * @return the <code>InputMap</code>
1263         */
1264        public Object createValue(UIDefaults table) {
1265            if (bindings != null) {
1266                InputMap km = LookAndFeel.makeInputMap(bindings);
1267                return km;
1268            }
1269            return null;
1270        }
1271    }
1272
1273    /**
1274     * <code>TextAndMnemonicHashMap</code> stores swing resource strings. Many of strings
1275     * can have a mnemonic. For example:
1276     *   FileChooser.saveButton.textAndMnemonic=&Save
1277     * For this case method get returns "Save" for the key "FileChooser.saveButtonText" and
1278     * mnemonic "S" for the key "FileChooser.saveButtonMnemonic"
1279     *
1280     * There are several patterns for the text and mnemonic suffixes which are checked by the
1281     * <code>TextAndMnemonicHashMap</code> class.
1282     * Patterns which are converted to the xxx.textAndMnemonic key:
1283     * (xxxNameText, xxxNameMnemonic)
1284     * (xxxNameText, xxxMnemonic)
1285     * (xxx.nameText, xxx.mnemonic)
1286     * (xxxText, xxxMnemonic)
1287     *
1288     * These patterns can have a mnemonic index in format
1289     * (xxxDisplayedMnemonicIndex)
1290     *
1291     * Pattern which is converted to the xxx.titleAndMnemonic key:
1292     * (xxxTitle, xxxMnemonic)
1293     *
1294     */
1295    private static class TextAndMnemonicHashMap extends HashMap<String, Object> {
1296
1297        static final String AND_MNEMONIC = "AndMnemonic";
1298        static final String TITLE_SUFFIX = ".titleAndMnemonic";
1299        static final String TEXT_SUFFIX = ".textAndMnemonic";
1300
1301        @Override
1302        public Object get(Object key) {
1303
1304            Object value = super.get(key);
1305
1306            if (value == null) {
1307
1308                boolean checkTitle = false;
1309
1310                String stringKey = key.toString();
1311                String compositeKey = null;
1312
1313                if (stringKey.endsWith(AND_MNEMONIC)) {
1314                    return null;
1315                }
1316
1317                if (stringKey.endsWith(".mnemonic")) {
1318                    compositeKey = composeKey(stringKey, 9, TEXT_SUFFIX);
1319                } else if (stringKey.endsWith("NameMnemonic")) {
1320                    compositeKey = composeKey(stringKey, 12, TEXT_SUFFIX);
1321                } else if (stringKey.endsWith("Mnemonic")) {
1322                    compositeKey = composeKey(stringKey, 8, TEXT_SUFFIX);
1323                    checkTitle = true;
1324                }
1325
1326                if (compositeKey != null) {
1327                    value = super.get(compositeKey);
1328                    if (value == null && checkTitle) {
1329                        compositeKey = composeKey(stringKey, 8, TITLE_SUFFIX);
1330                        value = super.get(compositeKey);
1331                    }
1332
1333                    return value == null ? null : getMnemonicFromProperty(value.toString());
1334                }
1335
1336                if (stringKey.endsWith("NameText")) {
1337                    compositeKey = composeKey(stringKey, 8, TEXT_SUFFIX);
1338                } else if (stringKey.endsWith(".nameText")) {
1339                    compositeKey = composeKey(stringKey, 9, TEXT_SUFFIX);
1340                } else if (stringKey.endsWith("Text")) {
1341                    compositeKey = composeKey(stringKey, 4, TEXT_SUFFIX);
1342                } else if (stringKey.endsWith("Title")) {
1343                    compositeKey = composeKey(stringKey, 5, TITLE_SUFFIX);
1344                }
1345
1346                if (compositeKey != null) {
1347                    value = super.get(compositeKey);
1348                    return value == null ? null : getTextFromProperty(value.toString());
1349                }
1350
1351                if (stringKey.endsWith("DisplayedMnemonicIndex")) {
1352                    compositeKey = composeKey(stringKey, 22, TEXT_SUFFIX);
1353                    value = super.get(compositeKey);
1354                    if (value == null) {
1355                        compositeKey = composeKey(stringKey, 22, TITLE_SUFFIX);
1356                        value = super.get(compositeKey);
1357                    }
1358                    return value == null ? null : getIndexFromProperty(value.toString());
1359                }
1360            }
1361
1362            return value;
1363        }
1364
1365        String composeKey(String key, int reduce, String sufix) {
1366            return key.substring(0, key.length() - reduce) + sufix;
1367        }
1368
1369        String getTextFromProperty(String text) {
1370            return text.replace("&", "");
1371        }
1372
1373        String getMnemonicFromProperty(String text) {
1374            int index = text.indexOf('&');
1375            if (0 <= index && index < text.length() - 1) {
1376                char c = text.charAt(index + 1);
1377                return Integer.toString((int) Character.toUpperCase(c));
1378            }
1379            return null;
1380        }
1381
1382        String getIndexFromProperty(String text) {
1383            int index = text.indexOf('&');
1384            return (index == -1) ? null : Integer.toString(index);
1385        }
1386    }
1387
1388}
1389