// TestAntWorker.java
//
// Created: November 2005
//
// This file is a component of the BioMoby project.
// Copyright Martin Senger (martin.senger@gmail.com).
//

import org.biomoby.service.dashboard.SimpleAnt;
import org.biomoby.service.dashboard.ExitSecurityManager;
import org.biomoby.service.dashboard.CommonConsole;

import org.tulsoft.tools.gui.SwingUtils;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ExitException;
import org.apache.tools.ant.NoBannerLogger;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import java.util.Properties;
import java.io.PrintStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;

/**
 * Work in progress...
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: TestAntWorker.java,v 1.5 2005/12/20 20:00:41 senger Exp $
 */
public class TestAntWorker {

    private static org.apache.commons.logging.Log log =
       org.apache.commons.logging.LogFactory.getLog (TestAntWorker.class);

    public void work (String[] args)
	throws BuildException {

 	Properties props = new Properties();
	SimpleAnt ant = new SimpleAnt();
	NoBannerLogger logger = new NoBannerLogger();
	logger.setOutputPrintStream (new PrintStream (new ConsoleStream (false)));
	logger.setErrorPrintStream (new PrintStream (new ConsoleStream (false)));
 	ant.addBuildListener (logger);
// 	ant.setMsgOutputLevel (Project.MSG_VERBOSE);
	ant.startAnt (args, props);
    }

    private CommonConsole console;

    /**************************************************************************
     * Create and show a console.
     **************************************************************************/
    public void show() {
	console = new CommonConsole();
	console.setAppendMode (true);


  	JFrame frame = SwingUtils.createMainFrame (console, "Testing SimpleAnt");
   	SwingUtils.showMainFrame (frame, 500, 300);
    }


    class ConsoleStream extends OutputStream {
	private boolean isErrorStream = false;
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

	public ConsoleStream (boolean isErrorStream) {
	    super();
	    this.isErrorStream = isErrorStream;
	    buffer = new ByteArrayOutputStream();
	}

	public void write (int b)
	    throws IOException {
	    ByteArrayOutputStream buf = new ByteArrayOutputStream();
	    buf.write (b);
	    console.setText ( buf.toString() );
	}
    }

    public static void main (String[] args) {

	// set exit manager to prevent System.exit()
	ExitSecurityManager exitman = ExitSecurityManager.createAndInstall();

	final TestAntWorker worker = new TestAntWorker();

	try {
	    // display a console
	    SwingUtilities.invokeAndWait (new Runnable() {
		    public void run() {
			worker.show();
		    }
		});

	    // call ant
	    worker.work (args);

	} catch (ExitException e) {
	    log.warn ("Ant tried to use System.exit() with exit code " + e.getStatus());

	} catch (BuildException e) {
	    log.info ("Build error already reported, right?");

	} catch (Exception e) {
	    log.error ("Error: " + e.toString());
	}

	// enable exit and exit
	exitman.setExitForbidden (false);
// 	System.exit (0);
    }
}
