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