f4c482204dfac6173e7b906c93fa48a70737b755
[jSite2.git] / src / net / pterodactylus / jsite / gui / ManageNodesDialog.java
1 /*
2  * jSite2 - ManageNodeDialog.java -
3  * Copyright © 2008 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.jsite.gui;
21
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;
29
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;
41
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;
49
50 /**
51  * Dialog that lets the user manage her nodes.
52  *
53  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
54  * @version $Id$
55  */
56 public class ManageNodesDialog extends JDialog implements ListSelectionListener, I18nable {
57
58         /** The core. */
59         private final Core core;
60
61         /** The original list of nodes. */
62         private List<Node> originalNodeList;
63
64         /** The “add node” action. */
65         private I18nAction addNodeAction;
66
67         /** The “edit node” action. */
68         private I18nAction editNodeAction;
69
70         /** The “delete node” action. */
71         private I18nAction deleteNodeAction;
72
73         /** The “okay” action. */
74         private I18nAction okayAction;
75
76         /** The “cancel” action. */
77         private I18nAction cancelAction;
78
79         /** The “edit node” dialog. */
80         private EditNodeDialog editNodeDialog;
81
82         /** The node list. */
83         private JList nodeList;
84
85         /** The mode for the node list. */
86         private NodeListModel nodeListModel = new NodeListModel();
87
88         /**
89          * Creates a new node manager dialog.
90          *
91          * @param swingInterface
92          *            The Swing interface
93          */
94         public ManageNodesDialog(SwingInterface swingInterface) {
95                 super(swingInterface.getMainWindow(), I18n.get("manageNodesDialog.title") + " – jSite " + Version.getVersion(), true);
96                 this.core = swingInterface.getCore();
97                 initActions();
98                 initComponents();
99                 initDialogs();
100                 pack();
101                 I18n.registerI18nable(this);
102                 SwingUtils.center(this);
103         }
104
105         //
106         // ACCESSORS
107         //
108
109         /**
110          * Returns the list of nodes.
111          *
112          * @return The list of nodes
113          */
114         public List<Node> getNodeList() {
115                 return originalNodeList;
116         }
117
118         /**
119          * Sets the list of nodes.
120          *
121          * @param nodeList
122          *            The list of nodes
123          */
124         public void setNodeList(List<Node> nodeList) {
125                 originalNodeList = nodeList;
126                 nodeListModel.clear();
127                 for (Node node: nodeList) {
128                         nodeListModel.addNode(node);
129                 }
130         }
131
132         //
133         // PRIVATE METHODS
134         //
135
136         /**
137          * Initializes all actions.
138          */
139         private void initActions() {
140                 okayAction = new I18nAction("general.button.okay") {
141
142                         /**
143                          * {@inheritDoc}
144                          */
145                         @SuppressWarnings("synthetic-access")
146                         public void actionPerformed(ActionEvent e) {
147                                 confirm();
148                         }
149                 };
150                 cancelAction = new I18nAction("general.button.cancel") {
151
152                         /**
153                          * {@inheritDoc}
154                          */
155                         @SuppressWarnings("synthetic-access")
156                         public void actionPerformed(ActionEvent e) {
157                                 cancel();
158                         }
159                 };
160                 addNodeAction = new I18nAction("manageNodesDialog.button.addNode") {
161
162                         /**
163                          * {@inheritDoc}
164                          */
165                         @SuppressWarnings("synthetic-access")
166                         public void actionPerformed(ActionEvent e) {
167                                 addNode();
168                         }
169                 };
170                 editNodeAction = new I18nAction("manageNodesDialog.button.editNode", false) {
171
172                         /**
173                          * {@inheritDoc}
174                          */
175                         @SuppressWarnings("synthetic-access")
176                         public void actionPerformed(ActionEvent e) {
177                                 editNode();
178                         }
179                 };
180                 deleteNodeAction = new I18nAction("manageNodesDialog.button.deleteNode", false) {
181
182                         /**
183                          * {@inheritDoc}
184                          */
185                         @SuppressWarnings("synthetic-access")
186                         public void actionPerformed(ActionEvent e) {
187                                 deleteNodes();
188                         }
189                 };
190         }
191
192         /**
193          * Initializes all components.
194          */
195         private void initComponents() {
196                 JPanel rootPanel = new JPanel(new BorderLayout(12, 12));
197                 rootPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
198
199                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
200                 rootPanel.add(buttonPanel, BorderLayout.PAGE_END);
201                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
202
203                 buttonPanel.add(new JButton(cancelAction));
204                 JButton okayButton = new JButton(okayAction);
205                 getRootPane().setDefaultButton(okayButton);
206                 buttonPanel.add(okayButton);
207
208                 JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
209                 rootPanel.add(contentPanel, BorderLayout.CENTER);
210                 contentPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
211
212                 JPanel listButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 12, 12));
213                 contentPanel.add(listButtonPanel, BorderLayout.PAGE_END);
214                 listButtonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
215                 listButtonPanel.add(new JButton(addNodeAction));
216                 listButtonPanel.add(new JButton(editNodeAction));
217                 listButtonPanel.add(new JButton(deleteNodeAction));
218
219                 nodeList = new JList(nodeListModel);
220                 nodeList.addListSelectionListener(this);
221                 contentPanel.add(new JScrollPane(nodeList), BorderLayout.CENTER);
222
223                 setContentPane(rootPanel);
224         }
225
226         /**
227          * Initializes all child dialogs.
228          */
229         private void initDialogs() {
230                 editNodeDialog = new EditNodeDialog(this);
231         }
232
233         //
234         // PRIVATE ACTIONS
235         //
236
237         /**
238          * Adds a new node via {@link #editNodeDialog}.
239          */
240         private void addNode() {
241                 editNodeDialog.setNodeName("New Node");
242                 editNodeDialog.setNodeHostname("localhost");
243                 editNodeDialog.setNodePort(9481);
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);
251                 }
252         }
253
254         /**
255          * Edits a node via {@link #editNodeDialog}.
256          */
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.setVisible(true);
263                 if (!editNodeDialog.wasCancelled()) {
264                         selectedNode.setName(editNodeDialog.getNodeName());
265                         selectedNode.setHostname(editNodeDialog.getNodeHostname());
266                         selectedNode.setPort(editNodeDialog.getNodePort());
267                         nodeList.repaint();
268                 }
269         }
270
271         /**
272          * Deletes the selected node.
273          */
274         private void deleteNodes() {
275                 Object[] selectedNodes = nodeList.getSelectedValues();
276                 for (Object node: selectedNodes) {
277                         Node selectedNode = (Node) node;
278                         if (core.isNodeConnected(selectedNode)) {
279                                 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);
280                                 if (response == JOptionPane.CANCEL_OPTION) {
281                                         break;
282                                 } else if (response == JOptionPane.NO_OPTION) {
283                                         continue;
284                                 }
285                         }
286                         nodeListModel.removeNode(selectedNode);
287                 }
288                 nodeList.clearSelection();
289         }
290
291         /**
292          * Checks whether the list of nodes is not empty.
293          *
294          * @return <code>true</code> if there is at least one node defined,
295          *         <code>false</code> otherwise
296          */
297         private boolean verifyNodesExist() {
298                 return nodeListModel.getSize() > 0;
299         }
300
301         /**
302          * This method is called when the “okay” button is pressed. The nodes from
303          * the list are read and the {@link #originalNodeList} member is set so that
304          * the calling code can use {@link #getNodeList()} to get the changed
305          * values.
306          */
307         private void confirm() {
308                 if (!verifyNodesExist()) {
309                         JOptionPane.showMessageDialog(this, I18n.get("manageNodesDialog.error.nodeListEmpty.message"), I18n.get("manageNodesDialog.error.nodeListEmpty.title"), JOptionPane.ERROR_MESSAGE);
310                         return;
311                 }
312                 originalNodeList.clear();
313                 for (Node node: nodeListModel) {
314                         originalNodeList.add(node);
315                 }
316                 setVisible(false);
317         }
318
319         /**
320          * Cancels the dialog.
321          */
322         private void cancel() {
323                 setVisible(false);
324         }
325
326         //
327         // INTERFACE ListSelectionListener
328         //
329
330         /**
331          * {@inheritDoc}
332          */
333         public void valueChanged(ListSelectionEvent listSelectionEvent) {
334                 JList list = (JList) listSelectionEvent.getSource();
335                 int selectCount = list.getSelectedIndices().length;
336                 editNodeAction.setEnabled(selectCount == 1);
337                 deleteNodeAction.setEnabled(selectCount >= 1);
338         }
339
340         //
341         // INTERFACE I18nable
342         //
343
344         /**
345          * {@inheritDoc}
346          */
347         public void updateI18n() {
348                 okayAction.updateI18n();
349                 cancelAction.updateI18n();
350                 addNodeAction.updateI18n();
351                 editNodeAction.updateI18n();
352                 deleteNodeAction.updateI18n();
353                 setTitle(I18n.get("manageNodesDialog.title") + " – jSite " + Version.getVersion());
354                 SwingUtils.repackCentered(this);
355         }
356
357         /**
358          * List model for the {@link ManageNodesDialog#nodeList}. TODO
359          *
360          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
361          * @version $Id$
362          */
363         private class NodeListModel extends AbstractListModel implements Iterable<Node> {
364
365                 /** The list of nodes. */
366                 @SuppressWarnings("hiding")
367                 private final List<Node> nodeList = new ArrayList<Node>();
368
369                 /**
370                  * Creates a new node list model.
371                  */
372                 public NodeListModel() {
373                         /* do nothing. */
374                 }
375
376                 /**
377                  * Adds the given node to the list model.
378                  *
379                  * @see Collection#add(Object)
380                  * @param node
381                  *            The node to add
382                  */
383                 public void addNode(Node node) {
384                         nodeList.add(node);
385                         fireIntervalAdded(this, nodeList.size() - 1, nodeList.size() - 1);
386                 }
387
388                 /**
389                  * Removes the given node from the list model.
390                  *
391                  * @see Collection#remove(Object)
392                  * @param node
393                  *            The node to remove
394                  */
395                 public void removeNode(Node node) {
396                         int nodeIndex = nodeList.indexOf(node);
397                         nodeList.remove(node);
398                         fireIntervalRemoved(this, nodeIndex, nodeIndex);
399                 }
400
401                 /**
402                  * Removes all nodes from the list model.
403                  *
404                  * @see Collection#clear()
405                  */
406                 public void clear() {
407                         int nodeCount = nodeList.size();
408                         if (nodeCount > 0) {
409                                 nodeList.clear();
410                                 fireIntervalRemoved(this, 0, nodeCount - 1);
411                         }
412                 }
413
414                 /**
415                  * {@inheritDoc}
416                  */
417                 public Iterator<Node> iterator() {
418                         return nodeList.iterator();
419                 }
420
421                 /**
422                  * {@inheritDoc}
423                  */
424                 @SuppressWarnings("synthetic-access")
425                 public Object getElementAt(int index) {
426                         return nodeList.get(index);
427                 }
428
429                 /**
430                  * {@inheritDoc}
431                  */
432                 public int getSize() {
433                         return nodeList.size();
434                 }
435
436         }
437
438 }