KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > util > FontProperties


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.gui.util;
19
20 import java.awt.Font JavaDoc;
21 import java.util.Observable JavaDoc;
22 import java.util.Observer JavaDoc;
23
24 import javax.swing.UIManager JavaDoc;
25 import javax.swing.plaf.FontUIResource JavaDoc;
26
27 import org.columba.core.config.Config;
28 import org.columba.core.xml.XmlElement;
29
30
31 /**
32  *
33  *
34  * Provides font configuration and helper methods to set the fonts
35  * application-wide.
36  * <p>
37  * text-font: this is the font used in the message-viewer and the composer
38  * <p>
39  * main-font: this is the application-wide font used for every gui element.
40  * <p>
41  * Generally Look and Feels set this. If the user wants to overwrite the Look
42  * and Feel font settings he/she has to change options.xml:/options/gui/fonts
43  * attribute: overwrite (true/false)
44  * <p>
45  * default is of course "false", to respect Look and Feel settings
46  *
47  * @author fdietz
48  */

49 public class FontProperties extends Observable JavaDoc implements Observer JavaDoc {
50     private static XmlElement fonts;
51
52     /**
53      *
54      */

55     public FontProperties() {
56         XmlElement options = Config.getInstance().get("options").getElement("/options");
57         XmlElement gui = options.getElement("gui");
58         fonts = gui.getElement("fonts");
59
60         if (fonts == null) {
61             fonts = gui.addSubElement("fonts");
62         }
63
64         XmlElement mainFontElement = fonts.getElement("main");
65
66         if (mainFontElement == null) {
67             mainFontElement = fonts.addSubElement("main");
68         }
69
70         XmlElement textFontElement = fonts.getElement("text");
71
72         if (textFontElement == null) {
73             textFontElement = fonts.addSubElement("text");
74         }
75
76         // register as configuration change listener
77
fonts.addObserver(this);
78     }
79
80     /**
81      * Gets the currently selected text font used in the message-viewer and
82      * composer editor.
83      *
84      * @return text font
85      */

86     public static Font JavaDoc getTextFont() {
87         return getFont("text");
88     }
89
90     /**
91      * Gets the currenlty selected widget font.
92      *
93      * @return widget font
94      */

95     public static Font JavaDoc getMainFont() {
96         return getFont("main");
97     }
98
99     /**
100      * Gets the currently configured font
101      *
102      * @param id
103      * can be of value "text" or "main"
104      * @return currently selected font
105      */

106     protected static Font JavaDoc getFont(String JavaDoc id) {
107         XmlElement textFontElement = fonts.getElement(id);
108
109         if (textFontElement == null) {
110             textFontElement = fonts.addSubElement(id);
111         }
112
113         boolean overwrite = Boolean.valueOf(fonts.getAttribute("overwrite",
114                     "true")).booleanValue();
115
116         Font JavaDoc font = null;
117         String JavaDoc name = null;
118         String JavaDoc size = null;
119
120         if (!overwrite) {
121             name = "Default";
122             size = "12";
123
124             font = new Font JavaDoc(name, Font.PLAIN, Integer.parseInt(size));
125         } else {
126             name = textFontElement.getAttribute("name", "Default");
127             size = textFontElement.getAttribute("size", "12");
128
129             font = new Font JavaDoc(name, Font.PLAIN, Integer.parseInt(size));
130         }
131
132         return font;
133     }
134
135     /**
136      *
137      * overwrite Look and Feel font settings
138      *
139      * @param item
140      * font configuration item
141      */

142     public static void setFont() {
143         // should we really overwrite the Look and Feel font settings
144
boolean overwrite = Boolean.valueOf(fonts.getAttribute("overwrite",
145                     "true")).booleanValue();
146
147         if (!overwrite) {
148             return;
149         }
150
151         FontUIResource JavaDoc mainFont = new FontUIResource JavaDoc(getFont("main"));
152
153         // patch submitted by forum user Turbo Chen
154
// FIXED: user wasn't able to enter chinese text in Composer Subject textfield
155

156         /*
157         UIManager.put("Label.font", mainFont);
158         UIManager.put("Textfield.font", mainFont);
159         UIManager.put("TextArea.font", mainFont);
160         UIManager.put("MenuItem.font", mainFont);
161         UIManager.put("MenuItem.acceleratorFont", mainFont);
162         UIManager.put("Menu.font", mainFont);
163         UIManager.put("Menu.acceleratorFont", mainFont);
164         UIManager.put("MenuBar.font", mainFont);
165         UIManager.put("Tree.font", mainFont);
166         UIManager.put("Table.font", mainFont);
167         UIManager.put("Button.font", mainFont);
168         UIManager.put("CheckBoxButton.font", mainFont);
169         UIManager.put("RadioButton.font", mainFont);
170         UIManager.put("ComboBox.font", mainFont);
171         UIManager.put("ToggleButton.font", mainFont);
172         UIManager.put("CheckBoxMenuItem.font", mainFont);
173         UIManager.put("RadioButtonMenuItem.font", mainFont);
174         UIManager.put("TabbedPane.font", mainFont);
175         UIManager.put("List.font", mainFont);
176         */

177         java.util.Enumeration JavaDoc keys = UIManager.getDefaults().keys();
178
179         while (keys.hasMoreElements()) {
180             Object JavaDoc key = keys.nextElement();
181             Object JavaDoc value = UIManager.get(key);
182
183             if (value instanceof javax.swing.plaf.FontUIResource JavaDoc) {
184                 UIManager.put(key, mainFont);
185             }
186         }
187     }
188
189     /**
190      * Gets fired if configuration changes.
191      *
192      * @see org.colulmba.core.gui.config.GeneralOptionsDialog
193      *
194      * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
195      */

196     public void update(Observable JavaDoc arg0, Object JavaDoc arg1) {
197     }
198 }
199
Popular Tags