KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > chat > command > PopulateRoasterCommand


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.chat.command;
19
20 import java.util.Iterator JavaDoc;
21
22 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
23
24 import org.columba.api.command.ICommandReference;
25 import org.columba.api.command.IWorkerStatusController;
26 import org.columba.chat.Connection;
27 import org.columba.chat.model.BuddyList;
28 import org.columba.chat.model.BuddyStatus;
29 import org.columba.chat.ui.frame.api.IChatFrameMediator;
30 import org.columba.core.command.Command;
31 import org.jivesoftware.smack.Roster;
32 import org.jivesoftware.smack.RosterEntry;
33 import org.jivesoftware.smack.RosterGroup;
34 import org.jivesoftware.smack.packet.Presence;
35
36 public class PopulateRoasterCommand extends Command {
37
38     private IChatFrameMediator mediator;
39
40     private DefaultMutableTreeNode JavaDoc root;
41
42     private DefaultMutableTreeNode JavaDoc uncategorizedNode;
43
44     public PopulateRoasterCommand(IChatFrameMediator mediator,
45             ICommandReference reference) {
46         super(reference);
47
48         this.mediator = mediator;
49     }
50
51     /**
52      * @see org.columba.core.command.Command#updateGUI()
53      */

54     @Override JavaDoc
55     public void updateGUI() throws Exception JavaDoc {
56         mediator.getRoasterTree().populate(root);
57     }
58
59     @Override JavaDoc
60     public void execute(IWorkerStatusController worker) throws Exception JavaDoc {
61         root = new DefaultMutableTreeNode JavaDoc("Roster");
62         uncategorizedNode = new DefaultMutableTreeNode JavaDoc("Uncategorized");
63
64         Roster roster = Connection.XMPPConnection.getRoster();
65
66         // add all groups as folder to JTree
67
Iterator JavaDoc it = roster.getGroups();
68         while (it.hasNext()) {
69
70             RosterGroup group = (RosterGroup) it.next();
71             DefaultMutableTreeNode JavaDoc child = new DefaultMutableTreeNode JavaDoc(group);
72
73             root.add(child);
74         }
75
76         // add "Uncategorized" note
77
root.add(uncategorizedNode);
78
79         // add all contacts as leafs of group folders
80
it = roster.getEntries();
81         while (it.hasNext()) {
82             RosterEntry entry = (RosterEntry) it.next();
83
84             // add to global buddy list
85
BuddyStatus buddy;
86             if (BuddyList.getInstance().exists(entry.getUser())) {
87                 // buddy already exists
88
buddy = BuddyList.getInstance().getBuddy(entry.getUser());
89
90             } else {
91                 // create new buddy
92
buddy = new BuddyStatus(entry.getUser());
93                 buddy.setName(entry.getName());
94                 // and add it to the buddylist
95
BuddyList.getInstance().add(entry.getUser(), buddy);
96             }
97
98             // get presence
99
Presence p = roster.getPresence(entry.getUser());
100             if (p != null) {
101                 // update status information
102

103                 buddy.setPresenceMode(p.getMode());
104                 buddy.setStatusMessage(p.getStatus());
105             }
106
107             // check if this buddy belongs to a group
108
Iterator JavaDoc groups = entry.getGroups();
109             boolean notAdded = true;
110             while (groups.hasNext()) {
111                 RosterGroup group = (RosterGroup) groups.next();
112
113                 DefaultMutableTreeNode JavaDoc parent = findGroup(root, group);
114
115                 if (parent != null) {
116                     // found group for buddy
117
parent.add(new DefaultMutableTreeNode JavaDoc(buddy));
118                     notAdded = false;
119                 }
120             }
121
122             // didn't find any group for this buddy
123
if (notAdded == true)
124                 // add to "Uncategorized" node
125
uncategorizedNode.add(new DefaultMutableTreeNode JavaDoc(buddy));
126
127         }
128
129     }
130
131     private DefaultMutableTreeNode JavaDoc findGroup(DefaultMutableTreeNode JavaDoc parent,
132             RosterGroup group) {
133         for (int i = 0; i < parent.getChildCount(); i++) {
134             DefaultMutableTreeNode JavaDoc child = (DefaultMutableTreeNode JavaDoc) parent
135                     .getChildAt(i);
136
137             if (group.equals(child.getUserObject()))
138                 return child;
139
140         }
141
142         return null;
143     }
144
145 }
146
Popular Tags