KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > applications > faces > auctionMonitor > ChatState


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.applications.faces.auctionMonitor;
35
36 import com.icesoft.applications.faces.auctionMonitor.beans.UserBean;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 import java.util.Arrays JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Vector JavaDoc;
43 import java.util.Random JavaDoc;
44
45 /**
46  * Class used to contain application wide state information Holds a list of all
47  * users currently in the chat Can also update everyone in this list by calling
48  * reRender on each UserBean This is done in a seperate thread so processing can
49  * continue normally
50  */

51 public class ChatState {
52     public static final String JavaDoc DEFAULT_COLOR = "#000000";
53     private static final String JavaDoc[] ALL_COLORS = {"33CC33", "660033", "FF0033",
54                                                 "FF6633", "FFCC33", "FFFF33",
55                                                 "3333CC", "33CCCC", "6600CC",
56                                                 "FF00CC", "CC00CC", "00FF99",
57                                                 "99FF33", "996633", "990033",
58                                                 "999999", "CCCCCC", "000000"};
59     private static Log log = LogFactory.getLog(ChatState.class);
60     private static ChatState singleton = null;
61     private Random JavaDoc generator = new Random JavaDoc(System.currentTimeMillis());
62     private Vector JavaDoc userList = new Vector JavaDoc(0);
63     private Vector JavaDoc colorList = new Vector JavaDoc(Arrays.asList(ALL_COLORS));
64     private boolean stamp = true; // timestamps enabled on chat message log, default = true
65

66     /**
67      * Default constructor with no parameters, is private to fufill the
68      * singleton
69      */

70     private ChatState() {
71     }
72
73     public int getNumParticipants() {
74         return (userList.size());
75     }
76
77     public boolean getTimeStampEnabled() {
78         return (stamp);
79     }
80
81     public boolean toggleTimeStamp() {
82         stamp = !stamp;
83         return (stamp);
84     }
85
86     /**
87      * Method to return a singleton instance of this class
88      *
89      * @return this ChatState object
90      */

91     public static synchronized ChatState getInstance() {
92         if (singleton == null) {
93             singleton = new ChatState();
94         }
95         return (singleton);
96     }
97
98     /**
99      * Method to add the passed UserBean to the current user list
100      *
101      * @param child UserBean to add
102      */

103     public void addUserChild(UserBean child) {
104         // Give the user a color, then add them to the list
105
child.setColor(generateColorCode());
106         userList.add(child);
107     }
108
109     /**
110      * Method to remove the passed UserBean from the current user list
111      *
112      * @param child UserBean to remove
113      * @return boolean true if the removal succeeded
114      */

115     public boolean removeUserChild(UserBean child) {
116         try{
117             // Make the user's color available again
118
colorList.add(child.getColor());
119             
120             int index = 0;
121             Iterator JavaDoc users = userList.iterator();
122             UserBean current;
123             while (users.hasNext()) {
124                 current = (UserBean) users.next();
125     
126                 // Ensure the current object is not null
127
// Otherwise casting it will cause an exception
128
if (current != null) {
129                     // Check if the current object equals the child to remove
130
if (current.equals(child)) {
131                         userList.remove(index);
132                         return (true);
133                     }
134                 } else {
135                     userList.remove(index);
136                 }
137     
138                 index++;
139             }
140         }catch (Exception JavaDoc failedRemove) {
141             failedRemove.printStackTrace();
142         }
143
144         return (false);
145     }
146
147     /**
148      * Method to loop through all children UserBeans and force each one to
149      * update Each UserBean is extracted, cast, and then has reRender() called
150      * This is done inside a seperate thread because otherwise there is a
151      * possibility of deadlock with the routine clock render calls in auction
152      * monitor Doing this may add more overhead, but much more stability
153      *
154      * @return boolean true if the update thread was executed
155      */

156     public boolean updateAll() {
157         Thread JavaDoc updater = new Thread JavaDoc(new Runnable JavaDoc() {
158             public void run() {
159                 try {
160                     Iterator JavaDoc users = userList.iterator();
161                     UserBean current;
162                     while (users.hasNext()) {
163                         current = (UserBean) users.next();
164
165                         if (current != null) {
166                             Thread.currentThread().setContextClassLoader(
167                                     current.getClass().getClassLoader());
168                             current.reRender();
169                         } else {
170                             users.remove();
171                         }
172                     }
173                 } catch (Exception JavaDoc e) {
174                     if (log.isErrorEnabled()) {
175                         log.error(
176                                 "Updating all users from the ChatState failed because of " +
177                                 e);
178                     }
179                 }
180             }
181         });
182         updater.start();
183
184         return (true);
185     }
186     
187     /**
188      * Method to randomly select an HTML color code from a preset list
189      * eg: #C62FD5
190      *
191      * @return String hex value (to be used directly in HTML tags)
192      */

193     private String JavaDoc generateColorCode() {
194         return "#" + colorList.remove(generator.nextInt(colorList.size())).toString();
195     }
196 }
197
Popular Tags