add status bar texts when connecting and disconnecting
[jSite2.git] / src / net / pterodactylus / jsite / gui / SwingInterface.java
index b2c7c92..65e1edd 100644 (file)
@@ -40,13 +40,17 @@ import java.util.logging.Logger;
 import javax.swing.AbstractAction;
 import javax.swing.Action;
 import javax.swing.JOptionPane;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
 
 import net.pterodactylus.jsite.core.Core;
 import net.pterodactylus.jsite.core.CoreListener;
 import net.pterodactylus.jsite.core.Node;
 import net.pterodactylus.jsite.core.Project;
+import net.pterodactylus.jsite.core.Request;
 import net.pterodactylus.jsite.i18n.I18n;
 import net.pterodactylus.jsite.i18n.gui.I18nAction;
+import net.pterodactylus.util.image.IconLoader;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.logging.LoggingListener;
@@ -138,7 +142,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
        private ConfigurationDialog configurationDialog;
 
        /** The list of all defined nodes. */
-       private List<Node> nodeList;
+       private List<Node> nodeList = Collections.synchronizedList(new ArrayList<Node>());
 
        //
        // CONFIGURATION
@@ -156,6 +160,9 @@ public class SwingInterface implements CoreListener, LoggingListener {
        /** The user font. */
        private String userFont;
 
+       /** The class name of the look and feel. */
+       private String lookAndFeel;
+
        /**
         * Creates a new swing interface.
         * 
@@ -169,6 +176,19 @@ public class SwingInterface implements CoreListener, LoggingListener {
                this.configDirectory = configDirectory;
                I18n.setLocale(Locale.ENGLISH);
                loadConfig();
+               if (lookAndFeel != null) {
+                       try {
+                               UIManager.setLookAndFeel(lookAndFeel);
+                       } catch (ClassNotFoundException cnfe1) {
+                               logger.log(Level.WARNING, "could not load look and feel", cnfe1);
+                       } catch (InstantiationException ie1) {
+                               logger.log(Level.WARNING, "could not load look and feel", ie1);
+                       } catch (IllegalAccessException iae1) {
+                               logger.log(Level.WARNING, "could not load look and feel", iae1);
+                       } catch (UnsupportedLookAndFeelException ulafe1) {
+                               logger.log(Level.WARNING, "could not load look and feel", ulafe1);
+                       }
+               }
                if (antialias) {
                        System.setProperty("swing.aatext", "true");
                }
@@ -380,6 +400,9 @@ public class SwingInterface implements CoreListener, LoggingListener {
                if (configProperties.containsKey("userFont")) {
                        userFont = configProperties.getProperty("userFont");
                }
+               if (configProperties.containsKey("lookAndFeel")) {
+                       lookAndFeel = configProperties.getProperty("lookAndFeel");
+               }
                if (configProperties.containsKey("language")) {
                        I18n.setLocale(new Locale(configProperties.getProperty("language")));
                }
@@ -410,6 +433,9 @@ public class SwingInterface implements CoreListener, LoggingListener {
                if (userFont != null) {
                        configProperties.setProperty("userFont", userFont);
                }
+               if (lookAndFeel != null) {
+                       configProperties.setProperty("lookAndFeel", lookAndFeel);
+               }
                configProperties.setProperty("language", I18n.getLocale().getLanguage());
                FileOutputStream configOutputStream = null;
                try {
@@ -426,7 +452,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
         * Initializes all actions.
         */
        private void initActions() {
-               configureAction = new I18nAction("mainWindow.menu.jSite.configure") {
+               configureAction = new I18nAction("mainWindow.menu.jSite.configure", IconLoader.loadIcon("/preferences-system.png")) {
 
                        /**
                         * {@inheritDoc}
@@ -446,7 +472,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
                                importConfig();
                        }
                };
-               quitAction = new I18nAction("mainWindow.menu.jSite.quit") {
+               quitAction = new I18nAction("mainWindow.menu.jSite.quit", IconLoader.loadIcon("/system-log-out.png")) {
 
                        /**
                         * {@inheritDoc}
@@ -571,6 +597,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
                configurationDialog.setAntialias(antialias);
                configurationDialog.setControlFont(controlFont);
                configurationDialog.setUserFont(userFont);
+               configurationDialog.setLookAndFeel(lookAndFeel);
                configurationDialog.setVisible(true);
                if (!configurationDialog.wasCancelled()) {
                        advancedMode = configurationDialog.isAdvancedMode();
@@ -581,6 +608,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
                        antialias = configurationDialog.isAntialias();
                        controlFont = configurationDialog.getControlFont();
                        userFont = configurationDialog.getUserFont();
+                       lookAndFeel = configurationDialog.getLookAndFeel();
                        saveConfig();
                }
        }
@@ -596,6 +624,8 @@ public class SwingInterface implements CoreListener, LoggingListener {
         * Quits jSite.
         */
        private void quit() {
+               /* TODO - ask */
+               core.stop();
                saveConfig();
                System.exit(0);
        }
@@ -607,6 +637,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
         *            The list of nodes
         */
        private void rebuildNodeActions(List<Node> nodes) {
+               logger.fine("rebuilding node actions…");
                nodeConnectActions.clear();
                nodeNodeConnectActions.clear();
                nodeConnectActionNodes.clear();
@@ -614,6 +645,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
                nodeNodeDisconnectActions.clear();
                nodeDisconnectActionNodes.clear();
                for (Node node: nodes) {
+                       logger.finer("adding node “" + node + "” to menu");
                        Action nodeConnectAction = new AbstractAction(node.getName()) {
 
                                /**
@@ -819,8 +851,6 @@ public class SwingInterface implements CoreListener, LoggingListener {
         * {@inheritDoc}
         */
        public void coreLoaded() {
-               this.nodeList = core.getNodes();
-               manageNodesDialog.setNodeList(nodeList);
                mainWindow.setVisible(true);
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreLoaded"));
        }
@@ -835,6 +865,27 @@ public class SwingInterface implements CoreListener, LoggingListener {
        /**
         * {@inheritDoc}
         */
+       public void nodeAdded(Node node) {
+               logger.log(Level.INFO, "node added: " + node);
+               nodeList.add(node);
+               manageNodesDialog.setNodeList(nodeList);
+               rebuildNodeActions(nodeList);
+               mainWindow.refreshNodeMenuItems();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void nodeRemoved(Node node) {
+               logger.log(Level.INFO, "node removed: " + node);
+               nodeList.remove(node);
+               rebuildNodeActions(nodeList);
+               mainWindow.refreshNodeMenuItems();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
        public void nodeConnecting(Node node) {
                Action nodeConnectAction = nodeNodeConnectActions.get(node);
                nodeConnectActions.remove(nodeConnectAction);
@@ -848,6 +899,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
        public void nodeConnected(Node node) {
                Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node);
                nodeDisconnectActions.add(nodeDisconnectAction);
+               mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectedToNode", node.getName(), node.getHostname(), node.getPort()));
                mainWindow.refreshNodeMenuItems();
        }
 
@@ -859,9 +911,25 @@ public class SwingInterface implements CoreListener, LoggingListener {
                nodeConnectActions.add(nodeConnectAction);
                Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node);
                nodeDisconnectActions.remove(nodeDisconnectAction);
+               mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.disconnectedFromNode", node.getName(), node.getHostname(), node.getPort()));
                mainWindow.refreshNodeMenuItems();
        }
 
+       /**
+        * {@inheritDoc}
+        */
+       public void requestAdded(Node node, Request request) {
+               logger.log(Level.INFO, "request added to node: " + request + ", " + node);
+               /* TODO - implement */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void requestProgressed(Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
+               /* TODO - update table model */
+       }
+
        //
        // INTERFACE LoggingListener
        //