23 package com.realtime.crossfire.jxclient.gui.log;
25 import java.awt.Color;
26 import java.time.LocalDateTime;
27 import java.time.format.DateTimeFormatter;
28 import java.util.HashMap;
29 import java.util.Locale;
31 import java.util.Map.Entry;
32 import java.util.regex.Pattern;
33 import org.jetbrains.annotations.NotNull;
46 private static final DateTimeFormatter
FORMAT = DateTimeFormatter.ofPattern(
"yyyy-MM-dd HH:mm:ss ", Locale.ENGLISH);
52 @SuppressWarnings(
"StaticCollection")
53 private static final Map<String,
FontID>
FONTS = new HashMap<>();
67 @SuppressWarnings(
"StaticCollection")
68 private static final Map<String, Color>
COLORS = new HashMap<>();
71 COLORS.put(
"black", Color.BLACK);
72 COLORS.put(
"blue", Color.BLUE);
73 COLORS.put(
"green",
new Color(0, 128, 0));
74 COLORS.put(
"red", Color.RED);
75 COLORS.put(
"white", Color.WHITE);
138 public void parse(@NotNull
final CharSequence text,
final int type,
final int subtype, @NotNull
final Color defaultColor, @NotNull
final Buffer buffer) {
141 parseLine(line, type, subtype, defaultColor, buffer);
155 if (text.length() == 0) {
174 private void parseLine(@NotNull
final String text,
final int type,
final int subtype, @NotNull
final Color defaultColor, @NotNull
final Buffer buffer) {
175 if (buffer.mergeLines(text, type, subtype, defaultColor)) {
176 buffer.replaceLine(
parseLine2(text+
" [["+buffer.getLastCount()+
" times]", type, subtype, defaultColor, buffer));
178 buffer.addLine(
parseLine2(text, type, subtype, defaultColor, buffer));
192 private Line parseLine2(@NotNull
final String text,
final int type,
final int subtype, @NotNull
final Color defaultColor, @NotNull
final Buffer buffer) {
196 boolean active =
false;
197 final int iMax = text.length();
198 for (
int i = 0; i < iMax; i++) {
199 final char ch = text.charAt(i);
202 processTag(text.substring(begin, i), defaultColor);
205 }
else if (ch ==
'[' && i == begin) {
212 processText(text.substring(begin, i), buffer, line);
219 processText(text.substring(begin, iMax), buffer, line);
234 if (buffer.mergeLines(text, type, subtype,
null)) {
235 processText(text+
" ["+buffer.getLastCount()+
" times]", buffer, line);
236 buffer.replaceLine(line);
239 buffer.addLine(line);
252 color = defaultColor;
261 private void processTag(@NotNull
final String tag, @NotNull
final Color defaultColor) {
266 if (tag.equals(
"b")) {
268 }
else if (tag.equals(
"/b")) {
270 }
else if (tag.equals(
"i")) {
272 }
else if (tag.equals(
"/i")) {
274 }
else if (tag.equals(
"ul")) {
276 }
else if (tag.equals(
"/ul")) {
278 }
else if (
FONTS.containsKey(tag)) {
281 }
else if (tag.startsWith(
"color=")) {
282 final String colorName = tag.substring(6).toLowerCase(Locale.ENGLISH);
285 if (colorName.startsWith(
"#") && colorName.length() == 7) {
287 color = Color.decode(colorName);
288 }
catch (
final NumberFormatException ignored) {
289 color = defaultColor;
292 color = defaultColor;
295 }
else if (tag.equals(
"/color")) {
296 color = defaultColor;
309 if (text.isEmpty()) {
313 final CharSequence newText;
314 final TextSegment prevSegment = line.getLastTextSegment();
315 if (prevSegment ==
null) {
319 newText = prevSegment.
getText()+text;
320 line.removeLastTextSegment();
327 for (
int i = 0; i < words.length-1; i++) {
330 if (!words[words.length-1].isEmpty()) {
344 for (Entry<String, Color> e :
COLORS.entrySet()) {
345 if (e.getValue() ==
color) {
350 return color.toString();
359 return FORMAT.format(LocalDateTime.now());