2 * jSite - NodeManagerPage.java - Copyright © 2006–2012 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.gui;
21 import java.awt.BorderLayout;
22 import java.awt.Dimension;
23 import java.awt.FlowLayout;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.Insets;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.KeyEvent;
29 import java.util.ArrayList;
30 import java.util.List;
32 import javax.swing.AbstractAction;
33 import javax.swing.Action;
34 import javax.swing.DefaultListModel;
35 import javax.swing.JButton;
36 import javax.swing.JLabel;
37 import javax.swing.JList;
38 import javax.swing.JOptionPane;
39 import javax.swing.JPanel;
40 import javax.swing.JScrollPane;
41 import javax.swing.JSpinner;
42 import javax.swing.JTextField;
43 import javax.swing.ListSelectionModel;
44 import javax.swing.SpinnerNumberModel;
45 import javax.swing.border.EmptyBorder;
46 import javax.swing.event.ChangeEvent;
47 import javax.swing.event.ChangeListener;
48 import javax.swing.event.DocumentEvent;
49 import javax.swing.event.DocumentListener;
50 import javax.swing.event.ListSelectionEvent;
51 import javax.swing.event.ListSelectionListener;
52 import javax.swing.text.BadLocationException;
53 import javax.swing.text.Document;
55 import de.todesbaum.jsite.application.Node;
56 import de.todesbaum.jsite.i18n.I18n;
57 import de.todesbaum.jsite.i18n.I18nContainer;
58 import de.todesbaum.util.swing.TLabel;
59 import de.todesbaum.util.swing.TWizard;
60 import de.todesbaum.util.swing.TWizardPage;
63 * Wizard page that lets the user edit his nodes.
65 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
67 public class NodeManagerPage extends TWizardPage implements ListSelectionListener, DocumentListener, ChangeListener {
69 /** List of node manager listeners. */
70 private List<NodeManagerListener> nodeManagerListeners = new ArrayList<NodeManagerListener>();
72 /** The “add node” action. */
73 protected Action addNodeAction;
75 /** The “delete node” action. */
76 protected Action deleteNodeAction;
78 /** The node list model. */
79 private DefaultListModel nodeListModel;
82 private JList nodeList;
84 /** The node name textfield. */
85 private JTextField nodeNameTextField;
87 /** The node hostname textfield. */
88 private JTextField nodeHostnameTextField;
90 /** The spinner for the node port. */
91 private JSpinner nodePortSpinner;
94 * Creates a new node manager wizard page.
97 * The wizard this page belongs to
99 public NodeManagerPage(final TWizard wizard) {
102 setHeading(I18n.getMessage("jsite.node-manager.heading"));
103 setDescription(I18n.getMessage("jsite.node-manager.description"));
104 I18nContainer.getInstance().registerRunnable(new Runnable() {
108 setHeading(I18n.getMessage("jsite.node-manager.heading"));
109 setDescription(I18n.getMessage("jsite.node-manager.description"));
115 * Adds a listener for node manager events.
117 * @param nodeManagerListener
118 * The listener to add
120 public void addNodeManagerListener(NodeManagerListener nodeManagerListener) {
121 nodeManagerListeners.add(nodeManagerListener);
125 * Removes a listener for node manager events.
127 * @param nodeManagerListener
128 * The listener to remove
130 public void removeNodeManagerListener(NodeManagerListener nodeManagerListener) {
131 nodeManagerListeners.remove(nodeManagerListener);
135 * Notifies all listeners that the node configuration has changed.
138 * The new list of nodes
140 protected void fireNodesUpdated(Node[] nodes) {
141 for (NodeManagerListener nodeManagerListener : nodeManagerListeners) {
142 nodeManagerListener.nodesUpdated(nodes);
147 * Notifies all listeners that a new node was selected.
150 * The newly selected node
152 protected void fireNodeSelected(Node node) {
153 for (NodeManagerListener nodeManagerListener : nodeManagerListeners) {
154 nodeManagerListener.nodeSelected(node);
159 * Creates all actions.
161 private void createActions() {
162 addNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.add-node")) {
165 @SuppressWarnings("synthetic-access")
166 public void actionPerformed(ActionEvent actionEvent) {
171 deleteNodeAction = new AbstractAction(I18n.getMessage("jsite.node-manager.delete-node")) {
174 @SuppressWarnings("synthetic-access")
175 public void actionPerformed(ActionEvent actionEvent) {
179 deleteNodeAction.setEnabled(false);
181 I18nContainer.getInstance().registerRunnable(new Runnable() {
185 addNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.node-manager.add-node"));
186 deleteNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.node-manager.delete-node"));
192 * Initializes the page and all components in it.
194 private void pageInit() {
196 nodeListModel = new DefaultListModel();
197 nodeList = new JList(nodeListModel);
198 nodeList.setName("node-list");
199 nodeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
200 nodeList.addListSelectionListener(this);
201 nodeList.setPreferredSize(new Dimension(250, -1));
203 nodeNameTextField = new JTextField("");
204 nodeNameTextField.getDocument().putProperty("Name", "node-name");
205 nodeNameTextField.getDocument().addDocumentListener(this);
206 nodeNameTextField.setEnabled(false);
208 nodeHostnameTextField = new JTextField("localhost");
209 nodeHostnameTextField.getDocument().putProperty("Name", "node-hostname");
210 nodeHostnameTextField.getDocument().addDocumentListener(this);
211 nodeHostnameTextField.setEnabled(false);
213 nodePortSpinner = new JSpinner(new SpinnerNumberModel(9481, 1, 65535, 1));
214 nodePortSpinner.setName("node-port");
215 nodePortSpinner.addChangeListener(this);
216 nodePortSpinner.setEnabled(false);
218 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
219 buttonPanel.setBorder(new EmptyBorder(-12, -12, -12, -12));
220 buttonPanel.add(new JButton(addNodeAction));
221 buttonPanel.add(new JButton(deleteNodeAction));
223 JPanel centerPanel = new JPanel(new BorderLayout());
224 JPanel nodeInformationPanel = new JPanel(new GridBagLayout());
225 centerPanel.add(nodeInformationPanel, BorderLayout.PAGE_START);
226 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));
227 final JLabel nodeInformationLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.node-manager.node-information") + "</b></html>");
228 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));
229 final TLabel nodeNameLabel = new TLabel(I18n.getMessage("jsite.node-manager.name") + ":", KeyEvent.VK_N, nodeNameTextField);
230 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));
231 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));
232 final TLabel nodeHostnameLabel = new TLabel(I18n.getMessage("jsite.node-manager.hostname") + ":", KeyEvent.VK_H, nodeHostnameTextField);
233 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));
234 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));
235 final TLabel nodePortLabel = new TLabel(I18n.getMessage("jsite.node-manager.port") + ":", KeyEvent.VK_P, nodePortSpinner);
236 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));
237 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));
239 setLayout(new BorderLayout(12, 12));
240 add(new JScrollPane(nodeList), BorderLayout.LINE_START);
241 add(centerPanel, BorderLayout.CENTER);
243 I18nContainer.getInstance().registerRunnable(new Runnable() {
247 nodeInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.node-manager.node-information") + "</b></html>");
248 nodeNameLabel.setText(I18n.getMessage("jsite.node-manager.name") + ":");
249 nodeHostnameLabel.setText(I18n.getMessage("jsite.node-manager.hostname") + ":");
250 nodePortLabel.setText(I18n.getMessage("jsite.node-manager.port") + ":");
259 public void pageAdded(TWizard wizard) {
260 this.wizard.setNextEnabled(nodeListModel.getSize() > 0);
261 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
262 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
263 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
267 * Sets the node list.
272 public void setNodes(Node[] nodes) {
273 nodeListModel.clear();
274 for (Node node : nodes) {
275 nodeListModel.addElement(node);
278 fireNodesUpdated(nodes);
282 * Returns the node list.
284 * @return The list of nodes
286 public Node[] getNodes() {
287 Node[] returnNodes = new Node[nodeListModel.getSize()];
288 for (int nodeIndex = 0, nodeCount = nodeListModel.getSize(); nodeIndex < nodeCount; nodeIndex++) {
289 returnNodes[nodeIndex] = (Node) nodeListModel.get(nodeIndex);
295 * Returns the currently selected node.
297 * @return The selected node, or <code>null</code> if no node is selected
299 private Node getSelectedNode() {
300 return (Node) nodeList.getSelectedValue();
304 * Updates node name or hostname when the user types into the textfields.
306 * @see #insertUpdate(DocumentEvent)
307 * @see #removeUpdate(DocumentEvent)
308 * @see #changedUpdate(DocumentEvent)
309 * @see DocumentListener
310 * @param documentEvent
313 private void updateTextField(DocumentEvent documentEvent) {
314 Node node = getSelectedNode();
318 Document document = documentEvent.getDocument();
319 String documentText = null;
321 documentText = document.getText(0, document.getLength());
322 } catch (BadLocationException ble1) {
325 if (documentText == null) {
328 String documentName = (String) document.getProperty("Name");
329 if ("node-name".equals(documentName)) {
330 node.setName(documentText);
332 fireNodesUpdated(getNodes());
333 } else if ("node-hostname".equals(documentName)) {
334 node.setHostname(documentText);
336 fireNodesUpdated(getNodes());
345 * Adds a new node to the list of nodes.
347 private void addNode() {
348 Node node = new Node("localhost", 9481, I18n.getMessage("jsite.node-manager.new-node"));
349 nodeListModel.addElement(node);
350 deleteNodeAction.setEnabled(nodeListModel.size() > 1);
351 wizard.setNextEnabled(true);
352 fireNodesUpdated(getNodes());
356 * Deletes the currently selected node from the list of nodes.
358 private void deleteNode() {
359 Node node = getSelectedNode();
363 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.node-manager.delete-node.warning"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION) {
366 int nodeIndex = nodeListModel.indexOf(node);
367 nodeListModel.removeElement(node);
369 fireNodeSelected((Node) nodeListModel.get(Math.min(nodeIndex, nodeListModel.size() - 1)));
370 fireNodesUpdated(getNodes());
371 deleteNodeAction.setEnabled(nodeListModel.size() > 1);
372 wizard.setNextEnabled(nodeListModel.size() > 0);
376 // INTERFACE ListSelectionListener
383 @SuppressWarnings("null")
384 public void valueChanged(ListSelectionEvent e) {
385 Object source = e.getSource();
386 if (source instanceof JList) {
387 JList sourceList = (JList) source;
388 if ("node-list".equals(sourceList.getName())) {
389 Node node = (Node) sourceList.getSelectedValue();
390 boolean enabled = (node != null);
391 nodeNameTextField.setEnabled(enabled);
392 nodeHostnameTextField.setEnabled(enabled);
393 nodePortSpinner.setEnabled(enabled);
394 deleteNodeAction.setEnabled(enabled && (nodeListModel.size() > 1));
396 nodeNameTextField.setText(node.getName());
397 nodeHostnameTextField.setText(node.getHostname());
398 nodePortSpinner.setValue(node.getPort());
400 nodeNameTextField.setText("");
401 nodeHostnameTextField.setText("localhost");
402 nodePortSpinner.setValue(9481);
409 // INTERFACE DocumentListener
416 public void insertUpdate(DocumentEvent e) {
424 public void removeUpdate(DocumentEvent e) {
432 public void changedUpdate(DocumentEvent e) {
437 // INTERFACE ChangeListener
444 public void stateChanged(ChangeEvent e) {
445 Object source = e.getSource();
446 Node selectedNode = getSelectedNode();
447 if (selectedNode == null) {
450 if (source instanceof JSpinner) {
451 JSpinner sourceSpinner = (JSpinner) source;
452 if ("node-port".equals(sourceSpinner.getName())) {
453 selectedNode.setPort((Integer) sourceSpinner.getValue());
454 fireNodeSelected(selectedNode);