make key generation throw exceptions in case of errors
[jSite2.git] / src / net / pterodactylus / jsite / gui / SwingInterface.java
index dff6f3a..3652388 100644 (file)
@@ -45,10 +45,13 @@ import javax.swing.UnsupportedLookAndFeelException;
 
 import net.pterodactylus.jsite.core.Core;
 import net.pterodactylus.jsite.core.CoreListener;
+import net.pterodactylus.jsite.core.NoNodeException;
 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;
@@ -140,7 +143,10 @@ 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>());
+
+       /** The request table model. */
+       private RequestTableModel requestTableModel = new RequestTableModel();
 
        //
        // CONFIGURATION
@@ -161,6 +167,18 @@ public class SwingInterface implements CoreListener, LoggingListener {
        /** The class name of the look and feel. */
        private String lookAndFeel;
 
+       /** X coordinate of the main window. */
+       private int mainWindowX = -1;
+
+       /** Y coordinate of the main window. */
+       private int mainWindowY = -1;
+
+       /** Width of the main window. */
+       private int mainWindowWidth = -1;
+
+       /** Height of the main window. */
+       private int mainWindowHeight = -1;
+
        /**
         * Creates a new swing interface.
         * 
@@ -200,6 +218,10 @@ public class SwingInterface implements CoreListener, LoggingListener {
                initDialogs();
                mainWindow = new MainWindow(this);
                mainWindow.setAdvancedMode(advancedMode);
+               if ((mainWindowX != -1) && (mainWindowY != -1) && (mainWindowWidth != -1) && (mainWindowHeight != -1)) {
+                       mainWindow.setLocation(mainWindowX, mainWindowY);
+                       mainWindow.setSize(mainWindowWidth, mainWindowHeight);
+               }
                logWindow = new LogWindow();
        }
 
@@ -352,6 +374,15 @@ public class SwingInterface implements CoreListener, LoggingListener {
                return deleteProjectAction;
        }
 
+       /**
+        * Returns the request table model.
+        * 
+        * @return The request table model
+        */
+       RequestTableModel getRequestTableModel() {
+               return requestTableModel;
+       }
+
        //
        // ACTIONS
        //
@@ -404,6 +435,18 @@ public class SwingInterface implements CoreListener, LoggingListener {
                if (configProperties.containsKey("language")) {
                        I18n.setLocale(new Locale(configProperties.getProperty("language")));
                }
+               if (configProperties.containsKey("mainWindowX")) {
+                       mainWindowX = Integer.valueOf(configProperties.getProperty("mainWindowX"));
+               }
+               if (configProperties.containsKey("mainWindowY")) {
+                       mainWindowY = Integer.valueOf(configProperties.getProperty("mainWindowY"));
+               }
+               if (configProperties.containsKey("mainWindowWidth")) {
+                       mainWindowWidth = Integer.valueOf(configProperties.getProperty("mainWindowWidth"));
+               }
+               if (configProperties.containsKey("mainWindowHeight")) {
+                       mainWindowHeight = Integer.valueOf(configProperties.getProperty("mainWindowHeight"));
+               }
        }
 
        /**
@@ -435,6 +478,10 @@ public class SwingInterface implements CoreListener, LoggingListener {
                        configProperties.setProperty("lookAndFeel", lookAndFeel);
                }
                configProperties.setProperty("language", I18n.getLocale().getLanguage());
+               configProperties.setProperty("mainWindowX", String.valueOf(mainWindowX));
+               configProperties.setProperty("mainWindowY", String.valueOf(mainWindowY));
+               configProperties.setProperty("mainWindowWidth", String.valueOf(mainWindowWidth));
+               configProperties.setProperty("mainWindowHeight", String.valueOf(mainWindowHeight));
                FileOutputStream configOutputStream = null;
                try {
                        configOutputStream = new FileOutputStream(configFile);
@@ -450,7 +497,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}
@@ -470,7 +517,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}
@@ -622,6 +669,12 @@ public class SwingInterface implements CoreListener, LoggingListener {
         * Quits jSite.
         */
        private void quit() {
+               /* TODO - ask */
+               core.stop();
+               mainWindowX = mainWindow.getX();
+               mainWindowY = mainWindow.getY();
+               mainWindowWidth = mainWindow.getWidth();
+               mainWindowHeight = mainWindow.getHeight();
                saveConfig();
                System.exit(0);
        }
@@ -633,6 +686,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();
@@ -640,6 +694,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
                nodeNodeDisconnectActions.clear();
                nodeDisconnectActionNodes.clear();
                for (Node node: nodes) {
+                       logger.finer("adding node “" + node + "” to menus");
                        Action nodeConnectAction = new AbstractAction(node.getName()) {
 
                                /**
@@ -665,7 +720,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
                                        nodeDisconnect(node);
                                }
                        };
-// nodeDisconnectActions.add(nodeDisconnectAction);
+                       // nodeDisconnectActions.add(nodeDisconnectAction);
                        nodeDisconnectActionNodes.put(nodeDisconnectAction, node);
                        nodeNodeDisconnectActions.put(node, nodeDisconnectAction);
                }
@@ -762,9 +817,17 @@ public class SwingInterface implements CoreListener, LoggingListener {
         * Adds a project.
         */
        private void addProject() {
-               Project project = new Project();
-               project.setName("New Project");
-               project.setDescription("");
+               try {
+                       Project project = core.createProject("New Project");
+                       System.out.println("private: " + project.getPrivateKey() + ", public: " + project.getPublicKey());
+                       mainWindow.addProject(project);
+               } catch (NoNodeException nne1) {
+                       /* TODO - add i18n */
+                       JOptionPane.showMessageDialog(mainWindow, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
+               } catch (IOException e) {
+                       /* TODO - add i18n */
+                       JOptionPane.showMessageDialog(mainWindow, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
+               }
        }
 
        /**
@@ -845,8 +908,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"));
        }
@@ -861,6 +922,28 @@ public class SwingInterface implements CoreListener, LoggingListener {
        /**
         * {@inheritDoc}
         */
+       public void nodeAdded(Node node) {
+               logger.log(Level.INFO, "node added: " + node);
+               nodeList.add(node);
+               logger.log(Level.FINE, "nodeList.size(): " + nodeList.size());
+               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);
@@ -874,6 +957,18 @@ 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();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void nodeConnectionFailed(Node node, Throwable cause) {
+               Action nodeConnectAction = nodeNodeConnectActions.get(node);
+               nodeConnectActions.add(nodeConnectAction);
+               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();
        }
 
@@ -885,9 +980,33 @@ 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(Request request) {
+               logger.log(Level.INFO, "request added to node: " + request + ", " + request.getNode());
+               /* TODO - implement */
+               requestTableModel.addRequest(request);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void requestProgressed(Request request) {
+               /* TODO - update table model */
+       }
+
+       /**
+        * @see net.pterodactylus.jsite.core.CoreListener#requestRemoved(net.pterodactylus.jsite.core.Request)
+        */
+       public void requestRemoved(Request request) {
+               requestTableModel.removeRequest(request);
+       }
+
        //
        // INTERFACE LoggingListener
        //