73602fc4ba343a9c0876f05b5e71ffd853922109
[jSite2.git] / src / net / pterodactylus / jsite / gui / EditNodeDialog.java
1 /*
2  * jSite2 - NodeEditDialog.java -
3  * Copyright © 2008 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.jsite.gui;
21
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;
30
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;
39
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;
44
45 /**
46  * Dialog that lets the user edit the properties of a node.
47  * 
48  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
49  * @version $Id$
50  */
51 public class EditNodeDialog extends JDialog implements I18nable {
52
53         /** The user-given name of the node. */
54         private String name;
55
56         /** The hostname of the node. */
57         private String hostname;
58
59         /** The FNP port number of the node. */
60         private int port;
61
62         /** Whether the node is on the same machine. */
63         private boolean sameMachine;
64
65         /** Action of the okay button. */
66         private I18nAction okayAction;
67
68         /** Action of the cancel button. */
69         private I18nAction cancelAction;
70
71         /** The name label. */
72         private I18nLabel nameLabel;
73
74         /** The name textfield. */
75         private JTextField nameTextField;
76
77         /** The hostname label. */
78         private I18nLabel hostnameLabel;
79
80         /** The hostname textfield. */
81         private JTextField hostnameTextField;
82
83         /** The port label. */
84         private I18nLabel portLabel;
85
86         /** The port textfield. */
87         private JTextField portTextField;
88
89         /** The same machine checkbox. */
90         private JCheckBox sameMachineCheckBox;
91
92         /** Whether the dialog was cancelled. */
93         private boolean cancelled;
94
95         /**
96          * Creates a new node edit dialog with the given parent.
97          * 
98          * @param parentDialog
99          *            The parent dialog of this dialog
100          */
101         public EditNodeDialog(JDialog parentDialog) {
102                 super(parentDialog, I18n.get("editNodeDialog.title") + " – jSite " + Version.getVersion(), true);
103                 initActions();
104                 initComponents();
105                 pack();
106                 I18n.registerI18nable(this);
107                 SwingUtils.center(this);
108         }
109
110         //
111         // ACCESSORS
112         //
113
114         /**
115          * Returns the user-given name of the node.
116          * 
117          * @return The user-given name of the node
118          */
119         public String getNodeName() {
120                 return name;
121         }
122
123         /**
124          * Sets the user-given name of the node.
125          * 
126          * @param name
127          *            The name of the node
128          */
129         public void setNodeName(String name) {
130                 this.name = name;
131                 nameTextField.setText(name);
132         }
133
134         /**
135          * Returns the hostname of the node.
136          * 
137          * @return The hostname of the node
138          */
139         public String getNodeHostname() {
140                 return hostname;
141         }
142
143         /**
144          * Sets the hostname of the node.
145          * 
146          * @param hostname
147          *            The hostname of the node
148          */
149         public void setNodeHostname(String hostname) {
150                 this.hostname = hostname;
151                 hostnameTextField.setText(hostname);
152         }
153
154         /**
155          * Returns the FCP port number of the node.
156          * 
157          * @return The FCP port number of the node
158          */
159         public int getNodePort() {
160                 return port;
161         }
162
163         /**
164          * Sets the FCP port number of the node.
165          * 
166          * @param port
167          *            The FCP port number of the node
168          */
169         public void setNodePort(int port) {
170                 this.port = port;
171                 portTextField.setText(String.valueOf(port));
172         }
173
174         /**
175          * Returns whether the node is on the same machine as jSite.
176          * 
177          * @return <code>true</code> if the node is on the same machine as jSite,
178          *         <code>false</code> otherwise
179          */
180         public boolean isNodeOnSameMachine() {
181                 return sameMachine;
182         }
183
184         /**
185          * Sets whether the node is on the same machine as jSite.
186          * 
187          * @param sameMachine
188          *            <code>true</code> if the node is on the same machine as
189          *            jSite, <code>false</code> otherwise
190          */
191         public void setNodeOnSameMachine(boolean sameMachine) {
192                 this.sameMachine = sameMachine;
193                 sameMachineCheckBox.setSelected(sameMachine);
194         }
195
196         /**
197          * Returns whether the dialog was cancelled.
198          * 
199          * @return <code>true</code> if the dialog was cancelled,
200          *         <code>false</code> if the user clicked “okay”
201          */
202         public boolean wasCancelled() {
203                 return cancelled;
204         }
205
206         //
207         // PRIVATE METHODS
208         //
209
210         /**
211          * Initializes all actions.
212          */
213         private void initActions() {
214                 okayAction = new I18nAction("general.button.okay") {
215
216                         /**
217                          * {@inheritDoc}
218                          */
219                         @SuppressWarnings("synthetic-access")
220                         public void actionPerformed(ActionEvent e) {
221                                 confirm();
222                         }
223                 };
224                 cancelAction = new I18nAction("general.button.cancel") {
225
226                         /**
227                          * {@inheritDoc}
228                          */
229                         @SuppressWarnings("synthetic-access")
230                         public void actionPerformed(ActionEvent e) {
231                                 cancel();
232                         }
233                 };
234         }
235
236         /**
237          * Initializes all components.
238          */
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));
243
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);
251
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)));
255
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));
259
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));
263
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));
267
268                 sameMachineCheckBox = new JCheckBox(new I18nAction("editNodeDialog.checkbox.sameMachine") {
269
270                         public void actionPerformed(ActionEvent e) {
271                                 /* don't do anything. */
272                         }
273                 });
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));
275         }
276
277         //
278         // PRIVATE ACTIONS
279         //
280
281         /**
282          * Checks the name textfield for valid input.
283          * 
284          * @return <code>true</code> if the name textfield seem okay,
285          *         <code>false</code> if there is an error
286          */
287         private boolean verifyName() {
288                 return (nameTextField.getText().trim().length() != 0);
289         }
290
291         /**
292          * Verifies the hostname textfield by resolving the given name.
293          * 
294          * @return <code>true</code> if the hostname is not empty and can be
295          *         resolved, <code>false</code> otherwise
296          */
297         private boolean verifyHostname() {
298                 if (hostnameTextField.getText().trim().length() == 0) {
299                         return false;
300                 }
301                 try {
302                         InetAddress.getByName(hostnameTextField.getText().trim());
303                         return true;
304                 } catch (UnknownHostException uhe1) {
305                         return false;
306                 }
307         }
308
309         /**
310          * Verifies that the port number is numeric and in the range from
311          * <code>0</code> to <code>65535</code>.
312          * 
313          * @return <code>true</code> if the port number is okay,
314          *         <code>false</code> otherwise
315          */
316         private boolean verifyPort() {
317                 try {
318                         int portNumber = Integer.valueOf(portTextField.getText().trim());
319                         if ((portNumber > 0) && (portNumber < 65536)) {
320                                 return true;
321                         }
322                 } catch (NumberFormatException nfe1) {
323                         /* ignore. */
324                 }
325                 return false;
326         }
327
328         /**
329          * Confirms the node settings and closes the dialog.
330          */
331         private void confirm() {
332                 if (!verifyName()) {
333                         JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.name.message"), I18n.get("editNodeDialog.error.name.title"), JOptionPane.ERROR_MESSAGE);
334                         return;
335                 }
336                 if (!verifyHostname()) {
337                         JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.hostname.message"), I18n.get("editNodeDialog.error.hostname.title"), JOptionPane.ERROR_MESSAGE);
338                         return;
339                 }
340                 if (!verifyPort()) {
341                         JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.port.message"), I18n.get("editNodeDialog.error.port.title"), JOptionPane.ERROR_MESSAGE);
342                         return;
343                 }
344                 name = nameTextField.getText().trim();
345                 hostname = hostnameTextField.getText().trim();
346                 try {
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!";
351                 }
352                 sameMachine = sameMachineCheckBox.isSelected();
353                 cancelled = false;
354                 setVisible(false);
355         }
356
357         /**
358          * Cancels the node settings and closes the dialog.
359          */
360         private void cancel() {
361                 cancelled = true;
362                 setVisible(false);
363         }
364
365         //
366         // INTERFACE I18nable
367         //
368
369         /**
370          * {@inheritDoc}
371          */
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);
380         }
381
382 }