Emit warning and abort insert if the selected node is not connected.
[jSite2.git] / src / net / pterodactylus / jsite / gui / SwingInterface.java
index b32694c..3df6402 100644 (file)
 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;
 import java.io.IOException;
+import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
@@ -38,8 +41,8 @@ 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.DefaultComboBoxModel;
+import javax.swing.JComboBox;
 import javax.swing.JOptionPane;
 import javax.swing.UIManager;
 import javax.swing.UnsupportedLookAndFeelException;
@@ -48,10 +51,9 @@ import net.pterodactylus.jsite.core.Core;
 import net.pterodactylus.jsite.core.CoreListener;
 import net.pterodactylus.jsite.core.JSiteException;
 import net.pterodactylus.jsite.core.Node;
-import net.pterodactylus.jsite.core.Request;
+import net.pterodactylus.jsite.core.Project;
 import net.pterodactylus.jsite.i18n.I18n;
 import net.pterodactylus.jsite.i18n.gui.I18nAction;
-import net.pterodactylus.jsite.project.Project;
 import net.pterodactylus.util.image.IconLoader;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.logging.Logging;
@@ -59,11 +61,10 @@ 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 {
+public class SwingInterface implements CoreListener, LoggingListener, PropertyChangeListener {
 
        /** The logger. */
        private static final Logger logger = Logging.getLogger(SwingInterface.class.getName());
@@ -92,35 +93,20 @@ public class SwingInterface implements CoreListener, LoggingListener {
        /** The “quit” action. */
        private I18nAction quitAction;
 
-       /** 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;
+       /** The “add node” action. */
+       private I18nAction addNodeAction;
 
        /** 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>());
+       private Map<Node, I18nAction> nodeDisconnectActions = Collections.synchronizedMap(new HashMap<Node, I18nAction>());
 
-       /** Mapping from node disconnect actions to nodes. */
-       private Map<Action, Node> nodeDisconnectActionNodes = Collections.synchronizedMap(new HashMap<Action, Node>());
+       /** All node edit actions. */
+       private Map<Node, I18nAction> nodeEditActions = Collections.synchronizedMap(new HashMap<Node, I18nAction>());
 
-       /** The node manager dialog. */
-       private ManageNodesDialog manageNodesDialog;
+       /** 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>();
@@ -131,11 +117,14 @@ public class SwingInterface implements CoreListener, LoggingListener {
        /** The “add project” action. */
        private I18nAction addProjectAction;
 
-       /** The “clone project” action. */
-       private I18nAction cloneProjectAction;
+       /** The “insert project” actions. */
+       private Map<Project, I18nAction> insertProjectActions = new HashMap<Project, I18nAction>();
+
+       /** 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;
@@ -143,9 +132,15 @@ public class SwingInterface implements CoreListener, LoggingListener {
        /** The configuration dialog. */
        private ConfigurationDialog configurationDialog;
 
+       /** The node editor dialog. */
+       private AddNodeDialog addNodeDialog;
+
        /** 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
        //
@@ -179,7 +174,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
 
        /**
         * Creates a new swing interface.
-        * 
+        *
         * @param core
         *            The core to operate on
         * @param configDirectory
@@ -229,7 +224,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
 
        /**
         * Returns the core that is controlled by the Swing interface.
-        * 
+        *
         * @return The core
         */
        Core getCore() {
@@ -238,7 +233,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
 
        /**
         * Returns the main window of the Swing interface.
-        * 
+        *
         * @return The main window
         */
        MainWindow getMainWindow() {
@@ -247,7 +242,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
 
        /**
         * Returns whether the advanced mode is activated.
-        * 
+        *
         * @return <code>true</code> if the advanced mode is activated,
         *         <code>false</code> if the simple mode is activated
         */
@@ -257,7 +252,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
 
        /**
         * Returns the “configure” action.
-        * 
+        *
         * @return The “configure” action
         */
        I18nAction getConfigureAction() {
@@ -266,7 +261,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
 
        /**
         * Returns the “import config” action.
-        * 
+        *
         * @return The “import config” action
         */
        I18nAction getImportConfigAction() {
@@ -275,7 +270,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
 
        /**
         * Returns the “quit” action.
-        * 
+        *
         * @return The “quit” action
         */
        I18nAction getQuitAction() {
@@ -283,53 +278,61 @@ public class SwingInterface implements CoreListener, LoggingListener {
        }
 
        /**
-        * Returns the “manage nodes” action.
-        * 
-        * @return The “manage nodes” action
+        * Returns the “add node” action.
+        *
+        * @return The “add node” action
         */
-       I18nAction getManageNodesAction() {
-               return manageNodesAction;
+       I18nAction getAddNodeAction() {
+               return addNodeAction;
        }
 
        /**
-        * 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
+        * 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
         */
-       List<Action> getNodeConnectActions() {
-               return nodeConnectActions;
+       I18nAction getNodeDisconnectAction(Node node) {
+               return nodeDisconnectActions.get(node);
        }
 
        /**
-        * Returns the “disconnect from node” action.
-        * 
-        * @return The “disconnect from node” action
+        * Returns the “edit node” action for the given node.
+        *
+        * @param node
+        *            The node to edit
+        * @return The “edit node” action
         */
-       I18nAction getNodeDisconnectAction() {
-               return nodeDisconnectAction;
+       I18nAction getNodeEditAction(Node node) {
+               return nodeEditActions.get(node);
        }
 
        /**
-        * Returns all “disconnect node” actions.
-        * 
-        * @return All “disconnect node” action
+        * Returns the “delete node” action for the given node.
+        *
+        * @param node
+        *            The node to delete
+        * @return The “delete node” action
         */
-       List<Action> getNodeDisconnectActions() {
-               return nodeDisconnectActions;
+       I18nAction getNodeDeleteAction(Node node) {
+               return nodeDeleteActions.get(node);
        }
 
        /**
         * Returns all language actions.
-        * 
+        *
         * @return All language actions
         */
        List<I18nAction> getLanguageActions() {
@@ -338,7 +341,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
 
        /**
         * Returns the “about” action.
-        * 
+        *
         * @return The “about” action
         */
        I18nAction getHelpAboutAction() {
@@ -347,7 +350,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
 
        /**
         * Returns the “add project” action.
-        * 
+        *
         * @return The “add project” action
         */
        I18nAction getAddProjectAction() {
@@ -355,21 +358,63 @@ public class SwingInterface implements CoreListener, LoggingListener {
        }
 
        /**
-        * Returns the “clone project” action.
-        * 
+        * Returns the “insert project” action for the given project.
+        *
+        * @param project
+        *            The project to get the “insert project” action for
+        * @return The “insert project” action
+        */
+       I18nAction getInsertProjectAction(Project project) {
+               return insertProjectActions.get(project);
+       }
+
+       /**
+        * 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);
+       }
+
+       /**
+        * Returns all currently configured nodes.
+        *
+        * @return All configured nodes
+        */
+       List<Node> getNodes() {
+               return nodeList;
+       }
+
+       /**
+        * 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
+        */
+       Executor getThreadPool() {
+               return threadPool;
        }
 
        //
@@ -516,46 +561,10 @@ public class SwingInterface implements CoreListener, LoggingListener {
                                quit();
                        }
                };
-               manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
-
-                       /**
-                        * {@inheritDoc}
-                        */
-                       @SuppressWarnings("synthetic-access")
-                       public void actionPerformed(ActionEvent actionEvent) {
-                               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()) {
+               for (final Locale locale : availableLanguages) {
+                       String language = locale.getLanguage();
+                       I18nAction languageAction = new I18nAction("general.language." + language, IconLoader.loadIcon("/flag-" + language + ".png")) {
 
                                @SuppressWarnings("synthetic-access")
                                public void actionPerformed(ActionEvent e) {
@@ -563,49 +572,39 @@ public class SwingInterface implements CoreListener, LoggingListener {
                                }
 
                        };
-                       if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
+                       if (I18n.getLocale().getLanguage().equals(language)) {
                                languageAction.setEnabled(false);
                        }
                        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();
                        }
                };
        }
@@ -614,9 +613,9 @@ public class SwingInterface implements CoreListener, LoggingListener {
         * Initializes all child dialogs.
         */
        private void initDialogs() {
-               manageNodesDialog = new ManageNodesDialog(this);
                aboutDialog = new AboutDialog(this);
                configurationDialog = new ConfigurationDialog(this);
+               addNodeDialog = new AddNodeDialog(mainWindow);
        }
 
        //
@@ -669,88 +668,60 @@ public class SwingInterface implements CoreListener, LoggingListener {
        }
 
        /**
-        * Rebuilds all node connect and disconnect actions.
-        * 
-        * @param nodes
-        *            The list of nodes
+        * Adds a node.
         */
-       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()) {
+       private void addNode() {
+               addNodeDialog.setNodeName(I18n.get(nodeList.isEmpty() ? "general.defaultNode.name" : "general.newNode.name"));
+               addNodeDialog.setNodeHostname("localhost");
+               addNodeDialog.setNodePort(9481);
+               addNodeDialog.setVisible(true);
+               if (!addNodeDialog.wasCancelled()) {
+                       Node newNode = new Node();
+                       newNode.setName(addNodeDialog.getNodeName());
+                       newNode.setHostname(addNodeDialog.getNodeHostname());
+                       newNode.setPort(addNodeDialog.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);
+                       }
+               }
+       }
 
-                               /**
-                                * {@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);
+       /**
+        * Edits the given node.
+        *
+        * @param node
+        *            The node to edit
+        */
+       private void editNode(Node node) {
+               addNodeDialog.setNodeName(node.getName());
+               addNodeDialog.setNodeHostname(node.getHostname());
+               addNodeDialog.setNodePort(node.getPort());
+               addNodeDialog.setVisible(true);
+               if (!addNodeDialog.wasCancelled()) {
+                       node.setName(addNodeDialog.getNodeName());
+                       node.setHostname(addNodeDialog.getNodeHostname());
+                       node.setPort(addNodeDialog.getNodePort());
                }
        }
 
        /**
-        * Pops up the “manage nodes” dialog.
+        * Deletes the given node.
+        *
+        * @param node
+        *            The node to delete
         */
-       private void manageNodes() {
-               if (advancedMode) {
-                       manageNodesDialog.setNodeList(nodeList);
-                       manageNodesDialog.setVisible(true);
-                       nodeList = manageNodesDialog.getNodeList();
-                       rebuildNodeActions(nodeList);
-                       mainWindow.refreshNodeMenuItems();
-               } 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 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
         *            The node to connect to
         */
@@ -770,7 +741,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
 
        /**
         * Disconnects from the node.
-        * 
+        *
         * @param node
         *            The node to disconnect from
         */
@@ -782,14 +753,14 @@ public class SwingInterface implements CoreListener, LoggingListener {
        /**
         * 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) {
+               for (I18nAction i18nAction : languageActions) {
                        i18nAction.setEnabled(i18nAction != languageAction);
                }
                I18n.setLocale(newLocale);
@@ -807,13 +778,9 @@ public class SwingInterface implements CoreListener, LoggingListener {
         */
        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);
-               } catch (JSiteException nne1) {
-                       /* TODO - add i18n */
-                       JOptionPane.showMessageDialog(mainWindow, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
+                       core.createProject();
+               } catch (JSiteException jse1) {
+                       JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.notConnected.message"), I18n.get("mainWindow.error.notConnected.title"), JOptionPane.ERROR_MESSAGE);
                } catch (IOException e) {
                        /* TODO - add i18n */
                        JOptionPane.showMessageDialog(mainWindow, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
@@ -821,17 +788,56 @@ public class SwingInterface implements CoreListener, LoggingListener {
        }
 
        /**
+        * Inserts the given project.
+        *
+        * @param project
+        *            The project to insert
+        */
+       private void insertProject(Project project) {
+               Node targetNode = project.getNode();
+               if (targetNode == null) {
+                       JComboBox nodeComboBox = new JComboBox();
+                       for (Node node : nodeList) {
+                               ((DefaultComboBoxModel) nodeComboBox.getModel()).addElement(node.getName() + " (" + node.getHostname() + ":" + node.getPort() + ")");
+                       }
+                       int selectedOption = JOptionPane.showConfirmDialog(mainWindow, new Object[] { I18n.get("nodeSelectionDialog.selectNode.text"), nodeComboBox }, I18n.get("nodeSelectionDialog.noNodeSelected.text"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
+                       if (selectedOption == JOptionPane.CANCEL_OPTION) {
+                               return;
+                       }
+                       int selectedNodeIndex = nodeComboBox.getSelectedIndex();
+                       logger.log(Level.FINE, "selected node index: " + selectedNodeIndex);
+                       targetNode = nodeList.get(selectedNodeIndex);
+               }
+               logger.log(Level.INFO, "Inserting project “" + project.getName() + "” to node “" + targetNode.getName() + "”...");
+               if (!core.isNodeConnected(targetNode)) {
+                       JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.nodeNotConnected.message", targetNode.getName()), I18n.get("mainWindow.error.nodeNotConnected.title", targetNode.getName()), JOptionPane.ERROR_MESSAGE);
+                       return;
+               }
+               core.insertProject(targetNode, project);
+       }
+
+       /**
         * Clones a project.
+        *
+        * @param project
+        *            The project to clone
         */
-       private void cloneProject() {
-               /* TODO */
+       private void cloneProject(Project project) {
+               core.cloneProject(project);
        }
 
        /**
         * Deletes a project.
+        *
+        * @param project
+        *            The project to delete
         */
-       private void deleteProject() {
-               /* TODO */
+       private void deleteProject(Project project) {
+               int choice = JOptionPane.showConfirmDialog(mainWindow, I18n.get("mainWindow.question.deleteProject.message", project.getName()), I18n.get("mainWindow.question.deleteProject.title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
+               if (choice == JOptionPane.NO_OPTION) {
+                       return;
+               }
+               core.removeProject(project);
        }
 
        //
@@ -843,8 +849,8 @@ public class SwingInterface implements CoreListener, LoggingListener {
         */
        public void loadingProjectsDone(String directory) {
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectLoadingDone"));
-               for (Project project: core.getProjects()) {
-                       mainWindow.addProject(project, false);
+               for (Project project : core.getProjects()) {
+                       projectAdded(project, false);
                }
        }
 
@@ -872,6 +878,70 @@ public class SwingInterface implements CoreListener, LoggingListener {
        /**
         * {@inheritDoc}
         */
+       public void projectAdded(Project project) {
+               project.setName(I18n.get("general.newProject.name"));
+               project.setDescription(I18n.get("general.newProject.description", new Date()));
+               projectAdded(project, true);
+       }
+
+       /**
+        * @param project
+        * @param switchToProject
+        */
+       private void projectAdded(final Project project, boolean switchToProject) {
+               insertProjectActions.put(project, new I18nAction("mainWindow.button.insertProject") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               insertProject(project);
+                       }
+               });
+               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 projectCloned(Project clonedProject, Project projectClone) {
+               projectAdded(projectClone, true);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void projectRemoved(Project project) {
+               mainWindow.removeProject(project);
+               cloneProjectActions.remove(project);
+               deleteProjectActions.remove(project);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
        public void loadingNodesDone(String directory) {
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.loadingNodesDone"));
        }
@@ -915,13 +985,53 @@ 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.node.item.connect") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent e) {
+                               nodeConnect(node);
+                       }
+               });
+               nodeDisconnectActions.put(node, new I18nAction("mainWindow.menu.node.item.disconnect") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent e) {
+                               nodeDisconnect(node);
+                       }
+               });
+               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);
        }
 
        /**
@@ -930,73 +1040,86 @@ 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);
+               nodeEditActions.remove(node);
+               nodeDeleteActions.remove(node);
+               mainWindow.removeNode(node);
        }
 
        /**
         * {@inheritDoc}
         */
        public void nodeConnecting(Node node) {
-               Action nodeConnectAction = nodeNodeConnectActions.get(node);
-               nodeConnectActions.remove(nodeConnectAction);
+               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()));
-               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();
+               mainWindow.setOnline(node);
        }
 
        /**
         * {@inheritDoc}
         */
        public void nodeConnectionFailed(Node node, Throwable cause) {
-               Action nodeConnectAction = nodeNodeConnectActions.get(node);
-               nodeConnectActions.add(nodeConnectAction);
+               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);
-               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);
+               nodeEditActions.get(node).setEnabled(true);
+               nodeDeleteActions.get(node).setEnabled(true);
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.disconnectedFromNode", node.getName(), node.getHostname(), node.getPort()));
-               mainWindow.refreshNodeMenuItems();
+               mainWindow.setOffline(node);
        }
 
        /**
-        * {@inheritDoc}
+        * @see net.pterodactylus.jsite.core.CoreListener#projectInsertStarted(net.pterodactylus.jsite.core.Project)
         */
-       public void requestAdded(Request request) {
-               logger.log(Level.INFO, "request added to node: " + request + ", " + request.getNode());
-               /* TODO - implement */
+       public void projectInsertStarted(Project project) {
+               mainWindow.projectInsertStarted(project);
        }
 
        /**
-        * {@inheritDoc}
+        * @see net.pterodactylus.jsite.core.CoreListener#projectInsertProgressed(net.pterodactylus.jsite.core.Project,
+        *      int, int, int, int, int, boolean)
         */
-       public void requestProgressed(Request request) {
-               /* TODO - implement */
+       public void projectInsertProgressed(Project project, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
+               mainWindow.projectInsertProgressed(project, totalBlocks, requiredBlocks, successfulBlocks, failedBlocks, fatallyFailedBlocks, finalizedTotal);
        }
 
        /**
-        * @see net.pterodactylus.jsite.core.CoreListener#requestRemoved(net.pterodactylus.jsite.core.Request)
+        * @see net.pterodactylus.jsite.core.CoreListener#projectInsertGeneratedURI(net.pterodactylus.jsite.core.Project,
+        *      java.lang.String)
         */
-       public void requestRemoved(Request request) {
-               /* TODO - implement */
+       public void projectInsertGeneratedURI(Project project, String uri) {
+               mainWindow.projectInsertGeneratedURI(project);
+       }
+
+       /**
+        * @see net.pterodactylus.jsite.core.CoreListener#projectInsertFinished(net.pterodactylus.jsite.core.Project,
+        *      boolean)
+        */
+       public void projectInsertFinished(Project project, boolean success) {
+               mainWindow.projectInsertFinished(project, success);
        }
 
        //
@@ -1010,4 +1133,15 @@ public class SwingInterface implements CoreListener, LoggingListener {
                logWindow.logged(logRecord);
        }
 
+       //
+       // INTERFACE PropertyChangeListener
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
+               /* do not react to anything (yet). */
+       }
+
 }