Path: blob/master/src/java.desktop/share/classes/sun/swing/DefaultLookup.java
41153 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.swing;2526import java.awt.Color;27import java.awt.Insets;28import javax.swing.*;29import javax.swing.border.Border;30import javax.swing.plaf.ComponentUI;31import sun.awt.AppContext;3233/**34* DefaultLookup provides a way to customize the lookup done by the35* UIManager. The default implementation of DefaultLookup forwards36* the call to the UIManager.37* <p>38* <b>WARNING:</b> While this class is public, it should not be treated as39* public API and its API may change in incompatable ways between dot dot40* releases and even patch releases. You should not rely on this class even41* existing.42*43* @author Scott Violet44*/45public class DefaultLookup {46/**47* Key used to store DefaultLookup for AppContext.48*/49private static final Object DEFAULT_LOOKUP_KEY = new50StringBuffer("DefaultLookup");51/**52* Thread that last asked for a default.53*/54private static Thread currentDefaultThread;55/**56* DefaultLookup for last thread.57*/58private static DefaultLookup currentDefaultLookup;5960/**61* If true, a custom DefaultLookup has been set.62*/63private static boolean isLookupSet;646566/**67* Sets the DefaultLookup instance to use for the current68* <code>AppContext</code>. Null implies the UIManager should be69* used.70*/71public static void setDefaultLookup(DefaultLookup lookup) {72synchronized(DefaultLookup.class) {73if (!isLookupSet && lookup == null) {74// Null was passed in, and no one has invoked setDefaultLookup75// with a non-null value, we don't need to do anything.76return;77}78else if (lookup == null) {79// null was passed in, but someone has invoked setDefaultLookup80// with a non-null value, use an instance of DefautLookup81// which will fallback to UIManager.82lookup = new DefaultLookup();83}84isLookupSet = true;85AppContext.getAppContext().put(DEFAULT_LOOKUP_KEY, lookup);86currentDefaultThread = Thread.currentThread();87currentDefaultLookup = lookup;88}89}9091public static Object get(JComponent c, ComponentUI ui, String key) {92boolean lookupSet;93synchronized(DefaultLookup.class) {94lookupSet = isLookupSet;95}96if (!lookupSet) {97// No one has set a valid DefaultLookup, use UIManager.98return UIManager.get(key, c.getLocale());99}100Thread thisThread = Thread.currentThread();101DefaultLookup lookup;102synchronized(DefaultLookup.class) {103// See if we've already cached the DefaultLookup for this thread,104// and use it if we have.105if (thisThread == currentDefaultThread) {106// It is cached, use it.107lookup = currentDefaultLookup;108}109else {110// Not cached, get the DefaultLookup to use from the AppContext111lookup = (DefaultLookup)AppContext.getAppContext().get(112DEFAULT_LOOKUP_KEY);113if (lookup == null) {114// Fallback to DefaultLookup, which will redirect to the115// UIManager.116lookup = new DefaultLookup();117AppContext.getAppContext().put(DEFAULT_LOOKUP_KEY, lookup);118}119// Cache the values to make the next lookup easier.120currentDefaultThread = thisThread;121currentDefaultLookup = lookup;122}123}124return lookup.getDefault(c, ui, key);125}126127//128// The following are convenience method that all use getDefault.129//130public static int getInt(JComponent c, ComponentUI ui, String key,131int defaultValue) {132Object iValue = get(c, ui, key);133134if (iValue == null || !(iValue instanceof Number)) {135return defaultValue;136}137return ((Number)iValue).intValue();138}139140public static int getInt(JComponent c, ComponentUI ui, String key) {141return getInt(c, ui, key, -1);142}143144public static Insets getInsets(JComponent c, ComponentUI ui, String key,145Insets defaultValue) {146Object iValue = get(c, ui, key);147148if (iValue == null || !(iValue instanceof Insets)) {149return defaultValue;150}151return (Insets)iValue;152}153154public static Insets getInsets(JComponent c, ComponentUI ui, String key) {155return getInsets(c, ui, key, null);156}157158public static boolean getBoolean(JComponent c, ComponentUI ui, String key,159boolean defaultValue) {160Object iValue = get(c, ui, key);161162if (iValue == null || !(iValue instanceof Boolean)) {163return defaultValue;164}165return ((Boolean)iValue).booleanValue();166}167168public static boolean getBoolean(JComponent c, ComponentUI ui, String key) {169return getBoolean(c, ui, key, false);170}171172public static Color getColor(JComponent c, ComponentUI ui, String key,173Color defaultValue) {174Object iValue = get(c, ui, key);175176if (iValue == null || !(iValue instanceof Color)) {177return defaultValue;178}179return (Color)iValue;180}181182public static Color getColor(JComponent c, ComponentUI ui, String key) {183return getColor(c, ui, key, null);184}185186public static Icon getIcon(JComponent c, ComponentUI ui, String key,187Icon defaultValue) {188Object iValue = get(c, ui, key);189if (iValue == null || !(iValue instanceof Icon)) {190return defaultValue;191}192return (Icon)iValue;193}194195public static Icon getIcon(JComponent c, ComponentUI ui, String key) {196return getIcon(c, ui, key, null);197}198199public static Border getBorder(JComponent c, ComponentUI ui, String key,200Border defaultValue) {201Object iValue = get(c, ui, key);202if (iValue == null || !(iValue instanceof Border)) {203return defaultValue;204}205return (Border)iValue;206}207208public static Border getBorder(JComponent c, ComponentUI ui, String key) {209return getBorder(c, ui, key, null);210}211212public Object getDefault(JComponent c, ComponentUI ui, String key) {213// basic214return UIManager.get(key, c.getLocale());215}216}217218219