From: David ‘Bombe’ Roden Date: Thu, 14 Jan 2010 12:57:47 +0000 (+0100) Subject: Add dialog to manage a project’s keys. X-Git-Tag: 0.9~32 X-Git-Url: https://git.pterodactylus.net/?p=jSite.git;a=commitdiff_plain;h=3a538c657652e408b4507d59a8c3c48bbf00caad Add dialog to manage a project’s keys. --- diff --git a/src/de/todesbaum/jsite/application/KeyDialog.java b/src/de/todesbaum/jsite/application/KeyDialog.java new file mode 100644 index 0000000..2e30829 --- /dev/null +++ b/src/de/todesbaum/jsite/application/KeyDialog.java @@ -0,0 +1,308 @@ +/* + * jSite - KeyDialog.java - Copyright © 2010 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package de.todesbaum.jsite.application; + +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.awt.Toolkit; +import java.awt.event.ActionEvent; +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 javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.BorderFactory; +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JSeparator; +import javax.swing.JTextField; +import javax.swing.KeyStroke; +import javax.swing.SwingConstants; + +import de.todesbaum.jsite.i18n.I18n; +import de.todesbaum.jsite.i18n.I18nContainer; + +/** + * A dialog that lets the user edit the private and public key for a project. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ +public class KeyDialog extends JDialog { + + /** Interface to the freenet node. */ + private final Freenet7Interface freenetInterface; + + /** The public key. */ + private String publicKey; + + /** The private key. */ + private String privateKey; + + /** The “OK” button’s action. */ + private Action okAction; + + /** The “Cancel” button’s action. */ + private Action cancelAction; + + /** The “Regenerate” button’s action. */ + private Action generateAction; + + /** The text field for the private key. */ + private JTextField privateKeyTextField; + + /** The text field for the public key. */ + private JTextField publicKeyTextField; + + /** + * Creates a new key dialog. + * + * @param freenetInterface + * Interface to the freenet node + * @param parent + * The parent frame + */ + public KeyDialog(Freenet7Interface freenetInterface, JFrame parent) { + super(parent, I18n.getMessage("jsite.key-dialog.title"), true); + this.freenetInterface = freenetInterface; + addWindowListener(new WindowAdapter() { + + @Override + @SuppressWarnings("synthetic-access") + public void windowClosing(WindowEvent windowEvent) { + actionCancel(); + } + }); + initDialog(); + } + + // + // ACCESSORS + // + + /** + * Returns the public key. + * + * @return The public key + */ + public String getPublicKey() { + return publicKey; + } + + /** + * Sets the public key. + * + * @param publicKey + * The public key + */ + public void setPublicKey(String publicKey) { + this.publicKey = publicKey; + publicKeyTextField.setText(publicKey); + pack(); + } + + /** + * Returns the private key. + * + * @return The private key + */ + public String getPrivateKey() { + return privateKey; + } + + /** + * Sets the private key. + * + * @param privateKey + * The private key + */ + public void setPrivateKey(String privateKey) { + this.privateKey = privateKey; + privateKeyTextField.setText(privateKey); + pack(); + } + + // + // ACTIONS + // + + /** + * {@inheritDoc} + */ + @Override + public void pack() { + super.pack(); + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2); + } + + // + // PRIVATE METHODS + // + + /** + * Creates all necessary actions. + */ + private void createActions() { + okAction = new AbstractAction(I18n.getMessage("jsite.general.ok")) { + + @Override + @SuppressWarnings("synthetic-access") + public void actionPerformed(ActionEvent actionEvent) { + actionOk(); + } + }; + okAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.ok.tooltip")); + okAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ENTER); + + cancelAction = new AbstractAction(I18n.getMessage("jsite.general.cancel")) { + + @Override + @SuppressWarnings("synthetic-access") + public void actionPerformed(ActionEvent actionEvent) { + actionCancel(); + } + }; + cancelAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.cancel.tooltip")); + cancelAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_ESCAPE); + + generateAction = new AbstractAction(I18n.getMessage("jsite.key-dialog.button.generate")) { + + @Override + @SuppressWarnings("synthetic-access") + public void actionPerformed(ActionEvent actionEvent) { + actionGenerate(); + } + }; + generateAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.key-dialog.button.generate.tooltip")); + generateAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_DOWN_MASK)); + } + + /** + * Initializes the dialog and all its components. + */ + private void initDialog() { + createActions(); + JPanel dialogPanel = new JPanel(new BorderLayout(12, 12)); + dialogPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); + + JPanel contentPanel = new JPanel(new GridBagLayout()); + dialogPanel.add(contentPanel, BorderLayout.CENTER); + + final JLabel keysLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.keys")); + 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)); + + final JLabel privateKeyLabel = new JLabel(I18n.getMessage("jsite.key-dialog.label.private-key")); + 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)); + + 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)); + + 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)); + + 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)); + + actionButtonPanel.add(new JButton(generateAction)); + + JPanel separatorPanel = new JPanel(new BorderLayout(12, 12)); + dialogPanel.add(separatorPanel, BorderLayout.PAGE_END); + separatorPanel.add(new JSeparator(SwingConstants.HORIZONTAL), BorderLayout.PAGE_START); + + JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12)); + buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12)); + separatorPanel.add(buttonPanel, BorderLayout.CENTER); + buttonPanel.add(new JButton(okAction)); + buttonPanel.add(new JButton(cancelAction)); + + I18nContainer.getInstance().registerRunnable(new Runnable() { + + @Override + public void run() { + 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")); + actionsLabel.setText(I18n.getMessage("jsite.key-dialog.label.actions")); + } + }); + + getContentPane().add(dialogPanel, BorderLayout.CENTER); + pack(); + setResizable(false); + } + + // + // PRIVATE ACTIONS + // + + /** + * Quits the dialog, accepting all changes. + */ + private void actionOk() { + publicKey = publicKeyTextField.getText(); + privateKey = privateKeyTextField.getText(); + setVisible(false); + } + + /** + * Quits the dialog, discarding all changes. + */ + private void actionCancel() { + setVisible(false); + } + + /** + * Generates a new key pair. + */ + private void actionGenerate() { + if (JOptionPane.showConfirmDialog(this, I18n.getMessage("jsite.project.warning.generate-new-key"), null, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) { + return; + } + String[] keyPair = null; + try { + keyPair = freenetInterface.generateKeyPair(); + } catch (IOException ioe1) { + JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.project.keygen.io-error"), ioe1.getMessage()), null, JOptionPane.ERROR_MESSAGE); + return; + } + publicKeyTextField.setText(keyPair[1].substring(keyPair[1].indexOf('@') + 1, keyPair[1].lastIndexOf('/'))); + privateKeyTextField.setText(keyPair[0].substring(keyPair[0].indexOf('@') + 1, keyPair[0].lastIndexOf('/'))); + pack(); + } + +} diff --git a/src/de/todesbaum/jsite/i18n/jSite.properties b/src/de/todesbaum/jsite/i18n/jSite.properties index 3333977..ed063c6 100644 --- a/src/de/todesbaum/jsite/i18n/jSite.properties +++ b/src/de/todesbaum/jsite/i18n/jSite.properties @@ -30,6 +30,9 @@ jsite.main.already-running=jSite is already running

A lock file has been found that suggests that another
instance of jSite is already running. Running multiple instances
of jSite is guaranteed to break your configuration. jsite.main.already-running.override=Start anyway +jsite.general.ok=OK +jsite.general.cancel=Cancel + jsite.wizard.previous=Previous jsite.wizard.next=Next jsite.wizard.quit=Quit @@ -173,3 +176,13 @@ jsite.update-checker.latest-version.title=Update Check jsite.update-checker.latest-version.newer.message=You are running version {0} but a newer
version ({1}) has been found! jsite.update-checker.latest-version.older.message=You are running version {0} but the
latest version seems to be {1}. jsite.update-checker.latest-version.okay.message=You are currently running version {0}
which is the latest version. + +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.generate=Regenerate Keys +jsite.key-dialog.button.generate.tooltip=Create a new key pair +jsite.key-dialog.label.keys=Keys +jsite.key-dialog.label.private-key=Private Key +jsite.key-dialog.label.public-key=Public Key +jsite.key-dialog.label.actions=Actions diff --git a/src/de/todesbaum/jsite/i18n/jSite_de.properties b/src/de/todesbaum/jsite/i18n/jSite_de.properties index 83c7f1c..b0d3268 100644 --- a/src/de/todesbaum/jsite/i18n/jSite_de.properties +++ b/src/de/todesbaum/jsite/i18n/jSite_de.properties @@ -30,6 +30,9 @@ jsite.main.already-running=jSite l\u00e4uft bereits!

Es wurde festgestellt, dass jSite bereits l\u00e4uft. Das kann
zu Besch\u00e4digungen an der Konfiguration f\u00fchren. jsite.main.already-running.override=Trotzdem starten +jsite.general.ok=OK +jsite.general.cancel=Abbrechen + jsite.wizard.previous=Zur\u00fcck jsite.wizard.next=Vorw\u00e4rts jsite.wizard.quit=Beenden @@ -173,3 +176,13 @@ jsite.update-checker.latest-version.title=Update\u00fcberpr\u00fcfung jsite.update-checker.latest-version.newer.message=Es l\u00e4uft momentan Version {0}, aber eine
neue Version ({1}) wurde bereits gefunden! jsite.update-checker.latest-version.older.message=Es l\u00e4uft momentan Version {0}, aber die
aktuelle Version ist erst {1}. jsite.update-checker.latest-version.okay.message=Es l\u00e4uft momentan Version {0},
und diese Version ist aktuell. + +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.generate=Schl\u00fcssel neu generieren +jsite.key-dialog.button.generate.tooltip=Generiert ein neues Schl\u00fcsselpaar +jsite.key-dialog.label.keys=Schl\u00fcssel +jsite.key-dialog.label.private-key=Privater Schl\u00fcssel +jsite.key-dialog.label.public-key=\u00d6ffentlicher Schl\u00fcssel +jsite.key-dialog.label.actions=Aktionen