Add possibility to copy a WoT identity’s keys.
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Thu, 20 Sep 2012 13:09:49 +0000 (15:09 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Thu, 20 Sep 2012 13:09:49 +0000 (15:09 +0200)
src/main/java/de/todesbaum/jsite/gui/KeyDialog.java
src/main/resources/de/todesbaum/jsite/i18n/jSite.properties
src/main/resources/de/todesbaum/jsite/i18n/jSite_de.properties
src/main/resources/de/todesbaum/jsite/i18n/jSite_fr.properties

index a48d921..2906175 100644 (file)
@@ -19,6 +19,7 @@
 package de.todesbaum.jsite.gui;
 
 import java.awt.BorderLayout;
+import java.awt.Component;
 import java.awt.Dimension;
 import java.awt.FlowLayout;
 import java.awt.GridBagConstraints;
@@ -26,20 +27,27 @@ import java.awt.GridBagLayout;
 import java.awt.Insets;
 import java.awt.Toolkit;
 import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.awt.event.InputEvent;
 import java.awt.event.KeyEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import java.io.IOException;
 import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
 
 import javax.swing.AbstractAction;
 import javax.swing.Action;
 import javax.swing.BorderFactory;
+import javax.swing.DefaultListCellRenderer;
 import javax.swing.JButton;
+import javax.swing.JComboBox;
 import javax.swing.JDialog;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
+import javax.swing.JList;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JSeparator;
@@ -47,9 +55,11 @@ import javax.swing.JTextField;
 import javax.swing.KeyStroke;
 import javax.swing.SwingConstants;
 
+import net.pterodactylus.util.swing.ComboBoxModelList;
 import de.todesbaum.jsite.application.Freenet7Interface;
 import de.todesbaum.jsite.i18n.I18n;
 import de.todesbaum.jsite.i18n.I18nContainer;
+import de.todesbaum.util.freenet.fcp2.wot.OwnIdentity;
 
 /**
  * A dialog that lets the user edit the private and public key for a project.
@@ -76,15 +86,24 @@ public class KeyDialog extends JDialog {
        /** The “Regenerate” button’s action. */
        private Action generateAction;
 
+       /** The “Copy from Identity” action. */
+       private Action copyFromIdentityAction;
+
        /** The text field for the private key. */
        private JTextField privateKeyTextField;
 
        /** The text field for the public key. */
        private JTextField publicKeyTextField;
 
+       /** The select box for the own identities. */
+       private JComboBox ownIdentitiesComboBox;
+
        /** Whether the dialog was cancelled. */
        private boolean cancelled;
 
+       /** The list of own identities. */
+       private final List<OwnIdentity> ownIdentities = new ArrayList<OwnIdentity>();
+
        /**
         * Creates a new key dialog.
         *
@@ -162,6 +181,28 @@ public class KeyDialog extends JDialog {
                pack();
        }
 
+       /**
+        * Sets the own identities to display and copy URIs from.
+        *
+        * @param ownIdentities
+        *            The list of own identities
+        */
+       public void setOwnIdentities(Collection<? extends OwnIdentity> ownIdentities) {
+               synchronized (this.ownIdentities) {
+                       this.ownIdentities.clear();
+                       this.ownIdentities.addAll(ownIdentities);
+               }
+               int selectedIndex = -1;
+               int index = 0;
+               for (OwnIdentity ownIdentity : ownIdentities) {
+                       if (ownIdentity.getInsertUri().equals(privateKey) && ownIdentity.getRequestUri().equals(publicKey)) {
+                               selectedIndex = index;
+                       }
+                       index++;
+               }
+               ownIdentitiesComboBox.setSelectedIndex(selectedIndex);
+       }
+
        //
        // ACTIONS
        //
@@ -206,6 +247,17 @@ public class KeyDialog extends JDialog {
                cancelAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.cancel.tooltip"));
                cancelAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ESCAPE);
 
+               copyFromIdentityAction = new AbstractAction(I18n.getMessage("jsite.key-dialog.button.copy-from-identity")) {
+
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionevent) {
+                               actionCopyFromIdentity();
+                       }
+               };
+               copyFromIdentityAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.copy-from-identity.tooltip"));
+               copyFromIdentityAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK));
+
                generateAction = new AbstractAction(I18n.getMessage("jsite.key-dialog.button.generate")) {
 
                        @Override
@@ -236,20 +288,54 @@ public class KeyDialog extends JDialog {
                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));
 
                privateKeyTextField = new JTextField();
-               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));
+               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));
 
                final JLabel publicKeyLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.public-key"));
                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));
 
                publicKeyTextField = new JTextField();
-               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));
+               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));
+
+               final JLabel identitiesLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.identities"));
+               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));
+
+               final JLabel identityLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.identity"));
+               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));
+
+               ownIdentitiesComboBox = new JComboBox(new ComboBoxModelList<OwnIdentity>(ownIdentities));
+               ownIdentitiesComboBox.addActionListener(new ActionListener() {
+
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionevent) {
+                               copyFromIdentityAction.setEnabled(ownIdentitiesComboBox.getSelectedIndex() > -1);
+                       }
+               });
+               ownIdentitiesComboBox.setRenderer(new DefaultListCellRenderer() {
+
+                       @Override
+                       public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
+                               super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
+                               if (value == null) {
+                                       setText("");
+                               } else {
+                                       OwnIdentity ownIdentity = (OwnIdentity) value;
+                                       setText(String.format("%s (%s)", ownIdentity.getNickname(), ownIdentity.getRequestUri().substring(0, ownIdentity.getRequestUri().indexOf(','))));
+                               }
+                               return this;
+                       }
+               });
+               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));
+
+               JButton copyFromIdentityButton = new JButton(copyFromIdentityAction);
+               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));
 
                final JLabel actionsLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.actions"));
-               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));
+               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));
 
                JPanel actionButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
                actionButtonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
-               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));
+               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));
 
                actionButtonPanel.add(new JButton(generateAction));
 
@@ -270,6 +356,8 @@ public class KeyDialog extends JDialog {
                                keysLabel.setText(I18n.getMessage("jsite.key-dialog.label.keys"));
                                privateKeyLabel.setText(I18n.getMessage("jsite.key-dialog.label.private-key"));
                                publicKeyLabel.setText(I18n.getMessage("jsite.key-dialog.label.public-key"));
+                               identitiesLabel.setText(I18n.getMessage("jsite.key-dialog.label.identities"));
+                               identityLabel.setText(I18n.getMessage("jsite.key-dialog.label.identity"));
                                actionsLabel.setText(I18n.getMessage("jsite.key-dialog.label.actions"));
                        }
                });
@@ -302,6 +390,18 @@ public class KeyDialog extends JDialog {
        }
 
        /**
+        * Copies the public and private key from the selected identity.
+        */
+       private void actionCopyFromIdentity() {
+               OwnIdentity ownIdentity = (OwnIdentity) ownIdentitiesComboBox.getSelectedItem();
+               if (ownIdentity == null) {
+                       return;
+               }
+               setPublicKey(ownIdentity.getRequestUri());
+               setPrivateKey(ownIdentity.getInsertUri());
+       }
+
+       /**
         * Generates a new key pair.
         */
        private void actionGenerate() {
index 942692d..81bfc89 100644 (file)
@@ -175,11 +175,15 @@ jsite.update-checker.latest-version.okay.message=<html>You are currently running
 jsite.key-dialog.title=Manage Project Keys
 jsite.key-dialog.button.ok.tooltip=Accepts the changes
 jsite.key-dialog.button.cancel.tooltip=Discards the changes
+jsite.key-dialog.button.copy-from-identity=Copy from Identity
+jsite.key-dialog.button.copy-from-identity.tooltip=Copies the public and private keys from the selected identity
 jsite.key-dialog.button.generate=Regenerate Keys
 jsite.key-dialog.button.generate.tooltip=Create a new key pair
 jsite.key-dialog.label.keys=<html><b>Keys</b></html>
 jsite.key-dialog.label.private-key=Private Key
 jsite.key-dialog.label.public-key=Public Key
+jsite.key-dialog.label.identities=<html><b>Identities</b></html>
+jsite.key-dialog.label.identity=Identity
 jsite.key-dialog.label.actions=<html><b>Actions</b></html>
 
 jsite.warning.empty-index=<html><b>No default file</b><br><br>You did not specify a default file for this project.<br>While it is possible to insert a project without a default<br>file you should specify one to ease browsing.</html>
index dd04f6f..7e34e0e 100644 (file)
@@ -175,11 +175,15 @@ jsite.update-checker.latest-version.okay.message=<html>Es l\u00e4uft momentan Ve
 jsite.key-dialog.title=Projektschl\u00fcsselverwaltung
 jsite.key-dialog.button.ok.tooltip=\u00c4nderungen akzeptieren
 jsite.key-dialog.button.cancel.tooltip=\u00c4nderungen verwerfen
+jsite.key-dialog.button.copy-from-identity=Von Identit\u00e4t kopieren
+jsite.key-dialog.button.copy-from-identity.tooltip=Kopiert das Schl\u00fcsselpaar der ausgew\u00e4hlten Identit\u00e4t
 jsite.key-dialog.button.generate=Schl\u00fcssel neu generieren
 jsite.key-dialog.button.generate.tooltip=Generiert ein neues Schl\u00fcsselpaar
 jsite.key-dialog.label.keys=<html><b>Schl\u00fcssel</b></html>
 jsite.key-dialog.label.private-key=Privater Schl\u00fcssel
 jsite.key-dialog.label.public-key=\u00d6ffentlicher Schl\u00fcssel
+jsite.key-dialog.label.identities=<html><b>Identit\u00e4ten</b></html>
+jsite.key-dialog.label.identity=Identit\u00e4t
 jsite.key-dialog.label.actions=<html><b>Aktionen</b></html>
 
 jsite.warning.empty-index=<html><b>Keine Index-Datei gew\u00e4hlt</b><br><br>Sie haben keine Index-Datei f\u00fcr das Projekt angegeben.<br>Obwohl es m\u00f6glich ist, das zu machen, sollten Sie doch<br>eine Index-Datei angeben, um das Browsen zu erleichtern.</html>
index 0f40e0f..0778bf2 100644 (file)
@@ -175,11 +175,15 @@ jsite.update-checker.latest-version.okay.message=<html>Vous utilisez la version
 jsite.key-dialog.title=G\u00e9rer les cl\u00e9s des projets
 jsite.key-dialog.button.ok.tooltip=Accepter les changements
 jsite.key-dialog.button.cancel.tooltip=Annuler les changements
+jsite.key-dialog.button.copy-from-identity=Copy from Identity
+jsite.key-dialog.button.copy-from-identity.tooltip=Copies the public and private keys from the selected identity
 jsite.key-dialog.button.generate=Reg\u00e9n\u00e9rer les cl\u00e9s
 jsite.key-dialog.button.generate.tooltip=Cr\u00e9er une nouvelle paire de cl\u00e9s
 jsite.key-dialog.label.keys=<html><b>Cl\u00e9s</b></html>
 jsite.key-dialog.label.private-key=Cl\u00e9 priv\u00e9e
 jsite.key-dialog.label.public-key=Cl\u00e9 publique
+jsite.key-dialog.label.identities=<html><b>Identities</b></html>
+jsite.key-dialog.label.identity=Identity
 jsite.key-dialog.label.actions=<html><b>Actions</b></html>
 
 jsite.warning.empty-index=<html><b>Pas de fichier par d\u00e9faut</b><br><br>Avez vous sp\u00e9cifi\u00e9 un fichier par d\u00e9faut pour le projet?<br>M\u00eame s'il est possible de ne pas en sp\u00e9cifier, c'est g\u00e9n\u00e9ralement une mauvaise id\u00e9e.</html>