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.UIDefaults;
036:
037: /**
038: * Provides predefined FontPolicy implementations.<p>
039: *
040: * <strong>Note:</strong> The available policies work well on Windows.
041: * On other platforms the fonts specified by the runtime environment
042: * are chosen. I plan to provide more logic or options for other platforms,
043: * for example that a Linux system checks for a Tahoma or Segoe UI.<p>
044: *
045: * TODO: Add a check for a custom font policy set in the System properties.<p>
046: *
047: * TODO: Add policies that emulate different Windows setups:
048: * default XP on 96dpi with normal fonts ("XP-normal-96"),
049: * Vista on 120dpi with large fonts ("Vista-large-120"), etc.
050: *
051: * @author Karsten Lentzsch
052: * @version $Revision: 1.8 $
053: *
054: * @see FontPolicy
055: * @see FontSet
056: * @see FontSets
057: * @see Fonts
058: *
059: * @since 2.0
060: */
061: public final class FontPolicies {
062:
063: private FontPolicies() {
064: // Override default constructor; prevents instantation.
065: }
066:
067: // Getting a FontPolicy *********************************************
068:
069: /**
070: * Returns a font policy that in turn always returns the specified FontSet.
071: * The FontSet will be fixed, but the FontSet itself may
072: * return different fonts in different environments.
073: *
074: * @param fontSet the FontSet to be return by this policy
075: * @return a font policy that returns the specified FontSet.
076: */
077: public static FontPolicy createFixedPolicy(FontSet fontSet) {
078: return new FixedPolicy(fontSet);
079: }
080:
081: /**
082: * Returns a font policy that checks for a custom FontPolicy
083: * and a custom FontSet specified in the System settings or UIManager.
084: * If no custom settings are available, the given default policy will
085: * be used to look up the FontSet.
086: *
087: * @param defaultPolicy the policy used if there are no custom settings
088: * @return a FontPolicy that checks for custom settings
089: * before the default policy is returned.
090: */
091: public static FontPolicy customSettingsPolicy(
092: FontPolicy defaultPolicy) {
093: return new CustomSettingsPolicy(defaultPolicy);
094: }
095:
096: // /**
097: // * Returns the default platform independent font choice policy.<p>
098: // *
099: // * The current implementation just returns the logical fonts.
100: // * A future version shall check for available good fonts
101: // * and shall use them before it falls back to the logical fonts.
102: // *
103: // * @return the default platform independent font choice policy.
104: // */
105: // public static FontPolicy getDefaultCrossPlatformPolicy() {
106: // return new DefaultCrossPlatformPolicy();
107: // }
108:
109: /**
110: * Returns the default font policy for Plastic on the Windows platform.
111: * It differs from the default Windows policy in that it uses a bold font
112: * for TitledBorders, titles, and titled separators.
113: *
114: * @return the default font policy for Plastic on the Windows platform.
115: */
116: public static FontPolicy getDefaultPlasticOnWindowsPolicy() {
117: return new DefaultPlasticOnWindowsPolicy();
118: }
119:
120: /**
121: * Returns the default Plastic FontPolicy that may vary
122: * with the platform and environment.
123: * On Windows, the PlasticOnWindowsPolicy is returned that
124: * is much like the defualt WindowsPolicy but uses a bold title font.
125: * On other Platforms, the logical fonts policy is returned
126: * that uses the logical fonts as specified by the Java runtime environment.
127: *
128: * @return a Windows-like policy on Windows, a logical fonts policy
129: * on all other platforms
130: */
131: public static FontPolicy getDefaultPlasticPolicy() {
132: if (LookUtils.IS_OS_WINDOWS) {
133: return getDefaultPlasticOnWindowsPolicy();
134: }
135: return getLogicalFontsPolicy();
136: // return getDefaultCrossPlatformPolicy();
137: }
138:
139: /**
140: * Returns the default font policy for the Windows platform.
141: * It aims to return a FontSet that is close to the native guidelines
142: * and useful for the current Java environment.<p>
143: *
144: * The control font scales with the platform screen resolution
145: * (96dpi/101dpi/120dpi/144dpi/...) and honors the desktop font settings
146: * (normal/large/extra large).
147: *
148: * @return the default font policy for the Windows platform.
149: */
150: public static FontPolicy getDefaultWindowsPolicy() {
151: return new DefaultWindowsPolicy();
152: }
153:
154: /**
155: * Returns a font policy that returns the logical fonts
156: * as specified by the Java runtime environment.
157: *
158: * @return a font policy that returns logical fonts.
159: */
160: public static FontPolicy getLogicalFontsPolicy() {
161: return createFixedPolicy(FontSets.getLogicalFontSet());
162: }
163:
164: /**
165: * Returns a font policy for getting a Plastic appearance that aims to be
166: * visual backward compatible with the JGoodies Looks version 1.x.
167: * It uses a font choice similar to the choice implemented
168: * by the Plastic L&fs in the JGoodies Looks version 1.x.
169: *
170: * @return a font policy that aims to reproduce the Plastic font choice
171: * in the JGoodies Looks 1.x.
172: */
173: public static FontPolicy getLooks1xPlasticPolicy() {
174: Font controlFont = Fonts
175: .getDefaultGUIFontWesternModernWindowsNormal();
176: Font menuFont = controlFont;
177: Font titleFont = controlFont.deriveFont(Font.BOLD);
178: FontSet fontSet = FontSets.createDefaultFontSet(controlFont,
179: menuFont, titleFont);
180: return createFixedPolicy(fontSet);
181: }
182:
183: /**
184: * Returns a font policy for getting a Windows appearance that aims to be
185: * visual backward compatible with the JGoodies Looks version 1.x.
186: * It uses a font choice similar to the choice implemented
187: * by the Windows L&f in the JGoodies Looks version 1.x.
188: *
189: * @return a font policy that aims to reproduce the Windows font choice
190: * in the JGoodies Looks 1.x.
191: */
192: public static FontPolicy getLooks1xWindowsPolicy() {
193: return new Looks1xWindowsPolicy();
194: }
195:
196: /**
197: * Returns a font policy intended for API users that want to
198: * move Plastic code from the Looks 1.x to the Looks 2.0.
199: * On Windows, it uses the Looks 2.0 Plastic fonts,
200: * on other platforms it uses the Looks 1.x Plastic fonts.
201: *
202: * @return the recent Plastic font policy on Windows,
203: * the JGoodies Looks 1.x on other Platforms.
204: */
205: public static FontPolicy getTransitionalPlasticPolicy() {
206: return LookUtils.IS_OS_WINDOWS ? getDefaultPlasticOnWindowsPolicy()
207: : getLooks1xPlasticPolicy();
208: }
209:
210: // Utility Methods ********************************************************
211:
212: /**
213: * Looks up and returns a custom FontSet for the given
214: * Look&Feel name, or <code>null</code> if no custom font set
215: * has been defined for this Look&Feel.
216: *
217: * @param the name of the Look&Feel, one of <code>"Plastic"</code> or
218: * <code>"Windows"</code>
219: * @return a custom FontPolicy - if any - or otherwise <code>null</code>
220: */
221: private static FontSet getCustomFontSet(String lafName) {
222: String controlFontKey = lafName + ".controlFont";
223: String menuFontKey = lafName + ".menuFont";
224: String decodedControlFont = LookUtils
225: .getSystemProperty(controlFontKey);
226: if (decodedControlFont == null)
227: return null;
228: Font controlFont = Font.decode(decodedControlFont);
229: String decodedMenuFont = LookUtils
230: .getSystemProperty(menuFontKey);
231: Font menuFont = decodedMenuFont != null ? Font
232: .decode(decodedMenuFont) : null;
233: Font titleFont = "Plastic".equals(lafName) ? controlFont
234: .deriveFont(Font.BOLD) : controlFont;
235: return FontSets.createDefaultFontSet(controlFont, menuFont,
236: titleFont);
237: }
238:
239: /**
240: * Looks up and returns a custom FontPolicy for the given
241: * Look&Feel name, or <code>null</code> if no custom policy has been
242: * defined for this Look&Feel.
243: *
244: * @param the name of the Look&Feel, one of <code>"Plastic"</code> or
245: * <code>"Windows"</code>
246: * @return a custom FontPolicy - if any - or otherwise <code>null</code>
247: */
248: private static FontPolicy getCustomPolicy(String lafName) {
249: // TODO: Look up predefined font choice policies
250: return null;
251: }
252:
253: private static final class CustomSettingsPolicy implements
254: FontPolicy {
255:
256: private final FontPolicy wrappedPolicy;
257:
258: CustomSettingsPolicy(FontPolicy wrappedPolicy) {
259: this .wrappedPolicy = wrappedPolicy;
260: }
261:
262: public FontSet getFontSet(String lafName, UIDefaults table) {
263: FontPolicy customPolicy = getCustomPolicy(lafName);
264: if (customPolicy != null) {
265: return customPolicy.getFontSet(null, table);
266: }
267: FontSet customFontSet = getCustomFontSet(lafName);
268: if (customFontSet != null) {
269: return customFontSet;
270: }
271: return wrappedPolicy.getFontSet(lafName, table);
272: }
273: }
274:
275: // private static final class DefaultCrossPlatformPolicy implements FontPolicy {
276: //
277: // public FontSet getFontSet(String lafName, UIDefaults table) {
278: // // TODO: If Tahoma or Segoe UI is available, return them
279: // // in a size appropriate for the screen resolution.
280: // // Otherwise return the logical font set.
281: // return FontSets.getLogicalFontSet();
282: // }
283: // }
284:
285: /**
286: * Implements the default font lookup for the Plastic L&f family
287: * when running in a Windows environment.
288: */
289: private static final class DefaultPlasticOnWindowsPolicy implements
290: FontPolicy {
291:
292: public FontSet getFontSet(String lafName, UIDefaults table) {
293: Font windowsControlFont = Fonts.getWindowsControlFont();
294: Font controlFont;
295: if (windowsControlFont != null) {
296: controlFont = windowsControlFont;
297: } else if (table != null) {
298: controlFont = table.getFont("Button.font");
299: } else {
300: controlFont = new Font("Dialog", Font.PLAIN, 12);
301: }
302:
303: Font menuFont = table == null ? controlFont : table
304: .getFont("Menu.font");
305: Font titleFont = controlFont.deriveFont(Font.BOLD);
306:
307: return FontSets.createDefaultFontSet(controlFont, menuFont,
308: titleFont);
309: }
310: }
311:
312: /**
313: * Implements the default font lookup on the Windows platform.
314: */
315: private static final class DefaultWindowsPolicy implements
316: FontPolicy {
317:
318: public FontSet getFontSet(String lafName, UIDefaults table) {
319: Font windowsControlFont = Fonts.getWindowsControlFont();
320: Font controlFont;
321: if (windowsControlFont != null) {
322: controlFont = windowsControlFont;
323: } else if (table != null) {
324: controlFont = table.getFont("Button.font");
325: } else {
326: controlFont = new Font("Dialog", Font.PLAIN, 12);
327: }
328: Font menuFont = table == null ? controlFont : table
329: .getFont("Menu.font");
330: Font titleFont = controlFont;
331: Font messageFont = table == null ? controlFont : table
332: .getFont("OptionPane.font");
333: Font smallFont = table == null ? controlFont
334: .deriveFont(controlFont.getSize2D() - 2f) : table
335: .getFont("ToolTip.font");
336: Font windowTitleFont = table == null ? controlFont : table
337: .getFont("InternalFrame.titleFont");
338: return FontSets.createDefaultFontSet(controlFont, menuFont,
339: titleFont, messageFont, smallFont, windowTitleFont);
340: }
341: }
342:
343: /**
344: * A FontPolicy that returns a fixed FontSet and that ignores
345: * the laf name and UIDefaults table.
346: */
347: private static final class FixedPolicy implements FontPolicy {
348:
349: private final FontSet fontSet;
350:
351: FixedPolicy(FontSet fontSet) {
352: this .fontSet = fontSet;
353: }
354:
355: public FontSet getFontSet(String lafName, UIDefaults table) {
356: return fontSet;
357: }
358: }
359:
360: /**
361: * Aims to mimic the font choice as used in the JGoodies Looks 1.x.
362: */
363: private static final class Looks1xWindowsPolicy implements
364: FontPolicy {
365:
366: public FontSet getFontSet(String lafName, UIDefaults table) {
367: Font windowsControlFont = Fonts
368: .getLooks1xWindowsControlFont();
369: Font controlFont;
370: if (windowsControlFont != null) {
371: controlFont = windowsControlFont;
372: } else if (table != null) {
373: controlFont = table.getFont("Button.font");
374: } else {
375: controlFont = new Font("Dialog", Font.PLAIN, 12);
376: }
377: return FontSets.createDefaultFontSet(controlFont);
378: }
379: }
380:
381: }
|