/*
* Copyright (C) 2012 Dr. John Lindsay <jlindsay@uoguelph.ca>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package whitebox.utilities;
import java.awt.*;
//import java.awt.event.*;
import java.io.*;
import javax.swing.*;
//import javax.swing.event.*;
//import javax.swing.text.*;
/**
*
* @author Dr. John Lindsay email: jlindsay@uoguelph.ca
*/
public class Console extends JPanel {
PipedInputStream piOut;
PipedInputStream piErr;
PipedOutputStream poOut;
PipedOutputStream poErr;
public JTextArea textArea = new JTextArea();
public Console() throws IOException {
// Set up System.out
piOut = new PipedInputStream();
poOut = new PipedOutputStream(piOut);
System.setOut(new PrintStream(poOut, true));
// Set up System.err
piErr = new PipedInputStream();
poErr = new PipedOutputStream(piErr);
System.setErr(new PrintStream(poErr, true));
// Add a scrolling text area
textArea.setEditable(false);
textArea.setRows(20);
textArea.setColumns(50);
this.add(new JScrollPane(textArea), BorderLayout.CENTER);
//pack();
setVisible(true);
startThreads();
// // Create reader threads
// new ReaderThread(piOut).start();
// new ReaderThread(piErr).start();
}
private void startThreads() {
// Create reader threads
new ReaderThread(piOut).start();
new ReaderThread(piErr).start();
}
class ReaderThread extends Thread {
PipedInputStream pi;
ReaderThread(PipedInputStream pi) {
this.pi = pi;
}
@Override
public void run() {
final byte[] buf = new byte[1024];
try {
while (true) {
final int len = pi.read(buf);
if (len == -1) {
break;
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
textArea.append(new String(buf, 0, len));
// Make sure the last line is always visible
textArea.setCaretPosition(textArea.getDocument().getLength());
// Keep the text area down to a certain character size
int idealSize = 1000;
int maxExcess = 500;
int excess = textArea.getDocument().getLength() - idealSize;
if (excess >= maxExcess) {
textArea.replaceRange("", 0, excess);
}
}
});
}
} catch (IOException e) {
}
}
}
}