|
From: Pelle B. <pe...@us...> - 2004-04-09 23:10:08
|
Update of /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/passphraseagents/swing In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5482/src/java/org/neuclear/commons/crypto/passphraseagents/swing Modified Files: NewAliasDialog.java SwingAgent.java Added Files: SimpleDialog.java Log Message: SwingAgent now manages key creation as well through the NewAliasDialog. Many small uservalidation features have also been added. Index: SwingAgent.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/passphraseagents/swing/SwingAgent.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SwingAgent.java 7 Apr 2004 17:22:08 -0000 1.1 --- SwingAgent.java 9 Apr 2004 22:56:44 -0000 1.2 *************** *** 5,8 **** --- 5,9 ---- import com.jgoodies.forms.layout.CellConstraints; import com.jgoodies.forms.layout.FormLayout; + import com.jgoodies.plaf.Options; import org.neuclear.commons.crypto.Base64; import org.neuclear.commons.crypto.passphraseagents.InteractiveAgent; *************** *** 14,20 **** --- 15,25 ---- import javax.swing.*; + import javax.swing.event.ListSelectionEvent; + import javax.swing.event.ListSelectionListener; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; + import java.awt.event.KeyEvent; + import java.awt.event.KeyListener; import java.net.URL; import java.security.KeyStoreException; *************** *** 28,31 **** --- 33,40 ---- $Id$ $Log$ + Revision 1.2 2004/04/09 22:56:44 pelle + SwingAgent now manages key creation as well through the NewAliasDialog. + Many small uservalidation features have also been added. + Revision 1.1 2004/04/07 17:22:08 pelle Added support for the new improved interactive signing model. A new Agent is also available with SwingAgent. *************** *** 43,46 **** --- 52,56 ---- try { UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel"); + UIManager.put(Options.USE_SYSTEM_FONTS_APP_KEY, Boolean.TRUE); } catch (Exception e) { // Likely PlasticXP is not in the class path; ignore. *************** *** 48,51 **** --- 58,62 ---- cache = new HashMap(); sign = new JButton("Sign"); + sign.setEnabled(false); cancel = new JButton("Cancel"); newId = new JButton("New ..."); *************** *** 59,67 **** icon = new JLabel("NeuClear"); ! frame = new JFrame(); ! frame.setTitle("NeuClear Signing Agent"); ! frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); ! frame.getContentPane().add(buildPanel()); ! frame.pack(); cancel.addActionListener(new ActionListener() { --- 70,79 ---- icon = new JLabel("NeuClear"); ! dialog = new JDialog(); ! dialog.setTitle("NeuClear Signing Agent"); ! dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); ! dialog.getContentPane().add(buildPanel()); ! dialog.pack(); ! nad = new NewAliasDialog(this); cancel.addActionListener(new ActionListener() { *************** *** 79,84 **** public void actionPerformed(final ActionEvent actionEvent) { synchronized (passphrase) { ! isCancel = false; ! passphrase.notifyAll(); } --- 91,98 ---- public void actionPerformed(final ActionEvent actionEvent) { synchronized (passphrase) { ! if (validate()) { ! isCancel = false; ! passphrase.notifyAll(); ! } } *************** *** 87,96 **** sign.addActionListener(action); passphrase.addActionListener(action); ! // frame.show(); } --- 101,149 ---- sign.addActionListener(action); passphrase.addActionListener(action); + final KeyListener validate = new KeyListener() { + public void keyPressed(KeyEvent e) { + } + public void keyReleased(KeyEvent e) { + sign.setEnabled(validate()); ! } ! ! public void keyTyped(KeyEvent e) { ! ! } ! }; ! passphrase.addKeyListener(validate); ! list.addListSelectionListener(new ListSelectionListener() { ! /** ! * Called whenever the value of the selection changes. ! * ! * @param e the event that characterizes the change. ! */ ! public void valueChanged(ListSelectionEvent e) { ! sign.setEnabled(validate()); ! } ! ! }); + newId.addActionListener(new ActionListener() { + public void actionPerformed(final ActionEvent actionEvent) { + SwingUtilities.invokeLater(nad); + } + }); + + + // dialog.show(); + + } + + void updateList(String alias) { + if (alias != null) { + fillAliasList(); + list.setSelectedValue(alias, true); + dialog.pack(); + } } *************** *** 130,145 **** public byte[] sign(BrowsableSigner signer, byte data[], SetPublicKeyCallBack callback) throws UserCancellationException { synchronized (passphrase) {//We dont want multiple agents popping up at the same time ! try { ! ! Iterator iter = signer.iterator(); ! Vector vector = new Vector(); ! while (iter.hasNext()) { ! Object o = iter.next(); ! vector.add(o); ! } ! list.setListData(vector); ! } catch (KeyStoreException e) { ! e.printStackTrace(); ! } // if (cache.containsKey(name)) --- 183,188 ---- public byte[] sign(BrowsableSigner signer, byte data[], SetPublicKeyCallBack callback) throws UserCancellationException { synchronized (passphrase) {//We dont want multiple agents popping up at the same time ! this.signer = signer; ! fillAliasList(); // if (cache.containsKey(name)) *************** *** 154,160 **** // // nameLabel.setText(name); ! frame.pack(); ! frame.show(); ! // frame.setVisible(true); try { passphrase.wait(); --- 197,204 ---- // // nameLabel.setText(name); ! dialog.pack(); ! dialog.show(); ! sign.setEnabled(false); ! // dialog.setVisible(true); try { passphrase.wait(); *************** *** 162,167 **** ; } ! frame.hide(); ! // frame.setVisible(false); final String phrase = passphrase.getText(); // if (remember.getState()) --- 206,211 ---- ; } ! dialog.hide(); ! // dialog.setVisible(false); final String phrase = passphrase.getText(); // if (remember.getState()) *************** *** 172,177 **** return signer.sign(list.getSelectedValue().toString(), phrase.toCharArray(), data, callback); } catch (InvalidPassphraseException e) { ! return new byte[0];//TODO handle invalid passphrase } } } --- 216,236 ---- return signer.sign(list.getSelectedValue().toString(), phrase.toCharArray(), data, callback); } catch (InvalidPassphraseException e) { ! return sign(signer, data, callback); ! } ! } ! } ! ! private void fillAliasList() { ! try { ! ! Iterator iter = signer.iterator(); ! Vector vector = new Vector(); ! while (iter.hasNext()) { ! Object o = iter.next(); ! vector.add(o); } + list.setListData(vector); + } catch (KeyStoreException e) { + e.printStackTrace(); } } *************** *** 201,206 **** // // nameLabel.setText(name); ! frame.pack(); ! frame.setVisible(true); try { passphrase.wait(); --- 260,265 ---- // // nameLabel.setText(name); ! dialog.pack(); ! dialog.setVisible(true); try { passphrase.wait(); *************** *** 208,212 **** ; } ! frame.setVisible(false); if (isCancel) throw new UserCancellationException(name); --- 267,271 ---- ; } ! dialog.setVisible(false); if (isCancel) throw new UserCancellationException(name); *************** *** 219,229 **** } private final JButton sign; private final JButton cancel; private final JButton newId; private final JList list; ! private final JTextField passphrase; ! private final JFrame frame; private final Map cache; private boolean isCancel; --- 278,301 ---- } + BrowsableSigner getSigner() { + return signer; + } + + JDialog getDialog() { + return dialog; + } + + private boolean validate() { + return (list.getSelectedIndex() > 0 && passphrase.getPassword().length > 0); + } + private BrowsableSigner signer; private final JButton sign; private final JButton cancel; private final JButton newId; private final JList list; ! private final JPasswordField passphrase; ! private final JDialog dialog; ! private final NewAliasDialog nad; private final Map cache; private boolean isCancel; *************** *** 233,238 **** final InteractiveAgent dia = new SwingAgent(); try { - final BrowsableSigner signer = new TestCaseSigner(dia); try { byte sig[] = signer.sign("testdata".getBytes(), new SetPublicKeyCallBack() { public void setPublicKey(PublicKey pub) { --- 305,310 ---- final InteractiveAgent dia = new SwingAgent(); try { try { + final BrowsableSigner signer = new TestCaseSigner(dia); byte sig[] = signer.sign("testdata".getBytes(), new SetPublicKeyCallBack() { public void setPublicKey(PublicKey pub) { --- NEW FILE: SimpleDialog.java --- package org.neuclear.commons.crypto.passphraseagents.swing; import com.jgoodies.forms.builder.ButtonBarBuilder; import com.jgoodies.forms.builder.PanelBuilder; import com.jgoodies.forms.layout.CellConstraints; import com.jgoodies.forms.layout.FormLayout; import com.jgoodies.plaf.Options; import org.neuclear.commons.crypto.passphraseagents.UserCancellationException; import org.neuclear.commons.crypto.signers.BrowsableSigner; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.net.URL; /* $Id: SimpleDialog.java,v 1.1 2004/04/09 22:56:44 pelle Exp $ $Log: SimpleDialog.java,v $ Revision 1.1 2004/04/09 22:56:44 pelle SwingAgent now manages key creation as well through the NewAliasDialog. Many small uservalidation features have also been added. Revision 1.1 2004/04/07 17:22:08 pelle Added support for the new improved interactive signing model. A new Agent is also available with SwingAgent. The XMLSig classes have also been updated to support this. */ /** * User: pelleb * Date: Apr 7, 2004 * Time: 9:55:37 AM */ public class SimpleDialog { public SimpleDialog(SwingAgent agent) { try { UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel"); UIManager.put(Options.USE_SYSTEM_FONTS_APP_KEY, Boolean.TRUE); } catch (Exception e) { // Likely PlasticXP is not in the class path; ignore. } this.agent = agent; ok = new JButton("Open"); ok.setEnabled(false); cancel = new JButton("Cancel"); passphrase = new JPasswordField(); final URL imageurl = this.getClass().getClassLoader().getResource("org/neuclear/commons/crypto/passphraseagents/neuclear.png"); if (imageurl != null) icon = new JLabel(new ImageIcon(imageurl)); else icon = new JLabel("NeuClear"); dialog = new JDialog(); dialog.setTitle("NeuClear Signing Agent"); dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); dialog.getContentPane().add(buildPanel()); dialog.pack(); cancel.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent actionEvent) { synchronized (passphrase) { passphrase.setText(""); isCancel = true; passphrase.notifyAll(); } } }); final ActionListener action = new ActionListener() { public void actionPerformed(final ActionEvent actionEvent) { synchronized (passphrase) { if (validate()) { isCancel = false; passphrase.notifyAll(); } } } }; ok.addActionListener(action); passphrase.addActionListener(action); final KeyListener validate = new KeyListener() { public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { ok.setEnabled(validate()); } public void keyTyped(KeyEvent e) { } }; passphrase.addKeyListener(validate); } private Component buildPanel() { FormLayout layout = new FormLayout("right:pref, 3dlu, pref:grow ", "pref,3dlu,pref, 3dlu, fill:pref:grow, 3dlu, pref, 7dlu, pref"); PanelBuilder builder = new PanelBuilder(layout); CellConstraints cc = new CellConstraints(); builder.setDefaultDialogBorder(); builder.add(icon, cc.xyw(1, 5, 1, CellConstraints.LEFT, CellConstraints.TOP)); builder.addSeparator("Enter passphrase", cc.xyw(1, 3, 3)); builder.addLabel("Passphrase:", cc.xy(1, 7)); builder.add(passphrase, cc.xy(3, 7)); ButtonBarBuilder bb = new ButtonBarBuilder(); bb.addGlue(); bb.addUnrelatedGap(); bb.addGridded(ok); bb.addGridded(cancel); builder.add(bb.getPanel(), cc.xyw(1, 9, 3)); return builder.getPanel(); } /** * Retrieve the PassPhrase for a given name/alias * * @param name * @return */ public char[] getPassPhrase(String name) throws UserCancellationException { return getPassPhrase(name, false); } public char[] getPassPhrase(String name, boolean incorrect) throws UserCancellationException { synchronized (passphrase) {//We dont want multiple agents popping up at the same time isCancel = true; // if (incorrect) // System.err.println("Incorrect passphrase"); // incorrectLabel.setVisible(incorrect); // nameLabel.setVisible(true); // // nameLabel.setText(name); dialog.pack(); dialog.setVisible(true); try { passphrase.wait(); } catch (InterruptedException e) { ; } dialog.setVisible(false); if (isCancel) throw new UserCancellationException(name); final String phrase = passphrase.getText(); // if (remember.getState()) // cache.put(name, phrase); passphrase.setText(""); return phrase.toCharArray(); } } BrowsableSigner getSigner() { return signer; } JDialog getDialog() { return dialog; } private boolean validate() { return (passphrase.getPassword().length > 0); } private BrowsableSigner signer; private final JButton ok; private final JButton cancel; private final JPasswordField passphrase; private final JDialog dialog; private boolean isCancel; private SwingAgent agent; private final JLabel icon; } Index: NewAliasDialog.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/passphraseagents/swing/NewAliasDialog.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** NewAliasDialog.java 9 Apr 2004 18:40:45 -0000 1.1 --- NewAliasDialog.java 9 Apr 2004 22:56:44 -0000 1.2 *************** *** 5,15 **** --- 5,25 ---- import com.jgoodies.forms.layout.CellConstraints; import com.jgoodies.forms.layout.FormLayout; + import com.jgoodies.plaf.Options; + import org.neuclear.commons.crypto.CryptoException; import javax.swing.*; import java.awt.*; + import java.awt.event.ActionEvent; + import java.awt.event.ActionListener; + import java.awt.event.KeyEvent; + import java.awt.event.KeyListener; /* $Id$ $Log$ + Revision 1.2 2004/04/09 22:56:44 pelle + SwingAgent now manages key creation as well through the NewAliasDialog. + Many small uservalidation features have also been added. + Revision 1.1 2004/04/09 18:40:45 pelle BrowsableSigner now inherits Signer and PublicKeySource, which means implementations only need to implement BrowsableSigner now. *************** *** 23,45 **** * Time: 5:58:38 PM */ ! public class NewAliasDialog { ! public NewAliasDialog() { ok = new JButton("Create"); ok.setEnabled(false); cancel = new JButton("Cancel"); alias = new JTextField(); passphrase1 = new JPasswordField(); passphrase2 = new JPasswordField(); ! frame = new JFrame(); ! frame.setTitle("NeuClear Signing Agent"); ! frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); ! frame.getContentPane().add(buildPanel()); ! frame.pack(); } private Component buildPanel() { ! FormLayout layout = new FormLayout("right:pref, 3dlu, pref:grow ", "pref,3dlu,pref, 3dlu, pref, 3dlu, pref, 7dlu, pref"); PanelBuilder builder = new PanelBuilder(layout); --- 33,121 ---- * Time: 5:58:38 PM */ ! public class NewAliasDialog implements Runnable { ! public NewAliasDialog(SwingAgent agent) { ! try { ! UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel"); ! UIManager.put(Options.USE_SYSTEM_FONTS_APP_KEY, Boolean.TRUE); ! } catch (Exception e) { ! // Likely PlasticXP is not in the class path; ignore. ! } ! this.agent = agent; ok = new JButton("Create"); ok.setEnabled(false); cancel = new JButton("Cancel"); alias = new JTextField(); + passphrase1 = new JPasswordField(); passphrase2 = new JPasswordField(); ! dialog = new JDialog(agent.getDialog(), true); ! dialog.setTitle("NeuClear Signing Agent"); ! dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); ! dialog.getContentPane().add(buildPanel()); ! dialog.pack(); ! ! cancel.addActionListener(new ActionListener() { ! public void actionPerformed(final ActionEvent actionEvent) { ! synchronized (alias) { ! passphrase1.setText(""); ! passphrase2.setText(""); ! alias.setText(""); ! dialog.hide(); ! } ! ! } ! }); ! ! final KeyListener validate = new KeyListener() { ! public void keyPressed(KeyEvent e) { ! ! } ! ! public void keyReleased(KeyEvent e) { ! ok.setEnabled(validate()); ! ! } ! ! public void keyTyped(KeyEvent e) { ! ! } ! }; ! alias.addKeyListener(validate); ! passphrase1.addKeyListener(validate); ! passphrase2.addKeyListener(validate); ! ! final ActionListener action = new ActionListener() { ! public void actionPerformed(final ActionEvent actionEvent) { ! synchronized (alias) { ! if (validate()) { ! createAlias(); ! } ! } ! ! } ! }; ! ! ok.addActionListener(action); ! passphrase2.addActionListener(action); } + private boolean validate() { + if (alias.getText().length() == 0) + return false; + char[] p1 = passphrase1.getPassword(); + char[] p2 = passphrase2.getPassword(); + if (p1 == null || p2 == null) + return false; + if (p1.length != p2.length) + return false; + if (p1.length == 0) + return false; + return true;//new String(p1).equals(new String(p2)); + } + private Component buildPanel() { ! FormLayout layout = new FormLayout("right:pref, 3dlu, 100dlu:grow ", "pref,3dlu,pref, 3dlu, pref, 3dlu, pref, 7dlu, pref"); PanelBuilder builder = new PanelBuilder(layout); *************** *** 67,71 **** } ! private JFrame frame; private JButton ok; private JButton cancel; --- 143,189 ---- } ! ! /** ! * When an object implementing interface <code>Runnable</code> is used ! * to create a thread, starting the thread causes the object's ! * <code>run</code> method to be called in that separately executing ! * thread. ! * <p/> ! * The general contract of the method <code>run</code> is that it may ! * take any action whatsoever. ! * ! * @see Thread#run() ! */ ! public void run() { ! ok.setEnabled(false); ! cancel.setEnabled(true); ! passphrase1.setEnabled(true); ! alias.setEnabled(true); ! passphrase2.setEnabled(true); ! dialog.pack(); ! dialog.show(); ! ! } ! ! private void createAlias() { ! dialog.setEnabled(false); ! passphrase1.setEnabled(false); ! alias.setEnabled(false); ! passphrase2.setEnabled(false); ! ok.setEnabled(false); ! cancel.setEnabled(true); ! try { ! System.out.println("Generating Key"); ! agent.getSigner().createKeyPair(alias.getText(), passphrase1.getPassword()); ! agent.updateList(alias.getText()); ! } catch (CryptoException e) { ! e.printStackTrace(); ! } ! ! dialog.hide(); ! } ! ! private SwingAgent agent; ! private JDialog dialog; private JButton ok; private JButton cancel; *************** *** 74,80 **** private JPasswordField passphrase2; - public static void main(String args[]) { - NewAliasDialog dg = new NewAliasDialog(); - dg.frame.show(); - } } --- 192,194 ---- |