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

https://github.com/kirill-grouchnikov/radiance · Java · 163 lines · 42 code · 15 blank · 106 comment · 6 complexity · db122ffe8024ae06981ec404a9110e5e 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.substance.internal.contrib.jgoodies.looks;
  31. /**
  32. * Provides convenience behavior used by the JGoodies Looks.
  33. *
  34. * @author Karsten Lentzsch
  35. */
  36. public final class LookUtils {
  37. // Basics System Properties **********************************************
  38. /**
  39. * The <code>os.name</code> System Property. Operating system name.
  40. * <p>
  41. *
  42. * Defaults to <code>null</code> if the runtime does not have security
  43. * access to read this property or the property does not exist.
  44. */
  45. private static final String OS_NAME = getSystemProperty("os.name");
  46. /**
  47. * The <code>os.version</code> System Property. Operating system version.
  48. * <p>
  49. *
  50. * Defaults to <code>null</code> if the runtime does not have security
  51. * access to read this property or the property does not exist.
  52. */
  53. private static final String OS_VERSION = getSystemProperty("os.version");
  54. // Requesting the Operating System Name ***********************************
  55. /**
  56. * True if this is the Mac OS X.
  57. */
  58. public static final boolean IS_OS_MAC = startsWith(OS_NAME, "Mac");
  59. /**
  60. * True if this is Windows 98/ME/2000/XP/VISTA.
  61. */
  62. public static final boolean IS_OS_WINDOWS_MODERN = startsWith(OS_NAME,
  63. "Windows")
  64. && !startsWith(OS_VERSION, "4.0");
  65. private LookUtils() {
  66. // Override default constructor; prevents instantiation.
  67. }
  68. // Accessing System Configuration *****************************************
  69. /**
  70. * Tries to look up the System property for the given key. In untrusted
  71. * environments this may throw a SecurityException. In this case we catch
  72. * the exception and answer <code>null</code>.
  73. *
  74. * @param key
  75. * the name of the system property
  76. * @return the system property's String value, or <code>null</code> if
  77. * there's no such value, or a SecurityException has been caught
  78. */
  79. public static String getSystemProperty(String key) {
  80. try {
  81. return System.getProperty(key);
  82. } catch (SecurityException e) {
  83. // log("Can't read the System property " + key + ".");
  84. return null;
  85. }
  86. }
  87. /**
  88. * Tries to look up the System property for the given key. In untrusted
  89. * environments this may throw a SecurityException. In this case, we catch
  90. * the exception and answer the default value.
  91. *
  92. * @param key
  93. * the name of the system property
  94. * @param defaultValue
  95. * the default value if no property exists.
  96. * @return the system property's String value, or the defaultValue if
  97. * there's no such value, or a SecurityException has been caught
  98. */
  99. public static String getSystemProperty(String key, String defaultValue) {
  100. try {
  101. return System.getProperty(key, defaultValue);
  102. } catch (SecurityException e) {
  103. // log("Can't read the System property " + key + ".");
  104. return defaultValue;
  105. }
  106. }
  107. /**
  108. * Checks if a boolean system property has been set for the given key, and
  109. * returns the associated Boolean, or <code>null</code> if no value has been
  110. * set. The test for the property ignores case. If a Boolean value has been
  111. * set, a message is logged with the given prefix.
  112. *
  113. * @param key
  114. * the key used to lookup the system property value
  115. * @param logMessage
  116. * a prefix used when a message is logged
  117. * @return <code>Boolean.TRUE</code> if the system property has been set to
  118. * "true" (case ignored), <code>Boolean.FALSE</code> if it has been
  119. * set to "false", <code>null</code> 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. return result;
  131. }
  132. /**
  133. * Checks and answers whether this toolkit provides native drop shadows for
  134. * popups such as the Mac OS X. Currently this is used to determine if the
  135. * Looks' popup drop shadow feature is active or not - even if it's enabled.
  136. *
  137. * @return true if the toolkit provides native drop shadows
  138. *
  139. * @see Options#isPopupDropShadowActive()
  140. */
  141. public static boolean getToolkitUsesNativeDropShadows() {
  142. return IS_OS_MAC;
  143. }
  144. // Private Helper Methods ***********************************************
  145. private static boolean startsWith(String str, String prefix) {
  146. return str != null && str.startsWith(prefix);
  147. }
  148. }