package ccw.core;

import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;

public class BotUtils {

	public final SWTWorkbenchBot bot;
	
	public BotUtils() throws Exception {
		bot = eclipseBot();
	}

	public SWTWorkbenchBot eclipseBot() {
		SWTWorkbenchBot bot = new SWTWorkbenchBot();
		return bot;
	}

	public BotUtils openJavaPerspective() {
		closeWelcome();
		bot.perspectiveByLabel("Java").activate();
		return this;
	}
	
	public BotUtils closeWelcome() {
		try {
			SWTBotView v = bot.viewByTitle("Welcome");
			if (v != null) {
				v.close();
			}
		} catch (Exception e) { 
			// Nevermind
		}
		return this;
		
	}
	public SWTBotMenu menu(String menu, String... subMenus) {
		SWTBotMenu ret = bot.menu(menu);
		for (String subMenu: subMenus) {
			ret = ret.menu(subMenu);
		}
		return ret;
	}

	public BotUtils activateShell(String shell) {
		SWTBotShell s = bot.shell(shell);
		s.activate();
		return this;
	}
	
	public BotUtils waitForWorkspace() throws Exception {
		// ensure that all queued workspace operations and locks are released
		ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
		public void run(IProgressMonitor monitor) throws CoreException {
					// nothing to do!
				}
			}, new NullProgressMonitor());
		return this;
	}
	

	public BotUtils createClojureProject(String projectName) throws Exception {
		menu("File", "New", "Project...")
			.click();
		return fillNewProject(bot, projectName);
	}

	/** Create new project in the workspace root folder */
	public BotUtils fillNewProject(SWTBot bot, String projectName) throws Exception {
		bot = activateShell("New Project").bot();
		bot.tree().expandNode("Clojure").select("Clojure Project");
		bot.button("Next >").click();
		bot.textWithLabel("Project name:").setText(projectName);
		bot.textWithId("location").setText(ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString());
		bot.button("Finish").click();
		waitForWorkspace();
		return this;
	}
	/** Test if a project exists by checking the Package Explorer View */
	public BotUtils assertProjectExists(String projectName) {
		SWTBotView packageExplorer = bot.viewByTitle("Package Explorer");
		SWTBotTree projectsTree = packageExplorer.bot().tree();
		projectsTree.expandNode(projectName);
		return this;
	}
	
	public SWTWorkbenchBot bot() {
		return bot;
	}
	
}

@@ -12,16 +12,54 @@
 .core;%0A%0A
+import static org.junit.Assert.fail;%0A%0A
 import o
@@ -465,24 +465,85 @@
 der.SWTBot;%0A
+import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;%0A
 import org.e
@@ -645,16 +645,73 @@
 tShell;%0A
+import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;%0A
 import o
@@ -2465,24 +2465,58 @@
 ;%0A%09%09
-bot.text
+SWTBotCheckBox sameLocation = bot.checkBox
 WithId(%22
 loca
@@ -2511,16 +2511,21 @@
 WithId(%22
+same-
 location
@@ -2530,84 +2530,537 @@
 on%22)
-.setText(ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString()
+;%0A%09%09SWTBotText location = bot.textWithId(%22location%22);%0A%09%09final String testLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString();%0A%09%09if (!testLocation.equals(location.getText())) %7B%0A%09%09%09if (!location.isEnabled()) %7B%0A%09%09%09%09if (sameLocation.isEnabled() && sameLocation.isChecked()) %7B%0A%09%09%09%09%09sameLocation.deselect(); // should have side effect of enabling location widget%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09fail(%22Location should only be disabled if sameLocation is enabled and checked%22);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09location.setText(testLocation
 );%0A%09

fix breaking test for project creation