Mavenize jSite.
[jSite.git] / src / main / java / de / todesbaum / jsite / gui / NodeManagerPage.java
1 /*
2  * jSite - NodeManagerPage.java - Copyright © 2006–2012 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                         public void run() {
107                                 setHeading(I18n.getMessage("jsite.node-manager.heading"));
108                                 setDescription(I18n.getMessage("jsite.node-manager.description"));
109                         }
110                 });
111         }
112
113         /**
114          * Adds a listener for node manager events.
115          *
116          * @param nodeManagerListener
117          *            The listener to add
118          */
119         public void addNodeManagerListener(NodeManagerListener nodeManagerListener) {
120                 nodeManagerListeners.add(nodeManagerListener);
121         }
122
123         /**
124          * Removes a listener for node manager events.
125          *
126          * @param nodeManagerListener
127          *            The listener to remove
128          */
129         public void removeNodeManagerListener(NodeManagerListener nodeManagerListener) {
130                 nodeManagerListeners.remove(nodeManagerListener);
131         }
132
133         /**
134          * Notifies all listeners that the node configuration has changed.
135          *
136          * @param nodes
137          *            The new list of nodes
138          */
139         protected void fireNodesUpdated(Node[] nodes) {
140                 for (NodeManagerListener nodeManagerListener : nodeManagerListeners) {
141                         nodeManagerListener.nodesUpdated(nodes);
142                 }
143         }
144
145         /**
146          * Notifies all listeners that a new node was selected.
147          *
148          * @param node
149          *            The newly selected node
150          */
151         protected void fireNodeSelected(Node node) {
152                 for (NodeManagerListener nodeManagerListener : nodeManagerListeners) {
153                         nodeManagerListener.nodeSelected(node);
154                 }
155         }
156
157         /**
158          * Creates all actions.
159          */
160         private void createActions() {
161                 addNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.add-node")) {
162
163                         @SuppressWarnings("synthetic-access")
164                         public void actionPerformed(ActionEvent actionEvent) {
165                                 addNode();
166                         }
167                 };
168
169                 deleteNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.delete-node")) {
170
171                         @SuppressWarnings("synthetic-access")
172                         public void actionPerformed(ActionEvent actionEvent) {
173                                 deleteNode();
174                         }
175                 };
176                 deleteNodeAction.setEnabled(false);
177
178                 I18nContainer.getInstance().registerRunnable(new Runnable() {
179
180                         public void run() {
181                                 addNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.node-manager.add-node"));
182                                 deleteNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.node-manager.delete-node"));
183                         }
184                 });
185         }
186
187         /**
188          * Initializes the page and all components in it.
189          */
190         private void pageInit() {
191                 createActions();
192                 nodeListModel = new DefaultListModel();
193                 nodeList = new JList(nodeListModel);
194                 nodeList.setName("node-list");
195                 nodeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
196                 nodeList.addListSelectionListener(this);
197                 nodeList.setPreferredSize(new Dimension(250, -1));
198
199                 nodeNameTextField = new JTextField("");
200                 nodeNameTextField.getDocument().putProperty("Name", "node-name");
201                 nodeNameTextField.getDocument().addDocumentListener(this);
202                 nodeNameTextField.setEnabled(false);
203
204                 nodeHostnameTextField = new JTextField("localhost");
205                 nodeHostnameTextField.getDocument().putProperty("Name", "node-hostname");
206                 nodeHostnameTextField.getDocument().addDocumentListener(this);
207                 nodeHostnameTextField.setEnabled(false);
208
209                 nodePortSpinner = new JSpinner(new SpinnerNumberModel(9481, 1, 65535, 1));
210                 nodePortSpinner.setName("node-port");
211                 nodePortSpinner.addChangeListener(this);
212                 nodePortSpinner.setEnabled(false);
213
214                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
215                 buttonPanel.setBorder(new EmptyBorder(-12, -12, -12, -12));
216                 buttonPanel.add(new JButton(addNodeAction));
217                 buttonPanel.add(new JButton(deleteNodeAction));
218
219                 JPanel centerPanel = new JPanel(new BorderLayout());
220                 JPanel nodeInformationPanel = new JPanel(new GridBagLayout());
221                 centerPanel.add(nodeInformationPanel, BorderLayout.PAGE_START);
222                 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));
223                 final JLabel nodeInformationLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.node-manager.node-information") + "</b></html>");
224                 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));
225                 final TLabel nodeNameLabel = new TLabel(I18n.getMessage("jsite.node-manager.name") + ":", KeyEvent.VK_N, nodeNameTextField);
226                 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));
227                 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));
228                 final TLabel nodeHostnameLabel = new TLabel(I18n.getMessage("jsite.node-manager.hostname") + ":", KeyEvent.VK_H, nodeHostnameTextField);
229                 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));
230                 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));
231                 final TLabel nodePortLabel = new TLabel(I18n.getMessage("jsite.node-manager.port") + ":", KeyEvent.VK_P, nodePortSpinner);
232                 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));
233                 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));
234
235                 setLayout(new BorderLayout(12, 12));
236                 add(new JScrollPane(nodeList), BorderLayout.LINE_START);
237                 add(centerPanel, BorderLayout.CENTER);
238
239                 I18nContainer.getInstance().registerRunnable(new Runnable() {
240
241                         public void run() {
242                                 nodeInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.node-manager.node-information") + "</b></html>");
243                                 nodeNameLabel.setText(I18n.getMessage("jsite.node-manager.name") + ":");
244                                 nodeHostnameLabel.setText(I18n.getMessage("jsite.node-manager.hostname") + ":");
245                                 nodePortLabel.setText(I18n.getMessage("jsite.node-manager.port") + ":");
246                         }
247                 });
248         }
249
250         /**
251          * {@inheritDoc}
252          */
253         @Override
254         public void pageAdded(TWizard wizard) {
255                 this.wizard.setNextEnabled(nodeListModel.getSize() > 0);
256                 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
257                 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
258                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
259         }
260
261         /**
262          * Sets the node list.
263          *
264          * @param nodes
265          *            The list of nodes
266          */
267         public void setNodes(Node[] nodes) {
268                 nodeListModel.clear();
269                 for (Node node : nodes) {
270                         nodeListModel.addElement(node);
271                 }
272                 nodeList.repaint();
273                 fireNodesUpdated(nodes);
274         }
275
276         /**
277          * Returns the node list.
278          *
279          * @return The list of nodes
280          */
281         public Node[] getNodes() {
282                 Node[] returnNodes = new Node[nodeListModel.getSize()];
283                 for (int nodeIndex = 0, nodeCount = nodeListModel.getSize(); nodeIndex < nodeCount; nodeIndex++) {
284                         returnNodes[nodeIndex] = (Node) nodeListModel.get(nodeIndex);
285                 }
286                 return returnNodes;
287         }
288
289         /**
290          * Returns the currently selected node.
291          *
292          * @return The selected node, or <code>null</code> if no node is selected
293          */
294         private Node getSelectedNode() {
295                 return (Node) nodeList.getSelectedValue();
296         }
297
298         /**
299          * Updates node name or hostname when the user types into the textfields.
300          *
301          * @see #insertUpdate(DocumentEvent)
302          * @see #removeUpdate(DocumentEvent)
303          * @see #changedUpdate(DocumentEvent)
304          * @see DocumentListener
305          * @param documentEvent
306          *            The document event
307          */
308         private void updateTextField(DocumentEvent documentEvent) {
309                 Node node = getSelectedNode();
310                 if (node == null) {
311                         return;
312                 }
313                 Document document = documentEvent.getDocument();
314                 String documentText = null;
315                 try {
316                         documentText = document.getText(0, document.getLength());
317                 } catch (BadLocationException ble1) {
318                         /* ignore. */
319                 }
320                 if (documentText == null) {
321                         return;
322                 }
323                 String documentName = (String) document.getProperty("Name");
324                 if ("node-name".equals(documentName)) {
325                         node.setName(documentText);
326                         nodeList.repaint();
327                         fireNodesUpdated(getNodes());
328                 } else if ("node-hostname".equals(documentName)) {
329                         node.setHostname(documentText);
330                         nodeList.repaint();
331                         fireNodesUpdated(getNodes());
332                 }
333         }
334
335         //
336         // ACTIONS
337         //
338
339         /**
340          * Adds a new node to the list of nodes.
341          */
342         private void addNode() {
343                 Node node = new Node("localhost", 9481, I18n.getMessage("jsite.node-manager.new-node"));
344                 nodeListModel.addElement(node);
345                 deleteNodeAction.setEnabled(nodeListModel.size() > 1);
346                 wizard.setNextEnabled(true);
347                 fireNodesUpdated(getNodes());
348         }
349
350         /**
351          * Deletes the currently selected node from the list of nodes.
352          */
353         private void deleteNode() {
354                 Node node = getSelectedNode();
355                 if (node == null) {
356                         return;
357                 }
358                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.node-manager.delete-node.warning"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION) {
359                         return;
360                 }
361                 int nodeIndex = nodeListModel.indexOf(node);
362                 nodeListModel.removeElement(node);
363                 nodeList.repaint();
364                 fireNodeSelected((Node) nodeListModel.get(Math.min(nodeIndex, nodeListModel.size() - 1)));
365                 fireNodesUpdated(getNodes());
366                 deleteNodeAction.setEnabled(nodeListModel.size() > 1);
367                 wizard.setNextEnabled(nodeListModel.size() > 0);
368         }
369
370         //
371         // INTERFACE ListSelectionListener
372         //
373
374         /**
375          * {@inheritDoc}
376          */
377         @SuppressWarnings("null")
378         public void valueChanged(ListSelectionEvent e) {
379                 Object source = e.getSource();
380                 if (source instanceof JList) {
381                         JList sourceList = (JList) source;
382                         if ("node-list".equals(sourceList.getName())) {
383                                 Node node = (Node) sourceList.getSelectedValue();
384                                 boolean enabled = (node != null);
385                                 nodeNameTextField.setEnabled(enabled);
386                                 nodeHostnameTextField.setEnabled(enabled);
387                                 nodePortSpinner.setEnabled(enabled);
388                                 deleteNodeAction.setEnabled(enabled && (nodeListModel.size() > 1));
389                                 if (enabled) {
390                                         nodeNameTextField.setText(node.getName());
391                                         nodeHostnameTextField.setText(node.getHostname());
392                                         nodePortSpinner.setValue(node.getPort());
393                                 } else {
394                                         nodeNameTextField.setText("");
395                                         nodeHostnameTextField.setText("localhost");
396                                         nodePortSpinner.setValue(9481);
397                                 }
398                         }
399                 }
400         }
401
402         //
403         // INTERFACE DocumentListener
404         //
405
406         /**
407          * {@inheritDoc}
408          */
409         public void insertUpdate(DocumentEvent e) {
410                 updateTextField(e);
411         }
412
413         /**
414          * {@inheritDoc}
415          */
416         public void removeUpdate(DocumentEvent e) {
417                 updateTextField(e);
418         }
419
420         /**
421          * {@inheritDoc}
422          */
423         public void changedUpdate(DocumentEvent e) {
424                 updateTextField(e);
425         }
426
427         //
428         // INTERFACE ChangeListener
429         //
430
431         /**
432          * {@inheritDoc}
433          */
434         public void stateChanged(ChangeEvent e) {
435                 Object source = e.getSource();
436                 Node selectedNode = getSelectedNode();
437                 if (selectedNode == null) {
438                         return;
439                 }
440                 if (source instanceof JSpinner) {
441                         JSpinner sourceSpinner = (JSpinner) source;
442                         if ("node-port".equals(sourceSpinner.getName())) {
443                                 selectedNode.setPort((Integer) sourceSpinner.getValue());
444                                 fireNodeSelected(selectedNode);
445                                 nodeList.repaint();
446                         }
447                 }
448         }
449
450 }