GP-2724: Use JTextPane and attributes instead of HTML for AgentWindow

This commit is contained in:
Dan 2022-11-08 12:12:13 -05:00
parent b5537b91e5
commit e3417cbfdc

View file

@ -21,6 +21,7 @@ import java.awt.event.WindowListener;
import java.net.SocketAddress; import java.net.SocketAddress;
import javax.swing.*; import javax.swing.*;
import javax.swing.text.*;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.Appender;
@ -36,8 +37,8 @@ import log.LogPanelAppender;
public class AgentWindow extends JFrame implements WindowListener, LogListener { public class AgentWindow extends JFrame implements WindowListener, LogListener {
public static final int MAX_LOG_CHARS = 100000; public static final int MAX_LOG_CHARS = 100000;
protected final JTextArea logArea = new JTextArea(); protected final JTextPane logPane = new JTextPane();
protected final JScrollPane logScroll = new JScrollPane(logArea); protected final JScrollPane logScroll = new JScrollPane(logPane);
public AgentWindow(String title, SocketAddress localAddress) { public AgentWindow(String title, SocketAddress localAddress) {
super(title); super(title);
@ -45,10 +46,12 @@ public class AgentWindow extends JFrame implements WindowListener, LogListener {
addWindowListener(this); addWindowListener(this);
add(new JLabel("<html>This agent is listening at <b>" + localAddress + add(new JLabel("<html>This agent is listening at <b>" + localAddress +
"</b>. Close this window to terminate it.</html>"), BorderLayout.NORTH); "</b>. Close this window to terminate it.</html>"), BorderLayout.NORTH);
logArea.setEditable(false); logPane.setEditable(false);
logArea.setFont(Font.decode(Font.MONOSPACED)); logPane.setFont(Font.decode(Font.MONOSPACED));
logArea.setAutoscrolls(true); logPane.setAutoscrolls(true);
logScroll.setAutoscrolls(true); logScroll.setAutoscrolls(true);
DefaultCaret caret = (DefaultCaret) logPane.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
add(logScroll); add(logScroll);
setMinimumSize(new Dimension(400, 300)); setMinimumSize(new Dimension(400, 300));
setVisible(true); setVisible(true);
@ -70,13 +73,21 @@ public class AgentWindow extends JFrame implements WindowListener, LogListener {
@Override @Override
public void messageLogged(String message, boolean isError) { public void messageLogged(String message, boolean isError) {
String fMessage = isError ? "<font color=\"red\">" + message + "</font>" : message;
Swing.runIfSwingOrRunLater(() -> { Swing.runIfSwingOrRunLater(() -> {
String allText = logArea.getText() + fMessage + "\n"; MutableAttributeSet attributes = new SimpleAttributeSet();
logArea.setText( if (isError) {
allText.substring(Math.max(0, allText.length() - MAX_LOG_CHARS), allText.length())); StyleConstants.setForeground(attributes, Color.RED);
JScrollBar vScroll = logScroll.getVerticalScrollBar(); }
vScroll.setValue(vScroll.getMaximum()); Document document = logPane.getStyledDocument();
try {
document.insertString(document.getLength(), message + "\n", attributes);
if (document.getLength() > MAX_LOG_CHARS) {
document.remove(0, document.getLength() - MAX_LOG_CHARS);
}
}
catch (BadLocationException e) {
throw new AssertionError(e);
}
}); });
} }