001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.chat.command;
019:
020: import java.util.Iterator;
021:
022: import javax.swing.tree.DefaultMutableTreeNode;
023:
024: import org.columba.api.command.ICommandReference;
025: import org.columba.api.command.IWorkerStatusController;
026: import org.columba.chat.Connection;
027: import org.columba.chat.model.BuddyList;
028: import org.columba.chat.model.BuddyStatus;
029: import org.columba.chat.ui.frame.api.IChatFrameMediator;
030: import org.columba.core.command.Command;
031: import org.jivesoftware.smack.Roster;
032: import org.jivesoftware.smack.RosterEntry;
033: import org.jivesoftware.smack.RosterGroup;
034: import org.jivesoftware.smack.packet.Presence;
035:
036: public class PopulateRoasterCommand extends Command {
037:
038: private IChatFrameMediator mediator;
039:
040: private DefaultMutableTreeNode root;
041:
042: private DefaultMutableTreeNode uncategorizedNode;
043:
044: public PopulateRoasterCommand(IChatFrameMediator mediator,
045: ICommandReference reference) {
046: super (reference);
047:
048: this .mediator = mediator;
049: }
050:
051: /**
052: * @see org.columba.core.command.Command#updateGUI()
053: */
054: @Override
055: public void updateGUI() throws Exception {
056: mediator.getRoasterTree().populate(root);
057: }
058:
059: @Override
060: public void execute(IWorkerStatusController worker)
061: throws Exception {
062: root = new DefaultMutableTreeNode("Roster");
063: uncategorizedNode = new DefaultMutableTreeNode("Uncategorized");
064:
065: Roster roster = Connection.XMPPConnection.getRoster();
066:
067: // add all groups as folder to JTree
068: Iterator it = roster.getGroups();
069: while (it.hasNext()) {
070:
071: RosterGroup group = (RosterGroup) it.next();
072: DefaultMutableTreeNode child = new DefaultMutableTreeNode(
073: group);
074:
075: root.add(child);
076: }
077:
078: // add "Uncategorized" note
079: root.add(uncategorizedNode);
080:
081: // add all contacts as leafs of group folders
082: it = roster.getEntries();
083: while (it.hasNext()) {
084: RosterEntry entry = (RosterEntry) it.next();
085:
086: // add to global buddy list
087: BuddyStatus buddy;
088: if (BuddyList.getInstance().exists(entry.getUser())) {
089: // buddy already exists
090: buddy = BuddyList.getInstance().getBuddy(
091: entry.getUser());
092:
093: } else {
094: // create new buddy
095: buddy = new BuddyStatus(entry.getUser());
096: buddy.setName(entry.getName());
097: // and add it to the buddylist
098: BuddyList.getInstance().add(entry.getUser(), buddy);
099: }
100:
101: // get presence
102: Presence p = roster.getPresence(entry.getUser());
103: if (p != null) {
104: // update status information
105:
106: buddy.setPresenceMode(p.getMode());
107: buddy.setStatusMessage(p.getStatus());
108: }
109:
110: // check if this buddy belongs to a group
111: Iterator groups = entry.getGroups();
112: boolean notAdded = true;
113: while (groups.hasNext()) {
114: RosterGroup group = (RosterGroup) groups.next();
115:
116: DefaultMutableTreeNode parent = findGroup(root, group);
117:
118: if (parent != null) {
119: // found group for buddy
120: parent.add(new DefaultMutableTreeNode(buddy));
121: notAdded = false;
122: }
123: }
124:
125: // didn't find any group for this buddy
126: if (notAdded == true)
127: // add to "Uncategorized" node
128: uncategorizedNode
129: .add(new DefaultMutableTreeNode(buddy));
130:
131: }
132:
133: }
134:
135: private DefaultMutableTreeNode findGroup(
136: DefaultMutableTreeNode parent, RosterGroup group) {
137: for (int i = 0; i < parent.getChildCount(); i++) {
138: DefaultMutableTreeNode child = (DefaultMutableTreeNode) parent
139: .getChildAt(i);
140:
141: if (group.equals(child.getUserObject()))
142: return child;
143:
144: }
145:
146: return null;
147: }
148:
149: }
|