2
* Copyright (c) 2001-2006 JGoodies Karsten Lentzsch. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
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.
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.
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.
31
package org.pushingpixels.lafwidget.utils;
34
import java.util.Locale;
37
* Provides convenience behavior used by the JGoodies Looks.
39
* @author Karsten Lentzsch
41
public final class LookUtils {
43
// Basics System Properties **********************************************
46
* The <code>java.version</code> System Property.
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.
52
private static final String JAVA_VERSION = getSystemProperty("java.version");
55
* The <code>os.name</code> System Property. Operating system name.
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.
61
private static final String OS_NAME = getSystemProperty("os.name");
64
* The <code>os.version</code> System Property. Operating system version.
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.
70
private static final String OS_VERSION = getSystemProperty("os.version");
72
// Requesting the Java Version ********************************************
75
* True if this is Java 1.4.
77
public static final boolean IS_JAVA_1_4 = startsWith(JAVA_VERSION, "1.4");
80
* True if this is Java 1.4.0_*.
82
public static final boolean IS_JAVA_1_4_0 = startsWith(JAVA_VERSION,
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.
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");
94
* True if this is Java 5.x. We check for a prefix of 1.5.
96
public static final boolean IS_JAVA_5 = startsWith(JAVA_VERSION, "1.5");
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.
102
public static final boolean IS_JAVA_5_OR_LATER = !IS_JAVA_1_4;
105
* True if this is Java 6. We check for a prefix of 1.6.
107
public static final boolean IS_JAVA_6 = startsWith(JAVA_VERSION, "1.6");
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.
113
public static final boolean IS_JAVA_6_OR_LATER = !IS_JAVA_1_4 && !IS_JAVA_5;
116
* True if this is Java 1.4 or Java 5.
118
public static final boolean IS_JAVA_1_4_OR_5 = IS_JAVA_1_4 || IS_JAVA_5;
120
// Requesting the Operating System Name ***********************************
123
* True if this is FreeBSD.
125
public static final boolean IS_OS_FREEBSD = startsWithIgnoreCase(OS_NAME,
129
* True if this is Linux.
131
public static final boolean IS_OS_LINUX = startsWithIgnoreCase(OS_NAME,
135
* True if this is OS/2.
137
public static final boolean IS_OS_OS2 = startsWith(OS_NAME, "OS/2");
140
* True if this is the Mac OS X.
142
public static final boolean IS_OS_MAC = startsWith(OS_NAME, "Mac");
145
* True if this is Windows.
147
public static final boolean IS_OS_WINDOWS = startsWith(OS_NAME, "Windows");
150
* True if this is Windows 98/ME/2000/XP/VISTA.
152
public static final boolean IS_OS_WINDOWS_MODERN = startsWith(OS_NAME,
154
&& !startsWith(OS_VERSION, "4.0");
157
* True if this is Windows 95.
161
public static final boolean IS_OS_WINDOWS_95 = startsWith(OS_NAME,
163
&& startsWith(OS_VERSION, "4.0");
166
* True if this is Windows 98.
170
public static final boolean IS_OS_WINDOWS_98 = startsWith(OS_NAME,
172
&& startsWith(OS_VERSION, "4.1");
175
* True if this is Windows NT.
179
public static final boolean IS_OS_WINDOWS_NT = startsWith(OS_NAME,
183
* True if this is Windows ME.
187
public static final boolean IS_OS_WINDOWS_ME = startsWith(OS_NAME,
189
&& startsWith(OS_VERSION, "4.9");
192
* True if this is Windows 2000.
196
public static final boolean IS_OS_WINDOWS_2000 = startsWith(OS_NAME,
198
&& startsWith(OS_VERSION, "5.0");
201
* True if this is Windows XP.
203
public static final boolean IS_OS_WINDOWS_XP = startsWith(OS_NAME,
205
&& startsWith(OS_VERSION, "5.1");
208
* True if this is Windows Vista.
212
public static final boolean IS_OS_WINDOWS_VISTA = startsWith(OS_NAME,
214
&& startsWith(OS_VERSION, "6.0");
217
* True if this is Solaris.
219
public static final boolean IS_OS_SOLARIS = startsWith(OS_NAME, "Solaris");
221
// Other Properties *******************************************************
224
* True if the Windows XP Look&Feel is enabled.
226
public static final boolean IS_LAF_WINDOWS_XP_ENABLED = isWindowsXPLafEnabled();
229
* True if if the screen resolution is smaller than 120 dpi.
231
* @see Toolkit#getScreenResolution()
233
public static final boolean IS_LOW_RESOLUTION = isLowResolution();
235
private static boolean loggingEnabled = true;
237
private LookUtils() {
238
// Override default constructor; prevents instantiation.
241
// Accessing System Configuration *****************************************
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>.
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
253
public static String getSystemProperty(String key) {
255
return System.getProperty(key);
256
} catch (SecurityException e) {
257
// log("Can't read the System property " + key + ".");
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.
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
274
public static String getSystemProperty(String key, String defaultValue) {
276
return System.getProperty(key, defaultValue);
277
} catch (SecurityException e) {
278
// log("Can't read the System property " + key + ".");
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.
290
* the key used to lookup the system property value
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
297
public static Boolean getBooleanSystemProperty(String key, String logMessage) {
298
String value = getSystemProperty(key, "");
300
if (value.equalsIgnoreCase("false"))
301
result = Boolean.FALSE;
302
else if (value.equalsIgnoreCase("true"))
303
result = Boolean.TRUE;
306
if (result != null) {
307
LookUtils.log(logMessage + " have been "
308
+ (result ? "en" : "dis")
309
+ "abled in the system properties.");
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&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.
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
327
* @return true if the Windows XP style is enabled
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;
338
* Checks and answers whether we have a true color system.
341
* the component used to determine the toolkit
342
* @return true if the component's toolkit has a pixel size >= 24
344
public static boolean isTrueColor(Component c) {
345
return c.getToolkit().getColorModel().getPixelSize() >= 24;
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.
353
* @return true if the toolkit provides native drop shadows
356
public static boolean getToolkitUsesNativeDropShadows() {
361
* Computes and returns a Color that is slightly brighter than the specified
365
* the color used as basis for the brightened color
366
* @return a slightly brighter color
368
public static Color getSlightlyBrighter(Color color) {
369
return getSlightlyBrighter(color, 1.1f);
373
* Computes and returns a Color that is slightly brighter than the specified
377
* the color used as basis for the brightened color
379
* the factor used to compute the brightness
380
* @return a slightly brighter color
382
public static Color getSlightlyBrighter(Color color, float factor) {
383
float[] hsbValues = new float[3];
384
Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(),
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);
393
// Minimal logging ******************************************************
396
* Enables or disables the Looks logging.
399
* true to enable logging, false to disable it
401
public static void setLoggingEnabled(boolean enabled) {
402
loggingEnabled = enabled;
406
* Prints a new line to the console if logging is enabled.
408
public static void log() {
409
if (loggingEnabled) {
410
System.out.println();
415
* Prints the given message to the console if logging is enabled.
418
* the message to print
420
public static void log(String message) {
421
if (loggingEnabled) {
422
System.out.println("JGoodies Looks: " + message);
426
// Private Helper Methods ***********************************************
429
* Checks and answers whether the screen resolution is low or high.
430
* Resolutions below 120 dpi are considere low, all others are high.
432
* @return true if the screen resolution is smaller than 120 dpi
434
private static boolean isLowResolution() {
435
return Toolkit.getDefaultToolkit().getScreenResolution() < 120;
438
private static boolean startsWith(String str, String prefix) {
439
return str != null && str.startsWith(prefix);
442
private static boolean startsWithIgnoreCase(String str, String prefix) {
444
&& str.toUpperCase(Locale.ENGLISH).startsWith(
445
prefix.toUpperCase(Locale.ENGLISH));