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