PageRenderTime 1223ms CodeModel.GetById 42ms RepoModel.GetById 0ms app.codeStats 0ms

/marathon-support/looks-2.2.0/src/core/com/jgoodies/looks/LookUtils.java

https://github.com/jalian-systems/Marathon
Java | 492 lines | 171 code | 73 blank | 248 comment | 30 complexity | 79ce3e2d8cd31ac96559a0a188478a2e MD5 | raw file
  1. /*
  2. * Copyright (c) 2001-2008 JGoodies Karsten Lentzsch. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * o Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. *
  10. * o Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * o Neither the name of JGoodies Karsten Lentzsch nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  20. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  27. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  28. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. package com.jgoodies.looks;
  31. import java.awt.Color;
  32. import java.awt.Component;
  33. import java.awt.Toolkit;
  34. import java.util.Collections;
  35. import java.util.List;
  36. import java.util.Locale;
  37. import javax.swing.LookAndFeel;
  38. import javax.swing.UIManager;
  39. import javax.swing.UnsupportedLookAndFeelException;
  40. import com.jgoodies.looks.plastic.PlasticLookAndFeel;
  41. import com.jgoodies.looks.plastic.PlasticTheme;
  42. /**
  43. * Provides convenience behavior used by the JGoodies Looks.
  44. *
  45. * @author Karsten Lentzsch
  46. * @version $Revision: 1.12 $
  47. */
  48. public final class LookUtils {
  49. // Basics System Properties **********************************************
  50. /**
  51. * The <code>java.version</code> System Property.<p>
  52. *
  53. * Defaults to <code>null</code> if the runtime does not have security
  54. * access to read this property or the property does not exist.
  55. */
  56. private static final String JAVA_VERSION = getSystemProperty("java.version");
  57. /**
  58. * The <code>os.name</code> System Property. Operating system name.<p>
  59. *
  60. * Defaults to <code>null</code> if the runtime does not have security
  61. * access to read this property or the property does not exist.
  62. */
  63. private static final String OS_NAME = getSystemProperty("os.name");
  64. /**
  65. * The <code>os.version</code> System Property. Operating system version.<p>
  66. *
  67. * Defaults to <code>null</code> if the runtime does not have security
  68. * access to read this property or the property does not exist.
  69. */
  70. private static final String OS_VERSION = getSystemProperty("os.version");
  71. // Requesting the Java Version ********************************************
  72. /**
  73. * True if this is Java 1.4.
  74. */
  75. public static final boolean IS_JAVA_1_4 =
  76. startsWith(JAVA_VERSION, "1.4");
  77. /**
  78. * True if this is Java 1.4.0_*.
  79. */
  80. public static final boolean IS_JAVA_1_4_0 = startsWith(JAVA_VERSION, "1.4.0");
  81. /**
  82. * True if this is Java 1.4.2 or later. Since we assume Java 1.4
  83. * we just check for 1.4.0 and 1.4.1.
  84. */
  85. public static final boolean IS_JAVA_1_4_2_OR_LATER =
  86. !startsWith(JAVA_VERSION, "1.4.0")
  87. && !startsWith(JAVA_VERSION, "1.4.1");
  88. /**
  89. * True if this is Java 5.x. We check for a prefix of 1.5.
  90. */
  91. public static final boolean IS_JAVA_5 =
  92. startsWith(JAVA_VERSION, "1.5");
  93. /**
  94. * True if this is Java 5.x or later. Since we don't support Java 1.3,
  95. * we can check that it's not 1.4.
  96. */
  97. public static final boolean IS_JAVA_5_OR_LATER =
  98. !IS_JAVA_1_4;
  99. /**
  100. * True if this is Java 6. We check for a prefix of 1.6.
  101. */
  102. public static final boolean IS_JAVA_6 =
  103. startsWith(JAVA_VERSION, "1.6");
  104. /**
  105. * True if this is Java 6.x or later. Since we don't support Java 1.3,
  106. * we can check that it's neither 1.4 nor 1.5.
  107. */
  108. public static final boolean IS_JAVA_6_OR_LATER =
  109. !IS_JAVA_1_4
  110. && !IS_JAVA_5;
  111. /**
  112. * True if this is Java 1.4 or Java 5.
  113. */
  114. public static final boolean IS_JAVA_1_4_OR_5 =
  115. IS_JAVA_1_4 || IS_JAVA_5;
  116. // Requesting the Operating System Name ***********************************
  117. /**
  118. * True if this is FreeBSD.
  119. */
  120. public static final boolean IS_OS_FREEBSD =
  121. startsWithIgnoreCase(OS_NAME, "FreeBSD");
  122. /**
  123. * True if this is Linux.
  124. */
  125. public static final boolean IS_OS_LINUX =
  126. startsWithIgnoreCase(OS_NAME, "Linux");
  127. /**
  128. * True if this is OS/2.
  129. */
  130. public static final boolean IS_OS_OS2 =
  131. startsWith(OS_NAME, "OS/2");
  132. /**
  133. * True if this is the Mac OS X.
  134. */
  135. public static final boolean IS_OS_MAC =
  136. startsWith(OS_NAME, "Mac");
  137. /**
  138. * True if this is Windows.
  139. */
  140. public static final boolean IS_OS_WINDOWS =
  141. startsWith(OS_NAME, "Windows");
  142. /**
  143. * True if this is Windows 98/ME/2000/Server 2003/XP/VISTA/Server 2008.
  144. */
  145. public static final boolean IS_OS_WINDOWS_MODERN =
  146. startsWith(OS_NAME, "Windows") && !startsWith(OS_VERSION, "4.0");
  147. /**
  148. * True if this is Windows 95.
  149. *
  150. * @since 2.0
  151. */
  152. public static final boolean IS_OS_WINDOWS_95 =
  153. startsWith(OS_NAME, "Windows 9") && startsWith(OS_VERSION, "4.0");
  154. /**
  155. * True if this is Windows 98.
  156. *
  157. * @since 2.0
  158. */
  159. public static final boolean IS_OS_WINDOWS_98 =
  160. startsWith(OS_NAME, "Windows 9") && startsWith(OS_VERSION, "4.1");
  161. /**
  162. * True if this is Windows NT.
  163. *
  164. * @since 2.0
  165. */
  166. public static final boolean IS_OS_WINDOWS_NT =
  167. startsWith(OS_NAME, "Windows NT");
  168. /**
  169. * True if this is Windows ME.
  170. *
  171. * @since 2.0
  172. */
  173. public static final boolean IS_OS_WINDOWS_ME =
  174. startsWith(OS_NAME, "Windows") && startsWith(OS_VERSION, "4.9");
  175. /**
  176. * True if this is Windows 2000.
  177. *
  178. * @since 2.0
  179. */
  180. public static final boolean IS_OS_WINDOWS_2000 =
  181. startsWith(OS_NAME, "Windows") && startsWith(OS_VERSION, "5.0");
  182. /**
  183. * True if this is Windows XP.
  184. */
  185. public static final boolean IS_OS_WINDOWS_XP =
  186. startsWith(OS_NAME, "Windows") && startsWith(OS_VERSION, "5.1");
  187. /**
  188. * True if this is Windows Vista.
  189. *
  190. * @since 2.0
  191. */
  192. public static final boolean IS_OS_WINDOWS_VISTA =
  193. startsWith(OS_NAME, "Windows") && startsWith(OS_VERSION, "6.0");
  194. /**
  195. * True if this is Solaris.
  196. */
  197. public static final boolean IS_OS_SOLARIS =
  198. startsWith(OS_NAME, "Solaris");
  199. // Other Properties *******************************************************
  200. /**
  201. * True if the Windows XP Look&amp;Feel is enabled.
  202. */
  203. public static final boolean IS_LAF_WINDOWS_XP_ENABLED = isWindowsXPLafEnabled();
  204. /**
  205. * True if if the screen resolution is smaller than 120 dpi.
  206. *
  207. * @see Toolkit#getScreenResolution()
  208. */
  209. public static final boolean IS_LOW_RESOLUTION = isLowResolution();
  210. private static boolean loggingEnabled = true;
  211. private LookUtils() {
  212. // Override default constructor; prevents instantiation.
  213. }
  214. // Accessing System Configuration *****************************************
  215. /**
  216. * Tries to look up the System property for the given key.
  217. * In untrusted environments this may throw a SecurityException.
  218. * In this case we catch the exception and answer <code>null</code>.
  219. *
  220. * @param key the name of the system property
  221. * @return the system property's String value, or <code>null</code> if there's
  222. * no such value, or a SecurityException has been caught
  223. */
  224. public static String getSystemProperty(String key) {
  225. try {
  226. return System.getProperty(key);
  227. } catch (SecurityException e) {
  228. log("Can't read the System property " + key + ".");
  229. return null;
  230. }
  231. }
  232. /**
  233. * Tries to look up the System property for the given key.
  234. * In untrusted environments this may throw a SecurityException.
  235. * In this case, we catch the exception and answer the default value.
  236. *
  237. * @param key the name of the system property
  238. * @param defaultValue the default value if no property exists.
  239. * @return the system property's String value, or the defaultValue
  240. * if there's no such value, or a SecurityException has been caught
  241. */
  242. public static String getSystemProperty(String key, String defaultValue) {
  243. try {
  244. return System.getProperty(key, defaultValue);
  245. } catch (SecurityException e) {
  246. log("Can't read the System property " + key + ".");
  247. return defaultValue;
  248. }
  249. }
  250. /**
  251. * Checks if a boolean system property has been set for the given key,
  252. * and returns the associated Boolean, or <code>null</code> if no value
  253. * has been set. The test for the property ignores case.
  254. * If a Boolean value has been set, a message is logged
  255. * with the given prefix.
  256. *
  257. * @param key the key used to lookup the system property value
  258. * @param logMessage a prefix used when a message is logged
  259. * @return <code>Boolean.TRUE</code> if the system property has been set to
  260. * "true" (case ignored), <code>Boolean.FALSE</code> if it has been set to
  261. * "false", <code>null</code> otherwise
  262. */
  263. public static Boolean getBooleanSystemProperty(String key, String logMessage) {
  264. String value = getSystemProperty(key, "");
  265. Boolean result;
  266. if (value.equalsIgnoreCase("false"))
  267. result = Boolean.FALSE;
  268. else if (value.equalsIgnoreCase("true"))
  269. result = Boolean.TRUE;
  270. else
  271. result = null;
  272. if (result != null) {
  273. LookUtils.log(
  274. logMessage
  275. + " have been "
  276. + (result.booleanValue() ? "en" : "dis")
  277. + "abled in the system properties.");
  278. }
  279. return result;
  280. }
  281. /**
  282. * Checks and answers whether the Windows XP style is enabled.
  283. * This method is intended to be called only if a Windows look&feel
  284. * is about to be installed or already active in the UIManager.
  285. * The XP style of the Windows look&amp;feel is enabled by default on
  286. * Windows XP platforms since the J2SE 1.4.2; it can be disabled either
  287. * in the Windows desktop as well as in the Java runtime by setting
  288. * a System property.<p>
  289. *
  290. * First checks the platform, platform version and Java version. Then
  291. * checks whether the desktop property <tt>win.xpstyle.themeActive</tt>
  292. * is set or not.
  293. *
  294. * @return true if the Windows XP style is enabled
  295. */
  296. private static boolean isWindowsXPLafEnabled() {
  297. return (IS_OS_WINDOWS_XP || IS_OS_WINDOWS_VISTA)
  298. && IS_JAVA_1_4_2_OR_LATER
  299. && Boolean.TRUE.equals(Toolkit.getDefaultToolkit().
  300. getDesktopProperty("win.xpstyle.themeActive"))
  301. && getSystemProperty("swing.noxp") == null;
  302. }
  303. /**
  304. * Checks and answers whether we have a true color system.
  305. *
  306. * @param c the component used to determine the toolkit
  307. * @return true if the component's toolkit has a pixel size >= 24
  308. */
  309. public static boolean isTrueColor(Component c) {
  310. return c.getToolkit().getColorModel().getPixelSize() >= 24;
  311. }
  312. /**
  313. * Checks and answers whether this toolkit provides native drop shadows
  314. * for popups such as the Mac OS X. Currently this is used to
  315. * determine if the Looks' popup drop shadow feature is active or not
  316. * - even if it's enabled.
  317. *
  318. * @return true if the toolkit provides native drop shadows
  319. *
  320. * @see Options#isPopupDropShadowActive()
  321. */
  322. public static boolean getToolkitUsesNativeDropShadows() {
  323. return IS_OS_MAC;
  324. }
  325. /**
  326. * Computes and returns a Color that is slightly brighter
  327. * than the specified Color.
  328. *
  329. * @param color the color used as basis for the brightened color
  330. * @return a slightly brighter color
  331. */
  332. public static Color getSlightlyBrighter(Color color) {
  333. return getSlightlyBrighter(color, 1.1f);
  334. }
  335. /**
  336. * Computes and returns a Color that is slightly brighter
  337. * than the specified Color.
  338. *
  339. * @param color the color used as basis for the brightened color
  340. * @param factor the factor used to compute the brightness
  341. * @return a slightly brighter color
  342. */
  343. public static Color getSlightlyBrighter(Color color, float factor) {
  344. float[] hsbValues = new float[3];
  345. Color.RGBtoHSB(
  346. color.getRed(),
  347. color.getGreen(),
  348. color.getBlue(),
  349. hsbValues);
  350. float hue = hsbValues[0];
  351. float saturation = hsbValues[1];
  352. float brightness = hsbValues[2];
  353. float newBrightness = Math.min(brightness * factor, 1.0f);
  354. return Color.getHSBColor(hue, saturation, newBrightness);
  355. }
  356. // Accessing Look, Theme, and Font Settings *****************************
  357. public static void setLookAndTheme(LookAndFeel laf, Object theme)
  358. throws UnsupportedLookAndFeelException {
  359. if ((laf instanceof PlasticLookAndFeel)
  360. && (theme != null)
  361. && (theme instanceof PlasticTheme)) {
  362. PlasticLookAndFeel.setPlasticTheme((PlasticTheme) theme);
  363. }
  364. UIManager.setLookAndFeel(laf);
  365. }
  366. public static Object getDefaultTheme(LookAndFeel laf) {
  367. return laf instanceof PlasticLookAndFeel
  368. ? PlasticLookAndFeel.createMyDefaultTheme()
  369. : null;
  370. }
  371. public static List getInstalledThemes(LookAndFeel laf) {
  372. return laf instanceof PlasticLookAndFeel
  373. ? PlasticLookAndFeel.getInstalledThemes()
  374. : Collections.EMPTY_LIST;
  375. }
  376. // Minimal logging ******************************************************
  377. /**
  378. * Enables or disables the Looks logging.
  379. *
  380. * @param enabled true to enable logging, false to disable it
  381. */
  382. public static void setLoggingEnabled(boolean enabled) {
  383. loggingEnabled = enabled;
  384. }
  385. /**
  386. * Prints a new line to the console if logging is enabled.
  387. */
  388. public static void log() {
  389. if (loggingEnabled) {
  390. System.out.println();
  391. }
  392. }
  393. /**
  394. * Prints the given message to the console if logging is enabled.
  395. *
  396. * @param message the message to print
  397. */
  398. public static void log(String message) {
  399. if (loggingEnabled) {
  400. System.out.println("JGoodies Looks: " + message);
  401. }
  402. }
  403. // Private Helper Methods ***********************************************
  404. /**
  405. * Checks and answers whether the screen resolution is low or high.
  406. * Resolutions below 120 dpi are considere low, all others are high.
  407. *
  408. * @return true if the screen resolution is smaller than 120 dpi
  409. */
  410. private static boolean isLowResolution() {
  411. return Toolkit.getDefaultToolkit().getScreenResolution() < 120;
  412. }
  413. private static boolean startsWith(String str, String prefix) {
  414. return str != null && str.startsWith(prefix);
  415. }
  416. private static boolean startsWithIgnoreCase(String str, String prefix) {
  417. return str != null && str.toUpperCase(Locale.ENGLISH).startsWith(prefix.toUpperCase(Locale.ENGLISH));
  418. }
  419. }