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.jsite.i18n.I18nContainer;
59 import de.todesbaum.util.swing.TLabel;
60 import de.todesbaum.util.swing.TWizard;
61 import de.todesbaum.util.swing.TWizardPage;
64 * @author David Roden <droden@gmail.com>
67 public class NodeManagerPage extends TWizardPage implements ListSelectionListener, DocumentListener, ChangeListener {
69 private List<NodeManagerListener> nodeManagerListeners = new ArrayList<NodeManagerListener>();
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;
79 public NodeManagerPage(final TWizard wizard) {
82 setHeading(I18n.getMessage("jsite.node-manager.heading"));
83 setDescription(I18n.getMessage("jsite.node-manager.description"));
84 I18nContainer.getInstance().registerRunnable(new Runnable() {
87 setHeading(I18n.getMessage("jsite.node-manager.heading"));
88 setDescription(I18n.getMessage("jsite.node-manager.description"));
93 public void addNodeManagerListener(NodeManagerListener nodeManagerListener) {
94 nodeManagerListeners.add(nodeManagerListener);
97 public void removeNodeManagerListener(NodeManagerListener nodeManagerListener) {
98 nodeManagerListeners.remove(nodeManagerListener);
101 protected void fireNodesUpdated(Node[] nodes) {
102 for (NodeManagerListener nodeManagerListener: nodeManagerListeners) {
103 nodeManagerListener.nodesUpdated(nodes);
107 private void createActions() {
108 addNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.add-node")) {
110 public void actionPerformed(ActionEvent actionEvent) {
115 deleteNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.delete-node")) {
117 public void actionPerformed(ActionEvent actionEvent) {
121 deleteNodeAction.setEnabled(false);
123 I18nContainer.getInstance().registerRunnable(new Runnable() {
126 addNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.node-manager.add-node"));
127 deleteNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.node-manager.delete-node"));
132 private void pageInit() {
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));
141 nodeNameTextField = new JTextField("");
142 nodeNameTextField.getDocument().putProperty("Name", "node-name");
143 nodeNameTextField.getDocument().addDocumentListener(this);
144 nodeNameTextField.setEnabled(false);
146 nodeHostnameTextField = new JTextField("localhost");
147 nodeHostnameTextField.getDocument().putProperty("Name", "node-hostname");
148 nodeHostnameTextField.getDocument().addDocumentListener(this);
149 nodeHostnameTextField.setEnabled(false);
151 nodePortSpinner = new JSpinner(new SpinnerNumberModel(9481, 1, 65535, 1));
152 nodePortSpinner.setName("node-port");
153 nodePortSpinner.addChangeListener(this);
154 nodePortSpinner.setEnabled(false);
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));
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));
177 setLayout(new BorderLayout(12, 12));
178 add(new JScrollPane(nodeList), BorderLayout.LINE_START);
179 add(centerPanel, BorderLayout.CENTER);
181 I18nContainer.getInstance().registerRunnable(new Runnable() {
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") + ":");
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"));
203 public void setNodes(Node[] nodes) {
204 nodeListModel.clear();
205 for (Node node: nodes) {
206 nodeListModel.addElement(node);
209 fireNodesUpdated(nodes);
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);
220 private Node getSelectedNode() {
221 return (Node) nodeList.getSelectedValue();
224 private void updateTextField(DocumentEvent documentEvent) {
225 Node node = getSelectedNode();
229 Document document = documentEvent.getDocument();
230 String documentText = null;
232 documentText = document.getText(0, document.getLength());
233 } catch (BadLocationException ble1) {
235 if (documentText == null) {
238 String documentName = (String) document.getProperty("Name");
239 if ("node-name".equals(documentName)) {
240 node.setName(documentText);
242 fireNodesUpdated(getNodes());
243 } else if ("node-hostname".equals(documentName)) {
244 node.setHostname(documentText);
246 fireNodesUpdated(getNodes());
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());
261 protected void deleteNode() {
262 Node node = getSelectedNode();
266 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.node-manager.delete-node.warning"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION) {
269 nodeListModel.removeElement(node);
271 fireNodesUpdated(getNodes());
272 wizard.setNextEnabled(nodeListModel.size() > 0);
276 // INTERFACE ListSelectionListener
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);
294 nodeNameTextField.setText(node.getName());
295 nodeHostnameTextField.setText(node.getHostname());
296 nodePortSpinner.setValue(node.getPort());
298 nodeNameTextField.setText("");
299 nodeHostnameTextField.setText("localhost");
300 nodePortSpinner.setValue(9481);
307 // INTERFACE DocumentListener
313 public void insertUpdate(DocumentEvent e) {
320 public void removeUpdate(DocumentEvent e) {
327 public void changedUpdate(DocumentEvent e) {
332 // INTERFACE ChangeListener
338 public void stateChanged(ChangeEvent e) {
339 Object source = e.getSource();
340 Node selectedNode = getSelectedNode();
341 if (selectedNode == null) {
344 if (source instanceof JSpinner) {
345 JSpinner sourceSpinner = (JSpinner) source;
346 if ("node-port".equals(sourceSpinner.getName())) {
347 selectedNode.setPort((Integer) sourceSpinner.getValue());