Update year in copyright headers.
[jSite.git] / src / main / java / de / todesbaum / jsite / gui / NodeManagerPage.java
1 /*
2  * jSite - NodeManagerPage.java - Copyright © 2006–2014 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.jsite.gui;
20
21 import java.awt.BorderLayout;
22 import java.awt.Dimension;
23 import java.awt.FlowLayout;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.Insets;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.KeyEvent;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import javax.swing.AbstractAction;
33 import javax.swing.Action;
34 import javax.swing.DefaultListModel;
35 import javax.swing.JButton;
36 import javax.swing.JLabel;
37 import javax.swing.JList;
38 import javax.swing.JOptionPane;
39 import javax.swing.JPanel;
40 import javax.swing.JScrollPane;
41 import javax.swing.JSpinner;
42 import javax.swing.JTextField;
43 import javax.swing.ListSelectionModel;
44 import javax.swing.SpinnerNumberModel;
45 import javax.swing.border.EmptyBorder;
46 import javax.swing.event.ChangeEvent;
47 import javax.swing.event.ChangeListener;
48 import javax.swing.event.DocumentEvent;
49 import javax.swing.event.DocumentListener;
50 import javax.swing.event.ListSelectionEvent;
51 import javax.swing.event.ListSelectionListener;
52 import javax.swing.text.BadLocationException;
53 import javax.swing.text.Document;
54
55 import de.todesbaum.jsite.application.Node;
56 import de.todesbaum.jsite.i18n.I18n;
57 import de.todesbaum.jsite.i18n.I18nContainer;
58 import de.todesbaum.util.swing.TLabel;
59 import de.todesbaum.util.swing.TWizard;
60 import de.todesbaum.util.swing.TWizardPage;
61
62 /**
63  * Wizard page that lets the user edit his nodes.
64  *
65  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
66  */
67 public class NodeManagerPage extends TWizardPage implements ListSelectionListener, DocumentListener, ChangeListener {
68
69         /** List of node manager listeners. */
70         private List<NodeManagerListener> nodeManagerListeners = new ArrayList<NodeManagerListener>();
71
72         /** The “add node” action. */
73         protected Action addNodeAction;
74
75         /** The “delete node” action. */
76         protected Action deleteNodeAction;
77
78         /** The node list model. */
79         private DefaultListModel nodeListModel;
80
81         /** The node list. */
82         private JList nodeList;
83
84         /** The node name textfield. */
85         private JTextField nodeNameTextField;
86
87         /** The node hostname textfield. */
88         private JTextField nodeHostnameTextField;
89
90         /** The spinner for the node port. */
91         private JSpinner nodePortSpinner;
92
93         /**
94          * Creates a new node manager wizard page.
95          *
96          * @param wizard
97          *            The wizard this page belongs to
98          */
99         public NodeManagerPage(final TWizard wizard) {
100                 super(wizard);
101                 pageInit();
102                 setHeading(I18n.getMessage("jsite.node-manager.heading"));
103                 setDescription(I18n.getMessage("jsite.node-manager.description"));
104                 I18nContainer.getInstance().registerRunnable(new Runnable() {
105
106                         @Override
107                         public void run() {
108                                 setHeading(I18n.getMessage("jsite.node-manager.heading"));
109                                 setDescription(I18n.getMessage("jsite.node-manager.description"));
110                         }
111                 });
112         }
113
114         /**
115          * Adds a listener for node manager events.
116          *
117          * @param nodeManagerListener
118          *            The listener to add
119          */
120         public void addNodeManagerListener(NodeManagerListener nodeManagerListener) {
121                 nodeManagerListeners.add(nodeManagerListener);
122         }
123
124         /**
125          * Removes a listener for node manager events.
126          *
127          * @param nodeManagerListener
128          *            The listener to remove
129          */
130         public void removeNodeManagerListener(NodeManagerListener nodeManagerListener) {
131                 nodeManagerListeners.remove(nodeManagerListener);
132         }
133
134         /**
135          * Notifies all listeners that the node configuration has changed.
136          *
137          * @param nodes
138          *            The new list of nodes
139          */
140         protected void fireNodesUpdated(Node[] nodes) {
141                 for (NodeManagerListener nodeManagerListener : nodeManagerListeners) {
142                         nodeManagerListener.nodesUpdated(nodes);
143                 }
144         }
145
146         /**
147          * Notifies all listeners that a new node was selected.
148          *
149          * @param node
150          *            The newly selected node
151          */
152         protected void fireNodeSelected(Node node) {
153                 for (NodeManagerListener nodeManagerListener : nodeManagerListeners) {
154                         nodeManagerListener.nodeSelected(node);
155                 }
156         }
157
158         /**
159          * Creates all actions.
160          */
161         private void createActions() {
162                 addNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.add-node")) {
163
164                         @Override
165                         @SuppressWarnings("synthetic-access")
166                         public void actionPerformed(ActionEvent actionEvent) {
167                                 addNode();
168                         }
169                 };
170
171                 deleteNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.delete-node")) {
172
173                         @Override
174                         @SuppressWarnings("synthetic-access")
175                         public void actionPerformed(ActionEvent actionEvent) {
176                                 deleteNode();
177                         }
178                 };
179                 deleteNodeAction.setEnabled(false);
180
181                 I18nContainer.getInstance().registerRunnable(new Runnable() {
182
183                         @Override
184                         public void run() {
185                                 addNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.node-manager.add-node"));
186                                 deleteNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.node-manager.delete-node"));
187                         }
188                 });
189         }
190
191         /**
192          * Initializes the page and all components in it.
193          */
194         private void pageInit() {
195                 createActions();
196                 nodeListModel = new DefaultListModel();
197                 nodeList = new JList(nodeListModel);
198                 nodeList.setName("node-list");
199                 nodeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
200                 nodeList.addListSelectionListener(this);
201
202                 nodeNameTextField = new JTextField("");
203                 nodeNameTextField.getDocument().putProperty("Name", "node-name");
204                 nodeNameTextField.getDocument().addDocumentListener(this);
205                 nodeNameTextField.setEnabled(false);
206
207                 nodeHostnameTextField = new JTextField("localhost");
208                 nodeHostnameTextField.getDocument().putProperty("Name", "node-hostname");
209                 nodeHostnameTextField.getDocument().addDocumentListener(this);
210                 nodeHostnameTextField.setEnabled(false);
211
212                 nodePortSpinner = new JSpinner(new SpinnerNumberModel(9481, 1, 65535, 1));
213                 nodePortSpinner.setName("node-port");
214                 nodePortSpinner.addChangeListener(this);
215                 nodePortSpinner.setEnabled(false);
216
217                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
218                 buttonPanel.setBorder(new EmptyBorder(-12, -12, -12, -12));
219                 buttonPanel.add(new JButton(addNodeAction));
220                 buttonPanel.add(new JButton(deleteNodeAction));
221
222                 JPanel centerPanel = new JPanel(new BorderLayout());
223                 JPanel nodeInformationPanel = new JPanel(new GridBagLayout());
224                 centerPanel.add(nodeInformationPanel, BorderLayout.PAGE_START);
225                 nodeInformationPanel.add(buttonPanel, new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
226                 final JLabel nodeInformationLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.node-manager.node-information") + "</b></html>");
227                 nodeInformationPanel.add(nodeInformationLabel, new GridBagConstraints(0, 1, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
228                 final TLabel nodeNameLabel = new TLabel(I18n.getMessage("jsite.node-manager.name") + ":", KeyEvent.VK_N, nodeNameTextField);
229                 nodeInformationPanel.add(nodeNameLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
230                 nodeInformationPanel.add(nodeNameTextField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
231                 final TLabel nodeHostnameLabel = new TLabel(I18n.getMessage("jsite.node-manager.hostname") + ":", KeyEvent.VK_H, nodeHostnameTextField);
232                 nodeInformationPanel.add(nodeHostnameLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
233                 nodeInformationPanel.add(nodeHostnameTextField, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
234                 final TLabel nodePortLabel = new TLabel(I18n.getMessage("jsite.node-manager.port") + ":", KeyEvent.VK_P, nodePortSpinner);
235                 nodeInformationPanel.add(nodePortLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
236                 nodeInformationPanel.add(nodePortSpinner, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 6, 0, 0), 0, 0));
237
238                 setLayout(new BorderLayout(12, 12));
239         final JScrollPane nodeListScrollPane = new JScrollPane(nodeList);
240         nodeListScrollPane.setPreferredSize(new Dimension(250, -1));
241         add(nodeListScrollPane, BorderLayout.LINE_START);
242                 add(centerPanel, BorderLayout.CENTER);
243
244                 I18nContainer.getInstance().registerRunnable(new Runnable() {
245
246                         @Override
247                         public void run() {
248                                 nodeInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.node-manager.node-information") + "</b></html>");
249                                 nodeNameLabel.setText(I18n.getMessage("jsite.node-manager.name") + ":");
250                                 nodeHostnameLabel.setText(I18n.getMessage("jsite.node-manager.hostname") + ":");
251                                 nodePortLabel.setText(I18n.getMessage("jsite.node-manager.port") + ":");
252                         }
253                 });
254         }
255
256         /**
257          * {@inheritDoc}
258          */
259         @Override
260         public void pageAdded(TWizard wizard) {
261                 this.wizard.setNextEnabled(nodeListModel.getSize() > 0);
262                 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
263                 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
264                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
265         }
266
267         /**
268          * Sets the node list.
269          *
270          * @param nodes
271          *            The list of nodes
272          */
273         public void setNodes(Node[] nodes) {
274                 nodeListModel.clear();
275                 for (Node node : nodes) {
276                         nodeListModel.addElement(node);
277                 }
278                 nodeList.repaint();
279                 fireNodesUpdated(nodes);
280         }
281
282         /**
283          * Returns the node list.
284          *
285          * @return The list of nodes
286          */
287         public Node[] getNodes() {
288                 Node[] returnNodes = new Node[nodeListModel.getSize()];
289                 for (int nodeIndex = 0, nodeCount = nodeListModel.getSize(); nodeIndex < nodeCount; nodeIndex++) {
290                         returnNodes[nodeIndex] = (Node) nodeListModel.get(nodeIndex);
291                 }
292                 return returnNodes;
293         }
294
295         /**
296          * Returns the currently selected node.
297          *
298          * @return The selected node, or <code>null</code> if no node is selected
299          */
300         private Node getSelectedNode() {
301                 return (Node) nodeList.getSelectedValue();
302         }
303
304         /**
305          * Updates node name or hostname when the user types into the textfields.
306          *
307          * @see #insertUpdate(DocumentEvent)
308          * @see #removeUpdate(DocumentEvent)
309          * @see #changedUpdate(DocumentEvent)
310          * @see DocumentListener
311          * @param documentEvent
312          *            The document event
313          */
314         private void updateTextField(DocumentEvent documentEvent) {
315                 Node node = getSelectedNode();
316                 if (node == null) {
317                         return;
318                 }
319                 Document document = documentEvent.getDocument();
320                 String documentText = null;
321                 try {
322                         documentText = document.getText(0, document.getLength());
323                 } catch (BadLocationException ble1) {
324                         /* ignore. */
325                 }
326                 if (documentText == null) {
327                         return;
328                 }
329                 String documentName = (String) document.getProperty("Name");
330                 if ("node-name".equals(documentName)) {
331                         node.setName(documentText);
332                         nodeList.repaint();
333                         fireNodesUpdated(getNodes());
334                 } else if ("node-hostname".equals(documentName)) {
335                         node.setHostname(documentText);
336                         nodeList.repaint();
337                         fireNodesUpdated(getNodes());
338                 }
339         }
340
341         //
342         // ACTIONS
343         //
344
345         /**
346          * Adds a new node to the list of nodes.
347          */
348         private void addNode() {
349                 Node node = new Node("localhost", 9481, I18n.getMessage("jsite.node-manager.new-node"));
350                 nodeListModel.addElement(node);
351         nodeList.setSelectedIndex(nodeListModel.size() - 1);
352                 deleteNodeAction.setEnabled(nodeListModel.size() > 1);
353                 wizard.setNextEnabled(true);
354                 fireNodesUpdated(getNodes());
355         }
356
357         /**
358          * Deletes the currently selected node from the list of nodes.
359          */
360         private void deleteNode() {
361                 Node node = getSelectedNode();
362                 if (node == null) {
363                         return;
364                 }
365                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.node-manager.delete-node.warning"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION) {
366                         return;
367                 }
368                 int nodeIndex = nodeListModel.indexOf(node);
369                 nodeListModel.removeElement(node);
370                 nodeList.repaint();
371                 fireNodeSelected((Node) nodeListModel.get(Math.min(nodeIndex, nodeListModel.size() - 1)));
372                 fireNodesUpdated(getNodes());
373                 deleteNodeAction.setEnabled(nodeListModel.size() > 1);
374                 wizard.setNextEnabled(nodeListModel.size() > 0);
375         }
376
377         //
378         // INTERFACE ListSelectionListener
379         //
380
381         /**
382          * {@inheritDoc}
383          */
384         @Override
385         @SuppressWarnings("null")
386         public void valueChanged(ListSelectionEvent e) {
387                 Object source = e.getSource();
388                 if (source instanceof JList) {
389                         JList sourceList = (JList) source;
390                         if ("node-list".equals(sourceList.getName())) {
391                                 Node node = (Node) sourceList.getSelectedValue();
392                                 boolean enabled = (node != null);
393                                 nodeNameTextField.setEnabled(enabled);
394                                 nodeHostnameTextField.setEnabled(enabled);
395                                 nodePortSpinner.setEnabled(enabled);
396                                 deleteNodeAction.setEnabled(enabled && (nodeListModel.size() > 1));
397                                 if (enabled) {
398                                         nodeNameTextField.setText(node.getName());
399                                         nodeHostnameTextField.setText(node.getHostname());
400                                         nodePortSpinner.setValue(node.getPort());
401                                 } else {
402                                         nodeNameTextField.setText("");
403                                         nodeHostnameTextField.setText("localhost");
404                                         nodePortSpinner.setValue(9481);
405                                 }
406                         }
407                 }
408         }
409
410         //
411         // INTERFACE DocumentListener
412         //
413
414         /**
415          * {@inheritDoc}
416          */
417         @Override
418         public void insertUpdate(DocumentEvent e) {
419                 updateTextField(e);
420         }
421
422         /**
423          * {@inheritDoc}
424          */
425         @Override
426         public void removeUpdate(DocumentEvent e) {
427                 updateTextField(e);
428         }
429
430         /**
431          * {@inheritDoc}
432          */
433         @Override
434         public void changedUpdate(DocumentEvent e) {
435                 updateTextField(e);
436         }
437
438         //
439         // INTERFACE ChangeListener
440         //
441
442         /**
443          * {@inheritDoc}
444          */
445         @Override
446         public void stateChanged(ChangeEvent e) {
447                 Object source = e.getSource();
448                 Node selectedNode = getSelectedNode();
449                 if (selectedNode == null) {
450                         return;
451                 }
452                 if (source instanceof JSpinner) {
453                         JSpinner sourceSpinner = (JSpinner) source;
454                         if ("node-port".equals(sourceSpinner.getName())) {
455                                 selectedNode.setPort((Integer) sourceSpinner.getValue());
456                                 fireNodeSelected(selectedNode);
457                                 nodeList.repaint();
458                         }
459                 }
460         }
461
462 }