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.BorderFactory;
32 import javax.swing.JButton;
33 import javax.swing.JDialog;
34 import javax.swing.JList;
35 import javax.swing.JOptionPane;
36 import javax.swing.JPanel;
37 import javax.swing.JScrollPane;
38 import javax.swing.border.EtchedBorder;
39 import javax.swing.event.ListSelectionEvent;
40 import javax.swing.event.ListSelectionListener;
42 import net.pterodactylus.jsite.core.Core;
43 import net.pterodactylus.jsite.core.Node;
44 import net.pterodactylus.jsite.i18n.I18n;
45 import net.pterodactylus.jsite.i18n.I18nable;
46 import net.pterodactylus.jsite.i18n.gui.I18nAction;
47 import net.pterodactylus.jsite.main.Version;
48 import net.pterodactylus.util.swing.SwingUtils;
51 * Dialog that lets the user manage her nodes.
53 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
56 public class ManageNodesDialog extends JDialog implements ListSelectionListener, I18nable {
59 private final Core core;
61 /** The original list of nodes. */
62 private List<Node> originalNodeList;
64 /** The “add node” action. */
65 private I18nAction addNodeAction;
67 /** The “edit node” action. */
68 private I18nAction editNodeAction;
70 /** The “delete node” action. */
71 private I18nAction deleteNodeAction;
73 /** The “okay” action. */
74 private I18nAction okayAction;
76 /** The “cancel” action. */
77 private I18nAction cancelAction;
79 /** The “edit node” dialog. */
80 private EditNodeDialog editNodeDialog;
83 private JList nodeList;
85 /** The mode for the node list. */
86 private NodeListModel nodeListModel = new NodeListModel();
89 * Creates a new node manager dialog.
91 * @param swingInterface
94 public ManageNodesDialog(SwingInterface swingInterface) {
95 super(swingInterface.getMainWindow(), I18n.get("manageNodesDialog.title") + " – jSite " + Version.getVersion(), true);
96 this.core = swingInterface.getCore();
101 I18n.registerI18nable(this);
102 SwingUtils.center(this);
110 * Expose the edit node dialog for the simple mode.
112 * @return The edit node dialog
114 EditNodeDialog getEditNodeDialog() {
115 return editNodeDialog;
119 * Returns the list of nodes.
121 * @return The list of nodes
123 public List<Node> getNodeList() {
124 return originalNodeList;
128 * Sets the list of nodes.
133 public void setNodeList(List<Node> nodeList) {
134 originalNodeList = new ArrayList<Node>(nodeList);
135 nodeListModel.clear();
136 for (Node node: nodeList) {
137 nodeListModel.addNode(node);
146 * Initializes all actions.
148 private void initActions() {
149 okayAction = new I18nAction("general.button.okay") {
154 @SuppressWarnings("synthetic-access")
155 public void actionPerformed(ActionEvent e) {
159 cancelAction = new I18nAction("general.button.cancel") {
164 @SuppressWarnings("synthetic-access")
165 public void actionPerformed(ActionEvent e) {
169 addNodeAction = new I18nAction("manageNodesDialog.button.addNode") {
174 @SuppressWarnings("synthetic-access")
175 public void actionPerformed(ActionEvent e) {
179 editNodeAction = new I18nAction("manageNodesDialog.button.editNode", false) {
184 @SuppressWarnings("synthetic-access")
185 public void actionPerformed(ActionEvent e) {
189 deleteNodeAction = new I18nAction("manageNodesDialog.button.deleteNode", false) {
194 @SuppressWarnings("synthetic-access")
195 public void actionPerformed(ActionEvent e) {
202 * Initializes all components.
204 private void initComponents() {
205 JPanel rootPanel = new JPanel(new BorderLayout(12, 12));
206 rootPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
208 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
209 rootPanel.add(buttonPanel, BorderLayout.PAGE_END);
210 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
212 buttonPanel.add(new JButton(cancelAction));
213 JButton okayButton = new JButton(okayAction);
214 getRootPane().setDefaultButton(okayButton);
215 buttonPanel.add(okayButton);
217 JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
218 rootPanel.add(contentPanel, BorderLayout.CENTER);
219 contentPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
221 JPanel listButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 12, 12));
222 contentPanel.add(listButtonPanel, BorderLayout.PAGE_END);
223 listButtonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
224 listButtonPanel.add(new JButton(addNodeAction));
225 listButtonPanel.add(new JButton(editNodeAction));
226 listButtonPanel.add(new JButton(deleteNodeAction));
228 nodeList = new JList(nodeListModel);
229 nodeList.addListSelectionListener(this);
230 contentPanel.add(new JScrollPane(nodeList), BorderLayout.CENTER);
232 setContentPane(rootPanel);
236 * Initializes all child dialogs.
238 private void initDialogs() {
239 editNodeDialog = new EditNodeDialog(this);
247 * Adds a new node via {@link #editNodeDialog}.
249 private void addNode() {
250 editNodeDialog.setNodeName(I18n.get("general.newNode.name"));
251 editNodeDialog.setNodeHostname("localhost");
252 editNodeDialog.setNodePort(9481);
253 editNodeDialog.setVisible(true);
254 if (!editNodeDialog.wasCancelled()) {
255 Node newNode = new Node();
256 newNode.setName(editNodeDialog.getNodeName());
257 newNode.setHostname(editNodeDialog.getNodeHostname());
258 newNode.setPort(editNodeDialog.getNodePort());
259 nodeListModel.addNode(newNode);
264 * Edits a node via {@link #editNodeDialog}.
266 private void editNode() {
267 Node selectedNode = (Node) nodeList.getSelectedValue();
268 editNodeDialog.setNodeName(selectedNode.getName());
269 editNodeDialog.setNodeHostname(selectedNode.getHostname());
270 editNodeDialog.setNodePort(selectedNode.getPort());
271 editNodeDialog.setVisible(true);
272 if (!editNodeDialog.wasCancelled()) {
273 selectedNode.setName(editNodeDialog.getNodeName());
274 selectedNode.setHostname(editNodeDialog.getNodeHostname());
275 selectedNode.setPort(editNodeDialog.getNodePort());
281 * Deletes the selected node.
283 private void deleteNodes() {
284 Object[] selectedNodes = nodeList.getSelectedValues();
285 for (Object node: selectedNodes) {
286 Node selectedNode = (Node) node;
287 if (core.isNodeConnected(selectedNode)) {
288 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);
289 if (response == JOptionPane.CANCEL_OPTION) {
291 } else if (response == JOptionPane.NO_OPTION) {
295 nodeListModel.removeNode(selectedNode);
297 nodeList.clearSelection();
301 * Checks whether the list of nodes is not empty.
303 * @return <code>true</code> if there is at least one node defined,
304 * <code>false</code> otherwise
306 private boolean verifyNodesExist() {
307 return nodeListModel.getSize() > 0;
311 * This method is called when the “okay” button is pressed. The nodes from
312 * the list are read and the {@link #originalNodeList} member is set so that
313 * the calling code can use {@link #getNodeList()} to get the changed
316 private void confirm() {
317 if (!verifyNodesExist()) {
318 JOptionPane.showMessageDialog(this, I18n.get("manageNodesDialog.error.nodeListEmpty.message"), I18n.get("manageNodesDialog.error.nodeListEmpty.title"), JOptionPane.ERROR_MESSAGE);
321 originalNodeList.clear();
322 for (Node node: nodeListModel) {
323 originalNodeList.add(node);
329 * Cancels the dialog.
331 private void cancel() {
336 // INTERFACE ListSelectionListener
342 public void valueChanged(ListSelectionEvent listSelectionEvent) {
343 JList list = (JList) listSelectionEvent.getSource();
344 int selectCount = list.getSelectedIndices().length;
345 editNodeAction.setEnabled(selectCount == 1);
346 deleteNodeAction.setEnabled(selectCount >= 1);
350 // INTERFACE I18nable
356 public void updateI18n() {
357 okayAction.updateI18n();
358 cancelAction.updateI18n();
359 addNodeAction.updateI18n();
360 editNodeAction.updateI18n();
361 deleteNodeAction.updateI18n();
362 setTitle(I18n.get("manageNodesDialog.title") + " – jSite " + Version.getVersion());
363 SwingUtils.repackCentered(this);
367 * List model for the {@link ManageNodesDialog#nodeList}. TODO
369 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
372 private class NodeListModel extends AbstractListModel implements Iterable<Node> {
374 /** The list of nodes. */
375 @SuppressWarnings("hiding")
376 private final List<Node> nodeList = new ArrayList<Node>();
379 * Creates a new node list model.
381 public NodeListModel() {
386 * Adds the given node to the list model.
388 * @see Collection#add(Object)
392 public void addNode(Node node) {
394 fireIntervalAdded(this, nodeList.size() - 1, nodeList.size() - 1);
398 * Removes the given node from the list model.
400 * @see Collection#remove(Object)
404 public void removeNode(Node node) {
405 int nodeIndex = nodeList.indexOf(node);
406 nodeList.remove(node);
407 fireIntervalRemoved(this, nodeIndex, nodeIndex);
411 * Removes all nodes from the list model.
413 * @see Collection#clear()
415 public void clear() {
416 int nodeCount = nodeList.size();
419 fireIntervalRemoved(this, 0, nodeCount - 1);
426 public Iterator<Node> iterator() {
427 return nodeList.iterator();
433 @SuppressWarnings("synthetic-access")
434 public Object getElementAt(int index) {
435 return nodeList.get(index);
441 public int getSize() {
442 return nodeList.size();