package wedgi;

import java.awt.*;
import java.awt.event.*;
import java.io.*;


/**
  * this is a Wedgi text area, it supports adding text from a Reader
  * or an InputStream in addition to normal TextArea functionality
  *
  * @author Owen Astrachan
  * @see Reader
  * @see InputStream
  */ 
public class WedgiText extends TextArea
{
    /**
      * bind the constructed object to a controller
      * @param con the controller through which communication to GUI happens
      */
    public WedgiText(Controller con)
    {
	super(30,80);
	myController = con;
    }

    /**
      * add by replacement all the text from a reader,
      * if the reader isn't buffered
      * a BufferedReader is constructed around it
      * 
      * @param r is the reader-source for text that replaces all text 
      */
    public void addText (Reader r)
    {
	BufferedReader br = null;

	try
	{
	    br = (BufferedReader)r;
	}
	catch (ClassCastException e)
	{
	    br = new BufferedReader(r);
	}

	setText("");
	try{
	    String s;
	    while ((s = br.readLine()) != null)
	    {
		append(s+"\n");
	    }		    	    
	}
	catch (IOException e)
        {}
    }

    /**
      * add by replacement all the text from an InputStream
      * this function creates a BufferedReader around the stream
      * and calls the Reader-based addText
      * 
      * @param in is the input stream-source of text
      */
    public void addText (InputStream in)
    {
	addText(new BufferedReader(new InputStreamReader(in)));
    }

    private Controller   myController;
}
