KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MyProfileWindow


1 import java.awt.*;
2 import java.awt.event.*;
3 import java.awt.image.ImageObserver JavaDoc;
4 import java.io.*;
5 import java.util.*;
6 import javax.swing.*;
7 import javax.swing.tree.*;
8 import javax.accessibility.Accessible JavaDoc;
9 import org.jivesoftware.smack.*;
10 import org.jivesoftware.smackx.*;
11 import org.jivesoftware.smack.packet.*;
12 import org.jivesoftware.smack.filter.*;
13 import org.jivesoftware.smack.util.StringUtils;
14 import whisper.PublicKey;
15
16 /** Window that shows a profile.*/
17 public final class MyProfileWindow extends JDialog implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, RootPaneContainer, Serializable, WindowConstants{
18     
19     private static final int GAP=10;
20     
21     final JProfilePane profilePane=new JProfilePane();
22     
23     final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT,GAP,GAP));
24         final JButton saveBtn=new JButton(Lang.gs("save"),Icons.SAVE);
25         final JButton cancelBtn=new JButton(Lang.gs("cancel"),Icons.CANCEL);
26     
27     JWaitDialog wait;
28     
29     private String JavaDoc ID;
30     
31     final BtnClick click=new BtnClick();
32     
33     public MyProfileWindow(){
34         super(WhisperIM.MainWindow,Lang.gs("menu_profile"),false);
35         this.ID=StringUtils.parseBareAddress(WhisperIM.MainWindow.Conn.getUser());
36         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
37         
38         Container cp=getContentPane();
39         cp.setLayout(new BorderLayout());
40         
41         profilePane.setEditable(true);
42         
43         
44         saveBtn.addActionListener(click);
45         cancelBtn.addActionListener(click);
46         cancelBtn.setDefaultCapable(true);
47         saveBtn.setToolTipText(Lang.gs("my profile save_tt"));
48         saveBtn.setMnemonic(Lang.s2k("save_mn"));
49         cancelBtn.setMnemonic(Lang.s2k("cancel_mn"));
50         btnPanel.add(saveBtn);
51         btnPanel.add(cancelBtn);
52         
53         cp.add(btnPanel,BorderLayout.SOUTH);
54         cp.add(profilePane,BorderLayout.CENTER);
55         getRootPane().setDefaultButton(cancelBtn);
56         
57         pack();
58         
59         getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(WhisperIM.HELP_KEY,0),"help"); //put in help key for action help
60
getRootPane().getActionMap().put("help",new HelpAction(this,"status.html#profile"));
61         wait=new JWaitDialog(this,"talk with server");
62         addKeyListener(new KeyAction());
63         addMouseListener(new MouseAction());
64     }
65     
66     public void show(){
67         setLocationRelativeTo(WhisperIM.MainWindow);
68         if(WhisperIM.MainWindow.gotMyProfile){
69             profilePane.set(WhisperIM.MainWindow.MyProfile,true,WhisperIM.KEYPAIR.publicKey(),true);
70             super.show();
71             return;
72         }
73         else{
74             if(!WhisperIM.MainWindow.Conn.isConnected()){
75                 GUI.showWarning(this,"menu_profile","need to be connected");
76                 return;
77             }
78             super.show();
79             wait.show();
80             try{
81                 WhisperIM.MainWindow.MyProfile.setFromVCard(VCard.fetch(WhisperIM.MainWindow.Conn));
82                 WhisperIM.MainWindow.gotMyProfile=true;
83             }
84             catch (XMPPException xe){
85                 wait.dispose();
86                 GUI.showError(this,"error","error",null,xe);
87                 this.dispose();
88                 return;
89             }
90             profilePane.set(WhisperIM.MainWindow.MyProfile,true,WhisperIM.KEYPAIR.publicKey(),true);
91             wait.dispose();
92         }
93     }
94     
95     public void setLang(){
96         setTitle(Lang.gs("menu_profile"));
97         profilePane.setLang();
98         saveBtn.setText(Lang.gs("save"));
99         saveBtn.setToolTipText(Lang.gs("my profile save_tt"));
100         saveBtn.setMnemonic(Lang.s2k("save_mn"));
101         cancelBtn.setText(Lang.gs("cancel"));
102         cancelBtn.setMnemonic(Lang.s2k("cancel_mn"));
103     }
104     
105     public void doSave(){
106         if(!WhisperIM.MainWindow.Conn.isConnected()){
107             GUI.showWarning(this,"error","need to be connected");
108             return;
109         }
110         wait.show();
111         VCard me=profilePane.get().asVcard();
112         me.setJabberID(ID);
113         me.setKey(WhisperIM.KEYPAIR.publicKey());
114         try{
115             me.upload(WhisperIM.MainWindow.Conn);
116         }
117         catch (XMPPException xe){
118             wait.dispose();
119             GUI.showError(this,"menu_profile","error",null,xe);
120             return;
121         }
122         WhisperIM.MainWindow.MyProfile=profilePane.get();
123         wait.dispose();
124         GUI.showInfo(this,"menu_profile","profile saved");
125         dispose();
126     }
127     
128     final class BtnClick implements ActionListener{
129         public void actionPerformed(ActionEvent ae){
130             //get button
131
String JavaDoc b=ae.getActionCommand();
132             // call method depending on button clicked
133
if(b.equals(cancelBtn.getText())){
134                 dispose();
135                 return;
136             }
137             if(b.equals(saveBtn.getText())){
138                 doSave();
139                 return;
140             }
141         }
142     }
143     
144 }
Popular Tags