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