package utilities;

import javax.swing.text.JTextComponent;
import java.io.*;

    /** Provide PrintWriter functionality into a JTextComponent.
     */
public class TextComponentPrintWriter extends PrintWriter {
        /** Direct print and println text to the given text component.
         */
    public TextComponentPrintWriter(JTextComponent t){
        // Use a StringWriter to collect all printed text.
        // Autoflush.
        super(new StringWriter(),true);
        component = t;
    }

        /** Flush pending output.
         */
    public void flush(){
        super.flush();
        StringWriter s = (StringWriter) out;
        getComponent().setText(s.toString());
    }

        /** Append a newline to the displayed text.
         */
    public void println(){
        super.println();
        flush();
    }

        /** Display the given text, with an appended newline.
         */
    public void println(String s){
        super.println(s);
        flush();
    }

    private JTextComponent getComponent(){
        return component;
    }

    final private JTextComponent component;
}
