40
40
41
41
import javax .swing .*;
42
42
import javax .swing .text .BadLocationException ;
43
- import javax .swing .text .Document ;
44
43
import javax .swing .text .SimpleAttributeSet ;
45
44
import java .io .ByteArrayOutputStream ;
46
45
import java .io .PrintStream ;
@@ -57,28 +56,23 @@ public class ConsoleOutputStream extends ByteArrayOutputStream {
57
56
private final PrintStream printStream ;
58
57
private final StringBuilder buffer ;
59
58
private final Timer timer ;
60
- private JScrollPane scrollPane ;
61
- private Document document ;
59
+ private volatile EditorConsole editorConsole ;
62
60
63
61
public ConsoleOutputStream (SimpleAttributeSet attributes , PrintStream printStream ) {
64
62
this .attributes = attributes ;
65
63
this .printStream = printStream ;
66
64
this .buffer = new StringBuilder ();
67
65
68
66
this .timer = new Timer (100 , (e ) -> {
69
- if (scrollPane != null ) {
70
- synchronized (scrollPane ) {
71
- scrollPane .getHorizontalScrollBar ().setValue (0 );
72
- scrollPane .getVerticalScrollBar ().setValue (scrollPane .getVerticalScrollBar ().getMaximum ());
73
- }
67
+ if (editorConsole != null ) {
68
+ editorConsole .scrollDown ();
74
69
}
75
70
});
76
71
timer .setRepeats (false );
77
72
}
78
73
79
- public synchronized void setCurrentEditorConsole (EditorConsole console ) {
80
- this .scrollPane = console ;
81
- this .document = console .getDocument ();
74
+ public void setCurrentEditorConsole (EditorConsole console ) {
75
+ this .editorConsole = console ;
82
76
}
83
77
84
78
public synchronized void flush () {
@@ -102,7 +96,7 @@ private void handleAppend(String message) {
102
96
}
103
97
104
98
private void resetBufferIfDocumentEmpty () {
105
- if (document != null && document . getLength () == 0 ) {
99
+ if (editorConsole != null && editorConsole . isEmpty () ) {
106
100
buffer .setLength (0 );
107
101
}
108
102
}
@@ -113,12 +107,10 @@ private void clearBuffer() {
113
107
114
108
printStream .print (line );
115
109
116
- if (document != null ) {
110
+ if (editorConsole != null ) {
117
111
SwingUtilities .invokeLater (() -> {
118
112
try {
119
- String lineWithoutCR = line .replace ("\r \n " , "\n " ).replace ("\r " , "\n " );
120
- int offset = document .getLength ();
121
- document .insertString (offset , lineWithoutCR , attributes );
113
+ editorConsole .insertString (line , attributes );
122
114
} catch (BadLocationException ble ) {
123
115
//ignore
124
116
}
0 commit comments