1 30 31 package com.jgoodies.looks; 32 33 import java.awt.Color ; 34 import java.awt.Component ; 35 import java.awt.Insets ; 36 import java.awt.Toolkit ; 37 import java.util.Collections ; 38 import java.util.List ; 39 40 import javax.swing.AbstractButton ; 41 import javax.swing.LookAndFeel ; 42 import javax.swing.UIManager ; 43 import javax.swing.UnsupportedLookAndFeelException ; 44 import javax.swing.plaf.InsetsUIResource ; 45 import javax.swing.plaf.UIResource ; 46 47 import com.jgoodies.looks.plastic.PlasticLookAndFeel; 48 import com.jgoodies.looks.plastic.PlasticTheme; 49 50 58 59 public final class LookUtils { 60 61 63 69 private static final String JAVA_VERSION = getSystemProperty("java.version"); 70 71 77 private static final String OS_NAME = getSystemProperty("os.name"); 78 79 85 private static final String OS_VERSION = getSystemProperty("os.version"); 86 87 88 90 93 public static final boolean IS_JAVA_1_4 = 94 startsWith(JAVA_VERSION, "1.4"); 95 96 99 static final boolean IS_JAVA_1_4_0 = startsWith(JAVA_VERSION, "1.4.0"); 100 101 105 public static final boolean IS_JAVA_1_4_2_OR_LATER = 106 !startsWith(JAVA_VERSION, "1.4.0") && 107 !startsWith(JAVA_VERSION, "1.4.1"); 108 109 113 public static final boolean IS_JAVA_5 = 114 startsWith(JAVA_VERSION, "1.5"); 115 116 117 119 122 public static final boolean IS_OS_FREEBSD = 123 startsWithIgnoreCase(OS_NAME, "FreeBSD"); 124 125 128 public static final boolean IS_OS_LINUX = 129 startsWithIgnoreCase(OS_NAME, "Linux"); 130 131 134 public static final boolean IS_OS_OS2 = 135 startsWith(OS_NAME, "OS/2"); 136 137 140 public static final boolean IS_OS_MAC = 141 startsWith(OS_NAME, "Mac"); 142 143 146 public static final boolean IS_OS_WINDOWS = 147 startsWith(OS_NAME, "Windows"); 148 149 152 public static final boolean IS_OS_WINDOWS_MODERN = 153 startsWith(OS_NAME, "Windows") && !startsWith(OS_VERSION, "4.0"); 154 155 158 public static final boolean IS_OS_WINDOWS_XP = 159 startsWith(OS_NAME, "Windows") && startsWith(OS_VERSION, "5.1"); 160 161 164 public static final boolean IS_OS_SOLARIS = 165 startsWith(OS_NAME, "Solaris"); 166 167 168 170 173 public static final boolean IS_LAF_WINDOWS_XP_ENABLED = isWindowsXPLafEnabled(); 174 175 180 public static final boolean IS_LOW_RESOLUTION = isLowResolution(); 181 182 private static boolean loggingEnabled = true; 183 184 185 private LookUtils() { 186 } 188 189 190 192 201 public static String getSystemProperty(String key) { 202 try { 203 return System.getProperty(key); 204 } catch (SecurityException e) { 205 log("Can't read the System property " + key + "."); 206 return null; 207 } 208 } 209 210 220 public static String getSystemProperty(String key, String defaultValue) { 221 try { 222 return System.getProperty(key, defaultValue); 223 } catch (SecurityException e) { 224 log("Can't read the System property " + key + "."); 225 return defaultValue; 226 } 227 } 228 229 242 public static Boolean getBooleanSystemProperty(String key, String logMessage) { 243 String value = getSystemProperty(key, ""); 244 Boolean result; 245 if (value.equalsIgnoreCase("false")) 246 result = Boolean.FALSE; 247 else if (value.equalsIgnoreCase("true")) 248 result = Boolean.TRUE; 249 else 250 result = null; 251 if (result != null) { 252 LookUtils.log( 253 logMessage 254 + " have been " 255 + (result.booleanValue() ? "en" : "dis") 256 + "abled in the system properties."); 257 } 258 return result; 259 } 260 261 262 277 private static boolean isWindowsXPLafEnabled() { 278 return IS_OS_WINDOWS_XP 279 && IS_JAVA_1_4_2_OR_LATER 280 && Boolean.TRUE.equals(Toolkit.getDefaultToolkit(). 281 getDesktopProperty("win.xpstyle.themeActive")) 282 && getSystemProperty("swing.noxp") == null; 283 } 284 285 286 292 public static boolean isTrueColor(Component c) { 293 return c.getToolkit().getColorModel().getPixelSize() >= 24; 294 } 295 296 297 299 305 public static void installNarrowMargin( 306 AbstractButton b, 307 String propertyPrefix) { 308 Object value = b.getClientProperty(Options.IS_NARROW_KEY); 309 boolean isNarrow = Boolean.TRUE.equals(value); 310 String defaultsKey = 311 propertyPrefix + (isNarrow ? "narrowMargin" : "margin"); 312 Insets insets = b.getMargin(); 313 if (insets == null || insets instanceof UIResource ) { 314 b.setMargin(UIManager.getInsets(defaultsKey)); 315 } 316 } 317 318 329 public static Insets createButtonMargin(boolean narrow) { 330 int pad = narrow || Options.getUseNarrowButtons() ? 4 : 14; 331 return IS_LOW_RESOLUTION 332 ? new InsetsUIResource (2, pad, 1, pad) 333 : new InsetsUIResource (3, pad, 3, pad); 334 } 335 336 338 345 public static Color getSlightlyBrighter(Color color) { 346 return getSlightlyBrighter(color, 1.1f); 347 } 348 349 357 public static Color getSlightlyBrighter(Color color, float factor) { 358 float[] hsbValues = new float[3]; 359 Color.RGBtoHSB( 360 color.getRed(), 361 color.getGreen(), 362 color.getBlue(), 363 hsbValues); 364 float hue = hsbValues[0]; 365 float saturation = hsbValues[1]; 366 float brightness = hsbValues[2]; 367 float newBrightness = Math.min(brightness * factor, 1.0f); 368 return Color.getHSBColor(hue, saturation, newBrightness); 369 } 370 371 372 374 public static void setLookAndTheme(LookAndFeel laf, Object theme) 375 throws UnsupportedLookAndFeelException { 376 if ((laf instanceof PlasticLookAndFeel) 377 && (theme != null) 378 && (theme instanceof PlasticTheme)) { 379 PlasticLookAndFeel.setMyCurrentTheme((PlasticTheme) theme); 380 } 381 UIManager.setLookAndFeel(laf); 382 } 383 384 public static Object getDefaultTheme(LookAndFeel laf) { 385 return laf instanceof PlasticLookAndFeel 386 ? PlasticLookAndFeel.createMyDefaultTheme() 387 : null; 388 } 389 390 public static List getInstalledThemes(LookAndFeel laf) { 391 return laf instanceof PlasticLookAndFeel 392 ? PlasticLookAndFeel.getInstalledThemes() 393 : Collections.EMPTY_LIST; 394 } 395 396 398 403 public static void setLoggingEnabled(boolean enabled) { 404 loggingEnabled = enabled; 405 } 406 407 410 public static void log() { 411 if (loggingEnabled) { 412 System.out.println(); 413 } 414 } 415 416 421 public static void log(String message) { 422 if (loggingEnabled) { 423 System.out.println("JGoodies Looks: " + message); 424 } 425 } 426 427 429 435 private static boolean isLowResolution() { 436 return Toolkit.getDefaultToolkit().getScreenResolution() < 120; 437 } 438 439 private static boolean startsWith(String str, String prefix) { 440 return str != null && str.startsWith(prefix); 441 } 442 443 private static boolean startsWithIgnoreCase(String str, String prefix) { 444 return str != null && str.toUpperCase().startsWith(prefix.toUpperCase()); 445 } 446 447 } | Popular Tags |