package de.prob2.ui.groovy;

import javax.script.ScriptEngine;

import com.google.inject.Inject;

import de.prob.scripting.ScriptEngineProvider;
import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.input.KeyCode;
import javafx.scene.shape.Path;
import javafx.stage.Popup;

public class GroovyCodeCompletion {
	
	private Popup popup;
	
	private ListView<String> lv_suggestions;
	
	private final ScriptEngine engine;

	
	//Trying with ListView
	
	@Inject
	public GroovyCodeCompletion(final ScriptEngineProvider sep) {
		engine = sep.get();
		popup = new Popup();
		lv_suggestions = new ListView<String>();
		lv_suggestions.getItems().add("boo");
		lv_suggestions.getItems().add("boo");
		lv_suggestions.getItems().add("boo");
		lv_suggestions.getItems().add("boo");
		lv_suggestions.getItems().add("boo");
		lv_suggestions.getItems().add("boo");
		for(int i = 0; i < lv_suggestions.getItems().size(); i++) {
			lv_suggestions.setOnKeyPressed(e-> {
				if(e.getCode().equals(KeyCode.ENTER)) {
					this.deactivate();
				}
			});
		}
		lv_suggestions.setMaxHeight(200);
		lv_suggestions.setMaxWidth(400);
		lv_suggestions.setPrefHeight(200);
		lv_suggestions.setPrefWidth(400);
		popup.getContent().add(lv_suggestions);
	}
	
	
	public void activate(GroovyConsole console) {
		lv_suggestions.getSelectionModel().selectFirst();
		Point2D point = findCaretPosition(findCaret(console));
		double x = point.getX() + 10;
		double y = point.getY() + 10;
		popup.show(console, x, y);
		
	}
	
	public void deactivate() {
		popup.hide();
	}
	
	public boolean isVisible() {
		return popup.isShowing();
	}
	
	
	private Path findCaret(Parent parent) {
		for (Node node : parent.getChildrenUnmodifiable()) {
			if (node instanceof Path) {
				return (Path) node;
			} else if (node instanceof Parent) {
				Path caret = findCaret((Parent) node);
				if (caret != null) {
					return caret;
				}
			}
		}
		return null;
	}
	

	private Point2D findCaretPosition(Node node) {
		double x = 0;
		double y = 0;
		if(node == null) {
			return null;
		}
		for (Node n = node; n != null; n=n.getParent()) {
			Bounds parentBounds = n.getBoundsInParent();
			x += parentBounds.getMinX();
			y += parentBounds.getMinY();
		}
		if(node.getScene() != null) {
			Scene scene = node.getScene();
			x += scene.getX() + scene.getWindow().getX();
			y += scene.getY() + scene.getWindow().getY();
			x = Math.min(scene.getWindow().getX() + scene.getWindow().getWidth() - 20, x);
			y = Math.min(scene.getWindow().getY() + scene.getWindow().getHeight() - 20, y);
		}
		return new Point2D(x,y);
	}

}

@@ -752,307 +752,90 @@
 ;%0A%09%09
-lv_suggestions.getItems().add(%22boo%22);%0A%09%09lv_suggestions.getItems().add(%22boo%22);%0A%09%09lv_suggestions.getItems().add(%22boo%22);%0A%09%09lv_suggestions.getItems().add(%22boo%22);%0A%09%09lv_suggestions.getItems().add(%22boo%22);%0A%09%09lv_suggestions.getItems().add(%22boo%22);%0A%09%09for(int i = 0; i %3C lv_suggestions.getItems().size(); i++) %7B
+for(int i = 0; i %3C 10; i++) %7B%0A%09%09%09lv_suggestions.getItems().add(String.valueOf(i));
 %0A%09%09%09
@@ -911,24 +911,103 @@
 e.ENTER)) %7B%0A
+%09%09%09%09%09System.out.println(lv_suggestions.getSelectionModel().getSelectedItem());%0A
 %09%09%09%09%09this.de

Update Groovy Code Completion