Debian Codegraph @

laf-widget/src/main/java/org/pushingpixels/lafwidget/utils/LookUtils.java

raw
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
31 package org.pushingpixels.lafwidget.utils;
32
33 import java.awt.*;
34 import java.util.Locale;
35
36 /**
37 * Provides convenience behavior used by the JGoodies Looks.
38 *
39 * @author Karsten Lentzsch
40 */
41 public final class LookUtils {
42
43 // Basics System Properties **********************************************
44
45 /**
46 * The <code>java.version</code> System Property.
47 * <p>
48 *
49 * Defaults to <code>null</code> if the runtime does not have security
50 * access to read this property or the property does not exist.
51 */
52 private static final String JAVA_VERSION = getSystemProperty("java.version");
53
54 /**
55 * The <code>os.name</code> System Property. Operating system name.
56 * <p>
57 *
58 * Defaults to <code>null</code> if the runtime does not have security
59 * access to read this property or the property does not exist.
60 */
61 private static final String OS_NAME = getSystemProperty("os.name");
62
63 /**
64 * The <code>os.version</code> System Property. Operating system version.
65 * <p>
66 *
67 * Defaults to <code>null</code> if the runtime does not have security
68 * access to read this property or the property does not exist.
69 */
70 private static final String OS_VERSION = getSystemProperty("os.version");
71
72 // Requesting the Java Version ********************************************
73
74 /**
75 * True if this is Java 1.4.
76 */
77 public static final boolean IS_JAVA_1_4 = startsWith(JAVA_VERSION, "1.4");
78
79 /**
80 * True if this is Java 1.4.0_*.
81 */
82 public static final boolean IS_JAVA_1_4_0 = startsWith(JAVA_VERSION,
83 "1.4.0");
84
85 /**
86 * True if this is Java 1.4.2 or later. Since we assume Java 1.4 we just
87 * check for 1.4.0 and 1.4.1.
88 */
89 public static final boolean IS_JAVA_1_4_2_OR_LATER = !startsWith(
90 JAVA_VERSION, "1.4.0")
91 && !startsWith(JAVA_VERSION, "1.4.1");
92
93 /**
94 * True if this is Java 5.x. We check for a prefix of 1.5.
95 */
96 public static final boolean IS_JAVA_5 = startsWith(JAVA_VERSION, "1.5");
97
98 /**
99 * True if this is Java 5.x or later. Since we don't support Java 1.3, we
100 * can check that it's not 1.4.
101 */
102 public static final boolean IS_JAVA_5_OR_LATER = !IS_JAVA_1_4;
103
104 /**
105 * True if this is Java 6. We check for a prefix of 1.6.
106 */
107 public static final boolean IS_JAVA_6 = startsWith(JAVA_VERSION, "1.6");
108
109 /**
110 * True if this is Java 6.x or later. Since we don't support Java 1.3, we
111 * can check that it's neither 1.4 nor 1.5.
112 */
113 public static final boolean IS_JAVA_6_OR_LATER = !IS_JAVA_1_4 && !IS_JAVA_5;
114
115 /**
116 * True if this is Java 1.4 or Java 5.
117 */
118 public static final boolean IS_JAVA_1_4_OR_5 = IS_JAVA_1_4 || IS_JAVA_5;
119
120 // Requesting the Operating System Name ***********************************
121
122 /**
123 * True if this is FreeBSD.
124 */
125 public static final boolean IS_OS_FREEBSD = startsWithIgnoreCase(OS_NAME,
126 "FreeBSD");
127
128 /**
129 * True if this is Linux.
130 */
131 public static final boolean IS_OS_LINUX = startsWithIgnoreCase(OS_NAME,
132 "Linux");
133
134 /**
135 * True if this is OS/2.
136 */
137 public static final boolean IS_OS_OS2 = startsWith(OS_NAME, "OS/2");
138
139 /**
140 * True if this is the Mac OS X.
141 */
142 public static final boolean IS_OS_MAC = startsWith(OS_NAME, "Mac");
143
144 /**
145 * True if this is Windows.
146 */
147 public static final boolean IS_OS_WINDOWS = startsWith(OS_NAME, "Windows");
148
149 /**
150 * True if this is Windows 98/ME/2000/XP/VISTA.
151 */
152 public static final boolean IS_OS_WINDOWS_MODERN = startsWith(OS_NAME,
153 "Windows")
154 && !startsWith(OS_VERSION, "4.0");
155
156 /**
157 * True if this is Windows 95.
158 *
159 * @since 2.0
160 */
161 public static final boolean IS_OS_WINDOWS_95 = startsWith(OS_NAME,
162 "Windows 9")
163 && startsWith(OS_VERSION, "4.0");
164
165 /**
166 * True if this is Windows 98.
167 *
168 * @since 2.0
169 */
170 public static final boolean IS_OS_WINDOWS_98 = startsWith(OS_NAME,
171 "Windows 9")
172 && startsWith(OS_VERSION, "4.1");
173
174 /**
175 * True if this is Windows NT.
176 *
177 * @since 2.0
178 */
179 public static final boolean IS_OS_WINDOWS_NT = startsWith(OS_NAME,
180 "Windows NT");
181
182 /**
183 * True if this is Windows ME.
184 *
185 * @since 2.0
186 */
187 public static final boolean IS_OS_WINDOWS_ME = startsWith(OS_NAME,
188 "Windows")
189 && startsWith(OS_VERSION, "4.9");
190
191 /**
192 * True if this is Windows 2000.
193 *
194 * @since 2.0
195 */
196 public static final boolean IS_OS_WINDOWS_2000 = startsWith(OS_NAME,
197 "Windows")
198 && startsWith(OS_VERSION, "5.0");
199
200 /**
201 * True if this is Windows XP.
202 */
203 public static final boolean IS_OS_WINDOWS_XP = startsWith(OS_NAME,
204 "Windows")
205 && startsWith(OS_VERSION, "5.1");
206
207 /**
208 * True if this is Windows Vista.
209 *
210 * @since 2.0
211 */
212 public static final boolean IS_OS_WINDOWS_VISTA = startsWith(OS_NAME,
213 "Windows")
214 && startsWith(OS_VERSION, "6.0");
215
216 /**
217 * True if this is Solaris.
218 */
219 public static final boolean IS_OS_SOLARIS = startsWith(OS_NAME, "Solaris");
220
221 // Other Properties *******************************************************
222
223 /**
224 * True if the Windows XP Look&amp;Feel is enabled.
225 */
226 public static final boolean IS_LAF_WINDOWS_XP_ENABLED = isWindowsXPLafEnabled();
227
228 /**
229 * True if if the screen resolution is smaller than 120 dpi.
230 *
231 * @see Toolkit#getScreenResolution()
232 */
233 public static final boolean IS_LOW_RESOLUTION = isLowResolution();
234
235 private static boolean loggingEnabled = true;
236
237 private LookUtils() {
238 // Override default constructor; prevents instantiation.
239 }
240
241 // Accessing System Configuration *****************************************
242
243 /**
244 * Tries to look up the System property for the given key. In untrusted
245 * environments this may throw a SecurityException. In this case we catch
246 * the exception and answer <code>null</code>.
247 *
248 * @param key
249 * the name of the system property
250 * @return the system property's String value, or <code>null</code> if
251 * there's no such value, or a SecurityException has been caught
252 */
253 public static String getSystemProperty(String key) {
254 try {
255 return System.getProperty(key);
256 } catch (SecurityException e) {
257 // log("Can't read the System property " + key + ".");
258 return null;
259 }
260 }
261
262 /**
263 * Tries to look up the System property for the given key. In untrusted
264 * environments this may throw a SecurityException. In this case, we catch
265 * the exception and answer the default value.
266 *
267 * @param key
268 * the name of the system property
269 * @param defaultValue
270 * the default value if no property exists.
271 * @return the system property's String value, or the defaultValue if
272 * there's no such value, or a SecurityException has been caught
273 */
274 public static String getSystemProperty(String key, String defaultValue) {
275 try {
276 return System.getProperty(key, defaultValue);
277 } catch (SecurityException e) {
278 // log("Can't read the System property " + key + ".");
279 return defaultValue;
280 }
281 }
282
283 /**
284 * Checks if a boolean system property has been set for the given key, and
285 * returns the associated Boolean, or <code>null</code> if no value has been
286 * set. The test for the property ignores case. If a Boolean value has been
287 * set, a message is logged with the given prefix.
288 *
289 * @param key
290 * the key used to lookup the system property value
291 * @param logMessage
292 * a prefix used when a message is logged
293 * @return <code>Boolean.TRUE</code> if the system property has been set to
294 * "true" (case ignored), <code>Boolean.FALSE</code> if it has been
295 * set to "false", <code>null</code> otherwise
296 */
297 public static Boolean getBooleanSystemProperty(String key, String logMessage) {
298 String value = getSystemProperty(key, "");
299 Boolean result;
300 if (value.equalsIgnoreCase("false"))
301 result = Boolean.FALSE;
302 else if (value.equalsIgnoreCase("true"))
303 result = Boolean.TRUE;
304 else
305 result = null;
306 if (result != null) {
307 LookUtils.log(logMessage + " have been "
308 + (result ? "en" : "dis")
309 + "abled in the system properties.");
310 }
311 return result;
312 }
313
314 /**
315 * Checks and answers whether the Windows XP style is enabled. This method
316 * is intended to be called only if a Windows look&feel is about to be
317 * installed or already active in the UIManager. The XP style of the Windows
318 * look&amp;feel is enabled by default on Windows XP platforms since the
319 * J2SE 1.4.2; it can be disabled either in the Windows desktop as well as
320 * in the Java runtime by setting a System property.
321 * <p>
322 *
323 * First checks the platform, platform version and Java version. Then checks
324 * whether the desktop property <tt>win.xpstyle.themeActive</tt> is set or
325 * not.
326 *
327 * @return true if the Windows XP style is enabled
328 */
329 private static boolean isWindowsXPLafEnabled() {
330 return (IS_OS_WINDOWS_XP || IS_OS_WINDOWS_VISTA)
331 && IS_JAVA_1_4_2_OR_LATER
332 && Boolean.TRUE.equals(Toolkit.getDefaultToolkit()
333 .getDesktopProperty("win.xpstyle.themeActive"))
334 && getSystemProperty("swing.noxp") == null;
335 }
336
337 /**
338 * Checks and answers whether we have a true color system.
339 *
340 * @param c
341 * the component used to determine the toolkit
342 * @return true if the component's toolkit has a pixel size >= 24
343 */
344 public static boolean isTrueColor(Component c) {
345 return c.getToolkit().getColorModel().getPixelSize() >= 24;
346 }
347
348 /**
349 * Checks and answers whether this toolkit provides native drop shadows for
350 * popups such as the Mac OS X. Currently this is used to determine if the
351 * Looks' popup drop shadow feature is active or not - even if it's enabled.
352 *
353 * @return true if the toolkit provides native drop shadows
354 *
355 */
356 public static boolean getToolkitUsesNativeDropShadows() {
357 return IS_OS_MAC;
358 }
359
360 /**
361 * Computes and returns a Color that is slightly brighter than the specified
362 * Color.
363 *
364 * @param color
365 * the color used as basis for the brightened color
366 * @return a slightly brighter color
367 */
368 public static Color getSlightlyBrighter(Color color) {
369 return getSlightlyBrighter(color, 1.1f);
370 }
371
372 /**
373 * Computes and returns a Color that is slightly brighter than the specified
374 * Color.
375 *
376 * @param color
377 * the color used as basis for the brightened color
378 * @param factor
379 * the factor used to compute the brightness
380 * @return a slightly brighter color
381 */
382 public static Color getSlightlyBrighter(Color color, float factor) {
383 float[] hsbValues = new float[3];
384 Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(),
385 hsbValues);
386 float hue = hsbValues[0];
387 float saturation = hsbValues[1];
388 float brightness = hsbValues[2];
389 float newBrightness = Math.min(brightness * factor, 1.0f);
390 return Color.getHSBColor(hue, saturation, newBrightness);
391 }
392
393 // Minimal logging ******************************************************
394
395 /**
396 * Enables or disables the Looks logging.
397 *
398 * @param enabled
399 * true to enable logging, false to disable it
400 */
401 public static void setLoggingEnabled(boolean enabled) {
402 loggingEnabled = enabled;
403 }
404
405 /**
406 * Prints a new line to the console if logging is enabled.
407 */
408 public static void log() {
409 if (loggingEnabled) {
410 System.out.println();
411 }
412 }
413
414 /**
415 * Prints the given message to the console if logging is enabled.
416 *
417 * @param message
418 * the message to print
419 */
420 public static void log(String message) {
421 if (loggingEnabled) {
422 System.out.println("JGoodies Looks: " + message);
423 }
424 }
425
426 // Private Helper Methods ***********************************************
427
428 /**
429 * Checks and answers whether the screen resolution is low or high.
430 * Resolutions below 120 dpi are considere low, all others are high.
431 *
432 * @return true if the screen resolution is smaller than 120 dpi
433 */
434 private static boolean isLowResolution() {
435 return Toolkit.getDefaultToolkit().getScreenResolution() < 120;
436 }
437
438 private static boolean startsWith(String str, String prefix) {
439 return str != null && str.startsWith(prefix);
440 }
441
442 private static boolean startsWithIgnoreCase(String str, String prefix) {
443 return str != null
444 && str.toUpperCase(Locale.ENGLISH).startsWith(
445 prefix.toUpperCase(Locale.ENGLISH));
446 }
447
448 }