externalize verifier for variouos stuff
[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
29 import javax.swing.BorderFactory;
30 import javax.swing.JButton;
31 import javax.swing.JDialog;
32 import javax.swing.JOptionPane;
33 import javax.swing.JPanel;
34 import javax.swing.JTextField;
35 import javax.swing.border.EtchedBorder;
36
37 import net.pterodactylus.jsite.core.Verifier;
38 import net.pterodactylus.jsite.i18n.I18n;
39 import net.pterodactylus.jsite.i18n.I18nable;
40 import net.pterodactylus.jsite.i18n.gui.I18nAction;
41 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
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         /** Action of the okay button. */
63         private I18nAction okayAction;
64
65         /** Action of the cancel button. */
66         private I18nAction cancelAction;
67
68         /** The name label. */
69         private I18nLabel nameLabel;
70
71         /** The name textfield. */
72         private JTextField nameTextField;
73
74         /** The hostname label. */
75         private I18nLabel hostnameLabel;
76
77         /** The hostname textfield. */
78         private JTextField hostnameTextField;
79
80         /** The port label. */
81         private I18nLabel portLabel;
82
83         /** The port textfield. */
84         private JTextField portTextField;
85
86         /** Whether the dialog was cancelled. */
87         private boolean cancelled;
88
89         /**
90          * Creates a new node edit dialog with the given parent.
91          *
92          * @param parentDialog
93          *            The parent dialog of this dialog
94          */
95         public EditNodeDialog(JDialog parentDialog) {
96                 super(parentDialog, I18n.get("editNodeDialog.title") + " – jSite " + Version.getVersion(), true);
97                 initActions();
98                 initComponents();
99                 pack();
100                 I18n.registerI18nable(this);
101                 SwingUtils.center(this);
102         }
103
104         //
105         // ACCESSORS
106         //
107
108         /**
109          * Returns the user-given name of the node.
110          *
111          * @return The user-given name of the node
112          */
113         public String getNodeName() {
114                 return name;
115         }
116
117         /**
118          * Sets the user-given name of the node.
119          *
120          * @param name
121          *            The name of the node
122          */
123         public void setNodeName(String name) {
124                 this.name = name;
125                 nameTextField.setText(name);
126         }
127
128         /**
129          * Returns the hostname of the node.
130          *
131          * @return The hostname of the node
132          */
133         public String getNodeHostname() {
134                 return hostname;
135         }
136
137         /**
138          * Sets the hostname of the node.
139          *
140          * @param hostname
141          *            The hostname of the node
142          */
143         public void setNodeHostname(String hostname) {
144                 this.hostname = hostname;
145                 hostnameTextField.setText(hostname);
146         }
147
148         /**
149          * Returns the FCP port number of the node.
150          *
151          * @return The FCP port number of the node
152          */
153         public int getNodePort() {
154                 return port;
155         }
156
157         /**
158          * Sets the FCP port number of the node.
159          *
160          * @param port
161          *            The FCP port number of the node
162          */
163         public void setNodePort(int port) {
164                 this.port = port;
165                 portTextField.setText(String.valueOf(port));
166         }
167
168         /**
169          * Returns whether the dialog was cancelled.
170          *
171          * @return <code>true</code> if the dialog was cancelled,
172          *         <code>false</code> if the user clicked “okay”
173          */
174         public boolean wasCancelled() {
175                 return cancelled;
176         }
177
178         //
179         // PRIVATE METHODS
180         //
181
182         /**
183          * Initializes all actions.
184          */
185         private void initActions() {
186                 okayAction = new I18nAction("general.button.okay") {
187
188                         /**
189                          * {@inheritDoc}
190                          */
191                         @SuppressWarnings("synthetic-access")
192                         public void actionPerformed(ActionEvent e) {
193                                 confirm();
194                         }
195                 };
196                 cancelAction = new I18nAction("general.button.cancel") {
197
198                         /**
199                          * {@inheritDoc}
200                          */
201                         @SuppressWarnings("synthetic-access")
202                         public void actionPerformed(ActionEvent e) {
203                                 cancel();
204                         }
205                 };
206         }
207
208         /**
209          * Initializes all components.
210          */
211         private void initComponents() {
212                 JPanel rootPanel = new JPanel(new BorderLayout(12, 12));
213                 setContentPane(rootPanel);
214                 rootPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
215
216                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
217                 rootPanel.add(buttonPanel, BorderLayout.PAGE_END);
218                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
219                 buttonPanel.add(new JButton(cancelAction));
220                 JButton okayButton = new JButton(okayAction);
221                 buttonPanel.add(okayButton);
222                 getRootPane().setDefaultButton(okayButton);
223
224                 JPanel contentPanel = new JPanel(new GridBagLayout());
225                 rootPanel.add(contentPanel, BorderLayout.CENTER);
226                 contentPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
227
228                 nameTextField = new JTextField();
229                 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));
230                 contentPanel.add(nameTextField, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 12, 0, 0), 0, 0));
231
232                 hostnameTextField = new JTextField();
233                 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));
234                 contentPanel.add(hostnameTextField, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0));
235
236                 portTextField = new JTextField();
237                 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));
238                 contentPanel.add(portTextField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0));
239
240                 contentPanel.add(new JPanel(), new GridBagConstraints(0, 3, 2, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
241         }
242
243         //
244         // PRIVATE ACTIONS
245         //
246
247         /**
248          * Confirms the node settings and closes the dialog.
249          */
250         private void confirm() {
251                 if (!Verifier.verifyNodeName(nameTextField.getText())) {
252                         JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.name.message"), I18n.get("editNodeDialog.error.name.title"), JOptionPane.ERROR_MESSAGE);
253                         return;
254                 }
255                 if (!Verifier.verifyHostname(hostnameTextField.getText())) {
256                         JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.hostname.message"), I18n.get("editNodeDialog.error.hostname.title"), JOptionPane.ERROR_MESSAGE);
257                         return;
258                 }
259                 if (!Verifier.verifyPort(portTextField.getText())) {
260                         JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.port.message"), I18n.get("editNodeDialog.error.port.title"), JOptionPane.ERROR_MESSAGE);
261                         return;
262                 }
263                 name = nameTextField.getText().trim();
264                 hostname = hostnameTextField.getText().trim();
265                 try {
266                         port = Integer.parseInt(portTextField.getText().trim());
267                 } catch (NumberFormatException nfe1) {
268                         /* should not occur, the value was checked! */
269                         assert false: "port number is invalid though it was checked!";
270                 }
271                 cancelled = false;
272                 setVisible(false);
273         }
274
275         /**
276          * Cancels the node settings and closes the dialog.
277          */
278         private void cancel() {
279                 cancelled = true;
280                 setVisible(false);
281         }
282
283         //
284         // INTERFACE I18nable
285         //
286
287         /**
288          * {@inheritDoc}
289          */
290         public void updateI18n() {
291                 okayAction.updateI18n();
292                 cancelAction.updateI18n();
293                 nameLabel.updateI18n();
294                 hostnameLabel.updateI18n();
295                 portLabel.updateI18n();
296                 setTitle(I18n.get("editNodeDialog.title") + " – jSite " + Version.getVersion());
297                 SwingUtils.repackCentered(this);
298         }
299
300 }