package tool.editors;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorLauncher;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import tool.ToolPlugin;
import tool.ToolProjectSupport;
import tool.process.ProcessExitDetector;
import tool.process.ProcessListener;
import tool.repository.InputStreamHandler;
import tool.repository.OutputContainer;
public class WindowEditorLauncher implements IEditorLauncher {
private static final String WINDOW_CONSOLE_NAME = "Tool Window Editor";
private Process editorProcess;
protected MessageConsole windowEditorConsole;
protected MessageConsoleStream windowEditorStream;
@Override
public void open(IPath path) {
IWorkbenchWindow window=PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkspace workspace = ResourcesPlugin.getWorkspace();
final IFile windowFile = workspace.getRoot().getFileForLocation(path);
IProject project = windowFile.getProject();
Shell activeShell = window.getShell();
String osName = System.getProperty("os.name");
if (!osName.startsWith("Windows")){ // Only run it on windows
MessageDialog.openInformation(activeShell, "Cannot Run", "The Tool Window Editor cannot run on this Operating System");
return;
}
try {
File reposDir = ToolProjectSupport.getInterfaceDir();
this.windowEditorConsole = ToolPlugin.findConsole(WINDOW_CONSOLE_NAME);
if (this.windowEditorConsole != null)
windowEditorStream = windowEditorConsole.newMessageStream();
String forteRoot = ToolProjectSupport.getForteRoot();//project.getPersistentProperty(ToolProjectSupport.forteRootQualifiedName);
String logFlags = project.getPersistentProperty(ToolProjectSupport.loggerQualifiedName);
IPath ftExecPath = new Path(forteRoot);
ftExecPath = ftExecPath.append("install");
ftExecPath = ftExecPath.append("bin");
ftExecPath = ftExecPath.append("ftexec.exe");
IPath interfacePath = new Path(reposDir.getAbsolutePath());
interfacePath = interfacePath.append("formed0");
ProcessBuilder pb = new ProcessBuilder(ftExecPath.toOSString(),
"-fcons",
"-fnict",
"-fs",
"-fi",
"bt:"+ interfacePath.toOSString(),
"-win",
path.toOSString());
Map<String, String> env = pb.environment();
env.put("FORTE_ROOT", forteRoot);
/*
* TODO may need to add cfg:os:21
* this causes the logmgr to flush all
* writes immediately
*/
env.put("FORTE_LOGGER_SETUP", logFlags);
this.editorProcess = pb.start();
// listen for the process exit so we can refresh the resource
ProcessExitDetector processExitDetector = new ProcessExitDetector(this.editorProcess);
processExitDetector.addProcessListener(new ProcessListener() {
public void processFinished(Process process) {
try {
windowFile.refreshLocal(IResource.DEPTH_ZERO, null);
} catch (CoreException e) {
ToolPlugin.showError("Error refreshing fsw file", e);
}
}
});
processExitDetector.start();
// Start readers to consume output. It looks very innocuous with the output from the process
// being mapped to p.inputStream, but this is correct.
OutputContainer container = new OutputContainer(this.windowEditorStream);
new Thread(new InputStreamHandler(this.editorProcess.getInputStream(), container, false), "OutputFileHandler").start();
new Thread(new InputStreamHandler(this.editorProcess.getErrorStream(), container, true), "ErrorFileHandler").start();
} catch (IOException e) {
ToolPlugin.showError("Error running Tool Window Editor", e);
} catch (CoreException e) {
ToolPlugin.showError("Error running Tool Window Editor", e);
} catch (URISyntaxException e) {
ToolPlugin.showError("Error running Tool Window Editor", e);
}
}
public void finalize() {
if (this.editorProcess != null){
this.editorProcess.destroy();
}
}
}