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.main.Version;
43 import net.pterodactylus.util.swing.SwingUtils;
46 * Dialog that lets the user edit the properties of a node.
48 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
51 public class EditNodeDialog extends JDialog implements I18nable {
53 /** The user-given name of the node. */
56 /** The hostname of the node. */
57 private String hostname;
59 /** The FNP port number of the node. */
62 /** Whether the node is on the same machine. */
63 private boolean sameMachine;
65 /** Action of the okay button. */
66 private I18nAction okayAction;
68 /** Action of the cancel button. */
69 private I18nAction cancelAction;
71 /** The name label. */
72 private I18nLabel nameLabel;
74 /** The name textfield. */
75 private JTextField nameTextField;
77 /** The hostname label. */
78 private I18nLabel hostnameLabel;
80 /** The hostname textfield. */
81 private JTextField hostnameTextField;
83 /** The port label. */
84 private I18nLabel portLabel;
86 /** The port textfield. */
87 private JTextField portTextField;
89 /** The same machine checkbox. */
90 private JCheckBox sameMachineCheckBox;
92 /** Whether the dialog was cancelled. */
93 private boolean cancelled;
96 * Creates a new node edit dialog with the given parent.
99 * The parent dialog of this dialog
101 public EditNodeDialog(JDialog parentDialog) {
102 super(parentDialog, I18n.get("editNodeDialog.title") + " – jSite " + Version.getVersion(), true);
106 I18n.registerI18nable(this);
107 SwingUtils.center(this);
115 * Returns the user-given name of the node.
117 * @return The user-given name of the node
119 public String getNodeName() {
124 * Sets the user-given name of the node.
127 * The name of the node
129 public void setNodeName(String name) {
131 nameTextField.setText(name);
135 * Returns the hostname of the node.
137 * @return The hostname of the node
139 public String getNodeHostname() {
144 * Sets the hostname of the node.
147 * The hostname of the node
149 public void setNodeHostname(String hostname) {
150 this.hostname = hostname;
151 hostnameTextField.setText(hostname);
155 * Returns the FCP port number of the node.
157 * @return The FCP port number of the node
159 public int getNodePort() {
164 * Sets the FCP port number of the node.
167 * The FCP port number of the node
169 public void setNodePort(int port) {
171 portTextField.setText(String.valueOf(port));
175 * Returns whether the node is on the same machine as jSite.
177 * @return <code>true</code> if the node is on the same machine as jSite,
178 * <code>false</code> otherwise
180 public boolean isNodeOnSameMachine() {
185 * Sets whether the node is on the same machine as jSite.
188 * <code>true</code> if the node is on the same machine as
189 * jSite, <code>false</code> otherwise
191 public void setNodeOnSameMachine(boolean sameMachine) {
192 this.sameMachine = sameMachine;
193 sameMachineCheckBox.setSelected(sameMachine);
197 * Returns whether the dialog was cancelled.
199 * @return <code>true</code> if the dialog was cancelled,
200 * <code>false</code> if the user clicked “okay”
202 public boolean wasCancelled() {
211 * Initializes all actions.
213 private void initActions() {
214 okayAction = new I18nAction("general.button.okay") {
219 @SuppressWarnings("synthetic-access")
220 public void actionPerformed(ActionEvent e) {
224 cancelAction = new I18nAction("general.button.cancel") {
229 @SuppressWarnings("synthetic-access")
230 public void actionPerformed(ActionEvent e) {
237 * Initializes all components.
239 private void initComponents() {
240 JPanel rootPanel = new JPanel(new BorderLayout(12, 12));
241 setContentPane(rootPanel);
242 rootPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
244 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
245 rootPanel.add(buttonPanel, BorderLayout.PAGE_END);
246 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
247 buttonPanel.add(new JButton(cancelAction));
248 JButton okayButton = new JButton(okayAction);
249 buttonPanel.add(okayButton);
250 getRootPane().setDefaultButton(okayButton);
252 JPanel contentPanel = new JPanel(new GridBagLayout());
253 rootPanel.add(contentPanel, BorderLayout.CENTER);
254 contentPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
256 nameTextField = new JTextField();
257 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));
258 contentPanel.add(nameTextField, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 12, 0, 0), 0, 0));
260 hostnameTextField = new JTextField();
261 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));
262 contentPanel.add(hostnameTextField, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0));
264 portTextField = new JTextField();
265 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));
266 contentPanel.add(portTextField, new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0));
268 sameMachineCheckBox = new JCheckBox(new I18nAction("editNodeDialog.checkbox.sameMachine") {
270 public void actionPerformed(ActionEvent e) {
271 /* don't do anything. */
274 contentPanel.add(sameMachineCheckBox, new GridBagConstraints(0, 3, 2, 1, 1, 1, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
282 * Checks the name textfield for valid input.
284 * @return <code>true</code> if the name textfield seem okay,
285 * <code>false</code> if there is an error
287 private boolean verifyName() {
288 return (nameTextField.getText().trim().length() != 0);
292 * Verifies the hostname textfield by resolving the given name.
294 * @return <code>true</code> if the hostname is not empty and can be
295 * resolved, <code>false</code> otherwise
297 private boolean verifyHostname() {
298 if (hostnameTextField.getText().trim().length() == 0) {
302 InetAddress.getByName(hostnameTextField.getText().trim());
304 } catch (UnknownHostException uhe1) {
310 * Verifies that the port number is numeric and in the range from
311 * <code>0</code> to <code>65535</code>.
313 * @return <code>true</code> if the port number is okay,
314 * <code>false</code> otherwise
316 private boolean verifyPort() {
318 int portNumber = Integer.valueOf(portTextField.getText().trim());
319 if ((portNumber > 0) && (portNumber < 65536)) {
322 } catch (NumberFormatException nfe1) {
329 * Confirms the node settings and closes the dialog.
331 private void confirm() {
333 JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.name.message"), I18n.get("editNodeDialog.error.name.title"), JOptionPane.ERROR_MESSAGE);
336 if (!verifyHostname()) {
337 JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.hostname.message"), I18n.get("editNodeDialog.error.hostname.title"), JOptionPane.ERROR_MESSAGE);
341 JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.port.message"), I18n.get("editNodeDialog.error.port.title"), JOptionPane.ERROR_MESSAGE);
344 name = nameTextField.getText().trim();
345 hostname = hostnameTextField.getText().trim();
347 port = Integer.parseInt(portTextField.getText().trim());
348 } catch (NumberFormatException nfe1) {
349 /* should not occur, the value was checked! */
350 assert false: "port number is invalid though it was checked!";
352 sameMachine = sameMachineCheckBox.isSelected();
358 * Cancels the node settings and closes the dialog.
360 private void cancel() {
366 // INTERFACE I18nable
372 public void updateI18n() {
373 okayAction.updateI18n();
374 cancelAction.updateI18n();
375 nameLabel.updateI18n();
376 hostnameLabel.updateI18n();
377 portLabel.updateI18n();
378 setTitle(I18n.get("editNodeDialog.title") + " – jSite " + Version.getVersion());
379 SwingUtils.repackCentered(this);