Crossfire JXClient, Trunk  R20561
MessageBufferUpdater.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient 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 JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.log;
23 
29 import java.awt.Color;
30 import org.jetbrains.annotations.NotNull;
31 
36 public class MessageBufferUpdater {
37 
41  public static final int NUM_COLORS = 13;
42 
46  @NotNull
47  private final Color[] colors = {
48  // black
49  new Color(0x000000),
50  // white
51  new Color(0x860F9F),
52  // navy blue
53  new Color(0x362589),
54  // red
55  new Color(0xD80C1B),
56  // orange
57  new Color(0xAF0093),
58  // dodger blue
59  new Color(0x2E00B7),
60  // dark orange
61  new Color(0xCF4507),
62  // sea green
63  new Color(0x275A0F),
64  // dark sea green
65  new Color(0x346B1A),
66  // grey
67  new Color(0x353535),
68  // brown sienna
69  new Color(0x775716),
70  // gold
71  new Color(0x874200),
72  // khaki
73  new Color(0x6C6E20),
74  };
75 
79  @NotNull
80  private static final String[] COLOR_NAMES = {
81  "black",
82  "white",
83  "navy blue",
84  "red",
85  "orange",
86  "dodger blue",
87  "dark orange",
88  "sea green",
89  "dark sea green",
90  "grey",
91  "brown sienna",
92  "gold",
93  "khaki",
94  };
95 
99  @NotNull
101 
105  @NotNull
106  private final Parser parser = new Parser();
107 
111  @NotNull
112  private final Color defaultColor;
113 
117  @NotNull
118  private final Buffer buffer;
119 
123  private int types = ~0;
124 
128  private boolean printMessageTypes;
129 
133  @NotNull
135 
136  @Override
137  public void commandQueryReceived(@NotNull final String prompt, final int queryType) {
140  }
141  }
142 
143  };
144 
149  @NotNull
151 
152  @Override
153  public void commandDrawextinfoReceived(final int color, final int type, final int subtype, @NotNull final String message) {
154  if (type == MessageTypes.MSG_TYPE_QUERY // should not happen; but if it happens just display it
155  || isTypeShown(type)) {
156  final CharSequence messageWithPrefix = addMessageTypePrefix(type, subtype, color, message);
157  if (type == MessageTypes.MSG_TYPE_COMMUNICATION) {
158  parser.parseWithoutMediaTags(messageWithPrefix, findColor(color), buffer);
159  } else {
160  parser.parse(messageWithPrefix, findColor(color), buffer);
161  }
162  }
163  }
164 
165  @Override
166  public void setDebugMode(final boolean printMessageTypes) {
168  }
169 
170  };
171 
176  @NotNull
178 
179  @Override
180  public void commandDrawinfoReceived(@NotNull final String text, final int type) {
181  // guess category from message color
182  final int messageType;
183  switch (type) {
184  case NDI_WHITE:
185  case NDI_ORANGE:
186  case NDI_BLUE:
187  case NDI_RED:
188  messageType = MessageTypes.MSG_TYPE_COMMUNICATION;
189  break;
190 
191  default:
192  messageType = MessageTypes.MSG_TYPE_MISC;
193  break;
194  }
195 
196  if (isTypeShown(messageType)) {
197  parser.parseWithoutMediaTags(addMessageTypePrefix(messageType, 0, type, text), findColor(type), buffer);
198  }
199  }
200 
201  };
202 
210  public MessageBufferUpdater(@NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final Buffer buffer, @NotNull final Color defaultColor) {
211  this.crossfireServerConnection = crossfireServerConnection;
212  this.buffer = buffer;
213  this.defaultColor = defaultColor;
214  this.crossfireServerConnection.addCrossfireQueryListener(crossfireQueryListener);
215  this.crossfireServerConnection.addCrossfireDrawextinfoListener(crossfireDrawextinfoListener);
216  this.crossfireServerConnection.addCrossfireDrawinfoListener(crossfireDrawinfoListener);
217  }
218 
222  public void dispose() {
223  crossfireServerConnection.removeCrossfireQueryListener(crossfireQueryListener);
224  crossfireServerConnection.removeCrossfireDrawextinfoListener(crossfireDrawextinfoListener);
225  crossfireServerConnection.removeCrossfireDrawinfoListener(crossfireDrawinfoListener);
226  }
227 
233  @NotNull
234  private Color findColor(final int index) {
235  try {
236  return colors[index];
237  } catch (final ArrayIndexOutOfBoundsException ignored) {
238  return defaultColor;
239  }
240  }
241 
247  @NotNull
248  public static String getColorName(final int index) {
249  try {
250  return COLOR_NAMES[index];
251  } catch (final ArrayIndexOutOfBoundsException ignored) {
252  return "undefined";
253  }
254  }
255 
261  public void setColor(final int index, @NotNull final Color color) {
262  colors[index] = color;
263  }
264 
269  public void setTypes(final int types) {
270  this.types = types;
271  }
272 
278  private boolean isTypeShown(final int type) {
279  return type < 0 || type > 31 || (types&(1<<type)) != 0;
280  }
281 
291  private CharSequence addMessageTypePrefix(final int type, final int subtype, final int color, @NotNull final CharSequence message) {
292  return printMessageTypes ? "(t="+MessageTypes.toString(type)+"/"+subtype+",c="+color+")"+message : message;
293  }
294 
295 }
boolean isTypeShown(final int type)
Returns whether a message type should be shown.
static final int NUM_COLORS
The number of supported colors.
final Color defaultColor
The color to use for invalid colors indices.
Encapsulates the message type numbers for drawextinfo messages.
boolean printMessageTypes
Whether message types are included in the buffer output.
Parser for parsing drawextinfo messages received from a Crossfire server to update a Buffer instance...
Definition: Parser.java:37
void setTypes(final int types)
Sets the message types to show.
Interface for listeners interested in drawinfo messages received from the Crossfire server...
static final int MSG_TYPE_MISC
drawextinfo message type: message that does not fit in any other category.
static String getColorName(final int index)
Returns the name of a color index.
CharSequence addMessageTypePrefix(final int type, final int subtype, final int color, @NotNull final CharSequence message)
Adds a message type prefix to a message if printMessageTypes is set.
final Parser parser
The Parser instance for parsing drawextinfo messages.
Color findColor(final int index)
Converts a Crossfire color index to a Color instance.
final CrossfireDrawinfoListener crossfireDrawinfoListener
The CrossfireDrawinfoListener registered to receive drawinfo commands.
void setColor(final int index, @NotNull final Color color)
Sets a color mapping.
final CrossfireQueryListener crossfireQueryListener
The CrossfireQueryListener registered to receive query commands.
static final String [] COLOR_NAMES
The colors names corresponding to colors.
void parseWithoutMediaTags(@NotNull final CharSequence text, @NotNull final Color color, @NotNull final Buffer buffer)
Parses a plain text message without media tags.
Definition: Parser.java:125
MessageBufferUpdater(@NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final Buffer buffer, @NotNull final Color defaultColor)
Creates a new instance.
Interface for listeners interested in drawextinfo messages received from the Crossfire server...
Manages the contents of the contents of a log window.
Definition: Buffer.java:41
static final int MSG_TYPE_COMMUNICATION
drawextinfo message type: communication between players.
static final int MSG_TYPE_QUERY
Internally used drawextinfo message type: a query message has been received.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
void parse(@NotNull final CharSequence text, @Nullable final Color defaultColor, @NotNull final Buffer buffer)
Parses a text message.
Definition: Parser.java:111
void removeCrossfireDrawinfoListener(@NotNull CrossfireDrawinfoListener listener)
Removes the given listener from the list of objects listening to the drawinfo S->C messages...
final CrossfireDrawextinfoListener crossfireDrawextinfoListener
The CrossfireDrawextinfoListener registered to receive drawextinfo commands.
final CrossfireServerConnection crossfireServerConnection
The CrossfireServerConnection to monitor.
Interface for listeners interested in query messages received from the Crossfire server.
Adds drawinfo, drawextinfo, and query messages to a Buffer instance.
void removeCrossfireQueryListener(@NotNull CrossfireQueryListener listener)
Removes the given listener from the list of objects listening to the query S->C messages.
static String toString(final int type)
Returns a string representation of a message type.
void removeCrossfireDrawextinfoListener(@NotNull CrossfireDrawextinfoListener listener)
Removes the given listener from the list of objects listening to the drawextinfo S->C messages...