KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > util > GroupUserList


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Created on 27.02.2004
19  */

20
21 package freecs.util;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import freecs.Server;
30 import freecs.content.MessageState;
31 import freecs.core.Group;
32 import freecs.core.User;
33
34 /**
35  * @author Manfred Andres
36  *
37  * freecs.util
38  */

39 public class GroupUserList {
40     HashMap JavaDoc ht = new HashMap JavaDoc();
41     public GroupUserList () {
42         if (Server.TRACE_CREATE_AND_FINALIZE)
43             Server.log (this, "++++++++++++++++++++++++++++++++++++++++CREATE", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
44     }
45     
46     public void addUser (User u) {
47         Group g = u.getGroup();
48         Vector JavaDoc v = (Vector JavaDoc) ht.get(g);
49         if (v == null)
50             v = new Vector JavaDoc();
51         v.addElement(u);
52         ht.put(g,v);
53     }
54     
55     public Iterator JavaDoc iterator () {
56         return ht.values().iterator();
57     }
58     
59     public void sendMessage (MessageState ms, String JavaDoc tpl, boolean target) {
60         String JavaDoc singular = tpl + ".singular";
61         String JavaDoc plural = tpl + ".plural";
62         ms.useRenderCache=false;
63         List JavaDoc l = new ArrayList JavaDoc();
64         for (int i = 0; i < ms.usrList.length; i++)
65             l.add(ms.usrList[i]);
66         l.add(ms.sender);
67         for (Iterator JavaDoc i = ht.keySet().iterator(); i.hasNext(); ) {
68             Group g = (Group) i.next();
69             Vector JavaDoc v = (Vector JavaDoc) ht.get(g);
70             if (v == null || v.size()==0)
71                 continue;
72             if (v.size()==1) {
73                 ms.msgTemplate=singular;
74                 ms.usercontext = (User) v.get(0);
75             } else if (v.size() > 1) {
76                 ms.usrList = v.toArray();
77                 ms.msgTemplate=plural;
78             }
79             if (target) {
80                 ms.targetGroup = g;
81                 g.exclusiveSendMessage(ms.mp, l);
82             } else {
83                 ms.sourceGroup = g;
84                 g.exclusiveSendMessage(ms.mp, l);
85             }
86         }
87    }
88
89     public void finalize() {
90         if (Server.TRACE_CREATE_AND_FINALIZE)
91             Server.log(this, "----------------------------------------FINALIZED", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
92     }
93 }
Popular Tags