/jide-oss/src/com/jidesoft/plaf/UIDefaultsLookup.java

# · Java · 414 lines · 171 code · 45 blank · 198 comment · 33 complexity · de356a15c5ac7ee1eacb98cebbf1b133 MD5 · raw file

  1. /*
  2. * @(#)UIManagerLookup.java 4/5/2007
  3. *
  4. * Copyright 2002 - 2007 JIDE Software Inc. All rights reserved.
  5. */
  6. package com.jidesoft.plaf;
  7. import com.jidesoft.converter.ObjectConverterManager;
  8. import sun.reflect.Reflection;
  9. import javax.swing.*;
  10. import javax.swing.border.Border;
  11. import java.awt.*;
  12. import java.util.Collection;
  13. import java.util.HashMap;
  14. import java.util.Locale;
  15. import java.util.Map;
  16. /**
  17. * This class simply uses UIManager's get method to lookup the UIDefaults. We used this everywhere in our code so that
  18. * we have one central place to find out which UIDefaults we are using. Another good thing is you can use {@link
  19. * #setTrace(boolean)} and {@link #setDebug(boolean)} to turn on the trace so that it will print out which UIDefaults we
  20. * are trying to get.
  21. */
  22. public class UIDefaultsLookup {
  23. private static boolean _debug = false;
  24. private static boolean _trace = false;
  25. /**
  26. * Sets the debug mode. If debug mode is on, we will print out any UIDefaults that the value is null.
  27. *
  28. * @param debug true or false.
  29. */
  30. public static void setDebug(boolean debug) {
  31. _debug = debug;
  32. }
  33. /**
  34. * Sets the trace mode. If trace mode is on, we will print out any UIDefaults we are trying to get and its current
  35. * value.
  36. *
  37. * @param trace true or false.
  38. */
  39. public static void setTrace(boolean trace) {
  40. _trace = trace;
  41. }
  42. public static void put(UIDefaults table, String key, Object value) {
  43. Object v = table.get(key);
  44. if (v == null || !(v instanceof Map)) {
  45. v = new HashMap<ClassLoader, Object>();
  46. table.put(key, v);
  47. }
  48. Object cl = UIManager.get("ClassLoader");
  49. if (!(cl instanceof ClassLoader)) {
  50. cl = value.getClass().getClassLoader();
  51. }
  52. ((Map) v).put(cl, value);
  53. }
  54. // Returns the invoker's class loader, or null if none.
  55. // NOTE: This must always be invoked when there is exactly one intervening
  56. // frame from the core libraries on the stack between this method's
  57. // invocation and the desired invoker.
  58. static ClassLoader getCallerClassLoader() {
  59. Object cl = UIManager.get("ClassLoader");
  60. if (cl instanceof ClassLoader) {
  61. return (ClassLoader) cl;
  62. }
  63. // NOTE use of more generic Reflection.getCallerClass()
  64. Class caller = Reflection.getCallerClass(3);
  65. // This can be null if the VM is requesting it
  66. if (caller == null) {
  67. return null;
  68. }
  69. // Circumvent security check since this is package-private
  70. return caller.getClassLoader();
  71. }
  72. public static Object get(Object key) {
  73. Object value = UIManager.get(key);
  74. log(value, key, null);
  75. if (value instanceof Map && "Theme.painter".equals(key)) {
  76. Map map = (Map) value;
  77. try {
  78. ClassLoader classLoader = getCallerClassLoader();
  79. Object o = map.get(classLoader);
  80. while (o == null && classLoader.getParent() != null) {
  81. classLoader = classLoader.getParent();
  82. o = map.get(classLoader);
  83. }
  84. if (o == null && map.size() >= 1) {
  85. Collection<Object> classLoaders = map.values();
  86. for (Object cl : classLoaders) {
  87. if (cl != null) {
  88. o = cl;
  89. break;
  90. }
  91. }
  92. }
  93. return o;
  94. }
  95. catch (Exception e) {
  96. if (map.size() == 1) {
  97. return map.values().iterator().next();
  98. }
  99. else {
  100. return map.get(LookAndFeelFactory.getUIManagerClassLoader());
  101. }
  102. }
  103. }
  104. return value;
  105. }
  106. public static Object get(Object key, Locale l) {
  107. Object value = UIManager.get(key, l);
  108. log(value, key, l);
  109. return value;
  110. }
  111. private static void log(Object value, Object key, Locale l) {
  112. if (_debug && value == null) {
  113. System.out.println("\"" + key + (l == null ? "" : l.toString()) + " \" ==> null ------------------------");
  114. }
  115. else if (_trace) {
  116. if (value == null) {
  117. System.out.println("\"" + key + (l == null ? "" : l.toString()) + " \" ==> null ------------------------");
  118. }
  119. else {
  120. System.out.println("\"" + key + (l == null ? "" : l.toString()) + " \" ==> " + value.getClass().getName() + "(" + ObjectConverterManager.toString(value) + ")");
  121. }
  122. }
  123. }
  124. /**
  125. * If the value of <code>key</code> is a <code>Font</code> return it, otherwise return <code>null</code>.
  126. *
  127. * @param key the desired key
  128. * @return if the value for <code>key</code> is a <code>Font</code>, return the <code>Font</code> object; otherwise
  129. * return <code>null</code>
  130. */
  131. public static Font getFont(Object key) {
  132. Object value = get(key);
  133. return (value instanceof Font) ? (Font) value : null;
  134. }
  135. /**
  136. * If the value of <code>key</code> for the given <code>Locale</code> is a <code>Font</code> return it, otherwise
  137. * return <code>null</code>.
  138. *
  139. * @param key the desired key
  140. * @param l the desired locale
  141. * @return if the value for <code>key</code> and <code>Locale</code> is a <code>Font</code>, return the
  142. * <code>Font</code> object; otherwise return <code>null</code>
  143. *
  144. * @since 1.9.5.04
  145. */
  146. public static Font getFont(Object key, Locale l) {
  147. Object value = get(key, l);
  148. return (value instanceof Font) ? (Font) value : null;
  149. }
  150. /**
  151. * If the value of <code>key</code> is a <code>Color</code> return it, otherwise return <code>null</code>.
  152. *
  153. * @param key the desired key
  154. * @return if the value for <code>key</code> is a <code>Color</code>, return the <code>Color</code> object;
  155. * otherwise return <code>null</code>
  156. */
  157. public static Color getColor(Object key) {
  158. Object value = get(key);
  159. return (value instanceof Color) ? (Color) value : null;
  160. }
  161. /**
  162. * If the value of <code>key</code> for the given <code>Locale</code> is a <code>Color</code> return it, otherwise
  163. * return <code>null</code>.
  164. *
  165. * @param key the desired key
  166. * @param l the desired locale
  167. * @return if the value for <code>key</code> and <code>Locale</code> is a <code>Color</code>, return the
  168. * <code>Color</code> object; otherwise return <code>null</code>
  169. *
  170. * @since 1.9.5.04
  171. */
  172. public static Color getColor(Object key, Locale l) {
  173. Object value = get(key, l);
  174. return (value instanceof Color) ? (Color) value : null;
  175. }
  176. /**
  177. * If the value of <code>key</code> is an <code>Icon</code> return it, otherwise return <code>null</code>.
  178. *
  179. * @param key the desired key
  180. * @return if the value for <code>key</code> is an <code>Icon</code>, return the <code>Icon</code> object; otherwise
  181. * return <code>null</code>
  182. */
  183. public static Icon getIcon(Object key) {
  184. Object value = get(key);
  185. return (value instanceof Icon) ? (Icon) value : null;
  186. }
  187. /**
  188. * If the value of <code>key</code> for the given <code>Locale</code> is an <code>Icon</code> return it, otherwise
  189. * return <code>null</code>.
  190. *
  191. * @param key the desired key
  192. * @param l the desired locale
  193. * @return if the value for <code>key</code> and <code>Locale</code> is an <code>Icon</code>, return the
  194. * <code>Icon</code> object; otherwise return <code>null</code>
  195. *
  196. * @since 1.9.5.04
  197. */
  198. public static Icon getIcon(Object key, Locale l) {
  199. Object value = get(key, l);
  200. return (value instanceof Icon) ? (Icon) value : null;
  201. }
  202. /**
  203. * If the value of <code>key</code> is a <code>Border</code> return it, otherwise return <code>null</code>.
  204. *
  205. * @param key the desired key
  206. * @return if the value for <code>key</code> is a <code>Border</code>, return the <code>Border</code> object;
  207. * otherwise return <code>null</code>
  208. */
  209. public static Border getBorder(Object key) {
  210. Object value = get(key);
  211. return (value instanceof Border) ? (Border) value : null;
  212. }
  213. /**
  214. * If the value of <code>key</code> for the given <code>Locale</code> is a <code>Border</code> return it, otherwise
  215. * return <code>null</code>.
  216. *
  217. * @param key the desired key
  218. * @param l the desired locale
  219. * @return if the value for <code>key</code> and <code>Locale</code> is a <code>Border</code>, return the
  220. * <code>Border</code> object; otherwise return <code>null</code>
  221. *
  222. * @since 1.9.5.04
  223. */
  224. public static Border getBorder(Object key, Locale l) {
  225. Object value = get(key, l);
  226. return (value instanceof Border) ? (Border) value : null;
  227. }
  228. /**
  229. * If the value of <code>key</code> is a <code>String</code> return it, otherwise return <code>null</code>.
  230. *
  231. * @param key the desired key
  232. * @return if the value for <code>key</code> is a <code>String</code>, return the <code>String</code> object;
  233. * otherwise return <code>null</code>
  234. */
  235. public static String getString(Object key) {
  236. Object value = get(key);
  237. return (value instanceof String) ? (String) value : null;
  238. }
  239. /**
  240. * If the value of <code>key</code> for the given <code>Locale</code> is a <code>String</code> return it, otherwise
  241. * return <code>null</code>.
  242. *
  243. * @param key the desired key
  244. * @param l the desired <code>Locale</code>
  245. * @return if the value for <code>key</code> for the given <code>Locale</code> is a <code>String</code>, return the
  246. * <code>String</code> object; otherwise return <code>null</code>
  247. *
  248. * @since 1.9.5.04
  249. */
  250. public static String getString(Object key, Locale l) {
  251. Object value = get(key, l);
  252. return (value instanceof String) ? (String) value : null;
  253. }
  254. /**
  255. * If the value of <code>key</code> is an <code>Integer</code> return its integer value, otherwise return 0.
  256. *
  257. * @param key the desired key
  258. * @return if the value for <code>key</code> is an <code>Integer</code>, return its value, otherwise return 0
  259. */
  260. public static int getInt(Object key) {
  261. Object value = get(key);
  262. return (value instanceof Integer) ? (Integer) value : 0;
  263. }
  264. /**
  265. * If the value of <code>key</code> for the given <code>Locale</code> is an <code>Integer</code> return its integer
  266. * value, otherwise return 0.
  267. *
  268. * @param key the desired key
  269. * @param l the desired locale
  270. * @return if the value for <code>key</code> and <code>Locale</code> is an <code>Integer</code>, return its value,
  271. * otherwise return 0
  272. *
  273. * @since 1.9.5.04
  274. */
  275. public static int getInt(Object key, Locale l) {
  276. Object value = get(key, l);
  277. return (value instanceof Integer) ? (Integer) value : 0;
  278. }
  279. /**
  280. * If the value of <code>key</code> is boolean, return the boolean value, otherwise return false.
  281. *
  282. * @param key an <code>Object</code> specifying the key for the desired boolean value
  283. * @return if the value of <code>key</code> is boolean, return the boolean value, otherwise return false.
  284. *
  285. * @since 1.9.5.04
  286. */
  287. public static boolean getBoolean(Object key) {
  288. Object value = get(key);
  289. return (value instanceof Boolean) ? (Boolean) value : false;
  290. }
  291. /**
  292. * If the value of <code>key</code> is boolean, return the boolean value, otherwise return false.
  293. *
  294. * @param key an <code>Object</code> specifying the key for the desired boolean value
  295. * @param defaultValue the default value if the key is missing
  296. * @return if the value of <code>key</code> is boolean, return the boolean value, otherwise return false.
  297. */
  298. public static boolean getBoolean(Object key, boolean defaultValue) {
  299. Object value = get(key);
  300. return (value instanceof Boolean) ? (Boolean) value : defaultValue;
  301. }
  302. /**
  303. * If the value of <code>key</code> for the given <code>Locale</code> is boolean, return the boolean value,
  304. * otherwise return false.
  305. *
  306. * @param key an <code>Object</code> specifying the key for the desired boolean value
  307. * @param l the desired locale
  308. * @return if the value for <code>key</code> and <code>Locale</code> is boolean, return the boolean value, otherwise
  309. * return false.
  310. *
  311. * @since 1.9.5.04
  312. */
  313. public static boolean getBoolean(Object key, Locale l) {
  314. Object value = get(key, l);
  315. return (value instanceof Boolean) ? (Boolean) value : false;
  316. }
  317. /**
  318. * If the value of <code>key</code> is an <code>Insets</code> return it, otherwise return <code>null</code>.
  319. *
  320. * @param key the desired key
  321. * @return if the value for <code>key</code> is an <code>Insets</code>, return the <code>Insets</code> object;
  322. * otherwise return <code>null</code>
  323. */
  324. public static Insets getInsets(Object key) {
  325. Object value = get(key);
  326. return (value instanceof Insets) ? (Insets) value : null;
  327. }
  328. /**
  329. * If the value of <code>key</code> for the given <code>Locale</code> is an <code>Insets</code> return it, otherwise
  330. * return <code>null</code>.
  331. *
  332. * @param key the desired key
  333. * @param l the desired locale
  334. * @return if the value for <code>key</code> and <code>Locale</code> is an <code>Insets</code>, return the
  335. * <code>Insets</code> object; otherwise return <code>null</code>
  336. *
  337. * @since 1.9.5.04
  338. */
  339. public static Insets getInsets(Object key, Locale l) {
  340. Object value = get(key, l);
  341. return (value instanceof Insets) ? (Insets) value : null;
  342. }
  343. /**
  344. * If the value of <code>key</code> is a <code>Dimension</code> return it, otherwise return <code>null</code>.
  345. *
  346. * @param key the desired key
  347. * @return if the value for <code>key</code> is a <code>Dimension</code>, return the <code>Dimension</code> object;
  348. * otherwise return <code>null</code>
  349. */
  350. public static Dimension getDimension(Object key) {
  351. Object value = get(key);
  352. return (value instanceof Dimension) ? (Dimension) value : null;
  353. }
  354. /**
  355. * If the value of <code>key</code> for the given <code>Locale</code> is a <code>Dimension</code> return it,
  356. * otherwise return <code>null</code>.
  357. *
  358. * @param key the desired key
  359. * @param l the desired locale
  360. * @return if the value for <code>key</code> and <code>Locale</code> is a <code>Dimension</code>, return the
  361. * <code>Dimension</code> object; otherwise return <code>null</code>
  362. *
  363. * @since 1.9.5.04
  364. */
  365. public static Dimension getDimension(Object key, Locale l) {
  366. Object value = get(key, l);
  367. return (value instanceof Dimension) ? (Dimension) value : null;
  368. }
  369. }