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.JFrame;
36 import javax.swing.JList;
37 import javax.swing.JOptionPane;
38 import javax.swing.JPanel;
39 import javax.swing.JScrollPane;
40 import javax.swing.border.EtchedBorder;
41 import javax.swing.event.ListSelectionEvent;
42 import javax.swing.event.ListSelectionListener;
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 {
57 /** The original list of nodes. */
58 private List<Node> originalNodeList;
60 /** The “add node” action. */
61 private Action addNodeAction;
63 /** The “edit node” action. */
64 private Action editNodeAction;
66 /** The “delete node” action. */
67 private Action deleteNodeAction;
69 /** The “okay” action. */
70 private Action okayAction;
72 /** The “cancel” action. */
73 private Action cancelAction;
75 /** The “edit node” dialog. */
76 private EditNodeDialog editNodeDialog;
79 private JList nodeList;
81 /** The mode for the node list. */
82 private NodeListModel nodeListModel = new NodeListModel();
85 * Creates a new node manager dialog.
88 * The parent frame of the dialog
90 public ManageNodesDialog(JFrame parentFrame) {
91 super(parentFrame, I18n.get("manageNodesDialog.title") + " – jSite " + Version.getVersion(), true);
96 SwingUtils.center(this);
104 * Returns the list of nodes.
106 * @return The list of nodes
108 public List<Node> getNodeList() {
109 return originalNodeList;
113 * Sets the list of nodes.
118 public void setNodeList(List<Node> nodeList) {
119 originalNodeList = nodeList;
120 nodeListModel.clear();
121 for (Node node: nodeList) {
122 nodeListModel.addNode(node);
131 * Initializes all actions.
133 private void initActions() {
134 okayAction = new I18nAction("general.button.okay") {
139 @SuppressWarnings("synthetic-access")
140 public void actionPerformed(ActionEvent e) {
144 cancelAction = new I18nAction("general.button.cancel") {
149 @SuppressWarnings("synthetic-access")
150 public void actionPerformed(ActionEvent e) {
154 addNodeAction = new I18nAction("manageNodesDialog.button.addNode") {
159 @SuppressWarnings("synthetic-access")
160 public void actionPerformed(ActionEvent e) {
164 editNodeAction = new I18nAction("manageNodesDialog.button.editNode", false) {
169 @SuppressWarnings("synthetic-access")
170 public void actionPerformed(ActionEvent e) {
174 deleteNodeAction = new I18nAction("manageNodesDialog.button.deleteNode", false) {
179 @SuppressWarnings("synthetic-access")
180 public void actionPerformed(ActionEvent e) {
187 * Initializes all components.
189 private void initComponents() {
190 JPanel rootPanel = new JPanel(new BorderLayout(12, 12));
191 rootPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
193 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
194 rootPanel.add(buttonPanel, BorderLayout.PAGE_END);
195 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
197 buttonPanel.add(new JButton(cancelAction));
198 JButton okayButton = new JButton(okayAction);
199 getRootPane().setDefaultButton(okayButton);
200 buttonPanel.add(okayButton);
202 JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
203 rootPanel.add(contentPanel, BorderLayout.CENTER);
204 contentPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
206 JPanel listButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 12, 12));
207 contentPanel.add(listButtonPanel, BorderLayout.PAGE_END);
208 listButtonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
209 listButtonPanel.add(new JButton(addNodeAction));
210 listButtonPanel.add(new JButton(editNodeAction));
211 listButtonPanel.add(new JButton(deleteNodeAction));
213 nodeList = new JList(nodeListModel);
214 nodeList.addListSelectionListener(this);
215 contentPanel.add(new JScrollPane(nodeList), BorderLayout.CENTER);
217 setContentPane(rootPanel);
221 * Initializes all child dialogs.
223 private void initDialogs() {
224 editNodeDialog = new EditNodeDialog(this);
232 * Adds a new node via {@link #editNodeDialog}.
234 private void addNode() {
235 editNodeDialog.setNodeName("New Node");
236 editNodeDialog.setNodeHostname("localhost");
237 editNodeDialog.setNodePort(9481);
238 editNodeDialog.setNodeOnSameMachine(true);
239 editNodeDialog.setVisible(true);
240 if (!editNodeDialog.wasCancelled()) {
241 Node newNode = new Node();
242 newNode.setName(editNodeDialog.getNodeName());
243 newNode.setHostname(editNodeDialog.getNodeHostname());
244 newNode.setPort(editNodeDialog.getNodePort());
245 nodeListModel.addNode(newNode);
250 * Edits a node via {@link #editNodeDialog}.
252 private void editNode() {
253 Node selectedNode = (Node) nodeList.getSelectedValue();
254 editNodeDialog.setNodeName(selectedNode.getName());
255 editNodeDialog.setNodeHostname(selectedNode.getHostname());
256 editNodeDialog.setNodePort(selectedNode.getPort());
257 editNodeDialog.setNodeOnSameMachine(selectedNode.isSameMachine());
258 editNodeDialog.setVisible(true);
259 if (!editNodeDialog.wasCancelled()) {
260 selectedNode.setName(editNodeDialog.getNodeName());
261 selectedNode.setHostname(editNodeDialog.getNodeHostname());
262 selectedNode.setPort(editNodeDialog.getNodePort());
263 selectedNode.setSameMachine(editNodeDialog.isNodeOnSameMachine());
269 * Deletes the selected node.
271 private void deleteNodes() {
272 Object[] selectedNodes = nodeList.getSelectedValues();
273 for (Object node: selectedNodes) {
274 nodeListModel.removeNode((Node) node);
276 nodeList.clearSelection();
280 * Checks whether the list of nodes is not empty.
282 * @return <code>true</code> if there is at least one node defined,
283 * <code>false</code> otherwise
285 private boolean verifyNodesExist() {
286 return nodeListModel.getSize() > 0;
290 * This method is called when the “okay” button is pressed. The nodes from
291 * the list are read and the {@link #originalNodeList} member is set so that
292 * the calling code can use {@link #getNodeList()} to get the changed
295 private void confirm() {
296 if (!verifyNodesExist()) {
297 JOptionPane.showMessageDialog(this, I18n.get("manageNodesDialog.error.nodeListEmpty.message"), I18n.get("manageNodesDialog.error.nodeListEmpty.title"), JOptionPane.ERROR_MESSAGE);
300 originalNodeList.clear();
301 for (Node node: nodeListModel) {
302 originalNodeList.add(node);
308 * Cancels the dialog.
310 private void cancel() {
315 // INTERFACE ListSelectionListener
321 public void valueChanged(ListSelectionEvent listSelectionEvent) {
322 JList list = (JList) listSelectionEvent.getSource();
323 int selectCount = list.getSelectedIndices().length;
324 editNodeAction.setEnabled(selectCount == 1);
325 deleteNodeAction.setEnabled(selectCount >= 1);
329 * List model for the {@link ManageNodesDialog#nodeList}. TODO
331 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
334 private class NodeListModel extends AbstractListModel implements Iterable<Node> {
336 /** The list of nodes. */
337 @SuppressWarnings("hiding")
338 private final List<Node> nodeList = new ArrayList<Node>();
341 * Creates a new node list model.
343 public NodeListModel() {
347 * Adds the given node to the list model.
349 * @see Collection#add(Object)
353 public void addNode(Node node) {
355 fireIntervalAdded(this, nodeList.size() - 1, nodeList.size() - 1);
359 * Removes the given node from the list model.
361 * @see Collection#remove(Object)
365 public void removeNode(Node node) {
366 int nodeIndex = nodeList.indexOf(node);
367 nodeList.remove(node);
368 fireIntervalRemoved(this, nodeIndex, nodeIndex);
372 * Removes all nodes from the list model.
374 * @see Collection#clear()
376 public void clear() {
377 int nodeCount = nodeList.size();
380 fireIntervalRemoved(this, 0, nodeCount - 1);
387 public Iterator<Node> iterator() {
388 return nodeList.iterator();
394 @SuppressWarnings("synthetic-access")
395 public Object getElementAt(int index) {
396 return nodeList.get(index);
402 public int getSize() {
403 return nodeList.size();