Skip to content

Commit e5252e3

Browse files
author
Federico Fissore
committed
Slightly improved how EditorConsole works
1 parent c7cb263 commit e5252e3

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

app/src/cc/arduino/ConsoleOutputStream.java

+8-16
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040

4141
import javax.swing.*;
4242
import javax.swing.text.BadLocationException;
43-
import javax.swing.text.Document;
4443
import javax.swing.text.SimpleAttributeSet;
4544
import java.io.ByteArrayOutputStream;
4645
import 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
}

app/src/processing/app/EditorConsole.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class EditorConsole extends JScrollPane {
3636
private static ConsoleOutputStream out;
3737
private static ConsoleOutputStream err;
3838

39-
public static synchronized void init(SimpleAttributeSet outStyle, PrintStream outStream, SimpleAttributeSet errStyle, PrintStream errStream) {
39+
private static synchronized void init(SimpleAttributeSet outStyle, PrintStream outStream, SimpleAttributeSet errStyle, PrintStream errStream) {
4040
if (out != null) {
4141
return;
4242
}
@@ -116,11 +116,22 @@ public void clear() {
116116
}
117117
}
118118

119+
public void scrollDown() {
120+
getHorizontalScrollBar().setValue(0);
121+
getVerticalScrollBar().setValue(getVerticalScrollBar().getMaximum());
122+
}
123+
124+
public boolean isEmpty() {
125+
return document.getLength() == 0;
126+
}
127+
128+
public void insertString(String line, SimpleAttributeSet attributes) throws BadLocationException {
129+
int offset = document.getLength();
130+
document.insertString(offset, line, attributes);
131+
}
132+
119133
public String getText() {
120134
return consoleTextPane.getText().trim();
121135
}
122136

123-
public Document getDocument() {
124-
return document;
125-
}
126137
}

0 commit comments

Comments
 (0)