package org.mwc.debrief.dis.runner;

import java.io.File;
import java.io.IOException;

import org.mwc.debrief.dis.ui.preferences.DebriefDISSimulatorPrefs;

public class SimulationRunner
{

  final private DebriefDISSimulatorPrefs _prefs;
  private Process _process;

  public SimulationRunner(DebriefDISSimulatorPrefs simPrefs)
  {
    _prefs = simPrefs;
  }

  public void run(final String inFile)
  {
    final File input = new File(inFile);
    final File parent = input.getParentFile();

    if (!input.exists())
    {
      System.err.println("Input file not found");
    }
    else if (!parent.isDirectory())
    {
      System.err.println("Unable to find parent directory");
    }
    else
    {
      final String[] exeCmd = new String[]
      {_prefs.getExePath(), inFile};

      // fire up the processs
      try
      {
        _process = Runtime.getRuntime().exec(exeCmd, null, parent);
      }
      catch (IOException e1)
      {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
    }
  }

  public void stop()
  {
    if (_process != null)
    {
      _process.destroy();
      _process = null;
    }
  }

}

@@ -30,16 +30,47 @@
 unner;%0A%0A
+import java.io.BufferedReader;%0A
 import j
@@ -110,16 +110,314 @@
 eption;%0A
+import java.io.InputStreamReader;%0A%0Aimport org.eclipse.core.runtime.Status;%0Aimport org.eclipse.ui.console.ConsolePlugin;%0Aimport org.eclipse.ui.console.IConsole;%0Aimport org.eclipse.ui.console.MessageConsole;%0Aimport org.eclipse.ui.console.MessageConsoleStream;%0Aimport org.mwc.debrief.dis.DisActivator;
 %0Aimport 
@@ -587,16 +587,122 @@
 process;
+%0A  private Thread oThread;%0A%0A  private MessageConsole disConsole;%0A  private MessageConsoleStream msgStream;
 %0A%0A  publ
@@ -1264,212 +1264,2204 @@
   %7B%0A
-        _process = Runtime.getRuntime().exec(exeCmd, null, parent);%0A      %7D%0A      catch (IOException e1)%0A      %7B%0A        // TODO Auto-generated catch block%0A        e1.printStackTrace();%0A      %7D%0A    %7D%0A  %7D%0A
+%0A        // get ready to write to the console%0A        if (disConsole == null)%0A        %7B%0A          disConsole =%0A              new MessageConsole(%22DIS Output%22, DisActivator%0A                  .imageDescriptorFromPlugin(%22org.mwc.debrief.dis%22,%0A                      %22icons/16px/dis_icon.png%22));%0A          ConsolePlugin.getDefault().getConsoleManager().addConsoles(%0A              new IConsole%5B%5D%0A              %7BdisConsole%7D);%0A          msgStream = disConsole.newMessageStream();%0A        %7D%0A        else%0A        %7B%0A          disConsole.clearConsole();%0A        %7D%0A%0A        _process = Runtime.getRuntime().exec(exeCmd, null, parent);%0A%0A        final BufferedReader iReader =%0A            new BufferedReader(new InputStreamReader(_process.getInputStream()));%0A%0A        Runnable outputter = new Runnable()%0A        %7B%0A          boolean _terminated = false;%0A%0A          @Override%0A          public void run()%0A          %7B%0A            while (!_terminated)%0A            %7B%0A              String line;%0A              try%0A              %7B%0A                line = iReader.readLine();%0A                if (line != null)%0A                %7B%0A                  System.err.println(line);%0A                  msgStream.println(line);%0A                %7D%0A%0A                // has it completed?%0A                try%0A                %7B%0A                  _process.exitValue();%0A                  _terminated = true;%0A                  System.err.println(%22TERMINATING OUTPUTTER%22);%0A                %7D%0A                catch (Exception e)%0A                %7B%0A                %7D%0A%0A              %7D%0A              catch (IOException e1)%0A              %7B%0A                e1.printStackTrace();%0A              %7D%0A            %7D%0A          %7D%0A        %7D;%0A%0A        oThread = new Thread(outputter);%0A        oThread.start();%0A%0A      %7D%0A      catch (IOException e1)%0A      %7B%0A        final String msg;%0A        if (e1.getMessage().contains(%22Permission denied%22))%0A        %7B%0A          msg =%0A              %22Permission denied. If on Unix the script requires the executable bit to be set%22;%0A        %7D%0A        else%0A        %7B%0A          msg = %22IO Exception whilst starting simulator%22;%0A        %7D%0A        DisActivator.log(Status.ERROR, msg, e1);%0A      %7D%0A    %7D%0A  %7D%0A%0A  @SuppressWarnings(%22deprecation%22)
 %0A  p
@@ -3565,19 +3565,103 @@
 null;%0A    %7D%0A
+    if (oThread != null)%0A    %7B%0A      oThread.destroy();%0A      oThread = null;%0A    %7D%0A
   %7D%0A%0A%7D%0A

introduce ability to watch simulator output from console