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