1 package SnowMailClient.view.dialogs; 2 3 import SnowMailClient.crypto.Utilities; 4 import SnowMailClient.utils.*; 5 import snow.utils.gui.*; 6 import SnowMailClient.*; 7 import SnowMailClient.Language.Language; 8 import javax.swing.*; 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.util.Arrays ; 12 13 17 public class PasswordInputDialog extends JDialog 18 { 19 private final JPasswordField pass1 = new JPasswordField(20); 20 private boolean testPassed = false; 21 Frame modalOwner; 22 byte[] sign; 23 24 public PasswordInputDialog(final Frame modalOwner, 25 String title, 26 String description, 27 final byte[] sign) 28 { 29 super(modalOwner, title, true); 30 31 this.modalOwner = modalOwner; 32 this.sign = (sign!=null? sign.clone(): null); 33 34 35 36 JTextArea descrTA = new JTextArea(description); 37 descrTA.setBorder(BorderFactory.createEmptyBorder(3,3,3,3)); 38 descrTA.setEditable(false); 39 40 getContentPane().setLayout( new BorderLayout()); 41 42 JPanel centerPanel = new JPanel(new BorderLayout()); 43 JPanel passPanel = new JPanel(new GridLayout(1, 2)); 44 centerPanel.add(passPanel,BorderLayout.CENTER); 45 getContentPane().add(centerPanel, BorderLayout.CENTER); 46 getContentPane().add(descrTA, BorderLayout.NORTH); 47 48 JPanel contrlpanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 49 50 getContentPane().add(contrlpanel, BorderLayout.SOUTH); 51 52 ActionListener disposeActionListener = new ActionListener() 53 { 54 public void actionPerformed(ActionEvent e) 55 { 56 dispose(); 57 } 58 }; 59 ActionListener okActionListener = new ActionListener() 60 { 61 public void actionPerformed(ActionEvent e) 62 { 63 okAction(); } 64 }; 65 66 67 JButton cancel = new JButton(Language.translate("Cancel"), Icons.CrossIcon.shared10); 68 contrlpanel.add(cancel); 69 cancel.addActionListener(disposeActionListener); 70 71 JButton close = new JButton(Language.translate(" Ok "), Icons.OkIcon.shared10); 72 contrlpanel.add(close); 73 close.addActionListener(okActionListener); 74 75 passPanel.add(new JLabel("Password ")); 76 passPanel.add(pass1); 77 78 pass1.addActionListener(okActionListener); 79 80 81 82 this.getRootPane().registerKeyboardAction( disposeActionListener, 83 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false), 84 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ); 85 86 this.getRootPane().registerKeyboardAction( okActionListener, 87 KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), 88 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ); 89 90 91 92 94 pack(); 95 SnowMailClientApp.centerComponentOnMainFrame(this); 96 pass1.requestFocus(); 97 setVisible(true); 98 } 99 100 101 private void okAction() 102 { 103 104 String hash = Utilities.hashPassword(new String (pass1.getPassword())); 106 107 if(sign!=null && !Arrays.equals(sign, Utilities.hashPassword(hash).substring(0,4).getBytes() )) 110 { 111 JOptionPane.showMessageDialog( modalOwner, "Bad Password" ); 112 pass1.setSelectionStart(0); 113 pass1.setSelectionEnd(pass1.getPassword().length); 114 pass1.requestFocus(); 115 116 } 117 else 118 { 119 testPassed = true; 123 dispose(); 124 } 125 } 126 127 128 public char[] getPassword() {return pass1.getPassword();} 129 public boolean getPasswordOK() {return testPassed;} 130 131 132 } | Popular Tags |