Fix calculation of project size.
[jSite.git] / src / de / todesbaum / jsite / gui / NodeManagerPage.java
index 1fa8937..8e44129 100644 (file)
@@ -1,6 +1,5 @@
 /*
- * jSite-0.7 - 
- * Copyright (C) 2006 David Roden
+ * jSite - NodeManagerPage.java - Copyright © 2006–2012 David Roden
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -61,21 +60,42 @@ import de.todesbaum.util.swing.TWizard;
 import de.todesbaum.util.swing.TWizardPage;
 
 /**
- * @author David Roden <droden@gmail.com>
- * @version $Id$
+ * Wizard page that lets the user edit his nodes.
+ *
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  */
 public class NodeManagerPage extends TWizardPage implements ListSelectionListener, DocumentListener, ChangeListener {
 
+       /** List of node manager listeners. */
        private List<NodeManagerListener> nodeManagerListeners = new ArrayList<NodeManagerListener>();
 
+       /** The “add node” action. */
        protected Action addNodeAction;
+
+       /** The “delete node” action. */
        protected Action deleteNodeAction;
+
+       /** The node list model. */
        private DefaultListModel nodeListModel;
+
+       /** The node list. */
        private JList nodeList;
+
+       /** The node name textfield. */
        private JTextField nodeNameTextField;
+
+       /** The node hostname textfield. */
        private JTextField nodeHostnameTextField;
+
+       /** The spinner for the node port. */
        private JSpinner nodePortSpinner;
 
+       /**
+        * Creates a new node manager wizard page.
+        *
+        * @param wizard
+        *            The wizard this page belongs to
+        */
        public NodeManagerPage(final TWizard wizard) {
                super(wizard);
                pageInit();
@@ -89,21 +109,54 @@ public class NodeManagerPage extends TWizardPage implements ListSelectionListene
                        }
                });
        }
-       
+
+       /**
+        * Adds a listener for node manager events.
+        *
+        * @param nodeManagerListener
+        *            The listener to add
+        */
        public void addNodeManagerListener(NodeManagerListener nodeManagerListener) {
                nodeManagerListeners.add(nodeManagerListener);
        }
-       
+
+       /**
+        * Removes a listener for node manager events.
+        *
+        * @param nodeManagerListener
+        *            The listener to remove
+        */
        public void removeNodeManagerListener(NodeManagerListener nodeManagerListener) {
                nodeManagerListeners.remove(nodeManagerListener);
        }
-       
+
+       /**
+        * Notifies all listeners that the node configuration has changed.
+        *
+        * @param nodes
+        *            The new list of nodes
+        */
        protected void fireNodesUpdated(Node[] nodes) {
-               for (NodeManagerListener nodeManagerListener: nodeManagerListeners) {
+               for (NodeManagerListener nodeManagerListener : nodeManagerListeners) {
                        nodeManagerListener.nodesUpdated(nodes);
                }
        }
 
+       /**
+        * Notifies all listeners that a new node was selected.
+        *
+        * @param node
+        *            The newly selected node
+        */
+       protected void fireNodeSelected(Node node) {
+               for (NodeManagerListener nodeManagerListener : nodeManagerListeners) {
+                       nodeManagerListener.nodeSelected(node);
+               }
+       }
+
+       /**
+        * Creates all actions.
+        */
        private void createActions() {
                addNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.add-node")) {
 
@@ -131,6 +184,9 @@ public class NodeManagerPage extends TWizardPage implements ListSelectionListene
                });
        }
 
+       /**
+        * Initializes the page and all components in it.
+        */
        private void pageInit() {
                createActions();
                nodeListModel = new DefaultListModel();
@@ -144,7 +200,7 @@ public class NodeManagerPage extends TWizardPage implements ListSelectionListene
                nodeNameTextField.getDocument().putProperty("Name", "node-name");
                nodeNameTextField.getDocument().addDocumentListener(this);
                nodeNameTextField.setEnabled(false);
-               
+
                nodeHostnameTextField = new JTextField("localhost");
                nodeHostnameTextField.getDocument().putProperty("Name", "node-hostname");
                nodeHostnameTextField.getDocument().addDocumentListener(this);
@@ -190,7 +246,7 @@ public class NodeManagerPage extends TWizardPage implements ListSelectionListene
                        }
                });
        }
-       
+
        /**
         * {@inheritDoc}
         */
@@ -202,15 +258,26 @@ public class NodeManagerPage extends TWizardPage implements ListSelectionListene
                this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
        }
 
+       /**
+        * Sets the node list.
+        *
+        * @param nodes
+        *            The list of nodes
+        */
        public void setNodes(Node[] nodes) {
                nodeListModel.clear();
-               for (Node node: nodes) {
+               for (Node node : nodes) {
                        nodeListModel.addElement(node);
                }
                nodeList.repaint();
                fireNodesUpdated(nodes);
        }
 
+       /**
+        * Returns the node list.
+        *
+        * @return The list of nodes
+        */
        public Node[] getNodes() {
                Node[] returnNodes = new Node[nodeListModel.getSize()];
                for (int nodeIndex = 0, nodeCount = nodeListModel.getSize(); nodeIndex < nodeCount; nodeIndex++) {
@@ -219,10 +286,25 @@ public class NodeManagerPage extends TWizardPage implements ListSelectionListene
                return returnNodes;
        }
 
+       /**
+        * Returns the currently selected node.
+        *
+        * @return The selected node, or <code>null</code> if no node is selected
+        */
        private Node getSelectedNode() {
                return (Node) nodeList.getSelectedValue();
        }
-       
+
+       /**
+        * Updates node name or hostname when the user types into the textfields.
+        *
+        * @see #insertUpdate(DocumentEvent)
+        * @see #removeUpdate(DocumentEvent)
+        * @see #changedUpdate(DocumentEvent)
+        * @see DocumentListener
+        * @param documentEvent
+        *            The document event
+        */
        private void updateTextField(DocumentEvent documentEvent) {
                Node node = getSelectedNode();
                if (node == null) {
@@ -233,6 +315,7 @@ public class NodeManagerPage extends TWizardPage implements ListSelectionListene
                try {
                        documentText = document.getText(0, document.getLength());
                } catch (BadLocationException ble1) {
+                       /* ignore. */
                }
                if (documentText == null) {
                        return;
@@ -253,13 +336,20 @@ public class NodeManagerPage extends TWizardPage implements ListSelectionListene
        // ACTIONS
        //
 
+       /**
+        * Adds a new node to the list of nodes.
+        */
        private void addNode() {
                Node node = new Node("localhost", 9481, I18n.getMessage("jsite.node-manager.new-node"));
                nodeListModel.addElement(node);
+               deleteNodeAction.setEnabled(nodeListModel.size() > 1);
                wizard.setNextEnabled(true);
                fireNodesUpdated(getNodes());
        }
 
+       /**
+        * Deletes the currently selected node from the list of nodes.
+        */
        private void deleteNode() {
                Node node = getSelectedNode();
                if (node == null) {
@@ -268,9 +358,12 @@ public class NodeManagerPage extends TWizardPage implements ListSelectionListene
                if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.node-manager.delete-node.warning"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION) {
                        return;
                }
+               int nodeIndex = nodeListModel.indexOf(node);
                nodeListModel.removeElement(node);
                nodeList.repaint();
+               fireNodeSelected((Node) nodeListModel.get(Math.min(nodeIndex, nodeListModel.size() - 1)));
                fireNodesUpdated(getNodes());
+               deleteNodeAction.setEnabled(nodeListModel.size() > 1);
                wizard.setNextEnabled(nodeListModel.size() > 0);
        }
 
@@ -281,6 +374,7 @@ public class NodeManagerPage extends TWizardPage implements ListSelectionListene
        /**
         * {@inheritDoc}
         */
+       @SuppressWarnings("null")
        public void valueChanged(ListSelectionEvent e) {
                Object source = e.getSource();
                if (source instanceof JList) {
@@ -291,7 +385,7 @@ public class NodeManagerPage extends TWizardPage implements ListSelectionListene
                                nodeNameTextField.setEnabled(enabled);
                                nodeHostnameTextField.setEnabled(enabled);
                                nodePortSpinner.setEnabled(enabled);
-                               deleteNodeAction.setEnabled(enabled);
+                               deleteNodeAction.setEnabled(enabled && (nodeListModel.size() > 1));
                                if (enabled) {
                                        nodeNameTextField.setText(node.getName());
                                        nodeHostnameTextField.setText(node.getHostname());
@@ -347,6 +441,7 @@ public class NodeManagerPage extends TWizardPage implements ListSelectionListene
                        JSpinner sourceSpinner = (JSpinner) source;
                        if ("node-port".equals(sourceSpinner.getName())) {
                                selectedNode.setPort((Integer) sourceSpinner.getValue());
+                               fireNodeSelected(selectedNode);
                                nodeList.repaint();
                        }
                }