22 package com.realtime.crossfire.jxclient.gui.log;
24 import java.awt.Color;
25 import java.util.HashMap;
27 import java.util.Map.Entry;
28 import java.util.regex.Pattern;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
43 private static final Map<String, FontID>
FONTS =
new HashMap<>();
57 private static final Map<String, Color>
COLORS =
new HashMap<>();
60 COLORS.put(
"black", Color.BLACK);
61 COLORS.put(
"blue", Color.BLUE);
62 COLORS.put(
"green", Color.GREEN);
63 COLORS.put(
"red", Color.RED);
64 COLORS.put(
"white", Color.WHITE);
111 public void parse(@NotNull
final CharSequence text, @Nullable
final Color defaultColor, @NotNull
final Buffer buffer) {
113 for (
final String line : END_OF_LINE_PATTERN.split(text, -1)) {
126 if (text.length() == 0) {
131 for (
final String line : END_OF_LINE_PATTERN.split(text, -1)) {
143 private void parseLine(@NotNull
final String text, @Nullable
final Color defaultColor, @NotNull
final Buffer buffer) {
144 if (buffer.mergeLines(text, defaultColor)) {
145 buffer.replaceLine(
parseLine(text+
" [["+buffer.getLastCount()+
" times]", defaultColor));
147 buffer.addLine(
parseLine(text, defaultColor));
158 private Line parseLine(@NotNull
final String text, @Nullable
final Color defaultColor) {
162 boolean active =
false;
163 final int iMax = text.length();
164 for (
int i = 0; i < iMax; i++) {
165 final char ch = text.charAt(i);
168 processTag(text.substring(begin, i), defaultColor);
171 }
else if (ch ==
'[' && i == begin) {
198 if (buffer.mergeLines(text, null)) {
199 processText(text+
" ["+buffer.getLastCount()+
" times]", line);
200 buffer.replaceLine(line);
203 buffer.addLine(line);
216 color = defaultColor;
225 private void processTag(@NotNull
final String tag, @Nullable
final Color defaultColor) {
230 if (tag.equals(
"b")) {
232 }
else if (tag.equals(
"/b")) {
234 }
else if (tag.equals(
"i")) {
236 }
else if (tag.equals(
"/i")) {
238 }
else if (tag.equals(
"ul")) {
240 }
else if (tag.equals(
"/ul")) {
242 }
else if (FONTS.containsKey(tag)) {
243 font = FONTS.get(tag);
245 }
else if (tag.startsWith(
"color=")) {
246 final String colorName = tag.substring(6).toLowerCase();
247 if (COLORS.containsKey(colorName)) {
248 color = COLORS.get(colorName);
249 assert color != null;
250 }
else if (colorName.startsWith(
"#") && colorName.length() == 7) {
252 color = Color.decode(colorName);
253 }
catch (
final NumberFormatException ignored) {
258 }
else if (tag.equals(
"/color")) {
259 color = defaultColor;
271 if (text.isEmpty()) {
275 final CharSequence newText;
276 final Segment prevSegment = line.getLastSegment();
278 final TextSegment prevTextSegment = (TextSegment)prevSegment;
279 if (prevTextSegment.
matches(bold, italic, underline, font, color)) {
280 newText = prevTextSegment.
getText()+text;
281 line.removeLastSegment();
289 final String[] words = WORD_SEPARATOR_PATTERN.split(newText, -1);
290 for (
int i = 0; i < words.length-1; i++) {
291 line.addSegment(words[i]+
" ", bold, italic, underline, font, color);
293 if (!words[words.length-1].isEmpty()) {
294 line.addSegment(words[words.length-1], bold, italic, underline, font, color);
304 public static String
toString(@NotNull
final Color color) {
307 for (
final Entry<String, Color> e : COLORS.entrySet()) {
308 if (e.getValue() ==
color) {
313 return color.toString();
String getText()
Returns the text to display.
Manages the contents of one text line.
Parser for parsing drawextinfo messages received from a Crossfire server to update a Buffer instance...
static final Pattern END_OF_LINE_PATTERN
Pattern to match line breaks.
boolean bold
Whether bold face is enabled.
Color color
The color to use.
STRANGE
The font used by the [strange] tag.
boolean italic
Whether italic face is enabled.
void processTag(@NotNull final String tag, @Nullable final Color defaultColor)
Processes a tag.
void parseLineWithoutMediaTags(@NotNull final String text, @NotNull final Buffer buffer)
Parses one text line of a plain text message without media tags.
One segment of a Line which should be displayed without changing text attributes. ...
One segment of a Line which should be displayed without changing attributes.
void parseLine(@NotNull final String text, @Nullable final Color defaultColor, @NotNull final Buffer buffer)
Parses one text line.
void parseWithoutMediaTags(@NotNull final CharSequence text, @NotNull final Color color, @NotNull final Buffer buffer)
Parses a plain text message without media tags.
FIXED
The font used by the [fixed] tag.
static String toString(@NotNull final Color color)
Returns the string representation for a color.
Manages the contents of the contents of a log window.
void processText(@NotNull final String text, @NotNull final Line line)
Processes one text segment.
static final Pattern WORD_SEPARATOR_PATTERN
The pattern to split a string into words.
void resetAttributes(@Nullable final Color defaultColor)
Resets all attributes to default values.
static final Map< String, Color > COLORS
Maps color tag name to color instance.
void parse(@NotNull final CharSequence text, @Nullable final Color defaultColor, @NotNull final Buffer buffer)
Parses a text message.
HAND
The font used by the [hand] tag.
ARCANE
The font used by the [arcane] tag.
boolean matches(final boolean bold, final boolean italic, final boolean underline, @NotNull final FontID font, @Nullable final Color color)
Returns whether this segment matches the given attributes.
boolean underline
Whether underlining is enabled.
static final Map< String, FontID > FONTS
Maps font tag name to font instance.
FontID font
The font to use.
Line parseLine(@NotNull final String text, @Nullable final Color defaultColor)
Parses one text line.