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 @SuppressWarnings("synthetic-access")
111 public void actionPerformed(ActionEvent actionEvent) {
116 deleteNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.delete-node")) {
118 @SuppressWarnings("synthetic-access")
119 public void actionPerformed(ActionEvent actionEvent) {
123 deleteNodeAction.setEnabled(false);
125 I18nContainer.getInstance().registerRunnable(new Runnable() {
128 addNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.node-manager.add-node"));
129 deleteNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.node-manager.delete-node"));
134 private void pageInit() {
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));
143 nodeNameTextField = new JTextField("");
144 nodeNameTextField.getDocument().putProperty("Name", "node-name");
145 nodeNameTextField.getDocument().addDocumentListener(this);
146 nodeNameTextField.setEnabled(false);
148 nodeHostnameTextField = new JTextField("localhost");
149 nodeHostnameTextField.getDocument().putProperty("Name", "node-hostname");
150 nodeHostnameTextField.getDocument().addDocumentListener(this);
151 nodeHostnameTextField.setEnabled(false);
153 nodePortSpinner = new JSpinner(new SpinnerNumberModel(9481, 1, 65535, 1));
154 nodePortSpinner.setName("node-port");
155 nodePortSpinner.addChangeListener(this);
156 nodePortSpinner.setEnabled(false);
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));
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));
179 setLayout(new BorderLayout(12, 12));
180 add(new JScrollPane(nodeList), BorderLayout.LINE_START);
181 add(centerPanel, BorderLayout.CENTER);
183 I18nContainer.getInstance().registerRunnable(new Runnable() {
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") + ":");
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"));
205 public void setNodes(Node[] nodes) {
206 nodeListModel.clear();
207 for (Node node: nodes) {
208 nodeListModel.addElement(node);
211 fireNodesUpdated(nodes);
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);
222 private Node getSelectedNode() {
223 return (Node) nodeList.getSelectedValue();
226 private void updateTextField(DocumentEvent documentEvent) {
227 Node node = getSelectedNode();
231 Document document = documentEvent.getDocument();
232 String documentText = null;
234 documentText = document.getText(0, document.getLength());
235 } catch (BadLocationException ble1) {
237 if (documentText == null) {
240 String documentName = (String) document.getProperty("Name");
241 if ("node-name".equals(documentName)) {
242 node.setName(documentText);
244 fireNodesUpdated(getNodes());
245 } else if ("node-hostname".equals(documentName)) {
246 node.setHostname(documentText);
248 fireNodesUpdated(getNodes());
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());
263 private void deleteNode() {
264 Node node = getSelectedNode();
268 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.node-manager.delete-node.warning"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION) {
271 nodeListModel.removeElement(node);
273 fireNodesUpdated(getNodes());
274 wizard.setNextEnabled(nodeListModel.size() > 0);
278 // INTERFACE ListSelectionListener
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);
296 nodeNameTextField.setText(node.getName());
297 nodeHostnameTextField.setText(node.getHostname());
298 nodePortSpinner.setValue(node.getPort());
300 nodeNameTextField.setText("");
301 nodeHostnameTextField.setText("localhost");
302 nodePortSpinner.setValue(9481);
309 // INTERFACE DocumentListener
315 public void insertUpdate(DocumentEvent e) {
322 public void removeUpdate(DocumentEvent e) {
329 public void changedUpdate(DocumentEvent e) {
334 // INTERFACE ChangeListener
340 public void stateChanged(ChangeEvent e) {
341 Object source = e.getSource();
342 Node selectedNode = getSelectedNode();
343 if (selectedNode == null) {
346 if (source instanceof JSpinner) {
347 JSpinner sourceSpinner = (JSpinner) source;
348 if ("node-port".equals(sourceSpinner.getName())) {
349 selectedNode.setPort((Integer) sourceSpinner.getValue());