KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PresenceChangeListener


1 import org.jivesoftware.smack.*;
2 import org.jivesoftware.smack.packet.*;
3 import org.jivesoftware.smack.util.StringUtils;
4
5 /** Listener when a user on the contact list changes thier presence.*/
6 public final class PresenceChangeListener implements RosterListener{
7     
8     public void rosterModified(){
9         WhisperIM.MainWindow.contactListPanel.tree.repaint();
10     }
11     
12     public void presenceChanged(String JavaDoc XMPPAddress){
13         //get the cli
14
final String JavaDoc bareID=StringUtils.parseBareAddress(XMPPAddress);
15         final ContactListItem cli=(ContactListItem)WhisperIM.MainWindow.Contacts.get("jabber"+bareID);
16         if(cli==null){ //shouldnt happen
17
return;
18         }
19         final Presence p=WhisperIM.MainWindow.roster.getPresence(XMPPAddress);
20         cli.setPresence(p);
21         RosterEntry re=WhisperIM.MainWindow.roster.getEntry(XMPPAddress);
22         if(re!=null){
23             cli.setSubscription(re.getType());
24         }
25         // see if currently chatting with this user
26
final ChatPanel cp=(ChatPanel)WhisperIM.MainWindow.Chats.get("jabber"+bareID);
27         final String JavaDoc status;
28         if(p!=null){
29             status=p.getStatus();
30         }
31         else{
32             status="";
33         }
34         javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
35             public void run(){
36                 WhisperIM.MainWindow.statusLbl.setText(GUI.getTime()+" "+cli.toString()+" "+ContactListCellRenderer.presenceToString(p));
37                 if(cp!=null){
38                     
39                     cp.announce((cp.Name+" : "+ContactListCellRenderer.presenceToString(p)+" ("+status+")"),GUI.getTime(p));
40                 }
41             }
42         });
43         
44         if(p!=null){
45             if(p.getMode()==Presence.Mode.AVAILABLE || p.getMode()==Presence.Mode.CHAT){
46                 Alert.online();
47             }
48         }
49         javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
50             public void run(){
51                 WhisperIM.MainWindow.contactListPanel.tree.repaint();
52             }
53         });
54                         
55     }
56 }
Popular Tags