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.i18n.gui.I18nLabel;
48 import net.pterodactylus.jsite.main.Version;
49 import net.pterodactylus.util.swing.SwingUtils;
52 * Dialog that lets the user manage her nodes.
54 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
57 public class ManageNodesDialog extends JDialog implements ListSelectionListener, I18nable {
60 private final Core core;
62 /** The original list of nodes. */
63 private List<Node> originalNodeList;
65 /** The “add node” action. */
66 private I18nAction addNodeAction;
68 /** The “edit node” action. */
69 private I18nAction editNodeAction;
71 /** The “delete node” action. */
72 private I18nAction deleteNodeAction;
74 /** The “okay” action. */
75 private I18nAction okayAction;
77 /** The “cancel” action. */
78 private I18nAction cancelAction;
80 /** The “edit node” dialog. */
81 private EditNodeDialog editNodeDialog;
83 /** The warning label. */
84 private I18nLabel immediatelyEffectiveLabel;
87 private JList nodeList;
89 /** The mode for the node list. */
90 private NodeListModel nodeListModel = new NodeListModel();
93 * Creates a new node manager dialog.
95 * @param swingInterface
98 public ManageNodesDialog(SwingInterface swingInterface) {
99 super(swingInterface.getMainWindow(), I18n.get("manageNodesDialog.title") + " – jSite " + Version.getVersion(), true);
100 this.core = swingInterface.getCore();
105 I18n.registerI18nable(this);
106 SwingUtils.center(this);
114 * Expose the edit node dialog for the simple mode.
116 * @return The edit node dialog
118 EditNodeDialog getEditNodeDialog() {
119 return editNodeDialog;
123 * Returns the list of nodes.
125 * @return The list of nodes
127 public List<Node> getNodeList() {
128 return originalNodeList;
132 * Sets the list of nodes.
137 public void setNodeList(List<Node> nodeList) {
138 originalNodeList = new ArrayList<Node>(nodeList);
139 nodeListModel.clear();
140 for (Node node: nodeList) {
141 nodeListModel.addNode(node);
150 * Initializes all actions.
152 private void initActions() {
153 okayAction = new I18nAction("general.button.okay") {
158 @SuppressWarnings("synthetic-access")
159 public void actionPerformed(ActionEvent e) {
163 cancelAction = new I18nAction("general.button.cancel") {
168 @SuppressWarnings("synthetic-access")
169 public void actionPerformed(ActionEvent e) {
173 addNodeAction = new I18nAction("manageNodesDialog.button.addNode") {
178 @SuppressWarnings("synthetic-access")
179 public void actionPerformed(ActionEvent e) {
183 editNodeAction = new I18nAction("manageNodesDialog.button.editNode", false) {
188 @SuppressWarnings("synthetic-access")
189 public void actionPerformed(ActionEvent e) {
193 deleteNodeAction = new I18nAction("manageNodesDialog.button.deleteNode", false) {
198 @SuppressWarnings("synthetic-access")
199 public void actionPerformed(ActionEvent e) {
206 * Initializes all components.
208 private void initComponents() {
209 JPanel rootPanel = new JPanel(new BorderLayout(12, 12));
210 rootPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
212 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
213 rootPanel.add(buttonPanel, BorderLayout.PAGE_END);
214 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
216 buttonPanel.add(new JButton(cancelAction));
217 JButton okayButton = new JButton(okayAction);
218 getRootPane().setDefaultButton(okayButton);
219 buttonPanel.add(okayButton);
221 JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
222 rootPanel.add(contentPanel, BorderLayout.CENTER);
223 contentPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
225 JPanel warningPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 12, 12));
226 warningPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
227 warningPanel.add(immediatelyEffectiveLabel = new I18nLabel("manageNodesDialog.label.immediatelyEffective"));
228 contentPanel.add(warningPanel, BorderLayout.PAGE_START);
230 JPanel listButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 12, 12));
231 contentPanel.add(listButtonPanel, BorderLayout.PAGE_END);
232 listButtonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
233 listButtonPanel.add(new JButton(addNodeAction));
234 listButtonPanel.add(new JButton(editNodeAction));
235 listButtonPanel.add(new JButton(deleteNodeAction));
237 nodeList = new JList(nodeListModel);
238 nodeList.addListSelectionListener(this);
239 contentPanel.add(new JScrollPane(nodeList), BorderLayout.CENTER);
241 setContentPane(rootPanel);
245 * Initializes all child dialogs.
247 private void initDialogs() {
248 editNodeDialog = new EditNodeDialog(this);
256 * Adds a new node via {@link #editNodeDialog}.
258 private void addNode() {
259 editNodeDialog.setNodeName(I18n.get("general.newNode.name"));
260 editNodeDialog.setNodeHostname("localhost");
261 editNodeDialog.setNodePort(9481);
262 editNodeDialog.setVisible(true);
263 if (!editNodeDialog.wasCancelled()) {
264 Node newNode = new Node();
265 newNode.setName(editNodeDialog.getNodeName());
266 newNode.setHostname(editNodeDialog.getNodeHostname());
267 newNode.setPort(editNodeDialog.getNodePort());
268 nodeListModel.addNode(newNode);
273 * Edits a node via {@link #editNodeDialog}.
275 private void editNode() {
276 Node selectedNode = (Node) nodeList.getSelectedValue();
277 editNodeDialog.setNodeName(selectedNode.getName());
278 editNodeDialog.setNodeHostname(selectedNode.getHostname());
279 editNodeDialog.setNodePort(selectedNode.getPort());
280 editNodeDialog.setVisible(true);
281 if (!editNodeDialog.wasCancelled()) {
282 selectedNode.setName(editNodeDialog.getNodeName());
283 selectedNode.setHostname(editNodeDialog.getNodeHostname());
284 selectedNode.setPort(editNodeDialog.getNodePort());
290 * Deletes the selected node.
292 private void deleteNodes() {
293 Object[] selectedNodes = nodeList.getSelectedValues();
294 for (Object node: selectedNodes) {
295 Node selectedNode = (Node) node;
296 if (core.isNodeConnected(selectedNode)) {
297 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);
298 if (response == JOptionPane.CANCEL_OPTION) {
300 } else if (response == JOptionPane.NO_OPTION) {
304 nodeListModel.removeNode(selectedNode);
306 nodeList.clearSelection();
310 * Checks whether the list of nodes is not empty.
312 * @return <code>true</code> if there is at least one node defined,
313 * <code>false</code> otherwise
315 private boolean verifyNodesExist() {
316 return nodeListModel.getSize() > 0;
320 * This method is called when the “okay” button is pressed. The nodes from
321 * the list are read and the {@link #originalNodeList} member is set so that
322 * the calling code can use {@link #getNodeList()} to get the changed
325 private void confirm() {
326 if (!verifyNodesExist()) {
327 JOptionPane.showMessageDialog(this, I18n.get("manageNodesDialog.error.nodeListEmpty.message"), I18n.get("manageNodesDialog.error.nodeListEmpty.title"), JOptionPane.ERROR_MESSAGE);
330 originalNodeList.clear();
331 for (Node node: nodeListModel) {
332 originalNodeList.add(node);
338 * Cancels the dialog.
340 private void cancel() {
345 // INTERFACE ListSelectionListener
351 public void valueChanged(ListSelectionEvent listSelectionEvent) {
352 JList list = (JList) listSelectionEvent.getSource();
353 int selectCount = list.getSelectedIndices().length;
354 editNodeAction.setEnabled(selectCount == 1);
355 deleteNodeAction.setEnabled(selectCount >= 1);
359 // INTERFACE I18nable
365 public void updateI18n() {
366 okayAction.updateI18n();
367 cancelAction.updateI18n();
368 addNodeAction.updateI18n();
369 editNodeAction.updateI18n();
370 deleteNodeAction.updateI18n();
371 immediatelyEffectiveLabel.updateI18n();
372 setTitle(I18n.get("manageNodesDialog.title") + " – jSite " + Version.getVersion());
373 SwingUtils.repackCentered(this);
377 * List model for the {@link ManageNodesDialog#nodeList}. TODO
379 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
382 private class NodeListModel extends AbstractListModel implements Iterable<Node> {
384 /** The list of nodes. */
385 @SuppressWarnings("hiding")
386 private final List<Node> nodeList = new ArrayList<Node>();
389 * Creates a new node list model.
391 public NodeListModel() {
396 * Adds the given node to the list model.
398 * @see Collection#add(Object)
402 public void addNode(Node node) {
404 fireIntervalAdded(this, nodeList.size() - 1, nodeList.size() - 1);
408 * Removes the given node from the list model.
410 * @see Collection#remove(Object)
414 public void removeNode(Node node) {
415 int nodeIndex = nodeList.indexOf(node);
416 nodeList.remove(node);
417 fireIntervalRemoved(this, nodeIndex, nodeIndex);
421 * Removes all nodes from the list model.
423 * @see Collection#clear()
425 public void clear() {
426 int nodeCount = nodeList.size();
429 fireIntervalRemoved(this, 0, nodeCount - 1);
436 public Iterator<Node> iterator() {
437 return nodeList.iterator();
443 @SuppressWarnings("synthetic-access")
444 public Object getElementAt(int index) {
445 return nodeList.get(index);
451 public int getSize() {
452 return nodeList.size();