3333import java .awt .*;
3434
3535import javax .swing .*;
36+ import javax .swing .text .AttributeSet ;
3637import javax .swing .text .BadLocationException ;
37- import javax .swing .text .Style ;
38+ import javax .swing .text .MutableAttributeSet ;
39+ import javax .swing .text .SimpleAttributeSet ;
3840import javax .swing .text .StyleConstants ;
3941import javax .swing .text .StyledDocument ;
4042
4648import org .scijava .log .LogMessage ;
4749import org .scijava .log .LogService ;
4850import org .scijava .log .Logger ;
51+ import org .scijava .log .LogLevel ;
4952import org .scijava .thread .ThreadService ;
5053import org .scijava .ui .swing .StaticSwingUtils ;
5154
6467@ IgnoreAsCallingClass
6568public 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