3 * Copyright (C) 2006 David Roden
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.
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.
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.
20 package de.todesbaum.jsite.gui;
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;
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;
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;
63 * @author David Roden <droden@gmail.com>
64 * @version $Id: NodeManagerPage.java 418 2006-03-29 17:49:16Z bombe $
66 public class NodeManagerPage extends TWizardPage implements ListSelectionListener, DocumentListener, ChangeListener {
68 private List<NodeManagerListener> nodeManagerListeners = new ArrayList<NodeManagerListener>();
69 private TWizard wizard;
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;
79 public NodeManagerPage() {
82 setHeading(I18n.getMessage("jsite.node-manager.heading"));
83 setDescription(I18n.getMessage("jsite.node-manager.description"));
86 public void addNodeManagerListener(NodeManagerListener nodeManagerListener) {
87 nodeManagerListeners.add(nodeManagerListener);
90 public void removeNodeManagerListener(NodeManagerListener nodeManagerListener) {
91 nodeManagerListeners.remove(nodeManagerListener);
94 protected void fireNodesUpdated(Node[] nodes) {
95 for (NodeManagerListener nodeManagerListener: nodeManagerListeners) {
96 nodeManagerListener.nodesUpdated(nodes);
100 private void createActions() {
101 addNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.add-node")) {
103 public void actionPerformed(ActionEvent actionEvent) {
108 deleteNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.delete-node")) {
110 public void actionPerformed(ActionEvent actionEvent) {
114 deleteNodeAction.setEnabled(false);
117 private void pageInit() {
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));
126 nodeNameTextField = new JTextField("");
127 nodeNameTextField.getDocument().putProperty("Name", "node-name");
128 nodeNameTextField.getDocument().addDocumentListener(this);
129 nodeNameTextField.setEnabled(false);
131 nodeHostnameTextField = new JTextField("localhost");
132 nodeHostnameTextField.getDocument().putProperty("Name", "node-hostname");
133 nodeHostnameTextField.getDocument().addDocumentListener(this);
134 nodeHostnameTextField.setEnabled(false);
136 nodePortSpinner = new JSpinner(new SpinnerNumberModel(9481, 1, 65535, 1));
137 nodePortSpinner.setName("node-port");
138 nodePortSpinner.addChangeListener(this);
139 nodePortSpinner.setEnabled(false);
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));
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));
158 setLayout(new BorderLayout(12, 12));
159 add(new JScrollPane(nodeList), BorderLayout.LINE_START);
160 add(centerPanel, BorderLayout.CENTER);
167 public void pageAdded(TWizard wizard) {
168 this.wizard = wizard;
169 wizard.setNextEnabled(nodeListModel.getSize() > 0);
172 public void setNodes(Node[] nodes) {
173 nodeListModel.clear();
174 for (Node node: nodes) {
175 nodeListModel.addElement(node);
178 fireNodesUpdated(nodes);
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);
189 private Node getSelectedNode() {
190 return (Node) nodeList.getSelectedValue();
193 private void updateTextField(DocumentEvent documentEvent) {
194 Node node = getSelectedNode();
198 Document document = documentEvent.getDocument();
199 String documentText = null;
201 documentText = document.getText(0, document.getLength());
202 } catch (BadLocationException ble1) {
204 if (documentText == null) {
207 String documentName = (String) document.getProperty("Name");
208 if ("node-name".equals(documentName)) {
209 node.setName(documentText);
211 fireNodesUpdated(getNodes());
212 } else if ("node-hostname".equals(documentName)) {
213 node.setHostname(documentText);
215 fireNodesUpdated(getNodes());
223 protected void addNode() {
224 Node node = new Node("localhost", 9481, I18n.getMessage("jsite.node-manager.new-node"));
225 nodeListModel.addElement(node);
226 wizard.setNextEnabled(true);
227 fireNodesUpdated(getNodes());
230 protected void deleteNode() {
231 Node node = getSelectedNode();
235 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.node-manager.delete-node.warning"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION) {
238 nodeListModel.removeElement(node);
240 fireNodesUpdated(getNodes());
241 wizard.setNextEnabled(nodeListModel.size() > 0);
245 // INTERFACE ListSelectionListener
251 public void valueChanged(ListSelectionEvent e) {
252 Object source = e.getSource();
253 if (source instanceof JList) {
254 JList sourceList = (JList) source;
255 if ("node-list".equals(sourceList.getName())) {
256 Node node = (Node) sourceList.getSelectedValue();
257 boolean enabled = (node != null);
258 nodeNameTextField.setEnabled(enabled);
259 nodeHostnameTextField.setEnabled(enabled);
260 nodePortSpinner.setEnabled(enabled);
261 deleteNodeAction.setEnabled(enabled);
263 nodeNameTextField.setText(node.getName());
264 nodeHostnameTextField.setText(node.getHostname());
265 nodePortSpinner.setValue(node.getPort());
267 nodeNameTextField.setText("");
268 nodeHostnameTextField.setText("localhost");
269 nodePortSpinner.setValue(9481);
276 // INTERFACE DocumentListener
282 public void insertUpdate(DocumentEvent e) {
289 public void removeUpdate(DocumentEvent e) {
296 public void changedUpdate(DocumentEvent e) {
301 // INTERFACE ChangeListener
307 public void stateChanged(ChangeEvent e) {
308 Object source = e.getSource();
309 Node selectedNode = getSelectedNode();
310 if (selectedNode == null) {
313 if (source instanceof JSpinner) {
314 JSpinner sourceSpinner = (JSpinner) source;
315 if ("node-port".equals(sourceSpinner.getName())) {
316 selectedNode.setPort((Integer) sourceSpinner.getValue());