move i18n gui components to subpackage of i18n
[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.setNodeOnSameMachine(true);
245                 editNodeDialog.setVisible(true);
246                 if (!editNodeDialog.wasCancelled()) {
247                         Node newNode = new Node();
248                         newNode.setName(editNodeDialog.getNodeName());
249                         newNode.setHostname(editNodeDialog.getNodeHostname());
250                         newNode.setPort(editNodeDialog.getNodePort());
251                         nodeListModel.addNode(newNode);
252                 }
253         }
254
255         /**
256          * Edits a node via {@link #editNodeDialog}.
257          */
258         private void editNode() {
259                 Node selectedNode = (Node) nodeList.getSelectedValue();
260                 editNodeDialog.setNodeName(selectedNode.getName());
261                 editNodeDialog.setNodeHostname(selectedNode.getHostname());
262                 editNodeDialog.setNodePort(selectedNode.getPort());
263                 editNodeDialog.setNodeOnSameMachine(selectedNode.isSameMachine());
264                 editNodeDialog.setVisible(true);
265                 if (!editNodeDialog.wasCancelled()) {
266                         selectedNode.setName(editNodeDialog.getNodeName());
267                         selectedNode.setHostname(editNodeDialog.getNodeHostname());
268                         selectedNode.setPort(editNodeDialog.getNodePort());
269                         selectedNode.setSameMachine(editNodeDialog.isNodeOnSameMachine());
270                         nodeList.repaint();
271                 }
272         }
273
274         /**
275          * Deletes the selected node.
276          */
277         private void deleteNodes() {
278                 Object[] selectedNodes = nodeList.getSelectedValues();
279                 for (Object node: selectedNodes) {
280                         Node selectedNode = (Node) node;
281                         if (core.isNodeConnected(selectedNode)) {
282                                 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);
283                                 if (response == JOptionPane.CANCEL_OPTION) {
284                                         break;
285                                 } else if (response == JOptionPane.NO_OPTION) {
286                                         continue;
287                                 }
288                         }
289                         nodeListModel.removeNode(selectedNode);
290                 }
291                 nodeList.clearSelection();
292         }
293
294         /**
295          * Checks whether the list of nodes is not empty.
296          * 
297          * @return <code>true</code> if there is at least one node defined,
298          *         <code>false</code> otherwise
299          */
300         private boolean verifyNodesExist() {
301                 return nodeListModel.getSize() > 0;
302         }
303
304         /**
305          * This method is called when the “okay” button is pressed. The nodes from
306          * the list are read and the {@link #originalNodeList} member is set so that
307          * the calling code can use {@link #getNodeList()} to get the changed
308          * values.
309          */
310         private void confirm() {
311                 if (!verifyNodesExist()) {
312                         JOptionPane.showMessageDialog(this, I18n.get("manageNodesDialog.error.nodeListEmpty.message"), I18n.get("manageNodesDialog.error.nodeListEmpty.title"), JOptionPane.ERROR_MESSAGE);
313                         return;
314                 }
315                 originalNodeList.clear();
316                 for (Node node: nodeListModel) {
317                         originalNodeList.add(node);
318                 }
319                 setVisible(false);
320         }
321
322         /**
323          * Cancels the dialog.
324          */
325         private void cancel() {
326                 setVisible(false);
327         }
328
329         //
330         // INTERFACE ListSelectionListener
331         //
332
333         /**
334          * {@inheritDoc}
335          */
336         public void valueChanged(ListSelectionEvent listSelectionEvent) {
337                 JList list = (JList) listSelectionEvent.getSource();
338                 int selectCount = list.getSelectedIndices().length;
339                 editNodeAction.setEnabled(selectCount == 1);
340                 deleteNodeAction.setEnabled(selectCount >= 1);
341         }
342
343         //
344         // INTERFACE I18nable
345         //
346
347         /**
348          * {@inheritDoc}
349          */
350         public void updateI18n() {
351                 okayAction.updateI18n();
352                 cancelAction.updateI18n();
353                 addNodeAction.updateI18n();
354                 editNodeAction.updateI18n();
355                 deleteNodeAction.updateI18n();
356                 setTitle(I18n.get("manageNodesDialog.title") + " – jSite " + Version.getVersion());
357                 SwingUtils.repackCentered(this);
358         }
359
360         /**
361          * List model for the {@link ManageNodesDialog#nodeList}. TODO
362          * 
363          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
364          * @version $Id$
365          */
366         private class NodeListModel extends AbstractListModel implements Iterable<Node> {
367
368                 /** The list of nodes. */
369                 @SuppressWarnings("hiding")
370                 private final List<Node> nodeList = new ArrayList<Node>();
371
372                 /**
373                  * Creates a new node list model.
374                  */
375                 public NodeListModel() {
376                 }
377
378                 /**
379                  * Adds the given node to the list model.
380                  * 
381                  * @see Collection#add(Object)
382                  * @param node
383                  *            The node to add
384                  */
385                 public void addNode(Node node) {
386                         nodeList.add(node);
387                         fireIntervalAdded(this, nodeList.size() - 1, nodeList.size() - 1);
388                 }
389
390                 /**
391                  * Removes the given node from the list model.
392                  * 
393                  * @see Collection#remove(Object)
394                  * @param node
395                  *            The node to remove
396                  */
397                 public void removeNode(Node node) {
398                         int nodeIndex = nodeList.indexOf(node);
399                         nodeList.remove(node);
400                         fireIntervalRemoved(this, nodeIndex, nodeIndex);
401                 }
402
403                 /**
404                  * Removes all nodes from the list model.
405                  * 
406                  * @see Collection#clear()
407                  */
408                 public void clear() {
409                         int nodeCount = nodeList.size();
410                         if (nodeCount > 0) {
411                                 nodeList.clear();
412                                 fireIntervalRemoved(this, 0, nodeCount - 1);
413                         }
414                 }
415
416                 /**
417                  * {@inheritDoc}
418                  */
419                 public Iterator<Node> iterator() {
420                         return nodeList.iterator();
421                 }
422
423                 /**
424                  * {@inheritDoc}
425                  */
426                 @SuppressWarnings("synthetic-access")
427                 public Object getElementAt(int index) {
428                         return nodeList.get(index);
429                 }
430
431                 /**
432                  * {@inheritDoc}
433                  */
434                 public int getSize() {
435                         return nodeList.size();
436                 }
437
438         }
439
440 }