PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/JGoodies/src/com/jgoodies/common/base/SystemUtils.java

https://bitbucket.org/djarvis/opcionfontviewer
Java | 318 lines | 83 code | 69 blank | 166 comment | 15 complexity | ba43267875328ba64edd06de90546f45 MD5 | raw file
  1. /*
  2. * Copyright (c) 2009-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.common.base;
  31. import java.awt.HeadlessException;
  32. import java.awt.Toolkit;
  33. import java.util.logging.Logger;
  34. /**
  35. * Provides convenience behavior to determine the operating system
  36. * and Java version.
  37. *
  38. * @author Karsten Lentzsch
  39. * @version $Revision: 1.5 $
  40. */
  41. public class SystemUtils {
  42. // Internal Constants *****************************************************
  43. /**
  44. * The {@code os.name} System Property. Operating system name.<p>
  45. *
  46. * Defaults to {@code null}, if the runtime does not have security
  47. * access to read this property or the property does not exist.
  48. */
  49. protected static final String OS_NAME = getSystemProperty("os.name");
  50. /**
  51. * The {@code os.version} System Property. Operating system version.<p>
  52. *
  53. * Defaults to {@code null}, if the runtime does not have security
  54. * access to read this property or the property does not exist.
  55. */
  56. protected static final String OS_VERSION = getSystemProperty("os.version");
  57. /**
  58. * The {@code os.name} System Property. Operating system name.<p>
  59. *
  60. * Defaults to {@code null}, if the runtime does not have security
  61. * access to read this property or the property does not exist.
  62. */
  63. protected static final String JAVA_VERSION = getSystemProperty("java.version");
  64. // Requesting the OS and OS Version ***************************************
  65. /**
  66. * Is true if this is Linux.
  67. */
  68. public static final boolean IS_OS_LINUX =
  69. startsWith(OS_NAME, "Linux") || startsWith(OS_NAME, "LINUX");
  70. /**
  71. * True if this is the Mac OS.
  72. */
  73. public static final boolean IS_OS_MAC =
  74. startsWith(OS_NAME, "Mac OS");
  75. /**
  76. * True if this is Solaris.
  77. */
  78. public static final boolean IS_OS_SOLARIS =
  79. startsWith(OS_NAME, "Solaris");
  80. /**
  81. * True if this is Windows.
  82. */
  83. public static final boolean IS_OS_WINDOWS =
  84. startsWith(OS_NAME, "Windows");
  85. /**
  86. * True if this is Windows 98.
  87. */
  88. public static final boolean IS_OS_WINDOWS_98 =
  89. startsWith(OS_NAME, "Windows 9") && startsWith(OS_VERSION, "4.1");
  90. /**
  91. * True if this is Windows ME.
  92. */
  93. public static final boolean IS_OS_WINDOWS_ME =
  94. startsWith(OS_NAME, "Windows") && startsWith(OS_VERSION, "4.9");
  95. /**
  96. * True if this is Windows 2000.
  97. */
  98. public static final boolean IS_OS_WINDOWS_2000 =
  99. startsWith(OS_NAME, "Windows") && startsWith(OS_VERSION, "5.0");
  100. /**
  101. * True if this is Windows XP.
  102. */
  103. public static final boolean IS_OS_WINDOWS_XP =
  104. startsWith(OS_NAME, "Windows") && startsWith(OS_VERSION, "5.1");
  105. /**
  106. * True if this is Windows Vista or Server 2008.
  107. */
  108. public static final boolean IS_OS_WINDOWS_VISTA =
  109. startsWith(OS_NAME, "Windows") && startsWith(OS_VERSION, "6.0");
  110. /**
  111. * True if this is Windows 7.
  112. */
  113. public static final boolean IS_OS_WINDOWS_7 =
  114. startsWith(OS_NAME, "Windows") && startsWith(OS_VERSION, "6.1");
  115. /**
  116. * True if this is Windows 8.
  117. */
  118. public static final boolean IS_OS_WINDOWS_8 =
  119. startsWith(OS_NAME, "Windows") && startsWith(OS_VERSION, "6.2");
  120. /**
  121. * True if this is Windows Vista/Server 2008/7/2008 R2/8.
  122. */
  123. public static final boolean IS_OS_WINDOWS_6_OR_LATER =
  124. startsWith(OS_NAME, "Windows") && startsWith(OS_VERSION, "6.");
  125. // Requesting the Java Version ********************************************
  126. /**
  127. * True if this is Java 6. We check for a prefix of 1.6.
  128. */
  129. public static final boolean IS_JAVA_6 =
  130. startsWith(JAVA_VERSION, "1.6");
  131. /**
  132. * True if this is Java 7. We check for a prefix of 1.7.
  133. */
  134. public static final boolean IS_JAVA_7 =
  135. startsWith(JAVA_VERSION, "1.7");
  136. /**
  137. * True if this is Java 7.x or later. We check that it's not 1.6.
  138. */
  139. public static final boolean IS_JAVA_7_OR_LATER =
  140. !IS_JAVA_6;
  141. /**
  142. * True if this is Java 7. We check for a prefix of 1.7.
  143. *
  144. * @since 1.6
  145. */
  146. public static final boolean IS_JAVA_8 =
  147. startsWith(JAVA_VERSION, "1.8");
  148. /**
  149. * True if this is Java 8.x or later.
  150. * We check that it's neither 1.6 nor 1.7.
  151. *
  152. * @since 1.6
  153. */
  154. public static final boolean IS_JAVA_8_OR_LATER =
  155. !IS_JAVA_6 && !IS_JAVA_7;
  156. // Visual Properties ******************************************************
  157. /**
  158. * True since Java 6 update 10.
  159. *
  160. * @since 1.2
  161. */
  162. public static final boolean HAS_MODERN_RASTERIZER = hasModernRasterizer();
  163. /**
  164. * True if the Windows XP Look&amp;Feel is enabled.
  165. *
  166. * @since 1.2
  167. */
  168. public static final boolean IS_LAF_WINDOWS_XP_ENABLED = isWindowsXPLafEnabled();
  169. /**
  170. * Is true if this environment's default toolkit reports a screen resolution
  171. * below 120 dpi.<p>
  172. *
  173. * @since 1.2
  174. */
  175. public static final boolean IS_LOW_RESOLUTION = isLowResolution();
  176. // Internal ***************************************************************
  177. private static final String AWT_UTILITIES_CLASS_NAME =
  178. "com.sun.awt.AWTUtilities";
  179. protected SystemUtils() {
  180. // Override default constructor; prevents instantiation.
  181. }
  182. /**
  183. * Tries to look up the System property for the given key.
  184. * In untrusted environments this may throw a SecurityException.
  185. * In this case we catch the exception and answer an empty string.
  186. *
  187. * @param key the name of the system property
  188. * @return the system property's String value, or {@code null} if there's
  189. * no such value, or an empty String when
  190. * a SecurityException has been caught
  191. */
  192. protected static String getSystemProperty(String key) {
  193. try {
  194. return System.getProperty(key);
  195. } catch (SecurityException e) {
  196. Logger.getLogger(SystemUtils.class.getName()).warning(
  197. "Can't access the System property " + key + ".");
  198. return "";
  199. }
  200. }
  201. protected static boolean startsWith(String str, String prefix) {
  202. return str != null && str.startsWith(prefix);
  203. }
  204. /**
  205. * Checks and answers whether this Java runtime has a modern rasterizer
  206. * or not. More precisely this method aims to understand whether a good
  207. * or poor rasterizer is used. Sun's Java runtime has improved its
  208. * rasterizer in the 1.6 N series after build 12.
  209. *
  210. * @return {@code true} if the AWTUtilities class is available,
  211. * {@code false} if this class is not in the class path.
  212. */
  213. private static boolean hasModernRasterizer() {
  214. try {
  215. Class.forName(AWT_UTILITIES_CLASS_NAME);
  216. return true;
  217. } catch (ClassNotFoundException e) {
  218. return false;
  219. }
  220. }
  221. /**
  222. * Checks and answers whether the Windows XP style is enabled.
  223. * This method is intended to be called only if a Windows look&feel
  224. * is about to be installed or already active in the UIManager.
  225. * The XP style of the Windows look&amp;feel is enabled by default on
  226. * Windows XP platforms since the J2SE 1.4.2; it can be disabled either
  227. * in the Windows desktop as well as in the Java runtime by setting
  228. * a System property.<p>
  229. *
  230. * First checks the platform, platform version and Java version. Then
  231. * checks whether the desktop property <tt>win.xpstyle.themeActive</tt>
  232. * is set or not.
  233. *
  234. * @return true if the Windows XP style is enabled
  235. */
  236. private static boolean isWindowsXPLafEnabled() {
  237. return IS_OS_WINDOWS
  238. && Boolean.TRUE.equals(Toolkit.getDefaultToolkit().
  239. getDesktopProperty("win.xpstyle.themeActive"))
  240. && getSystemProperty("swing.noxp") == null;
  241. }
  242. private static boolean isLowResolution() {
  243. try {
  244. return Toolkit.getDefaultToolkit().getScreenResolution() < 120;
  245. } catch (HeadlessException e) {
  246. return true;
  247. }
  248. }
  249. }