package examples.applets.ping;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.applet.*;
import java.util.*;

/**
 * This class allows us to (optionally) re-direct
 * a stream to a TextArea.
 * Useful for debugging, since WinMain apps don't have
 * a console.
 *
 * @author Copyright (c) 1997-2000 by BEA Systems, Inc. All Rights Reserved.
 */
/* stdout, stderr in a text frame */
public class TextOut extends PrintStream {
    
    static final String nl = "\n";

    TextArea t;

    void newLine() {
	write(nl);
    }

    private static final int MAX_CHARS = 10000;

    private void write(String s) {
	if ((t.getText().length() + s.length()) >= MAX_CHARS) {
	    t.setText("****<truncated buffer>***\n");
	}
	t.appendText(s);
    }

    public static void main(String[] a) throws Exception {
	(new TOFrame("Stdout/Stderr")).run();
    }

    public /**/ TextOut(TextArea t) {
	super(null);
	this.t = t;
    }

    public void write(int b) {
	write(String.valueOf((char)b));
    }

    public void write(byte[] buf, int off, int len) {
	for (int i = off; i < len; ++i) {
	    write(buf[i]);
	}
    }

    public void print(boolean b) {
	write(b ? "true" : "false");
    }

    public void print(char c) {
	write(String.valueOf(c));
    }

    public void print(int i) {
	write(String.valueOf(i));
    }
    public void print(long l) {
	write(String.valueOf(l));
    }
    public void print(float f) {
	write(String.valueOf(f));
    }
    public void print(double d) {
	write(String.valueOf(d));
    }
    public void print(char s[]) {
      //	for (int i = 0; i < s.length; i++) 
      //	    write(s[i]);
      print(new String(s));
    }
    
    public void print(String s) {
	if (s == null) {
	    s = "null";
	}
	write(s);
    }
    public void print(Object obj) {
	write(String.valueOf(obj));
    }
    public void println() {
	write("\n");
    }

    public void println(boolean x) {
	synchronized (this) {
	    print(x);
	    newLine();
	}
    }

    /**
     * Print a character, and then finish the line.
     *
     * @see #print(char)
     */
    public void println(char x) {
	synchronized (this) {
	    print(x);
	    newLine();
	}
    }

    /**
     * Print an integer, and then finish the line.
     *
     * @see #print(int)
     */
    public void println(int x) {
	synchronized (this) {
	    print(x);
	    newLine();
	}
    }

    /**
     * Print a long, and then finish the line.
     *
     * @see #print(long)
     */
    public void println(long x) {
	synchronized (this) {
	    print(x);
	    newLine();
	}
    }

    /**
     * Print a float, and then finish the line.
     *
     * @see #print(float)
     */
    public void println(float x) {
	synchronized (this) {
	    print(x);
	    newLine();
	}
    }

    /**
     * Print a double, and then finish the line.
     *
     * @see #print(double)
     */
    public void println(double x) {
	synchronized (this) {
	    print(x);
	    newLine();
	}
    }

    /**
     * Print an array of characters, and then finish the line.
     *
     * @see #print(char[])
     */
    public void println(char x[]) {
	synchronized (this) {
	    print(x);
	    newLine();
	}
    }

    /**
     * Print a String, and then finish the line.
     *
     * @see #print(String)
     */
    public void println(String x) {
	synchronized (this) {
	    print(x);
	    newLine();
	}
    }

    /**
     * Print an Object, and then finish the line.
     *
     * @see #print(Object)
     */
    public void println(Object x) {
	synchronized (this) {
	    print(x);
	    newLine();
	}
    }

}

class TOFrame extends Frame implements Runnable, java.awt.event.ActionListener {

    public TOFrame(String name) {
	super(name);
    }

    Button goAway;

    public void run() {
	setLayout(new BorderLayout());
	TextArea t = new TextArea("", 40, 12, TextArea.SCROLLBARS_BOTH);
	TextOut o = new TextOut(t);
	setSize(400, 700);
	add("North", t);
	Panel p = new Panel();
	goAway = new Button("GO AWAY");
	goAway.addActionListener(this);
	p.add(goAway);
	add("South", p);
	System.setOut(o);
	System.setErr(o);
	setVisible(true);
	o.println("HelloWorld!");
    }

    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == goAway) {
	    System.exit(0);
	}
    }
}








