package de.prob2.ui.groovy;

import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.shape.Path;

public class GroovyCodeCompletion {
	
	private ContextMenu suggestions;
	
	private ScrollPane sp;
	
	public GroovyCodeCompletion() {
		//sp = new ScrollPane();
		suggestions = new ContextMenu(new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"), new MenuItem("boo"));
		//sp.setContextMenu(suggestions);
	}
	
	public void activate(GroovyConsole console) {
		Point2D point = findCaretPosition(findCaret(console));
		double x = point.getX() + 10;
		double y = point.getY() + 10;
		suggestions.show(console, x, y);
	}
	
	public void deactivate() {
		suggestions.hide();
	}
	
	public boolean isVisible() {
		return suggestions.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);
	}

}

@@ -198,97 +198,16 @@
 rol.
-ContextMenu;%0Aimport javafx.scene.control.MenuItem;%0Aimport javafx.scene.control.ScrollPane
+ListView
 ;%0Aim
@@ -235,16 +235,43 @@
 pe.Path;
+%0Aimport javafx.stage.Popup;
 %0A%0Apublic
@@ -315,20 +315,52 @@
 ate 
-ContextMenu 
+Popup popup;%0A%09%0A%09private ListView%3CString%3E lv_
 sugg
@@ -372,35 +372,34 @@
 ns;%0A
+%0A
 %09%0A%09
-private ScrollPane sp;%0A%09
+//Trying with ListView
 %0A%09pu
@@ -434,19 +434,20 @@
 %7B%0A%09%09
-//s
+popu
 p = new 
 Scro
@@ -446,24 +446,22 @@
 new 
-ScrollPane
+Popup
 ();%0A%09%09
+lv_
 sugg
@@ -478,568 +478,440 @@
 new 
-ContextMenu(new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22), new MenuItem(%22boo%22)
+ListView%3CString%3E();%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%09lv_suggestions.getItems().add(%22boo%22);%0A%09%09lv_suggestions.setMaxHeight(200);%0A%09%09lv_suggestions.setMaxWidth(400);%0A%09%09lv_suggestions.setPrefHeight(200);%0A%09%09lv_suggestions.setPrefWidth(400
 );%0A%09%09
-//sp.s
+popup.g
 etConte
-xtMenu(
+nt().add(lv_
 sugg
@@ -920,16 +920,22 @@
 tions);%0A
+%09%09%0A%09%09%0A
 %09%7D%0A%09%0A%09pu
@@ -1101,27 +1101,21 @@
 + 10;%0A%09%09
-suggestions
+popup
 .show(co
@@ -1128,16 +1128,19 @@
  x, y);%0A
+%09%09%0A
 %09%7D%0A%09%0A%09pu
@@ -1166,27 +1166,21 @@
 e() %7B%0A%09%09
-suggestions
+popup
 .hide();
@@ -1224,27 +1224,21 @@
 %09return 
-suggestions
+popup
 .isShowi

Fix a few problems and keep preparing Groovy Code Completion