Make I18n class non-static.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 27 Jan 2009 23:27:51 +0000 (00:27 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 27 Jan 2009 23:27:51 +0000 (00:27 +0100)
src/net/pterodactylus/util/i18n/I18n.java
src/net/pterodactylus/util/i18n/I18nable.java
src/net/pterodactylus/util/i18n/gui/I18nAction.java
src/net/pterodactylus/util/i18n/gui/I18nLabel.java
src/net/pterodactylus/util/i18n/gui/I18nMenu.java

index e6f30b9..28d174a 100644 (file)
@@ -49,23 +49,39 @@ public class I18n {
        /** Logger. */
        private static final Logger logger = Logging.getLogger(I18n.class.getName());
 
        /** Logger. */
        private static final Logger logger = Logging.getLogger(I18n.class.getName());
 
+       /** The base name of the resource files. */
+       private String resourceName;
+
+       /** The class whose class loader is used to load resource files. */
+       private final Class<?> resourceClass;
+
        /** List of I18nables that are notified when the language changes. */
        /** List of I18nables that are notified when the language changes. */
-       private static final List<I18nable> i18nables = new ArrayList<I18nable>();
+       private final List<I18nable> i18nables = new ArrayList<I18nable>();
 
        /** The current locale. */
 
        /** The current locale. */
-       private static Locale currentLocale;
+       private Locale currentLocale;
 
        /** The default language. */
 
        /** The default language. */
-       private static Properties defaultLanguage;
+       private Properties defaultLanguage;
 
        /** The current language. */
 
        /** The current language. */
-       private static Properties currentLanguage;
+       private Properties currentLanguage;
 
 
-       static {
+       /**
+        * Creates a new I18n container.
+        *
+        * @param resourceName
+        *            The base name of the language resource files
+        * @param resourceClass
+        *            The class whose class loader is used to load resource files
+        */
+       public I18n(String resourceName, Class<?> resourceClass) {
+               this.resourceName = resourceName;
+               this.resourceClass = resourceClass;
                defaultLanguage = new Properties();
                InputStream inputStream = null;
                try {
                defaultLanguage = new Properties();
                InputStream inputStream = null;
                try {
-                       inputStream = I18n.class.getResourceAsStream("jkeytool.properties");
+                       inputStream = resourceClass.getResourceAsStream(resourceName + ".properties");
                        if (inputStream != null) {
                                defaultLanguage.load(inputStream);
                        }
                        if (inputStream != null) {
                                defaultLanguage.load(inputStream);
                        }
@@ -88,7 +104,7 @@ public class I18n {
         * @return The translated message, or the key itself if no translation could
         *         be found
         */
         * @return The translated message, or the key itself if no translation could
         *         be found
         */
-       public static String get(String key, Object... parameters) {
+       public String get(String key, Object... parameters) {
                String value = null;
                value = currentLanguage.getProperty(key);
                if (value == null) {
                String value = null;
                value = currentLanguage.getProperty(key);
                if (value == null) {
@@ -111,7 +127,7 @@ public class I18n {
         *            The key under which the keycode is stored
         * @return The keycode
         */
         *            The key under which the keycode is stored
         * @return The keycode
         */
-       public static int getKey(String key) {
+       public int getKey(String key) {
                String value = currentLanguage.getProperty(key);
                if ((value != null) && value.startsWith("VK_")) {
                        try {
                String value = currentLanguage.getProperty(key);
                if ((value != null) && value.startsWith("VK_")) {
                        try {
@@ -139,7 +155,7 @@ public class I18n {
         * @return The key stroke, or <code>null</code> if no key stroke could be
         *         created from the translated value
         */
         * @return The key stroke, or <code>null</code> if no key stroke could be
         *         created from the translated value
         */
-       public static KeyStroke getKeyStroke(String key) {
+       public KeyStroke getKeyStroke(String key) {
                String value = currentLanguage.getProperty(key);
                if (value == null) {
                        return null;
                String value = currentLanguage.getProperty(key);
                if (value == null) {
                        return null;
@@ -184,7 +200,7 @@ public class I18n {
         * @param newLocale
         *            The new locale to use
         */
         * @param newLocale
         *            The new locale to use
         */
-       public static void setLocale(Locale newLocale) {
+       public void setLocale(Locale newLocale) {
                setLocale(newLocale, true);
        }
 
                setLocale(newLocale, true);
        }
 
@@ -194,10 +210,10 @@ public class I18n {
         * @param newLocale
         *            The new locale to use
         * @param notify
         * @param newLocale
         *            The new locale to use
         * @param notify
-        *            <code>true</code> to notify registered {@link I18nable}s
-        *            after the language was changed
+        *            <code>true</code> to notify registered {@link I18nable}s after
+        *            the language was changed
         */
         */
-       private static void setLocale(Locale newLocale, boolean notify) {
+       private void setLocale(Locale newLocale, boolean notify) {
                currentLocale = newLocale;
                InputStream inputStream = null;
                try {
                currentLocale = newLocale;
                InputStream inputStream = null;
                try {
@@ -208,7 +224,7 @@ public class I18n {
                                }
                                return;
                        }
                                }
                                return;
                        }
-                       inputStream = I18n.class.getResourceAsStream("jSite_" + newLocale.getLanguage() + ".properties");
+                       inputStream = resourceClass.getResourceAsStream(resourceName + "_" + newLocale.getLanguage() + ".properties");
                        if (inputStream != null) {
                                currentLanguage.load(inputStream);
                                if (notify) {
                        if (inputStream != null) {
                                currentLanguage.load(inputStream);
                                if (notify) {
@@ -229,7 +245,7 @@ public class I18n {
         *
         * @return The current locale
         */
         *
         * @return The current locale
         */
-       public static Locale getLocale() {
+       public Locale getLocale() {
                return currentLocale;
        }
 
                return currentLocale;
        }
 
@@ -251,7 +267,7 @@ public class I18n {
         * @param i18nable
         *            The i18nable to register
         */
         * @param i18nable
         *            The i18nable to register
         */
-       public static void registerI18nable(I18nable i18nable) {
+       public void registerI18nable(I18nable i18nable) {
                i18nables.add(i18nable);
        }
 
                i18nables.add(i18nable);
        }
 
@@ -262,7 +278,7 @@ public class I18n {
         * @param i18nable
         *            The i18nable to register
         */
         * @param i18nable
         *            The i18nable to register
         */
-       public static void deregisterI18nable(I18nable i18nable) {
+       public void deregisterI18nable(I18nable i18nable) {
                i18nables.remove(i18nable);
        }
 
                i18nables.remove(i18nable);
        }
 
@@ -273,9 +289,9 @@ public class I18n {
        /**
         * Notifies all registered {@link I18nable}s that the language was changed.
         */
        /**
         * Notifies all registered {@link I18nable}s that the language was changed.
         */
-       private static void notifyI18nables() {
-               for (I18nable i18nable: i18nables) {
-                       i18nable.updateI18n();
+       private void notifyI18nables() {
+               for (I18nable i18nable : i18nables) {
+                       i18nable.updateI18n(this);
                }
        }
 
                }
        }
 
index c32fda6..59c5877 100644 (file)
@@ -21,14 +21,17 @@ package net.pterodactylus.util.i18n;
 
 /**
  * Interface for objects that want to be notified when the language is changed.
 
 /**
  * Interface for objects that want to be notified when the language is changed.
- * 
+ *
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  */
 public interface I18nable {
 
        /**
         * Notifies the object that the language in {@link I18n} was changed.
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  */
 public interface I18nable {
 
        /**
         * Notifies the object that the language in {@link I18n} was changed.
+        *
+        * @param i18n
+        *            The i18n container that is updated
         */
         */
-       public void updateI18n();
+       public void updateI18n(I18n i18n);
 
 }
 
 }
index 6078335..0682b35 100644 (file)
@@ -1,4 +1,3 @@
-
 package net.pterodactylus.util.i18n.gui;
 
 import javax.swing.AbstractAction;
 package net.pterodactylus.util.i18n.gui;
 
 import javax.swing.AbstractAction;
@@ -22,43 +21,51 @@ public abstract class I18nAction extends AbstractAction implements I18nable {
         * Creates a new action that uses the given name as base name to get values
         * from {@link I18n}.
         *
         * Creates a new action that uses the given name as base name to get values
         * from {@link I18n}.
         *
+        * @param i18n
+        *            The I18n container
         * @param i18nName
         *            The base name of the action
         */
         * @param i18nName
         *            The base name of the action
         */
-       public I18nAction(String i18nName) {
-               this(i18nName, null);
+       public I18nAction(I18n i18n, String i18nName) {
+               this(i18n, i18nName, null);
        }
 
        /**
         * Creates a new action that uses the given name as base name to get values
         * from {@link I18n} and the given icon.
         *
        }
 
        /**
         * Creates a new action that uses the given name as base name to get values
         * from {@link I18n} and the given icon.
         *
+        * @param i18n
+        *            The I18n container
         * @param i18nName
         *            The base name of the action
         * @param icon
         *            The icon for the action
         */
         * @param i18nName
         *            The base name of the action
         * @param icon
         *            The icon for the action
         */
-       public I18nAction(String i18nName, Icon icon) {
-               this(i18nName, true, icon);
+       public I18nAction(I18n i18n, String i18nName, Icon icon) {
+               this(i18n, i18nName, true, icon);
        }
 
        /**
         * Creates a new action that uses the given name as base name to get values
         * from {@link I18n}.
         *
        }
 
        /**
         * Creates a new action that uses the given name as base name to get values
         * from {@link I18n}.
         *
+        * @param i18n
+        *            The I18n container
         * @param i18nName
         *            The base name of the action
         * @param enabled
         *            Whether the action should be enabled
         */
         * @param i18nName
         *            The base name of the action
         * @param enabled
         *            Whether the action should be enabled
         */
-       public I18nAction(String i18nName, boolean enabled) {
-               this(i18nName, enabled, null);
+       public I18nAction(I18n i18n, String i18nName, boolean enabled) {
+               this(i18n, i18nName, enabled, null);
        }
 
        /**
         * Creates a new action that uses the given name as base name to get values
         * from {@link I18n} and the given icon.
         *
        }
 
        /**
         * Creates a new action that uses the given name as base name to get values
         * from {@link I18n} and the given icon.
         *
+        * @param i18n
+        *            The I18n container
         * @param i18nName
         *            The base name of the action
         * @param enabled
         * @param i18nName
         *            The base name of the action
         * @param enabled
@@ -66,24 +73,24 @@ public abstract class I18nAction extends AbstractAction implements I18nable {
         * @param icon
         *            The icon for the action
         */
         * @param icon
         *            The icon for the action
         */
-       public I18nAction(String i18nName, boolean enabled, Icon icon) {
+       public I18nAction(I18n i18n, String i18nName, boolean enabled, Icon icon) {
                this.i18nName = i18nName;
                if (icon != null) {
                        putValue(Action.SMALL_ICON, icon);
                }
                setEnabled(enabled);
                this.i18nName = i18nName;
                if (icon != null) {
                        putValue(Action.SMALL_ICON, icon);
                }
                setEnabled(enabled);
-               updateI18n();
+               updateI18n(i18n);
        }
 
        /**
         * {@inheritDoc}
         */
        }
 
        /**
         * {@inheritDoc}
         */
-       public void updateI18n() {
-               putValue(Action.NAME, I18n.get(i18nName + ".name"));
-               putValue(Action.MNEMONIC_KEY, I18n.getKey(i18nName + ".mnemonic"));
-               putValue(Action.ACCELERATOR_KEY, I18n.getKeyStroke(i18nName + ".accelerator"));
-               putValue(Action.SHORT_DESCRIPTION, I18n.get(i18nName + ".shortDescription"));
-               putValue(Action.LONG_DESCRIPTION, I18n.get(i18nName + ".longDescription"));
+       public void updateI18n(I18n i18n) {
+               putValue(Action.NAME, i18n.get(i18nName + ".name"));
+               putValue(Action.MNEMONIC_KEY, i18n.getKey(i18nName + ".mnemonic"));
+               putValue(Action.ACCELERATOR_KEY, i18n.getKeyStroke(i18nName + ".accelerator"));
+               putValue(Action.SHORT_DESCRIPTION, i18n.get(i18nName + ".shortDescription"));
+               putValue(Action.LONG_DESCRIPTION, i18n.get(i18nName + ".longDescription"));
        }
 
        }
 
-}
\ No newline at end of file
+}
index 32ad57d..a42e626 100644 (file)
@@ -28,7 +28,7 @@ import net.pterodactylus.util.i18n.I18nable;
 
 /**
  * Label that can update itself from {@link I18n}.
 
 /**
  * Label that can update itself from {@link I18n}.
- * 
+ *
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  */
 public class I18nLabel extends JLabel implements I18nable {
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  */
 public class I18nLabel extends JLabel implements I18nable {
@@ -41,18 +41,22 @@ public class I18nLabel extends JLabel implements I18nable {
 
        /**
         * Creates a new label with the given I18n basename.
 
        /**
         * Creates a new label with the given I18n basename.
-        * 
+        *
+        * @param i18n
+        *            The I18n container
         * @param i18nBasename
         *            The I18n basename of the label
         */
         * @param i18nBasename
         *            The I18n basename of the label
         */
-       public I18nLabel(String i18nBasename) {
-               this(i18nBasename, (Component) null);
+       public I18nLabel(I18n i18n, String i18nBasename) {
+               this(i18n, i18nBasename, (Component) null);
        }
 
        /**
         * Creates a new label with the given I18n basename that optionally is a
         * label for the given component.
        }
 
        /**
         * Creates a new label with the given I18n basename that optionally is a
         * label for the given component.
-        * 
+        *
+        * @param i18n
+        *            The I18n container
         * @param i18nBasename
         *            The I18n basename of the label
         * @param component
         * @param i18nBasename
         *            The I18n basename of the label
         * @param component
@@ -60,28 +64,32 @@ public class I18nLabel extends JLabel implements I18nable {
         *            <code>null</code> if this label should not activate a
         *            component
         */
         *            <code>null</code> if this label should not activate a
         *            component
         */
-       public I18nLabel(String i18nBasename, Component component) {
-               this(i18nBasename, component, (Object[]) null);
+       public I18nLabel(I18n i18n, String i18nBasename, Component component) {
+               this(i18n, i18nBasename, component, (Object[]) null);
        }
 
        /**
         * Creates a new label with the given I18n basename that optionally is a
         * label for the given component.
        }
 
        /**
         * Creates a new label with the given I18n basename that optionally is a
         * label for the given component.
-        * 
+        *
+        * @param i18n
+        *            The I18n container
         * @param i18nBasename
         *            The I18n basename of the label
         * @param arguments
         *            Optional arguments that are handed in to
         *            {@link I18n#get(String, Object...)}
         */
         * @param i18nBasename
         *            The I18n basename of the label
         * @param arguments
         *            Optional arguments that are handed in to
         *            {@link I18n#get(String, Object...)}
         */
-       public I18nLabel(String i18nBasename, Object... arguments) {
-               this(i18nBasename, null, arguments);
+       public I18nLabel(I18n i18n, String i18nBasename, Object... arguments) {
+               this(i18n, i18nBasename, null, arguments);
        }
 
        /**
         * Creates a new label with the given I18n basename that optionally is a
         * label for the given component.
        }
 
        /**
         * Creates a new label with the given I18n basename that optionally is a
         * label for the given component.
-        * 
+        *
+        * @param i18n
+        *            The I18n container
         * @param i18nBasename
         *            The I18n basename of the label
         * @param component
         * @param i18nBasename
         *            The I18n basename of the label
         * @param component
@@ -92,23 +100,23 @@ public class I18nLabel extends JLabel implements I18nable {
         *            Optional arguments that are handed in to
         *            {@link I18n#get(String, Object...)}
         */
         *            Optional arguments that are handed in to
         *            {@link I18n#get(String, Object...)}
         */
-       public I18nLabel(String i18nBasename, Component component, Object... arguments) {
+       public I18nLabel(I18n i18n, String i18nBasename, Component component, Object... arguments) {
                super();
                this.i18nBasename = i18nBasename;
                this.arguments = arguments;
                if (component != null) {
                        setLabelFor(component);
                }
                super();
                this.i18nBasename = i18nBasename;
                this.arguments = arguments;
                if (component != null) {
                        setLabelFor(component);
                }
-               updateI18n();
+               updateI18n(i18n);
        }
 
        /**
         * {@inheritDoc}
         */
        }
 
        /**
         * {@inheritDoc}
         */
-       public void updateI18n() {
-               setText(I18n.get(i18nBasename + ".name", arguments));
+       public void updateI18n(I18n i18n) {
+               setText(i18n.get(i18nBasename + ".name", arguments));
                if (getLabelFor() != null) {
                if (getLabelFor() != null) {
-                       setDisplayedMnemonic(I18n.getKey(i18nBasename + ".mnemonic"));
+                       setDisplayedMnemonic(i18n.getKey(i18nBasename + ".mnemonic"));
                }
        }
 
                }
        }
 
index 993be97..e47ec1c 100644 (file)
@@ -26,7 +26,7 @@ import net.pterodactylus.util.i18n.I18nable;
 
 /**
  * Menu that receives its properties from {@link I18n}.
 
 /**
  * Menu that receives its properties from {@link I18n}.
- * 
+ *
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  */
 public class I18nMenu extends JMenu implements I18nable {
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  */
 public class I18nMenu extends JMenu implements I18nable {
@@ -36,13 +36,15 @@ public class I18nMenu extends JMenu implements I18nable {
 
        /**
         * Creates a new menu with the given {@link I18n} basename.
 
        /**
         * Creates a new menu with the given {@link I18n} basename.
-        * 
+        *
+        * @param i18n
+        *            The I18n container
         * @param i18nBasename
         *            The basename of the {@link I18n} properties
         */
         * @param i18nBasename
         *            The basename of the {@link I18n} properties
         */
-       public I18nMenu(String i18nBasename) {
+       public I18nMenu(I18n i18n, String i18nBasename) {
                this.i18nBasename = i18nBasename;
                this.i18nBasename = i18nBasename;
-               updateI18n();
+               updateI18n(i18n);
        }
 
        //
        }
 
        //
@@ -52,9 +54,9 @@ public class I18nMenu extends JMenu implements I18nable {
        /**
         * {@inheritDoc}
         */
        /**
         * {@inheritDoc}
         */
-       public void updateI18n() {
-               setText(I18n.get(i18nBasename + ".name"));
-               setMnemonic(I18n.getKey(i18nBasename + ".mnemonic"));
+       public void updateI18n(I18n i18n) {
+               setText(i18n.get(i18nBasename + ".name"));
+               setMnemonic(i18n.getKey(i18nBasename + ".mnemonic"));
        }
 
 }
        }
 
 }