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