29061750dffd64a48f91cb100f3083e4a730ec69
[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.Component;
23 import java.awt.Dimension;
24 import java.awt.FlowLayout;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.Insets;
28 import java.awt.Toolkit;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.awt.event.InputEvent;
32 import java.awt.event.KeyEvent;
33 import java.awt.event.WindowAdapter;
34 import java.awt.event.WindowEvent;
35 import java.io.IOException;
36 import java.text.MessageFormat;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.List;
40
41 import javax.swing.AbstractAction;
42 import javax.swing.Action;
43 import javax.swing.BorderFactory;
44 import javax.swing.DefaultListCellRenderer;
45 import javax.swing.JButton;
46 import javax.swing.JComboBox;
47 import javax.swing.JDialog;
48 import javax.swing.JFrame;
49 import javax.swing.JLabel;
50 import javax.swing.JList;
51 import javax.swing.JOptionPane;
52 import javax.swing.JPanel;
53 import javax.swing.JSeparator;
54 import javax.swing.JTextField;
55 import javax.swing.KeyStroke;
56 import javax.swing.SwingConstants;
57
58 import net.pterodactylus.util.swing.ComboBoxModelList;
59 import de.todesbaum.jsite.application.Freenet7Interface;
60 import de.todesbaum.jsite.i18n.I18n;
61 import de.todesbaum.jsite.i18n.I18nContainer;
62 import de.todesbaum.util.freenet.fcp2.wot.OwnIdentity;
63
64 /**
65  * A dialog that lets the user edit the private and public key for a project.
66  *
67  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
68  */
69 public class KeyDialog extends JDialog {
70
71         /** Interface to the freenet node. */
72         private final Freenet7Interface freenetInterface;
73
74         /** The public key. */
75         private String publicKey;
76
77         /** The private key. */
78         private String privateKey;
79
80         /** The “OK” button’s action. */
81         private Action okAction;
82
83         /** The “Cancel” button’s action. */
84         private Action cancelAction;
85
86         /** The “Regenerate” button’s action. */
87         private Action generateAction;
88
89         /** The “Copy from Identity” action. */
90         private Action copyFromIdentityAction;
91
92         /** The text field for the private key. */
93         private JTextField privateKeyTextField;
94
95         /** The text field for the public key. */
96         private JTextField publicKeyTextField;
97
98         /** The select box for the own identities. */
99         private JComboBox ownIdentitiesComboBox;
100
101         /** Whether the dialog was cancelled. */
102         private boolean cancelled;
103
104         /** The list of own identities. */
105         private final List<OwnIdentity> ownIdentities = new ArrayList<OwnIdentity>();
106
107         /**
108          * Creates a new key dialog.
109          *
110          * @param freenetInterface
111          *            Interface to the freenet node
112          * @param parent
113          *            The parent frame
114          */
115         public KeyDialog(Freenet7Interface freenetInterface, JFrame parent) {
116                 super(parent, I18n.getMessage("jsite.key-dialog.title"), true);
117                 this.freenetInterface = freenetInterface;
118                 addWindowListener(new WindowAdapter() {
119
120                         @Override
121                         @SuppressWarnings("synthetic-access")
122                         public void windowClosing(WindowEvent windowEvent) {
123                                 actionCancel();
124                         }
125                 });
126                 initDialog();
127         }
128
129         //
130         // ACCESSORS
131         //
132
133         /**
134          * Returns whether the dialog was cancelled.
135          *
136          * @return {@code true} if the dialog was cancelled, {@code false} otherwise
137          */
138         public boolean wasCancelled() {
139                 return cancelled;
140         }
141
142         /**
143          * Returns the public key.
144          *
145          * @return The public key
146          */
147         public String getPublicKey() {
148                 return publicKey;
149         }
150
151         /**
152          * Sets the public key.
153          *
154          * @param publicKey
155          *            The public key
156          */
157         public void setPublicKey(String publicKey) {
158                 this.publicKey = publicKey;
159                 publicKeyTextField.setText(publicKey);
160                 pack();
161         }
162
163         /**
164          * Returns the private key.
165          *
166          * @return The private key
167          */
168         public String getPrivateKey() {
169                 return privateKey;
170         }
171
172         /**
173          * Sets the private key.
174          *
175          * @param privateKey
176          *            The private key
177          */
178         public void setPrivateKey(String privateKey) {
179                 this.privateKey = privateKey;
180                 privateKeyTextField.setText(privateKey);
181                 pack();
182         }
183
184         /**
185          * Sets the own identities to display and copy URIs from.
186          *
187          * @param ownIdentities
188          *            The list of own identities
189          */
190         public void setOwnIdentities(Collection<? extends OwnIdentity> ownIdentities) {
191                 synchronized (this.ownIdentities) {
192                         this.ownIdentities.clear();
193                         this.ownIdentities.addAll(ownIdentities);
194                 }
195                 int selectedIndex = -1;
196                 int index = 0;
197                 for (OwnIdentity ownIdentity : ownIdentities) {
198                         if (ownIdentity.getInsertUri().equals(privateKey) && ownIdentity.getRequestUri().equals(publicKey)) {
199                                 selectedIndex = index;
200                         }
201                         index++;
202                 }
203                 ownIdentitiesComboBox.setSelectedIndex(selectedIndex);
204         }
205
206         //
207         // ACTIONS
208         //
209
210         /**
211          * {@inheritDoc}
212          */
213         @Override
214         public void pack() {
215                 super.pack();
216                 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
217                 setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2);
218         }
219
220         //
221         // PRIVATE METHODS
222         //
223
224         /**
225          * Creates all necessary actions.
226          */
227         private void createActions() {
228                 okAction = new AbstractAction(I18n.getMessage("jsite.general.ok")) {
229
230                         @Override
231                         @SuppressWarnings("synthetic-access")
232                         public void actionPerformed(ActionEvent actionEvent) {
233                                 actionOk();
234                         }
235                 };
236                 okAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.ok.tooltip"));
237                 okAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ENTER);
238
239                 cancelAction = new AbstractAction(I18n.getMessage("jsite.general.cancel")) {
240
241                         @Override
242                         @SuppressWarnings("synthetic-access")
243                         public void actionPerformed(ActionEvent actionEvent) {
244                                 actionCancel();
245                         }
246                 };
247                 cancelAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.cancel.tooltip"));
248                 cancelAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ESCAPE);
249
250                 copyFromIdentityAction = new AbstractAction(I18n.getMessage("jsite.key-dialog.button.copy-from-identity")) {
251
252                         @Override
253                         @SuppressWarnings("synthetic-access")
254                         public void actionPerformed(ActionEvent actionevent) {
255                                 actionCopyFromIdentity();
256                         }
257                 };
258                 copyFromIdentityAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.copy-from-identity.tooltip"));
259                 copyFromIdentityAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK));
260
261                 generateAction = new AbstractAction(I18n.getMessage("jsite.key-dialog.button.generate")) {
262
263                         @Override
264                         @SuppressWarnings("synthetic-access")
265                         public void actionPerformed(ActionEvent actionEvent) {
266                                 actionGenerate();
267                         }
268                 };
269                 generateAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.generate.tooltip"));
270                 generateAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_DOWN_MASK));
271         }
272
273         /**
274          * Initializes the dialog and all its components.
275          */
276         private void initDialog() {
277                 createActions();
278                 JPanel dialogPanel = new JPanel(new BorderLayout(12, 12));
279                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
280
281                 JPanel contentPanel = new JPanel(new GridBagLayout());
282                 dialogPanel.add(contentPanel, BorderLayout.CENTER);
283
284                 final JLabel keysLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.keys"));
285                 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));
286
287                 final JLabel privateKeyLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.private-key"));
288                 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));
289
290                 privateKeyTextField = new JTextField();
291                 contentPanel.add(privateKeyTextField, new GridBagConstraints(1, 1, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 12, 0, 0), 0, 0));
292
293                 final JLabel publicKeyLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.public-key"));
294                 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));
295
296                 publicKeyTextField = new JTextField();
297                 contentPanel.add(publicKeyTextField, new GridBagConstraints(1, 2, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 12, 0, 0), 0, 0));
298
299                 final JLabel identitiesLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.identities"));
300                 contentPanel.add(identitiesLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
301
302                 final JLabel identityLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.identity"));
303                 contentPanel.add(identityLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 18, 0, 0), 0, 0));
304
305                 ownIdentitiesComboBox = new JComboBox(new ComboBoxModelList<OwnIdentity>(ownIdentities));
306                 ownIdentitiesComboBox.addActionListener(new ActionListener() {
307
308                         @Override
309                         @SuppressWarnings("synthetic-access")
310                         public void actionPerformed(ActionEvent actionevent) {
311                                 copyFromIdentityAction.setEnabled(ownIdentitiesComboBox.getSelectedIndex() > -1);
312                         }
313                 });
314                 ownIdentitiesComboBox.setRenderer(new DefaultListCellRenderer() {
315
316                         @Override
317                         public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
318                                 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
319                                 if (value == null) {
320                                         setText("");
321                                 } else {
322                                         OwnIdentity ownIdentity = (OwnIdentity) value;
323                                         setText(String.format("%s (%s)", ownIdentity.getNickname(), ownIdentity.getRequestUri().substring(0, ownIdentity.getRequestUri().indexOf(','))));
324                                 }
325                                 return this;
326                         }
327                 });
328                 contentPanel.add(ownIdentitiesComboBox, new GridBagConstraints(1, 4, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 12, 0, 0), 0, 0));
329
330                 JButton copyFromIdentityButton = new JButton(copyFromIdentityAction);
331                 contentPanel.add(copyFromIdentityButton, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(6, 12, 0, 0), 0, 0));
332
333                 final JLabel actionsLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.actions"));
334                 contentPanel.add(actionsLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
335
336                 JPanel actionButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
337                 actionButtonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
338                 contentPanel.add(actionButtonPanel, new GridBagConstraints(0, 6, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 18, 0, 0), 0, 0));
339
340                 actionButtonPanel.add(new JButton(generateAction));
341
342                 JPanel separatorPanel = new JPanel(new BorderLayout(12, 12));
343                 dialogPanel.add(separatorPanel, BorderLayout.PAGE_END);
344                 separatorPanel.add(new JSeparator(SwingConstants.HORIZONTAL), BorderLayout.PAGE_START);
345
346                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
347                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
348                 separatorPanel.add(buttonPanel, BorderLayout.CENTER);
349                 buttonPanel.add(new JButton(okAction));
350                 buttonPanel.add(new JButton(cancelAction));
351
352                 I18nContainer.getInstance().registerRunnable(new Runnable() {
353
354                         @Override
355                         public void run() {
356                                 keysLabel.setText(I18n.getMessage("jsite.key-dialog.label.keys"));
357                                 privateKeyLabel.setText(I18n.getMessage("jsite.key-dialog.label.private-key"));
358                                 publicKeyLabel.setText(I18n.getMessage("jsite.key-dialog.label.public-key"));
359                                 identitiesLabel.setText(I18n.getMessage("jsite.key-dialog.label.identities"));
360                                 identityLabel.setText(I18n.getMessage("jsite.key-dialog.label.identity"));
361                                 actionsLabel.setText(I18n.getMessage("jsite.key-dialog.label.actions"));
362                         }
363                 });
364
365                 getContentPane().add(dialogPanel, BorderLayout.CENTER);
366                 pack();
367                 setResizable(false);
368         }
369
370         //
371         // PRIVATE ACTIONS
372         //
373
374         /**
375          * Quits the dialog, accepting all changes.
376          */
377         private void actionOk() {
378                 publicKey = publicKeyTextField.getText();
379                 privateKey = privateKeyTextField.getText();
380                 cancelled = false;
381                 setVisible(false);
382         }
383
384         /**
385          * Quits the dialog, discarding all changes.
386          */
387         private void actionCancel() {
388                 cancelled = true;
389                 setVisible(false);
390         }
391
392         /**
393          * Copies the public and private key from the selected identity.
394          */
395         private void actionCopyFromIdentity() {
396                 OwnIdentity ownIdentity = (OwnIdentity) ownIdentitiesComboBox.getSelectedItem();
397                 if (ownIdentity == null) {
398                         return;
399                 }
400                 setPublicKey(ownIdentity.getRequestUri());
401                 setPrivateKey(ownIdentity.getInsertUri());
402         }
403
404         /**
405          * Generates a new key pair.
406          */
407         private void actionGenerate() {
408                 if (JOptionPane.showConfirmDialog(this, I18n.getMessage("jsite.project.warning.generate-new-key"), null, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
409                         return;
410                 }
411                 String[] keyPair = null;
412                 try {
413                         keyPair = freenetInterface.generateKeyPair();
414                 } catch (IOException ioe1) {
415                         JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
416                         return;
417                 }
418                 publicKeyTextField.setText(keyPair[1].substring(keyPair[1].indexOf('@') + 1, keyPair[1].lastIndexOf('/')));
419                 privateKeyTextField.setText(keyPair[0].substring(keyPair[0].indexOf('@') + 1, keyPair[0].lastIndexOf('/')));
420                 pack();
421         }
422
423 }