remove subversion $Id$ tags
[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  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
65  */
66 public class NodeManagerPage extends TWizardPage implements ListSelectionListener, DocumentListener, ChangeListener {
67
68         private List<NodeManagerListener> nodeManagerListeners = new ArrayList<NodeManagerListener>();
69
70         protected Action addNodeAction;
71         protected Action deleteNodeAction;
72         private DefaultListModel nodeListModel;
73         private JList nodeList;
74         private JTextField nodeNameTextField;
75         private JTextField nodeHostnameTextField;
76         private JSpinner nodePortSpinner;
77
78         public NodeManagerPage(final TWizard wizard) {
79                 super(wizard);
80                 pageInit();
81                 setHeading(I18n.getMessage("jsite.node-manager.heading"));
82                 setDescription(I18n.getMessage("jsite.node-manager.description"));
83                 I18nContainer.getInstance().registerRunnable(new Runnable() {
84
85                         public void run() {
86                                 setHeading(I18n.getMessage("jsite.node-manager.heading"));
87                                 setDescription(I18n.getMessage("jsite.node-manager.description"));
88                         }
89                 });
90         }
91         
92         public void addNodeManagerListener(NodeManagerListener nodeManagerListener) {
93                 nodeManagerListeners.add(nodeManagerListener);
94         }
95         
96         public void removeNodeManagerListener(NodeManagerListener nodeManagerListener) {
97                 nodeManagerListeners.remove(nodeManagerListener);
98         }
99         
100         protected void fireNodesUpdated(Node[] nodes) {
101                 for (NodeManagerListener nodeManagerListener: nodeManagerListeners) {
102                         nodeManagerListener.nodesUpdated(nodes);
103                 }
104         }
105
106         private void createActions() {
107                 addNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.add-node")) {
108
109                         @SuppressWarnings("synthetic-access")
110                         public void actionPerformed(ActionEvent actionEvent) {
111                                 addNode();
112                         }
113                 };
114
115                 deleteNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.delete-node")) {
116
117                         @SuppressWarnings("synthetic-access")
118                         public void actionPerformed(ActionEvent actionEvent) {
119                                 deleteNode();
120                         }
121                 };
122                 deleteNodeAction.setEnabled(false);
123
124                 I18nContainer.getInstance().registerRunnable(new Runnable() {
125
126                         public void run() {
127                                 addNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.node-manager.add-node"));
128                                 deleteNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.node-manager.delete-node"));
129                         }
130                 });
131         }
132
133         private void pageInit() {
134                 createActions();
135                 nodeListModel = new DefaultListModel();
136                 nodeList = new JList(nodeListModel);
137                 nodeList.setName("node-list");
138                 nodeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
139                 nodeList.addListSelectionListener(this);
140                 nodeList.setPreferredSize(new Dimension(250, -1));
141
142                 nodeNameTextField = new JTextField("");
143                 nodeNameTextField.getDocument().putProperty("Name", "node-name");
144                 nodeNameTextField.getDocument().addDocumentListener(this);
145                 nodeNameTextField.setEnabled(false);
146                 
147                 nodeHostnameTextField = new JTextField("localhost");
148                 nodeHostnameTextField.getDocument().putProperty("Name", "node-hostname");
149                 nodeHostnameTextField.getDocument().addDocumentListener(this);
150                 nodeHostnameTextField.setEnabled(false);
151
152                 nodePortSpinner = new JSpinner(new SpinnerNumberModel(9481, 1, 65535, 1));
153                 nodePortSpinner.setName("node-port");
154                 nodePortSpinner.addChangeListener(this);
155                 nodePortSpinner.setEnabled(false);
156
157                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
158                 buttonPanel.setBorder(new EmptyBorder(-12, -12, -12, -12));
159                 buttonPanel.add(new JButton(addNodeAction));
160                 buttonPanel.add(new JButton(deleteNodeAction));
161
162                 JPanel centerPanel = new JPanel(new BorderLayout());
163                 JPanel nodeInformationPanel = new JPanel(new GridBagLayout());
164                 centerPanel.add(nodeInformationPanel, BorderLayout.PAGE_START);
165                 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));
166                 final JLabel nodeInformationLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.node-manager.node-information") + "</b></html>");
167                 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));
168                 final TLabel nodeNameLabel = new TLabel(I18n.getMessage("jsite.node-manager.name") + ":", KeyEvent.VK_N, nodeNameTextField);
169                 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));
170                 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));
171                 final TLabel nodeHostnameLabel = new TLabel(I18n.getMessage("jsite.node-manager.hostname") + ":", KeyEvent.VK_H, nodeHostnameTextField);
172                 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));
173                 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));
174                 final TLabel nodePortLabel = new TLabel(I18n.getMessage("jsite.node-manager.port") + ":", KeyEvent.VK_P, nodePortSpinner);
175                 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));
176                 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));
177
178                 setLayout(new BorderLayout(12, 12));
179                 add(new JScrollPane(nodeList), BorderLayout.LINE_START);
180                 add(centerPanel, BorderLayout.CENTER);
181
182                 I18nContainer.getInstance().registerRunnable(new Runnable() {
183
184                         public void run() {
185                                 nodeInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.node-manager.node-information") + "</b></html>");
186                                 nodeNameLabel.setText(I18n.getMessage("jsite.node-manager.name") + ":");
187                                 nodeHostnameLabel.setText(I18n.getMessage("jsite.node-manager.hostname") + ":");
188                                 nodePortLabel.setText(I18n.getMessage("jsite.node-manager.port") + ":");
189                         }
190                 });
191         }
192         
193         /**
194          * {@inheritDoc}
195          */
196         @Override
197         public void pageAdded(TWizard wizard) {
198                 this.wizard.setNextEnabled(nodeListModel.getSize() > 0);
199                 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
200                 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
201                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
202         }
203
204         public void setNodes(Node[] nodes) {
205                 nodeListModel.clear();
206                 for (Node node: nodes) {
207                         nodeListModel.addElement(node);
208                 }
209                 nodeList.repaint();
210                 fireNodesUpdated(nodes);
211         }
212
213         public Node[] getNodes() {
214                 Node[] returnNodes = new Node[nodeListModel.getSize()];
215                 for (int nodeIndex = 0, nodeCount = nodeListModel.getSize(); nodeIndex < nodeCount; nodeIndex++) {
216                         returnNodes[nodeIndex] = (Node) nodeListModel.get(nodeIndex);
217                 }
218                 return returnNodes;
219         }
220
221         private Node getSelectedNode() {
222                 return (Node) nodeList.getSelectedValue();
223         }
224         
225         private void updateTextField(DocumentEvent documentEvent) {
226                 Node node = getSelectedNode();
227                 if (node == null) {
228                         return;
229                 }
230                 Document document = documentEvent.getDocument();
231                 String documentText = null;
232                 try {
233                         documentText = document.getText(0, document.getLength());
234                 } catch (BadLocationException ble1) {
235                 }
236                 if (documentText == null) {
237                         return;
238                 }
239                 String documentName = (String) document.getProperty("Name");
240                 if ("node-name".equals(documentName)) {
241                         node.setName(documentText);
242                         nodeList.repaint();
243                         fireNodesUpdated(getNodes());
244                 } else if ("node-hostname".equals(documentName)) {
245                         node.setHostname(documentText);
246                         nodeList.repaint();
247                         fireNodesUpdated(getNodes());
248                 }
249         }
250
251         //
252         // ACTIONS
253         //
254
255         private void addNode() {
256                 Node node = new Node("localhost", 9481, I18n.getMessage("jsite.node-manager.new-node"));
257                 nodeListModel.addElement(node);
258                 wizard.setNextEnabled(true);
259                 fireNodesUpdated(getNodes());
260         }
261
262         private void deleteNode() {
263                 Node node = getSelectedNode();
264                 if (node == null) {
265                         return;
266                 }
267                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.node-manager.delete-node.warning"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION) {
268                         return;
269                 }
270                 nodeListModel.removeElement(node);
271                 nodeList.repaint();
272                 fireNodesUpdated(getNodes());
273                 wizard.setNextEnabled(nodeListModel.size() > 0);
274         }
275
276         //
277         // INTERFACE ListSelectionListener
278         //
279
280         /**
281          * {@inheritDoc}
282          */
283         public void valueChanged(ListSelectionEvent e) {
284                 Object source = e.getSource();
285                 if (source instanceof JList) {
286                         JList sourceList = (JList) source;
287                         if ("node-list".equals(sourceList.getName())) {
288                                 Node node = (Node) sourceList.getSelectedValue();
289                                 boolean enabled = (node != null);
290                                 nodeNameTextField.setEnabled(enabled);
291                                 nodeHostnameTextField.setEnabled(enabled);
292                                 nodePortSpinner.setEnabled(enabled);
293                                 deleteNodeAction.setEnabled(enabled);
294                                 if (enabled) {
295                                         nodeNameTextField.setText(node.getName());
296                                         nodeHostnameTextField.setText(node.getHostname());
297                                         nodePortSpinner.setValue(node.getPort());
298                                 } else {
299                                         nodeNameTextField.setText("");
300                                         nodeHostnameTextField.setText("localhost");
301                                         nodePortSpinner.setValue(9481);
302                                 }
303                         }
304                 }
305         }
306
307         //
308         // INTERFACE DocumentListener
309         //
310
311         /**
312          * {@inheritDoc}
313          */
314         public void insertUpdate(DocumentEvent e) {
315                 updateTextField(e);
316         }
317
318         /**
319          * {@inheritDoc}
320          */
321         public void removeUpdate(DocumentEvent e) {
322                 updateTextField(e);
323         }
324
325         /**
326          * {@inheritDoc}
327          */
328         public void changedUpdate(DocumentEvent e) {
329                 updateTextField(e);
330         }
331
332         //
333         // INTERFACE ChangeListener
334         //
335
336         /**
337          * {@inheritDoc}
338          */
339         public void stateChanged(ChangeEvent e) {
340                 Object source = e.getSource();
341                 Node selectedNode = getSelectedNode();
342                 if (selectedNode == null) {
343                         return;
344                 }
345                 if (source instanceof JSpinner) {
346                         JSpinner sourceSpinner = (JSpinner) source;
347                         if ("node-port".equals(sourceSpinner.getName())) {
348                                 selectedNode.setPort((Integer) sourceSpinner.getValue());
349                                 nodeList.repaint();
350                         }
351                 }
352         }
353
354 }