[jsword-svn] r1470 - in trunk: bibledesktop/src/main/java/org/crosswire/bibledesktop/desktop common/src/main/java/org/crosswire/common/config common/src/main/java/org/crosswire/common/progress common/src/main/java/org/crosswire/common/util jsword/src/main/java/org/crosswire/jsword/book/install jsword-limbo/src/main/java/org/crosswire/jsword/book/jdbc jsword-limbo/src/main/java/org/crosswire/jsword/book/raw

dmsmith at www.crosswire.org dmsmith at www.crosswire.org
Tue Jul 3 08:18:54 MST 2007


Author: dmsmith
Date: 2007-07-03 08:18:54 -0700 (Tue, 03 Jul 2007)
New Revision: 1470

Modified:
   trunk/bibledesktop/src/main/java/org/crosswire/bibledesktop/desktop/Desktop.java
   trunk/bibledesktop/src/main/java/org/crosswire/bibledesktop/desktop/Translations.java
   trunk/common/src/main/java/org/crosswire/common/config/Config.java
   trunk/common/src/main/java/org/crosswire/common/progress/Job.java
   trunk/common/src/main/java/org/crosswire/common/util/CollectionUtil.java
   trunk/common/src/main/java/org/crosswire/common/util/NetUtil.java
   trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/jdbc/JDBCBookDriver.java
   trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/raw/RawBookDriver.java
   trunk/jsword/src/main/java/org/crosswire/jsword/book/install/InstallManager.java
Log:
Fixed a bug that prevented Options from being internationalized into German or Farsi.

Modified: trunk/bibledesktop/src/main/java/org/crosswire/bibledesktop/desktop/Desktop.java
===================================================================
--- trunk/bibledesktop/src/main/java/org/crosswire/bibledesktop/desktop/Desktop.java	2007-07-03 13:00:31 UTC (rev 1469)
+++ trunk/bibledesktop/src/main/java/org/crosswire/bibledesktop/desktop/Desktop.java	2007-07-03 15:18:54 UTC (rev 1470)
@@ -150,14 +150,16 @@
      */
     public Desktop()
     {
+        // The first thing that has to be done is to set the locale.
+        Translations.instance().setLocale();
+
         // Load the configuration.
         // This has to be done before any gui components are created
-        // (Including the splash) and before setting the locale.
+        // (Including the splash).
         // This includes code that is invoked by it.
+        // This has to be done after setting the locale.
         generateConfig();
 
-        // Now set the locale to the one the user chose, if any.
-        Translations.setLocale();
 
         // Make this be the root frame of optiondialogs
         JOptionPane.setRootFrame(this);
@@ -1028,7 +1030,7 @@
     {
         refreshBooks();
 
-        Translations.register();
+        Translations.instance().register();
 
         // And the array of allowed osis>html converters
         Map converters = ConverterFactory.getKnownConverters();

Modified: trunk/bibledesktop/src/main/java/org/crosswire/bibledesktop/desktop/Translations.java
===================================================================
--- trunk/bibledesktop/src/main/java/org/crosswire/bibledesktop/desktop/Translations.java	2007-07-03 13:00:31 UTC (rev 1469)
+++ trunk/bibledesktop/src/main/java/org/crosswire/bibledesktop/desktop/Translations.java	2007-07-03 15:18:54 UTC (rev 1470)
@@ -21,12 +21,18 @@
  */
 package org.crosswire.bibledesktop.desktop;
 
+import java.io.IOException;
+import java.net.URI;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
+import java.util.Properties;
 
 import org.crosswire.common.config.ChoiceFactory;
 import org.crosswire.common.util.Languages;
+import org.crosswire.common.util.Logger;
+import org.crosswire.common.util.NetUtil;
+import org.crosswire.jsword.util.Project;
 
 /**
  * Translations provides a list of languages that BibleDesktop has been translated into.
@@ -38,13 +44,38 @@
 public class Translations
 {
     /**
-     * Utility classes have private constructors.
+     * Singleton classes have private constructors.
      */
     private Translations()
     {
+        try
+        {
+            URI inputURI = Project.instance().getWritablePropertiesURI(getClass().getName());
+            Properties props = NetUtil.loadProperties(inputURI);
+            translation = props.getProperty(TRANSLATION_KEY, DEFAULT_TRANSLATION);
+        }
+        catch (IOException e)
+        {
+            translation = DEFAULT_TRANSLATION;
+        }
     }
+    
+    /**
+     * All access to Translations is through this single instance.
+     * 
+     * @return the singleton instance
+     */
+    public static Translations instance()
+    {
+        return instance;
+    }
 
-    public static String[] getSupportedTranslations()
+    /**
+     * Gets a listing of all the translations that Bible Desktop supports.
+     * 
+     * @return an string array of translations in locale friendly names.
+     */
+    public String[] getSupported()
     {
         List names = new ArrayList();
 
@@ -60,7 +91,7 @@
      * Get the locale for the current translation.
      * @return the translation's locale
      */
-    public static Locale getCurrentLocale()
+    public Locale getCurrentLocale()
     {
         return new Locale(translation);
     }
@@ -70,7 +101,7 @@
      * 
      * @return the current translation
      */
-    public static String getCurrentTranslation()
+    public String getCurrent()
     {
         return Languages.getLanguage(translation);
     }
@@ -80,20 +111,31 @@
      * 
      * @param translation the translation to use
      */
-    public static void setCurrentTranslation(String translation)
+    public void setCurrent(String newTranslation)
     {
         String lang = DEFAULT_TRANSLATION;
         String currentLang = ""; //$NON-NLS-1$
         for (int i = 0; i < translations.length; i++)
         {
             currentLang = Languages.getLanguage(translations[i]);
-            if (currentLang.equals(translation))
+            if (currentLang.equals(newTranslation))
             {
                 lang = translations[i];
             }
         }
 
-        Translations.translation = lang;
+        try
+        {
+            translation = lang;
+            Properties props = new Properties();
+            props.put(TRANSLATION_KEY, translation);
+            URI outputURI = Project.instance().getWritablePropertiesURI(getClass().getName());
+            NetUtil.storeProperties(props, outputURI, "BibleDesktop UI Translation"); //$NON-NLS-1$
+        }
+        catch (IOException ex)
+        {
+            log.error("Failed to save BibleDesktop UI Translation", ex); //$NON-NLS-1$
+        }
     }
 
     /**
@@ -103,23 +145,53 @@
      * 
      * This only makes sense after config has called setCurrentTranslation.
      */
-    public static void setLocale()
+    public void setLocale()
     {
         if (!translation.equals(Translations.DEFAULT_TRANSLATION))
         {
-            Locale.setDefault(Translations.getCurrentLocale());
+            Locale.setDefault(getCurrentLocale());
         }
     }
 
     /**
      * Register this class with the common config engine.
      */
-    public static void register()
+    public void register()
     {
         ChoiceFactory.getDataMap().put(TRANSLATION_KEY, getSupportedTranslations());
     }
 
     /**
+     * Get the current translation as a human readable string.
+     * 
+     * @return the current translation
+     */
+    public static String getCurrentTranslation()
+    {
+        return Translations.instance().getCurrent();
+    }
+
+    /**
+     * Set the current translation, using human readable string.
+     * 
+     * @param translation the translation to use
+     */
+    public static void setCurrentTranslation(String newTranslation)
+    {
+        Translations.instance().setCurrent(newTranslation);
+    }
+
+    /**
+     * Gets a listing of all the translations that Bible Desktop supports.
+     * 
+     * @return an string array of translations in locale friendly names.
+     */
+    public static String[] getSupportedTranslations()
+    {
+        return Translations.instance().getSupported();
+    }
+
+    /**
      * The key used in config.xml
      */
     private static final String TRANSLATION_KEY = "translation-codes"; //$NON-NLS-1$
@@ -132,16 +204,22 @@
     /**
      * The language that BibleDesktop should use.
      */
-    private static String translation = DEFAULT_TRANSLATION;
+    private String translation = DEFAULT_TRANSLATION;
 
     /**
      * List of available languages.
-     * TODO(DMS): externalize this list.
      */
-    private static String[] translations = {
+    private String[] translations =
+        {
             "en", //$NON-NLS-1$
             "de", //$NON-NLS-1$
             "fa", //$NON-NLS-1$
-    };
+        };
 
+    private static Translations instance = new Translations();
+
+    /**
+     * The log stream
+     */
+    private static final Logger log = Logger.getLogger(Translations.class);
 }

Modified: trunk/common/src/main/java/org/crosswire/common/config/Config.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/config/Config.java	2007-07-03 13:00:31 UTC (rev 1469)
+++ trunk/common/src/main/java/org/crosswire/common/config/Config.java	2007-07-03 15:18:54 UTC (rev 1470)
@@ -24,10 +24,7 @@
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.beans.PropertyChangeSupport;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -36,7 +33,6 @@
 import java.util.ResourceBundle;
 
 import org.crosswire.common.util.EventListenerList;
-import org.crosswire.common.util.IOUtil;
 import org.crosswire.common.util.Logger;
 import org.crosswire.common.util.LucidException;
 import org.crosswire.common.util.NetUtil;
@@ -422,41 +418,16 @@
      */
     public void permanentToLocal(URI uri) throws IOException
     {
-        InputStream is = null;
-        try
-        {
-            is = NetUtil.getInputStream(uri);
-            Properties prop = new Properties();
-            prop.load(is);
-            is.close();
-            setProperties(prop);
-        }
-        finally
-        {
-            IOUtil.close(is);
-        }
+        setProperties(NetUtil.loadProperties(uri));
     }
 
     /**
      * Take the data in the local storage area and store it permanently,
      * using the configured storage area.
      */
-    public void localToPermanent(URI url) throws IOException
+    public void localToPermanent(URI uri) throws IOException
     {
-        OutputStream out = null;
-
-        try
-        {
-            out = new FileOutputStream(url.getPath());
-            getProperties().store(out, title);
-        }
-        finally
-        {
-            if (out != null)
-            {
-                out.close();
-            }
-        }
+        NetUtil.storeProperties(getProperties(), uri, title);
     }
 
     /**

Modified: trunk/common/src/main/java/org/crosswire/common/progress/Job.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/progress/Job.java	2007-07-03 13:00:31 UTC (rev 1469)
+++ trunk/common/src/main/java/org/crosswire/common/progress/Job.java	2007-07-03 15:18:54 UTC (rev 1470)
@@ -22,8 +22,6 @@
 package org.crosswire.common.progress;
 
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Date;
@@ -35,7 +33,6 @@
 import java.util.Timer;
 import java.util.TimerTask;
 
-import org.crosswire.common.util.IOUtil;
 import org.crosswire.common.util.Logger;
 import org.crosswire.common.util.NetUtil;
 
@@ -456,49 +453,39 @@
      */
     private synchronized void loadPredictions()
     {
-        InputStream in = null;
         try
         {
-            in = NetUtil.getInputStream(predictURI);
-            if (in != null)
+            predicted = new HashMap();
+            Properties temp = NetUtil.loadProperties(predictURI);
+
+            Iterator iter = temp.keySet().iterator();
+            while (iter.hasNext())
             {
-                predicted = new HashMap();
-                Properties temp = new Properties();
-                temp.load(in);
+                String title = (String) iter.next();
+                String timestr = temp.getProperty(title);
 
-                Iterator iter = temp.keySet().iterator();
-                while (iter.hasNext())
+                try
                 {
-                    String title = (String) iter.next();
-                    String timestr = temp.getProperty(title);
+                    Integer time = new Integer(timestr);
+                    predicted.put(title, time);
 
-                    try
+                    // if this time is later than the latest
+                    int age = time.intValue();
+                    if (age > predictedLength)
                     {
-                        Integer time = new Integer(timestr);
-                        predicted.put(title, time);
-
-                        // if this time is later than the latest
-                        int age = time.intValue();
-                        if (age > predictedLength)
-                        {
-                            predictedLength = age;
-                        }
+                        predictedLength = age;
                     }
-                    catch (NumberFormatException ex)
-                    {
-                        log.error("Time format error", ex); //$NON-NLS-1$
-                    }
                 }
+                catch (NumberFormatException ex)
+                {
+                    log.error("Time format error", ex); //$NON-NLS-1$
+                }
             }
         }
         catch (IOException ex)
         {
             log.debug("Failed to load prediction times - guessing"); //$NON-NLS-1$
         }
-        finally
-        {
-            IOUtil.close(in);
-        }
     }
 
     /**
@@ -533,8 +520,7 @@
         // And save. It's not a disaster if this goes wrong
         try
         {
-            OutputStream out = NetUtil.getOutputStream(predictURI);
-            predictions.store(out, "Predicted Startup Times"); //$NON-NLS-1$
+            NetUtil.storeProperties(predictions, predictURI, "Predicted Startup Times"); //$NON-NLS-1$
         }
         catch (IOException ex)
         {

Modified: trunk/common/src/main/java/org/crosswire/common/util/CollectionUtil.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/util/CollectionUtil.java	2007-07-03 13:00:31 UTC (rev 1469)
+++ trunk/common/src/main/java/org/crosswire/common/util/CollectionUtil.java	2007-07-03 15:18:54 UTC (rev 1470)
@@ -22,7 +22,6 @@
 package org.crosswire.common.util;
 
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -83,6 +82,12 @@
         return reply;
     }
 
+    /**
+     * Convert a <code>Properties</code> into a <code>Map</code>.
+     * 
+     * @param prop The Properties to convert
+     * @return The map
+     */
     public static Map properties2Map(Properties prop)
     {
         Map propMap = new HashMap();
@@ -95,21 +100,15 @@
         return propMap;
     }
 
+    /**
+     * Convert a <code>Properties</code> located at <code>propURI</code> into a <code>Map</code>.
+     * 
+     * @param propUri The URI of the Properties to convert
+     * @return The map
+     */
     public static Map properties2Map(URI propUri) throws IOException
     {
-        InputStream in = null;
-        try
-        {
-            in = NetUtil.getInputStream(propUri);
-            Properties prop = new Properties();
-            prop.load(in);
-            in.close();
-            return properties2Map(prop);
-        }
-        finally
-        {
-            IOUtil.close(in);
-        }
+        return properties2Map(NetUtil.loadProperties(propUri));
     }
 
 }

Modified: trunk/common/src/main/java/org/crosswire/common/util/NetUtil.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/util/NetUtil.java	2007-07-03 13:00:31 UTC (rev 1469)
+++ trunk/common/src/main/java/org/crosswire/common/util/NetUtil.java	2007-07-03 15:18:54 UTC (rev 1470)
@@ -40,6 +40,7 @@
 import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
+import java.util.Properties;
 import java.util.jar.JarEntry;
 
 /**
@@ -596,6 +597,53 @@
     }
 
     /**
+     * Load up properties given by a URI.
+     * 
+     * @param uri the location of the properties
+     * @return the properties given by the URI
+     * @throws IOException
+     */
+    public static Properties loadProperties(URI uri) throws IOException
+    {
+        InputStream is = null;
+        try
+        {
+            is = NetUtil.getInputStream(uri);
+            Properties prop = new Properties();
+            prop.load(is);
+            is.close();
+            return prop;
+        }
+        finally
+        {
+            IOUtil.close(is);
+        }
+    }
+
+    /**
+     * Store the properties at the location given by the uri using the supplied title.
+     * 
+     * @param properties the properties to store
+     * @param uri the location of the store
+     * @param title the label held in the properties file
+     * @throws IOException
+     */
+    public static void storeProperties(Properties properties, URI uri, String title) throws IOException
+    {
+        OutputStream out = null;
+
+        try
+        {
+            out = NetUtil.getOutputStream(uri);
+            properties.store(out, title);
+        }
+        finally
+        {
+            IOUtil.close(out);
+        }
+    }
+
+    /**
      * @param uri the resource whose size is wanted
      * @return the size of that resource
      */

Modified: trunk/jsword/src/main/java/org/crosswire/jsword/book/install/InstallManager.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/book/install/InstallManager.java	2007-07-03 13:00:31 UTC (rev 1469)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/book/install/InstallManager.java	2007-07-03 15:18:54 UTC (rev 1470)
@@ -22,7 +22,6 @@
 package org.crosswire.jsword.book.install;
 
 import java.io.IOException;
-import java.io.OutputStream;
 import java.net.URI;
 import java.util.Collections;
 import java.util.Iterator;
@@ -124,8 +123,7 @@
         URI outputURI = Project.instance().getWritablePropertiesURI(getClass().getName());
         try
         {
-            OutputStream out = NetUtil.getOutputStream(outputURI);
-            props.store(out, "Saved Installer Sites"); //$NON-NLS-1$
+            NetUtil.storeProperties(props, outputURI, "Saved Installer Sites"); //$NON-NLS-1$
         }
         catch (IOException e)
         {

Modified: trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/jdbc/JDBCBookDriver.java
===================================================================
--- trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/jdbc/JDBCBookDriver.java	2007-07-03 13:00:31 UTC (rev 1469)
+++ trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/jdbc/JDBCBookDriver.java	2007-07-03 15:18:54 UTC (rev 1470)
@@ -22,14 +22,12 @@
 package org.crosswire.jsword.book.jdbc;
 
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
 
-import org.crosswire.common.util.IOUtil;
 import org.crosswire.common.util.Logger;
 import org.crosswire.common.util.NetUtil;
 import org.crosswire.jsword.book.Book;
@@ -87,12 +85,9 @@
         {
             URI url = NetUtil.lengthenURI(dir, names[i]);
             URI propUri = NetUtil.lengthenURI(url, "bible.properties"); //$NON-NLS-1$
-            InputStream is = null;
             try
             {
-                is = NetUtil.getInputStream(propUri);
-                Properties prop = new Properties();
-                prop.load(is);
+                Properties prop = NetUtil.loadProperties(propUri);
 
                 Book book = new JDBCBook(this, prop);
 
@@ -106,10 +101,6 @@
             {
                 continue;
             }
-            finally
-            {
-                IOUtil.close(is);
-            }
         }
 
         return (Book[]) books.toArray(new Book[books.size()]);

Modified: trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/raw/RawBookDriver.java
===================================================================
--- trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/raw/RawBookDriver.java	2007-07-03 13:00:31 UTC (rev 1469)
+++ trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/raw/RawBookDriver.java	2007-07-03 15:18:54 UTC (rev 1470)
@@ -22,14 +22,12 @@
 package org.crosswire.jsword.book.raw;
 
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
 
-import org.crosswire.common.util.IOUtil;
 import org.crosswire.common.util.Logger;
 import org.crosswire.common.util.NetUtil;
 import org.crosswire.jsword.book.Book;
@@ -84,15 +82,12 @@
 
         for (int i = 0; i < names.length; i++)
         {
-            InputStream is = null;
             try
             {
                 URI uri = NetUtil.lengthenURI(dir, names[i]);
                 URI propURI = NetUtil.lengthenURI(uri, RawConstants.FILE_BIBLE_PROPERTIES);
 
-                is = NetUtil.getInputStream(propURI);
-                Properties prop = new Properties();
-                prop.load(NetUtil.getInputStream(propURI));
+                Properties prop = NetUtil.loadProperties(propURI);
 
                 Book book = new RawBook(this, prop, uri);
 
@@ -102,10 +97,6 @@
             {
                 continue;
             }
-            finally
-            {
-                IOUtil.close(is);
-            }
         }
 
         return (Book[]) books.toArray(new Book[books.size()]);




More information about the jsword-svn mailing list