KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > LogInFrame


1
2 import java.awt.GridLayout JavaDoc;
3 import java.awt.event.*;
4 import java.io.*;
5 import javax.swing.*;
6 import javax.swing.border.EmptyBorder JavaDoc;
7 import javax.swing.plaf.metal.MetalLookAndFeel JavaDoc;
8 import whisper.*;
9
10 public final class LogInFrame{
11     
12     final JFrame frame=new JFrame("Whisper IM "+Lang.gs("login"));
13     final JButton configBtn=new JButton(Lang.gs("configure"), Icons.CONFIG);
14     final JButton loginBtn=new JButton(Lang.gs("login"),Icons.LOGIN);
15     final JButton addBtn=new JButton(Lang.gs("new account"), Icons.NEW_ACCOUNT);
16     final JButton quitBtn=new JButton(Lang.gs("quit"), Icons.QUIT);
17     final JPasswordField passphrase=new JPasswordField(25);
18     final JComboBox accountList=new JComboBox();
19     final JLabel accountLbl=new JLabel(Lang.gs("account"));
20     final JLabel accountNameLbl=new JLabel();
21     final JLabel passphraseLbl=new JLabel(Lang.gs("passphrase"));
22     final JLabel blank=new JLabel("");
23     final JLabel blank2=new JLabel("");
24     final GridLayout JavaDoc gl=new GridLayout JavaDoc(5,2,10,10);
25     final JPanel p=new JPanel(gl);
26     final BtnClick click=new BtnClick();
27     
28     public LogInFrame(){
29         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
30         frame.setResizable(false);
31         frame.getContentPane().add(p);
32         p.setBorder(new EmptyBorder JavaDoc(10, 10, 10, 10));
33         
34         frame.setIconImage(Icons.LOGO.getImage());
35         
36         loginBtn.setDefaultCapable(true);
37         addBtn.setDefaultCapable(true);
38
39         // See if configBtn is enabled
40
configBtn.setVisible(WhisperIM.CONFIG_FILE.canWrite());
41         // see if we need the log in components (ie account exists) on non shared environment
42
boolean needLogin=true;
43         if(!WhisperIM.sharedMachine){
44             if(WhisperIM.userDir.exists()){
45                 accountNameLbl.setText(System.getProperties().getProperty("user.name"));
46             }
47             else{
48                 needLogin=false;
49             }
50         }
51         //If in shared environment get accounts listing
52
if(WhisperIM.sharedMachine){
53             File[] aclist=WhisperIM.accountDir.listFiles();
54             if(aclist.length==0){
55                 needLogin=false; // no need for login components if no accounts
56
}
57             else{
58                 for (int i=0;i<aclist.length;i++){
59                     if(aclist[i].isDirectory()){
60                         accountList.addItem(aclist[i].getName());
61                     }
62                 }
63                 if(accountList.getItemCount()==0){
64                     needLogin=false;
65                 }
66             }
67         }
68         // see if we need new account button
69
if(needLogin && !WhisperIM.sharedMachine){
70             addBtn.setVisible(false);
71         }
72         // set visiility of login components
73
if(!needLogin){
74             accountLbl.setVisible(false);
75             accountNameLbl.setVisible(false);
76             if(WhisperIM.sharedMachine){
77                 accountList.setVisible(false);
78             }
79             passphraseLbl.setVisible(false);
80             passphrase.setVisible(false);
81             loginBtn.setVisible(false);
82             frame.getRootPane().setDefaultButton(addBtn);
83         }
84         else{
85             frame.getRootPane().setDefaultButton(loginBtn);
86         }
87         // set label text alignment and controls
88
accountLbl.setHorizontalAlignment(SwingConstants.RIGHT);
89         passphraseLbl.setHorizontalAlignment(SwingConstants.RIGHT);
90         if(WhisperIM.sharedMachine){
91             accountLbl.setLabelFor(accountList);
92         }
93         passphraseLbl.setLabelFor(passphrase);
94         // set mneomics
95
if(WhisperIM.sharedMachine){
96             accountLbl.setDisplayedMnemonic(Lang.s2k("account_mn"));
97         }
98         passphraseLbl.setDisplayedMnemonic(Lang.s2k("passphrase_mn"));
99         loginBtn.setMnemonic(Lang.s2k("login_mn"));
100         configBtn.setMnemonic(Lang.s2k("configure_mn"));
101         addBtn.setMnemonic(Lang.s2k("new account_mn"));
102         quitBtn.setMnemonic(Lang.s2k("quit_mn"));
103         // add controls to panel
104
p.add(accountLbl);
105         if(WhisperIM.sharedMachine){
106             p.add(accountList);
107         }
108         else{
109             p.add(accountNameLbl);
110         }
111         p.add(passphraseLbl);
112         p.add(passphrase);
113         p.add(blank);
114         p.add(loginBtn);
115         p.add(configBtn);
116         p.add(addBtn);
117         p.add(blank2);
118         p.add(quitBtn);
119         frame.pack();
120         // add action listeners
121
frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(WhisperIM.HELP_KEY,0),"help"); //put in help key for action help
122
frame.getRootPane().getActionMap().put("help",new HelpAction(frame,"intro.html")); //add help action for help action
123
loginBtn.addActionListener(click);
124         addBtn.addActionListener(click);
125         configBtn.addActionListener(click);
126         quitBtn.addActionListener(click);
127         //set frame set and position
128
frame.setSize(frame.getMinimumSize());
129         frame.setLocationRelativeTo(null);
130     }
131     
132     public void doLogin(){
133         char pp[]=passphrase.getPassword();
134         if(pp.length<20){
135             GUI.showInfo(frame,"login","incorrect passphrase");
136             passphrase.setText("");
137             passphrase.requestFocus();
138             return;
139         }
140         frame.getGlassPane().setVisible(true);
141         frame.setCursor(GUI.WAIT);
142         frame.setEnabled(false);
143         // set the user directory
144
if(WhisperIM.sharedMachine){
145             WhisperIM.userDir=new File(WhisperIM.accountDir,accountList.getSelectedItem().toString());
146         }
147         
148         try{
149             File f=new File(WhisperIM.userDir,WhisperIM.KEYPAIR_FILENAME);
150             FileInputStream fis=new FileInputStream(f);
151             byte salt[]=new byte[64];
152             fis.read(salt,0,64);
153             
154             WhisperInputStream wis=new WhisperInputStream(fis,Util.passphrase2Key(pp,salt));
155             PublicKey pubKey=(PublicKey)wis.read();
156             PrivateKey privKey=(PrivateKey)wis.read();
157             
158             WhisperIM.KEYPAIR=new KeyPair(pubKey,privKey);
159             
160             wis.close();
161             fis.close();
162         }
163         catch(whisper.CryptoIOException ce){
164             frame.setCursor(GUI.NORMAL);
165             frame.getGlassPane().setVisible(false);
166             frame.setEnabled(true);
167             GUI.showInfo(frame,"login","incorrect passphrase");
168             passphrase.setText("");
169             passphrase.requestFocus();
170             return;
171         }
172         catch(whisper.CryptoException c){
173             frame.setCursor(GUI.NORMAL);
174             frame.getGlassPane().setVisible(false);
175             frame.setEnabled(true);
176             GUI.showInfo(frame,"login","incorrect passphrase");
177             passphrase.setText("");
178             passphrase.requestFocus();
179             return;
180         }
181         catch(Exception JavaDoc kpe){
182             try{
183                 kpe.printStackTrace();
184                 BackUp.restore(new File(WhisperIM.userDir,WhisperIM.KEYPAIR_FILENAME));
185                 File f=new File(WhisperIM.userDir,WhisperIM.KEYPAIR_FILENAME);
186                 FileInputStream fis=new FileInputStream(f);
187                 byte salt[]=new byte[64];
188                 fis.read(salt,0,64);
189                 WhisperInputStream wis=new WhisperInputStream(fis,Util.passphrase2Key(pp,salt));
190                 PublicKey pubKey=(PublicKey)wis.read();
191                 PrivateKey privKey=(PrivateKey)wis.read();
192                 WhisperIM.KEYPAIR=new KeyPair(pubKey,privKey);
193                 wis.close();
194                 fis.close();
195             }
196             catch(Exception JavaDoc kpe2){
197                 frame.setCursor(GUI.NORMAL);
198                 frame.getGlassPane().setVisible(false);
199                 frame.setEnabled(true);
200                 kpe2.printStackTrace();
201                 GUI.showError(frame,"error","could not load account settings",kpe.getMessage());
202                 return;
203             }
204         }
205         try{
206             WhisperIM.MainWindow.loadKeyCacheKey();
207         }
208         catch(Exception JavaDoc kcke){
209             try{
210                 kcke.printStackTrace();
211                 BackUp.restore(new File(WhisperIM.userDir,WhisperIM.KCK_FILE_NAME));
212                 WhisperIM.MainWindow.loadKeyCacheKey();
213             }
214             catch(Exception JavaDoc kcke2){
215                 kcke2.printStackTrace();
216                 GUI.showError(frame,"error","could not load account settings",kcke.getMessage());
217                 return;
218             }
219         }
220         
221         // Load account settings
222
try{
223             WhisperIM.accountSettings.Load();
224         }
225         catch(Exception JavaDoc ase){
226             try{
227                 ase.printStackTrace();
228                 BackUp.restore(new File(WhisperIM.userDir,AccountSettings.FILENAME));
229                 WhisperIM.accountSettings.Load();
230             }
231             catch(Exception JavaDoc ase2){
232                 frame.setCursor(GUI.NORMAL);
233                 frame.getGlassPane().setVisible(false);
234                 frame.setEnabled(true);
235                 ase2.printStackTrace();
236                 GUI.showError(frame,"error","could not load account settings",ase.getMessage());
237                 return;
238             }
239         }
240         
241         // load prefs
242
if(!WhisperIM.userPrefLoaded){
243             String JavaDoc currentLang=Lang.gs("language_code");
244             GUI.loadPrefs();
245             if( (!WhisperIM.UserPref.getProperty("lang").equals("auto")) || (!Lang.gs("language_code").equals(currentLang)) ){ // see if language overidden by user preferences
246
WhisperIM.MainWindow.setLang(); // change labels to new language
247
}
248             if(!WhisperIM.currentLaf.equals(WhisperIM.UserPref.getProperty("laf"))){
249                 //System.out.println(WhisperIM.currentLaf);
250
//System.out.println(WhisperIM.UserPref.getProperty("laf"));
251
try{//change the look and feel
252
if(WhisperIM.UserPref.getProperty("laf").equals("metal")){
253                         //UIManager.put("ClassLoader", getClass().getClassLoader());
254
UIManager.setLookAndFeel(new com.incors.plaf.kunststoff.KunststoffLookAndFeel());
255                     }
256                     else{
257                         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
258                     }
259                     WhisperIM.currentLaf=WhisperIM.UserPref.getProperty("laf");
260                     // update
261
WhisperIM.MainWindow.dispose();
262                     WhisperIM.MainWindow=new JMainFrame();
263                     WhisperIM.MainWindow.loadKeyCacheKey();
264                     WhisperIM.About.dispose();
265                     WhisperIM.About=new JAboutDialog();
266                     if(WhisperIM.Help_Window!=null){
267                         SwingUtilities.updateComponentTreeUI(WhisperIM.Help_Window.getFrame());
268                     }
269                 }
270                 catch(Exception JavaDoc lafe){
271                     System.err.println("Problem changing look and feel in LogInFrame.class doLogin");
272                     lafe.printStackTrace();
273                 }
274             }
275         }
276         passphrase.setText("");
277         if(WhisperIM.Config_Window!=null){
278             WhisperIM.Config_Window.dispose();
279             WhisperIM.Config_Window=null;
280         }
281         if(WhisperIM.New_Account_Window!=null){
282             WhisperIM.New_Account_Window.dispose();
283             WhisperIM.New_Account_Window=null;
284         }
285         Util.wipe(pp);
286         frame.dispose();
287         WhisperIM.Login_Window=null;
288         WhisperIM.MainWindow.show();
289     }
290     
291     public void show(){
292         frame.show();
293         frame.toFront();
294         if(passphrase.isVisible()){
295             if( (!WhisperIM.sharedMachine) || accountList.getItemCount()==1){
296                 passphrase.requestFocus();
297                 return;
298             }
299             accountList.requestFocus();
300             return;
301         }
302         addBtn.requestFocus();
303     }
304     
305     public void doQuit(){
306         System.exit(0);
307     }
308     
309     public void dispose(){
310         frame.dispose();
311     }
312     
313     public JFrame getFrame(){
314         return frame;
315     }
316     
317     final class BtnClick implements ActionListener{
318         public void actionPerformed(ActionEvent ae){
319             //get button
320
String JavaDoc b=ae.getActionCommand();
321             // call method depending on button clicked
322
if(b.equals(quitBtn.getText())){
323                 doQuit();
324                 return;
325             }
326             if(b.equals(configBtn.getText())){
327                 if(WhisperIM.Config_Window==null){
328                     frame.setCursor(GUI.WAIT);
329                     WhisperIM.Config_Window=new ConfigDialog();
330                     frame.setCursor(GUI.NORMAL);
331                 }
332                 WhisperIM.Config_Window.show();
333                 return;
334             }
335             if(b.equals(addBtn.getText())){
336                 if(WhisperIM.New_Account_Window==null){
337                     frame.setCursor(GUI.WAIT);
338                     WhisperIM.New_Account_Window=new NewAccountWindow();
339                     frame.setCursor(GUI.NORMAL);
340                 }
341                 WhisperIM.New_Account_Window.show();
342                 return;
343             }
344             if(b.equals(loginBtn.getText())){
345                 doLogin();
346                 return;
347             }
348         }
349     }
350 }
Popular Tags