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.Action;
32 import javax.swing.BorderFactory;
33 import javax.swing.JButton;
34 import javax.swing.JCheckBox;
35 import javax.swing.JComponent;
36 import javax.swing.JDialog;
37 import javax.swing.JLabel;
38 import javax.swing.JOptionPane;
39 import javax.swing.JPanel;
40 import javax.swing.JTextField;
41 import javax.swing.border.EtchedBorder;
43 import net.pterodactylus.jsite.i18n.I18n;
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 {
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 Action okayAction;
70 /** Action of the cancel button. */
71 private Action cancelAction;
73 /** The name textfield. */
74 private JTextField nameTextField;
76 /** The hostname textfield. */
77 private JTextField hostnameTextField;
79 /** The port textfield. */
80 private JTextField portTextField;
82 /** The same machine checkbox. */
83 private JCheckBox sameMachineCheckBox;
85 /** Whether the dialog was cancelled. */
86 private boolean cancelled;
89 * Creates a new node edit dialog with the given parent.
92 * The parent dialog of this dialog
94 public EditNodeDialog(JDialog parentDialog) {
95 super(parentDialog, I18n.get("editNodeDialog.title") + " – jSite " + Version.getVersion(), true);
99 SwingUtils.center(this);
107 * Returns the user-given name of the node.
109 * @return The user-given name of the node
111 public String getNodeName() {
116 * Sets the user-given name of the node.
119 * The name of the node
121 public void setNodeName(String name) {
123 nameTextField.setText(name);
127 * Returns the hostname of the node.
129 * @return The hostname of the node
131 public String getNodeHostname() {
136 * Sets the hostname of the node.
139 * The hostname of the node
141 public void setNodeHostname(String hostname) {
142 this.hostname = hostname;
143 hostnameTextField.setText(hostname);
147 * Returns the FCP port number of the node.
149 * @return The FCP port number of the node
151 public int getNodePort() {
156 * Sets the FCP port number of the node.
159 * The FCP port number of the node
161 public void setNodePort(int port) {
163 portTextField.setText(String.valueOf(port));
167 * Returns whether the node is on the same machine as jSite.
169 * @return <code>true</code> if the node is on the same machine as jSite,
170 * <code>false</code> otherwise
172 public boolean isNodeOnSameMachine() {
177 * Sets whether the node is on the same machine as jSite.
180 * <code>true</code> if the node is on the same machine as
181 * jSite, <code>false</code> otherwise
183 public void setNodeOnSameMachine(boolean sameMachine) {
184 this.sameMachine = sameMachine;
185 sameMachineCheckBox.setSelected(sameMachine);
189 * Returns whether the dialog was cancelled.
191 * @return <code>true</code> if the dialog was cancelled,
192 * <code>false</code> if the user clicked “okay”
194 public boolean wasCancelled() {
203 * Initializes all actions.
205 private void initActions() {
206 okayAction = new I18nAction("general.button.okay") {
211 @SuppressWarnings("synthetic-access")
212 public void actionPerformed(ActionEvent e) {
216 cancelAction = new I18nAction("general.button.cancel") {
221 @SuppressWarnings("synthetic-access")
222 public void actionPerformed(ActionEvent e) {
229 * Initializes all components.
231 private void initComponents() {
232 JPanel rootPanel = new JPanel(new BorderLayout(12, 12));
233 setContentPane(rootPanel);
234 rootPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
236 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
237 rootPanel.add(buttonPanel, BorderLayout.PAGE_END);
238 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
239 buttonPanel.add(new JButton(cancelAction));
240 JButton okayButton = new JButton(okayAction);
241 buttonPanel.add(okayButton);
242 getRootPane().setDefaultButton(okayButton);
244 JPanel contentPanel = new JPanel(new GridBagLayout());
245 rootPanel.add(contentPanel, BorderLayout.CENTER);
246 contentPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
248 nameTextField = new JTextField();
249 contentPanel.add(createLabel(I18n.get("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));
250 contentPanel.add(nameTextField, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 12, 0, 0), 0, 0));
252 hostnameTextField = new JTextField();
253 contentPanel.add(createLabel(I18n.get("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));
254 contentPanel.add(hostnameTextField, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0));
256 portTextField = new JTextField();
257 contentPanel.add(createLabel(I18n.get("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));
258 contentPanel.add(portTextField, new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0));
260 sameMachineCheckBox = new JCheckBox(new I18nAction("editNodeDialog.checkbox.sameMachine") {
262 public void actionPerformed(ActionEvent e) {
263 /* don't do anything. */
266 contentPanel.add(sameMachineCheckBox, new GridBagConstraints(0, 3, 2, 1, 1, 1, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
270 * Creates a label whose name and mnemonic are initialized from the given
273 * @param i18nBasename
274 * The i18n property basename of the label
276 * The component this label describes
277 * @return The created label
279 private JLabel createLabel(String i18nBasename, JComponent labelFor) {
280 JLabel label = new JLabel(I18n.get(i18nBasename + ".name"));
281 label.setDisplayedMnemonic(I18n.getKey(i18nBasename + ".mnemonic"));
282 label.setLabelFor(labelFor);
291 * Checks the name textfield for valid input.
293 * @return <code>true</code> if the name textfield seem okay,
294 * <code>false</code> if there is an error
296 private boolean verifyName() {
297 return (nameTextField.getText().trim().length() != 0);
301 * Verifies the hostname textfield by resolving the given name.
303 * @return <code>true</code> if the hostname is not empty and can be
304 * resolved, <code>false</code> otherwise
306 private boolean verifyHostname() {
307 if (hostnameTextField.getText().trim().length() == 0) {
311 InetAddress.getByName(hostnameTextField.getText().trim());
313 } catch (UnknownHostException uhe1) {
319 * Verifies that the port number is numeric and in the range from
320 * <code>0</code> to <code>65535</code>.
322 * @return <code>true</code> if the port number is okay,
323 * <code>false</code> otherwise
325 private boolean verifyPort() {
327 int portNumber = Integer.valueOf(portTextField.getText().trim());
328 if ((portNumber > 0) && (portNumber < 65536)) {
331 } catch (NumberFormatException nfe1) {
338 * Confirms the node settings and closes the dialog.
340 private void confirm() {
342 JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.name.message"), I18n.get("editNodeDialog.error.name.title"), JOptionPane.ERROR_MESSAGE);
345 if (!verifyHostname()) {
346 JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.hostname.message"), I18n.get("editNodeDialog.error.hostname.title"), JOptionPane.ERROR_MESSAGE);
350 JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.port.message"), I18n.get("editNodeDialog.error.port.title"), JOptionPane.ERROR_MESSAGE);
353 name = nameTextField.getText().trim();
354 hostname = hostnameTextField.getText().trim();
356 port = Integer.parseInt(portTextField.getText().trim());
357 } catch (NumberFormatException nfe1) {
358 /* should not occur, the value was checked! */
359 assert false: "port number is invalid though it was checked!";
361 sameMachine = sameMachineCheckBox.isSelected();
367 * Cancels the node settings and closes the dialog.
369 private void cancel() {