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

/**
* Log handler handles all the request from the log menu
* extends Handler object so it can be loaded in JPE object
*/
public class LogHandler extends Handler {

	// new printstreams to handle the rerouted output/error
	PrintStream	lpso;
	PrintStream	lpse;
	boolean first=false;

	// Strings for the pulldown
	String StdOutString="StdOut";
	String StdErrString="StdErr";

	// swap file keeps pointer to the file that was open when the
	// user called the out/error window to support toggle function
	OpenFile swapFile;

    // Openfiles to store the data we get from the rerouted streams
	OpenFile stdOut;
	OpenFile stdErr;

	
	/**
	* create the log handler
	*/
	public LogHandler(JPE jpe) {
		super(jpe);
		name="Log";

		// create the Openfile that will hold our StdOut data
		stdOut=new OpenFile(StdOutString);
		stdOut.setLocked();

		// create a new stream for stdout
		Log2AreaStream laso=new Log2AreaStream(new ByteArrayOutputStream());
		// set the file in our our new stream so it knows where to leave its data
		laso.setFile(stdOut,this);
		// create a new printStream from our stream
		lpso=new PrintStream(laso);
		// ask the system/jvm to reroute stdout to our new stream
		System.setOut(lpso);

		// create the Openfile that will hold our StdErr data
		stdErr=new OpenFile(StdErrString);
		stdErr.setLocked();

		// create a new stream for stderr
		Log2AreaStream lase=new Log2AreaStream(new ByteArrayOutputStream());
		// set the file in our our new stream so it knows where to leave its data
		lase.setFile(stdErr,this);
		// create a new printStream from our stream
		lpse=new PrintStream(lase);
		// ask the system/jvm to reroute stderr to our new stream
		System.setErr(lpse);
	}

	/**
	*  called by superclass to create its menu
	*/
	public Menu createMenu() {
		Menu m = new Menu(name);
		m.add(new MenuItem(StdOutString,new MenuShortcut('D')));
		m.add(new MenuItem(StdErrString,new MenuShortcut('H')));
		m.addActionListener(this);        
		return(m);
	}


	/**
	*  AWT callback (implemented in Handler) for menu events
	*/
    public void actionPerformed(ActionEvent evt)	{
		String cmd = evt.getActionCommand();
		if (cmd.equals(StdOutString)) {
			doSwitchStdOut();
		} else if (cmd.equals(StdErrString)) {
			doSwitchStdErr();
		}
	}

	public void LogFileChanged(OpenFile file) {
		TextHandler txth=jpe.getTextHandler();		
		if (txth.getEditFile()==file && file==stdOut) {
			txth.reloadFile();
		}
	}

	/**
	* toggle between the StdOut buffer and the selected file
	*/
	public void doSwitchStdOut() {
		// get the text handler
		TextHandler txth=jpe.getTextHandler();		
		if (stdOut==txth.getEditFile()) {
			// switch to swap file since we where allready active
			txth.useFile(swapFile);
		} else {
			// switch to StdErr file
			if (stdErr!=txth.getEditFile()) swapFile=txth.getEditFile();
			txth.useFile(stdOut);
		}
	}

	/**
	* toggle between the StdErr buffer and the selected file
	*/
	public void doSwitchStdErr() {
		TextHandler txth=jpe.getTextHandler();
		if (stdErr==txth.getEditFile()) {
			// switch to swap file since we where allready active
			txth.useFile(swapFile);
		} else {
			// switch to StdErr file
			if (stdOut!=txth.getEditFile()) swapFile=txth.getEditFile();
			txth.useFile(stdErr);
		}
	}

	/**
	* clear both the stdout and stderror file
	*/
	public void clearLogs() {
		stdOut.setText("");
		stdErr.setText("");
	}

	/**
	* obtain the text from stderror (used by the compiler)
	*/
	public String getErrorText() {
		if (stdErr!=null) {
			return(stdErr.getText());
		} else {
			return(null);
		}
	}
}
