< prev index next >

src/java.desktop/share/classes/javax/swing/UIDefaults.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2015, 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


  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.io.UncheckedIOException;
  36 import java.lang.reflect.*;
  37 import java.util.HashMap;
  38 import java.util.Map;
  39 import java.util.Enumeration;
  40 import java.util.Hashtable;
  41 import java.util.ResourceBundle;
  42 import java.util.Locale;
  43 import java.util.Vector;
  44 import java.util.MissingResourceException;
  45 import java.awt.Font;
  46 import java.awt.Color;
  47 import java.awt.Insets;
  48 import java.awt.Dimension;
  49 import java.beans.PropertyChangeListener;
  50 import java.security.AccessController;
  51 import java.security.AccessControlContext;
  52 import java.security.PrivilegedAction;

  53 
  54 import sun.reflect.misc.MethodUtil;
  55 import sun.reflect.misc.ReflectUtil;
  56 import sun.swing.SwingUtilities2;
  57 
  58 /**
  59  * A table of defaults for Swing components.  Applications can set/get
  60  * default values via the <code>UIManager</code>.
  61  * <p>
  62  * <strong>Warning:</strong>
  63  * Serialized objects of this class will not be compatible with
  64  * future Swing releases. The current serialization support is
  65  * appropriate for short term storage or RMI between applications running
  66  * the same version of Swing.  As of 1.4, support for long term storage
  67  * of all JavaBeans&trade;
  68  * has been added to the <code>java.beans</code> package.
  69  * Please see {@link java.beans.XMLEncoder}.
  70  *
  71  * @see UIManager
  72  * @author Hans Muller


  74  */
  75 @SuppressWarnings("serial") // Same-version serialization only
  76 public class UIDefaults extends Hashtable<Object,Object>
  77 {
  78     private static final Object PENDING = new Object();
  79 
  80     private SwingPropertyChangeSupport changeSupport;
  81 
  82     private Vector<String> resourceBundles;
  83 
  84     private Locale defaultLocale = Locale.getDefault();
  85 
  86     /**
  87      * Maps from a Locale to a cached Map of the ResourceBundle. This is done
  88      * so as to avoid an exception being thrown when a value is asked for.
  89      * Access to this should be done while holding a lock on the
  90      * UIDefaults, eg synchronized(this).
  91      */
  92     private Map<Locale, Map<String, Object>> resourceCache;
  93 
























  94     /**
  95      * Creates an empty defaults table.
  96      */
  97     public UIDefaults() {
  98         this(700, .75f);
  99     }
 100 
 101     /**
 102      * Creates an empty defaults table with the specified initial capacity and
 103      * load factor.
 104      *
 105      * @param initialCapacity   the initial capacity of the defaults table
 106      * @param loadFactor        the load factor of the defaults table
 107      * @see java.util.Hashtable
 108      * @since 1.6
 109      */
 110     public UIDefaults(int initialCapacity, float loadFactor) {
 111         super(initialCapacity, loadFactor);
 112         resourceCache = new HashMap<Locale, Map<String, Object>>();
 113     }


 381      * Puts all of the key/value pairs in the database and
 382      * unconditionally generates one <code>PropertyChangeEvent</code>.
 383      * The events oldValue and newValue will be <code>null</code> and its
 384      * <code>propertyName</code> will be "UIDefaults".  The key/value pairs are
 385      * added for all locales.
 386      *
 387      * @param keyValueList  an array of key/value pairs
 388      * @see #put
 389      * @see java.util.Hashtable#put
 390      */
 391     public void putDefaults(Object[] keyValueList) {
 392         for(int i = 0, max = keyValueList.length; i < max; i += 2) {
 393             Object value = keyValueList[i + 1];
 394             if (value == null) {
 395                 super.remove(keyValueList[i]);
 396             }
 397             else {
 398                 super.put(keyValueList[i], value);
 399             }
 400         }





 401         firePropertyChange("UIDefaults", null, null);
 402     }
 403 
 404 
 405     /**
 406      * If the value of <code>key</code> is a <code>Font</code> return it,
 407      * otherwise return <code>null</code>.
 408      * @param key the desired key
 409      * @return if the value for <code>key</code> is a <code>Font</code>,
 410      *          return the <code>Font</code> object; otherwise return
 411      *          <code>null</code>
 412      */
 413     public Font getFont(Object key) {
 414         Object value = get(key);
 415         return (value instanceof Font) ? (Font)value : null;
 416     }
 417 
 418 
 419     /**
 420      * If the value of <code>key</code> for the given <code>Locale</code>


   1 /*
   2  * Copyright (c) 1997, 2016, 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


  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.io.UncheckedIOException;
  36 import java.lang.reflect.*;
  37 import java.util.HashMap;
  38 import java.util.Map;
  39 import java.util.Enumeration;
  40 import java.util.Hashtable;
  41 import java.util.ResourceBundle;
  42 import java.util.Locale;
  43 import java.util.Vector;
  44 import java.util.MissingResourceException;
  45 import java.awt.Font;
  46 import java.awt.Color;
  47 import java.awt.Insets;
  48 import java.awt.Dimension;
  49 import java.beans.PropertyChangeListener;
  50 import java.security.AccessController;
  51 import java.security.AccessControlContext;
  52 import java.security.PrivilegedAction;
  53 import sun.awt.OSInfo;
  54 
  55 import sun.reflect.misc.MethodUtil;
  56 import sun.reflect.misc.ReflectUtil;
  57 import 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


  75  */
  76 @SuppressWarnings("serial") // Same-version serialization only
  77 public 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     private static boolean disableAquaMenuBarUI = true;            
  96 
  97     static {
  98         AccessController
  99             .doPrivileged((PrivilegedAction<Void>) () -> {
 100                 if (OSInfo.getOSType() == OSInfo.OSType.MACOSX 
 101                         && !Boolean.getBoolean("apple.laf.disableForcedScreenMenuBar")) {
 102 
 103                     try {
 104                         Class<?> cls = Class.forName("com.apple.laf.AquaMenuBarUI");
 105                         Method m = cls.getDeclaredMethod("getScreenMenuBarProperty");
 106                         m.setAccessible(true);
 107 
 108                         disableAquaMenuBarUI = !((boolean) m.invoke(null));
 109                     } catch (Exception ignored) {}
 110 
 111                     if (!disableAquaMenuBarUI) {
 112                         System.loadLibrary("osxui");
 113                     }
 114                 }
 115                 return null;
 116             });
 117     }
 118 
 119     /**
 120      * Creates an empty defaults table.
 121      */
 122     public UIDefaults() {
 123         this(700, .75f);
 124     }
 125 
 126     /**
 127      * Creates an empty defaults table with the specified initial capacity and
 128      * load factor.
 129      *
 130      * @param initialCapacity   the initial capacity of the defaults table
 131      * @param loadFactor        the load factor of the defaults table
 132      * @see java.util.Hashtable
 133      * @since 1.6
 134      */
 135     public UIDefaults(int initialCapacity, float loadFactor) {
 136         super(initialCapacity, loadFactor);
 137         resourceCache = new HashMap<Locale, Map<String, Object>>();
 138     }


 406      * Puts all of the key/value pairs in the database and
 407      * unconditionally generates one <code>PropertyChangeEvent</code>.
 408      * The events oldValue and newValue will be <code>null</code> and its
 409      * <code>propertyName</code> will be "UIDefaults".  The key/value pairs are
 410      * added for all locales.
 411      *
 412      * @param keyValueList  an array of key/value pairs
 413      * @see #put
 414      * @see java.util.Hashtable#put
 415      */
 416     public void putDefaults(Object[] keyValueList) {
 417         for(int i = 0, max = keyValueList.length; i < max; i += 2) {
 418             Object value = keyValueList[i + 1];
 419             if (value == null) {
 420                 super.remove(keyValueList[i]);
 421             }
 422             else {
 423                 super.put(keyValueList[i], value);
 424             }
 425         }
 426 
 427         if (!disableAquaMenuBarUI) {
 428             UIManager.put("MenuBarUI", "com.apple.laf.AquaMenuBarUI");
 429         }
 430 
 431         firePropertyChange("UIDefaults", null, null);
 432     }
 433 
 434 
 435     /**
 436      * If the value of <code>key</code> is a <code>Font</code> return it,
 437      * otherwise return <code>null</code>.
 438      * @param key the desired key
 439      * @return if the value for <code>key</code> is a <code>Font</code>,
 440      *          return the <code>Font</code> object; otherwise return
 441      *          <code>null</code>
 442      */
 443     public Font getFont(Object key) {
 444         Object value = get(key);
 445         return (value instanceof Font) ? (Font)value : null;
 446     }
 447 
 448 
 449     /**
 450      * If the value of <code>key</code> for the given <code>Locale</code>


< prev index next >