rework node connection menus
[jSite2.git] / src / net / pterodactylus / jsite / gui / SwingInterface.java
index 4fb7139..fad301d 100644 (file)
@@ -20,6 +20,8 @@
 package net.pterodactylus.jsite.gui;
 
 import java.awt.event.ActionEvent;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -38,7 +40,6 @@ import java.util.logging.Level;
 import java.util.logging.LogRecord;
 import java.util.logging.Logger;
 
-import javax.swing.AbstractAction;
 import javax.swing.Action;
 import javax.swing.JOptionPane;
 import javax.swing.UIManager;
@@ -62,7 +63,7 @@ import net.pterodactylus.util.logging.LoggingListener;
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  * @version $Id$
  */
-public class SwingInterface implements CoreListener, LoggingListener {
+public class SwingInterface implements CoreListener, LoggingListener, PropertyChangeListener {
 
        /** The logger. */
        private static final Logger logger = Logging.getLogger(SwingInterface.class.getName());
@@ -94,29 +95,11 @@ public class SwingInterface implements CoreListener, LoggingListener {
        /** The “manage nodes” action. */
        private I18nAction manageNodesAction;
 
-       /** The “connect to node” (simple mode) action. */
-       private I18nAction nodeConnectAction;
-
-       /** The “disconnect from node” (simple mode) action. */
-       private I18nAction nodeDisconnectAction;
-
        /** All node menu items. */
-       private List<Action> nodeConnectActions = Collections.synchronizedList(new ArrayList<Action>());
-
-       /** Mapping from nodes to node connect actions. */
-       private Map<Node, Action> nodeNodeConnectActions = Collections.synchronizedMap(new HashMap<Node, Action>());
-
-       /** Mapping from node connect actions to nodes. */
-       private Map<Action, Node> nodeConnectActionNodes = Collections.synchronizedMap(new HashMap<Action, Node>());
+       private Map<Node, I18nAction> nodeConnectActions = Collections.synchronizedMap(new HashMap<Node, I18nAction>());
 
        /** All node disconnect actions. */
-       private List<Action> nodeDisconnectActions = Collections.synchronizedList(new ArrayList<Action>());
-
-       /** Mapping from nodes to node disconnect actions. */
-       private Map<Node, Action> nodeNodeDisconnectActions = Collections.synchronizedMap(new HashMap<Node, Action>());
-
-       /** Mapping from node disconnect actions to nodes. */
-       private Map<Action, Node> nodeDisconnectActionNodes = Collections.synchronizedMap(new HashMap<Action, Node>());
+       private Map<Node, I18nAction> nodeDisconnectActions = Collections.synchronizedMap(new HashMap<Node, I18nAction>());
 
        /** The node manager dialog. */
        private ManageNodesDialog manageNodesDialog;
@@ -291,39 +274,25 @@ public class SwingInterface implements CoreListener, LoggingListener {
        }
 
        /**
-        * Returns the “connect to node” action.
+        * Returns the “connect to node” action for the given node.
         * 
+        * @param node
+        *            The node go get the “connect” action for
         * @return The “connect to node” action
         */
-       I18nAction getNodeConnectAction() {
-               return nodeConnectAction;
+       I18nAction getNodeConnectAction(Node node) {
+               return nodeConnectActions.get(node);
        }
 
        /**
-        * Returns all “connect node” actions.
-        * 
-        * @return All “connect node” actions
-        */
-       List<Action> getNodeConnectActions() {
-               return nodeConnectActions;
-       }
-
-       /**
-        * Returns the “disconnect from node” action.
+        * Returns the “disconnect from node” action for the given node.
         * 
+        * @param node
+        *            The node go get the “disconnect” action for
         * @return The “disconnect from node” action
         */
-       I18nAction getNodeDisconnectAction() {
-               return nodeDisconnectAction;
-       }
-
-       /**
-        * Returns all “disconnect node” actions.
-        * 
-        * @return All “disconnect node” action
-        */
-       List<Action> getNodeDisconnectActions() {
-               return nodeDisconnectActions;
+       I18nAction getNodeDisconnectAction(Node node) {
+               return nodeDisconnectActions.get(node);
        }
 
        /**
@@ -371,6 +340,15 @@ public class SwingInterface implements CoreListener, LoggingListener {
                return deleteProjectAction;
        }
 
+       /**
+        * Returns all currently configured nodes.
+        * 
+        * @return All configured nodes
+        */
+       List<Node> getNodes() {
+               return nodeList;
+       }
+
        //
        // ACTIONS
        //
@@ -525,33 +503,6 @@ public class SwingInterface implements CoreListener, LoggingListener {
                                manageNodes();
                        }
                };
-               nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
-
-                       @SuppressWarnings("synthetic-access")
-                       public void actionPerformed(ActionEvent actionEvent) {
-                               List<Node> nodes = core.getNodes();
-                               if (nodes.isEmpty()) {
-                                       return;
-                               }
-                               nodeConnect(nodes.get(0));
-                       }
-
-               };
-               nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
-
-                       /**
-                        * {@inheritDoc}
-                        */
-                       @SuppressWarnings("synthetic-access")
-                       public void actionPerformed(ActionEvent e) {
-                               List<Node> nodes = core.getNodes();
-                               if (nodes.isEmpty()) {
-                                       return;
-                               }
-                               nodeDisconnect(nodes.get(0));
-                       }
-               };
-               rebuildNodeActions(core.getNodes());
                List<Locale> availableLanguages = I18n.findAvailableLanguages();
                for (final Locale locale: availableLanguages) {
                        I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
@@ -668,53 +619,6 @@ public class SwingInterface implements CoreListener, LoggingListener {
        }
 
        /**
-        * Rebuilds all node connect and disconnect actions.
-        * 
-        * @param nodes
-        *            The list of nodes
-        */
-       private void rebuildNodeActions(List<Node> nodes) {
-               logger.fine("rebuilding node actions…");
-               nodeConnectActions.clear();
-               nodeNodeConnectActions.clear();
-               nodeConnectActionNodes.clear();
-               nodeDisconnectActions.clear();
-               nodeNodeDisconnectActions.clear();
-               nodeDisconnectActionNodes.clear();
-               for (Node node: nodes) {
-                       logger.finer("adding node “" + node + "” to menus");
-                       Action nodeConnectAction = new AbstractAction(node.getName()) {
-
-                               /**
-                                * {@inheritDoc}
-                                */
-                               @SuppressWarnings("synthetic-access")
-                               public void actionPerformed(ActionEvent e) {
-                                       Node node = nodeConnectActionNodes.get(this);
-                                       nodeConnect(node);
-                               }
-                       };
-                       nodeConnectActions.add(nodeConnectAction);
-                       nodeConnectActionNodes.put(nodeConnectAction, node);
-                       nodeNodeConnectActions.put(node, nodeConnectAction);
-                       Action nodeDisconnectAction = new AbstractAction(node.getName()) {
-
-                               /**
-                                * {@inheritDoc}
-                                */
-                               @SuppressWarnings("synthetic-access")
-                               public void actionPerformed(ActionEvent e) {
-                                       Node node = nodeDisconnectActionNodes.get(this);
-                                       nodeDisconnect(node);
-                               }
-                       };
-                       // nodeDisconnectActions.add(nodeDisconnectAction);
-                       nodeDisconnectActionNodes.put(nodeDisconnectAction, node);
-                       nodeNodeDisconnectActions.put(node, nodeDisconnectAction);
-               }
-       }
-
-       /**
         * Pops up the “manage nodes” dialog.
         */
        private void manageNodes() {
@@ -722,8 +626,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
                        manageNodesDialog.setNodeList(nodeList);
                        manageNodesDialog.setVisible(true);
                        nodeList = manageNodesDialog.getNodeList();
-                       rebuildNodeActions(nodeList);
-                       mainWindow.refreshNodeMenuItems();
+                       /* TODO - notify main window of changes */
                } else {
                        if (nodeList.isEmpty()) {
                                Node newNode = new Node();
@@ -914,13 +817,34 @@ public class SwingInterface implements CoreListener, LoggingListener {
        /**
         * {@inheritDoc}
         */
-       public void nodeAdded(Node node) {
+       public void nodeAdded(final Node node) {
                logger.log(Level.INFO, "node added: " + node);
                nodeList.add(node);
+               node.addPropertyChangeListener(this);
                logger.log(Level.FINE, "nodeList.size(): " + nodeList.size());
                manageNodesDialog.setNodeList(nodeList);
-               rebuildNodeActions(nodeList);
-               mainWindow.refreshNodeMenuItems();
+               nodeConnectActions.put(node, new I18nAction("mainWindow.menu.connect") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent e) {
+                               nodeConnect(node);
+                       }
+               });
+               nodeDisconnectActions.put(node, new I18nAction("mainWindow.menu.disconnect") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent e) {
+                               nodeDisconnect(node);
+                       }
+               });
+               nodeDisconnectActions.get(node).setEnabled(false);
+               mainWindow.addNode(node);
        }
 
        /**
@@ -929,51 +853,44 @@ public class SwingInterface implements CoreListener, LoggingListener {
        public void nodeRemoved(Node node) {
                logger.log(Level.INFO, "node removed: " + node);
                nodeList.remove(node);
-               rebuildNodeActions(nodeList);
-               mainWindow.refreshNodeMenuItems();
+               node.removePropertyChangeListener(this);
+               nodeConnectActions.remove(node);
+               nodeDisconnectActions.remove(node);
+               mainWindow.removeNode(node);
        }
 
        /**
         * {@inheritDoc}
         */
        public void nodeConnecting(Node node) {
-               Action nodeConnectAction = nodeNodeConnectActions.get(node);
-               nodeConnectActions.remove(nodeConnectAction);
+               nodeConnectActions.get(node).setEnabled(false);
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectingToNode", node.getName(), node.getHostname(), node.getPort()));
-               mainWindow.refreshNodeMenuItems();
        }
 
        /**
         * {@inheritDoc}
         */
        public void nodeConnected(Node node) {
-               Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node);
-               nodeDisconnectActions.add(nodeDisconnectAction);
+               nodeDisconnectActions.get(node).setEnabled(true);
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectedToNode", node.getName(), node.getHostname(), node.getPort()));
-               mainWindow.refreshNodeMenuItems();
        }
 
        /**
         * {@inheritDoc}
         */
        public void nodeConnectionFailed(Node node, Throwable cause) {
-               Action nodeConnectAction = nodeNodeConnectActions.get(node);
-               nodeConnectActions.add(nodeConnectAction);
+               nodeConnectActions.get(node).setEnabled(true);
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectionToNodeFailed", node.getName(), node.getHostname(), node.getPort(), (cause != null) ? cause.getMessage() : "no reason given"));
                JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.nodeConnectionFailed.message", node.getName(), node.getHostname(), node.getPort(), (cause != null) ? cause.getMessage() : "no reason given"), I18n.get("mainWindow.error.nodeConnectionFailed.title"), JOptionPane.ERROR_MESSAGE);
-               mainWindow.refreshNodeMenuItems();
        }
 
        /**
         * {@inheritDoc}
         */
        public void nodeDisconnected(Node node, Throwable throwable) {
-               Action nodeConnectAction = nodeNodeConnectActions.get(node);
-               nodeConnectActions.add(nodeConnectAction);
-               Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node);
-               nodeDisconnectActions.remove(nodeDisconnectAction);
+               nodeDisconnectActions.get(node).setEnabled(false);
+               nodeConnectActions.get(node).setEnabled(true);
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.disconnectedFromNode", node.getName(), node.getHostname(), node.getPort()));
-               mainWindow.refreshNodeMenuItems();
        }
 
        /**
@@ -1018,4 +935,19 @@ public class SwingInterface implements CoreListener, LoggingListener {
                logWindow.logged(logRecord);
        }
 
+       //
+       // INTERFACE PropertyChangeListener
+       //
+       
+       /**
+        * {@inheritDoc}
+        */
+       public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
+               if (propertyChangeEvent.getSource() instanceof Node) {
+                       Node changedNode = (Node) propertyChangeEvent.getSource();
+                       nodeConnectActions.get(changedNode).putValue(Action.NAME, changedNode.getName());
+                       nodeDisconnectActions.get(changedNode).putValue(Action.NAME, changedNode.getName());
+               }
+       }
+
 }