J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | package sun.swing; |
| 26 | |
| 27 | import java.awt.Color; |
| 28 | import java.awt.Insets; |
| 29 | import javax.swing.*; |
| 30 | import javax.swing.plaf.ComponentUI; |
| 31 | import sun.awt.AppContext; |
| 32 | |
| 33 | /** |
| 34 | * DefaultLookup provides a way to customize the lookup done by the |
| 35 | * UIManager. The default implementation of DefaultLookup forwards |
| 36 | * the call to the UIManager. |
| 37 | * <p> |
| 38 | * <b>WARNING:</b> While this class is public, it should not be treated as |
| 39 | * public API and its API may change in incompatable ways between dot dot |
| 40 | * releases and even patch releases. You should not rely on this class even |
| 41 | * existing. |
| 42 | * |
| 43 | * @author Scott Violet |
| 44 | */ |
| 45 | public class DefaultLookup { |
| 46 | /** |
| 47 | * Key used to store DefaultLookup for AppContext. |
| 48 | */ |
| 49 | private static final Object DEFAULT_LOOKUP_KEY = new |
| 50 | StringBuffer("DefaultLookup"); |
| 51 | /** |
| 52 | * Thread that last asked for a default. |
| 53 | */ |
| 54 | private static Thread currentDefaultThread; |
| 55 | /** |
| 56 | * DefaultLookup for last thread. |
| 57 | */ |
| 58 | private static DefaultLookup currentDefaultLookup; |
| 59 | |
| 60 | /** |
| 61 | * If true, a custom DefaultLookup has been set. |
| 62 | */ |
| 63 | private static boolean isLookupSet; |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * Sets the DefaultLookup instance to use for the current |
| 68 | * <code>AppContext</code>. Null implies the UIManager should be |
| 69 | * used. |
| 70 | */ |
| 71 | public static void setDefaultLookup(DefaultLookup lookup) { |
| 72 | synchronized(DefaultLookup.class) { |
| 73 | if (!isLookupSet && lookup == null) { |
| 74 | // Null was passed in, and no one has invoked setDefaultLookup |
| 75 | // with a non-null value, we don't need to do anything. |
| 76 | return; |
| 77 | } |
| 78 | else if (lookup == null) { |
| 79 | // null was passed in, but someone has invoked setDefaultLookup |
| 80 | // with a non-null value, use an instance of DefautLookup |
| 81 | // which will fallback to UIManager. |
| 82 | lookup = new DefaultLookup(); |
| 83 | } |
| 84 | isLookupSet = true; |
| 85 | AppContext.getAppContext().put(DEFAULT_LOOKUP_KEY, lookup); |
| 86 | currentDefaultThread = Thread.currentThread(); |
| 87 | currentDefaultLookup = lookup; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public static Object get(JComponent c, ComponentUI ui, String key) { |
| 92 | boolean lookupSet; |
| 93 | synchronized(DefaultLookup.class) { |
| 94 | lookupSet = isLookupSet; |
| 95 | } |
| 96 | if (!lookupSet) { |
| 97 | // No one has set a valid DefaultLookup, use UIManager. |
| 98 | return UIManager.get(key, c.getLocale()); |
| 99 | } |
| 100 | Thread thisThread = Thread.currentThread(); |
| 101 | DefaultLookup lookup; |
| 102 | synchronized(DefaultLookup.class) { |
| 103 | // See if we've already cached the DefaultLookup for this thread, |
| 104 | // and use it if we have. |
| 105 | if (thisThread == currentDefaultThread) { |
| 106 | // It is cached, use it. |
| 107 | lookup = currentDefaultLookup; |
| 108 | } |
| 109 | else { |
| 110 | // Not cached, get the DefaultLookup to use from the AppContext |
| 111 | lookup = (DefaultLookup)AppContext.getAppContext().get( |
| 112 | DEFAULT_LOOKUP_KEY); |
| 113 | if (lookup == null) { |
| 114 | // Fallback to DefaultLookup, which will redirect to the |
| 115 | // UIManager. |
| 116 | lookup = new DefaultLookup(); |
| 117 | AppContext.getAppContext().put(DEFAULT_LOOKUP_KEY, lookup); |
| 118 | } |
| 119 | // Cache the values to make the next lookup easier. |
| 120 | currentDefaultThread = thisThread; |
| 121 | currentDefaultLookup = lookup; |
| 122 | } |
| 123 | } |
| 124 | return lookup.getDefault(c, ui, key); |
| 125 | } |
| 126 | |
| 127 | // |
| 128 | // The following are convenience method that all use getDefault. |
| 129 | // |
| 130 | public static int getInt(JComponent c, ComponentUI ui, String key, |
| 131 | int defaultValue) { |
| 132 | Object iValue = get(c, ui, key); |
| 133 | |
| 134 | if (iValue == null || !(iValue instanceof Number)) { |
| 135 | return defaultValue; |
| 136 | } |
| 137 | return ((Number)iValue).intValue(); |
| 138 | } |
| 139 | |
| 140 | public static Insets getInsets(JComponent c, ComponentUI ui, String key, |
| 141 | Insets defaultValue) { |
| 142 | Object iValue = get(c, ui, key); |
| 143 | |
| 144 | if (iValue == null || !(iValue instanceof Insets)) { |
| 145 | return defaultValue; |
| 146 | } |
| 147 | return (Insets)iValue; |
| 148 | } |
| 149 | |
| 150 | public static boolean getBoolean(JComponent c, ComponentUI ui, String key, |
| 151 | boolean defaultValue) { |
| 152 | Object iValue = get(c, ui, key); |
| 153 | |
| 154 | if (iValue == null || !(iValue instanceof Boolean)) { |
| 155 | return defaultValue; |
| 156 | } |
| 157 | return ((Boolean)iValue).booleanValue(); |
| 158 | } |
| 159 | |
| 160 | public static Color getColor(JComponent c, ComponentUI ui, String key, |
| 161 | Color defaultValue) { |
| 162 | Object iValue = get(c, ui, key); |
| 163 | |
| 164 | if (iValue == null || !(iValue instanceof Color)) { |
| 165 | return defaultValue; |
| 166 | } |
| 167 | return (Color)iValue; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | public Object getDefault(JComponent c, ComponentUI ui, String key) { |
| 173 | // basic |
| 174 | return UIManager.get(key, c.getLocale()); |
| 175 | } |
| 176 | } |