2 * jSite2 - ManageNodeDialog.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.jsite.gui;
22 import java.awt.BorderLayout;
23 import java.awt.FlowLayout;
24 import java.awt.event.ActionEvent;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Iterator;
28 import java.util.List;
30 import javax.swing.AbstractListModel;
31 import javax.swing.Action;
32 import javax.swing.BorderFactory;
33 import javax.swing.JButton;
34 import javax.swing.JDialog;
35 import javax.swing.JList;
36 import javax.swing.JOptionPane;
37 import javax.swing.JPanel;
38 import javax.swing.JScrollPane;
39 import javax.swing.border.EtchedBorder;
40 import javax.swing.event.ListSelectionEvent;
41 import javax.swing.event.ListSelectionListener;
43 import net.pterodactylus.jsite.core.Core;
44 import net.pterodactylus.jsite.core.Node;
45 import net.pterodactylus.jsite.i18n.I18n;
46 import net.pterodactylus.jsite.main.Version;
47 import net.pterodactylus.util.swing.SwingUtils;
50 * Dialog that lets the user manage her nodes.
52 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
55 public class ManageNodesDialog extends JDialog implements ListSelectionListener {
58 private final Core core;
60 /** The original list of nodes. */
61 private List<Node> originalNodeList;
63 /** The “add node” action. */
64 private Action addNodeAction;
66 /** The “edit node” action. */
67 private Action editNodeAction;
69 /** The “delete node” action. */
70 private Action deleteNodeAction;
72 /** The “okay” action. */
73 private Action okayAction;
75 /** The “cancel” action. */
76 private Action cancelAction;
78 /** The “edit node” dialog. */
79 private EditNodeDialog editNodeDialog;
82 private JList nodeList;
84 /** The mode for the node list. */
85 private NodeListModel nodeListModel = new NodeListModel();
88 * Creates a new node manager dialog.
90 * @param swingInterface
93 public ManageNodesDialog(SwingInterface swingInterface) {
94 super(swingInterface.getMainWindow(), I18n.get("manageNodesDialog.title") + " – jSite " + Version.getVersion(), true);
95 this.core = swingInterface.getCore();
100 SwingUtils.center(this);
108 * Returns the list of nodes.
110 * @return The list of nodes
112 public List<Node> getNodeList() {
113 return originalNodeList;
117 * Sets the list of nodes.
122 public void setNodeList(List<Node> nodeList) {
123 originalNodeList = nodeList;
124 nodeListModel.clear();
125 for (Node node: nodeList) {
126 nodeListModel.addNode(node);
135 * Initializes all actions.
137 private void initActions() {
138 okayAction = new I18nAction("general.button.okay") {
143 @SuppressWarnings("synthetic-access")
144 public void actionPerformed(ActionEvent e) {
148 cancelAction = new I18nAction("general.button.cancel") {
153 @SuppressWarnings("synthetic-access")
154 public void actionPerformed(ActionEvent e) {
158 addNodeAction = new I18nAction("manageNodesDialog.button.addNode") {
163 @SuppressWarnings("synthetic-access")
164 public void actionPerformed(ActionEvent e) {
168 editNodeAction = new I18nAction("manageNodesDialog.button.editNode", false) {
173 @SuppressWarnings("synthetic-access")
174 public void actionPerformed(ActionEvent e) {
178 deleteNodeAction = new I18nAction("manageNodesDialog.button.deleteNode", false) {
183 @SuppressWarnings("synthetic-access")
184 public void actionPerformed(ActionEvent e) {
191 * Initializes all components.
193 private void initComponents() {
194 JPanel rootPanel = new JPanel(new BorderLayout(12, 12));
195 rootPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
197 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
198 rootPanel.add(buttonPanel, BorderLayout.PAGE_END);
199 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
201 buttonPanel.add(new JButton(cancelAction));
202 JButton okayButton = new JButton(okayAction);
203 getRootPane().setDefaultButton(okayButton);
204 buttonPanel.add(okayButton);
206 JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
207 rootPanel.add(contentPanel, BorderLayout.CENTER);
208 contentPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
210 JPanel listButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 12, 12));
211 contentPanel.add(listButtonPanel, BorderLayout.PAGE_END);
212 listButtonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
213 listButtonPanel.add(new JButton(addNodeAction));
214 listButtonPanel.add(new JButton(editNodeAction));
215 listButtonPanel.add(new JButton(deleteNodeAction));
217 nodeList = new JList(nodeListModel);
218 nodeList.addListSelectionListener(this);
219 contentPanel.add(new JScrollPane(nodeList), BorderLayout.CENTER);
221 setContentPane(rootPanel);
225 * Initializes all child dialogs.
227 private void initDialogs() {
228 editNodeDialog = new EditNodeDialog(this);
236 * Adds a new node via {@link #editNodeDialog}.
238 private void addNode() {
239 editNodeDialog.setNodeName("New Node");
240 editNodeDialog.setNodeHostname("localhost");
241 editNodeDialog.setNodePort(9481);
242 editNodeDialog.setNodeOnSameMachine(true);
243 editNodeDialog.setVisible(true);
244 if (!editNodeDialog.wasCancelled()) {
245 Node newNode = new Node();
246 newNode.setName(editNodeDialog.getNodeName());
247 newNode.setHostname(editNodeDialog.getNodeHostname());
248 newNode.setPort(editNodeDialog.getNodePort());
249 nodeListModel.addNode(newNode);
254 * Edits a node via {@link #editNodeDialog}.
256 private void editNode() {
257 Node selectedNode = (Node) nodeList.getSelectedValue();
258 editNodeDialog.setNodeName(selectedNode.getName());
259 editNodeDialog.setNodeHostname(selectedNode.getHostname());
260 editNodeDialog.setNodePort(selectedNode.getPort());
261 editNodeDialog.setNodeOnSameMachine(selectedNode.isSameMachine());
262 editNodeDialog.setVisible(true);
263 if (!editNodeDialog.wasCancelled()) {
264 selectedNode.setName(editNodeDialog.getNodeName());
265 selectedNode.setHostname(editNodeDialog.getNodeHostname());
266 selectedNode.setPort(editNodeDialog.getNodePort());
267 selectedNode.setSameMachine(editNodeDialog.isNodeOnSameMachine());
273 * Deletes the selected node.
275 private void deleteNodes() {
276 Object[] selectedNodes = nodeList.getSelectedValues();
277 for (Object node: selectedNodes) {
278 Node selectedNode = (Node) node;
279 if (core.isNodeConnected(selectedNode)) {
280 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);
281 if (response == JOptionPane.CANCEL_OPTION) {
283 } else if (response == JOptionPane.NO_OPTION) {
287 nodeListModel.removeNode(selectedNode);
289 nodeList.clearSelection();
293 * Checks whether the list of nodes is not empty.
295 * @return <code>true</code> if there is at least one node defined,
296 * <code>false</code> otherwise
298 private boolean verifyNodesExist() {
299 return nodeListModel.getSize() > 0;
303 * This method is called when the “okay” button is pressed. The nodes from
304 * the list are read and the {@link #originalNodeList} member is set so that
305 * the calling code can use {@link #getNodeList()} to get the changed
308 private void confirm() {
309 if (!verifyNodesExist()) {
310 JOptionPane.showMessageDialog(this, I18n.get("manageNodesDialog.error.nodeListEmpty.message"), I18n.get("manageNodesDialog.error.nodeListEmpty.title"), JOptionPane.ERROR_MESSAGE);
313 originalNodeList.clear();
314 for (Node node: nodeListModel) {
315 originalNodeList.add(node);
321 * Cancels the dialog.
323 private void cancel() {
328 // INTERFACE ListSelectionListener
334 public void valueChanged(ListSelectionEvent listSelectionEvent) {
335 JList list = (JList) listSelectionEvent.getSource();
336 int selectCount = list.getSelectedIndices().length;
337 editNodeAction.setEnabled(selectCount == 1);
338 deleteNodeAction.setEnabled(selectCount >= 1);
342 * List model for the {@link ManageNodesDialog#nodeList}. TODO
344 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
347 private class NodeListModel extends AbstractListModel implements Iterable<Node> {
349 /** The list of nodes. */
350 @SuppressWarnings("hiding")
351 private final List<Node> nodeList = new ArrayList<Node>();
354 * Creates a new node list model.
356 public NodeListModel() {
360 * Adds the given node to the list model.
362 * @see Collection#add(Object)
366 public void addNode(Node node) {
368 fireIntervalAdded(this, nodeList.size() - 1, nodeList.size() - 1);
372 * Removes the given node from the list model.
374 * @see Collection#remove(Object)
378 public void removeNode(Node node) {
379 int nodeIndex = nodeList.indexOf(node);
380 nodeList.remove(node);
381 fireIntervalRemoved(this, nodeIndex, nodeIndex);
385 * Removes all nodes from the list model.
387 * @see Collection#clear()
389 public void clear() {
390 int nodeCount = nodeList.size();
393 fireIntervalRemoved(this, 0, nodeCount - 1);
400 public Iterator<Node> iterator() {
401 return nodeList.iterator();
407 @SuppressWarnings("synthetic-access")
408 public Object getElementAt(int index) {
409 return nodeList.get(index);
415 public int getSize() {
416 return nodeList.size();