KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > Options


1 /*
2  * Copyright (c) 2001-2005 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 com.jgoodies.looks;
32
33 import java.awt.Dimension JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import javax.swing.UIManager JavaDoc;
38
39 import com.jgoodies.looks.common.ShadowPopup;
40
41 /**
42  * Provides access to several optional properties for the
43  * JGoodies L&amp;Fs, either by a key to the <code>UIDefaults</code> table
44  * or via a method or both.
45  *
46  * @author Karsten Lentzsch
47  * @version $Revision: 1.6 $
48  */

49
50 public final class Options {
51
52     // Look & Feel Names ****************************************************
53

54     public static final String JavaDoc PLASTIC_NAME =
55         "com.jgoodies.looks.plastic.PlasticLookAndFeel";
56         
57     public static final String JavaDoc PLASTIC3D_NAME =
58         "com.jgoodies.looks.plastic.Plastic3DLookAndFeel";
59         
60     public static final String JavaDoc PLASTICXP_NAME =
61         "com.jgoodies.looks.plastic.PlasticXPLookAndFeel";
62         
63     public static final String JavaDoc JGOODIES_WINDOWS_NAME =
64         "com.jgoodies.looks.windows.WindowsLookAndFeel";
65         
66     /**
67      * This outdated constant will be removed in the Looks version 1.4.
68      *
69      * @deprecated Replaced by {@link #JGOODIES_WINDOWS_NAME}.
70      */

71     public static final String JavaDoc EXT_WINDOWS_NAME =
72         JGOODIES_WINDOWS_NAME;
73         
74     public static final String JavaDoc DEFAULT_LOOK_NAME =
75         PLASTIC3D_NAME;
76
77     /**
78      * Holds a Map that enables the look&amp;feel replacement
79      * mechanism to replace one look by another.
80      * Maps the original class names to their replacement class names.
81      */

82     private static final Map JavaDoc LAF_REPLACEMENTS;
83     static {
84         LAF_REPLACEMENTS = new HashMap JavaDoc();
85         initializeDefaultReplacements();
86     }
87     
88
89     // Keys for Overriding Font Settings ************************************
90

91     public static final String JavaDoc MENU_FONT_KEY =
92         "jgoodies.menuFont";
93         
94     public static final String JavaDoc CONTROL_FONT_KEY =
95         "jgoodies.controlFont";
96         
97     public static final String JavaDoc FONT_SIZE_HINTS_KEY =
98         "jgoodies.fontSizeHints";
99         
100     public static final String JavaDoc USE_SYSTEM_FONTS_KEY =
101         "swing.useSystemFontSettings";
102         
103     public static final String JavaDoc USE_SYSTEM_FONTS_APP_KEY =
104         "Application.useSystemFontSettings";
105
106
107     // Optional Global User Properties **************************************
108

109     public static final String JavaDoc DEFAULT_ICON_SIZE_KEY =
110         "jgoodies.defaultIconSize";
111         
112     public static final String JavaDoc USE_NARROW_BUTTONS_KEY =
113         "jgoodies.useNarrowButtons";
114         
115     public static final String JavaDoc TAB_ICONS_ENABLED_KEY =
116         "jgoodies.tabIconsEnabled";
117         
118     public static final String JavaDoc POPUP_DROP_SHADOW_ENABLED_KEY =
119         "jgoodies.popupDropShadowEnabled";
120         
121
122     // Optional Client Properties *******************************************
123

124     /**
125      * Hint that the button margin should be narrow.
126      */

127     public static final String JavaDoc IS_NARROW_KEY = "jgoodies.isNarrow";
128
129     /**
130      * Hint that the scroll pane border should be etched.
131      */

132     public static final String JavaDoc IS_ETCHED_KEY = "jgoodies.isEtched";
133
134     /**
135      * Hint for the style: Single or Both, see <code>HeaderStyle</code>.
136      */

137     public static final String JavaDoc HEADER_STYLE_KEY = "jgoodies.headerStyle";
138
139     /**
140      * Hint that the menu items in the menu have no icons.
141      */

142     public static final String JavaDoc NO_ICONS_KEY = "jgoodies.noIcons";
143
144     /**
145      * A client property key for JTrees.
146      * Used with the angled and none style values.
147      */

148     public static final String JavaDoc TREE_LINE_STYLE_KEY =
149         "JTree.lineStyle";
150
151     /**
152      * A client property value for JTrees
153      * that indicates that lines shall be drawn.
154      */

155     public static final String JavaDoc TREE_LINE_STYLE_ANGLED_VALUE =
156         "Angled";
157
158     /**
159      * A client property value for JTrees
160      * that indicates that lines shall be hidden.
161      */

162     public static final String JavaDoc TREE_LINE_STYLE_NONE_VALUE =
163         "None";
164
165     /**
166      * A client property key for JTabbedPanes that indicates
167      * that no content border shall be painted.
168      * Supported by the Plastic look&amp;feel family.
169      * This effect will be achieved also if the EMBEDDED property is true.
170      */

171     public static final String JavaDoc NO_CONTENT_BORDER_KEY =
172         "jgoodies.noContentBorder";
173
174     /**
175      * A client property key for JTabbedPanes that indicates
176      * that tabs are painted with a special embedded appearance.
177      * Supported by the Plastic look&amp;feel family.
178      * This effect will be achieved also if the EMBEDDED property is true.
179      */

180     public static final String JavaDoc EMBEDDED_TABS_KEY =
181         "jgoodies.embeddedTabs";
182
183
184     // System Settings ********************************************************
185

186     /**
187      * Holds the Boolean system property value for the tab icon enablement,
188      * or null, if it has not been set. If this property has been
189      * set, we log a message about the choosen value.
190      *
191      * @see #isTabIconsEnabled()
192      */

193     private static final Boolean JavaDoc TAB_ICONS_ENABLED_SYSTEM_VALUE =
194         LookUtils.getBooleanSystemProperty(
195                 TAB_ICONS_ENABLED_KEY, "Icons in tabbed panes");
196     
197
198     /**
199      * Holds the Boolean system property value for the popup drop shadow
200      * enablement, or null, if it has not been set. If this property has been
201      * set, we log a message about the choosen value.<p>
202      *
203      * This property just set the feature's enablement, not its actual
204      * activation. For example, drop shadows are always inactive on
205      * the Mac OS X, because this platform already provides shadows.
206      * The activation is requested in <code>#isPopupDropShadowActive</code>.
207      *
208      * @see #isPopupDropShadowEnabled()
209      * @see #isPopupDropShadowActive()
210      */

211     private static final Boolean JavaDoc POPUP_DROP_SHADOW_ENABLED_SYSTEM_VALUE =
212         LookUtils.getBooleanSystemProperty(
213                 POPUP_DROP_SHADOW_ENABLED_KEY, "Popup drop shadows");
214     
215     
216     // Private ****************************************************************
217

218     private static final Dimension JavaDoc DEFAULT_ICON_SIZE =
219         new Dimension JavaDoc(20, 20);
220
221     private Options() {
222         // Override default constructor; prevents instantiation.
223
}
224
225
226     // Accessing Options ******************************************************
227

228     /**
229      * Returns whether a hint is set in the UIManager that indicates,
230      * that a look&amp;feel may use the native system fonts.
231      *
232      * @return true if the UIManager indicates that system fonts shall be used
233      * @see #setUseSystemFonts(boolean)
234      */

235     public static boolean getUseSystemFonts() {
236         return UIManager.get(USE_SYSTEM_FONTS_APP_KEY).equals(Boolean.TRUE);
237     }
238
239     /**
240      * Sets a value in the UIManager to indicate,
241      * that a look&amp;feel may use the native system fonts.
242      *
243      * @param useSystemFonts true to enable system fonts in the UIManager
244      * @see #getUseSystemFonts()
245      */

246     public static void setUseSystemFonts(boolean useSystemFonts) {
247         UIManager.put(USE_SYSTEM_FONTS_APP_KEY, Boolean.valueOf(useSystemFonts));
248     }
249
250     /**
251      * Returns the default icon size that is used in menus, menu items and
252      * toolbars. Menu items that have no icon set are aligned using the default
253      * icon dimensions.
254      *
255      * @return the dimension of the default icon
256      * @see #setDefaultIconSize(Dimension)
257      */

258     public static Dimension JavaDoc getDefaultIconSize() {
259         Dimension JavaDoc size = UIManager.getDimension(DEFAULT_ICON_SIZE_KEY);
260         return size == null ? DEFAULT_ICON_SIZE : size;
261     }
262
263     /**
264      * Sets the default icon size.
265      *
266      * @param defaultIconSize the default icon size to set
267      * @see #getDefaultIconSize()
268      */

269     public static void setDefaultIconSize(Dimension JavaDoc defaultIconSize) {
270         UIManager.put(DEFAULT_ICON_SIZE_KEY, defaultIconSize);
271     }
272
273     /**
274      * Returns the global <code>FontSizeHints</code>
275      * that can be overriden by a look-specific setting.
276      *
277      * @return the gobally used FontSizeHints object
278      * @see #setGlobalFontSizeHints(FontSizeHints)
279      */

280     public static FontSizeHints getGlobalFontSizeHints() {
281         Object JavaDoc value = UIManager.get(FONT_SIZE_HINTS_KEY);
282         if (value != null)
283             return (FontSizeHints) value;
284
285         String JavaDoc name = LookUtils.getSystemProperty(FONT_SIZE_HINTS_KEY, "");
286         try {
287             return FontSizeHints.valueOf(name);
288         } catch (IllegalArgumentException JavaDoc e) {
289             return FontSizeHints.DEFAULT;
290         }
291     }
292
293     /**
294      * Sets the global <code>FontSizeHints</code>.
295      *
296      * @param hints the FontSizeHints object to be used globally
297      * @see #getGlobalFontSizeHints()
298      */

299     public static void setGlobalFontSizeHints(FontSizeHints hints) {
300         UIManager.put(FONT_SIZE_HINTS_KEY, hints);
301     }
302
303     /**
304      * Checks and answers if we shall use narrow button margins of 4 pixels.
305      * Sun's L&amp;F implementations use a much wider button margin of 14 pixels,
306      * which leads to good button minimum width in the typical case.<p>
307      *
308      * Using narrow button margins can potentially cause compatibility issues,
309      * so this feature must be switched on programmatically.<p>
310      *
311      * If you use narrow margin, you should take care of minimum button width,
312      * either by the layout management or appropriate ButtonUI minimum widths.
313      *
314      * @return true if all buttons shall use narrow margins
315      * @see #setUseNarrowButtons(boolean)
316      */

317     public static boolean getUseNarrowButtons() {
318         return UIManager.getBoolean(USE_NARROW_BUTTONS_KEY);
319     }
320
321     /**
322      * Sets if we use narrow or standard button margins.
323      *
324      * @param b true to use narrow button margins globally
325      * @see #getUseNarrowButtons()
326      */

327     public static void setUseNarrowButtons(boolean b) {
328         UIManager.put(USE_NARROW_BUTTONS_KEY, Boolean.valueOf(b));
329     }
330
331     /**
332      * Checks and answers if we shall use icons in JTabbedPanes.
333      * By default, tab icons are enabled. If the user has set a system property,
334      * we log a message about the choosen style.
335      *
336      * @return true if icons in tabbed panes are enabled, false if disabled
337      * @see #setTabIconsEnabled(boolean)
338      */

339     public static boolean isTabIconsEnabled() {
340         return TAB_ICONS_ENABLED_SYSTEM_VALUE == null
341             ? !Boolean.FALSE.equals(UIManager.get(TAB_ICONS_ENABLED_KEY))
342             : TAB_ICONS_ENABLED_SYSTEM_VALUE.booleanValue();
343     }
344
345     /**
346      * Enables or disables the use of icons in JTabbedPanes.
347      *
348      * @param b true to enable icons in tabbed panes, false to disable them
349      * @see #isTabIconsEnabled()
350      */

351     public static void setTabIconsEnabled(boolean b) {
352         UIManager.put(TAB_ICONS_ENABLED_KEY, Boolean.valueOf(b));
353     }
354     
355     
356     /**
357      * Checks and answers whether popup drop shadows are active.
358      * This feature shall be inactive with toolkits that use
359      * native drop shadows, such as Aqua on the Mac OS X.
360      * It is also inactive if the ShadowPopup cannot snapshot
361      * the desktop background (due to security and AWT exceptions).
362      * Otherwise the feature's enablement state is returned.<p>
363      *
364      * Currently only the Mac OS X is detected as platform where
365      * the toolkit uses native drop shadows.<p>
366      *
367      * TODO: Consider delegating the check for native drop shadows
368      * to <code>LookUtils#getToolkitUsesNativeDropShadows()</code>.
369      *
370      * @return true if drop shadows are active, false if inactive
371      *
372      * @see #isPopupDropShadowEnabled()
373      * @see #setPopupDropShadowEnabled(boolean)
374      */

375     public static boolean isPopupDropShadowActive() {
376         boolean toolkitUsesNativeDropShadows =
377             LookUtils.IS_OS_MAC;
378         
379         return !toolkitUsesNativeDropShadows
380              && ShadowPopup.canSnapshot()
381              && isPopupDropShadowEnabled();
382     }
383
384     /**
385      * Checks and answers whether the optional drop shadows for
386      * PopupMenus are enabled or disabled.
387      *
388      * @return true if drop shadows are enabled, false if disabled
389      *
390      * @see #isPopupDropShadowActive()
391      * @see #setPopupDropShadowEnabled(boolean)
392      */

393     public static boolean isPopupDropShadowEnabled() {
394         if (POPUP_DROP_SHADOW_ENABLED_SYSTEM_VALUE != null)
395             return POPUP_DROP_SHADOW_ENABLED_SYSTEM_VALUE.booleanValue();
396
397         Object JavaDoc value = UIManager.get(POPUP_DROP_SHADOW_ENABLED_KEY);
398         return value == null
399             ? isPopupDropShadowEnabledDefault()
400             : Boolean.TRUE.equals(value);
401     }
402
403     /**
404      * Enables or disables drop shadows in PopupMenus.
405      * Note that drop shadows are always inactive on platforms
406      * that provide native drop shadows such as the Mac OS X.<p>
407      *
408      * It is recommended to enable this feature only on platforms that
409      * accelerate translucency and snapshots with the hardware.
410      *
411      * @param b true to enable drop shadows, false to disable them
412      *
413      * @see #isPopupDropShadowActive()
414      * @see #isPopupDropShadowEnabled()
415      */

416     public static void setPopupDropShadowEnabled(boolean b) {
417         UIManager.put(POPUP_DROP_SHADOW_ENABLED_KEY, Boolean.valueOf(b));
418     }
419     
420     /**
421      * Checks and answers whether popup drop shadows are enabled
422      * or disabled by default. True for modern Windows platforms:
423      * Windows 98/ME/2000/XP.<p>
424      *
425      * TODO: Consider enabling popup drop shadows on Linux by default.<p>
426      *
427      * TODO: Consider moving the default to the individual L&amp;F's
428      * component defaults initialization. For example Plastic and Plastic3D
429      * may disable this feature by default, while PlasticXP enables it
430      * by default.
431      *
432      * @return false
433      */

434     private static boolean isPopupDropShadowEnabledDefault() {
435         return LookUtils.IS_OS_WINDOWS_MODERN;
436     }
437
438
439     // Look And Feel Replacements *******************************************
440

441     /**
442      * Puts a replacement name for a given <code>LookAndFeel</code>
443      * class name in the list of all look and feel replacements.
444      *
445      * @param original the name of the look-and-feel to replace
446      * @param replacement the name of the replacement look-and-feel
447      * @see #removeLookAndFeelReplacement(String)
448      * @see #getReplacementClassNameFor(String)
449      */

450     public static void putLookAndFeelReplacement(
451         String JavaDoc original,
452         String JavaDoc replacement) {
453         LAF_REPLACEMENTS.put(original, replacement);
454     }
455
456     /**
457      * Removes a replacement name for a given <code>LookAndFeel</code>
458      * class name from the list of all look and feel replacements.
459      *
460      * @param original the name of the look-and-feel that has been replaced
461      * @see #putLookAndFeelReplacement(String, String)
462      * @see #getReplacementClassNameFor(String)
463      */

464     public static void removeLookAndFeelReplacement(String JavaDoc original) {
465         LAF_REPLACEMENTS.remove(original);
466     }
467
468     /**
469      * Initializes some default class name replacements, that replace
470      * Sun's Java look and feel, and Sun's Windows look and feel by
471      * the appropriate JGoodies replacements.
472      *
473      * @see #putLookAndFeelReplacement(String, String)
474      * @see #removeLookAndFeelReplacement(String)
475      * @see #getReplacementClassNameFor(String)
476      */

477     public static void initializeDefaultReplacements() {
478         putLookAndFeelReplacement(
479             "javax.swing.plaf.metal.MetalLookAndFeel",
480             PLASTIC3D_NAME);
481         putLookAndFeelReplacement(
482             "com.sun.java.swing.plaf.windows.WindowsLookAndFeel",
483             JGOODIES_WINDOWS_NAME);
484     }
485
486     /**
487      * Returns the class name that can be used to replace the specified
488      * <code>LookAndFeel</code> class name.
489      *
490      * @param className the name of the look-and-feel class
491      * @return the name of the suggested replacement class
492      * @see #putLookAndFeelReplacement(String, String)
493      * @see #removeLookAndFeelReplacement(String)
494      * @see #initializeDefaultReplacements()
495      */

496     public static String JavaDoc getReplacementClassNameFor(String JavaDoc className) {
497         String JavaDoc replacement = (String JavaDoc) LAF_REPLACEMENTS.get(className);
498         return replacement == null ? className : replacement;
499     }
500
501     /**
502      * Returns the class name for a cross-platform <code>LookAndFeel</code>.
503      *
504      * @return the name of a cross platform look-and-feel class
505      * @see #getSystemLookAndFeelClassName()
506      */

507     public static String JavaDoc getCrossPlatformLookAndFeelClassName() {
508         return PLASTIC3D_NAME;
509     }
510
511     /**
512      * Returns the class name for a system specific <code>LookAndFeel</code>.
513      *
514      * @return the name of the system look-and-feel class
515      * @see #getCrossPlatformLookAndFeelClassName()
516      */

517     public static String JavaDoc getSystemLookAndFeelClassName() {
518         String JavaDoc osName = System.getProperty("os.name");
519         if (osName.startsWith("Windows"))
520             return Options.JGOODIES_WINDOWS_NAME;
521         else if (osName.startsWith("Mac"))
522             return UIManager.getSystemLookAndFeelClassName();
523         else
524             return getCrossPlatformLookAndFeelClassName();
525     }
526
527 }
Popular Tags