PageRenderTime 1400ms CodeModel.GetById 51ms RepoModel.GetById 1ms app.codeStats 0ms

/JGoodies/src/com/jgoodies/looks/LookUtils.java

https://bitbucket.org/djarvis/opcionfontviewer
Java | 276 lines | 111 code | 36 blank | 129 comment | 14 complexity | 0b7ef32ff7351570cd4b30e8043cb508 MD5 | raw file
  1. /*
  2. * Copyright (c) 2001-2013 JGoodies Software GmbH. 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 Software GmbH 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.util.Collections;
  34. import java.util.List;
  35. import javax.swing.LookAndFeel;
  36. import javax.swing.UIManager;
  37. import javax.swing.UnsupportedLookAndFeelException;
  38. import com.jgoodies.common.base.SystemUtils;
  39. import com.jgoodies.looks.plastic.PlasticLookAndFeel;
  40. import com.jgoodies.looks.plastic.PlasticTheme;
  41. /**
  42. * Provides convenience behavior used by the JGoodies Looks.
  43. *
  44. * @author Karsten Lentzsch
  45. * @version $Revision: 1.18 $
  46. */
  47. public final class LookUtils extends SystemUtils {
  48. // Basics System Properties **********************************************
  49. /**
  50. * True if this is Windows 98/ME/2000/Server 2003/XP/VISTA/Server 2008.
  51. */
  52. public static final boolean IS_OS_WINDOWS_MODERN =
  53. startsWith(OS_NAME, "Windows") && !startsWith(OS_VERSION, "4.0");
  54. /**
  55. * True if this is Windows 95.
  56. *
  57. * @since 2.0
  58. */
  59. public static final boolean IS_OS_WINDOWS_95 =
  60. startsWith(OS_NAME, "Windows 9") && startsWith(OS_VERSION, "4.0");
  61. /**
  62. * True if this is Windows NT.
  63. *
  64. * @since 2.0
  65. */
  66. public static final boolean IS_OS_WINDOWS_NT =
  67. startsWith(OS_NAME, "Windows NT");
  68. private static boolean loggingEnabled = true;
  69. private LookUtils() {
  70. // Override default constructor; prevents instantiation.
  71. }
  72. // Accessing System Configuration *****************************************
  73. /**
  74. * Tries to look up the System property for the given key.
  75. * In untrusted environments this may throw a SecurityException.
  76. * In this case we catch the exception and answer {@code null}.
  77. *
  78. * @param key the name of the system property
  79. * @return the system property's String value, or {@code null} if there's
  80. * no such value, or a SecurityException has been caught
  81. */
  82. public static String getSystemProperty(String key) {
  83. try {
  84. return System.getProperty(key);
  85. } catch (SecurityException e) {
  86. log("Can't read the System property " + key + ".");
  87. return null;
  88. }
  89. }
  90. /**
  91. * Tries to look up the System property for the given key.
  92. * In untrusted environments this may throw a SecurityException.
  93. * In this case, we catch the exception and answer the default value.
  94. *
  95. * @param key the name of the system property
  96. * @param defaultValue the default value if no property exists.
  97. * @return the system property's String value, or the defaultValue
  98. * if there's no such value, or a SecurityException has been caught
  99. */
  100. public static String getSystemProperty(String key, String defaultValue) {
  101. try {
  102. return System.getProperty(key, defaultValue);
  103. } catch (SecurityException e) {
  104. log("Can't read the System property " + key + ".");
  105. return defaultValue;
  106. }
  107. }
  108. /**
  109. * Checks if a boolean system property has been set for the given key,
  110. * and returns the associated Boolean, or {@code null} if no value
  111. * has been set. The test for the property ignores case.
  112. * If a Boolean value has been set, a message is logged
  113. * with the given prefix.
  114. *
  115. * @param key the key used to lookup the system property value
  116. * @param logMessage a prefix used when a message is logged
  117. * @return {@code Boolean.TRUE} if the system property has been set to
  118. * "true" (case ignored), {@code Boolean.FALSE} if it has been set to
  119. * "false", {@code null} otherwise
  120. */
  121. public static Boolean getBooleanSystemProperty(String key, String logMessage) {
  122. String value = getSystemProperty(key, "");
  123. Boolean result;
  124. if (value.equalsIgnoreCase("false")) {
  125. result = Boolean.FALSE;
  126. } else if (value.equalsIgnoreCase("true")) {
  127. result = Boolean.TRUE;
  128. } else {
  129. result = null;
  130. }
  131. if (result != null) {
  132. LookUtils.log(
  133. logMessage
  134. + " have been "
  135. + (result.booleanValue() ? "en" : "dis")
  136. + "abled in the system properties.");
  137. }
  138. return result;
  139. }
  140. /**
  141. * Checks and answers whether we have a true color system.
  142. *
  143. * @param c the component used to determine the toolkit
  144. * @return true if the component's toolkit has a pixel size >= 24
  145. */
  146. public static boolean isTrueColor(Component c) {
  147. return c.getToolkit().getColorModel().getPixelSize() >= 24;
  148. }
  149. /**
  150. * Checks and answers whether this toolkit provides native drop shadows
  151. * for popups such as the Mac OS X. Currently this is used to
  152. * determine if the Looks' popup drop shadow feature is active or not
  153. * - even if it's enabled.
  154. *
  155. * @return true if the toolkit provides native drop shadows
  156. *
  157. * @see Options#isPopupDropShadowActive()
  158. */
  159. public static boolean getToolkitUsesNativeDropShadows() {
  160. return IS_OS_MAC;
  161. }
  162. /**
  163. * Computes and returns a Color that is slightly brighter
  164. * than the specified Color.
  165. *
  166. * @param color the color used as basis for the brightened color
  167. * @return a slightly brighter color
  168. */
  169. public static Color getSlightlyBrighter(Color color) {
  170. return getSlightlyBrighter(color, 1.1f);
  171. }
  172. /**
  173. * Computes and returns a Color that is slightly brighter
  174. * than the specified Color.
  175. *
  176. * @param color the color used as basis for the brightened color
  177. * @param factor the factor used to compute the brightness
  178. * @return a slightly brighter color
  179. */
  180. public static Color getSlightlyBrighter(Color color, float factor) {
  181. float[] hsbValues = new float[3];
  182. Color.RGBtoHSB(
  183. color.getRed(),
  184. color.getGreen(),
  185. color.getBlue(),
  186. hsbValues);
  187. float hue = hsbValues[0];
  188. float saturation = hsbValues[1];
  189. float brightness = hsbValues[2];
  190. float newBrightness = Math.min(brightness * factor, 1.0f);
  191. return Color.getHSBColor(hue, saturation, newBrightness);
  192. }
  193. // Accessing Look, Theme, and Font Settings *****************************
  194. public static void setLookAndTheme(LookAndFeel laf, Object theme)
  195. throws UnsupportedLookAndFeelException {
  196. if (laf instanceof PlasticLookAndFeel
  197. && theme != null
  198. && theme instanceof PlasticTheme) {
  199. PlasticLookAndFeel.setPlasticTheme((PlasticTheme) theme);
  200. }
  201. UIManager.setLookAndFeel(laf);
  202. }
  203. public static Object getDefaultTheme(LookAndFeel laf) {
  204. return laf instanceof PlasticLookAndFeel
  205. ? PlasticLookAndFeel.createMyDefaultTheme()
  206. : null;
  207. }
  208. public static List getInstalledThemes(LookAndFeel laf) {
  209. return laf instanceof PlasticLookAndFeel
  210. ? PlasticLookAndFeel.getInstalledThemes()
  211. : Collections.EMPTY_LIST;
  212. }
  213. // Minimal logging ******************************************************
  214. /**
  215. * Enables or disables the Looks logging.
  216. *
  217. * @param enabled true to enable logging, false to disable it
  218. */
  219. public static void setLoggingEnabled(boolean enabled) {
  220. loggingEnabled = enabled;
  221. }
  222. /**
  223. * Prints a new line to the console if logging is enabled.
  224. */
  225. public static void log() {
  226. if (loggingEnabled) {
  227. System.out.println();
  228. }
  229. }
  230. /**
  231. * Prints the given message to the console if logging is enabled.
  232. *
  233. * @param message the message to print
  234. */
  235. public static void log(String message) {
  236. if (loggingEnabled) {
  237. System.out.println("JGoodies Looks: " + message);
  238. }
  239. }
  240. }