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