001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.gui.util;
019:
020: import java.awt.Font;
021: import java.util.Observable;
022: import java.util.Observer;
023:
024: import javax.swing.UIManager;
025: import javax.swing.plaf.FontUIResource;
026:
027: import org.columba.core.config.Config;
028: import org.columba.core.xml.XmlElement;
029:
030: /**
031: *
032: *
033: * Provides font configuration and helper methods to set the fonts
034: * application-wide.
035: * <p>
036: * text-font: this is the font used in the message-viewer and the composer
037: * <p>
038: * main-font: this is the application-wide font used for every gui element.
039: * <p>
040: * Generally Look and Feels set this. If the user wants to overwrite the Look
041: * and Feel font settings he/she has to change options.xml:/options/gui/fonts
042: * attribute: overwrite (true/false)
043: * <p>
044: * default is of course "false", to respect Look and Feel settings
045: *
046: * @author fdietz
047: */
048: public class FontProperties extends Observable implements Observer {
049: private static XmlElement fonts;
050:
051: /**
052: *
053: */
054: public FontProperties() {
055: XmlElement options = Config.getInstance().get("options")
056: .getElement("/options");
057: XmlElement gui = options.getElement("gui");
058: fonts = gui.getElement("fonts");
059:
060: if (fonts == null) {
061: fonts = gui.addSubElement("fonts");
062: }
063:
064: XmlElement mainFontElement = fonts.getElement("main");
065:
066: if (mainFontElement == null) {
067: mainFontElement = fonts.addSubElement("main");
068: }
069:
070: XmlElement textFontElement = fonts.getElement("text");
071:
072: if (textFontElement == null) {
073: textFontElement = fonts.addSubElement("text");
074: }
075:
076: // register as configuration change listener
077: fonts.addObserver(this );
078: }
079:
080: /**
081: * Gets the currently selected text font used in the message-viewer and
082: * composer editor.
083: *
084: * @return text font
085: */
086: public static Font getTextFont() {
087: return getFont("text");
088: }
089:
090: /**
091: * Gets the currenlty selected widget font.
092: *
093: * @return widget font
094: */
095: public static Font getMainFont() {
096: return getFont("main");
097: }
098:
099: /**
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 getFont(String id) {
107: XmlElement textFontElement = fonts.getElement(id);
108:
109: if (textFontElement == null) {
110: textFontElement = fonts.addSubElement(id);
111: }
112:
113: boolean overwrite = Boolean.valueOf(
114: fonts.getAttribute("overwrite", "true")).booleanValue();
115:
116: Font font = null;
117: String name = null;
118: String size = null;
119:
120: if (!overwrite) {
121: name = "Default";
122: size = "12";
123:
124: font = new Font(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(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(
145: fonts.getAttribute("overwrite", "true")).booleanValue();
146:
147: if (!overwrite) {
148: return;
149: }
150:
151: FontUIResource mainFont = new FontUIResource(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 keys = UIManager.getDefaults().keys();
178:
179: while (keys.hasMoreElements()) {
180: Object key = keys.nextElement();
181: Object value = UIManager.get(key);
182:
183: if (value instanceof javax.swing.plaf.FontUIResource) {
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 arg0, Object arg1) {
197: }
198: }
|