1 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 ; 41 import java.util.Iterator ; 42 import java.util.Vector ; 43 import java.util.Random ; 44 45 51 public class ChatState { 52 public static final String DEFAULT_COLOR = "#000000"; 53 private static final String [] 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 generator = new Random (System.currentTimeMillis()); 62 private Vector userList = new Vector (0); 63 private Vector colorList = new Vector (Arrays.asList(ALL_COLORS)); 64 private boolean stamp = true; 66 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 91 public static synchronized ChatState getInstance() { 92 if (singleton == null) { 93 singleton = new ChatState(); 94 } 95 return (singleton); 96 } 97 98 103 public void addUserChild(UserBean child) { 104 child.setColor(generateColorCode()); 106 userList.add(child); 107 } 108 109 115 public boolean removeUserChild(UserBean child) { 116 try{ 117 colorList.add(child.getColor()); 119 120 int index = 0; 121 Iterator users = userList.iterator(); 122 UserBean current; 123 while (users.hasNext()) { 124 current = (UserBean) users.next(); 125 126 if (current != null) { 129 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 failedRemove) { 141 failedRemove.printStackTrace(); 142 } 143 144 return (false); 145 } 146 147 156 public boolean updateAll() { 157 Thread updater = new Thread (new Runnable () { 158 public void run() { 159 try { 160 Iterator 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 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 193 private String generateColorCode() { 194 return "#" + colorList.remove(generator.nextInt(colorList.size())).toString(); 195 } 196 } 197 | Popular Tags |