[jsword-svn] r1599 - in trunk: common common-swing/src/main/java/org/crosswire/common/swing

dmsmith at www.crosswire.org dmsmith at www.crosswire.org
Wed Aug 1 04:27:45 MST 2007


Author: dmsmith
Date: 2007-08-01 04:27:45 -0700 (Wed, 01 Aug 2007)
New Revision: 1599

Added:
   trunk/common-swing/src/main/java/org/crosswire/common/swing/FontStore.java
Modified:
   trunk/common/JSwordDictionary.txt
Log:
Fundamental mechanism for persistent font specifications.

Modified: trunk/common/JSwordDictionary.txt
===================================================================
--- trunk/common/JSwordDictionary.txt	2007-08-01 07:19:43 UTC (rev 1598)
+++ trunk/common/JSwordDictionary.txt	2007-08-01 11:27:45 UTC (rev 1599)
@@ -10,3 +10,6 @@
 combo
 tabbed
 locale
+fallback
+yahoo
+com

Added: trunk/common-swing/src/main/java/org/crosswire/common/swing/FontStore.java
===================================================================
--- trunk/common-swing/src/main/java/org/crosswire/common/swing/FontStore.java	                        (rev 0)
+++ trunk/common-swing/src/main/java/org/crosswire/common/swing/FontStore.java	2007-08-01 11:27:45 UTC (rev 1599)
@@ -0,0 +1,240 @@
+/**
+ * Distribution License:
+ * JSword is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License, version 2.1 as published by
+ * the Free Software Foundation. This program is distributed in the hope
+ * that it will be useful, but WITHOUT ANY WARRANTY; without even the
+ * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * The License is available on the internet at:
+ *       http://www.gnu.org/copyleft/lgpl.html
+ * or by writing to:
+ *      Free Software Foundation, Inc.
+ *      59 Temple Place - Suite 330
+ *      Boston, MA 02111-1307, USA
+ *
+ * Copyright: 2007
+ *     The copyright to this program is held by it's authors.
+ *
+ * ID: $Id: org.eclipse.jdt.ui.prefs 1178 2006-11-06 12:48:02Z dmsmith $
+ */
+package org.crosswire.common.swing;
+
+import java.awt.Font;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Properties;
+
+import org.crosswire.common.util.Logger;
+import org.crosswire.common.util.NetUtil;
+
+/**
+ * Font Store maintains a persistent, hierarchical store of user font preferences. A font
+ * preference consists of the name of a resource and a font specification for
+ * that resource. The name of the resource may be any unique value that
+ * follows the rules for a property key. The font specification is the font
+ * itself or a string representation of the font that can be turned into a font
+ * with <code>Font.decode(String)</code>.
+ * <p>
+ * Many languages share the same script. Rather than setting a font spec for
+ * many resources with the same language, this class makes it possible to set
+ * a font spec for each language.
+ * </p>
+ * <p>
+ * Thus, the look up hierarchy begins with an exact match for the requested resource.
+ * If it does not work the lookup continues in the following order: 
+ * the specified language's font, the fallback font, and
+ * the default font. Of course, if that does not work, use any font that
+ * Java thinks is appropriate, but use the size and style of the default
+ * font.
+ * Since scripts are shared by many languages, this FontStore supports the
+ * setting of Language defaults. If the requested language font does not exist a
+ * more general one will be provided.
+ * </p>
+ * 
+ * @see gnu.lgpl.License for license details.<br>
+ *      The copyright to this program is held by it's authors.
+ * @author DM Smith [dmsmith555 at yahoo dot com]
+ */
+public class FontStore
+{
+
+    /**
+     * Create an new FontStore with the given persistent store.
+     * 
+     * @param storeName The name of the store, used as a label inside the
+     *            fontStore.
+     * @param fontStore The location of the resource.
+     */
+    public FontStore(String storeName, URI fontStore)
+    {
+        if (fontStore == null)
+        {
+            throw new IllegalArgumentException("fontStore cannot be null"); //$NON-NLS-1$
+        }
+        this.storeName = storeName;
+        this.fontStore = fontStore;
+        this.fontMap = new Properties();
+    }
+
+    /**
+     * @return the defaultFont
+     */
+    public String getDefaultFont()
+    {
+        load();
+        defaultFont = fontMap.getProperty(DEFAULT_KEY, DEFAULT_FONT);
+        return defaultFont;
+    }
+
+    /**
+     * @param defaultFont the defaultFont to set
+     */
+    public void setDefaultFont(String defaultFont)
+    {
+        load();
+        this.defaultFont = defaultFont;
+        fontMap.setProperty(DEFAULT_KEY, defaultFont);
+        store();
+    }
+
+    /**
+     * Store a font specification for the resource.
+     * 
+     * @param resource the resource
+     * @param fontSpec the font specification, understandable by
+     *            <code>Font.decode()</code>
+     */
+    public void setFont(String resource, String fontSpec)
+    {
+        load();
+        fontMap.setProperty(resource, fontSpec);
+        store();
+    }
+
+    /**
+     * Get a font for the specified resource. If it does not work try the
+     * following in order: the specified language's font, the fallback font, and
+     * the default font. Of course, if that does not work, use any font that
+     * Java thinks is appropriate, but use the size and style of the default
+     * font.
+     * 
+     * @param resource the name of the resource for whom the font is stored.
+     * @param lang the language of the resource
+     * @return the requested font if possible. A fallback font otherwise.
+     */
+    public Font getFont(String resource, String fallback, String lang)
+    {
+        load();
+
+        String fontSpec = null;
+        if (resource != null)
+        {
+            fontSpec = fontMap.getProperty(resource);
+        }
+
+        if (fontSpec != null)
+        {
+            Font obtainedFont = obtainFont(fontSpec);
+            if (obtainedFont != null)
+            {
+                return obtainedFont;
+            }
+            fontSpec = null;
+        }
+
+        if (lang != null)
+        {
+            fontSpec = fontMap.getProperty(new StringBuffer(LANG_KEY_PREFIX).append(lang).toString());
+        }
+
+        if (fontSpec != null)
+        {
+            Font obtainedFont = obtainFont(fontSpec);
+            if (obtainedFont != null)
+            {
+                return obtainedFont;
+            }
+        }
+
+        fontSpec = fallback;
+        if (fontSpec != null)
+        {
+            Font obtainedFont = obtainFont(fontSpec);
+            if (obtainedFont != null)
+            {
+                return obtainedFont;
+            }
+        }
+
+        return GuiConvert.string2Font(defaultFont);
+    }
+
+    /**
+     * Load the store, if it has not been loaded.
+     */
+    private void load()
+    {
+        if (loaded)
+        {
+            return;
+        }
+
+        try
+        {
+            fontMap = NetUtil.loadProperties(fontStore);
+            loaded = true;
+        }
+        catch (IOException e)
+        {
+            log.error("Unable to load the font store: " + fontStore); //$NON-NLS-1$
+            fontMap = new Properties();
+        }
+    }
+
+    /**
+     * Store the store, if it exists.
+     */
+    private void store()
+    {
+        load();
+
+        try
+        {
+            NetUtil.storeProperties(fontMap, fontStore, storeName);
+        }
+        catch (IOException ex)
+        {
+            log.error("Failed to save BibleDesktop UI Translation", ex); //$NON-NLS-1$
+        }
+    }
+
+    private Font obtainFont(String fontSpec)
+    {
+        if (fontSpec != null)
+        {
+            // Creating a font never fails. Java just silently does
+            // substitution.
+            // Ensure that substitution does not happen.
+            Font obtainedFont = GuiConvert.string2Font(fontSpec);
+            String obtainedFontSpec = GuiConvert.font2String(obtainedFont);
+            if (obtainedFontSpec != null && obtainedFontSpec.equalsIgnoreCase(fontSpec))
+            {
+                return obtainedFont;
+            }
+        }
+        return null;
+    }
+
+    private static final String DEFAULT_FONT    = "Dialog-PLAIN-12";                //$NON-NLS-1$
+    private String              storeName;
+    private String              defaultFont;
+    private URI                 fontStore;
+    private boolean             loaded;
+    private Properties          fontMap;
+
+    private static final String LANG_KEY_PREFIX = "lang.";                          //$NON-NLS-1$
+    private static final String DEFAULT_KEY     = "default";                        //$NON-NLS-1$
+    private static final Logger log             = Logger.getLogger(FontStore.class);
+}




More information about the jsword-svn mailing list