KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ContactListCellRenderer


1 import java.awt.*;
2 import javax.swing.*;
3 import javax.swing.tree.*;
4 import org.jivesoftware.smack.packet.*;
5
6 public final class ContactListCellRenderer extends DefaultTreeCellRenderer{
7
8
9      // This is the only method defined by ListCellRenderer.
10
// We just reconfigure the JLabel each time we're called.
11

12      public Component getTreeCellRendererComponent(JTree tree,Object JavaDoc value,boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus) {
13     super.getTreeCellRendererComponent(tree, value, sel,expanded, leaf, row, hasFocus);
14     DefaultMutableTreeNode node =(DefaultMutableTreeNode)value;
15     ContactListItem item=(ContactListItem)(node.getUserObject());
16     int type=item.getType();
17     if(type==-1){ //Its the root
18
setText(Lang.gs("contacts"));
19         setToolTipText(Lang.gs("contacts"));
20         setIcon(Icons.CONTACTS);
21         return this;
22     }
23     setText(item.toString());
24     if(type==ContactListItem.GROUP){
25         setToolTipText(item.toString());
26         return this;
27     }
28     Presence p=item.getPresence();
29     if(type==ContactListItem.SERVICE){
30         setToolTipText("<html>"+item.getID()+"<br>"+presenceToString(p)+"</html>");
31         if(p==null){
32             setIcon(DISCOWindow.SERVICE_OFFLINE_ICON);
33             return this;
34         }
35         String JavaDoc sm=p.getMode().toString().trim().toLowerCase();
36         if(sm.equals("available") || sm.equals("chat")){
37             setIcon(DISCOWindow.SERVICE_ICON);
38         }
39         else{
40             setIcon(DISCOWindow.SERVICE_OFFLINE_ICON);
41         }
42         return this;
43     }
44     // its user object
45
String JavaDoc status="<html>"+item.getID()+"<br>";
46     if(p==null){
47         String JavaDoc sub=item.getSubscription().toString();
48         if(sub.equals(RosterPacket.ItemType.NONE.toString()) || sub.equals(RosterPacket.ItemType.FROM)){
49             setToolTipText(status+Lang.gs("status_unknown")+"</html>");
50             setIcon(Icons.STATUS_UNKNOWN);
51             return this;
52         }
53         else{
54             setToolTipText(status+Lang.gs("status_offline")+"</html>");
55             setIcon(Icons.STATUS_OFFLINE);
56             return this;
57         }
58     }
59     else{
60         String JavaDoc m=p.getMode().toString().trim().toLowerCase();
61         if(m.equals("available")){
62             status=status+Lang.gs("status_online");
63             setIcon(Icons.STATUS_ONLINE);
64         }
65         if(m.equals("chat")){
66             status=status+Lang.gs("status_available");
67             setIcon(Icons.STATUS_AVAILABLE);
68         }
69         if(m.equals("away")){status=status+Lang.gs("status_away");
70             setIcon(Icons.STATUS_AWAY);
71         }
72         if(m.equals("xa")){
73             status=status+Lang.gs("status_xa");
74             setIcon(Icons.STATUS_XA);
75         }
76         if(m.equals("dnd")){
77             status=status+Lang.gs("status_dnd");
78             setIcon(Icons.STATUS_DND);
79         }
80         if(m.equals("invisible")){
81             status=status+Lang.gs("status_invisible");
82             setIcon(Icons.STATUS_INVISIBLE);
83         }
84         if(p.getStatus()==null){
85             setToolTipText(status+"</html>");
86             return this;
87         }
88         setToolTipText(status+"<br>"+p.getStatus()+"</html>");
89     }
90     return this;
91      }
92      
93      
94     public static String JavaDoc presenceToString(Presence p){
95         if(p==null){
96             return Lang.gs("status_offline");
97         }
98         String JavaDoc m=p.getMode().toString().trim().toLowerCase();
99         if(m.equals("available")){
100             return Lang.gs("status_online");
101         }
102         if(m.equals("chat")){
103             return Lang.gs("status_available");
104         }
105         if(m.equals("away")){
106             return Lang.gs("status_away");
107         }
108         if(m.equals("xa")){
109             return Lang.gs("status_xa");
110
111         }
112         if(m.equals("dnd")){
113             return Lang.gs("status_dnd");
114
115         }
116         if(m.equals("invisible")){
117             return Lang.gs("status_invisible");
118         }
119         return "";
120     }
121         
122  }
Popular Tags