-/*
- * jSite2 - ManageNodeDialog.java -
- * Copyright © 2008 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
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-package net.pterodactylus.jsite.gui;
-
-import java.awt.BorderLayout;
-import java.awt.FlowLayout;
-import java.awt.event.ActionEvent;
-import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.logging.Logger;
-
-import javax.swing.AbstractListModel;
-import javax.swing.BorderFactory;
-import javax.swing.JButton;
-import javax.swing.JDialog;
-import javax.swing.JList;
-import javax.swing.JOptionPane;
-import javax.swing.JPanel;
-import javax.swing.JScrollPane;
-import javax.swing.border.EtchedBorder;
-import javax.swing.event.ListSelectionEvent;
-import javax.swing.event.ListSelectionListener;
-
-import net.pterodactylus.jsite.core.Core;
-import net.pterodactylus.jsite.core.Node;
-import net.pterodactylus.jsite.i18n.I18n;
-import net.pterodactylus.jsite.i18n.I18nable;
-import net.pterodactylus.jsite.i18n.gui.I18nAction;
-import net.pterodactylus.jsite.i18n.gui.I18nLabel;
-import net.pterodactylus.jsite.main.Version;
-import net.pterodactylus.util.logging.Logging;
-import net.pterodactylus.util.swing.SwingUtils;
-
-/**
- * Dialog that lets the user manage her nodes.
- *
- * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- * @version $Id$
- */
-public class ManageNodesDialog extends JDialog implements ListSelectionListener, I18nable {
-
- /** The logger. */
- @SuppressWarnings("unused")
- private static final Logger logger = Logging.getLogger(ManageNodesDialog.class.getName());
-
- /** The core. */
- private final Core core;
-
- /** The original list of nodes. */
- private List<Node> originalNodeList;
-
- /** The “add node” action. */
- private I18nAction addNodeAction;
-
- /** The “edit node” action. */
- private I18nAction editNodeAction;
-
- /** The “delete node” action. */
- private I18nAction deleteNodeAction;
-
- /** The “okay” action. */
- private I18nAction okayAction;
-
- /** The “cancel” action. */
- private I18nAction cancelAction;
-
- /** The “edit node” dialog. */
- private EditNodeDialog editNodeDialog;
-
- /** The warning label. */
- private I18nLabel immediatelyEffectiveLabel;
-
- /** The node list. */
- private JList nodeList;
-
- /** The mode for the node list. */
- private NodeListModel nodeListModel = new NodeListModel();
-
- /**
- * Creates a new node manager dialog.
- *
- * @param swingInterface
- * The Swing interface
- */
- public ManageNodesDialog(SwingInterface swingInterface) {
- super(swingInterface.getMainWindow(), I18n.get("manageNodesDialog.title") + " – jSite " + Version.getVersion(), true);
- this.core = swingInterface.getCore();
- initActions();
- initComponents();
- initDialogs();
- pack();
- I18n.registerI18nable(this);
- SwingUtils.center(this);
- }
-
- //
- // ACCESSORS
- //
-
- /**
- * Expose the edit node dialog for the simple mode.
- *
- * @return The edit node dialog
- */
- EditNodeDialog getEditNodeDialog() {
- return editNodeDialog;
- }
-
- /**
- * Returns the list of nodes.
- *
- * @return The list of nodes
- */
- public List<Node> getNodeList() {
- return originalNodeList;
- }
-
- /**
- * Sets the list of nodes.
- *
- * @param nodeList
- * The list of nodes
- */
- public void setNodeList(List<Node> nodeList) {
- originalNodeList = new ArrayList<Node>(nodeList);
- nodeListModel.clear();
- for (Node node: nodeList) {
- nodeListModel.addNode(node);
- }
- }
-
- //
- // PRIVATE METHODS
- //
-
- /**
- * Initializes all actions.
- */
- private void initActions() {
- okayAction = new I18nAction("general.button.okay") {
-
- /**
- * {@inheritDoc}
- */
- @SuppressWarnings("synthetic-access")
- public void actionPerformed(ActionEvent e) {
- confirm();
- }
- };
- cancelAction = new I18nAction("general.button.cancel") {
-
- /**
- * {@inheritDoc}
- */
- @SuppressWarnings("synthetic-access")
- public void actionPerformed(ActionEvent e) {
- cancel();
- }
- };
- addNodeAction = new I18nAction("manageNodesDialog.button.addNode") {
-
- /**
- * {@inheritDoc}
- */
- @SuppressWarnings("synthetic-access")
- public void actionPerformed(ActionEvent e) {
- addNode();
- }
- };
- editNodeAction = new I18nAction("manageNodesDialog.button.editNode", false) {
-
- /**
- * {@inheritDoc}
- */
- @SuppressWarnings("synthetic-access")
- public void actionPerformed(ActionEvent e) {
- editNode();
- }
- };
- deleteNodeAction = new I18nAction("manageNodesDialog.button.deleteNode", false) {
-
- /**
- * {@inheritDoc}
- */
- @SuppressWarnings("synthetic-access")
- public void actionPerformed(ActionEvent e) {
- deleteNodes();
- }
- };
- }
-
- /**
- * Initializes all components.
- */
- private void initComponents() {
- JPanel rootPanel = new JPanel(new BorderLayout(12, 12));
- rootPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
-
- JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
- rootPanel.add(buttonPanel, BorderLayout.PAGE_END);
- buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
-
- buttonPanel.add(new JButton(cancelAction));
- JButton okayButton = new JButton(okayAction);
- getRootPane().setDefaultButton(okayButton);
- buttonPanel.add(okayButton);
-
- JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
- rootPanel.add(contentPanel, BorderLayout.CENTER);
- contentPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
-
- JPanel warningPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 12, 12));
- warningPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
- warningPanel.add(immediatelyEffectiveLabel = new I18nLabel("manageNodesDialog.label.immediatelyEffective"));
- contentPanel.add(warningPanel, BorderLayout.PAGE_START);
-
- JPanel listButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 12, 12));
- contentPanel.add(listButtonPanel, BorderLayout.PAGE_END);
- listButtonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
- listButtonPanel.add(new JButton(addNodeAction));
- listButtonPanel.add(new JButton(editNodeAction));
- listButtonPanel.add(new JButton(deleteNodeAction));
-
- nodeList = new JList(nodeListModel);
- nodeList.addListSelectionListener(this);
- contentPanel.add(new JScrollPane(nodeList), BorderLayout.CENTER);
-
- setContentPane(rootPanel);
- }
-
- /**
- * Initializes all child dialogs.
- */
- private void initDialogs() {
- editNodeDialog = new EditNodeDialog(this);
- }
-
- //
- // PRIVATE ACTIONS
- //
-
- /**
- * Adds a new node via {@link #editNodeDialog}.
- */
- private void addNode() {
- editNodeDialog.setNodeName(I18n.get("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 {
- if (!core.addNode(newNode)) {
- JOptionPane.showMessageDialog(this, I18n.get("manageNodesDialog.error.nodeAlreadyKnown.message", newNode.getHostname(), newNode.getPort()), I18n.get("manageNodesDialog.error.nodeAlreadyKnown.title"), JOptionPane.ERROR_MESSAGE);
- }
- } catch (UnknownHostException e) {
- /*
- * normally this shouldn't throw because the node editor catches
- * it.
- */
- JOptionPane.showMessageDialog(this, I18n.get("manageNodesDialog.error.nodeUnresolvable.message", newNode.getHostname()), I18n.get("manageNodesDialog.error.nodeUnresolvable.title"), JOptionPane.ERROR_MESSAGE);
- }
- }
- }
-
- /**
- * Edits a node via {@link #editNodeDialog}.
- */
- private void editNode() {
- Node selectedNode = (Node) nodeList.getSelectedValue();
- editNodeDialog.setNodeName(selectedNode.getName());
- editNodeDialog.setNodeHostname(selectedNode.getHostname());
- editNodeDialog.setNodePort(selectedNode.getPort());
- editNodeDialog.setVisible(true);
- if (!editNodeDialog.wasCancelled()) {
- selectedNode.setName(editNodeDialog.getNodeName());
- selectedNode.setHostname(editNodeDialog.getNodeHostname());
- selectedNode.setPort(editNodeDialog.getNodePort());
- nodeList.repaint();
- }
- }
-
- /**
- * Deletes the selected node.
- */
- private void deleteNodes() {
- Object[] selectedNodes = nodeList.getSelectedValues();
- for (Object node: selectedNodes) {
- Node selectedNode = (Node) node;
- if (core.isNodeConnected(selectedNode)) {
- int response = JOptionPane.showConfirmDialog(this, I18n.get("manageNodesDialog.error.nodeConnected.message", selectedNode.getName()), I18n.get("manageNodesDialog.error.nodeConnected.title"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
- if (response == JOptionPane.CANCEL_OPTION) {
- break;
- } else if (response == JOptionPane.NO_OPTION) {
- continue;
- }
- }
- nodeListModel.removeNode(selectedNode);
- }
- nodeList.clearSelection();
- }
-
- /**
- * 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 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 #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 (Node node: nodeListModel) {
- originalNodeList.add(node);
- }
- setVisible(false);
- }
-
- /**
- * Cancels the dialog.
- */
- private void cancel() {
- setVisible(false);
- }
-
- //
- // INTERFACE ListSelectionListener
- //
-
- /**
- * {@inheritDoc}
- */
- public void valueChanged(ListSelectionEvent listSelectionEvent) {
- JList list = (JList) listSelectionEvent.getSource();
- int selectCount = list.getSelectedIndices().length;
- editNodeAction.setEnabled(selectCount == 1);
- deleteNodeAction.setEnabled(selectCount >= 1);
- }
-
- //
- // INTERFACE I18nable
- //
-
- /**
- * {@inheritDoc}
- */
- public void updateI18n() {
- okayAction.updateI18n();
- cancelAction.updateI18n();
- addNodeAction.updateI18n();
- editNodeAction.updateI18n();
- deleteNodeAction.updateI18n();
- immediatelyEffectiveLabel.updateI18n();
- setTitle(I18n.get("manageNodesDialog.title") + " – jSite " + Version.getVersion());
- SwingUtils.repackCentered(this);
- }
-
- /**
- * List model for the {@link ManageNodesDialog#nodeList}.
- *
- * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- * @version $Id$
- */
- private class NodeListModel extends AbstractListModel implements Iterable<Node> {
-
- /** The list of nodes. */
- @SuppressWarnings("hiding")
- private final List<Node> nodeList = new ArrayList<Node>();
-
- /**
- * Creates a new node list model.
- */
- public NodeListModel() {
- /* do nothing. */
- }
-
- /**
- * Adds the given node to the list model.
- *
- * @see Collection#add(Object)
- * @param node
- * The node to add
- */
- public void addNode(Node node) {
- nodeList.add(node);
- fireIntervalAdded(this, nodeList.size() - 1, nodeList.size() - 1);
- }
-
- /**
- * Removes the given node from the list model.
- *
- * @see Collection#remove(Object)
- * @param node
- * The node to remove
- */
- public void removeNode(Node node) {
- int nodeIndex = nodeList.indexOf(node);
- nodeList.remove(node);
- fireIntervalRemoved(this, nodeIndex, nodeIndex);
- }
-
- /**
- * Removes all nodes from the list model.
- *
- * @see Collection#clear()
- */
- public void clear() {
- int nodeCount = nodeList.size();
- if (nodeCount > 0) {
- nodeList.clear();
- fireIntervalRemoved(this, 0, nodeCount - 1);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public Iterator<Node> iterator() {
- return nodeList.iterator();
- }
-
- /**
- * {@inheritDoc}
- */
- @SuppressWarnings("synthetic-access")
- public Object getElementAt(int index) {
- return nodeList.get(index);
- }
-
- /**
- * {@inheritDoc}
- */
- public int getSize() {
- return nodeList.size();
- }
-
- }
-
-}