package org.jboss.reddeer.eclipse.wst.server.ui;

import java.util.ArrayList;
import java.util.List;

import org.jboss.reddeer.common.logging.Logger;
import org.jboss.reddeer.eclipse.wst.server.ui.wizard.NewRuntimeWizardDialog;
import org.jboss.reddeer.swt.api.Table;
import org.jboss.reddeer.swt.condition.ShellWithTextIsActive;
import org.jboss.reddeer.swt.impl.button.PushButton;
import org.jboss.reddeer.swt.impl.table.DefaultTable;
import org.jboss.reddeer.workbench.preference.WorkbenchPreferencePage;

/**
 * 
 * Preference page for managing server runtime environments.  
 * 
 * @author Lucia Jelinkova
 *
 */
public class RuntimePreferencePage extends WorkbenchPreferencePage {

	public static final String PAGE_NAME = "Runtime Environments";

	private static final Logger log = Logger.getLogger(RuntimePreferencePage.class);
	
	/**
	 * Constructs the preference page with "Server" > {@value #PAGE_NAME}.
	 */
	public RuntimePreferencePage() {
		super("Server", PAGE_NAME);
	}
	
	/**
	 * Return list of server runtimes.
	 * 
	 * @return List of server runtimes
	 */
	public List<Runtime> getServerRuntimes(){
		List<Runtime> runtimes = new ArrayList<Runtime>();
		
		Table table = new DefaultTable();
		int rows = table.rowCount();
		
		for (int i = 0; i < rows; i++){
			Runtime runtime = new Runtime();
			runtime.setName(table.getItem(i).getText());
			runtime.setType(table.getItem(i).getText(1));
			runtimes.add(runtime);
		}
		return runtimes;
	}
	
	/**
	 * Removes a given runtime.
	 * 
	 * @param runtime Runtime
	 */
	public void removeRuntime(Runtime runtime){
		log.info("Removing runtime " + runtime);
		selectRuntime(runtime.getName());
		new PushButton("Remove").click();
		if(new ShellWithTextIsActive("Server").test()){
			new PushButton("OK").click();
		}
	}
	
	/**
	 * Removes all runtimes.
	 */
	public void removeAllRuntimes(){
		log.info("Removing all runtimes");
		for (Runtime runtime : getServerRuntimes()){
			removeRuntime(runtime);
		}
	}
	
	private void selectRuntime(String name){
		
		Table table = new DefaultTable();
		
		for (int i = 0; i < table.rowCount(); i++){
			if (table.getItem(i).getText().equals(name)){
				table.select(i);
			}
		}
	}
	
	/**
	 * Opens a wizard for adding new runtime.
	 * 
	 * @return Wizard for adding new runtime
	 */
	public NewRuntimeWizardDialog addRuntime(){
		log.info("Adding new runtime");
		new PushButton("Add...").click();
		return new NewRuntimeWizardDialog();
	}
}

@@ -143,16 +143,82 @@
 Logger;%0A
+import org.jboss.reddeer.eclipse.exception.EclipseLayerException;%0A
 import o
@@ -2116,24 +2116,61 @@
 tTable();%0A%09%09
+log.debug(%22Selecting runtime %22+name);
 %0A%09%09for (int 
@@ -2211,12 +2211,29 @@
 %0A%09%09%09
-if (
+String runtimeName = 
 tabl
@@ -2258,53 +2258,254 @@
 xt()
-.equals(name))%7B%0A%09%09%09%09table.select(i);%0A%09%09%09%7D%0A%09%09%7D
+;%0A%09%09%09log.debug(runtimeName+%22 was found%22);%0A%09%09%09if (runtimeName.equals(name))%7B%0A%09%09%09%09log.debug(runtimeName+%22 matched %22+name+%22! Selecting...%22);%0A%09%09%09%09table.select(i);%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09throw new EclipseLayerException(%22Unable to find runtime %22+name);
 %0A%09%7D%0A

Throw exception if runtime could not be found. #905