1 /** 2 * Jin - a chess client for internet chess servers. 3 * More information is available at http://www.jinchess.com/. 4 * Copyright (C) 2002 Alexander Maryanovsky. 5 * All rights reserved. 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 2 10 * of the License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 20 */ 21 22 package free.jin.event; 23 24 import free.jin.event.JinEvent; 25 import free.jin.Connection; 26 27 28 /** 29 * The event that is sent to ChatListeners when a chat related message 30 * arrives from the server. The ChatEvent contains the type of the message, 31 * the player who sent it, his title, the message itself and optionally 32 * the forum on which it was sent (channel, game). For one-on-one (personal) 33 * tells or tells that go to everyone the forum is null. <br> 34 * 35 * Note that this class isn't aware of the value of the tell type, it's a server 36 * specific string which should be dealt by a server specific class.<br> 37 * 38 * The source of the event is the Connection it came from. 39 */ 40 41 public class ChatEvent extends JinEvent{ 42 43 44 45 /** 46 * The constant for chat which doesn't fit into one of the other categories. 47 */ 48 49 public static final int OTHER_CHAT_CATEGORY = -1; 50 51 52 53 /** 54 * The constant for person-to-person chat. 55 */ 56 57 public static final int PERSON_TO_PERSON_CHAT_CATEGORY = 0; 58 59 60 61 /** 62 * The constant for in-game chat. 63 */ 64 65 public static final int GAME_CHAT_CATEGORY = 1; 66 67 68 69 /** 70 * The constant for chat which is sent to all (or almost all) players. 71 */ 72 73 public static final int BROADCAST_CHAT_CATEGORY = 2; 74 75 76 77 /** 78 * The constant for chat belonging to a certain "room" or "channel" which 79 * players can choose to be in. 80 */ 81 82 public static final int ROOM_CHAT_CATEGORY = 3; 83 84 85 86 /** 87 * The constant for chat associated with a certain tournament. 88 */ 89 90 public static final int TOURNEY_CHAT_CATEGORY = 4; 91 92 93 94 /** 95 * The type of the ChatEvent. This is something server specific and thus 96 * should only be handled by server specific classes. 97 */ 98 99 private final String type; 100 101 102 103 /** 104 * The category of this chat event. 105 */ 106 107 private final int category; 108 109 110 111 /** 112 * The name/handle of the player who sent the message. 113 */ 114 115 private final String sender; 116 117 118 119 /** 120 * The title of the player who sent the string, must be a non-null 121 * string. 122 */ 123 124 private final String senderTitle; 125 126 127 128 /** 129 * The rating of the player who sent the string, -1 if unknown. 130 */ 131 132 private final int senderRating; 133 134 135 136 /** 137 * The message itself. Must be a non-null string. 138 */ 139 140 private final String message; 141 142 143 144 /** 145 * The forum on which the message was sent. Only applies to 146 * messages that aren't one-on-one or go to everyone. For kibitzes 147 * and whispers this is an Integer specifying the game number for example, 148 * for channel tells, the channel number. 149 */ 150 151 private final Object forum; 152 153 154 155 /** 156 * Creates a new ChatEvent with the given type, category, sender, sender 157 * titles, sender rating (-1 if unknown), message and forum. Note that the 158 * list of possible chat categories is not final (and will never be such). 159 * If your chat type belongs to a category which does not exist yet, contact 160 * the person responsible for the code and ask him to add a new category. In 161 * the meanwhile (or if you are happy with it), use 162 * <code>UNCATEGORIZED_CHAT</code> 163 */ 164 ChatEvent(Connection conn, String type, int category, String sender, String senderTitle, int senderRating, String message, Object forum)165 public ChatEvent(Connection conn, String type, int category, String sender, String senderTitle, 166 int senderRating, String message, Object forum){ 167 super(conn); 168 169 if (type == null) 170 throw new IllegalArgumentException("ChatEvent type may not be null"); 171 172 switch (category){ 173 case OTHER_CHAT_CATEGORY: 174 case PERSON_TO_PERSON_CHAT_CATEGORY: 175 case GAME_CHAT_CATEGORY: 176 case BROADCAST_CHAT_CATEGORY: 177 case ROOM_CHAT_CATEGORY: 178 case TOURNEY_CHAT_CATEGORY: 179 break; 180 default: 181 throw new IllegalArgumentException("Unknown chat category value: " + category); 182 } 183 184 this.type = type; 185 this.category = category; 186 this.sender = sender; 187 this.senderTitle = senderTitle; 188 this.senderRating = senderRating; 189 this.message = message; 190 this.forum = forum; 191 } 192 193 194 195 /** 196 * Returns the type of this message. This is a server specific string and 197 * should be dealt by a server specific class. 198 */ 199 getType()200 public String getType(){ 201 return type; 202 } 203 204 205 206 /** 207 * Returns the category of the chat. Note that the list of possible categories 208 * is not final (and will never be), so don't make your code assume that the 209 * category is one of the current categories. 210 */ 211 getCategory()212 public int getCategory(){ 213 return category; 214 } 215 216 217 218 /** 219 * Returns the name/handle of the player who sent the message. 220 */ 221 getSender()222 public String getSender(){ 223 return sender; 224 } 225 226 227 228 /** 229 * Returns the title of the player who sent the message. 230 */ 231 getSenderTitle()232 public String getSenderTitle(){ 233 return senderTitle; 234 } 235 236 237 238 /** 239 * Returns the rating of the sender, or -1 if unknown. 240 */ 241 getSenderRating()242 public int getSenderRating(){ 243 return senderRating; 244 } 245 246 247 248 /** 249 * Returns the message itself. 250 */ 251 getMessage()252 public String getMessage(){ 253 return message; 254 } 255 256 257 258 /** 259 * Returns the forum on which the message was sent. The forum identifies a 260 * certain instance of a chat type. For room/channel tells this is a 261 * <code>String/Integer</code> specifying the room/channel name/number. 262 * For kibitzes and whispers, the game number. For chat types with only a 263 * single instance (such as shouts, announcements) or where the instance is 264 * already identified by the sender (personal tells) this is 265 * <code>null</code>. This value is somewhat server specific, it should 266 * probably be handled by server specific code. 267 */ 268 getForum()269 public Object getForum(){ 270 return forum; 271 } 272 273 274 275 /** 276 * Returns a textual representation of this ChatEvent. 277 */ 278 toString()279 public String toString(){ 280 return getClass().getName()+"[Sender="+getSender()+";Title="+getSenderTitle()+";Forum="+getForum()+";Message="+getMessage()+"]"; 281 } 282 283 284 285 } 286