33
33
import java .awt .*;
34
34
35
35
import javax .swing .*;
36
+ import javax .swing .text .AttributeSet ;
36
37
import javax .swing .text .BadLocationException ;
37
- import javax .swing .text .Style ;
38
+ import javax .swing .text .MutableAttributeSet ;
39
+ import javax .swing .text .SimpleAttributeSet ;
38
40
import javax .swing .text .StyleConstants ;
39
41
import javax .swing .text .StyledDocument ;
40
42
46
48
import org .scijava .log .LogMessage ;
47
49
import org .scijava .log .LogService ;
48
50
import org .scijava .log .Logger ;
51
+ import org .scijava .log .LogLevel ;
49
52
import org .scijava .thread .ThreadService ;
50
53
import org .scijava .ui .swing .StaticSwingUtils ;
51
54
64
67
@ IgnoreAsCallingClass
65
68
public class LoggingPanel extends JPanel implements LogListener
66
69
{
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
+
67
78
private JTextPane textPane ;
68
79
private JScrollPane scrollPane ;
69
80
70
81
private StyledDocument doc ;
71
- private Style defaultStyle ;
72
82
73
83
private final LogFormatter formatter = new LogFormatter ();
74
84
@@ -87,12 +97,12 @@ public void clear() {
87
97
88
98
@ Override
89
99
public void messageLogged (LogMessage message ) {
90
- appendText (formatter .format (message ), defaultStyle );
100
+ appendText (formatter .format (message ), getLevelStyle ( message . level ()) );
91
101
}
92
102
93
103
// -- Helper methods --
94
104
95
- private void appendText (final String text , final Style style ) {
105
+ private void appendText (final String text , final AttributeSet style ) {
96
106
threadService .queue (new Runnable () {
97
107
98
108
@ Override
@@ -119,8 +129,6 @@ private synchronized void initGui() {
119
129
120
130
doc = textPane .getStyledDocument ();
121
131
122
- defaultStyle = createStyle ("stdoutLocal" , null , Color .black , null , null );
123
-
124
132
// NB: We wrap the JTextPane in a JPanel to disable
125
133
// the text pane's intelligent line wrapping behavior.
126
134
// I.e.: we want console lines _not_ to wrap, but instead
@@ -143,13 +151,32 @@ private synchronized void initGui() {
143
151
add (scrollPane );
144
152
}
145
153
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 );
153
180
return style ;
154
181
}
155
182
0 commit comments