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.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, I18nable {
58 private final Core core;
60 /** The original list of nodes. */
61 private List<Node> originalNodeList;
63 /** The “add node” action. */
64 private I18nAction addNodeAction;
66 /** The “edit node” action. */
67 private I18nAction editNodeAction;
69 /** The “delete node” action. */
70 private I18nAction deleteNodeAction;
72 /** The “okay” action. */
73 private I18nAction okayAction;
75 /** The “cancel” action. */
76 private I18nAction 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 I18n.registerI18nable(this);
101 SwingUtils.center(this);
109 * Returns the list of nodes.
111 * @return The list of nodes
113 public List<Node> getNodeList() {
114 return originalNodeList;
118 * Sets the list of nodes.
123 public void setNodeList(List<Node> nodeList) {
124 originalNodeList = nodeList;
125 nodeListModel.clear();
126 for (Node node: nodeList) {
127 nodeListModel.addNode(node);
136 * Initializes all actions.
138 private void initActions() {
139 okayAction = new I18nAction("general.button.okay") {
144 @SuppressWarnings("synthetic-access")
145 public void actionPerformed(ActionEvent e) {
149 cancelAction = new I18nAction("general.button.cancel") {
154 @SuppressWarnings("synthetic-access")
155 public void actionPerformed(ActionEvent e) {
159 addNodeAction = new I18nAction("manageNodesDialog.button.addNode") {
164 @SuppressWarnings("synthetic-access")
165 public void actionPerformed(ActionEvent e) {
169 editNodeAction = new I18nAction("manageNodesDialog.button.editNode", false) {
174 @SuppressWarnings("synthetic-access")
175 public void actionPerformed(ActionEvent e) {
179 deleteNodeAction = new I18nAction("manageNodesDialog.button.deleteNode", false) {
184 @SuppressWarnings("synthetic-access")
185 public void actionPerformed(ActionEvent e) {
192 * Initializes all components.
194 private void initComponents() {
195 JPanel rootPanel = new JPanel(new BorderLayout(12, 12));
196 rootPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
198 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
199 rootPanel.add(buttonPanel, BorderLayout.PAGE_END);
200 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
202 buttonPanel.add(new JButton(cancelAction));
203 JButton okayButton = new JButton(okayAction);
204 getRootPane().setDefaultButton(okayButton);
205 buttonPanel.add(okayButton);
207 JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
208 rootPanel.add(contentPanel, BorderLayout.CENTER);
209 contentPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
211 JPanel listButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 12, 12));
212 contentPanel.add(listButtonPanel, BorderLayout.PAGE_END);
213 listButtonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
214 listButtonPanel.add(new JButton(addNodeAction));
215 listButtonPanel.add(new JButton(editNodeAction));
216 listButtonPanel.add(new JButton(deleteNodeAction));
218 nodeList = new JList(nodeListModel);
219 nodeList.addListSelectionListener(this);
220 contentPanel.add(new JScrollPane(nodeList), BorderLayout.CENTER);
222 setContentPane(rootPanel);
226 * Initializes all child dialogs.
228 private void initDialogs() {
229 editNodeDialog = new EditNodeDialog(this);
237 * Adds a new node via {@link #editNodeDialog}.
239 private void addNode() {
240 editNodeDialog.setNodeName("New Node");
241 editNodeDialog.setNodeHostname("localhost");
242 editNodeDialog.setNodePort(9481);
243 editNodeDialog.setNodeOnSameMachine(true);
244 editNodeDialog.setVisible(true);
245 if (!editNodeDialog.wasCancelled()) {
246 Node newNode = new Node();
247 newNode.setName(editNodeDialog.getNodeName());
248 newNode.setHostname(editNodeDialog.getNodeHostname());
249 newNode.setPort(editNodeDialog.getNodePort());
250 nodeListModel.addNode(newNode);
255 * Edits a node via {@link #editNodeDialog}.
257 private void editNode() {
258 Node selectedNode = (Node) nodeList.getSelectedValue();
259 editNodeDialog.setNodeName(selectedNode.getName());
260 editNodeDialog.setNodeHostname(selectedNode.getHostname());
261 editNodeDialog.setNodePort(selectedNode.getPort());
262 editNodeDialog.setNodeOnSameMachine(selectedNode.isSameMachine());
263 editNodeDialog.setVisible(true);
264 if (!editNodeDialog.wasCancelled()) {
265 selectedNode.setName(editNodeDialog.getNodeName());
266 selectedNode.setHostname(editNodeDialog.getNodeHostname());
267 selectedNode.setPort(editNodeDialog.getNodePort());
268 selectedNode.setSameMachine(editNodeDialog.isNodeOnSameMachine());
274 * Deletes the selected node.
276 private void deleteNodes() {
277 Object[] selectedNodes = nodeList.getSelectedValues();
278 for (Object node: selectedNodes) {
279 Node selectedNode = (Node) node;
280 if (core.isNodeConnected(selectedNode)) {
281 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);
282 if (response == JOptionPane.CANCEL_OPTION) {
284 } else if (response == JOptionPane.NO_OPTION) {
288 nodeListModel.removeNode(selectedNode);
290 nodeList.clearSelection();
294 * Checks whether the list of nodes is not empty.
296 * @return <code>true</code> if there is at least one node defined,
297 * <code>false</code> otherwise
299 private boolean verifyNodesExist() {
300 return nodeListModel.getSize() > 0;
304 * This method is called when the “okay” button is pressed. The nodes from
305 * the list are read and the {@link #originalNodeList} member is set so that
306 * the calling code can use {@link #getNodeList()} to get the changed
309 private void confirm() {
310 if (!verifyNodesExist()) {
311 JOptionPane.showMessageDialog(this, I18n.get("manageNodesDialog.error.nodeListEmpty.message"), I18n.get("manageNodesDialog.error.nodeListEmpty.title"), JOptionPane.ERROR_MESSAGE);
314 originalNodeList.clear();
315 for (Node node: nodeListModel) {
316 originalNodeList.add(node);
322 * Cancels the dialog.
324 private void cancel() {
329 // INTERFACE ListSelectionListener
335 public void valueChanged(ListSelectionEvent listSelectionEvent) {
336 JList list = (JList) listSelectionEvent.getSource();
337 int selectCount = list.getSelectedIndices().length;
338 editNodeAction.setEnabled(selectCount == 1);
339 deleteNodeAction.setEnabled(selectCount >= 1);
343 // INTERFACE I18nable
349 public void updateI18n() {
350 okayAction.updateI18n();
351 cancelAction.updateI18n();
352 addNodeAction.updateI18n();
353 editNodeAction.updateI18n();
354 deleteNodeAction.updateI18n();
355 setTitle(I18n.get("manageNodesDialog.title") + " – jSite " + Version.getVersion());
356 SwingUtils.repackCentered(this);
360 * List model for the {@link ManageNodesDialog#nodeList}. TODO
362 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
365 private class NodeListModel extends AbstractListModel implements Iterable<Node> {
367 /** The list of nodes. */
368 @SuppressWarnings("hiding")
369 private final List<Node> nodeList = new ArrayList<Node>();
372 * Creates a new node list model.
374 public NodeListModel() {
378 * Adds the given node to the list model.
380 * @see Collection#add(Object)
384 public void addNode(Node node) {
386 fireIntervalAdded(this, nodeList.size() - 1, nodeList.size() - 1);
390 * Removes the given node from the list model.
392 * @see Collection#remove(Object)
396 public void removeNode(Node node) {
397 int nodeIndex = nodeList.indexOf(node);
398 nodeList.remove(node);
399 fireIntervalRemoved(this, nodeIndex, nodeIndex);
403 * Removes all nodes from the list model.
405 * @see Collection#clear()
407 public void clear() {
408 int nodeCount = nodeList.size();
411 fireIntervalRemoved(this, 0, nodeCount - 1);
418 public Iterator<Node> iterator() {
419 return nodeList.iterator();
425 @SuppressWarnings("synthetic-access")
426 public Object getElementAt(int index) {
427 return nodeList.get(index);
433 public int getSize() {
434 return nodeList.size();