KickJava   Java API By Example, From Geeks To Geeks.

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


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.Font JavaDoc;
34
35 import javax.swing.UIDefaults JavaDoc;
36 import javax.swing.UIManager JavaDoc;
37 import javax.swing.plaf.FontUIResource JavaDoc;
38
39 /**
40  * Provides convenience behavior to set font defaults.
41  * Used by the JGoodies look&feel implementations.
42  *
43  * @author Karsten Lentzsch
44  * @version $Revision: 1.2 $
45  *
46  * @see FontSizeHints
47  */

48  
49 public final class FontUtils {
50     
51     private FontUtils() {
52         // Override default constructor; prevents instantiation.
53
}
54     
55
56     /**
57      * Checks and answers if we shall use system font settings.
58      * Using the fonts set by the user can potentially cause
59      * performance and compatibility issues, so we allow this feature
60      * to be switched off either at runtime or programmatically.<p>
61      *
62      * First checks whether system fonts have been explicitly turned
63      * off in the system properties. Then checks whether a property
64      * has been set in the UIManager.
65      *
66      * @return true if system fonts shall be used
67      */

68     public static boolean useSystemFontSettings() {
69         String JavaDoc systemFonts = LookUtils
70                 .getSystemProperty(Options.USE_SYSTEM_FONTS_KEY);
71         if ("false".equalsIgnoreCase(systemFonts))
72             return false;
73
74         Object JavaDoc value = UIManager.get(Options.USE_SYSTEM_FONTS_APP_KEY);
75         return !Boolean.FALSE.equals(value);
76     }
77
78     
79     
80     /**
81      * Sets different fonts to all known widget defaults.
82      * If the specified <code>menuFont</code> is null,
83      * the given defaults won't be overriden.
84      *
85      * @param table the UIDefaults table used to set fonts
86      * @param controlFont the control font to be set
87      * @param controlBoldFont a bold version of the control font
88      * @param fixedControlFont a fixed size control font
89      * @param menuFont the font used for menus
90      * @param messageFont the font used in message
91      * @param toolTipFont the font used in tool tips
92      * @param windowFont the general dialog font
93      */

94     public static void initFontDefaults(UIDefaults JavaDoc table,
95         Object JavaDoc controlFont, Object JavaDoc controlBoldFont, Object JavaDoc fixedControlFont,
96         Object JavaDoc menuFont, Object JavaDoc messageFont, Object JavaDoc toolTipFont, Object JavaDoc windowFont) {
97             
98 // LookUtils.log("Menu font =" + menuFont);
99
// LookUtils.log("Control font=" + controlFont);
100
// LookUtils.log("Message font=" + messageFont);
101

102         Object JavaDoc[] defaults = {
103                 "Button.font", controlFont,
104                 "CheckBox.font", controlFont,
105                 "ColorChooser.font", controlFont,
106                 "ComboBox.font", controlFont,
107                 "EditorPane.font", controlFont,
108                 "FormattedTextField.font", controlFont,
109                 "Label.font", controlFont,
110                 "List.font", controlFont,
111                 "Panel.font", controlFont,
112                 "PasswordField.font", controlFont,
113                 "ProgressBar.font", controlFont,
114                 "RadioButton.font", controlFont,
115                 "ScrollPane.font", controlFont,
116                 "Spinner.font", controlFont,
117                 "TabbedPane.font", controlFont,
118                 "Table.font", controlFont,
119                 "TableHeader.font", controlFont,
120                 "TextField.font", controlFont,
121                 "TextPane.font", controlFont,
122                 "ToolBar.font", controlFont,
123                 "ToggleButton.font", controlFont,
124                 "Tree.font", controlFont,
125                 "Viewport.font", controlFont,
126
127                 "InternalFrame.titleFont", windowFont, // controlBold
128
"OptionPane.font", messageFont,
129                 "OptionPane.messageFont", messageFont,
130                 "OptionPane.buttonFont", messageFont,
131                 "Spinner.font", fixedControlFont,
132                 "TextArea.font", fixedControlFont,
133                 "TitledBorder.font", controlBoldFont,
134                 "ToolTip.font", toolTipFont,
135                 };
136         table.putDefaults(defaults);
137         
138         if (menuFont != null) {
139             Object JavaDoc[] menuDefaults = {
140                 "CheckBoxMenuItem.font", menuFont,
141                 "CheckBoxMenuItem.acceleratorFont", menuFont, // 1.3 only ?
142
"Menu.font", menuFont,
143                 "Menu.acceleratorFont", menuFont,
144                 "MenuBar.font", menuFont,
145                 "MenuItem.font", menuFont,
146                 "MenuItem.acceleratorFont", menuFont,
147                 "PopupMenu.font", menuFont,
148                 "RadioButtonMenuItem.font", menuFont,
149                 "RadioButtonMenuItem.acceleratorFont", menuFont, // 1.3 only ?
150
};
151             table.putDefaults(menuDefaults);
152         }
153     }
154     
155     
156     /**
157      * Computes and answers the menu font using the specified
158      * <code>UIDefaults</code> and <code>FontSizeHints</code>.<p>
159      *
160      * The defaults can be overriden using the system property "jgoodies.menuFont".
161      * You can set this property either by setting VM runtime arguments, e.g.
162      * <pre>
163      * -Djgoodies.menuFont=Tahoma-PLAIN-11
164      * </pre>
165      * or by setting them during the application startup process, e.g.
166      * <pre>
167      * System.setProperty(Options.MENU_FONT_KEY, "dialog-BOLD-12");
168      * </pre>
169      *
170      * @param table the UIDefaults table to work with
171      * @param hints the FontSizeHints used to determine the menu font
172      * @return the menu font for the given defaults and hints
173      */

174     public static Font JavaDoc getMenuFont(UIDefaults JavaDoc table, FontSizeHints hints) {
175         // Check whether a concrete font has been specified in the system properties.
176
String JavaDoc fontDescription = LookUtils.getSystemProperty(Options.MENU_FONT_KEY);
177         if (fontDescription != null) {
178             return Font.decode(fontDescription);
179         }
180         
181         Font JavaDoc menuFont= table.getFont("Menu.font");
182         if (menuFont.getName().equals("Tahoma")) {
183             float size = menuFont.getSize() + hints.menuFontSizeDelta();
184             float minSize = hints.menuFontSize();
185             menuFont = menuFont.deriveFont(Math.max(minSize, size));
186         }
187         
188         return new FontUIResource JavaDoc(menuFont);
189     }
190     
191     
192     /**
193      * Computes and answers the control font using the specified
194      * <code>UIDefaults</code> and <code>FontSizeHints</code>.<p>
195      *
196      * The defaults can be overriden using the system property "jgoodies.controlFont".
197      * You can set this property either by setting VM runtime arguments, e.g.
198      * <pre>
199      * -Djgoodies.controlFont=Tahoma-PLAIN-14
200      * </pre>
201      * or by setting them during the application startup process, e.g.
202      * <pre>
203      * System.setProperty(Options.CONTROL_FONT_KEY, "Arial-ITALIC-12");
204      * </pre>
205      *
206      * @param table the UIDefaults table to work with
207      * @param hints the FontSizeHints used to determine the control font
208      * @return the control font for the given defaults and hints
209      */

210     public static Font JavaDoc getControlFont(UIDefaults JavaDoc table, FontSizeHints hints) {
211         // Check whether a concrete font has been specified in the system properties.
212
String JavaDoc fontDescription = LookUtils.getSystemProperty(Options.CONTROL_FONT_KEY);
213         if (fontDescription != null) {
214             return Font.decode(fontDescription);
215         }
216         
217         Font JavaDoc controlFont;
218             //LookUtils.log("Label.font =" + table.getFont("Label.font"));
219
//LookUtils.log("Button.font =" + table.getFont("Button.font"));
220
//LookUtils.log("OptionPane.font=" + table.getFont("OptionPane.font"));
221

222             String JavaDoc fontKey = LookUtils.IS_JAVA_1_4_0
223                 ? "Label.font"
224                 : "OptionPane.font";
225             controlFont = table.getFont(fontKey);
226             if (controlFont.getName().equals("Tahoma")) {
227                 float oldSize = controlFont.getSize();
228                 float minSize = hints.controlFontSize();
229                 float size = oldSize + hints.controlFontSizeDelta();
230                 controlFont = controlFont.deriveFont(Math.max(minSize, size));
231             }
232         //System.out.println("Hints font size =" + hints.controlFontSize());
233
//System.out.println("Hints size delta =" + hints.controlFontSizeDelta());
234
//System.out.println("Control font size=" + controlFont.getSize());
235
return new FontUIResource JavaDoc(controlFont);
236     }
237     
238         
239 }
Popular Tags