Better organization of formatting functionality.

This commit is contained in:
StevenLawson 2014-08-21 19:44:51 -04:00
parent fd944e110c
commit a6be8fb5a0
5 changed files with 188 additions and 131 deletions

View file

@ -1,5 +1,6 @@
package me.StevenLawson.BukkitTelnetClient;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
@ -7,6 +8,8 @@ import java.util.Map;
import java.util.logging.Level;
import javax.swing.Timer;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.net.telnet.TelnetClient;
public class BTC_ConnectionManager
@ -31,7 +34,7 @@ public class BTC_ConnectionManager
btc.getTxtServer().setEnabled(false);
btc.getBtnDisconnect().setEnabled(true);
btc.writeToConsole("Connecting to " + hostname + ":" + port + "...");
btc.writeToConsole(new BTC_ConsoleMessage("Connecting to " + hostname + ":" + port + "...", Color.RED));
this.hostname = hostname;
this.port = port;
@ -96,7 +99,7 @@ public class BTC_ConnectionManager
updateTitle(false);
btc.writeToConsole("Disconnected.");
btc.writeToConsole(new BTC_ConsoleMessage("Disconnected.", Color.RED));
}
public void sendCommand(final String text)
@ -110,7 +113,7 @@ public class BTC_ConnectionManager
{
if (verbose)
{
BukkitTelnetClient.mainPanel.writeToConsole(":" + text);
BukkitTelnetClient.mainPanel.writeToConsole(new BTC_ConsoleMessage(":" + text));
}
this.telnetClient.getOutputStream().write((text + "\r\n").getBytes());
@ -165,6 +168,7 @@ public class BTC_ConnectionManager
String line;
while ((line = reader.readLine()) != null)
{
String _loginName = null;
if (BTC_ConnectionManager.this.loginName == null)
{
@ -185,9 +189,10 @@ public class BTC_ConnectionManager
}
else
{
if (!BTC_FormatHandler.skipLine(line))
final BTC_TelnetMessage message = new BTC_TelnetMessage(line);
if (!message.skip())
{
btc.writeToConsole(line);
btc.writeToConsole(message);
}
}
}
@ -198,8 +203,7 @@ public class BTC_ConnectionManager
}
catch (IOException ex)
{
ex.printStackTrace(BTC_MainPanel.CONSOLE_STREAM);
btc.updateConsole();
btc.writeToConsole(new BTC_ConsoleMessage(ex.getMessage() + SystemUtils.LINE_SEPARATOR + ExceptionUtils.getStackTrace(ex)));
}
finishDisconnect();

View file

@ -0,0 +1,31 @@
package me.StevenLawson.BukkitTelnetClient;
import java.awt.Color;
public class BTC_ConsoleMessage
{
private final String message;
private final Color color;
public BTC_ConsoleMessage(final String message)
{
this.message = message;
this.color = Color.BLACK;
}
public BTC_ConsoleMessage(final String message, final Color color)
{
this.message = message;
this.color = color;
}
public String getMessage()
{
return message;
}
public Color getColor()
{
return color;
}
}

View file

@ -1,92 +0,0 @@
package me.StevenLawson.BukkitTelnetClient;
import java.awt.Color;
import java.util.regex.Pattern;
public class BTC_FormatHandler
{
private static final Color PURPLE = new Color(128, 0, 128);
private static final Color DARK_GREEN = new Color(86, 130, 3);
private static final Pattern CHAT_MESSAGE = Pattern.compile("^:\\[.+? INFO\\]: \\<");
private static final Pattern SAY_MESSAGE = Pattern.compile("^:\\[.+? INFO\\]: \\[Server:");
private static final Pattern CSAY_MESSAGE = Pattern.compile("^:\\[.+? INFO\\]: \\[CONSOLE\\]<");
private static final Pattern ADMINSAY_MESSAGE = Pattern.compile("^:\\[.+? INFO\\]: \\[TotalFreedomMod\\] \\[ADMIN\\] ");
private static final Pattern WORLD_EDIT = Pattern.compile("^:\\[.+? INFO\\]: WorldEdit: ");
private static final Pattern PREPROCESS_COMMAND = Pattern.compile("^:\\[.+? INFO\\]: \\[PREPROCESS_COMMAND\\] ");
private static final Pattern ISSUED_SERVER_COMMAND = Pattern.compile("^:\\[.+? INFO\\]: .+? issued server command: ");
private static final Pattern PLAYER_COMMAND = Pattern.compile("^:\\[.+? INFO\\]: \\[PLAYER_COMMAND\\] ");
private static final Pattern ERROR_MESSAGE = Pattern.compile("^:\\[.+? (!?(WARN)|(ERROR))\\]: ");
private static final Pattern EXCEPTION_MESSAGE = Pattern.compile("^[^\\[][^\\s]+: ");
private static final Pattern STACK_TRACE = Pattern.compile("^\\t");
private BTC_FormatHandler()
{
throw new AssertionError();
}
public static final boolean skipLine(String line)
{
final BTC_MainPanel mainPanel = BukkitTelnetClient.mainPanel;
if (mainPanel == null)
{
return false;
}
if (mainPanel.getChkShowChatOnly().isSelected())
{
if (!CHAT_MESSAGE.matcher(line).find() && !SAY_MESSAGE.matcher(line).find() && !ADMINSAY_MESSAGE.matcher(line).find() && !CSAY_MESSAGE.matcher(line).find())
{
return true;
}
}
else if (mainPanel.getChkIgnoreServerCommands().isSelected() && ISSUED_SERVER_COMMAND.matcher(line).find())
{
return true;
}
else if (mainPanel.getChkIgnorePlayerCommands().isSelected() && PLAYER_COMMAND.matcher(line).find())
{
return true;
}
else if (mainPanel.getChkIgnoreErrors().isSelected() && (ERROR_MESSAGE.matcher(line).find() || STACK_TRACE.matcher(line).find() || EXCEPTION_MESSAGE.matcher(line).find()))
{
return true;
}
return false;
}
public static final Color getColor(String line)
{
Color color = Color.BLACK;
if (CHAT_MESSAGE.matcher(line).find() || SAY_MESSAGE.matcher(line).find() || CSAY_MESSAGE.matcher(line).find())
{
color = Color.BLUE;
}
else if (ADMINSAY_MESSAGE.matcher(line).find())
{
color = PURPLE;
}
else if (WORLD_EDIT.matcher(line).find())
{
color = Color.RED;
}
else if (PREPROCESS_COMMAND.matcher(line).find())
{
color = DARK_GREEN;
}
else if (ERROR_MESSAGE.matcher(line).find() || STACK_TRACE.matcher(line).find() || EXCEPTION_MESSAGE.matcher(line).find())
{
color = Color.LIGHT_GRAY;
}
return color;
}
}

View file

@ -3,8 +3,6 @@ package me.StevenLawson.BukkitTelnetClient;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.*;
import javax.swing.*;
@ -12,12 +10,10 @@ import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
public class BTC_MainPanel extends javax.swing.JFrame
{
public static final ByteArrayOutputStream CONSOLE = new ByteArrayOutputStream();
public static final PrintStream CONSOLE_STREAM = new PrintStream(CONSOLE);
private final BTC_ConnectionManager connectionManager = new BTC_ConnectionManager();
public BTC_MainPanel()
@ -57,9 +53,9 @@ public class BTC_MainPanel extends javax.swing.JFrame
this.setVisible(true);
}
public final void updateTextPane(final String line)
public final void writeToConsole(final BTC_ConsoleMessage message)
{
if (line.isEmpty())
if (message.getMessage().isEmpty())
{
return;
}
@ -77,8 +73,8 @@ public class BTC_MainPanel extends javax.swing.JFrame
{
styledDocument.insertString(
styledDocument.getLength(),
line,
StyleContext.getDefaultStyleContext().addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, BTC_FormatHandler.getColor(line))
message.getMessage() + SystemUtils.LINE_SEPARATOR,
StyleContext.getDefaultStyleContext().addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, message.getColor())
);
}
catch (BadLocationException ex)
@ -113,27 +109,6 @@ public class BTC_MainPanel extends javax.swing.JFrame
});
}
public final void updateConsole()
{
final String data = CONSOLE.toString();
CONSOLE.reset();
final String[] lines = data.split("\\r?\\n");
for (String line : lines)
{
if (!line.isEmpty())
{
updateTextPane(line + '\n');
}
}
}
public final void writeToConsole(String line)
{
CONSOLE_STREAM.append(line + '\n');
updateConsole();
}
public final PlayerInfo getSelectedPlayer()
{
String name = null;
@ -333,13 +308,13 @@ public class BTC_MainPanel extends javax.swing.JFrame
case "Copy IP":
{
copyToClipboard(_player.getIp());
BTC_MainPanel.this.writeToConsole("Copied IP to clipboard: " + _player.getIp());
BTC_MainPanel.this.writeToConsole(new BTC_ConsoleMessage("Copied IP to clipboard: " + _player.getIp()));
break;
}
case "Copy Name":
{
copyToClipboard(_player.getName());
BTC_MainPanel.this.writeToConsole("Copied name to clipboard: " + _player.getName());
BTC_MainPanel.this.writeToConsole(new BTC_ConsoleMessage("Copied name to clipboard: " + _player.getName()));
break;
}
}

View file

@ -0,0 +1,139 @@
package me.StevenLawson.BukkitTelnetClient;
import java.awt.Color;
import java.util.regex.Pattern;
public class BTC_TelnetMessage extends BTC_ConsoleMessage
{
private static final String PATTERN_PREFIX = "^:\\[.+? INFO\\]: ";
private static final Color PURPLE = new Color(128, 0, 128);
private static final Color DARK_GREEN = new Color(86, 130, 3);
private final BTC_LogMessageType messageType;
public BTC_TelnetMessage(String message)
{
super(message);
this.messageType = BTC_LogMessageType.getMessageType(message);
}
public BTC_LogMessageType getMessageType()
{
return this.messageType;
}
public boolean skip()
{
final BTC_MainPanel mainPanel = BukkitTelnetClient.mainPanel;
if (mainPanel == null || this.messageType == null)
{
return false;
}
if (mainPanel.getChkShowChatOnly().isSelected())
{
switch (messageType)
{
case CHAT_MESSAGE:
case CSAY_MESSAGE:
case SAY_MESSAGE:
case ADMINSAY_MESSAGE:
return false;
default:
return true;
}
}
else if (mainPanel.getChkIgnoreServerCommands().isSelected() && this.messageType == BTC_LogMessageType.ISSUED_SERVER_COMMAND)
{
return true;
}
else if (mainPanel.getChkIgnorePlayerCommands().isSelected() && this.messageType == BTC_LogMessageType.PLAYER_COMMAND)
{
return true;
}
else if (mainPanel.getChkIgnoreErrors().isSelected())
{
switch (this.messageType)
{
case ERROR_MESSAGE:
case STACK_TRACE:
case EXCEPTION_MESSAGE:
return false;
default:
return true;
}
}
return false;
}
@Override
public Color getColor()
{
if (this.messageType == null)
{
return super.getColor();
}
else
{
return this.messageType.getColor();
}
}
public static enum BTC_LogMessageType
{
CHAT_MESSAGE(PATTERN_PREFIX + "\\<", Color.BLUE),
SAY_MESSAGE(PATTERN_PREFIX + "\\[Server:", Color.BLUE),
CSAY_MESSAGE(PATTERN_PREFIX + "\\[CONSOLE\\]<", Color.BLUE),
//
ADMINSAY_MESSAGE(PATTERN_PREFIX + "\\[TotalFreedomMod\\] \\[ADMIN\\] ", PURPLE),
//
WORLD_EDIT(PATTERN_PREFIX + "WorldEdit: ", Color.RED),
//
PREPROCESS_COMMAND(PATTERN_PREFIX + "\\[PREPROCESS_COMMAND\\] ", DARK_GREEN),
//
ISSUED_SERVER_COMMAND(PATTERN_PREFIX + ".+? issued server command: "),
PLAYER_COMMAND(PATTERN_PREFIX + "\\[PLAYER_COMMAND\\] "),
//
ERROR_MESSAGE("^:\\[.+? (!?(WARN)|(ERROR))\\]: ", Color.LIGHT_GRAY),
EXCEPTION_MESSAGE("^[^\\[][^\\s]+: ", Color.LIGHT_GRAY),
STACK_TRACE("^\\t", Color.LIGHT_GRAY);
private final Pattern messagePattern;
private final Color color;
private BTC_LogMessageType(final String messagePatternStr)
{
this.messagePattern = Pattern.compile(messagePatternStr);
this.color = Color.BLACK;
}
private BTC_LogMessageType(final String messagePatternStr, final Color color)
{
this.messagePattern = Pattern.compile(messagePatternStr);
this.color = color;
}
public Pattern getMessagePattern()
{
return this.messagePattern;
}
public Color getColor()
{
return this.color;
}
public static BTC_LogMessageType getMessageType(final String message)
{
for (final BTC_LogMessageType type : values())
{
if (type.getMessagePattern().matcher(message).find())
{
return type;
}
}
return null;
}
}
}