import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.io.* ;

public class LogWin extends Thread implements WindowListener {
    private JFrame frame ;
    private JScrollPane scroll ;
    private JTextArea tarea ;
    private BufferedReader in ;
    private boolean flag = true ;

    public LogWin(InputStream input) {
	frame = new JFrame("Log");
	scroll = new JScrollPane();
	tarea = new JTextArea() ;
	tarea.setLocation(0,0);
	tarea.setRows(250) ;
	tarea.setEditable(false) ;
	scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED ) ;
	scroll.setViewportView(tarea);
	frame.getContentPane().setLayout(new GridLayout());
	frame.getContentPane().add(scroll, "scroll pane");
	frame.setSize(new Dimension(500,300)) ;
	frame.setVisible(true) ;
	in = new BufferedReader(new InputStreamReader(input));
    }

    public void run() {
	while(flag) {
	    try {
		if(in.ready()) {
                    tarea.append(in.readLine()) ;
		    tarea.append("\n") ;
                }
                else
                    sleep(250) ;
	    } 
	    catch (IOException e1) {
		tarea.append("IOException occured ! Can't append to Log window") ;
	    }
	    catch (InterruptedException e2) {
		tarea.append("Interrupted by an other thread !") ;
	    }
	}
	    
    }
    
    public void windowActivated(WindowEvent e) {
    }

    public void windowClosed(WindowEvent e) {
	this.stop() ; // arret du thread
	frame.dispose() ;
    }

    public void windowClosing(WindowEvent e) {
    }

    public void windowDeactivated(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }

    public void windowIconified(WindowEvent e) {
    }

    public void windowOpened(WindowEvent e) {
    }

    /*
      public static void main(String[] arg) {
      LogWin log = new LogWin(System.in) ;
      log.run() ;
      }
    */
}
