PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/laf-widget/src/main/java/org/pushingpixels/lafwidget/utils/LookUtils.java

https://github.com/alexandraleahu/insubstantial
Java | 448 lines | 135 code | 54 blank | 259 comment | 26 complexity | e2270dadacaf0199e3cd3c498366ce42 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, CC-BY-SA-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. /*
  2. * Copyright (c) 2001-2006 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 org.pushingpixels.lafwidget.utils;
  31. import java.awt.*;
  32. import java.util.Locale;
  33. /**
  34. * Provides convenience behavior used by the JGoodies Looks.
  35. *
  36. * @author Karsten Lentzsch
  37. */
  38. public final class LookUtils {
  39. // Basics System Properties **********************************************
  40. /**
  41. * The <code>java.version</code> System Property.
  42. * <p>
  43. *
  44. * Defaults to <code>null</code> if the runtime does not have security
  45. * access to read this property or the property does not exist.
  46. */
  47. private static final String JAVA_VERSION = getSystemProperty("java.version");
  48. /**
  49. * The <code>os.name</code> System Property. Operating system name.
  50. * <p>
  51. *
  52. * Defaults to <code>null</code> if the runtime does not have security
  53. * access to read this property or the property does not exist.
  54. */
  55. private static final String OS_NAME = getSystemProperty("os.name");
  56. /**
  57. * The <code>os.version</code> System Property. Operating system version.
  58. * <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_VERSION = getSystemProperty("os.version");
  64. // Requesting the Java Version ********************************************
  65. /**
  66. * True if this is Java 1.4.
  67. */
  68. public static final boolean IS_JAVA_1_4 = startsWith(JAVA_VERSION, "1.4");
  69. /**
  70. * True if this is Java 1.4.0_*.
  71. */
  72. public static final boolean IS_JAVA_1_4_0 = startsWith(JAVA_VERSION,
  73. "1.4.0");
  74. /**
  75. * True if this is Java 1.4.2 or later. Since we assume Java 1.4 we just
  76. * check for 1.4.0 and 1.4.1.
  77. */
  78. public static final boolean IS_JAVA_1_4_2_OR_LATER = !startsWith(
  79. JAVA_VERSION, "1.4.0")
  80. && !startsWith(JAVA_VERSION, "1.4.1");
  81. /**
  82. * True if this is Java 5.x. We check for a prefix of 1.5.
  83. */
  84. public static final boolean IS_JAVA_5 = startsWith(JAVA_VERSION, "1.5");
  85. /**
  86. * True if this is Java 5.x or later. Since we don't support Java 1.3, we
  87. * can check that it's not 1.4.
  88. */
  89. public static final boolean IS_JAVA_5_OR_LATER = !IS_JAVA_1_4;
  90. /**
  91. * True if this is Java 6. We check for a prefix of 1.6.
  92. */
  93. public static final boolean IS_JAVA_6 = startsWith(JAVA_VERSION, "1.6");
  94. /**
  95. * True if this is Java 6.x or later. Since we don't support Java 1.3, we
  96. * can check that it's neither 1.4 nor 1.5.
  97. */
  98. public static final boolean IS_JAVA_6_OR_LATER = !IS_JAVA_1_4 && !IS_JAVA_5;
  99. /**
  100. * True if this is Java 1.4 or Java 5.
  101. */
  102. public static final boolean IS_JAVA_1_4_OR_5 = IS_JAVA_1_4 || IS_JAVA_5;
  103. // Requesting the Operating System Name ***********************************
  104. /**
  105. * True if this is FreeBSD.
  106. */
  107. public static final boolean IS_OS_FREEBSD = startsWithIgnoreCase(OS_NAME,
  108. "FreeBSD");
  109. /**
  110. * True if this is Linux.
  111. */
  112. public static final boolean IS_OS_LINUX = startsWithIgnoreCase(OS_NAME,
  113. "Linux");
  114. /**
  115. * True if this is OS/2.
  116. */
  117. public static final boolean IS_OS_OS2 = startsWith(OS_NAME, "OS/2");
  118. /**
  119. * True if this is the Mac OS X.
  120. */
  121. public static final boolean IS_OS_MAC = startsWith(OS_NAME, "Mac");
  122. /**
  123. * True if this is Windows.
  124. */
  125. public static final boolean IS_OS_WINDOWS = startsWith(OS_NAME, "Windows");
  126. /**
  127. * True if this is Windows 98/ME/2000/XP/VISTA.
  128. */
  129. public static final boolean IS_OS_WINDOWS_MODERN = startsWith(OS_NAME,
  130. "Windows")
  131. && !startsWith(OS_VERSION, "4.0");
  132. /**
  133. * True if this is Windows 95.
  134. *
  135. * @since 2.0
  136. */
  137. public static final boolean IS_OS_WINDOWS_95 = startsWith(OS_NAME,
  138. "Windows 9")
  139. && startsWith(OS_VERSION, "4.0");
  140. /**
  141. * True if this is Windows 98.
  142. *
  143. * @since 2.0
  144. */
  145. public static final boolean IS_OS_WINDOWS_98 = startsWith(OS_NAME,
  146. "Windows 9")
  147. && startsWith(OS_VERSION, "4.1");
  148. /**
  149. * True if this is Windows NT.
  150. *
  151. * @since 2.0
  152. */
  153. public static final boolean IS_OS_WINDOWS_NT = startsWith(OS_NAME,
  154. "Windows NT");
  155. /**
  156. * True if this is Windows ME.
  157. *
  158. * @since 2.0
  159. */
  160. public static final boolean IS_OS_WINDOWS_ME = startsWith(OS_NAME,
  161. "Windows")
  162. && startsWith(OS_VERSION, "4.9");
  163. /**
  164. * True if this is Windows 2000.
  165. *
  166. * @since 2.0
  167. */
  168. public static final boolean IS_OS_WINDOWS_2000 = startsWith(OS_NAME,
  169. "Windows")
  170. && startsWith(OS_VERSION, "5.0");
  171. /**
  172. * True if this is Windows XP.
  173. */
  174. public static final boolean IS_OS_WINDOWS_XP = startsWith(OS_NAME,
  175. "Windows")
  176. && startsWith(OS_VERSION, "5.1");
  177. /**
  178. * True if this is Windows Vista.
  179. *
  180. * @since 2.0
  181. */
  182. public static final boolean IS_OS_WINDOWS_VISTA = startsWith(OS_NAME,
  183. "Windows")
  184. && startsWith(OS_VERSION, "6.0");
  185. /**
  186. * True if this is Solaris.
  187. */
  188. public static final boolean IS_OS_SOLARIS = startsWith(OS_NAME, "Solaris");
  189. // Other Properties *******************************************************
  190. /**
  191. * True if the Windows XP Look&amp;Feel is enabled.
  192. */
  193. public static final boolean IS_LAF_WINDOWS_XP_ENABLED = isWindowsXPLafEnabled();
  194. /**
  195. * True if if the screen resolution is smaller than 120 dpi.
  196. *
  197. * @see Toolkit#getScreenResolution()
  198. */
  199. public static final boolean IS_LOW_RESOLUTION = isLowResolution();
  200. private static boolean loggingEnabled = true;
  201. private LookUtils() {
  202. // Override default constructor; prevents instantiation.
  203. }
  204. // Accessing System Configuration *****************************************
  205. /**
  206. * Tries to look up the System property for the given key. In untrusted
  207. * environments this may throw a SecurityException. In this case we catch
  208. * the exception and answer <code>null</code>.
  209. *
  210. * @param key
  211. * the name of the system property
  212. * @return the system property's String value, or <code>null</code> if
  213. * there's no such value, or a SecurityException has been caught
  214. */
  215. public static String getSystemProperty(String key) {
  216. try {
  217. return System.getProperty(key);
  218. } catch (SecurityException e) {
  219. // log("Can't read the System property " + key + ".");
  220. return null;
  221. }
  222. }
  223. /**
  224. * Tries to look up the System property for the given key. In untrusted
  225. * environments this may throw a SecurityException. In this case, we catch
  226. * the exception and answer the default value.
  227. *
  228. * @param key
  229. * the name of the system property
  230. * @param defaultValue
  231. * the default value if no property exists.
  232. * @return the system property's String value, or the defaultValue if
  233. * there's no such value, or a SecurityException has been caught
  234. */
  235. public static String getSystemProperty(String key, String defaultValue) {
  236. try {
  237. return System.getProperty(key, defaultValue);
  238. } catch (SecurityException e) {
  239. // log("Can't read the System property " + key + ".");
  240. return defaultValue;
  241. }
  242. }
  243. /**
  244. * Checks if a boolean system property has been set for the given key, and
  245. * returns the associated Boolean, or <code>null</code> if no value has been
  246. * set. The test for the property ignores case. If a Boolean value has been
  247. * set, a message is logged with the given prefix.
  248. *
  249. * @param key
  250. * the key used to lookup the system property value
  251. * @param logMessage
  252. * a prefix used when a message is logged
  253. * @return <code>Boolean.TRUE</code> if the system property has been set to
  254. * "true" (case ignored), <code>Boolean.FALSE</code> if it has been
  255. * set to "false", <code>null</code> otherwise
  256. */
  257. public static Boolean getBooleanSystemProperty(String key, String logMessage) {
  258. String value = getSystemProperty(key, "");
  259. Boolean result;
  260. if (value.equalsIgnoreCase("false"))
  261. result = Boolean.FALSE;
  262. else if (value.equalsIgnoreCase("true"))
  263. result = Boolean.TRUE;
  264. else
  265. result = null;
  266. if (result != null) {
  267. LookUtils.log(logMessage + " have been "
  268. + (result ? "en" : "dis")
  269. + "abled in the system properties.");
  270. }
  271. return result;
  272. }
  273. /**
  274. * Checks and answers whether the Windows XP style is enabled. This method
  275. * is intended to be called only if a Windows look&feel is about to be
  276. * installed or already active in the UIManager. The XP style of the Windows
  277. * look&amp;feel is enabled by default on Windows XP platforms since the
  278. * J2SE 1.4.2; it can be disabled either in the Windows desktop as well as
  279. * in the Java runtime by setting a System property.
  280. * <p>
  281. *
  282. * First checks the platform, platform version and Java version. Then checks
  283. * whether the desktop property <tt>win.xpstyle.themeActive</tt> is set or
  284. * not.
  285. *
  286. * @return true if the Windows XP style is enabled
  287. */
  288. private static boolean isWindowsXPLafEnabled() {
  289. return (IS_OS_WINDOWS_XP || IS_OS_WINDOWS_VISTA)
  290. && IS_JAVA_1_4_2_OR_LATER
  291. && Boolean.TRUE.equals(Toolkit.getDefaultToolkit()
  292. .getDesktopProperty("win.xpstyle.themeActive"))
  293. && getSystemProperty("swing.noxp") == null;
  294. }
  295. /**
  296. * Checks and answers whether we have a true color system.
  297. *
  298. * @param c
  299. * the component used to determine the toolkit
  300. * @return true if the component's toolkit has a pixel size >= 24
  301. */
  302. public static boolean isTrueColor(Component c) {
  303. return c.getToolkit().getColorModel().getPixelSize() >= 24;
  304. }
  305. /**
  306. * Checks and answers whether this toolkit provides native drop shadows for
  307. * popups such as the Mac OS X. Currently this is used to determine if the
  308. * Looks' popup drop shadow feature is active or not - even if it's enabled.
  309. *
  310. * @return true if the toolkit provides native drop shadows
  311. *
  312. */
  313. public static boolean getToolkitUsesNativeDropShadows() {
  314. return IS_OS_MAC;
  315. }
  316. /**
  317. * Computes and returns a Color that is slightly brighter than the specified
  318. * Color.
  319. *
  320. * @param color
  321. * the color used as basis for the brightened color
  322. * @return a slightly brighter color
  323. */
  324. public static Color getSlightlyBrighter(Color color) {
  325. return getSlightlyBrighter(color, 1.1f);
  326. }
  327. /**
  328. * Computes and returns a Color that is slightly brighter than the specified
  329. * Color.
  330. *
  331. * @param color
  332. * the color used as basis for the brightened color
  333. * @param factor
  334. * the factor used to compute the brightness
  335. * @return a slightly brighter color
  336. */
  337. public static Color getSlightlyBrighter(Color color, float factor) {
  338. float[] hsbValues = new float[3];
  339. Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(),
  340. hsbValues);
  341. float hue = hsbValues[0];
  342. float saturation = hsbValues[1];
  343. float brightness = hsbValues[2];
  344. float newBrightness = Math.min(brightness * factor, 1.0f);
  345. return Color.getHSBColor(hue, saturation, newBrightness);
  346. }
  347. // Minimal logging ******************************************************
  348. /**
  349. * Enables or disables the Looks logging.
  350. *
  351. * @param enabled
  352. * true to enable logging, false to disable it
  353. */
  354. public static void setLoggingEnabled(boolean enabled) {
  355. loggingEnabled = enabled;
  356. }
  357. /**
  358. * Prints a new line to the console if logging is enabled.
  359. */
  360. public static void log() {
  361. if (loggingEnabled) {
  362. System.out.println();
  363. }
  364. }
  365. /**
  366. * Prints the given message to the console if logging is enabled.
  367. *
  368. * @param message
  369. * the message to print
  370. */
  371. public static void log(String message) {
  372. if (loggingEnabled) {
  373. System.out.println("JGoodies Looks: " + message);
  374. }
  375. }
  376. // Private Helper Methods ***********************************************
  377. /**
  378. * Checks and answers whether the screen resolution is low or high.
  379. * Resolutions below 120 dpi are considere low, all others are high.
  380. *
  381. * @return true if the screen resolution is smaller than 120 dpi
  382. */
  383. private static boolean isLowResolution() {
  384. return Toolkit.getDefaultToolkit().getScreenResolution() < 120;
  385. }
  386. private static boolean startsWith(String str, String prefix) {
  387. return str != null && str.startsWith(prefix);
  388. }
  389. private static boolean startsWithIgnoreCase(String str, String prefix) {
  390. return str != null
  391. && str.toUpperCase(Locale.ENGLISH).startsWith(
  392. prefix.toUpperCase(Locale.ENGLISH));
  393. }
  394. }