40
40
41
41
import net .miginfocom .swing .MigLayout ;
42
42
43
- import org .scijava .console .OutputEvent ;
44
43
import org .scijava .console .OutputListener ;
45
44
import org .scijava .log .IgnoreAsCallingClass ;
46
45
import org .scijava .log .LogListener ;
46
+ import org .scijava .log .LogMessage ;
47
47
import org .scijava .log .LogService ;
48
48
import org .scijava .log .Logger ;
49
49
import org .scijava .thread .ThreadService ;
62
62
* @author Matthias Arzt
63
63
*/
64
64
@ IgnoreAsCallingClass
65
- public class LoggingPanel extends JPanel implements OutputListener
65
+ public class LoggingPanel extends JPanel implements LogListener
66
66
{
67
67
private JTextPane textPane ;
68
68
private JScrollPane scrollPane ;
69
69
70
70
private StyledDocument doc ;
71
- private Style stdoutLocal ;
72
- private Style stderrLocal ;
73
- private Style stdoutGlobal ;
74
- private Style stderrGlobal ;
71
+ private Style defaultStyle ;
72
+
73
+ private final LogFormatter formatter = new LogFormatter ();
75
74
76
75
private final ThreadService threadService ;
77
76
@@ -84,16 +83,24 @@ public void clear() {
84
83
textPane .setText ("" );
85
84
}
86
85
86
+ // -- LogListener methods --
87
+
87
88
@ Override
88
- public void outputOccurred (OutputEvent event ) {
89
+ public void messageLogged (LogMessage message ) {
90
+ appendText (formatter .format (message ), defaultStyle );
91
+ }
92
+
93
+ // -- Helper methods --
94
+
95
+ private void appendText (final String text , final Style style ) {
89
96
threadService .queue (new Runnable () {
90
97
91
98
@ Override
92
99
public void run () {
93
100
final boolean atBottom =
94
101
StaticSwingUtils .isScrolledToBottom (scrollPane );
95
102
try {
96
- doc .insertString (doc .getLength (), event . getOutput (), getStyle ( event ) );
103
+ doc .insertString (doc .getLength (), text , style );
97
104
}
98
105
catch (final BadLocationException exc ) {
99
106
throw new RuntimeException (exc );
@@ -112,10 +119,7 @@ private synchronized void initGui() {
112
119
113
120
doc = textPane .getStyledDocument ();
114
121
115
- stdoutLocal = createStyle ("stdoutLocal" , null , Color .black , null , null );
116
- stderrLocal = createStyle ("stderrLocal" , null , Color .red , null , null );
117
- stdoutGlobal = createStyle ("stdoutGlobal" , stdoutLocal , null , null , true );
118
- stderrGlobal = createStyle ("stderrGlobal" , stderrLocal , null , null , true );
122
+ defaultStyle = createStyle ("stdoutLocal" , null , Color .black , null , null );
119
123
120
124
// NB: We wrap the JTextPane in a JPanel to disable
121
125
// the text pane's intelligent line wrapping behavior.
@@ -138,7 +142,6 @@ private synchronized void initGui() {
138
142
139
143
add (scrollPane );
140
144
}
141
- // -- Helper methods --
142
145
143
146
private Style createStyle (final String name , final Style parent ,
144
147
final Color foreground , final Boolean bold , final Boolean italic )
@@ -150,14 +153,9 @@ private Style createStyle(final String name, final Style parent,
150
153
return style ;
151
154
}
152
155
153
- private Style getStyle (final OutputEvent event ) {
154
- final boolean stderr = event .getSource () == OutputEvent .Source .STDERR ;
155
- final boolean contextual = event .isContextual ();
156
- if (stderr ) return contextual ? stderrLocal : stderrGlobal ;
157
- return contextual ? stdoutLocal : stdoutGlobal ;
158
- }
156
+ // -- Helper methods - testing --
159
157
160
- public JTextPane getTextPane () {
158
+ JTextPane getTextPane () {
161
159
return textPane ;
162
160
}
163
161
}
0 commit comments