2e3082912530c888926ad66907e7881e93ca1f1f
[jSite.git] / src / de / todesbaum / jsite / application / KeyDialog.java
1 /*
2  * jSite - KeyDialog.java - Copyright © 2010 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         /**
85          * Creates a new key dialog.
86          *
87          * @param freenetInterface
88          *            Interface to the freenet node
89          * @param parent
90          *            The parent frame
91          */
92         public KeyDialog(Freenet7Interface freenetInterface, JFrame parent) {
93                 super(parent, I18n.getMessage("jsite.key-dialog.title"), true);
94                 this.freenetInterface = freenetInterface;
95                 addWindowListener(new WindowAdapter() {
96
97                         @Override
98                         @SuppressWarnings("synthetic-access")
99                         public void windowClosing(WindowEvent windowEvent) {
100                                 actionCancel();
101                         }
102                 });
103                 initDialog();
104         }
105
106         //
107         // ACCESSORS
108         //
109
110         /**
111          * Returns the public key.
112          *
113          * @return The public key
114          */
115         public String getPublicKey() {
116                 return publicKey;
117         }
118
119         /**
120          * Sets the public key.
121          *
122          * @param publicKey
123          *            The public key
124          */
125         public void setPublicKey(String publicKey) {
126                 this.publicKey = publicKey;
127                 publicKeyTextField.setText(publicKey);
128                 pack();
129         }
130
131         /**
132          * Returns the private key.
133          *
134          * @return The private key
135          */
136         public String getPrivateKey() {
137                 return privateKey;
138         }
139
140         /**
141          * Sets the private key.
142          *
143          * @param privateKey
144          *            The private key
145          */
146         public void setPrivateKey(String privateKey) {
147                 this.privateKey = privateKey;
148                 privateKeyTextField.setText(privateKey);
149                 pack();
150         }
151
152         //
153         // ACTIONS
154         //
155
156         /**
157          * {@inheritDoc}
158          */
159         @Override
160         public void pack() {
161                 super.pack();
162                 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
163                 setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2);
164         }
165
166         //
167         // PRIVATE METHODS
168         //
169
170         /**
171          * Creates all necessary actions.
172          */
173         private void createActions() {
174                 okAction = new AbstractAction(I18n.getMessage("jsite.general.ok")) {
175
176                         @Override
177                         @SuppressWarnings("synthetic-access")
178                         public void actionPerformed(ActionEvent actionEvent) {
179                                 actionOk();
180                         }
181                 };
182                 okAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.ok.tooltip"));
183                 okAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ENTER);
184
185                 cancelAction = new AbstractAction(I18n.getMessage("jsite.general.cancel")) {
186
187                         @Override
188                         @SuppressWarnings("synthetic-access")
189                         public void actionPerformed(ActionEvent actionEvent) {
190                                 actionCancel();
191                         }
192                 };
193                 cancelAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.cancel.tooltip"));
194                 cancelAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ESCAPE);
195
196                 generateAction = new AbstractAction(I18n.getMessage("jsite.key-dialog.button.generate")) {
197
198                         @Override
199                         @SuppressWarnings("synthetic-access")
200                         public void actionPerformed(ActionEvent actionEvent) {
201                                 actionGenerate();
202                         }
203                 };
204                 generateAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.generate.tooltip"));
205                 generateAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_DOWN_MASK));
206         }
207
208         /**
209          * Initializes the dialog and all its components.
210          */
211         private void initDialog() {
212                 createActions();
213                 JPanel dialogPanel = new JPanel(new BorderLayout(12, 12));
214                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
215
216                 JPanel contentPanel = new JPanel(new GridBagLayout());
217                 dialogPanel.add(contentPanel, BorderLayout.CENTER);
218
219                 final JLabel keysLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.keys"));
220                 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));
221
222                 final JLabel privateKeyLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.private-key"));
223                 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));
224
225                 privateKeyTextField = new JTextField();
226                 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));
227
228                 final JLabel publicKeyLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.public-key"));
229                 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));
230
231                 publicKeyTextField = new JTextField();
232                 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));
233
234                 final JLabel actionsLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.actions"));
235                 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));
236
237                 JPanel actionButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
238                 actionButtonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
239                 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));
240
241                 actionButtonPanel.add(new JButton(generateAction));
242
243                 JPanel separatorPanel = new JPanel(new BorderLayout(12, 12));
244                 dialogPanel.add(separatorPanel, BorderLayout.PAGE_END);
245                 separatorPanel.add(new JSeparator(SwingConstants.HORIZONTAL), BorderLayout.PAGE_START);
246
247                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
248                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
249                 separatorPanel.add(buttonPanel, BorderLayout.CENTER);
250                 buttonPanel.add(new JButton(okAction));
251                 buttonPanel.add(new JButton(cancelAction));
252
253                 I18nContainer.getInstance().registerRunnable(new Runnable() {
254
255                         @Override
256                         public void run() {
257                                 keysLabel.setText(I18n.getMessage("jsite.key-dialog.label.keys"));
258                                 privateKeyLabel.setText(I18n.getMessage("jsite.key-dialog.label.private-key"));
259                                 publicKeyLabel.setText(I18n.getMessage("jsite.key-dialog.label.public-key"));
260                                 actionsLabel.setText(I18n.getMessage("jsite.key-dialog.label.actions"));
261                         }
262                 });
263
264                 getContentPane().add(dialogPanel, BorderLayout.CENTER);
265                 pack();
266                 setResizable(false);
267         }
268
269         //
270         // PRIVATE ACTIONS
271         //
272
273         /**
274          * Quits the dialog, accepting all changes.
275          */
276         private void actionOk() {
277                 publicKey = publicKeyTextField.getText();
278                 privateKey = privateKeyTextField.getText();
279                 setVisible(false);
280         }
281
282         /**
283          * Quits the dialog, discarding all changes.
284          */
285         private void actionCancel() {
286                 setVisible(false);
287         }
288
289         /**
290          * Generates a new key pair.
291          */
292         private void actionGenerate() {
293                 if (JOptionPane.showConfirmDialog(this, I18n.getMessage("jsite.project.warning.generate-new-key"), null, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
294                         return;
295                 }
296                 String[] keyPair = null;
297                 try {
298                         keyPair = freenetInterface.generateKeyPair();
299                 } catch (IOException ioe1) {
300                         JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
301                         return;
302                 }
303                 publicKeyTextField.setText(keyPair[1].substring(keyPair[1].indexOf('@') + 1, keyPair[1].lastIndexOf('/')));
304                 privateKeyTextField.setText(keyPair[0].substring(keyPair[0].indexOf('@') + 1, keyPair[0].lastIndexOf('/')));
305                 pack();
306         }
307
308 }