KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PassphraseDialog


1 import java.awt.*;
2 import java.awt.event.*;
3 import java.awt.image.ImageObserver JavaDoc;
4 import java.io.*;
5 import java.util.ListResourceBundle JavaDoc;
6 import java.util.Properties JavaDoc;
7 import javax.swing.*;
8 import javax.swing.border.*;
9 import javax.accessibility.Accessible JavaDoc;
10 import whisper.*;
11
12
13 /** Dialog for re-entering passphrase.*/
14 public final class PassphraseDialog extends JDialog implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, RootPaneContainer, Serializable, SwingConstants,WindowConstants{
15     
16     private final static int GAP=5;
17         
18     final BorderLayout layout=new BorderLayout(GAP,GAP);
19     final JPanel panel=new JPanel(new SpringLayout());
20         final JLabel pass1Lbl=new JLabel(Lang.gs("passphrase"));
21         final JPasswordField passphrase=new JPasswordField(20);
22     
23     final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT,GAP,GAP));
24         final JButton loginBtn=new JButton(Lang.gs("login"),Icons.LOGIN);
25         final JButton quitBtn=new JButton(Lang.gs("quit"),Icons.QUIT);
26     
27     final BtnClick click=new BtnClick();
28     
29     public PassphraseDialog(){
30         super(WhisperIM.MainWindow,Lang.gs("locked"),true);
31         setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
32         Container cp=getContentPane();
33         cp.setLayout(layout);
34         
35         panel.add(pass1Lbl);
36         panel.add(passphrase);
37         SpringUtilities.makeCompactGrid(panel,1,2,GAP,GAP,GAP,GAP);
38         cp.add(panel,BorderLayout.CENTER);
39         
40         quitBtn.setMnemonic(Lang.s2k("quit_mn"));
41         quitBtn.addActionListener(click);
42         loginBtn.addActionListener(click);
43         loginBtn.setDefaultCapable(true);
44         getRootPane().setDefaultButton(loginBtn);
45         btnPanel.add(quitBtn);
46         btnPanel.add(loginBtn);
47         cp.add(btnPanel,BorderLayout.SOUTH);
48         pack();
49         setLocationRelativeTo(WhisperIM.MainWindow);
50         WhisperIM.MainWindow.contactListPanel.setVisible(false);
51         WhisperIM.MainWindow.chatPane.setVisible(false);
52     }
53     
54     public void doLogin(){
55         char pp[]=passphrase.getPassword();
56         if(pp.length<20){
57             GUI.showInfo(this,"login","incorrect passphrase");
58             passphrase.setText("");
59             passphrase.requestFocus();
60             return;
61         }
62         setCursor(GUI.WAIT);
63         try{
64             File f=new File(WhisperIM.userDir,WhisperIM.KEYPAIR_FILENAME);
65             FileInputStream fis=new FileInputStream(f);
66             byte salt[]=new byte[64];
67             fis.read(salt,0,64);
68             
69             WhisperInputStream wis=new WhisperInputStream(fis,Util.passphrase2Key(pp,salt));
70             PublicKey pubKey=(PublicKey)wis.read();
71             PrivateKey privKey=(PrivateKey)wis.read();
72             
73             WhisperIM.KEYPAIR=new KeyPair(pubKey,privKey);
74             
75             wis.close();
76             fis.close();
77             WhisperIM.MainWindow.loadKeyCacheKey();
78         }
79         catch(whisper.CryptoIOException ce){
80             setCursor(GUI.NORMAL);
81             setEnabled(true);
82             GUI.showInfo(this,"login","incorrect passphrase");
83             passphrase.setText("");
84             passphrase.requestFocus();
85             return;
86         }
87         catch(whisper.CryptoException c){
88             setCursor(GUI.NORMAL);
89             setEnabled(true);
90             GUI.showInfo(this,"login","incorrect passphrase");
91             passphrase.setText("");
92             passphrase.requestFocus();
93             return;
94         }
95         catch(Exception JavaDoc e){
96             setCursor(GUI.NORMAL);
97             setEnabled(true);
98             e.printStackTrace();
99             GUI.showError(this,"error","could not load account settings",e.getMessage());
100             return;
101         }
102         passphrase.setText("");
103         Util.wipe(pp);
104         WhisperIM.MainWindow.contactListPanel.setVisible(true);
105         WhisperIM.MainWindow.chatPane.setVisible(true);
106         setCursor(GUI.NORMAL);
107         dispose();
108     }
109     
110     final class BtnClick implements ActionListener{
111         public void actionPerformed(ActionEvent ae){
112             //get button
113
String JavaDoc b=ae.getActionCommand();
114             // call method depending on button clicked
115
if(b.equals(quitBtn.getText())){
116                 WhisperIM.MainWindow.intentionalQuit=true;
117                 dispose();
118                 System.exit(0);
119             }
120             if(b.equals(loginBtn.getText())){
121                 doLogin();
122             }
123         }
124     }
125 }
Popular Tags