Skip to content

Commit 149f906

Browse files
committed
Merge pull request #3433 from ffissore/new-console
New console
2 parents 7c91cba + 98874e4 commit 149f906

8 files changed

+198
-261
lines changed
+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* This file is part of Arduino.
3+
*
4+
* Arduino is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
28+
*
29+
* Original version of this file courtesy of Rob Camick
30+
* <p>
31+
* https://tips4java.wordpress.com/2008/11/08/message-console/
32+
* <p>
33+
* About page at https://tips4java.wordpress.com/about/ says something
34+
* like MIT
35+
*/
36+
37+
package cc.arduino;
38+
39+
import processing.app.EditorConsole;
40+
41+
import javax.swing.*;
42+
import javax.swing.text.BadLocationException;
43+
import javax.swing.text.Document;
44+
import javax.swing.text.SimpleAttributeSet;
45+
import java.io.ByteArrayOutputStream;
46+
import java.io.PrintStream;
47+
48+
/*
49+
* Class to intercept output from a PrintStream and add it to a Document.
50+
* The output can optionally be redirected to a different PrintStream.
51+
* The text displayed in the Document can be color coded to indicate
52+
* the output source.
53+
*/
54+
public class ConsoleOutputStream extends ByteArrayOutputStream {
55+
56+
private final SimpleAttributeSet attributes;
57+
private final PrintStream printStream;
58+
private final StringBuilder buffer;
59+
private final Timer timer;
60+
private JScrollPane scrollPane;
61+
private Document document;
62+
63+
public ConsoleOutputStream(SimpleAttributeSet attributes, PrintStream printStream) {
64+
this.attributes = attributes;
65+
this.printStream = printStream;
66+
this.buffer = new StringBuilder();
67+
68+
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+
}
74+
}
75+
});
76+
timer.setRepeats(false);
77+
}
78+
79+
public synchronized void setCurrentEditorConsole(EditorConsole console) {
80+
this.scrollPane = console;
81+
this.document = console.getDocument();
82+
}
83+
84+
public synchronized void flush() {
85+
String message = toString();
86+
87+
if (message.length() == 0) {
88+
return;
89+
}
90+
91+
handleAppend(message);
92+
93+
reset();
94+
}
95+
96+
private void handleAppend(String message) {
97+
resetBufferIfDocumentEmpty();
98+
99+
buffer.append(message);
100+
101+
clearBuffer();
102+
}
103+
104+
private void resetBufferIfDocumentEmpty() {
105+
if (document != null && document.getLength() == 0) {
106+
buffer.setLength(0);
107+
}
108+
}
109+
110+
private void clearBuffer() {
111+
String line = buffer.toString();
112+
buffer.setLength(0);
113+
114+
printStream.print(line);
115+
116+
if (document != null) {
117+
SwingUtilities.invokeLater(() -> {
118+
try {
119+
String lineWithoutSlashR = line.replace("\r\n", "\n").replace("\r", "\n");
120+
int offset = document.getLength();
121+
document.insertString(offset, lineWithoutSlashR, attributes);
122+
} catch (BadLocationException ble) {
123+
//ignore
124+
}
125+
});
126+
127+
if (!timer.isRunning()) {
128+
timer.restart();
129+
}
130+
}
131+
}
132+
}

app/src/processing/app/Base.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ protected void handleActivated(Editor whichEditor) {
641641
}
642642

643643
// set the current window to be the console that's getting output
644-
EditorConsoleStream.setCurrent(activeEditor.console);
644+
EditorConsole.setCurrentEditorConsole(activeEditor.console);
645645
}
646646

647647

app/src/processing/app/Editor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public void windowDeactivated(WindowEvent e) {
264264
status = new EditorStatus(this);
265265
consolePanel.add(status, BorderLayout.NORTH);
266266

267-
console = new EditorConsole(this);
267+
console = new EditorConsole();
268268
console.setName("console");
269269
// windows puts an ugly border on this guy
270270
console.setBorder(null);

0 commit comments

Comments
 (0)