eclipse plugin: show a message if the tool returned non-zero value

This commit is contained in:
Timotei Dolean 2010-05-20 20:35:26 +00:00
parent 4a082c3004
commit 11ef1a535b
2 changed files with 63 additions and 17 deletions

View File

@ -8,6 +8,9 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.IWorkbenchWindow;
/** /**
* @author Timotei Dolean * @author Timotei Dolean
* *
@ -58,9 +61,19 @@ public class ExternalToolInvoker {
bufferedReaderOutput_ = new BufferedReader(new InputStreamReader(process_.getInputStream())); bufferedReaderOutput_ = new BufferedReader(new InputStreamReader(process_.getInputStream()));
bufferedReaderError_ = new BufferedReader(new InputStreamReader(process_.getErrorStream())); bufferedReaderError_ = new BufferedReader(new InputStreamReader(process_.getErrorStream()));
} }
public void waitFor() throws InterruptedException /**
* Waits for the current tool, and returns the return value
* @return the return value of the tool
*/
public int waitFor()
{ {
process_.waitFor(); try{
return process_.waitFor();
}
catch (Exception e) {
e.printStackTrace();
return -1;
}
} }
public String readOutputLine() public String readOutputLine()
{ {
@ -119,30 +132,33 @@ public class ExternalToolInvoker {
* @param waitFor true to wait till the program ends and show the output * @param waitFor true to wait till the program ends and show the output
* at the end of the program or false to show it as it arrises * at the end of the program or false to show it as it arrises
* @param useThread true to launch the tool on a separate thread * @param useThread true to launch the tool on a separate thread
* @param workbenchWindow the workbench window used to show messages
* (if null no messages will be triggered)
* @return * @return
*/ */
public static boolean launchTool(String fileName, Collection<String> args, boolean showOutput, public static boolean launchTool(final String fileName,final Collection<String> args,final boolean showOutput,
boolean waitFor,boolean useThread) final boolean waitFor,final boolean useThread,final IWorkbenchWindow workbenchWindow)
{ {
final boolean wait = waitFor; // we need a new thread so we won't block the caller
final boolean show = showOutput;
final boolean thread = useThread;
final String file= fileName;
final Collection<String> arguments = args;
Thread launcherThread = new Thread(new Runnable() { Thread launcherThread = new Thread(new Runnable() {
@Override @Override
public void run() public void run()
{ {
try{ try{
final ExternalToolInvoker toolInvoker = new ExternalToolInvoker(file, arguments, thread); final ExternalToolInvoker toolInvoker = new ExternalToolInvoker(fileName, args, useThread);
toolInvoker.run(); toolInvoker.run();
if (wait)
toolInvoker.waitFor();
if (show) if (waitFor)
{ {
if (wait) if (toolInvoker.waitFor() != 0 && workbenchWindow != null)
{
showMessageBox(workbenchWindow, "The tool returned a non-zero value.");
}
}
if (showOutput)
{
if (waitFor)
{ {
String line=""; String line="";
while((line = toolInvoker.readOutputLine()) != null) while((line = toolInvoker.readOutputLine()) != null)
@ -156,7 +172,6 @@ public class ExternalToolInvoker {
System.out.println("tool exited."); System.out.println("tool exited.");
} }
else { else {
// we need a new thread so we won't block the caller
Thread outputStreamThread = new Thread(new Runnable() { Thread outputStreamThread = new Thread(new Runnable() {
@Override @Override
public void run() public void run()
@ -178,6 +193,11 @@ public class ExternalToolInvoker {
System.out.println(line); System.out.println(line);
} }
System.out.println("tool exited."); System.out.println("tool exited.");
if (toolInvoker.waitFor() != 0)
{
showMessageBox(workbenchWindow, "The tool returned a non-zero value.");
}
} }
}); });
outputStreamThread.start(); outputStreamThread.start();
@ -193,4 +213,29 @@ public class ExternalToolInvoker {
launcherThread.start(); launcherThread.start();
return true; return true;
} }
/**
* Shows a message box with the specified message (thread-safe)
* @param window the window where to show the message box
* @param message the message to print
*/
private static void showMessageBox(final IWorkbenchWindow window,final String message)
{
try
{
window.getShell().getDisplay().asyncExec(new Runnable() {
@Override
public void run()
{
MessageBox box = new MessageBox(window.getShell());
box.setMessage(message);
box.open();
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
} }

View File

@ -36,7 +36,8 @@ public class OpenEditorHandler extends AbstractHandler
} }
System.out.printf("Running: [%s] with args: %s\n", editorPath, getLaunchEditorArguments("", workingDir)); System.out.printf("Running: [%s] with args: %s\n", editorPath, getLaunchEditorArguments("", workingDir));
ExternalToolInvoker.launchTool(editorPath, getLaunchEditorArguments("", workingDir),true,false, true); ExternalToolInvoker.launchTool(editorPath, getLaunchEditorArguments("", workingDir),true,false, true,
window);
return null; return null;
} }