mirror of
https://github.com/arduino/Arduino.git
synced 2025-03-15 12:29:26 +01:00
Splitted GUI and Streams in EditorConsole
This commit is contained in:
parent
93562a7800
commit
872897d6ad
@ -764,7 +764,7 @@ public class Base {
|
|||||||
activeEditor = whichEditor;
|
activeEditor = whichEditor;
|
||||||
|
|
||||||
// set the current window to be the console that's getting output
|
// set the current window to be the console that's getting output
|
||||||
EditorConsole.setEditor(activeEditor);
|
EditorConsoleStream.setCurrent(activeEditor.console);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,19 +21,12 @@
|
|||||||
|
|
||||||
package processing.app;
|
package processing.app;
|
||||||
|
|
||||||
import static processing.app.I18n._;
|
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
import java.awt.FontMetrics;
|
import java.awt.FontMetrics;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.PrintStream;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -65,25 +58,10 @@ public class EditorConsole extends JScrollPane {
|
|||||||
SimpleAttributeSet stdStyle;
|
SimpleAttributeSet stdStyle;
|
||||||
SimpleAttributeSet errStyle;
|
SimpleAttributeSet errStyle;
|
||||||
|
|
||||||
static File errFile;
|
|
||||||
static File outFile;
|
|
||||||
static File tempFolder;
|
|
||||||
|
|
||||||
// Single static instance shared because there's only one real System.out.
|
// Single static instance shared because there's only one real System.out.
|
||||||
// Within the input handlers, the currentConsole variable will be used to
|
// Within the input handlers, the currentConsole variable will be used to
|
||||||
// echo things to the correct location.
|
// echo things to the correct location.
|
||||||
|
|
||||||
static public PrintStream systemOut;
|
|
||||||
static public PrintStream systemErr;
|
|
||||||
|
|
||||||
static PrintStream consoleOut;
|
|
||||||
static PrintStream consoleErr;
|
|
||||||
|
|
||||||
static OutputStream stdoutFile;
|
|
||||||
static OutputStream stderrFile;
|
|
||||||
|
|
||||||
static EditorConsole currentConsole;
|
|
||||||
|
|
||||||
public EditorConsole(Editor _editor) {
|
public EditorConsole(Editor _editor) {
|
||||||
editor = _editor;
|
editor = _editor;
|
||||||
|
|
||||||
@ -136,47 +114,7 @@ public class EditorConsole extends JScrollPane {
|
|||||||
setPreferredSize(new Dimension(1024, (height * lines) + sizeFudge));
|
setPreferredSize(new Dimension(1024, (height * lines) + sizeFudge));
|
||||||
setMinimumSize(new Dimension(1024, (height * 4) + sizeFudge));
|
setMinimumSize(new Dimension(1024, (height * 4) + sizeFudge));
|
||||||
|
|
||||||
if (systemOut == null) {
|
EditorConsoleStream.init();
|
||||||
systemOut = System.out;
|
|
||||||
systemErr = System.err;
|
|
||||||
|
|
||||||
// Create a temporary folder which will have a randomized name. Has to
|
|
||||||
// be randomized otherwise another instance of Processing (or one of its
|
|
||||||
// sister IDEs) might collide with the file causing permissions problems.
|
|
||||||
// The files and folders are not deleted on exit because they may be
|
|
||||||
// needed for debugging or bug reporting.
|
|
||||||
tempFolder = Base.createTempFolder("console");
|
|
||||||
tempFolder.deleteOnExit();
|
|
||||||
try {
|
|
||||||
String outFileName = Preferences.get("console.output.file");
|
|
||||||
if (outFileName != null) {
|
|
||||||
outFile = new File(tempFolder, outFileName);
|
|
||||||
outFile.deleteOnExit();
|
|
||||||
stdoutFile = new FileOutputStream(outFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
String errFileName = Preferences.get("console.error.file");
|
|
||||||
if (errFileName != null) {
|
|
||||||
errFile = new File(tempFolder, errFileName);
|
|
||||||
errFile.deleteOnExit();
|
|
||||||
stderrFile = new FileOutputStream(errFile);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
Base.showWarning(_("Console Error"),
|
|
||||||
_("A problem occurred while trying to open the\nfiles used to store the console output."), e);
|
|
||||||
}
|
|
||||||
consoleOut = new PrintStream(new EditorConsoleStream(false));
|
|
||||||
consoleErr = new PrintStream(new EditorConsoleStream(true));
|
|
||||||
|
|
||||||
if (Preferences.getBoolean("console")) {
|
|
||||||
try {
|
|
||||||
System.setOut(consoleOut);
|
|
||||||
System.setErr(consoleErr);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace(systemOut);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// to fix ugliness.. normally macosx java 1.3 puts an
|
// to fix ugliness.. normally macosx java 1.3 puts an
|
||||||
// ugly white border around this object, so turn it off.
|
// ugly white border around this object, so turn it off.
|
||||||
@ -205,70 +143,6 @@ public class EditorConsole extends JScrollPane {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static public void setEditor(Editor editor) {
|
|
||||||
currentConsole = editor.console;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close the streams so that the temporary files can be deleted.
|
|
||||||
* <p/>
|
|
||||||
* File.deleteOnExit() cannot be used because the stdout and stderr
|
|
||||||
* files are inside a folder, and have to be deleted before the
|
|
||||||
* folder itself is deleted, which can't be guaranteed when using
|
|
||||||
* the deleteOnExit() method.
|
|
||||||
*/
|
|
||||||
public void handleQuit() {
|
|
||||||
// replace original streams to remove references to console's streams
|
|
||||||
System.setOut(systemOut);
|
|
||||||
System.setErr(systemErr);
|
|
||||||
|
|
||||||
// close the PrintStream
|
|
||||||
consoleOut.close();
|
|
||||||
consoleErr.close();
|
|
||||||
|
|
||||||
// also have to close the original FileOutputStream
|
|
||||||
// otherwise it won't be shut down completely
|
|
||||||
try {
|
|
||||||
stdoutFile.close();
|
|
||||||
stderrFile.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
outFile.delete();
|
|
||||||
errFile.delete();
|
|
||||||
tempFolder.delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void write(byte b[], int offset, int length, boolean err) {
|
|
||||||
// we could do some cross platform CR/LF mangling here before outputting
|
|
||||||
// add text to output document
|
|
||||||
message(new String(b, offset, length), err, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// added sync for 0091.. not sure if it helps or hinders
|
|
||||||
synchronized public void message(String what, boolean err, boolean advance) {
|
|
||||||
if (err) {
|
|
||||||
systemErr.print(what);
|
|
||||||
if (advance)
|
|
||||||
systemErr.println();
|
|
||||||
} else {
|
|
||||||
systemOut.print(what);
|
|
||||||
if (advance)
|
|
||||||
systemOut.println();
|
|
||||||
}
|
|
||||||
|
|
||||||
// to console display
|
|
||||||
appendText(what, err);
|
|
||||||
if (advance)
|
|
||||||
appendText("\n", err);
|
|
||||||
// moved down here since something is punting
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append a piece of text to the console.
|
* Append a piece of text to the console.
|
||||||
* <P>
|
* <P>
|
||||||
@ -281,7 +155,7 @@ public class EditorConsole extends JScrollPane {
|
|||||||
* Updates are buffered to the console and displayed at regular
|
* Updates are buffered to the console and displayed at regular
|
||||||
* intervals on Swing's event-dispatching thread. (patch by David Mellis)
|
* intervals on Swing's event-dispatching thread. (patch by David Mellis)
|
||||||
*/
|
*/
|
||||||
synchronized private void appendText(String txt, boolean e) {
|
synchronized void appendText(String txt, boolean e) {
|
||||||
consoleDoc.appendString(txt, e ? errStyle : stdStyle);
|
consoleDoc.appendString(txt, e ? errStyle : stdStyle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,57 +168,6 @@ public class EditorConsole extends JScrollPane {
|
|||||||
// maybe not a good idea in the long run?
|
// maybe not a good idea in the long run?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
|
||||||
|
|
||||||
|
|
||||||
private static class EditorConsoleStream extends OutputStream {
|
|
||||||
final boolean err; // whether stderr or stdout
|
|
||||||
PrintStream system;
|
|
||||||
OutputStream file;
|
|
||||||
|
|
||||||
public EditorConsoleStream(boolean _err) {
|
|
||||||
err = _err;
|
|
||||||
if (err) {
|
|
||||||
system = systemErr;
|
|
||||||
file = stderrFile;
|
|
||||||
} else {
|
|
||||||
system = systemOut;
|
|
||||||
file = stdoutFile;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void close() { }
|
|
||||||
|
|
||||||
public void flush() { }
|
|
||||||
|
|
||||||
public void write(int b) {
|
|
||||||
write(new byte[] { (byte) b });
|
|
||||||
}
|
|
||||||
|
|
||||||
public void write(byte b[]) { // appears never to be used
|
|
||||||
write(b, 0, b.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void write(byte b[], int offset, int length) {
|
|
||||||
if (currentConsole != null) {
|
|
||||||
currentConsole.write(b, offset, length, err);
|
|
||||||
} else {
|
|
||||||
system.write(b, offset, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (file != null) {
|
|
||||||
try {
|
|
||||||
file.write(b, offset, length);
|
|
||||||
file.flush();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
file = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
150
app/src/processing/app/EditorConsoleStream.java
Normal file
150
app/src/processing/app/EditorConsoleStream.java
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
package processing.app;
|
||||||
|
|
||||||
|
import static processing.app.I18n._;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
|
||||||
|
class EditorConsoleStream extends OutputStream {
|
||||||
|
static File tempFolder;
|
||||||
|
static File outFile;
|
||||||
|
static File errFile;
|
||||||
|
|
||||||
|
static EditorConsole currentConsole;
|
||||||
|
|
||||||
|
static OutputStream stderrFile;
|
||||||
|
static OutputStream stdoutFile;
|
||||||
|
static PrintStream consoleErr;
|
||||||
|
static PrintStream consoleOut;
|
||||||
|
static public PrintStream systemErr;
|
||||||
|
static public PrintStream systemOut;
|
||||||
|
|
||||||
|
public static void init() {
|
||||||
|
if (systemOut == null) {
|
||||||
|
systemOut = System.out;
|
||||||
|
systemErr = System.err;
|
||||||
|
|
||||||
|
// Create a temporary folder which will have a randomized name. Has to
|
||||||
|
// be randomized otherwise another instance of Processing (or one of its
|
||||||
|
// sister IDEs) might collide with the file causing permissions problems.
|
||||||
|
// The files and folders are not deleted on exit because they may be
|
||||||
|
// needed for debugging or bug reporting.
|
||||||
|
tempFolder = Base.createTempFolder("console");
|
||||||
|
tempFolder.deleteOnExit();
|
||||||
|
try {
|
||||||
|
String outFileName = Preferences.get("console.output.file");
|
||||||
|
if (outFileName != null) {
|
||||||
|
outFile = new File(tempFolder, outFileName);
|
||||||
|
outFile.deleteOnExit();
|
||||||
|
stdoutFile = new FileOutputStream(outFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
String errFileName = Preferences.get("console.error.file");
|
||||||
|
if (errFileName != null) {
|
||||||
|
errFile = new File(tempFolder, errFileName);
|
||||||
|
errFile.deleteOnExit();
|
||||||
|
stderrFile = new FileOutputStream(errFile);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
Base.showWarning(_("Console Error"),
|
||||||
|
_("A problem occurred while trying to open the\nfiles used to store the console output."),
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
consoleOut = new PrintStream(new EditorConsoleStream(false));
|
||||||
|
consoleErr = new PrintStream(new EditorConsoleStream(true));
|
||||||
|
|
||||||
|
if (Preferences.getBoolean("console")) {
|
||||||
|
try {
|
||||||
|
System.setOut(consoleOut);
|
||||||
|
System.setErr(consoleErr);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace(systemOut);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close the streams so that the temporary files can be deleted.
|
||||||
|
* <p/>
|
||||||
|
* File.deleteOnExit() cannot be used because the stdout and stderr files are
|
||||||
|
* inside a folder, and have to be deleted before the folder itself is
|
||||||
|
* deleted, which can't be guaranteed when using the deleteOnExit() method.
|
||||||
|
*/
|
||||||
|
public static void quit() {
|
||||||
|
// replace original streams to remove references to console's streams
|
||||||
|
System.setOut(systemOut);
|
||||||
|
System.setErr(systemErr);
|
||||||
|
|
||||||
|
// close the PrintStream
|
||||||
|
consoleOut.close();
|
||||||
|
consoleErr.close();
|
||||||
|
|
||||||
|
// also have to close the original FileOutputStream
|
||||||
|
// otherwise it won't be shut down completely
|
||||||
|
try {
|
||||||
|
stdoutFile.close();
|
||||||
|
stderrFile.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
outFile.delete();
|
||||||
|
errFile.delete();
|
||||||
|
tempFolder.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
final boolean err; // whether stderr or stdout
|
||||||
|
PrintStream system;
|
||||||
|
OutputStream file;
|
||||||
|
|
||||||
|
public EditorConsoleStream(boolean _err) {
|
||||||
|
err = _err;
|
||||||
|
if (err) {
|
||||||
|
system = systemErr;
|
||||||
|
file = stderrFile;
|
||||||
|
} else {
|
||||||
|
system = systemOut;
|
||||||
|
file = stdoutFile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void flush() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void write(int b) {
|
||||||
|
write(new byte[] { (byte) b });
|
||||||
|
}
|
||||||
|
|
||||||
|
public void write(byte b[]) { // appears never to be used
|
||||||
|
write(b, 0, b.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void write(byte b[], int offset, int length) {
|
||||||
|
if (currentConsole != null)
|
||||||
|
currentConsole.appendText(new String(b, offset, length), err);
|
||||||
|
|
||||||
|
system.write(b, offset, length);
|
||||||
|
|
||||||
|
if (file != null) {
|
||||||
|
try {
|
||||||
|
file.write(b, offset, length);
|
||||||
|
file.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
file = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static public void setCurrent(EditorConsole console) {
|
||||||
|
currentConsole = console;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -194,7 +194,7 @@ public class Sketch {
|
|||||||
code[codeCount++] =
|
code[codeCount++] =
|
||||||
new SketchCode(new File(folder, filename));
|
new SketchCode(new File(folder, filename));
|
||||||
} else {
|
} else {
|
||||||
editor.console.message(I18n.format("File name {0} is invalid: ignored", filename), true, false);
|
System.err.println(I18n.format("File name {0} is invalid: ignored", filename));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user