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 /** The same machine action. */
95 private I18nAction sameMachineAction;
97 /** Whether the dialog was cancelled. */
98 private boolean cancelled;
101 * Creates a new node edit dialog with the given parent.
103 * @param parentDialog
104 * The parent dialog of this dialog
106 public EditNodeDialog(JDialog parentDialog) {
107 super(parentDialog, I18n.get("editNodeDialog.title") + " – jSite " + Version.getVersion(), true);
111 I18n.registerI18nable(this);
112 SwingUtils.center(this);
120 * Returns the user-given name of the node.
122 * @return The user-given name of the node
124 public String getNodeName() {
129 * Sets the user-given name of the node.
132 * The name of the node
134 public void setNodeName(String name) {
136 nameTextField.setText(name);
140 * Returns the hostname of the node.
142 * @return The hostname of the node
144 public String getNodeHostname() {
149 * Sets the hostname of the node.
152 * The hostname of the node
154 public void setNodeHostname(String hostname) {
155 this.hostname = hostname;
156 hostnameTextField.setText(hostname);
160 * Returns the FCP port number of the node.
162 * @return The FCP port number of the node
164 public int getNodePort() {
169 * Sets the FCP port number of the node.
172 * The FCP port number of the node
174 public void setNodePort(int port) {
176 portTextField.setText(String.valueOf(port));
180 * Returns whether the node is on the same machine as jSite.
182 * @return <code>true</code> if the node is on the same machine as jSite,
183 * <code>false</code> otherwise
185 public boolean isNodeOnSameMachine() {
190 * Sets whether the node is on the same machine as jSite.
193 * <code>true</code> if the node is on the same machine as
194 * jSite, <code>false</code> otherwise
196 public void setNodeOnSameMachine(boolean sameMachine) {
197 this.sameMachine = sameMachine;
198 sameMachineCheckBox.setSelected(sameMachine);
202 * Returns whether the dialog was cancelled.
204 * @return <code>true</code> if the dialog was cancelled,
205 * <code>false</code> if the user clicked “okay”
207 public boolean wasCancelled() {
216 * Initializes all actions.
218 private void initActions() {
219 okayAction = new I18nAction("general.button.okay") {
224 @SuppressWarnings("synthetic-access")
225 public void actionPerformed(ActionEvent e) {
229 cancelAction = new I18nAction("general.button.cancel") {
234 @SuppressWarnings("synthetic-access")
235 public void actionPerformed(ActionEvent e) {
239 sameMachineAction = new I18nAction("editNodeDialog.checkbox.sameMachine") {
241 public void actionPerformed(ActionEvent e) {
242 /* don't do anything. */
248 * Initializes all components.
250 private void initComponents() {
251 JPanel rootPanel = new JPanel(new BorderLayout(12, 12));
252 setContentPane(rootPanel);
253 rootPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
255 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
256 rootPanel.add(buttonPanel, BorderLayout.PAGE_END);
257 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
258 buttonPanel.add(new JButton(cancelAction));
259 JButton okayButton = new JButton(okayAction);
260 buttonPanel.add(okayButton);
261 getRootPane().setDefaultButton(okayButton);
263 JPanel contentPanel = new JPanel(new GridBagLayout());
264 rootPanel.add(contentPanel, BorderLayout.CENTER);
265 contentPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
267 nameTextField = new JTextField();
268 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));
269 contentPanel.add(nameTextField, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 12, 0, 0), 0, 0));
271 hostnameTextField = new JTextField();
272 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));
273 contentPanel.add(hostnameTextField, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0));
275 portTextField = new JTextField();
276 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));
277 contentPanel.add(portTextField, new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0));
279 sameMachineCheckBox = new JCheckBox(sameMachineAction);
280 contentPanel.add(sameMachineCheckBox, new GridBagConstraints(0, 3, 2, 1, 1, 1, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
288 * Checks the name textfield for valid input.
290 * @return <code>true</code> if the name textfield seem okay,
291 * <code>false</code> if there is an error
293 private boolean verifyName() {
294 return (nameTextField.getText().trim().length() != 0);
298 * Verifies the hostname textfield by resolving the given name.
300 * @return <code>true</code> if the hostname is not empty and can be
301 * resolved, <code>false</code> otherwise
303 private boolean verifyHostname() {
304 if (hostnameTextField.getText().trim().length() == 0) {
308 InetAddress.getByName(hostnameTextField.getText().trim());
310 } catch (UnknownHostException uhe1) {
316 * Verifies that the port number is numeric and in the range from
317 * <code>0</code> to <code>65535</code>.
319 * @return <code>true</code> if the port number is okay,
320 * <code>false</code> otherwise
322 private boolean verifyPort() {
324 int portNumber = Integer.valueOf(portTextField.getText().trim());
325 if ((portNumber > 0) && (portNumber < 65536)) {
328 } catch (NumberFormatException nfe1) {
335 * Confirms the node settings and closes the dialog.
337 private void confirm() {
339 JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.name.message"), I18n.get("editNodeDialog.error.name.title"), JOptionPane.ERROR_MESSAGE);
342 if (!verifyHostname()) {
343 JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.hostname.message"), I18n.get("editNodeDialog.error.hostname.title"), JOptionPane.ERROR_MESSAGE);
347 JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.port.message"), I18n.get("editNodeDialog.error.port.title"), JOptionPane.ERROR_MESSAGE);
350 name = nameTextField.getText().trim();
351 hostname = hostnameTextField.getText().trim();
353 port = Integer.parseInt(portTextField.getText().trim());
354 } catch (NumberFormatException nfe1) {
355 /* should not occur, the value was checked! */
356 assert false: "port number is invalid though it was checked!";
358 sameMachine = sameMachineCheckBox.isSelected();
364 * Cancels the node settings and closes the dialog.
366 private void cancel() {
372 // INTERFACE I18nable
378 public void updateI18n() {
379 okayAction.updateI18n();
380 cancelAction.updateI18n();
381 nameLabel.updateI18n();
382 hostnameLabel.updateI18n();
383 portLabel.updateI18n();
384 sameMachineAction.updateI18n();
385 setTitle(I18n.get("editNodeDialog.title") + " – jSite " + Version.getVersion());
386 SwingUtils.repackCentered(this);