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