2 * jSite - KeyDialog.java - Copyright © 2010–2012 David Roden
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.
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.
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.
19 package de.todesbaum.jsite.gui;
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.Collections;
40 import java.util.Comparator;
41 import java.util.List;
43 import javax.swing.AbstractAction;
44 import javax.swing.Action;
45 import javax.swing.BorderFactory;
46 import javax.swing.DefaultListCellRenderer;
47 import javax.swing.JButton;
48 import javax.swing.JComboBox;
49 import javax.swing.JDialog;
50 import javax.swing.JFrame;
51 import javax.swing.JLabel;
52 import javax.swing.JList;
53 import javax.swing.JOptionPane;
54 import javax.swing.JPanel;
55 import javax.swing.JSeparator;
56 import javax.swing.JTextField;
57 import javax.swing.KeyStroke;
58 import javax.swing.SwingConstants;
60 import net.pterodactylus.util.swing.ComboBoxModelList;
61 import de.todesbaum.jsite.application.Freenet7Interface;
62 import de.todesbaum.jsite.i18n.I18n;
63 import de.todesbaum.jsite.i18n.I18nContainer;
64 import de.todesbaum.util.freenet.fcp2.wot.OwnIdentity;
67 * A dialog that lets the user edit the private and public key for a project.
69 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
71 public class KeyDialog extends JDialog {
73 /** Interface to the freenet node. */
74 private final Freenet7Interface freenetInterface;
76 /** The public key. */
77 private String publicKey;
79 /** The private key. */
80 private String privateKey;
82 /** The “OK” button’s action. */
83 private Action okAction;
85 /** The “Cancel” button’s action. */
86 private Action cancelAction;
88 /** The “Regenerate” button’s action. */
89 private Action generateAction;
91 /** The “Copy from Identity” action. */
92 private Action copyFromIdentityAction;
94 /** The text field for the private key. */
95 private JTextField privateKeyTextField;
97 /** The text field for the public key. */
98 private JTextField publicKeyTextField;
100 /** The select box for the own identities. */
101 private JComboBox ownIdentitiesComboBox;
103 /** Whether the dialog was cancelled. */
104 private boolean cancelled;
106 /** The list of own identities. */
107 private final List<OwnIdentity> ownIdentities = new ArrayList<OwnIdentity>();
110 * Creates a new key dialog.
112 * @param freenetInterface
113 * Interface to the freenet node
117 public KeyDialog(Freenet7Interface freenetInterface, JFrame parent) {
118 super(parent, I18n.getMessage("jsite.key-dialog.title"), true);
119 this.freenetInterface = freenetInterface;
120 addWindowListener(new WindowAdapter() {
123 @SuppressWarnings("synthetic-access")
124 public void windowClosing(WindowEvent windowEvent) {
136 * Returns whether the dialog was cancelled.
138 * @return {@code true} if the dialog was cancelled, {@code false} otherwise
140 public boolean wasCancelled() {
145 * Returns the public key.
147 * @return The public key
149 public String getPublicKey() {
154 * Sets the public key.
159 public void setPublicKey(String publicKey) {
160 this.publicKey = publicKey;
161 publicKeyTextField.setText(publicKey);
166 * Returns the private key.
168 * @return The private key
170 public String getPrivateKey() {
175 * Sets the private key.
180 public void setPrivateKey(String privateKey) {
181 this.privateKey = privateKey;
182 privateKeyTextField.setText(privateKey);
187 * Sets the own identities to display and copy URIs from.
189 * @param ownIdentities
190 * The list of own identities
192 public void setOwnIdentities(Collection<? extends OwnIdentity> ownIdentities) {
193 synchronized (this.ownIdentities) {
194 this.ownIdentities.clear();
195 this.ownIdentities.addAll(ownIdentities);
196 Collections.sort(this.ownIdentities, new Comparator<OwnIdentity>() {
199 public int compare(OwnIdentity leftOwnIdentity, OwnIdentity rightOwnIdentity) {
200 return leftOwnIdentity.getNickname().compareToIgnoreCase(rightOwnIdentity.getNickname());
204 int selectedIndex = -1;
206 for (OwnIdentity ownIdentity : this.ownIdentities) {
207 if (ownIdentity.getInsertUri().equals(privateKey) && ownIdentity.getRequestUri().equals(publicKey)) {
208 selectedIndex = index;
212 ownIdentitiesComboBox.setSelectedIndex(selectedIndex);
225 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
226 setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2);
234 * Creates all necessary actions.
236 private void createActions() {
237 okAction = new AbstractAction(I18n.getMessage("jsite.general.ok")) {
240 @SuppressWarnings("synthetic-access")
241 public void actionPerformed(ActionEvent actionEvent) {
245 okAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.ok.tooltip"));
246 okAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ENTER);
248 cancelAction = new AbstractAction(I18n.getMessage("jsite.general.cancel")) {
251 @SuppressWarnings("synthetic-access")
252 public void actionPerformed(ActionEvent actionEvent) {
256 cancelAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.cancel.tooltip"));
257 cancelAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ESCAPE);
259 copyFromIdentityAction = new AbstractAction(I18n.getMessage("jsite.key-dialog.button.copy-from-identity")) {
262 @SuppressWarnings("synthetic-access")
263 public void actionPerformed(ActionEvent actionevent) {
264 actionCopyFromIdentity();
267 copyFromIdentityAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.copy-from-identity.tooltip"));
268 copyFromIdentityAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK));
270 generateAction = new AbstractAction(I18n.getMessage("jsite.key-dialog.button.generate")) {
273 @SuppressWarnings("synthetic-access")
274 public void actionPerformed(ActionEvent actionEvent) {
278 generateAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.generate.tooltip"));
279 generateAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_DOWN_MASK));
283 * Initializes the dialog and all its components.
285 private void initDialog() {
287 JPanel dialogPanel = new JPanel(new BorderLayout(12, 12));
288 dialogPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
290 JPanel contentPanel = new JPanel(new GridBagLayout());
291 dialogPanel.add(contentPanel, BorderLayout.CENTER);
293 final JLabel keysLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.keys"));
294 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));
296 final JLabel privateKeyLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.private-key"));
297 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));
299 privateKeyTextField = new JTextField();
300 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));
302 final JLabel publicKeyLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.public-key"));
303 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));
305 publicKeyTextField = new JTextField();
306 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));
308 final JLabel identitiesLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.identities"));
309 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));
311 final JLabel identityLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.identity"));
312 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));
314 ownIdentitiesComboBox = new JComboBox(new ComboBoxModelList<OwnIdentity>(ownIdentities));
315 ownIdentitiesComboBox.addActionListener(new ActionListener() {
318 @SuppressWarnings("synthetic-access")
319 public void actionPerformed(ActionEvent actionevent) {
320 copyFromIdentityAction.setEnabled(ownIdentitiesComboBox.getSelectedIndex() > -1);
323 ownIdentitiesComboBox.setRenderer(new DefaultListCellRenderer() {
326 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
327 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
331 OwnIdentity ownIdentity = (OwnIdentity) value;
332 setText(String.format("%s (%s)", ownIdentity.getNickname(), ownIdentity.getRequestUri().substring(0, ownIdentity.getRequestUri().indexOf(','))));
337 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));
339 JButton copyFromIdentityButton = new JButton(copyFromIdentityAction);
340 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));
342 final JLabel actionsLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.actions"));
343 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));
345 JPanel actionButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
346 actionButtonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
347 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));
349 actionButtonPanel.add(new JButton(generateAction));
351 JPanel separatorPanel = new JPanel(new BorderLayout(12, 12));
352 dialogPanel.add(separatorPanel, BorderLayout.PAGE_END);
353 separatorPanel.add(new JSeparator(SwingConstants.HORIZONTAL), BorderLayout.PAGE_START);
355 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
356 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
357 separatorPanel.add(buttonPanel, BorderLayout.CENTER);
358 buttonPanel.add(new JButton(okAction));
359 buttonPanel.add(new JButton(cancelAction));
361 I18nContainer.getInstance().registerRunnable(new Runnable() {
365 keysLabel.setText(I18n.getMessage("jsite.key-dialog.label.keys"));
366 privateKeyLabel.setText(I18n.getMessage("jsite.key-dialog.label.private-key"));
367 publicKeyLabel.setText(I18n.getMessage("jsite.key-dialog.label.public-key"));
368 identitiesLabel.setText(I18n.getMessage("jsite.key-dialog.label.identities"));
369 identityLabel.setText(I18n.getMessage("jsite.key-dialog.label.identity"));
370 actionsLabel.setText(I18n.getMessage("jsite.key-dialog.label.actions"));
374 getContentPane().add(dialogPanel, BorderLayout.CENTER);
384 * Quits the dialog, accepting all changes.
386 private void actionOk() {
387 publicKey = publicKeyTextField.getText();
388 privateKey = privateKeyTextField.getText();
394 * Quits the dialog, discarding all changes.
396 private void actionCancel() {
402 * Copies the public and private key from the selected identity.
404 private void actionCopyFromIdentity() {
405 OwnIdentity ownIdentity = (OwnIdentity) ownIdentitiesComboBox.getSelectedItem();
406 if (ownIdentity == null) {
409 setPublicKey(ownIdentity.getRequestUri());
410 setPrivateKey(ownIdentity.getInsertUri());
414 * Generates a new key pair.
416 private void actionGenerate() {
417 if (JOptionPane.showConfirmDialog(this, I18n.getMessage("jsite.project.warning.generate-new-key"), null, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
420 String[] keyPair = null;
422 keyPair = freenetInterface.generateKeyPair();
423 } catch (IOException ioe1) {
424 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
427 publicKeyTextField.setText(keyPair[1].substring(keyPair[1].indexOf('@') + 1, keyPair[1].lastIndexOf('/')));
428 privateKeyTextField.setText(keyPair[0].substring(keyPair[0].indexOf('@') + 1, keyPair[0].lastIndexOf('/')));