2 * jSite - KeyDialog.java - Copyright © 2010–2014 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.application.Project;
63 import de.todesbaum.jsite.i18n.I18n;
64 import de.todesbaum.jsite.i18n.I18nContainer;
65 import de.todesbaum.util.freenet.fcp2.wot.OwnIdentity;
68 * A dialog that lets the user edit the private and public key for a project.
70 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
72 public class KeyDialog extends JDialog {
74 /** Interface to the freenet node. */
75 private final Freenet7Interface freenetInterface;
77 /** The public key. */
78 private String publicKey;
80 /** The private key. */
81 private String privateKey;
83 /** The “OK” button’s action. */
84 private Action okAction;
86 /** The “Cancel” button’s action. */
87 private Action cancelAction;
89 /** The “Regenerate” button’s action. */
90 private Action generateAction;
92 /** The “Copy from Project” action. */
93 private Action copyFromProjectAction;
95 /** The “Copy from Identity” action. */
96 private Action copyFromIdentityAction;
98 /** The text field for the private key. */
99 private JTextField privateKeyTextField;
101 /** The text field for the public key. */
102 private JTextField publicKeyTextField;
104 /** The select box for the projects. */
105 private JComboBox projectsComboBox;
107 /** The select box for the own identities. */
108 private JComboBox ownIdentitiesComboBox;
110 /** Whether the dialog was cancelled. */
111 private boolean cancelled;
113 /** The list of projects. */
114 private final List<Project> projects = new ArrayList<Project>();
116 /** The list of own identities. */
117 private final List<OwnIdentity> ownIdentities = new ArrayList<OwnIdentity>();
120 * Creates a new key dialog.
122 * @param freenetInterface
123 * Interface to the freenet node
127 public KeyDialog(Freenet7Interface freenetInterface, JFrame parent) {
128 super(parent, I18n.getMessage("jsite.key-dialog.title"), true);
129 this.freenetInterface = freenetInterface;
130 addWindowListener(new WindowAdapter() {
133 @SuppressWarnings("synthetic-access")
134 public void windowClosing(WindowEvent windowEvent) {
146 * Returns whether the dialog was cancelled.
148 * @return {@code true} if the dialog was cancelled, {@code false} otherwise
150 public boolean wasCancelled() {
155 * Returns the public key.
157 * @return The public key
159 public String getPublicKey() {
164 * Sets the public key.
169 public void setPublicKey(String publicKey) {
170 this.publicKey = publicKey;
171 publicKeyTextField.setText(publicKey);
176 * Returns the private key.
178 * @return The private key
180 public String getPrivateKey() {
185 * Sets the private key.
190 public void setPrivateKey(String privateKey) {
191 this.privateKey = privateKey;
192 privateKeyTextField.setText(privateKey);
197 * Sets the projects to display and copy URIs from.
200 * The list of projects
202 public void setProjects(Collection<? extends Project> projects) {
203 synchronized (this.projects) {
204 this.projects.clear();
205 this.projects.addAll(projects);
207 projectsComboBox.setSelectedIndex(-1);
211 * Sets the own identities to display and copy URIs from.
213 * @param ownIdentities
214 * The list of own identities
216 public void setOwnIdentities(Collection<? extends OwnIdentity> ownIdentities) {
217 synchronized (this.ownIdentities) {
218 this.ownIdentities.clear();
219 this.ownIdentities.addAll(ownIdentities);
220 Collections.sort(this.ownIdentities, new Comparator<OwnIdentity>() {
223 public int compare(OwnIdentity leftOwnIdentity, OwnIdentity rightOwnIdentity) {
224 return leftOwnIdentity.getNickname().compareToIgnoreCase(rightOwnIdentity.getNickname());
228 int selectedIndex = -1;
230 for (OwnIdentity ownIdentity : this.ownIdentities) {
231 if (ownIdentity.getInsertUri().equals(privateKey) && ownIdentity.getRequestUri().equals(publicKey)) {
232 selectedIndex = index;
236 ownIdentitiesComboBox.setSelectedIndex(selectedIndex);
249 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
250 setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2);
258 * Creates all necessary actions.
260 private void createActions() {
261 okAction = new AbstractAction(I18n.getMessage("jsite.general.ok")) {
264 @SuppressWarnings("synthetic-access")
265 public void actionPerformed(ActionEvent actionEvent) {
269 okAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.ok.tooltip"));
270 okAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ENTER);
272 cancelAction = new AbstractAction(I18n.getMessage("jsite.general.cancel")) {
275 @SuppressWarnings("synthetic-access")
276 public void actionPerformed(ActionEvent actionEvent) {
280 cancelAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.cancel.tooltip"));
281 cancelAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ESCAPE);
283 copyFromProjectAction = new AbstractAction(I18n.getMessage("jsite.key-dialog.button.copy-from-project")) {
286 @SuppressWarnings("synthetic-access")
287 public void actionPerformed(ActionEvent actionevent) {
288 actionCopyFromProject();
291 copyFromProjectAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.copy-from-project.tooltip"));
292 copyFromProjectAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_P);
293 copyFromProjectAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_DOWN_MASK));
295 copyFromIdentityAction = new AbstractAction(I18n.getMessage("jsite.key-dialog.button.copy-from-identity")) {
298 @SuppressWarnings("synthetic-access")
299 public void actionPerformed(ActionEvent actionevent) {
300 actionCopyFromIdentity();
303 copyFromIdentityAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.copy-from-identity.tooltip"));
304 copyFromIdentityAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_I);
305 copyFromIdentityAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_DOWN_MASK));
307 generateAction = new AbstractAction(I18n.getMessage("jsite.key-dialog.button.generate")) {
310 @SuppressWarnings("synthetic-access")
311 public void actionPerformed(ActionEvent actionEvent) {
315 generateAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.generate.tooltip"));
316 generateAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_DOWN_MASK));
320 * Initializes the dialog and all its components.
322 private void initDialog() {
324 JPanel dialogPanel = new JPanel(new BorderLayout(12, 12));
325 dialogPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
327 JPanel contentPanel = new JPanel(new GridBagLayout());
328 dialogPanel.add(contentPanel, BorderLayout.CENTER);
330 final JLabel keysLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.keys"));
331 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));
333 final JLabel privateKeyLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.private-key"));
334 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));
336 privateKeyTextField = new JTextField();
337 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));
339 final JLabel publicKeyLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.public-key"));
340 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));
342 publicKeyTextField = new JTextField();
343 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));
345 final JLabel copyKeysLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.copy-keys"));
346 contentPanel.add(copyKeysLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
348 final JLabel projectLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.project"));
349 contentPanel.add(projectLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 18, 0, 0), 0, 0));
351 projectsComboBox = new JComboBox(new ComboBoxModelList<Project>(projects));
352 projectsComboBox.addActionListener(new ActionListener() {
355 @SuppressWarnings("synthetic-access")
356 public void actionPerformed(ActionEvent actionEvent) {
357 copyFromProjectAction.setEnabled(projectsComboBox.getSelectedIndex() > -1);
361 contentPanel.add(projectsComboBox, new GridBagConstraints(1, 4, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 12, 0, 0), 0, 0));
363 JButton copyFromProjectButton = new JButton(copyFromProjectAction);
364 contentPanel.add(copyFromProjectButton, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.HORIZONTAL, new Insets(6, 12, 0, 0), 0, 0));
366 final JLabel identityLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.identity"));
367 contentPanel.add(identityLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 18, 0, 0), 0, 0));
369 ownIdentitiesComboBox = new JComboBox(new ComboBoxModelList<OwnIdentity>(ownIdentities));
370 ownIdentitiesComboBox.addActionListener(new ActionListener() {
373 @SuppressWarnings("synthetic-access")
374 public void actionPerformed(ActionEvent actionevent) {
375 copyFromIdentityAction.setEnabled(ownIdentitiesComboBox.getSelectedIndex() > -1);
378 ownIdentitiesComboBox.setRenderer(new DefaultListCellRenderer() {
381 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
382 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
386 OwnIdentity ownIdentity = (OwnIdentity) value;
387 setText(String.format("%s (%s)", ownIdentity.getNickname(), ownIdentity.getRequestUri().substring(0, ownIdentity.getRequestUri().indexOf(','))));
392 contentPanel.add(ownIdentitiesComboBox, new GridBagConstraints(1, 5, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(6, 12, 0, 0), 0, 0));
394 JButton copyFromIdentityButton = new JButton(copyFromIdentityAction);
395 contentPanel.add(copyFromIdentityButton, new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.HORIZONTAL, new Insets(6, 12, 0, 0), 0, 0));
397 final JLabel actionsLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.actions"));
398 contentPanel.add(actionsLabel, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
400 JPanel actionButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
401 actionButtonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
402 contentPanel.add(actionButtonPanel, new GridBagConstraints(0, 7, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 18, 0, 0), 0, 0));
404 actionButtonPanel.add(new JButton(generateAction));
406 JPanel separatorPanel = new JPanel(new BorderLayout(12, 12));
407 dialogPanel.add(separatorPanel, BorderLayout.PAGE_END);
408 separatorPanel.add(new JSeparator(SwingConstants.HORIZONTAL), BorderLayout.PAGE_START);
410 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
411 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
412 separatorPanel.add(buttonPanel, BorderLayout.CENTER);
413 buttonPanel.add(new JButton(okAction));
414 buttonPanel.add(new JButton(cancelAction));
416 I18nContainer.getInstance().registerRunnable(new Runnable() {
420 keysLabel.setText(I18n.getMessage("jsite.key-dialog.label.keys"));
421 privateKeyLabel.setText(I18n.getMessage("jsite.key-dialog.label.private-key"));
422 publicKeyLabel.setText(I18n.getMessage("jsite.key-dialog.label.public-key"));
423 copyKeysLabel.setText(I18n.getMessage("jsite.key-dialog.label.copy-keys"));
424 identityLabel.setText(I18n.getMessage("jsite.key-dialog.label.identity"));
425 projectLabel.setText(I18n.getMessage("jsite.key-dialog.label.project"));
426 actionsLabel.setText(I18n.getMessage("jsite.key-dialog.label.actions"));
430 getContentPane().add(dialogPanel, BorderLayout.CENTER);
440 * Quits the dialog, accepting all changes.
442 private void actionOk() {
443 publicKey = publicKeyTextField.getText();
444 privateKey = privateKeyTextField.getText();
450 * Quits the dialog, discarding all changes.
452 private void actionCancel() {
458 * Copies the public and private key from the selected project.
460 private void actionCopyFromProject() {
461 Project project = (Project) projectsComboBox.getSelectedItem();
462 if (project == null) {
465 setPublicKey(project.getRequestURI());
466 setPrivateKey(project.getInsertURI());
470 * Copies the public and private key from the selected identity.
472 private void actionCopyFromIdentity() {
473 OwnIdentity ownIdentity = (OwnIdentity) ownIdentitiesComboBox.getSelectedItem();
474 if (ownIdentity == null) {
477 setPublicKey(ownIdentity.getRequestUri());
478 setPrivateKey(ownIdentity.getInsertUri());
482 * Generates a new key pair.
484 private void actionGenerate() {
485 if (JOptionPane.showConfirmDialog(this, I18n.getMessage("jsite.project.warning.generate-new-key"), null, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
488 String[] keyPair = null;
490 keyPair = freenetInterface.generateKeyPair();
491 } catch (IOException ioe1) {
492 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE);
495 publicKeyTextField.setText(keyPair[1].substring(keyPair[1].indexOf('@') + 1, keyPair[1].lastIndexOf('/')));
496 privateKeyTextField.setText(keyPair[0].substring(keyPair[0].indexOf('@') + 1, keyPair[0].lastIndexOf('/')));