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