KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ChangeJabberPasswordDialog


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
9 /** Dialog for user preferences.*/
10 public final class ChangeJabberPasswordDialog extends JDialog implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, RootPaneContainer, Serializable, SwingConstants,WindowConstants{
11     
12     private final static int GAP=5;
13         
14     final BorderLayout layout=new BorderLayout(GAP,GAP);
15     final JPanel panel=new JPanel();
16     
17         final Box box=new Box(BoxLayout.X_AXIS);
18             final JLabel passwordLbl=new JLabel(Lang.gs("new password"));
19             final JPasswordField password=new JPasswordField(15);
20     
21     final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT,GAP,GAP));
22         final JButton changeBtn=new JButton(Lang.gs("change password"));
23         final JButton cancelBtn=new JButton(Lang.gs("cancel"));
24     
25     final BtnClick click=new BtnClick();
26     
27     public ChangeJabberPasswordDialog(){
28         super(WhisperIM.MainWindow.optionsDialog,Lang.gs("change password"),true);
29         setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
30         Container cp=getContentPane();
31         cp.setLayout(layout);
32         
33         box.add(passwordLbl);
34         box.add(box.createHorizontalStrut(GAP));
35         box.add(password);
36         panel.add(box);
37         cp.add(panel,BorderLayout.CENTER);
38         
39         cancelBtn.setMnemonic(Lang.s2k("cancel_mn"));
40         cancelBtn.addActionListener(click);
41         changeBtn.addActionListener(click);
42         cancelBtn.setDefaultCapable(true);
43         getRootPane().setDefaultButton(cancelBtn);
44         btnPanel.add(changeBtn);
45         btnPanel.add(cancelBtn);
46         cp.add(btnPanel,BorderLayout.SOUTH);
47         pack();
48         setLocationRelativeTo(WhisperIM.MainWindow.optionsDialog);
49         addKeyListener(new KeyAction());
50         addMouseListener(new MouseAction());
51     }
52     
53     public void doChange(){
54         try{
55             if(password.getPassword().length==0){
56                 return;
57             }
58             setCursor(GUI.WAIT);
59             AccountManager am=new AccountManager(WhisperIM.MainWindow.Conn);
60             String JavaDoc pw=new String JavaDoc(password.getPassword());
61             password.setText("");
62             am.changePassword(pw);
63             setCursor(GUI.NORMAL);
64             WhisperIM.accountSettings.JabberPassword=pw;
65             WhisperIM.MainWindow.optionsDialog.needToSaveAccountSettings=true;
66             WhisperIM.MainWindow.optionsDialog.password.setText(pw);
67             WhisperIM.MainWindow.optionsDialog.password.setEditable(false);
68             WhisperIM.MainWindow.optionsDialog.cancelBtn.setEnabled(false);
69             GUI.showInfo(this,"change password","jabber password changed");
70             dispose();
71         }
72         catch(XMPPException xe){
73             setCursor(GUI.NORMAL);
74             GUI.showError(this,"change password",null,null,xe);
75             return;
76         }
77         catch(Exception JavaDoc e){
78             setCursor(GUI.NORMAL);
79             GUI.showError(this,"change password",null,e.getMessage());
80         }
81     }
82     
83     final class BtnClick implements ActionListener{
84         public void actionPerformed(ActionEvent ae){
85             //get button
86
String JavaDoc b=ae.getActionCommand();
87             // call method depending on button clicked
88
if(b.equals(cancelBtn.getText())){
89                 dispose();
90                 return;
91             }
92             if(b.equals(changeBtn.getText())){
93                 doChange();
94             }
95         }
96     }
97 }
Popular Tags