/neon/src/main/java/org/pushingpixels/neon/internal/contrib/jgoodies/looks/LookUtils.java

https://github.com/kirill-grouchnikov/radiance · Java · 202 lines · 52 code · 26 blank · 124 comment · 14 complexity · cc02e089de6f66ddd7800230b561fb98 MD5 · raw file

  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.neon.internal.contrib.jgoodies.looks;
  31. import java.awt.*;
  32. /**
  33. * Provides convenience behavior used by the JGoodies Looks.
  34. *
  35. * @author Karsten Lentzsch
  36. */
  37. public final class LookUtils {
  38. // Basics System Properties **********************************************
  39. /**
  40. * The <code>os.name</code> System Property. Operating system name.
  41. * <p>
  42. * <p>
  43. * Defaults to <code>null</code> if the runtime does not have security
  44. * access to read this property or the property does not exist.
  45. */
  46. private static final String OS_NAME = getSystemProperty("os.name");
  47. /**
  48. * The <code>os.version</code> System Property. Operating system version.
  49. * <p>
  50. * <p>
  51. * Defaults to <code>null</code> if the runtime does not have security
  52. * access to read this property or the property does not exist.
  53. */
  54. private static final String OS_VERSION = getSystemProperty("os.version");
  55. // Requesting the Operating System Name ***********************************
  56. /**
  57. * True if this is the Mac OS X.
  58. */
  59. public static final boolean IS_OS_MAC = startsWith(OS_NAME, "Mac");
  60. /**
  61. * True if this is Windows.
  62. */
  63. public static final boolean IS_OS_WINDOWS = startsWith(OS_NAME, "Windows");
  64. /**
  65. * True if this is Windows 95.
  66. *
  67. * @since 2.0
  68. */
  69. public static final boolean IS_OS_WINDOWS_95 = startsWith(OS_NAME,
  70. "Windows 9")
  71. && startsWith(OS_VERSION, "4.0");
  72. /**
  73. * True if this is Windows 98.
  74. *
  75. * @since 2.0
  76. */
  77. public static final boolean IS_OS_WINDOWS_98 = startsWith(OS_NAME,
  78. "Windows 9")
  79. && startsWith(OS_VERSION, "4.1");
  80. /**
  81. * True if this is Windows NT.
  82. *
  83. * @since 2.0
  84. */
  85. public static final boolean IS_OS_WINDOWS_NT = startsWith(OS_NAME,
  86. "Windows NT");
  87. /**
  88. * True if this is Windows ME.
  89. *
  90. * @since 2.0
  91. */
  92. public static final boolean IS_OS_WINDOWS_ME = startsWith(OS_NAME,
  93. "Windows")
  94. && startsWith(OS_VERSION, "4.9");
  95. /**
  96. * True if this is Mac Yosemite.
  97. */
  98. public static final boolean IS_OS_MAC_YOSEMITE = IS_OS_MAC
  99. && startsWith(OS_VERSION, "10.10");
  100. /**
  101. * True if this is Mac El Capitan or later
  102. */
  103. public static final boolean IS_OS_MAC_EL_CAPITAN_OR_LATER = IS_OS_MAC
  104. && (startsWith(OS_VERSION, "10.11") || startsWith(OS_VERSION, "10.12")
  105. || startsWith(OS_VERSION, "10.13") || startsWith(OS_VERSION, "10.14"))
  106. || startsWith(OS_VERSION, "10.15");
  107. /**
  108. * True if this is Mac Mojave or later
  109. */
  110. public static final boolean IS_OS_MAC_MOJAVE_OR_LATER = IS_OS_MAC
  111. && (startsWith(OS_VERSION, "10.14") || startsWith(OS_VERSION, "10.15"));
  112. /**
  113. * True if this is Mac Mojave or later
  114. */
  115. public static final boolean IS_OS_MAC_CATALINA_OR_LATER = IS_OS_MAC
  116. && startsWith(OS_VERSION, "10.15");
  117. // Other Properties *******************************************************
  118. /**
  119. * True if if the screen resolution is smaller than 120 dpi.
  120. *
  121. * @see Toolkit#getScreenResolution()
  122. */
  123. public static final boolean IS_LOW_RESOLUTION = isLowResolution();
  124. private LookUtils() {
  125. // Override default constructor; prevents instantiation.
  126. }
  127. // Accessing System Configuration *****************************************
  128. /**
  129. * Tries to look up the System property for the given key. In untrusted
  130. * environments this may throw a SecurityException. In this case we catch
  131. * the exception and answer <code>null</code>.
  132. *
  133. * @param key the name of the system property
  134. * @return the system property's String value, or <code>null</code> if
  135. * there's no such value, or a SecurityException has been caught
  136. */
  137. public static String getSystemProperty(String key) {
  138. try {
  139. return System.getProperty(key);
  140. } catch (SecurityException e) {
  141. // log("Can't read the System property " + key + ".");
  142. return null;
  143. }
  144. }
  145. /**
  146. * Tries to look up the System property for the given key. In untrusted
  147. * environments this may throw a SecurityException. In this case, we catch
  148. * the exception and answer the default value.
  149. *
  150. * @param key the name of the system property
  151. * @param defaultValue the default value if no property exists.
  152. * @return the system property's String value, or the defaultValue if
  153. * there's no such value, or a SecurityException has been caught
  154. */
  155. public static String getSystemProperty(String key, String defaultValue) {
  156. try {
  157. return System.getProperty(key, defaultValue);
  158. } catch (SecurityException e) {
  159. // log("Can't read the System property " + key + ".");
  160. return defaultValue;
  161. }
  162. }
  163. // Private Helper Methods ***********************************************
  164. /**
  165. * Checks and answers whether the screen resolution is low or high.
  166. * Resolutions below 120 dpi are considere low, all others are high.
  167. *
  168. * @return true if the screen resolution is smaller than 120 dpi
  169. */
  170. private static boolean isLowResolution() {
  171. return Toolkit.getDefaultToolkit().getScreenResolution() < 120;
  172. }
  173. private static boolean startsWith(String str, String prefix) {
  174. return str != null && str.startsWith(prefix);
  175. }
  176. }