PageRenderTime 1053ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jars-src/looks/src/com/jgoodies/plaf/LookUtils.java

https://bitbucket.org/wrapman/frostwire.desktop.translations.pirate
Java | 432 lines | 166 code | 62 blank | 204 comment | 24 complexity | debb3e65cb31a11ac9994323de2be016 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception
  1. /*
  2. * Copyright (c) 2001-2004 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.plaf;
  31. import java.awt.Color;
  32. import java.awt.Component;
  33. import java.awt.Insets;
  34. import java.awt.Toolkit;
  35. import java.util.Collections;
  36. import java.util.List;
  37. import javax.swing.AbstractButton;
  38. import javax.swing.LookAndFeel;
  39. import javax.swing.UIManager;
  40. import javax.swing.UnsupportedLookAndFeelException;
  41. import javax.swing.plaf.InsetsUIResource;
  42. import javax.swing.plaf.UIResource;
  43. import com.jgoodies.clearlook.ClearLookManager;
  44. import com.jgoodies.plaf.plastic.PlasticLookAndFeel;
  45. import com.jgoodies.plaf.plastic.PlasticTheme;
  46. /**
  47. * Provides convenience behavior used by the JGoodies Looks.
  48. *
  49. * @author Karsten Lentzsch
  50. * @version $revision: $
  51. *
  52. * @see com.jgoodies.plaf.FontUtils
  53. */
  54. public final class LookUtils {
  55. // Basics System Properties **********************************************
  56. /**
  57. * The <code>java.version</code> System Property.<p>
  58. *
  59. * Defaults to <code>null</code> if the runtime does not have security
  60. * access to read this property or the property does not exist.
  61. */
  62. private static final String JAVA_VERSION = getSystemProperty("java.version");
  63. /**
  64. * The <code>os.name</code> System Property. Operating system name.<p>
  65. *
  66. * Defaults to <code>null</code> if the runtime does not have security
  67. * access to read this property or the property does not exist.
  68. */
  69. private static final String OS_NAME = getSystemProperty("os.name");
  70. /**
  71. * The <code>os.version</code> System Property. Operating system version.<p>
  72. *
  73. * Defaults to <code>null</code> if the runtime does not have security
  74. * access to read this property or the property does not exist.
  75. */
  76. private static final String OS_VERSION = getSystemProperty("os.version");
  77. // Requesting the Java Version ********************************************
  78. /**
  79. * True if this is Java 1.4.
  80. */
  81. public static final boolean IS_JAVA_1_4 =
  82. startsWith(JAVA_VERSION, "1.4");
  83. /**
  84. * True if this is Java 1.4.0_*.
  85. */
  86. static final boolean IS_JAVA_1_4_0 = startsWith(JAVA_VERSION, "1.4.0");
  87. /**
  88. * True if this is Java 1.4.2 or later. Since we assume Java 1.4
  89. * we just check for 1.4.0 and 1.4.1.
  90. */
  91. public static final boolean IS_JAVA_1_4_2_OR_LATER =
  92. !startsWith(JAVA_VERSION, "1.4.0") &&
  93. !startsWith(JAVA_VERSION, "1.4.1");
  94. // Requesting the Operating System Name ***********************************
  95. /**
  96. * True if this is FreeBSD.
  97. */
  98. public static final boolean IS_OS_FREEBSD =
  99. startsWithIgnoreCase(OS_NAME, "FreeBSD");
  100. /**
  101. * True if this is Linux.
  102. */
  103. public static final boolean IS_OS_LINUX =
  104. startsWithIgnoreCase(OS_NAME, "Linux");
  105. /**
  106. * True if this is OS/2.
  107. */
  108. public static final boolean IS_OS_OS2 =
  109. startsWith(OS_NAME, "OS/2");
  110. /**
  111. * True if this is the Mac OS X.
  112. */
  113. public static final boolean IS_OS_MAC =
  114. startsWith(OS_NAME, "Mac");
  115. /**
  116. * True if this is Windows.
  117. */
  118. public static final boolean IS_OS_WINDOWS =
  119. startsWith(OS_NAME, "Windows");
  120. /**
  121. * True if this is Windows 98/ME/2000/XP.
  122. */
  123. public static final boolean IS_OS_WINDOWS_MODERN =
  124. startsWith(OS_NAME, "Windows") && !startsWith(OS_VERSION, "4.0");
  125. /**
  126. * True if this is Windows XP.
  127. */
  128. public static final boolean IS_OS_WINDOWS_XP =
  129. startsWith(OS_NAME, "Windows") && startsWith(OS_VERSION, "5.1");
  130. /**
  131. * True if this is Solaris.
  132. */
  133. public static final boolean IS_OS_SOLARIS =
  134. startsWith(OS_NAME, "Solaris");
  135. // Other Properties *******************************************************
  136. /**
  137. * True if the Windows XP Look&amp;Feel is enabled.
  138. */
  139. public static final boolean IS_LAF_WINDOWS_XP_ENABLED = isWindowsXPLafEnabled();
  140. /**
  141. * True if this is a NetBeans environment.
  142. */
  143. public static final boolean IS_NETBEANS;
  144. public static final boolean IS_LOW_RESOLUTION = isLowResolution();
  145. private static boolean loggingEnabled;
  146. static {
  147. loggingEnabled = true;
  148. IS_NETBEANS = isNetBeans();
  149. if (IS_NETBEANS) {
  150. // Force the ClearLookManager to immediately log messages.
  151. ClearLookManager.getMode();
  152. }
  153. }
  154. private LookUtils() {
  155. // Override default constructor; prevents instantiation.
  156. }
  157. // Accessing System Configuration *****************************************
  158. /**
  159. * Tries to look up the System property for the given key.
  160. * In untrusted environments this may throw a SecurityException.
  161. * In this case we catch the exception and answer <code>null</code>.
  162. *
  163. * @param key the name of the system property
  164. * @return the system property's String value, or <code>null</code> if there's
  165. * no such value, or a SecurityException has been catched
  166. */
  167. public static String getSystemProperty(String key) {
  168. try {
  169. return System.getProperty(key);
  170. } catch (SecurityException e) {
  171. log("Can't read the System property " + key + ".");
  172. return null;
  173. }
  174. }
  175. /**
  176. * Tries to look up the System property for the given key.
  177. * In untrusted environments this may throw a SecurityException.
  178. * In this case, we catch the exception and answer the default value.
  179. *
  180. * @param key the name of the system property
  181. * @param defaultValue the default value if no property exists.
  182. * @return the system property's String value, or the defaultValue
  183. * if there's no such value, or a SecurityException has been catched
  184. */
  185. public static String getSystemProperty(String key, String defaultValue) {
  186. try {
  187. return System.getProperty(key, defaultValue);
  188. } catch (SecurityException e) {
  189. log("Can't read the System property " + key + ".");
  190. return defaultValue;
  191. }
  192. }
  193. /**
  194. * Checks and answers whether the Windows XP style is enabled.
  195. * This method is intended to be called only if a Windows look&feel
  196. * is about to be installed or already active in the UIManager.
  197. * The XP style of the Windows look&amp;feel is enabled by default on
  198. * Windows XP platforms since the J2SE 1.4.2; it can be disabled either
  199. * in the Windows desktop as well as in the Java runtime by setting
  200. * a System property.<p>
  201. *
  202. * First checks the platform, platform version and Java version. Then
  203. * checks whether the desktop property <tt>win.xpstyle.themeActive</tt>
  204. * is set or not.
  205. *
  206. * @return true if the Windows XP style is enabled
  207. */
  208. private static boolean isWindowsXPLafEnabled() {
  209. return IS_OS_WINDOWS_XP
  210. && IS_JAVA_1_4_2_OR_LATER
  211. && Boolean.TRUE.equals(Toolkit.getDefaultToolkit().
  212. getDesktopProperty("win.xpstyle.themeActive"))
  213. && getSystemProperty("swing.noxp") == null;
  214. }
  215. /**
  216. * Checks and answers whether we have a true color system.
  217. *
  218. * @param c the component used to determine the toolkit
  219. * @return true if the component's toolkit has a pixel size >= 24
  220. */
  221. public static boolean isTrueColor(Component c) {
  222. return c.getToolkit().getColorModel().getPixelSize() >= 24;
  223. }
  224. // Working with Button Margins ******************************************
  225. /**
  226. * Installs a narrow margin, if property <code>isNarrow</code> has been set.
  227. *
  228. * @param b the button that shall get a narrow margin
  229. * @param propertyPrefix the component type prefeix for the UIDefaults
  230. */
  231. public static void installNarrowMargin(
  232. AbstractButton b,
  233. String propertyPrefix) {
  234. Object value = b.getClientProperty(Options.IS_NARROW_KEY);
  235. boolean isNarrow = Boolean.TRUE.equals(value);
  236. String defaultsKey =
  237. propertyPrefix + (isNarrow ? "narrowMargin" : "margin");
  238. Insets insets = b.getMargin();
  239. if (insets == null || insets instanceof UIResource) {
  240. b.setMargin(UIManager.getInsets(defaultsKey));
  241. }
  242. }
  243. /**
  244. * Creates and answers the margin used by <code>JButton</code>
  245. * and <code>JToggleButton</code>. Honors the screen resolution
  246. * and the global <code>isNarrowButtonsEnabled</code> property.<p>
  247. *
  248. * Sun's L&F implementations use wide button margins.
  249. * @see Options#getUseNarrowButtons()
  250. *
  251. * @param narrow true to create a narrow margin, false for a wide margin
  252. * @return an Insets object used to create a button margin
  253. */
  254. public static Insets createButtonMargin(boolean narrow) {
  255. int pad = narrow || Options.getUseNarrowButtons() ? 4 : 14;
  256. return IS_LOW_RESOLUTION
  257. ? new InsetsUIResource(2, pad, 1, pad)
  258. : new InsetsUIResource(3, pad, 3, pad);
  259. }
  260. // Color Modifications **************************************************
  261. /**
  262. * Computes and answers a <code>Color</code> that is slightly brighter
  263. * than the specified <code>Color</code>. Required for 1.3 only.
  264. *
  265. * @param color the color used as basis for the brightened color
  266. * @return a slightly brighter color
  267. */
  268. public static Color getSlightlyBrighter(Color color) {
  269. return getSlightlyBrighter(color, 1.1f);
  270. }
  271. /**
  272. * Computes and answers a <code>Color</code> that is slightly brighter
  273. * than the specified <code>Color</code>. Required for 1.3 only.
  274. *
  275. * @param color the color used as basis for the brightened color
  276. * @param factor the factor used to compute the brightness
  277. * @return a slightly brighter color
  278. */
  279. public static Color getSlightlyBrighter(Color color, float factor) {
  280. float[] hsbValues = new float[3];
  281. Color.RGBtoHSB(
  282. color.getRed(),
  283. color.getGreen(),
  284. color.getBlue(),
  285. hsbValues);
  286. float hue = hsbValues[0];
  287. float saturation = hsbValues[1];
  288. float brightness = hsbValues[2];
  289. float newBrightness = Math.min(brightness * factor, 1.0f);
  290. return Color.getHSBColor(hue, saturation, newBrightness);
  291. }
  292. // Accessing Look, Theme, and Font Settings *****************************
  293. public static void setLookAndTheme(LookAndFeel laf, Object theme)
  294. throws UnsupportedLookAndFeelException {
  295. if ((laf instanceof PlasticLookAndFeel)
  296. && (theme != null)
  297. && (theme instanceof PlasticTheme)) {
  298. PlasticLookAndFeel.setMyCurrentTheme((PlasticTheme) theme);
  299. }
  300. UIManager.setLookAndFeel(laf);
  301. }
  302. public static Object getDefaultTheme(LookAndFeel laf) {
  303. return laf instanceof PlasticLookAndFeel
  304. ? PlasticLookAndFeel.createMyDefaultTheme()
  305. : null;
  306. }
  307. public static List getInstalledThemes(LookAndFeel laf) {
  308. return laf instanceof PlasticLookAndFeel
  309. ? PlasticLookAndFeel.getInstalledThemes()
  310. : Collections.EMPTY_LIST;
  311. }
  312. // Minimal logging ******************************************************
  313. /**
  314. * Enables or disables the logging.
  315. *
  316. * @param enabled true to enable logging, false to disable it
  317. */
  318. public static void setLoggingEnabled(boolean enabled) {
  319. loggingEnabled = enabled;
  320. }
  321. /**
  322. * Prints a new line to the console if logging is enabled.
  323. */
  324. public static void log() {
  325. if (loggingEnabled) {
  326. System.out.println();
  327. }
  328. }
  329. /**
  330. * Prints the given message to the console if logging is enabled.
  331. *
  332. * @param message the message to print
  333. */
  334. public static void log(String message) {
  335. if (loggingEnabled) {
  336. System.out.println("JGoodies Looks: " + message);
  337. }
  338. }
  339. // Private Helper Methods ***********************************************
  340. /**
  341. * Checks and answers whether the screen resolution is low or high.
  342. * Resolutions below 120 dpi are considere low, all others are high.
  343. *
  344. * @return true if the screen resolution is smaller than 120 dpi
  345. */
  346. private static boolean isLowResolution() {
  347. return Toolkit.getDefaultToolkit().getScreenResolution() < 120;
  348. }
  349. /**
  350. * Checks and answers whether we are in the Netbeans IDE
  351. * by looking for a NetBeans buildnumber.
  352. *
  353. * @return true if NetBeans could be detected, false otherwise
  354. */
  355. private static boolean isNetBeans() {
  356. String property = getSystemProperty("netbeans.buildnumber");
  357. boolean hasNetBeansBuildNumber =
  358. property != null && property.length() > 0;
  359. if (hasNetBeansBuildNumber) {
  360. log("NetBeans detected - dobry den!");
  361. }
  362. return hasNetBeansBuildNumber;
  363. }
  364. private static boolean startsWith(String str, String prefix) {
  365. return str != null && str.startsWith(prefix);
  366. }
  367. private static boolean startsWithIgnoreCase(String str, String prefix) {
  368. return str != null && str.toUpperCase().startsWith(prefix.toUpperCase());
  369. }
  370. }