001: /*
002: * Copyright (c) 2001-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.looks;
032:
033: import java.awt.Font;
034:
035: import javax.swing.plaf.FontUIResource;
036:
037: /**
038: * Provides predefined FontSet implementations.
039: *
040: * @author Karsten Lentzsch
041: * @version $Revision: 1.11 $
042: *
043: * @see FontSet
044: * @see FontPolicy
045: * @see FontPolicies
046: *
047: * @since 2.0
048: */
049: public final class FontSets {
050:
051: private static FontSet logicalFontSet;
052:
053: private FontSets() {
054: // Override default constructor; prevents instantation.
055: }
056:
057: // Default FontSets *******************************************************
058:
059: /**
060: * Creates and returns a FontSet that is based only
061: * on the given control font. The small font will be
062: * derived from the control font; all other fonts
063: * returned are the control font.
064: *
065: * @param controlFont the font used for all controls
066: *
067: * @return a FontSet based on the given fonts
068: *
069: * @throws NullPointerException if the control font is <code>null</code>
070: */
071: public static FontSet createDefaultFontSet(Font controlFont) {
072: return createDefaultFontSet(controlFont, null);
073: }
074:
075: /**
076: * Creates and returns a FontSet that is based on the given control font
077: * and menu font. The small font will be derived from the control font;
078: * all other fonts return, except the menu font, are the control font.
079: *
080: * @param controlFont the font used for all controls
081: * @param menuFont the font used for the menu bar and menu items
082: *
083: * @return a FontSet based on the given fonts
084: *
085: * @throws NullPointerException if the control font is <code>null</code>
086: */
087: public static FontSet createDefaultFontSet(Font controlFont,
088: Font menuFont) {
089: return createDefaultFontSet(controlFont, menuFont, null, null,
090: null, null);
091: }
092:
093: /**
094: * Creates and returns a FontSet that is based on the given control font
095: * and menu font. The small font will be derived from the control font;
096: * all other fonts return, except the menu font, are the control font.
097: *
098: * @param controlFont the font used for all controls
099: * @param menuFont the font used for the menu bar and menu items
100: * @param titleFont used for TitledBorder, titles and titled separators
101: *
102: * @return a FontSet based on the given fonts
103: *
104: * @throws NullPointerException if the control font is <code>null</code>
105: */
106: public static FontSet createDefaultFontSet(Font controlFont,
107: Font menuFont, Font titleFont) {
108: return createDefaultFontSet(controlFont, menuFont, titleFont,
109: null, null, null);
110: }
111:
112: /**
113: * Creates and returns a FontSet for the given fonts.
114: * If a font is <code>null</code>, it uses the control font as
115: * fallback. If the small font is <code>null</code> it will
116: * be derived from the control font.
117: *
118: * @param controlFont used for all controls
119: * @param menuFont used for the menu bar and menu items
120: * @param titleFont used for TitledBorder, titles and titled separators
121: * @param messageFont used for OptionPanes
122: * @param smallFont used for tool tips and similar components
123: * @param windowTitleFont used for internal frame window titles
124: *
125: * @return a FontSet based on the given fonts
126: *
127: * @throws NullPointerException if the control font is <code>null</code>
128: */
129: public static FontSet createDefaultFontSet(Font controlFont,
130: Font menuFont, Font titleFont, Font messageFont,
131: Font smallFont, Font windowTitleFont) {
132: return new DefaultFontSet(controlFont, menuFont, titleFont,
133: messageFont, smallFont, windowTitleFont);
134: }
135:
136: // Logical FontSet ********************************************************
137:
138: /**
139: * Lazily creates and returns the FontSet that returns
140: * the logical fonts specified by the Java runtime environment.
141: *
142: * @return a FontSets that uses the logical fonts specified
143: * by the Java environment
144: */
145: public static FontSet getLogicalFontSet() {
146: if (logicalFontSet == null) {
147: logicalFontSet = new LogicalFontSet();
148: }
149: return logicalFontSet;
150: }
151:
152: // Helper Code ************************************************************
153:
154: private static final class DefaultFontSet implements FontSet {
155:
156: private final FontUIResource controlFont;
157: private final FontUIResource menuFont;
158: private final FontUIResource titleFont;
159: private final FontUIResource messageFont;
160: private final FontUIResource smallFont;
161: private final FontUIResource windowTitleFont;
162:
163: // Instance Creation --------------------------------------------------
164:
165: /**
166: * Constructs a DefaultFontSet for the given fonts.
167: * If a font is <code>null</code>, it uses the control font as
168: * fallback. If the small font is <code>null</code> it will
169: * be derived from the control font.
170: *
171: * @param controlFont used for all controls
172: * @param menuFont used for the menu bar and menu items
173: * @param titleFont used for TitledBorder, titles and titled separators
174: * @param messageFont used for OptionPanes
175: * @param smallFont used for tool tips and similar components
176: * @param windowTitleFont used for internal frame window titles
177: *
178: * @throws NullPointerException if the control font is <code>null</code>
179: */
180: public DefaultFontSet(Font controlFont, Font menuFont,
181: Font titleFont, Font messageFont, Font smallFont,
182: Font windowTitleFont) {
183: this .controlFont = new FontUIResource(controlFont);
184: this .menuFont = menuFont != null ? new FontUIResource(
185: menuFont) : this .controlFont;
186: this .titleFont = titleFont != null ? new FontUIResource(
187: titleFont) : this .controlFont; //new FontUIResource(controlFont.deriveFont(Font.BOLD))
188: this .messageFont = messageFont != null ? new FontUIResource(
189: messageFont)
190: : this .controlFont;
191: this .smallFont = new FontUIResource(
192: smallFont != null ? smallFont : controlFont
193: .deriveFont(controlFont.getSize2D() - 2f));
194: this .windowTitleFont = windowTitleFont != null ? new FontUIResource(
195: windowTitleFont)
196: : this .titleFont;
197: }
198:
199: // FontSet API --------------------------------------------------------
200:
201: public FontUIResource getControlFont() {
202: return controlFont;
203: }
204:
205: public FontUIResource getMenuFont() {
206: return menuFont;
207: }
208:
209: public FontUIResource getTitleFont() {
210: return titleFont;
211: }
212:
213: public FontUIResource getWindowTitleFont() {
214: return windowTitleFont;
215: }
216:
217: public FontUIResource getSmallFont() {
218: return smallFont;
219: }
220:
221: public FontUIResource getMessageFont() {
222: return messageFont;
223: }
224:
225: }
226:
227: /**
228: * Looks up and returns the logical fonts as specified
229: * by the Java runtime environment.
230: */
231: private static final class LogicalFontSet implements FontSet {
232:
233: private FontUIResource controlFont;
234: private FontUIResource titleFont;
235: private FontUIResource systemFont;
236: private FontUIResource smallFont;
237:
238: public FontUIResource getControlFont() {
239: if (controlFont == null) {
240: controlFont = new FontUIResource(Font.getFont(
241: "swing.plaf.metal.controlFont", new Font(
242: "Dialog", Font.PLAIN, 12)));
243:
244: }
245: return controlFont;
246: }
247:
248: public FontUIResource getMenuFont() {
249: return getControlFont();
250: }
251:
252: public FontUIResource getTitleFont() {
253: if (titleFont == null) {
254: titleFont = new FontUIResource(getControlFont()
255: .deriveFont(Font.BOLD));
256: }
257: return titleFont;
258: }
259:
260: public FontUIResource getSmallFont() {
261: if (smallFont == null) {
262: smallFont = new FontUIResource(Font.getFont(
263: "swing.plaf.metal.smallFont", new Font(
264: "Dialog", Font.PLAIN, 10)));
265: }
266: return smallFont;
267: }
268:
269: public FontUIResource getMessageFont() {
270: if (systemFont == null) {
271: systemFont = new FontUIResource(Font.getFont(
272: "swing.plaf.metal.systemFont", new Font(
273: "Dialog", Font.PLAIN, 12)));
274: }
275: return systemFont;
276: }
277:
278: public FontUIResource getWindowTitleFont() {
279: return getTitleFont();
280: }
281:
282: }
283:
284: }
|