2 * jSite2 - NodeEditDialog.java -
3 * Copyright © 2008 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 net.pterodactylus.jsite.gui;
22 import java.awt.BorderLayout;
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.net.InetAddress;
29 import java.net.UnknownHostException;
31 import javax.swing.BorderFactory;
32 import javax.swing.JButton;
33 import javax.swing.JCheckBox;
34 import javax.swing.JDialog;
35 import javax.swing.JOptionPane;
36 import javax.swing.JPanel;
37 import javax.swing.JTextField;
38 import javax.swing.border.EtchedBorder;
40 import net.pterodactylus.jsite.i18n.I18n;
41 import net.pterodactylus.jsite.i18n.I18nable;
42 import net.pterodactylus.jsite.i18n.gui.I18nAction;
43 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
44 import net.pterodactylus.jsite.main.Version;
45 import net.pterodactylus.util.swing.SwingUtils;
48 * Dialog that lets the user edit the properties of a node.
50 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
53 public class EditNodeDialog extends JDialog implements I18nable {
55 /** The user-given name of the node. */
58 /** The hostname of the node. */
59 private String hostname;
61 /** The FNP port number of the node. */
64 /** Whether the node is on the same machine. */
65 private boolean sameMachine;
67 /** Action of the okay button. */
68 private I18nAction okayAction;
70 /** Action of the cancel button. */
71 private I18nAction cancelAction;
73 /** The name label. */
74 private I18nLabel nameLabel;
76 /** The name textfield. */
77 private JTextField nameTextField;
79 /** The hostname label. */
80 private I18nLabel hostnameLabel;
82 /** The hostname textfield. */
83 private JTextField hostnameTextField;
85 /** The port label. */
86 private I18nLabel portLabel;
88 /** The port textfield. */
89 private JTextField portTextField;
91 /** The same machine checkbox. */
92 private JCheckBox sameMachineCheckBox;
94 /** Whether the dialog was cancelled. */
95 private boolean cancelled;
98 * Creates a new node edit dialog with the given parent.
100 * @param parentDialog
101 * The parent dialog of this dialog
103 public EditNodeDialog(JDialog parentDialog) {
104 super(parentDialog, I18n.get("editNodeDialog.title") + " – jSite " + Version.getVersion(), true);
108 I18n.registerI18nable(this);
109 SwingUtils.center(this);
117 * Returns the user-given name of the node.
119 * @return The user-given name of the node
121 public String getNodeName() {
126 * Sets the user-given name of the node.
129 * The name of the node
131 public void setNodeName(String name) {
133 nameTextField.setText(name);
137 * Returns the hostname of the node.
139 * @return The hostname of the node
141 public String getNodeHostname() {
146 * Sets the hostname of the node.
149 * The hostname of the node
151 public void setNodeHostname(String hostname) {
152 this.hostname = hostname;
153 hostnameTextField.setText(hostname);
157 * Returns the FCP port number of the node.
159 * @return The FCP port number of the node
161 public int getNodePort() {
166 * Sets the FCP port number of the node.
169 * The FCP port number of the node
171 public void setNodePort(int port) {
173 portTextField.setText(String.valueOf(port));
177 * Returns whether the node is on the same machine as jSite.
179 * @return <code>true</code> if the node is on the same machine as jSite,
180 * <code>false</code> otherwise
182 public boolean isNodeOnSameMachine() {
187 * Sets whether the node is on the same machine as jSite.
190 * <code>true</code> if the node is on the same machine as
191 * jSite, <code>false</code> otherwise
193 public void setNodeOnSameMachine(boolean sameMachine) {
194 this.sameMachine = sameMachine;
195 sameMachineCheckBox.setSelected(sameMachine);
199 * Returns whether the dialog was cancelled.
201 * @return <code>true</code> if the dialog was cancelled,
202 * <code>false</code> if the user clicked “okay”
204 public boolean wasCancelled() {
213 * Initializes all actions.
215 private void initActions() {
216 okayAction = new I18nAction("general.button.okay") {
221 @SuppressWarnings("synthetic-access")
222 public void actionPerformed(ActionEvent e) {
226 cancelAction = new I18nAction("general.button.cancel") {
231 @SuppressWarnings("synthetic-access")
232 public void actionPerformed(ActionEvent e) {
239 * Initializes all components.
241 private void initComponents() {
242 JPanel rootPanel = new JPanel(new BorderLayout(12, 12));
243 setContentPane(rootPanel);
244 rootPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
246 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
247 rootPanel.add(buttonPanel, BorderLayout.PAGE_END);
248 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
249 buttonPanel.add(new JButton(cancelAction));
250 JButton okayButton = new JButton(okayAction);
251 buttonPanel.add(okayButton);
252 getRootPane().setDefaultButton(okayButton);
254 JPanel contentPanel = new JPanel(new GridBagLayout());
255 rootPanel.add(contentPanel, BorderLayout.CENTER);
256 contentPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
258 nameTextField = new JTextField();
259 contentPanel.add(nameLabel = new I18nLabel("editNodeDialog.label.name", nameTextField), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
260 contentPanel.add(nameTextField, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 12, 0, 0), 0, 0));
262 hostnameTextField = new JTextField();
263 contentPanel.add(hostnameLabel = new I18nLabel("editNodeDialog.label.hostname", hostnameTextField), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
264 contentPanel.add(hostnameTextField, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0));
266 portTextField = new JTextField();
267 contentPanel.add(portLabel = new I18nLabel("editNodeDialog.label.port", portTextField), new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
268 contentPanel.add(portTextField, new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0));
270 sameMachineCheckBox = new JCheckBox(new I18nAction("editNodeDialog.checkbox.sameMachine") {
272 public void actionPerformed(ActionEvent e) {
273 /* don't do anything. */
276 contentPanel.add(sameMachineCheckBox, new GridBagConstraints(0, 3, 2, 1, 1, 1, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
284 * Checks the name textfield for valid input.
286 * @return <code>true</code> if the name textfield seem okay,
287 * <code>false</code> if there is an error
289 private boolean verifyName() {
290 return (nameTextField.getText().trim().length() != 0);
294 * Verifies the hostname textfield by resolving the given name.
296 * @return <code>true</code> if the hostname is not empty and can be
297 * resolved, <code>false</code> otherwise
299 private boolean verifyHostname() {
300 if (hostnameTextField.getText().trim().length() == 0) {
304 InetAddress.getByName(hostnameTextField.getText().trim());
306 } catch (UnknownHostException uhe1) {
312 * Verifies that the port number is numeric and in the range from
313 * <code>0</code> to <code>65535</code>.
315 * @return <code>true</code> if the port number is okay,
316 * <code>false</code> otherwise
318 private boolean verifyPort() {
320 int portNumber = Integer.valueOf(portTextField.getText().trim());
321 if ((portNumber > 0) && (portNumber < 65536)) {
324 } catch (NumberFormatException nfe1) {
331 * Confirms the node settings and closes the dialog.
333 private void confirm() {
335 JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.name.message"), I18n.get("editNodeDialog.error.name.title"), JOptionPane.ERROR_MESSAGE);
338 if (!verifyHostname()) {
339 JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.hostname.message"), I18n.get("editNodeDialog.error.hostname.title"), JOptionPane.ERROR_MESSAGE);
343 JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.port.message"), I18n.get("editNodeDialog.error.port.title"), JOptionPane.ERROR_MESSAGE);
346 name = nameTextField.getText().trim();
347 hostname = hostnameTextField.getText().trim();
349 port = Integer.parseInt(portTextField.getText().trim());
350 } catch (NumberFormatException nfe1) {
351 /* should not occur, the value was checked! */
352 assert false: "port number is invalid though it was checked!";
354 sameMachine = sameMachineCheckBox.isSelected();
360 * Cancels the node settings and closes the dialog.
362 private void cancel() {
368 // INTERFACE I18nable
374 public void updateI18n() {
375 okayAction.updateI18n();
376 cancelAction.updateI18n();
377 nameLabel.updateI18n();
378 hostnameLabel.updateI18n();
379 portLabel.updateI18n();
380 setTitle(I18n.get("editNodeDialog.title") + " – jSite " + Version.getVersion());
381 SwingUtils.repackCentered(this);