4040
4141import javax .swing .*;
4242import javax .swing .text .BadLocationException ;
43- import javax .swing .text .Document ;
4443import javax .swing .text .SimpleAttributeSet ;
4544import java .io .ByteArrayOutputStream ;
4645import java .io .PrintStream ;
@@ -57,28 +56,23 @@ public class ConsoleOutputStream extends ByteArrayOutputStream {
5756 private final PrintStream printStream ;
5857 private final StringBuilder buffer ;
5958 private final Timer timer ;
60- private JScrollPane scrollPane ;
61- private Document document ;
59+ private volatile EditorConsole editorConsole ;
6260
6361 public ConsoleOutputStream (SimpleAttributeSet attributes , PrintStream printStream ) {
6462 this .attributes = attributes ;
6563 this .printStream = printStream ;
6664 this .buffer = new StringBuilder ();
6765
6866 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 ();
7469 }
7570 });
7671 timer .setRepeats (false );
7772 }
7873
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 ;
8276 }
8377
8478 public synchronized void flush () {
@@ -102,7 +96,7 @@ private void handleAppend(String message) {
10296 }
10397
10498 private void resetBufferIfDocumentEmpty () {
105- if (document != null && document . getLength () == 0 ) {
99+ if (editorConsole != null && editorConsole . isEmpty () ) {
106100 buffer .setLength (0 );
107101 }
108102 }
@@ -113,12 +107,10 @@ private void clearBuffer() {
113107
114108 printStream .print (line );
115109
116- if (document != null ) {
110+ if (editorConsole != null ) {
117111 SwingUtilities .invokeLater (() -> {
118112 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 );
122114 } catch (BadLocationException ble ) {
123115 //ignore
124116 }
0 commit comments