Skip to content

Commit f510812

Browse files
committed
LoggingPanel: use colors to visualize log levels
1 parent 4f7bd3b commit f510812

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

src/main/java/org/scijava/ui/swing/console/LoggingPanel.java

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
import java.awt.*;
3434

3535
import javax.swing.*;
36+
import javax.swing.text.AttributeSet;
3637
import javax.swing.text.BadLocationException;
37-
import javax.swing.text.Style;
38+
import javax.swing.text.MutableAttributeSet;
39+
import javax.swing.text.SimpleAttributeSet;
3840
import javax.swing.text.StyleConstants;
3941
import javax.swing.text.StyledDocument;
4042

@@ -46,6 +48,7 @@
4648
import org.scijava.log.LogMessage;
4749
import org.scijava.log.LogService;
4850
import org.scijava.log.Logger;
51+
import org.scijava.log.LogLevel;
4952
import org.scijava.thread.ThreadService;
5053
import org.scijava.ui.swing.StaticSwingUtils;
5154

@@ -64,11 +67,18 @@
6467
@IgnoreAsCallingClass
6568
public class LoggingPanel extends JPanel implements LogListener
6669
{
70+
private static final AttributeSet DEFAULT_STYLE = new SimpleAttributeSet();
71+
private static final AttributeSet STYLE_ERROR = normal(new Color(200, 0, 0));
72+
private static final AttributeSet STYLE_WARN = normal(new Color(200, 140, 0));
73+
private static final AttributeSet STYLE_INFO = normal(Color.BLACK);
74+
private static final AttributeSet STYLE_DEBUG = normal(new Color(0, 0, 200));
75+
private static final AttributeSet STYLE_TRACE = normal(Color.GRAY);
76+
private static final AttributeSet STYLE_OTHERS = normal(Color.GRAY);
77+
6778
private JTextPane textPane;
6879
private JScrollPane scrollPane;
6980

7081
private StyledDocument doc;
71-
private Style defaultStyle;
7282

7383
private final LogFormatter formatter = new LogFormatter();
7484

@@ -87,12 +97,12 @@ public void clear() {
8797

8898
@Override
8999
public void messageLogged(LogMessage message) {
90-
appendText(formatter.format(message), defaultStyle);
100+
appendText(formatter.format(message), getLevelStyle(message.level()));
91101
}
92102

93103
// -- Helper methods --
94104

95-
private void appendText(final String text, final Style style) {
105+
private void appendText(final String text, final AttributeSet style) {
96106
threadService.queue(new Runnable() {
97107

98108
@Override
@@ -119,8 +129,6 @@ private synchronized void initGui() {
119129

120130
doc = textPane.getStyledDocument();
121131

122-
defaultStyle = createStyle("stdoutLocal", null, Color.black, null, null);
123-
124132
// NB: We wrap the JTextPane in a JPanel to disable
125133
// the text pane's intelligent line wrapping behavior.
126134
// I.e.: we want console lines _not_ to wrap, but instead
@@ -143,13 +151,32 @@ private synchronized void initGui() {
143151
add(scrollPane);
144152
}
145153

146-
private Style createStyle(final String name, final Style parent,
147-
final Color foreground, final Boolean bold, final Boolean italic)
148-
{
149-
final Style style = textPane.addStyle(name, parent);
150-
if (foreground != null) StyleConstants.setForeground(style, foreground);
151-
if (bold != null) StyleConstants.setBold(style, bold);
152-
if (italic != null) StyleConstants.setItalic(style, italic);
154+
private static AttributeSet getLevelStyle(int i) {
155+
switch (i) {
156+
case LogLevel.ERROR:
157+
return STYLE_ERROR;
158+
case LogLevel.WARN:
159+
return STYLE_WARN;
160+
case LogLevel.INFO:
161+
return STYLE_INFO;
162+
case LogLevel.DEBUG:
163+
return STYLE_DEBUG;
164+
case LogLevel.TRACE:
165+
return STYLE_TRACE;
166+
default:
167+
return STYLE_OTHERS;
168+
}
169+
}
170+
171+
private static MutableAttributeSet normal(Color color) {
172+
MutableAttributeSet style = new SimpleAttributeSet();
173+
StyleConstants.setForeground(style, color);
174+
return style;
175+
}
176+
177+
private static MutableAttributeSet italic(Color color) {
178+
MutableAttributeSet style = normal(color);
179+
StyleConstants.setItalic(style, true);
153180
return style;
154181
}
155182

0 commit comments

Comments
 (0)