remove debug output
[jSite2.git] / src / net / pterodactylus / jsite / gui / SwingInterface.java
index edcc0df..e55fffa 100644 (file)
 package net.pterodactylus.jsite.gui;
 
 import java.awt.event.ActionEvent;
+import java.util.ArrayList;
 import java.util.List;
-
-import javax.swing.Action;
+import java.util.Locale;
 
 import net.pterodactylus.jsite.core.Core;
 import net.pterodactylus.jsite.core.CoreListener;
 import net.pterodactylus.jsite.core.Node;
+import net.pterodactylus.jsite.i18n.I18n;
 
 /**
  * TODO
@@ -43,17 +44,20 @@ public class SwingInterface implements CoreListener {
        private MainWindow mainWindow;
 
        /** The “manage nodes” action. */
-       private Action manageNodesAction;
+       private I18nAction manageNodesAction;
 
        /** The “connect to node” action. */
-       private Action nodeConnectAction;
+       private I18nAction nodeConnectAction;
 
        /** The “disconnect from node” action. */
-       private Action nodeDisconnectAction;
+       private I18nAction nodeDisconnectAction;
 
        /** The node manager dialog. */
        private ManageNodesDialog manageNodesDialog;
 
+       /** All lanugage menu items. */
+       private List<I18nAction> languageActions = new ArrayList<I18nAction>();
+
        /** The list of all defined nodes. */
        private List<Node> nodeList;
 
@@ -65,6 +69,7 @@ public class SwingInterface implements CoreListener {
         */
        public SwingInterface(Core core) {
                this.core = core;
+               I18n.setLocale(Locale.ENGLISH); /* TODO - load config */
                initActions();
                initDialogs();
        }
@@ -96,7 +101,7 @@ public class SwingInterface implements CoreListener {
         * 
         * @return The “manage nodes” action
         */
-       Action getManageNodesAction() {
+       I18nAction getManageNodesAction() {
                return manageNodesAction;
        }
 
@@ -105,7 +110,7 @@ public class SwingInterface implements CoreListener {
         * 
         * @return The “connect to node” action
         */
-       Action getNodeConnectAction() {
+       I18nAction getNodeConnectAction() {
                return nodeConnectAction;
        }
 
@@ -114,10 +119,19 @@ public class SwingInterface implements CoreListener {
         * 
         * @return The “disconnect from node” action
         */
-       Action getNodeDisconnectAction() {
+       I18nAction getNodeDisconnectAction() {
                return nodeDisconnectAction;
        }
 
+       /**
+        * Returns all language actions.
+        * 
+        * @return All language actions
+        */
+       List<I18nAction> getLanguageActions() {
+               return languageActions;
+       }
+
        //
        // ACTIONS
        //
@@ -169,6 +183,22 @@ public class SwingInterface implements CoreListener {
                                nodeDisconnect();
                        }
                };
+               List<Locale> availableLanguages = I18n.findAvailableLanguages();
+               for (final Locale locale: availableLanguages) {
+                       I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
+
+                               @SuppressWarnings("synthetic-access")
+                               public void actionPerformed(ActionEvent e) {
+                                       changeLanguage(locale, this);
+                               }
+
+                       };
+                       if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
+                               languageAction.setEnabled(false);
+                       }
+                       languageActions.add(languageAction);
+               }
+
        }
 
        /**
@@ -178,6 +208,10 @@ public class SwingInterface implements CoreListener {
                manageNodesDialog = new ManageNodesDialog(this);
        }
 
+       //
+       // PRIVATE ACTIONS
+       //
+
        /**
         * Pops up the “manage nodes” dialog.
         */
@@ -199,6 +233,22 @@ public class SwingInterface implements CoreListener {
        private void nodeDisconnect() {
        }
 
+       /**
+        * Changes the language of the interface. This method also disables the
+        * action for the newly set language and enables all others.
+        * 
+        * @param newLocale
+        *            The new language
+        * @param languageAction
+        *            The action that triggered the change
+        */
+       private void changeLanguage(Locale newLocale, I18nAction languageAction) {
+               for (I18nAction i18nAction: languageActions) {
+                       i18nAction.setEnabled(i18nAction != languageAction);
+               }
+               I18n.setLocale(newLocale);
+       }
+
        //
        // INTERFACE CoreListener
        //