// Copyright 1996, Marimba Inc. All Rights Reserved.


// @(#)TextOutputStream.java, 1.4, 12/19/96





package marimba.gui;





import java.io.*;


import java.util.*;


import java.awt.*;


import marimba.util.*;





/**


 * A stream for printing to a TextWidget. For example:


 * <pre>


 *	TextWidget widget = ...;


 *	PrintStream out = new FastOutputStream(new TextOutputStream(widget));


 *	out.println("Hello World");


 *	out.flush();


 * </pre>


 *


 * @author	Arthur van Hoff


 * @version 	1.4, 12/19/96


 */


public class TextOutputStream extends OutputStream implements TimerClient {


    TextWidget text;


    boolean notified;





    /**


     * Create an output stream which writes to a TextWidget.


     */


    public TextOutputStream(TextWidget text) {


	this.text = text;


    }





    /**


     * Write a byte.


     */


    public void write(int b) {


	text.append((char)b);


    }





    /**


     * Write a buffer.


     */


    public void write(byte buf[], int off, int len) {


	text.append(new String(buf, 0, off, len));


    }





    /**


     * Flush


     */


    public synchronized void flush() {


	if (!notified) {


	    notified = true;


	    Timer.master.add(this, 500, null);


	}


    }





    /**


     * Focus on the end of the text.


     */


    public synchronized long tick(long tm, Object arg) {


	notified = false;


	text.focus(text.getLength());


	return -1;


    }


}


