package rfplugin.action;

import java.io.IOException;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.console.MessageConsoleStream;

import rfplugin.Activator;
import rfplugin.model.Test;
import rfplugin.views.ConsoleManager;

public class StopTestAction extends Action{
	TreeViewer treeViewer;
	Action runTestAction;
	Image stopImage = Activator.getImageDescriptor("icons/stop.gif")
			.createImage();
	ImageDescriptor stopImageDescriptor = ImageDescriptor
			.createFromImage(stopImage);
	
	public StopTestAction(TreeViewer treeViewer){
		this.treeViewer = treeViewer;
		this.setImageDescriptor(stopImageDescriptor);
		this.setEnabled(false);
	}
	
	public void setRunTestAction(Action runTestAction){
		this.runTestAction = runTestAction;
	}
	
	public void run() {
		try {
			ISelection selection = treeViewer.getSelection();
			Object obj = ((IStructuredSelection) selection)
					.getFirstElement();

			if (obj instanceof Test) {
				((Test) obj).setStatus("Not run");
				treeViewer.refresh();
				treeViewer.expandAll();

				Runtime.getRuntime().exec("taskkill /F /IM python.exe");
				MessageConsoleStream out = ConsoleManager
						.getMessageConsoleStream("Console");
				out.println("Test Stop");
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		runTestAction.setEnabled(true);
		this.setEnabled(false);
	}
}

@@ -528,16 +528,17 @@
 s Action
+ 
 %7B%0D%0A%09Tree
@@ -757,17 +757,16 @@
 mage);%0D%0A
-%09
 %0D%0A%09publi
@@ -804,16 +804,17 @@
 eViewer)
+ 
 %7B%0D%0A%09%09thi
@@ -917,25 +917,24 @@
 alse);%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09public vo
@@ -974,16 +974,17 @@
 tAction)
+ 
 %7B%0D%0A%09%09thi
@@ -1021,17 +1021,16 @@
 n;%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09publi
@@ -1158,23 +1158,16 @@
 lection)
-%0D%0A%09%09%09%09%09
 .getFirs
@@ -1212,16 +1212,75 @@
 est) %7B%0D%0A
+%09%09%09%09if (((Test) obj).getStatus().equals(%22Actual run%22)) %7B%0D%0A%09
 %09%09%09%09((Te
@@ -1307,24 +1307,25 @@
 Not run%22);%0D%0A
+%09
 %09%09%09%09treeView
@@ -1335,24 +1335,25 @@
 refresh();%0D%0A
+%09
 %09%09%09%09treeView
@@ -1375,16 +1375,17 @@
 %0D%0A%0D%0A%09%09%09%09
+%09
 Runtime.
@@ -1438,16 +1438,17 @@
 );%0D%0A%09%09%09%09
+%09
 MessageC
@@ -1482,16 +1482,17 @@
 anager%0D%0A
+%09
 %09%09%09%09%09%09.g
@@ -1531,16 +1531,17 @@
 );%0D%0A%09%09%09%09
+%09
 out.prin
@@ -1559,16 +1559,98 @@
 top%22);%0D%0A
+%09%09%09%09%09%0D%0A%09%09%09%09%09runTestAction.setEnabled(true);%0D%0A%09%09%09%09%09this.setEnabled(false);%0D%0A%09%09%09%09%7D%0D%0A
 %09%09%09%7D%0D%0A%0D%0A
@@ -1712,68 +1712,8 @@
 %09%7D%0D%0A
-%09%09runTestAction.setEnabled(true);%0D%0A%09%09this.setEnabled(false);
 %0D%0A%09%7D

fix bug with stop test