23fafe642e34afa48ce4951424fbcc0c37479ce3
[jSite.git] / src / de / todesbaum / jsite / gui / NodeManagerPage.java
1 /*
2  * jSite-0.7 -
3  * Copyright (C) 2006 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 de.todesbaum.jsite.gui;
21
22 import java.awt.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.FlowLayout;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.Insets;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.KeyEvent;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import javax.swing.AbstractAction;
34 import javax.swing.Action;
35 import javax.swing.DefaultListModel;
36 import javax.swing.JButton;
37 import javax.swing.JLabel;
38 import javax.swing.JList;
39 import javax.swing.JOptionPane;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JSpinner;
43 import javax.swing.JTextField;
44 import javax.swing.ListSelectionModel;
45 import javax.swing.SpinnerNumberModel;
46 import javax.swing.border.EmptyBorder;
47 import javax.swing.event.ChangeEvent;
48 import javax.swing.event.ChangeListener;
49 import javax.swing.event.DocumentEvent;
50 import javax.swing.event.DocumentListener;
51 import javax.swing.event.ListSelectionEvent;
52 import javax.swing.event.ListSelectionListener;
53 import javax.swing.text.BadLocationException;
54 import javax.swing.text.Document;
55
56 import de.todesbaum.jsite.application.Node;
57 import de.todesbaum.jsite.i18n.I18n;
58 import de.todesbaum.jsite.i18n.I18nContainer;
59 import de.todesbaum.util.swing.TLabel;
60 import de.todesbaum.util.swing.TWizard;
61 import de.todesbaum.util.swing.TWizardPage;
62
63 /**
64  * Wizard page that lets the user edit his nodes.
65  *
66  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
67  */
68 public class NodeManagerPage extends TWizardPage implements ListSelectionListener, DocumentListener, ChangeListener {
69
70         /** List of node manager listeners. */
71         private List<NodeManagerListener> nodeManagerListeners = new ArrayList<NodeManagerListener>();
72
73         /** The “add node” action. */
74         protected Action addNodeAction;
75
76         /** The “delete node” action. */
77         protected Action deleteNodeAction;
78
79         /** The node list model. */
80         private DefaultListModel nodeListModel;
81
82         /** The node list. */
83         private JList nodeList;
84
85         /** The node name textfield. */
86         private JTextField nodeNameTextField;
87
88         /** The node hostname textfield. */
89         private JTextField nodeHostnameTextField;
90
91         /** The spinner for the node port. */
92         private JSpinner nodePortSpinner;
93
94         /**
95          * Creates a new node manager wizard page.
96          *
97          * @param wizard
98          *            The wizard this page belongs to
99          */
100         public NodeManagerPage(final TWizard wizard) {
101                 super(wizard);
102                 pageInit();
103                 setHeading(I18n.getMessage("jsite.node-manager.heading"));
104                 setDescription(I18n.getMessage("jsite.node-manager.description"));
105                 I18nContainer.getInstance().registerRunnable(new Runnable() {
106
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                         @SuppressWarnings("synthetic-access")
165                         public void actionPerformed(ActionEvent actionEvent) {
166                                 addNode();
167                         }
168                 };
169
170                 deleteNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.delete-node")) {
171
172                         @SuppressWarnings("synthetic-access")
173                         public void actionPerformed(ActionEvent actionEvent) {
174                                 deleteNode();
175                         }
176                 };
177                 deleteNodeAction.setEnabled(false);
178
179                 I18nContainer.getInstance().registerRunnable(new Runnable() {
180
181                         public void run() {
182                                 addNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.node-manager.add-node"));
183                                 deleteNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.node-manager.delete-node"));
184                         }
185                 });
186         }
187
188         /**
189          * Initializes the page and all components in it.
190          */
191         private void pageInit() {
192                 createActions();
193                 nodeListModel = new DefaultListModel();
194                 nodeList = new JList(nodeListModel);
195                 nodeList.setName("node-list");
196                 nodeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
197                 nodeList.addListSelectionListener(this);
198                 nodeList.setPreferredSize(new Dimension(250, -1));
199
200                 nodeNameTextField = new JTextField("");
201                 nodeNameTextField.getDocument().putProperty("Name", "node-name");
202                 nodeNameTextField.getDocument().addDocumentListener(this);
203                 nodeNameTextField.setEnabled(false);
204
205                 nodeHostnameTextField = new JTextField("localhost");
206                 nodeHostnameTextField.getDocument().putProperty("Name", "node-hostname");
207                 nodeHostnameTextField.getDocument().addDocumentListener(this);
208                 nodeHostnameTextField.setEnabled(false);
209
210                 nodePortSpinner = new JSpinner(new SpinnerNumberModel(9481, 1, 65535, 1));
211                 nodePortSpinner.setName("node-port");
212                 nodePortSpinner.addChangeListener(this);
213                 nodePortSpinner.setEnabled(false);
214
215                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
216                 buttonPanel.setBorder(new EmptyBorder(-12, -12, -12, -12));
217                 buttonPanel.add(new JButton(addNodeAction));
218                 buttonPanel.add(new JButton(deleteNodeAction));
219
220                 JPanel centerPanel = new JPanel(new BorderLayout());
221                 JPanel nodeInformationPanel = new JPanel(new GridBagLayout());
222                 centerPanel.add(nodeInformationPanel, BorderLayout.PAGE_START);
223                 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));
224                 final JLabel nodeInformationLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.node-manager.node-information") + "</b></html>");
225                 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));
226                 final TLabel nodeNameLabel = new TLabel(I18n.getMessage("jsite.node-manager.name") + ":", KeyEvent.VK_N, nodeNameTextField);
227                 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));
228                 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));
229                 final TLabel nodeHostnameLabel = new TLabel(I18n.getMessage("jsite.node-manager.hostname") + ":", KeyEvent.VK_H, nodeHostnameTextField);
230                 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));
231                 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));
232                 final TLabel nodePortLabel = new TLabel(I18n.getMessage("jsite.node-manager.port") + ":", KeyEvent.VK_P, nodePortSpinner);
233                 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));
234                 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));
235
236                 setLayout(new BorderLayout(12, 12));
237                 add(new JScrollPane(nodeList), BorderLayout.LINE_START);
238                 add(centerPanel, BorderLayout.CENTER);
239
240                 I18nContainer.getInstance().registerRunnable(new Runnable() {
241
242                         public void run() {
243                                 nodeInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.node-manager.node-information") + "</b></html>");
244                                 nodeNameLabel.setText(I18n.getMessage("jsite.node-manager.name") + ":");
245                                 nodeHostnameLabel.setText(I18n.getMessage("jsite.node-manager.hostname") + ":");
246                                 nodePortLabel.setText(I18n.getMessage("jsite.node-manager.port") + ":");
247                         }
248                 });
249         }
250
251         /**
252          * {@inheritDoc}
253          */
254         @Override
255         public void pageAdded(TWizard wizard) {
256                 this.wizard.setNextEnabled(nodeListModel.getSize() > 0);
257                 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
258                 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
259                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
260         }
261
262         /**
263          * Sets the node list.
264          *
265          * @param nodes
266          *            The list of nodes
267          */
268         public void setNodes(Node[] nodes) {
269                 nodeListModel.clear();
270                 for (Node node : nodes) {
271                         nodeListModel.addElement(node);
272                 }
273                 nodeList.repaint();
274                 fireNodesUpdated(nodes);
275         }
276
277         /**
278          * Returns the node list.
279          *
280          * @return The list of nodes
281          */
282         public Node[] getNodes() {
283                 Node[] returnNodes = new Node[nodeListModel.getSize()];
284                 for (int nodeIndex = 0, nodeCount = nodeListModel.getSize(); nodeIndex < nodeCount; nodeIndex++) {
285                         returnNodes[nodeIndex] = (Node) nodeListModel.get(nodeIndex);
286                 }
287                 return returnNodes;
288         }
289
290         /**
291          * Returns the currently selected node.
292          *
293          * @return The selected node, or <code>null</code> if no node is selected
294          */
295         private Node getSelectedNode() {
296                 return (Node) nodeList.getSelectedValue();
297         }
298
299         /**
300          * Updates node name or hostname when the user types into the textfields.
301          *
302          * @see #insertUpdate(DocumentEvent)
303          * @see #removeUpdate(DocumentEvent)
304          * @see #changedUpdate(DocumentEvent)
305          * @see DocumentListener
306          * @param documentEvent
307          *            The document event
308          */
309         private void updateTextField(DocumentEvent documentEvent) {
310                 Node node = getSelectedNode();
311                 if (node == null) {
312                         return;
313                 }
314                 Document document = documentEvent.getDocument();
315                 String documentText = null;
316                 try {
317                         documentText = document.getText(0, document.getLength());
318                 } catch (BadLocationException ble1) {
319                         /* ignore. */
320                 }
321                 if (documentText == null) {
322                         return;
323                 }
324                 String documentName = (String) document.getProperty("Name");
325                 if ("node-name".equals(documentName)) {
326                         node.setName(documentText);
327                         nodeList.repaint();
328                         fireNodesUpdated(getNodes());
329                 } else if ("node-hostname".equals(documentName)) {
330                         node.setHostname(documentText);
331                         nodeList.repaint();
332                         fireNodesUpdated(getNodes());
333                 }
334         }
335
336         //
337         // ACTIONS
338         //
339
340         /**
341          * Adds a new node to the list of nodes.
342          */
343         private void addNode() {
344                 Node node = new Node("localhost", 9481, I18n.getMessage("jsite.node-manager.new-node"));
345                 nodeListModel.addElement(node);
346                 deleteNodeAction.setEnabled(nodeListModel.size() > 1);
347                 wizard.setNextEnabled(true);
348                 fireNodesUpdated(getNodes());
349         }
350
351         /**
352          * Deletes the currently selected node from the list of nodes.
353          */
354         private void deleteNode() {
355                 Node node = getSelectedNode();
356                 if (node == null) {
357                         return;
358                 }
359                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.node-manager.delete-node.warning"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION) {
360                         return;
361                 }
362                 int nodeIndex = nodeListModel.indexOf(node);
363                 nodeListModel.removeElement(node);
364                 nodeList.repaint();
365                 fireNodeSelected((Node) nodeListModel.get(Math.min(nodeIndex, nodeListModel.size() - 1)));
366                 fireNodesUpdated(getNodes());
367                 deleteNodeAction.setEnabled(nodeListModel.size() > 1);
368                 wizard.setNextEnabled(nodeListModel.size() > 0);
369         }
370
371         //
372         // INTERFACE ListSelectionListener
373         //
374
375         /**
376          * {@inheritDoc}
377          */
378         @SuppressWarnings("null")
379         public void valueChanged(ListSelectionEvent e) {
380                 Object source = e.getSource();
381                 if (source instanceof JList) {
382                         JList sourceList = (JList) source;
383                         if ("node-list".equals(sourceList.getName())) {
384                                 Node node = (Node) sourceList.getSelectedValue();
385                                 boolean enabled = (node != null);
386                                 nodeNameTextField.setEnabled(enabled);
387                                 nodeHostnameTextField.setEnabled(enabled);
388                                 nodePortSpinner.setEnabled(enabled);
389                                 deleteNodeAction.setEnabled(enabled && (nodeListModel.size() > 1));
390                                 if (enabled) {
391                                         nodeNameTextField.setText(node.getName());
392                                         nodeHostnameTextField.setText(node.getHostname());
393                                         nodePortSpinner.setValue(node.getPort());
394                                 } else {
395                                         nodeNameTextField.setText("");
396                                         nodeHostnameTextField.setText("localhost");
397                                         nodePortSpinner.setValue(9481);
398                                 }
399                         }
400                 }
401         }
402
403         //
404         // INTERFACE DocumentListener
405         //
406
407         /**
408          * {@inheritDoc}
409          */
410         public void insertUpdate(DocumentEvent e) {
411                 updateTextField(e);
412         }
413
414         /**
415          * {@inheritDoc}
416          */
417         public void removeUpdate(DocumentEvent e) {
418                 updateTextField(e);
419         }
420
421         /**
422          * {@inheritDoc}
423          */
424         public void changedUpdate(DocumentEvent e) {
425                 updateTextField(e);
426         }
427
428         //
429         // INTERFACE ChangeListener
430         //
431
432         /**
433          * {@inheritDoc}
434          */
435         public void stateChanged(ChangeEvent e) {
436                 Object source = e.getSource();
437                 Node selectedNode = getSelectedNode();
438                 if (selectedNode == null) {
439                         return;
440                 }
441                 if (source instanceof JSpinner) {
442                         JSpinner sourceSpinner = (JSpinner) source;
443                         if ("node-port".equals(sourceSpinner.getName())) {
444                                 selectedNode.setPort((Integer) sourceSpinner.getValue());
445                                 nodeList.repaint();
446                         }
447                 }
448         }
449
450 }