package istlab.KisoJikken;
import java.awt.Point;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Stack;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class JTAConsole extends JTextArea implements Runnable, WindowListener {
// static JTAConsole lastWindow;
// static Point lastActivePoint;
static Stack<JTAConsole> winStack = new Stack<JTAConsole>();
JFrame frame;
Process process;
Thread thread;
InputStream instream;
BufferedReader reader;
// ProcessBuilder pb;
// public JTAConsole(ProcessBuilder prob) {
public JTAConsole(String title) {
// pb = prob;
frame = new JFrame(title);
frame.getContentPane().add(new JScrollPane(this));
frame.setSize(500, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.addWindowListener(this);
if (winStack.size()>0){
JTAConsole last = winStack.peek();
if (last.isVisible()){
Point lastFrameP = last.frame.getLocationOnScreen();
frame.setLocation(lastFrameP.x+470, lastFrameP.y);
}
}
// if (lastActivePoint != null){
// frame.setLocation(lastActivePoint.x+500, lastActivePoint.y);
// } else if (lastWindow != null && lastWindow.isVisible()){
// Point lastFrameP = lastWindow.frame.getLocationOnScreen();
// frame.setLocation(lastFrameP.x+500, lastFrameP.y);
// }
// lastWindow = this;
winStack.push(this);
}
public void Systemoutprintln(String s) {
append(s + "\n");
int len = getDocument().getLength();
setCaretPosition(len);
}
public void startBR(Process proc) {
process = proc;
reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
try {
while ((line = reader.readLine()) != null) {
Systemoutprintln(line);
}
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Systemoutprintln("=== end ===");
// thread = new Thread(this);
// thread.start();
}
@Override
public void run() {
String line;
try {
while ((line = reader.readLine()) != null) {
Systemoutprintln(line);
}
} catch (IOException e) {
e.printStackTrace();
}
Systemoutprintln("=== end ===");
}
public static void main(String[] arg) {
ProcessBuilder processBuilder = new ProcessBuilder("ping", "localhost");
Process process = null;
try {
process = processBuilder.start();
} catch (IOException e) {
e.printStackTrace();
}
JTAConsole con = new JTAConsole("Demo");
con.startBR(process);
}
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
process.destroy();
winStack.remove(this);
setVisible(false);
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
// JTAConsole.lastActivePoint = frame.getLocationOnScreen();
}
@Override
public void windowDeactivated(WindowEvent e) {
}
}