remove default node from interface
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 4 Apr 2008 22:52:12 +0000 (22:52 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 4 Apr 2008 22:52:12 +0000 (22:52 +0000)
git-svn-id: http://trooper/svn/projects/jSite/trunk@595 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/jsite/gui/ManageNodesDialog.java
src/net/pterodactylus/jsite/gui/SwingInterface.java

index d4c8397..c2d6adf 100644 (file)
@@ -23,7 +23,8 @@ import java.awt.BorderLayout;
 import java.awt.FlowLayout;
 import java.awt.event.ActionEvent;
 import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
 
 import javax.swing.AbstractListModel;
@@ -33,9 +34,9 @@ import javax.swing.JButton;
 import javax.swing.JDialog;
 import javax.swing.JFrame;
 import javax.swing.JList;
+import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
-import javax.swing.ListCellRenderer;
 import javax.swing.border.EtchedBorder;
 import javax.swing.event.ListSelectionEvent;
 import javax.swing.event.ListSelectionListener;
@@ -56,12 +57,6 @@ public class ManageNodesDialog extends JDialog implements ListSelectionListener
        /** The original list of nodes. */
        private List<Node> originalNodeList;
 
-       /** The current list of wrapped nodes. */
-       private List<NodeWrapper> wrappedNodeList = new ArrayList<NodeWrapper>();
-
-       /** The current default node. */
-       private Node defaultNode;
-
        /** The “add node” action. */
        private Action addNodeAction;
 
@@ -71,9 +66,6 @@ public class ManageNodesDialog extends JDialog implements ListSelectionListener
        /** The “delete node” action. */
        private Action deleteNodeAction;
 
-       /** The “set default node” action. */
-       private Action setDefaultNodeAction;
-
        /** The “okay” action. */
        private Action okayAction;
 
@@ -109,27 +101,6 @@ public class ManageNodesDialog extends JDialog implements ListSelectionListener
        //
 
        /**
-        * Returns the default node.
-        * 
-        * @return The default node, or <code>null</code> if no default node has
-        *         been set
-        */
-       public Node getDefaultNode() {
-               return defaultNode;
-       }
-
-       /**
-        * Sets the default node.
-        * 
-        * @param defaultNode
-        *            The default node, or <code>null</code> if no default node
-        *            has been set
-        */
-       public void setDefaultNode(Node defaultNode) {
-               this.defaultNode = defaultNode;
-       }
-
-       /**
         * Returns the list of nodes.
         * 
         * @return The list of nodes
@@ -146,11 +117,10 @@ public class ManageNodesDialog extends JDialog implements ListSelectionListener
         */
        public void setNodeList(List<Node> nodeList) {
                originalNodeList = nodeList;
-               wrappedNodeList.clear();
+               nodeListModel.clear();
                for (Node node: nodeList) {
-                       wrappedNodeList.add(new NodeWrapper(node));
+                       nodeListModel.addNode(node);
                }
-               nodeListModel.setNodeList(wrappedNodeList);
        }
 
        //
@@ -208,17 +178,7 @@ public class ManageNodesDialog extends JDialog implements ListSelectionListener
                         */
                        @SuppressWarnings("synthetic-access")
                        public void actionPerformed(ActionEvent e) {
-                               deleteNode();
-                       }
-               };
-               setDefaultNodeAction = new I18nAction("manageNodesDialog.button.setDefaultNode", false) {
-
-                       /**
-                        * {@inheritDoc}
-                        */
-                       @SuppressWarnings("synthetic-access")
-                       public void actionPerformed(ActionEvent e) {
-                               setDefaultNode();
+                               deleteNodes();
                        }
                };
        }
@@ -249,7 +209,6 @@ public class ManageNodesDialog extends JDialog implements ListSelectionListener
                listButtonPanel.add(new JButton(addNodeAction));
                listButtonPanel.add(new JButton(editNodeAction));
                listButtonPanel.add(new JButton(deleteNodeAction));
-               listButtonPanel.add(new JButton(setDefaultNodeAction));
 
                nodeList = new JList(nodeListModel);
                nodeList.addListSelectionListener(this);
@@ -283,13 +242,7 @@ public class ManageNodesDialog extends JDialog implements ListSelectionListener
                        newNode.setName(editNodeDialog.getNodeName());
                        newNode.setHostname(editNodeDialog.getNodeHostname());
                        newNode.setPort(editNodeDialog.getNodePort());
-                       NodeWrapper newNodeWrapper = new NodeWrapper(newNode);
-                       if (wrappedNodeList.isEmpty()) {
-                               defaultNode = newNode;
-                               newNodeWrapper.setDefaultNode(true);
-                       }
-                       wrappedNodeList.add(new NodeWrapper(newNode));
-                       nodeListModel.setNodeList(wrappedNodeList);
+                       nodeListModel.addNode(newNode);
                }
        }
 
@@ -297,8 +250,7 @@ public class ManageNodesDialog extends JDialog implements ListSelectionListener
         * Edits a node via {@link #editNodeDialog}.
         */
        private void editNode() {
-               NodeWrapper selectedNodeWrapper = (NodeWrapper) nodeList.getSelectedValue();
-               Node selectedNode = selectedNodeWrapper.getWrappedNode();
+               Node selectedNode = (Node) nodeList.getSelectedValue();
                editNodeDialog.setNodeName(selectedNode.getName());
                editNodeDialog.setNodeHostname(selectedNode.getHostname());
                editNodeDialog.setNodePort(selectedNode.getPort());
@@ -316,38 +268,38 @@ public class ManageNodesDialog extends JDialog implements ListSelectionListener
        /**
         * Deletes the selected node.
         */
-       private void deleteNode() {
-               NodeWrapper selectedNode = (NodeWrapper) nodeList.getSelectedValue();
-               wrappedNodeList.remove(selectedNode);
+       private void deleteNodes() {
+               Object[] selectedNodes = nodeList.getSelectedValues();
+               for (Object node: selectedNodes) {
+                       nodeListModel.removeNode((Node) node);
+               }
                nodeList.clearSelection();
-               nodeList.repaint();
        }
 
        /**
-        * Sets the default node to the selected node.
+        * Checks whether the list of nodes is not empty.
+        * 
+        * @return <code>true</code> if there is at least one node defined,
+        *         <code>false</code> otherwise
         */
-       private void setDefaultNode() {
-               NodeWrapper selectedNode = (NodeWrapper) nodeList.getSelectedValue();
-               for (NodeWrapper nodeWrapper: wrappedNodeList) {
-                       nodeWrapper.setDefaultNode(nodeWrapper == selectedNode);
-               }
-               nodeList.repaint();
+       private boolean verifyNodesExist() {
+               return nodeListModel.getSize() > 0;
        }
 
        /**
         * This method is called when the “okay” button is pressed. The nodes from
-        * the list are read and the {@link #wrappedNodeList} and
-        * {@link #defaultNode} members are set so that the calling code can use
-        * {@link #getNodeList()} and {@link #getDefaultNode()} to get the changed
+        * the list are read and the {@link #originalNodeList} member is set so that
+        * the calling code can use {@link #getNodeList()} to get the changed
         * values.
         */
        private void confirm() {
+               if (!verifyNodesExist()) {
+                       JOptionPane.showMessageDialog(this, I18n.get("manageNodesDialog.error.nodeListEmpty.message"), I18n.get("manageNodesDialog.error.nodeListEmpty.title"), JOptionPane.ERROR_MESSAGE);
+                       return;
+               }
                originalNodeList.clear();
-               for (NodeWrapper nodeWrapper: wrappedNodeList) {
-                       originalNodeList.add(nodeWrapper.getWrappedNode());
-                       if (nodeWrapper.isDefaultNode()) {
-                               defaultNode = nodeWrapper.getWrappedNode();
-                       }
+               for (Node node: nodeListModel) {
+                       originalNodeList.add(node);
                }
                setVisible(false);
        }
@@ -371,102 +323,69 @@ public class ManageNodesDialog extends JDialog implements ListSelectionListener
                int selectCount = list.getSelectedIndices().length;
                editNodeAction.setEnabled(selectCount == 1);
                deleteNodeAction.setEnabled(selectCount >= 1);
-               setDefaultNodeAction.setEnabled(selectCount == 1);
        }
 
        /**
-        * Wrapper around a {@link Node} to store whether a node is the default
-        * node. This frees us from having to write a {@link ListCellRenderer} for
-        * {@link ManageNodesDialog#nodeList}.
+        * List model for the {@link ManageNodesDialog#nodeList}. TODO
         * 
         * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
         * @version $Id$
         */
-       private static class NodeWrapper {
+       private class NodeListModel extends AbstractListModel implements Iterable<Node> {
 
-               /** The wrapped node. */
-               private final Node wrappedNode;
-
-               /** The default node. */
-               private boolean defaultNode;
+               /** The list of nodes. */
+               @SuppressWarnings("hiding")
+               private final List<Node> nodeList = new ArrayList<Node>();
 
                /**
-                * Creates a new node wrapper around the given node.
-                * 
-                * @param wrappedNode
-                *            The node to wrap
+                * Creates a new node list model.
                 */
-               public NodeWrapper(Node wrappedNode) {
-                       this.wrappedNode = wrappedNode;
+               public NodeListModel() {
                }
 
                /**
-                * Returns the wrapped node.
+                * Adds the given node to the list model.
                 * 
-                * @return The wrapped node
+                * @see Collection#add(Object)
+                * @param node
+                *            The node to add
                 */
-               public Node getWrappedNode() {
-                       return wrappedNode;
+               public void addNode(Node node) {
+                       nodeList.add(node);
+                       fireIntervalAdded(this, nodeList.size() - 1, nodeList.size() - 1);
                }
 
                /**
-                * Returns whether the wrapped node is the default node.
+                * Removes the given node from the list model.
                 * 
-                * @return <code>true</code> if the wrapped node is the default node,
-                *         <code>false</code> otherwise
+                * @see Collection#remove(Object)
+                * @param node
+                *            The node to remove
                 */
-               public boolean isDefaultNode() {
-                       return defaultNode;
+               public void removeNode(Node node) {
+                       int nodeIndex = nodeList.indexOf(node);
+                       nodeList.remove(node);
+                       fireIntervalRemoved(this, nodeIndex, nodeIndex);
                }
 
                /**
-                * Sets whether the wrapped node is the default node.
+                * Removes all nodes from the list model.
                 * 
-                * @param defaultNode
-                *            <code>true</code> if the wrapped node is the default
-                *            node, <code>false</code> otherwise
+                * @see Collection#clear()
                 */
-               public void setDefaultNode(boolean defaultNode) {
-                       this.defaultNode = defaultNode;
+               public void clear() {
+                       int nodeCount = nodeList.size();
+                       if (nodeCount > 0) {
+                               nodeList.clear();
+                               fireIntervalRemoved(this, 0, nodeCount - 1);
+                       }
                }
 
                /**
                 * {@inheritDoc}
                 */
-               @Override
-               public String toString() {
-                       return wrappedNode.toString() + (defaultNode ? " (default)" : "");
-               }
-
-       }
-
-       /**
-        * List model for the {@link ManageNodesDialog#nodeList}. TODO
-        * 
-        * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
-        * @version $Id$
-        */
-       private class NodeListModel extends AbstractListModel {
-
-               /** The list of wrapped nodes. */
-               @SuppressWarnings("hiding")
-               private List<NodeWrapper> nodeList = Collections.emptyList();
-
-               /**
-                * Creates a new node list model.
-                */
-               public NodeListModel() {
-               }
-
-               /**
-                * Sets the new node list.
-                * 
-                * @param nodeList
-                *            The list of nodes to display
-                */
-               public void setNodeList(List<NodeWrapper> nodeList) {
-                       this.nodeList = nodeList;
-                       fireContentsChanged(this, 0, nodeList.size() - 1);
+               public Iterator<Node> iterator() {
+                       return nodeList.iterator();
                }
 
                /**
@@ -474,8 +393,7 @@ public class ManageNodesDialog extends JDialog implements ListSelectionListener
                 */
                @SuppressWarnings("synthetic-access")
                public Object getElementAt(int index) {
-                       NodeWrapper currentNodeWrapper = nodeList.get(index);
-                       return currentNodeWrapper;
+                       return nodeList.get(index);
                }
 
                /**
index fab1bc9..ef15f79 100644 (file)
@@ -57,9 +57,6 @@ public class SwingInterface implements CoreListener {
        /** The list of all defined nodes. */
        private List<Node> nodeList;
 
-       /** The current default node. */
-       private Node defaultNode;
-
        /**
         * Creates a new swing interface.
         * 
@@ -167,7 +164,9 @@ public class SwingInterface implements CoreListener {
         * Pops up the “manage nodes” dialog.
         */
        private void manageNodes() {
+               manageNodesDialog.setNodeList(nodeList);
                manageNodesDialog.setVisible(true);
+               nodeList = manageNodesDialog.getNodeList();
        }
 
        /**
@@ -192,9 +191,7 @@ public class SwingInterface implements CoreListener {
         */
        public void coreLoaded() {
                this.nodeList = core.getNodeList();
-               this.defaultNode = core.getDefaultNode();
                manageNodesDialog.setNodeList(nodeList);
-               manageNodesDialog.setDefaultNode(defaultNode);
                mainWindow.setVisible(true);
                mainWindow.setStatusBarText("Core loaded.");
        }