make delete and clone buttons project specific
[jSite2.git] / src / net / pterodactylus / jsite / gui / SwingInterface.java
index c79c37d..2c032ea 100644 (file)
@@ -26,6 +26,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
@@ -60,7 +61,6 @@ import net.pterodactylus.util.logging.LoggingListener;
  * The Swing user interface.
  * 
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- * @version $Id$
  */
 public class SwingInterface implements CoreListener, LoggingListener, PropertyChangeListener {
 
@@ -91,8 +91,8 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        /** The “quit” action. */
        private I18nAction quitAction;
 
-       /** The “manage nodes” action. */
-       private I18nAction manageNodesAction;
+       /** The “add node” action. */
+       private I18nAction addNodeAction;
 
        /** All node menu items. */
        private Map<Node, I18nAction> nodeConnectActions = Collections.synchronizedMap(new HashMap<Node, I18nAction>());
@@ -100,8 +100,11 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        /** All node disconnect actions. */
        private Map<Node, I18nAction> nodeDisconnectActions = Collections.synchronizedMap(new HashMap<Node, I18nAction>());
 
-       /** The node manager dialog. */
-       private ManageNodesDialog manageNodesDialog;
+       /** All node edit actions. */
+       private Map<Node, I18nAction> nodeEditActions = Collections.synchronizedMap(new HashMap<Node, I18nAction>());
+
+       /** All node removal actions. */
+       private Map<Node, I18nAction> nodeDeleteActions = Collections.synchronizedMap(new HashMap<Node, I18nAction>());
 
        /** All lanugage menu items. */
        private List<I18nAction> languageActions = new ArrayList<I18nAction>();
@@ -112,11 +115,11 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        /** The “add project” action. */
        private I18nAction addProjectAction;
 
-       /** The “clone project” action. */
-       private I18nAction cloneProjectAction;
+       /** The “clone project” actions. */
+       private Map<Project, I18nAction> cloneProjectActions = new HashMap<Project, I18nAction>();
 
-       /** The “delete project” action. */
-       private I18nAction deleteProjectAction;
+       /** The “delete project” actions. */
+       private Map<Project, I18nAction> deleteProjectActions = new HashMap<Project, I18nAction>();
 
        /** The “about” dialog. */
        private AboutDialog aboutDialog;
@@ -124,9 +127,15 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        /** The configuration dialog. */
        private ConfigurationDialog configurationDialog;
 
+       /** The node editor dialog. */
+       private EditNodeDialog editNodeDialog;
+
        /** The list of all defined nodes. */
        private List<Node> nodeList = Collections.synchronizedList(new ArrayList<Node>());
 
+       /** The list of all projects. */
+       private List<Project> projectList = Collections.synchronizedList(new ArrayList<Project>());
+
        //
        // CONFIGURATION
        //
@@ -264,12 +273,12 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        }
 
        /**
-        * Returns the “manage nodes” action.
+        * Returns the “add node” action.
         * 
-        * @return The “manage nodes” action
+        * @return The “add node” action
         */
-       I18nAction getManageNodesAction() {
-               return manageNodesAction;
+       I18nAction getAddNodeAction() {
+               return addNodeAction;
        }
 
        /**
@@ -295,6 +304,28 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        }
 
        /**
+        * Returns the “edit node” action for the given node.
+        * 
+        * @param node
+        *            The node to edit
+        * @return The “edit node” action
+        */
+       I18nAction getNodeEditAction(Node node) {
+               return nodeEditActions.get(node);
+       }
+
+       /**
+        * Returns the “delete node” action for the given node.
+        * 
+        * @param node
+        *            The node to delete
+        * @return The “delete node” action
+        */
+       I18nAction getNodeDeleteAction(Node node) {
+               return nodeDeleteActions.get(node);
+       }
+
+       /**
         * Returns all language actions.
         * 
         * @return All language actions
@@ -322,21 +353,25 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        }
 
        /**
-        * Returns the “clone project” action.
+        * Returns the “clone project” action for the given project.
         * 
+        * @param project
+        *            The project to get the “clone project” action for
         * @return The “clone project” action
         */
-       I18nAction getCloneProjectAction() {
-               return cloneProjectAction;
+       I18nAction getCloneProjectAction(Project project) {
+               return cloneProjectActions.get(project);
        }
 
        /**
-        * Returns the “delete project” action.
+        * Returns the “delete project” action for the given project.
         * 
+        * @param project
+        *            The project to get the “delete project” action for
         * @return The “delete project” action
         */
-       I18nAction getDeleteProjectAction() {
-               return deleteProjectAction;
+       I18nAction getDeleteProjectAction(Project project) {
+               return deleteProjectActions.get(project);
        }
 
        /**
@@ -349,6 +384,15 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        }
 
        /**
+        * Returns a list of all projects.
+        * 
+        * @return All projects
+        */
+       List<Project> getProjects() {
+               return projectList;
+       }
+
+       /**
         * Returns the thread pool used for off-thread processes.
         * 
         * @return The thread pool
@@ -501,16 +545,6 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
                                quit();
                        }
                };
-               manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
-
-                       /**
-                        * {@inheritDoc}
-                        */
-                       @SuppressWarnings("synthetic-access")
-                       public void actionPerformed(ActionEvent actionEvent) {
-                               manageNodes();
-                       }
-               };
                List<Locale> availableLanguages = I18n.findAvailableLanguages();
                for (final Locale locale: availableLanguages) {
                        I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
@@ -526,44 +560,34 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
                        }
                        languageActions.add(languageAction);
                }
-               helpAboutAction = new I18nAction("mainWindow.menu.help.item.about") {
-
-                       /**
-                        * {@inheritDoc}
-                        */
-                       @SuppressWarnings("synthetic-access")
-                       public void actionPerformed(ActionEvent actionEvent) {
-                               helpAbout();
-                       }
-               };
-               addProjectAction = new I18nAction("mainWindow.button.addProject") {
+               addNodeAction = new I18nAction("mainWindow.menu.node.item.addNode", IconLoader.loadIcon("/node-new.png")) {
 
                        /**
                         * {@inheritDoc}
                         */
                        @SuppressWarnings("synthetic-access")
                        public void actionPerformed(ActionEvent actionEvent) {
-                               addProject();
+                               addNode();
                        }
                };
-               cloneProjectAction = new I18nAction("mainWindow.button.cloneProject") {
+               helpAboutAction = new I18nAction("mainWindow.menu.help.item.about") {
 
                        /**
                         * {@inheritDoc}
                         */
                        @SuppressWarnings("synthetic-access")
                        public void actionPerformed(ActionEvent actionEvent) {
-                               cloneProject();
+                               helpAbout();
                        }
                };
-               deleteProjectAction = new I18nAction("mainWindow.button.deleteProject") {
+               addProjectAction = new I18nAction("mainWindow.button.addProject") {
 
                        /**
                         * {@inheritDoc}
                         */
                        @SuppressWarnings("synthetic-access")
                        public void actionPerformed(ActionEvent actionEvent) {
-                               deleteProject();
+                               addProject();
                        }
                };
        }
@@ -572,9 +596,9 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
         * Initializes all child dialogs.
         */
        private void initDialogs() {
-               manageNodesDialog = new ManageNodesDialog(this);
                aboutDialog = new AboutDialog(this);
                configurationDialog = new ConfigurationDialog(this);
+               editNodeDialog = new EditNodeDialog(mainWindow);
        }
 
        //
@@ -627,38 +651,58 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        }
 
        /**
-        * Pops up the “manage nodes” dialog.
+        * Adds a node.
         */
-       private void manageNodes() {
-               if (advancedMode) {
-                       manageNodesDialog.setNodeList(nodeList);
-                       manageNodesDialog.setVisible(true);
-                       nodeList = manageNodesDialog.getNodeList();
-                       /* TODO - notify main window of changes */
-               } else {
-                       if (nodeList.isEmpty()) {
-                               Node newNode = new Node();
-                               newNode.setName(I18n.get("general.defaultNode.name"));
-                               newNode.setHostname("localhost");
-                               newNode.setPort(9481);
-                               nodeList.add(newNode);
-                       }
-                       Node firstNode = nodeList.get(0);
-                       EditNodeDialog editNodeDialog = manageNodesDialog.getEditNodeDialog();
-                       editNodeDialog.setNodeName(firstNode.getName());
-                       editNodeDialog.setNodeHostname(firstNode.getHostname());
-                       editNodeDialog.setNodePort(firstNode.getPort());
-                       editNodeDialog.setVisible(true);
-                       if (!editNodeDialog.wasCancelled()) {
-                               firstNode.setName(editNodeDialog.getNodeName());
-                               firstNode.setHostname(editNodeDialog.getNodeHostname());
-                               firstNode.setPort(editNodeDialog.getNodePort());
-                               /* TODO - give to core. */
+       private void addNode() {
+               editNodeDialog.setNodeName(I18n.get(nodeList.isEmpty() ? "general.defaultNode.name" : "general.newNode.name"));
+               editNodeDialog.setNodeHostname("localhost");
+               editNodeDialog.setNodePort(9481);
+               editNodeDialog.setVisible(true);
+               if (!editNodeDialog.wasCancelled()) {
+                       Node newNode = new Node();
+                       newNode.setName(editNodeDialog.getNodeName());
+                       newNode.setHostname(editNodeDialog.getNodeHostname());
+                       newNode.setPort(editNodeDialog.getNodePort());
+                       try {
+                               core.addNode(newNode);
+                       } catch (UnknownHostException e) {
+                               JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.hostnameUnresolvable.message"), I18n.get("mainWindow.error.hostnameUnresolvable.title"), JOptionPane.ERROR_MESSAGE);
                        }
                }
        }
 
        /**
+        * Edits the given node.
+        * 
+        * @param node
+        *            The node to edit
+        */
+       private void editNode(Node node) {
+               editNodeDialog.setNodeName(node.getName());
+               editNodeDialog.setNodeHostname(node.getHostname());
+               editNodeDialog.setNodePort(node.getPort());
+               editNodeDialog.setVisible(true);
+               if (!editNodeDialog.wasCancelled()) {
+                       node.setName(editNodeDialog.getNodeName());
+                       node.setHostname(editNodeDialog.getNodeHostname());
+                       node.setPort(editNodeDialog.getNodePort());
+               }
+       }
+
+       /**
+        * Deletes the given node.
+        * 
+        * @param node
+        *            The node to delete
+        */
+       private void deleteNode(Node node) {
+               int option = JOptionPane.showConfirmDialog(mainWindow, I18n.get("mainWindow.question.deleteNode.message", node.getName()), I18n.get("mainWindow.question.deleteNode.title"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
+               if (option == JOptionPane.OK_OPTION) {
+                       core.removeNode(node);
+               }
+       }
+
+       /**
         * Connects to the node.
         * 
         * @param node
@@ -717,10 +761,7 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
         */
        private void addProject() {
                try {
-                       Project project = core.createProject();
-                       project.setName(I18n.get("general.newProject.name"));
-                       project.setDescription(I18n.get("general.newProject.description", new Date()));
-                       mainWindow.addProject(project, true);
+                       core.createProject();
                } catch (JSiteException nne1) {
                        /* TODO - add i18n */
                        JOptionPane.showMessageDialog(mainWindow, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
@@ -732,16 +773,23 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
 
        /**
         * Clones a project.
+        * 
+        * @param project
+        *            The project to clone
         */
-       private void cloneProject() {
+       private void cloneProject(Project project) {
+               System.out.println("clone " + project);
                /* TODO */
        }
 
        /**
         * Deletes a project.
+        * 
+        * @param project
+        *            The project to delete
         */
-       private void deleteProject() {
-               /* TODO */
+       private void deleteProject(Project project) {
+               System.out.println("delete " + project);
        }
 
        //
@@ -754,7 +802,7 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        public void loadingProjectsDone(String directory) {
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectLoadingDone"));
                for (Project project: core.getProjects()) {
-                       mainWindow.addProject(project, false);
+                       projectAdded(project, false);
                }
        }
 
@@ -782,6 +830,52 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        /**
         * {@inheritDoc}
         */
+       public void projectAdded(Project project) {
+               projectAdded(project, true);
+       }
+       
+       /**
+        * 
+        * @param project
+        * @param switchToProject
+        */
+       private void projectAdded(final Project project, boolean switchToProject) {
+               project.setName(I18n.get("general.newProject.name"));
+               project.setDescription(I18n.get("general.newProject.description", new Date()));
+               cloneProjectActions.put(project, new I18nAction("mainWindow.button.cloneProject") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               cloneProject(project);
+                       }
+               });
+               deleteProjectActions.put(project, new I18nAction("mainWindow.button.deleteProject") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               deleteProject(project);
+                       }
+               });
+               projectList.add(project);
+               mainWindow.addProject(project, switchToProject);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void projectRemoved(Project project) {
+               /* TODO - implement */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
        public void loadingNodesDone(String directory) {
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.loadingNodesDone"));
        }
@@ -830,8 +924,7 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
                nodeList.add(node);
                node.addPropertyChangeListener(this);
                logger.log(Level.FINE, "nodeList.size(): " + nodeList.size());
-               manageNodesDialog.setNodeList(nodeList);
-               nodeConnectActions.put(node, new I18nAction("mainWindow.menu.connect") {
+               nodeConnectActions.put(node, new I18nAction("mainWindow.menu.node.item.connect") {
 
                        /**
                         * {@inheritDoc}
@@ -841,7 +934,7 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
                                nodeConnect(node);
                        }
                });
-               nodeDisconnectActions.put(node, new I18nAction("mainWindow.menu.disconnect") {
+               nodeDisconnectActions.put(node, new I18nAction("mainWindow.menu.node.item.disconnect") {
 
                        /**
                         * {@inheritDoc}
@@ -852,6 +945,26 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
                        }
                });
                nodeDisconnectActions.get(node).setEnabled(false);
+               nodeEditActions.put(node, new I18nAction("mainWindow.menu.node.item.edit") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               editNode(node);
+                       }
+               });
+               nodeDeleteActions.put(node, new I18nAction("mainWindow.menu.node.item.remove") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               deleteNode(node);
+                       }
+               });
                mainWindow.addNode(node);
        }
 
@@ -864,6 +977,8 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
                node.removePropertyChangeListener(this);
                nodeConnectActions.remove(node);
                nodeDisconnectActions.remove(node);
+               nodeEditActions.remove(node);
+               nodeDeleteActions.remove(node);
                mainWindow.removeNode(node);
        }
 
@@ -872,6 +987,8 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
         */
        public void nodeConnecting(Node node) {
                nodeConnectActions.get(node).setEnabled(false);
+               nodeEditActions.get(node).setEnabled(false);
+               nodeDeleteActions.get(node).setEnabled(false);
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectingToNode", node.getName(), node.getHostname(), node.getPort()));
        }
 
@@ -881,6 +998,7 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        public void nodeConnected(Node node) {
                nodeDisconnectActions.get(node).setEnabled(true);
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectedToNode", node.getName(), node.getHostname(), node.getPort()));
+               mainWindow.setOnline(node);
        }
 
        /**
@@ -888,7 +1006,10 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
         */
        public void nodeConnectionFailed(Node node, Throwable cause) {
                nodeConnectActions.get(node).setEnabled(true);
+               nodeEditActions.get(node).setEnabled(true);
+               nodeDeleteActions.get(node).setEnabled(true);
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectionToNodeFailed", node.getName(), node.getHostname(), node.getPort(), (cause != null) ? cause.getMessage() : "no reason given"));
+               mainWindow.setError(node);
                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);
        }
 
@@ -898,7 +1019,10 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        public void nodeDisconnected(Node node, Throwable throwable) {
                nodeDisconnectActions.get(node).setEnabled(false);
                nodeConnectActions.get(node).setEnabled(true);
+               nodeEditActions.get(node).setEnabled(true);
+               nodeDeleteActions.get(node).setEnabled(true);
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.disconnectedFromNode", node.getName(), node.getHostname(), node.getPort()));
+               mainWindow.setOffline(node);
        }
 
        /**