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.List;
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;
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;
65 * A dialog that lets the user edit the private and public key for a project.
67 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
69 public class KeyDialog extends JDialog {
71 /** Interface to the freenet node. */
72 private final Freenet7Interface freenetInterface;
74 /** The public key. */
75 private String publicKey;
77 /** The private key. */
78 private String privateKey;
80 /** The “OK” button’s action. */
81 private Action okAction;
83 /** The “Cancel” button’s action. */
84 private Action cancelAction;
86 /** The “Regenerate” button’s action. */
87 private Action generateAction;
89 /** The “Copy from Identity” action. */
90 private Action copyFromIdentityAction;
92 /** The text field for the private key. */
93 private JTextField privateKeyTextField;
95 /** The text field for the public key. */
96 private JTextField publicKeyTextField;
98 /** The select box for the own identities. */
99 private JComboBox ownIdentitiesComboBox;
101 /** Whether the dialog was cancelled. */
102 private boolean cancelled;
104 /** The list of own identities. */
105 private final List<OwnIdentity> ownIdentities = new ArrayList<OwnIdentity>();
108 * Creates a new key dialog.
110 * @param freenetInterface
111 * Interface to the freenet node
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() {
121 @SuppressWarnings("synthetic-access")
122 public void windowClosing(WindowEvent windowEvent) {
134 * Returns whether the dialog was cancelled.
136 * @return {@code true} if the dialog was cancelled, {@code false} otherwise
138 public boolean wasCancelled() {
143 * Returns the public key.
145 * @return The public key
147 public String getPublicKey() {
152 * Sets the public key.
157 public void setPublicKey(String publicKey) {
158 this.publicKey = publicKey;
159 publicKeyTextField.setText(publicKey);
164 * Returns the private key.
166 * @return The private key
168 public String getPrivateKey() {
173 * Sets the private key.
178 public void setPrivateKey(String privateKey) {
179 this.privateKey = privateKey;
180 privateKeyTextField.setText(privateKey);
185 * Sets the own identities to display and copy URIs from.
187 * @param ownIdentities
188 * The list of own identities
190 public void setOwnIdentities(Collection<? extends OwnIdentity> ownIdentities) {
191 synchronized (this.ownIdentities) {
192 this.ownIdentities.clear();
193 this.ownIdentities.addAll(ownIdentities);
195 int selectedIndex = -1;
197 for (OwnIdentity ownIdentity : ownIdentities) {
198 if (ownIdentity.getInsertUri().equals(privateKey) && ownIdentity.getRequestUri().equals(publicKey)) {
199 selectedIndex = index;
203 ownIdentitiesComboBox.setSelectedIndex(selectedIndex);
216 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
217 setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2);
225 * Creates all necessary actions.
227 private void createActions() {
228 okAction = new AbstractAction(I18n.getMessage("jsite.general.ok")) {
231 @SuppressWarnings("synthetic-access")
232 public void actionPerformed(ActionEvent actionEvent) {
236 okAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.ok.tooltip"));
237 okAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ENTER);
239 cancelAction = new AbstractAction(I18n.getMessage("jsite.general.cancel")) {
242 @SuppressWarnings("synthetic-access")
243 public void actionPerformed(ActionEvent actionEvent) {
247 cancelAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.cancel.tooltip"));
248 cancelAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ESCAPE);
250 copyFromIdentityAction = new AbstractAction(I18n.getMessage("jsite.key-dialog.button.copy-from-identity")) {
253 @SuppressWarnings("synthetic-access")
254 public void actionPerformed(ActionEvent actionevent) {
255 actionCopyFromIdentity();
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));
261 generateAction = new AbstractAction(I18n.getMessage("jsite.key-dialog.button.generate")) {
264 @SuppressWarnings("synthetic-access")
265 public void actionPerformed(ActionEvent actionEvent) {
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));
274 * Initializes the dialog and all its components.
276 private void initDialog() {
278 JPanel dialogPanel = new JPanel(new BorderLayout(12, 12));
279 dialogPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
281 JPanel contentPanel = new JPanel(new GridBagLayout());
282 dialogPanel.add(contentPanel, BorderLayout.CENTER);
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));
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));
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));
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));
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));
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));
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));
305 ownIdentitiesComboBox = new JComboBox(new ComboBoxModelList<OwnIdentity>(ownIdentities));
306 ownIdentitiesComboBox.addActionListener(new ActionListener() {
309 @SuppressWarnings("synthetic-access")
310 public void actionPerformed(ActionEvent actionevent) {
311 copyFromIdentityAction.setEnabled(ownIdentitiesComboBox.getSelectedIndex() > -1);
314 ownIdentitiesComboBox.setRenderer(new DefaultListCellRenderer() {
317 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
318 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
322 OwnIdentity ownIdentity = (OwnIdentity) value;
323 setText(String.format("%s (%s)", ownIdentity.getNickname(), ownIdentity.getRequestUri().substring(0, ownIdentity.getRequestUri().indexOf(','))));
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));
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));
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));
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));
340 actionButtonPanel.add(new JButton(generateAction));
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);
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));
352 I18nContainer.getInstance().registerRunnable(new Runnable() {
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"));
365 getContentPane().add(dialogPanel, BorderLayout.CENTER);
375 * Quits the dialog, accepting all changes.
377 private void actionOk() {
378 publicKey = publicKeyTextField.getText();
379 privateKey = privateKeyTextField.getText();
385 * Quits the dialog, discarding all changes.
387 private void actionCancel() {
393 * Copies the public and private key from the selected identity.
395 private void actionCopyFromIdentity() {
396 OwnIdentity ownIdentity = (OwnIdentity) ownIdentitiesComboBox.getSelectedItem();
397 if (ownIdentity == null) {
400 setPublicKey(ownIdentity.getRequestUri());
401 setPrivateKey(ownIdentity.getInsertUri());
405 * Generates a new key pair.
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) {
411 String[] keyPair = null;
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);
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('/')));