001: /*
002: * SmoothGradientLookUtils.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.plaf.smoothgradient;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.awt.Insets;
027: import java.awt.Toolkit;
028:
029: import javax.swing.AbstractButton;
030: import javax.swing.UIManager;
031: import javax.swing.plaf.InsetsUIResource;
032: import javax.swing.plaf.UIResource;
033:
034: /* ----------------------------------------------------------
035: * CVS NOTE: Changes to the CVS repository prior to the
036: * release of version 3.0.0beta1 has meant a
037: * resetting of CVS revision numbers.
038: * ----------------------------------------------------------
039: */
040:
041: /**
042: *
043: * @author Takis Diakoumis
044: * @version $Revision: 1.4 $
045: * @date $Date: 2006/05/14 06:56:07 $
046: */
047: public final class SmoothGradientLookUtils {
048:
049: // Properties and Keys for Internal Use Only - May Change without Notice
050:
051: public static final boolean IS_BEFORE_14 = isBefore14();
052: public static final boolean IS_140 = is140();
053: public static final boolean IS_142_OR_LATER = is142orLater();
054: public static final boolean HAS_XP_LAF = IS_142_OR_LATER;
055:
056: public static boolean isLowRes = isLowResolution();
057:
058: private static boolean loggingEnabled;
059:
060: // Override default constructor;
061: private SmoothGradientLookUtils() {
062: }
063:
064: /**
065: * Checks and answers whether we have a true color system.
066: */
067: public static boolean isTrueColor(Component c) {
068: return c.getToolkit().getColorModel().getPixelSize() >= 24;
069: }
070:
071: /**
072: * Tries to look up the System property for the given key.
073: * In untrusted environments this may throw a SecurityException.
074: * In this case, we catch the exception and answer an empty string.
075: *
076: * @param key the name of the system property
077: * @return the system property's String value, or null if there's
078: * no such value, or an empty String when
079: * a SecurityException has been catched
080: */
081: public static String getSystemProperty(String key) {
082: try {
083: return System.getProperty(key);
084: } catch (SecurityException e) {
085: log("Can't read the System property " + key + ".");
086: return "";
087: }
088: }
089:
090: /**
091: * Tries to look up the System property for the given key.
092: * In untrusted environments this may throw a SecurityException.
093: * In this case, we catch the exception and answer an empty string.
094: *
095: * @param key the name of the system property
096: * @param defaultValue the default value if no property exists.
097: * @return the system property's String value, or the defaultValue
098: * if there's no such value, or an empty String when
099: * a SecurityException has been catched
100: */
101: public static String getSystemProperty(String key,
102: String defaultValue) {
103: try {
104: return System.getProperty(key, defaultValue);
105: } catch (SecurityException e) {
106: log("Can't read the System property " + key + ".");
107: return "";
108: }
109: }
110:
111: /**
112: * Checks and answers whether we're on Windows.
113: */
114: public static boolean isWindows() {
115: return System.getProperty("os.name").startsWith("Windows");
116: }
117:
118: /**
119: * Checks and answers if we are on Windows 95 or NT.
120: */
121: public static boolean isClassicWindows() {
122: String osName = System.getProperty("os.name");
123: String osVersion = System.getProperty("os.version");
124: return osName.startsWith("Windows")
125: && osVersion.startsWith("4.0");
126: }
127:
128: /**
129: * Checks and answers if we are on Windows 98/ME/2000/XP.
130: */
131: public static boolean isModernWindows() {
132: String osName = System.getProperty("os.name");
133: String osVersion = System.getProperty("os.version");
134: return osName.startsWith("Windows")
135: && !osVersion.startsWith("4.0");
136: }
137:
138: /**
139: * Checks and answers if we are on Windows XP.
140: */
141: public static boolean isWindowsXP() {
142: String osName = System.getProperty("os.name");
143: String osVersion = System.getProperty("os.version");
144: return osName.startsWith("Windows")
145: && osVersion.startsWith("5.1");
146: }
147:
148: // Working with Button Margins ******************************************
149:
150: /**
151: * Installs a narrow margin, if property <code>isNarrow</code> has been set.
152: */
153: public static void installNarrowMargin(AbstractButton b,
154: String propertyPrefix) {
155: Object value = b.getClientProperty(Options.IS_NARROW_KEY);
156: boolean isNarrow = Boolean.TRUE.equals(value);
157: String defaultsKey = propertyPrefix
158: + (isNarrow ? "narrowMargin" : "margin");
159: Insets insets = b.getMargin();
160: if (insets == null || insets instanceof UIResource) {
161: b.setMargin(UIManager.getInsets(defaultsKey));
162: }
163: }
164:
165: /**
166: * Creates and answers the margin used by <code>JButton</code>
167: * and <code>JToggleButton</code>. Honors the screen resolution
168: * and the global <code>isNarrowButtonsEnabled</code> property.<p>
169: *
170: * Sun's L&F implementations use wide button margins.
171: * @see Options#getUseNarrowButtons()
172: */
173: public static Insets createButtonMargin(boolean narrow) {
174: int pad = 4;//narrow || Options.getUseNarrowButtons() ? 4 : 14;
175: return isLowRes ? (IS_BEFORE_14 ? new InsetsUIResource(0, pad,
176: 1, pad) : new InsetsUIResource(2, pad, 1, pad))
177: : (IS_BEFORE_14 ? new InsetsUIResource(2, pad, 2, pad)
178: : new InsetsUIResource(3, pad, 3, pad));
179: }
180:
181: // Color Modifications **************************************************
182:
183: /**
184: * Computes and answers a <code>Color</code> that is slightly brighter
185: * than the specified <code>Color</code>. Required for 1.3 only.
186: *
187: * @param color the color used as basis for the brightened color
188: * @return a slightly brighter color
189: */
190: public static Color getSlightlyBrighter(Color color) {
191: return getSlightlyBrighter(color, 1.1f);
192: }
193:
194: /**
195: * Computes and answers a <code>Color</code> that is slightly brighter
196: * than the specified <code>Color</code>. Required for 1.3 only.
197: *
198: * @param color the color used as basis for the brightened color
199: * @param factor the factor used to compute the brightness
200: * @return a slightly brighter color
201: */
202: public static Color getSlightlyBrighter(Color color, float factor) {
203: float[] hsbValues = new float[3];
204: Color.RGBtoHSB(color.getRed(), color.getGreen(), color
205: .getBlue(), hsbValues);
206: float hue = hsbValues[0];
207: float saturation = hsbValues[1];
208: float brightness = hsbValues[2];
209: float newBrightness = Math.min(brightness * factor, 1.0f);
210: return Color.getHSBColor(hue, saturation, newBrightness);
211: }
212:
213: // Minimal logging ******************************************************
214:
215: /**
216: * Enables or disables the logging.
217: */
218: public static void setLoggingEnabled(boolean enabled) {
219: loggingEnabled = enabled;
220: }
221:
222: /**
223: * Prints a new line to the console if logging is enabled.
224: */
225: public static void log() {
226: if (loggingEnabled) {
227: System.out.println();
228: }
229: }
230:
231: /**
232: * Prints the given message to the console if logging is enabled.
233: *
234: * @param message the message to print
235: */
236: public static void log(String message) {
237: if (loggingEnabled) {
238: System.out.println("JGoodies Looks: " + message);
239: }
240: }
241:
242: // Private Helper Methods ***********************************************
243:
244: /**
245: * Checks and answers if we are on a 1.2 or 1.3 runtime environment.
246: */
247: private static boolean isBefore14() {
248: String version = System.getProperty("java.version");
249: return version.startsWith("1.2") || version.startsWith("1.3");
250: }
251:
252: /**
253: * Checks and answers if we are on a J2SE 1.4.2 or later.
254: */
255: private static boolean is142orLater() {
256: String version = System.getProperty("java.version");
257: return !version.startsWith("1.2") && !version.startsWith("1.3")
258: && !version.startsWith("1.4.0")
259: && !version.startsWith("1.4.1");
260: }
261:
262: /**
263: * Checks and answers whether this is a J2RE 1.4.0x
264: */
265: private static boolean is140() {
266: return System.getProperty("java.version").startsWith("1.4.0");
267: }
268:
269: /**
270: * Checks and answers whether the screen resolution is low or high.
271: */
272: private static boolean isLowResolution() {
273: return Toolkit.getDefaultToolkit().getScreenResolution() < 120;
274: }
275:
276: }
|