KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > crypto > PassphraseSettingDialog


1 package SnowMailClient.crypto;
2
3 import snow.utils.gui.*;
4
5 import SnowMailClient.SnowMailClientApp;
6 import snow.Language.Language;
7 import snow.crypto.*;
8
9 import java.awt.*;
10 import java.awt.event.*;
11 import javax.swing.*;
12
13 import java.security.*;
14 import java.security.spec.*;
15 import javax.crypto.spec.*;
16 import javax.crypto.*;
17 import javax.swing.border.*;
18 import java.io.*;
19 import java.util.zip.*;
20 import java.util.*;
21
22 /** Ask for a passphrase
23 */

24 public final class PassphraseSettingDialog extends JDialog
25 {
26
27   final int fontSize = UIManager.getFont("Label.font").getSize();
28   final private JPasswordField passField = new JPasswordField(20);
29   final private JPasswordField passField2 = new JPasswordField(20);
30
31   final private JTextArea explanationArea = new JTextArea();
32   private CloseControlPanel ccp = null;
33   private SecureInputKeyboardDialog keyboardDialog;
34
35
36   /**
37   */

38   public PassphraseSettingDialog( JFrame owner, String JavaDoc title, boolean isModal, String JavaDoc explanation )
39   {
40     super(owner,title,isModal);
41     explanationArea.setText(explanation);
42     init();
43   }
44
45   public PassphraseSettingDialog( JDialog owner, String JavaDoc title, boolean isModal, String JavaDoc explanation )
46   {
47     super(owner,title,isModal);
48     explanationArea.setText(explanation);
49     init();
50   }
51
52   private void init()
53   {
54     JPanel cp = new SnowBackgroundPanel(new BorderLayout());
55     setContentPane(cp);
56     cp.setBorder(new EmptyBorder(fontSize/2, fontSize/2, fontSize/2, fontSize/2));
57
58     // South
59
ccp = new CloseControlPanel(this, true, true, Language.translate("Validate"));
60     getContentPane().add(ccp, BorderLayout.SOUTH);
61
62     // Center
63
getContentPane().add(this.explanationArea, BorderLayout.NORTH);
64     explanationArea.setEditable(false);
65     explanationArea.setBackground(cp.getBackground());
66
67     JPanel centerPanel_ = new JPanel(); //new GridLayout(2,2, fontSize/2, fontSize/2));
68
GridLayout3 grid = new GridLayout3(2, centerPanel_);
69     centerPanel_.setBorder(new EmptyBorder(fontSize/2, fontSize/2, fontSize/2, fontSize/2));
70     getContentPane().add(centerPanel_, BorderLayout.CENTER);
71     grid.add(new JContrastLabel(Language.translate("Passphrase"),
72         SnowMailClientApp.loadImageIcon("pics/key.PNG"), JLabel.LEFT), false);
73     grid.add(passField, true);
74     passField.addActionListener(new ActionListener()
75     {
76       public void actionPerformed(ActionEvent e)
77       {
78         passField2.requestFocus();
79       }
80     });
81
82     KeyAdapter ka = new KeyAdapter()
83     {
84       @Override JavaDoc public void keyReleased(KeyEvent e)
85       {
86         ccp.setOkEnabled(confirmationMatches());
87         shiftDown = e.isShiftDown();
88       }
89
90       @Override JavaDoc public void keyPressed(KeyEvent e)
91       {
92         shiftDown = e.isShiftDown();
93       }
94     };
95     passField.addKeyListener(ka);
96     passField2.addKeyListener(ka);
97     ccp.setOkEnabled(false);
98
99     grid.add(new JContrastLabel(Language.translate("Confirmation")), false);
100     grid.add(passField2, true);
101     passField2.addActionListener(new ActionListener()
102     {
103       public void actionPerformed(ActionEvent e)
104       {
105         terminate();
106       }
107     });
108     passField2.addKeyListener(new KeyAdapter()
109     {
110       @Override JavaDoc public void keyReleased(KeyEvent e)
111       {
112         ccp.setOkEnabled(confirmationMatches());
113       }
114     });
115
116     this.pack();
117
118     this.setLocationRelativeTo(this.getOwner());
119
120     this.addComponentListener(new ComponentAdapter()
121     {
122       @Override JavaDoc public void componentShown(ComponentEvent e)
123       {
124         passField.requestFocus();
125       }
126     });
127
128     keyboardDialog = new SecureInputKeyboardDialog(this);
129     keyboardDialog.addKeyListener(new SecureInputKeyboardDialog.HitListener()
130     {
131        public void keyTyped(char c)
132        {
133           JPasswordField pf = (passField2.hasFocus() ? passField2 : passField);
134           if(shiftDown)
135           {
136             pf.setText( new String JavaDoc(pf.getPassword()) + Character.toUpperCase(c) );
137           }
138           else
139           {
140             pf.setText( new String JavaDoc(pf.getPassword()) + c );
141           }
142           ccp.setOkEnabled(confirmationMatches());
143        }
144     });
145
146     keyboardDialog.setVisible(true);
147
148     addWindowListener(new WindowAdapter()
149     {
150         public void windowClosed(WindowEvent e)
151         {
152            terminate();
153         }
154         public void windowClosing(WindowEvent e)
155         {
156            terminate();
157         }
158     });
159
160
161     // MODAL !!!
162
this.setVisible(true);
163
164   } // initialize
165

166
167   private boolean shiftDown = false;
168
169   public boolean wasCancelled()
170   {
171     return ccp.getWasCancelled();
172   }
173
174   public boolean confirmationMatches()
175   {
176     return Arrays.equals(passField.getPassword(), passField2.getPassword());
177   }
178
179   public SecretKey getKey() throws Exception JavaDoc
180   {
181     byte[] pass = new String JavaDoc(passField.getPassword()).getBytes();
182     return SecretKeyUtilities.generateSecretKeyFromPassphrase(pass, 16);
183   }
184
185
186   private final void terminate()
187   {
188     setVisible(false);
189     this.keyboardDialog.terminate();
190
191   }
192
193 } // PassphraseSettingDialog
Popular Tags