5aba000fc735f7e5594bb3d795dce0c665387ce7
[jSite.git] / KeyDialog.java
1 /*
2  * jSite - KeyDialog.java - Copyright © 2010–2011 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.jsite.application;
20
21 import java.awt.BorderLayout;
22 import java.awt.Dimension;
23 import java.awt.FlowLayout;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.Insets;
27 import java.awt.Toolkit;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.InputEvent;
30 import java.awt.event.KeyEvent;
31 import java.awt.event.WindowAdapter;
32 import java.awt.event.WindowEvent;
33 import java.io.IOException;
34 import java.text.MessageFormat;
35
36 import javax.swing.AbstractAction;
37 import javax.swing.Action;
38 import javax.swing.BorderFactory;
39 import javax.swing.JButton;
40 import javax.swing.JDialog;
41 import javax.swing.JFrame;
42 import javax.swing.JLabel;
43 import javax.swing.JOptionPane;
44 import javax.swing.JPanel;
45 import javax.swing.JSeparator;
46 import javax.swing.JTextField;
47 import javax.swing.KeyStroke;
48 import javax.swing.SwingConstants;
49
50 import de.todesbaum.jsite.i18n.I18n;
51 import de.todesbaum.jsite.i18n.I18nContainer;
52
53 /**
54  * A dialog that lets the user edit the private and public key for a project.
55  *
56  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
57  */
58 public class KeyDialog extends JDialog {
59
60         /** Interface to the freenet node. */
61         private final Freenet7Interface freenetInterface;
62
63         /** The public key. */
64         private String publicKey;
65
66         /** The private key. */
67         private String privateKey;
68
69         /** The “OK” button’s action. */
70         private Action okAction;
71
72         /** The “Cancel” button’s action. */
73         private Action cancelAction;
74
75         /** The “Regenerate” button’s action. */
76         private Action generateAction;
77
78         /** The text field for the private key. */
79         private JTextField privateKeyTextField;
80
81         /** The text field for the public key. */
82         private JTextField publicKeyTextField;
83
84         /** Whether the dialog was cancelled. */
85         private boolean cancelled;
86
87         /**
88          * Creates a new key dialog.
89          *
90          * @param freenetInterface
91          *            Interface to the freenet node
92          * @param parent
93          *            The parent frame
94          */
95         public KeyDialog(Freenet7Interface freenetInterface, JFrame parent) {
96                 super(parent, I18n.getMessage("jsite.key-dialog.title"), true);
97                 this.freenetInterface = freenetInterface;
98                 addWindowListener(new WindowAdapter() {
99
100                         @Override
101                         @SuppressWarnings("synthetic-access")
102                         public void windowClosing(WindowEvent windowEvent) {
103                                 actionCancel();
104                         }
105                 });
106                 initDialog();
107         }
108
109         //
110         // ACCESSORS
111         //
112
113         /**
114          * Returns whether the dialog was cancelled.
115          *
116          * @return {@code true} if the dialog was cancelled, {@code false} otherwise
117          */
118         public boolean wasCancelled() {
119                 return cancelled;
120         }
121
122         /**
123          * Returns the public key.
124          *
125          * @return The public key
126          */
127         public String getPublicKey() {
128                 return publicKey;
129         }
130
131         /**
132          * Sets the public key.
133          *
134          * @param publicKey
135          *            The public key
136          */
137         public void setPublicKey(String publicKey) {
138                 this.publicKey = publicKey;
139                 publicKeyTextField.setText(publicKey);
140                 pack();
141         }
142
143         /**
144          * Returns the private key.
145          *
146          * @return The private key
147          */
148         public String getPrivateKey() {
149                 return privateKey;
150         }
151
152         /**
153          * Sets the private key.
154          *
155          * @param privateKey
156          *            The private key
157          */
158         public void setPrivateKey(String privateKey) {
159                 this.privateKey = privateKey;
160                 privateKeyTextField.setText(privateKey);
161                 pack();
162         }
163
164         //
165         // ACTIONS
166         //
167
168         /**
169          * {@inheritDoc}
170          */
171         @Override
172         public void pack() {
173                 super.pack();
174                 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
175                 setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2);
176         }
177
178         //
179         // PRIVATE METHODS
180         //
181
182         /**
183          * Creates all necessary actions.
184          */
185         private void createActions() {
186                 okAction = new AbstractAction(I18n.getMessage("jsite.general.ok")) {
187
188                         @SuppressWarnings("synthetic-access")
189                         public void actionPerformed(ActionEvent actionEvent) {
190                                 actionOk();
191                         }
192                 };
193                 okAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.ok.tooltip"));
194                 okAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ENTER);
195
196                 cancelAction = new AbstractAction(I18n.getMessage("jsite.general.cancel")) {
197
198                         @SuppressWarnings("synthetic-access")
199                         public void actionPerformed(ActionEvent actionEvent) {
200                                 actionCancel();
201                         }
202                 };
203                 cancelAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.cancel.tooltip"));
204                 cancelAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ESCAPE);
205
206                 generateAction = new AbstractAction(I18n.getMessage("jsite.key-dialog.button.generate")) {
207
208                         @SuppressWarnings("synthetic-access")
209                         public void actionPerformed(ActionEvent actionEvent) {
210                                 actionGenerate();
211                         }
212                 };
213                 generateAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.generate.tooltip"));
214                 generateAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_DOWN_MASK));
215         }
216
217         /**
218          * Initializes the dialog and all its components.
219          */
220         private void initDialog() {
221                 createActions();
222                 JPanel dialogPanel = new JPanel(new BorderLayout(12, 12));
223                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
224
225                 JPanel contentPanel = new JPanel(new GridBagLayout());
226                 dialogPanel.add(contentPanel, BorderLayout.CENTER);
227
228                 final JLabel keysLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.keys"));
229                 contentPanel.add(keysLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
230
231                 final JLabel privateKeyLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.private-key"));
232                 contentPanel.add(privateKeyLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 18, 0, 0), 0, 0));
233
234                 privateKeyTextField = new JTextField();
235                 contentPanel.add(privateKeyTextField, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 12, 0, 0), 0, 0));
236
237                 final JLabel publicKeyLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.public-key"));
238                 contentPanel.add(publicKeyLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
239
240                 publicKeyTextField = new JTextField();
241                 contentPanel.add(publicKeyTextField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 12, 0, 0), 0, 0));
242
243                 final JLabel actionsLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.actions"));
244                 contentPanel.add(actionsLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
245
246                 JPanel actionButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
247                 actionButtonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
248                 contentPanel.add(actionButtonPanel, new GridBagConstraints(0, 4, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 18, 0, 0), 0, 0));
249
250                 actionButtonPanel.add(new JButton(generateAction));
251
252                 JPanel separatorPanel = new JPanel(new BorderLayout(12, 12));
253                 dialogPanel.add(separatorPanel, BorderLayout.PAGE_END);
254                 separatorPanel.add(new JSeparator(SwingConstants.HORIZONTAL), BorderLayout.PAGE_START);
255
256                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
257                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
258                 separatorPanel.add(buttonPanel, BorderLayout.CENTER);
259                 buttonPanel.add(new JButton(okAction));
260                 buttonPanel.add(new JButton(cancelAction));
261
262                 I18nContainer.getInstance().registerRunnable(new Runnable() {
263
264                         public void run() {
265                                 keysLabel.setText(I18n.getMessage("jsite.key-dialog.label.keys"));
266                                 privateKeyLabel.setText(I18n.getMessage("jsite.key-dialog.label.private-key"));
267                                 publicKeyLabel.setText(I18n.getMessage("jsite.key-dialog.label.public-key"));
268                                 actionsLabel.setText(I18n.getMessage("jsite.key-dialog.label.actions"));
269                         }
270                 });
271
272                 getContentPane().add(dialogPanel, BorderLayout.CENTER);
273                 pack();
274                 setResizable(false);
275         }
276
277         //
278         // PRIVATE ACTIONS
279         //
280
281         /**
282          * Quits the dialog, accepting all changes.
283          */
284         private void actionOk() {
285                 publicKey = publicKeyTextField.getText();
286                 privateKey = privateKeyTextField.getText();
287                 cancelled = false;
288                 setVisible(false);
289         }
290
291         /**
292          * Quits the dialog, discarding all changes.
293          */
294         private void actionCancel() {
295                 cancelled = true;
296                 setVisible(false);
297         }
298
299         /**
300          * Generates a new key pair.
301          */
302         private void actionGenerate() {
303                 if (JOptionPane.showConfirmDialog(this, I18n.getMessage("jsite.project.warning.generate-new-key"), null, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
304                         return;
305                 }
306                 String[] keyPair = null;
307                 try {
308                         keyPair = freenetInterface.generateKeyPair();
309                 } catch (IOException ioe1) {
310                         JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
311                         return;
312                 }
313                 publicKeyTextField.setText(keyPair[1].substring(keyPair[1].indexOf('@') + 1, keyPair[1].lastIndexOf('/')));
314                 privateKeyTextField.setText(keyPair[0].substring(keyPair[0].indexOf('@') + 1, keyPair[0].lastIndexOf('/')));
315                 pack();
316         }
317
318 }