001    /*
002     * Copyright (C) 2003 Adam Olsen This program is free software; you can
003     * redistribute it and/or modify it under the terms of the GNU General Public
004     * License as published by the Free Software Foundation; either version 1, or
005     * (at your option) any later version. This program is distributed in the hope
006     * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
007     * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
008     * General Public License for more details. You should have received a copy of
009     * the GNU General Public License along with this program; if not, write to the
010     * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
011     */
012    package com.valhalla.jbother.jabber;
013    
014    import java.util.Vector;
015    
016    import org.dotuseful.ui.tree.AutomatedTreeNode;
017    
018    import com.valhalla.settings.Settings;
019    
020    public class BuddyGroup {
021    
022        private String groupName;
023    
024        private Vector buddies = new Vector();
025    
026        private AutomatedTreeNode node = null;
027    
028        public BuddyGroup(final String groupName) {
029            this.groupName = groupName;
030        }
031    
032        /**
033         * @return Returns the groupName.
034         */
035        public String getGroupName() {
036            return groupName;
037        }
038    
039        public int getOnlineCount() {
040            int count = 0;
041            //synchronized (buddies) {
042                for (int i = 0; i < buddies.size(); i++) {
043                    BuddyStatus currentBuddy = (BuddyStatus) buddies.get(i);
044                    if (currentBuddy.size() > 0)
045                        count++;
046                }
047            //}
048    
049            return count;
050        }
051    
052        public void addBuddy(BuddyStatus buddy) {
053            if (buddy == null)
054                return;
055            //synchronized (buddies) {
056                for (int i = 0; i < buddies.size(); i++) {
057                    BuddyStatus currentBuddy = (BuddyStatus) buddies.get(i);
058                    if (currentBuddy != null
059                            && buddy.getUser().equals(currentBuddy.getUser())) {
060                        return;
061                    }
062                }
063                buddies.add(buddy);
064            //}
065    
066            if (node != null)
067                node.setUserObject(this); //node.nodeChanged();
068        }
069    
070        public void removeBuddy(BuddyStatus buddy) {
071            if (buddy == null)
072                return;
073            //synchronized (buddies) {
074                for (int i = 0; i < buddies.size(); i++) {
075                    BuddyStatus currentBuddy = (BuddyStatus) buddies.get(i);
076                    if (currentBuddy != null
077                            && buddy.getUser().equals(currentBuddy.getUser())) {
078                        buddies.remove(i);
079                    }
080                }
081            //}
082            if (node != null)
083                node.setUserObject(this); //node.nodeChanged();
084    
085        }
086    
087        public void setNode(AutomatedTreeNode node) {
088            this.node = node;
089        }
090    
091        public String toString() {
092            if (!Settings.getInstance().getBoolean("showNumbersInGroups"))
093                return groupName;
094            int totalUsers = 0;
095            int onlineUsers = 0;
096    
097           // synchronized (buddies) {
098                for (int i = 0; i < buddies.size(); i++) {
099                    BuddyStatus buddy = (BuddyStatus) buddies.get(i);
100                    if (buddy.size() > 0) {
101                        onlineUsers++;
102                    }
103                    totalUsers++;
104                }
105            //}
106            return groupName + " (" + onlineUsers + "/" + totalUsers + ")";
107        }
108    }