KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ChangePassphraseDialog


1 import java.awt.*;
2 import java.awt.event.*;
3 import java.awt.image.ImageObserver JavaDoc;
4 import java.io.*;
5 import javax.swing.*;
6 import javax.accessibility.Accessible JavaDoc;
7 import org.jivesoftware.smack.*;
8 import whisper.WhisperOutputStream;
9 import whisper.Util;
10
11 /** Dialog for user preferences.*/
12 public final class ChangePassphraseDialog extends JDialog implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, RootPaneContainer, Serializable, SwingConstants,WindowConstants{
13     
14     private final static int GAP=5;
15         
16     final BorderLayout layout=new BorderLayout(GAP,GAP);
17     final JPanel panel=new JPanel(new SpringLayout());
18         final JLabel pass1Lbl=new JLabel(Lang.gs("new passphrase"));
19         final JPasswordField pass1=new JPasswordField(20);
20         final JLabel pass2Lbl=new JLabel(Lang.gs("reenter new passphrase"));
21         final JPasswordField pass2=new JPasswordField(20);
22     
23     final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT,GAP,GAP));
24         final JButton changeBtn=new JButton(Lang.gs("change passphrase"));
25         final JButton cancelBtn=new JButton(Lang.gs("cancel"));
26     
27     final BtnClick click=new BtnClick();
28     
29     public ChangePassphraseDialog(){
30         super(WhisperIM.MainWindow.optionsDialog,Lang.gs("change passphrase"),true);
31         setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
32         Container cp=getContentPane();
33         cp.setLayout(layout);
34         
35         panel.add(pass1Lbl);
36         panel.add(pass1);
37         panel.add(pass2Lbl);
38         panel.add(pass2);
39         SpringUtilities.makeCompactGrid(panel,2,2,GAP,GAP,GAP,GAP);
40         cp.add(panel,BorderLayout.CENTER);
41         
42         cancelBtn.setMnemonic(Lang.s2k("cancel_mn"));
43         cancelBtn.addActionListener(click);
44         changeBtn.addActionListener(click);
45         cancelBtn.setDefaultCapable(true);
46         getRootPane().setDefaultButton(cancelBtn);
47         btnPanel.add(changeBtn);
48         btnPanel.add(cancelBtn);
49         cp.add(btnPanel,BorderLayout.SOUTH);
50         pack();
51         setLocationRelativeTo(WhisperIM.MainWindow.optionsDialog);
52         addKeyListener(new KeyAction());
53         addMouseListener(new MouseAction());
54     }
55     
56     public void doChange(){
57         try{
58             //validate first
59
String JavaDoc p1=new String JavaDoc(pass1.getPassword());
60             String JavaDoc p2=new String JavaDoc(pass2.getPassword());
61             if(p1.length()<20){
62                 GUI.showInfo(this,"change passphrase","passphrase too short");
63                 pass1.requestFocus();
64                 return;
65             }
66             if(!p1.equals(p2)){
67                 GUI.showInfo(this,"change passphrase","passphrase mismatch");
68                 pass2.requestFocus();
69                 return;
70             }
71             p1=null;
72             p2=null;
73             setCursor(GUI.WAIT);
74             // backup first
75
BackUp.backUp(new File(WhisperIM.userDir,WhisperIM.KEYPAIR_FILENAME));
76             // key pair
77
WhisperOutputStream wos=null;
78             try{
79                 byte salt[]=new byte[64];
80                 WhisperIM.SR.nextBytes(salt);
81                 wos=new WhisperOutputStream(new File(WhisperIM.userDir,WhisperIM.KEYPAIR_FILENAME), Util.passphrase2Key(pass1.getPassword(),salt), WhisperIM.SR, salt);
82                 wos.write(WhisperIM.KEYPAIR.publicKey());
83                 wos.write(WhisperIM.KEYPAIR.privateKey());
84                 wos.close();
85             }
86             catch(Exception JavaDoc kpe){
87                 if(wos!=null){
88                     wos.close();
89                 }
90                 //pray the restore works or we're in trouble
91
BackUp.restore(new File(WhisperIM.userDir,WhisperIM.KEYPAIR_FILENAME));
92                 setCursor(GUI.NORMAL);
93                 GUI.showError(this,"change passphrase","cannot save keypair",kpe.getMessage());
94                 return;
95             }
96             GUI.showInfo(this,"change passphrase","passphrase changed");
97             dispose();
98         }
99         catch(Exception JavaDoc e){
100             setCursor(GUI.NORMAL);
101             GUI.showError(this,"change password",null,e.getMessage());
102         }
103     }
104     
105     final class BtnClick implements ActionListener{
106         public void actionPerformed(ActionEvent ae){
107             //get button
108
String JavaDoc b=ae.getActionCommand();
109             // call method depending on button clicked
110
if(b.equals(cancelBtn.getText())){
111                 dispose();
112                 return;
113             }
114             if(b.equals(changeBtn.getText())){
115                 doChange();
116             }
117         }
118     }
119 }
Popular Tags