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