package org.aksw.limes.core.gui.util;

import java.util.List;

import org.testfx.api.FxRobot;

import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;

public class CustomGuiTest {
	
	public static void waitUntilNodeIsNotNull(String nodeId, int timeout){
		Node node = new FxRobot().lookup(nodeId).query();
		while(node == null && timeout != 0)
		{
		  try {
			  node = new FxRobot().lookup(nodeId).query();
			  timeout --;
			  Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
	}
	
	/**
	 * Uses FxRobot from TestFX to lookup the node 
	 * @param nodeId ID of the node
	 * @param timeout in seconds
	 */
	public static void waitUntilNodeIsVisible(String nodeId, int timeout){
		Node node = new FxRobot().lookup(nodeId).query();
		if(node == null){
			waitUntilNodeIsNotNull(nodeId, timeout);
		}
		node = new FxRobot().lookup(nodeId).query();
		while(!node.isVisible() && timeout != 0)
		{
		  try {
			  timeout --;
			  Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		
	}
	
	public static TableRow<?> getFirstRowOfTableView(String tableSelector) {
		TableView<?> tableView = new FxRobot().lookup(tableSelector).query();

	    List<Node> current = tableView.getChildrenUnmodifiable();
	    while (current.size() == 1) {
	        current = ((Parent) current.get(0)).getChildrenUnmodifiable();
	    }

	    current = ((Parent) current.get(1)).getChildrenUnmodifiable();
	    while (!(current.get(0) instanceof TableRow)) {
	        current = ((Parent) current.get(0)).getChildrenUnmodifiable();
	    }

	    Node node = current.get(0);
	    if (node instanceof TableRow) {
	        return (TableRow<?>) node;
	    } else {
	        throw new RuntimeException("Expected Group with only TableRows as children");
	    }
	}

}

@@ -52,24 +52,81 @@
 util.List;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.t
@@ -305,16 +305,104 @@
 uiTest %7B
+%0A%0A    private static final Logger logger = LoggerFactory.getLogger(CustomGuiTest.class);
 %0A%09%0A%09publ
@@ -403,36 +403,39 @@
 %0A%09public static 
-void
+boolean
  waitUntilNodeIs
@@ -711,74 +711,307 @@
 %0A%09%09%09
-// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09%7D
+logger.error(%22Interrupted while waiting for Node to be not null!%22);%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch(NullPointerException e)%7B%0A%09%09%09logger.error(%22Maximum timeout reached, while waiting for Node to be not null!%22);%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09%7D%0A%09%09if(node != null)%7B%0A%09%09%09return false;%0A%09%09%7D%0A%09%09return false;
 %0A%09%7D%0A
@@ -1258,16 +1258,41 @@
 uery();%0A
+%09%09boolean found = false;%0A
 %09%09if(nod
@@ -1306,16 +1306,24 @@
 ll)%7B%0A%09%09%09
+found = 
 waitUnti
@@ -1359,16 +1359,40 @@
 t);%0A%09%09%7D%0A
+%09%09if(!found)%0A%09%09%09return;%0A
 %09%09node =
@@ -1573,42 +1573,216 @@
 %0A%09%09%09
-// TODO Auto-generated catch block
+logger.error(%22Interrupted while waiting for Node to be visible!%22);%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch(NullPointerException e)%7B%0A%09%09%09logger.error(%22Maximum timeout reached, while waiting for Node to be visible!%22);
 %0A%09%09%09

Fix bug in CustomGuiTest