package de.prob2.ui.visualisation.magiclayout.graph;

import de.prob2.ui.visualisation.magiclayout.graph.layout.Layout;
import javafx.scene.layout.Pane;

public class Graph extends Pane {

	private Model model;
	private Layout layout;

	public Graph(Model model) {
		this.model = model;
	}
	
	public Model getModel() {
		return model;
	}
	
	public void layout(Layout layout) {
		this.layout =  layout;
		
		this.getChildren().clear();
		model.getVertices().forEach(vertex -> this.getChildren().add(vertex));
		model.getEdges().forEach(edge -> this.getChildren().add(edge));
		
		layout.drawGraph(this);
		model.finishUpdate();
	}
	
	public void update() {	
		model.getRemovedVertices().forEach(vertex -> this.getChildren().remove(vertex));
		model.getRemovedEdges().forEach(edge -> this.getChildren().remove(edge));
		
		model.getAddedVertices().forEach(vertex -> this.getChildren().add(vertex));
		model.getAddedEdges().forEach(edge -> this.getChildren().add(edge));
		
		layout.updateGraph(this);
		model.finishUpdate();
	}
	
	public boolean vertexAt(double x, double y, double offsetX, double offsetY) {
		for (Vertex vertex : model.getVertices()) {
			if (x > (vertex.getLeftX() - offsetX) 
					&& x < vertex.getRightX() 
					&& y > (vertex.getTopY() - offsetY)
					&& y < vertex.getBottomY()) {
				return true;
			}
		}
		return false;
	}
}

@@ -275,33 +275,32 @@
 del = model;%0A%09%7D%0A
-%09
 %0A%09public Model g
@@ -326,25 +326,24 @@
 n model;%0A%09%7D%0A
-%09
 %0A%09public voi
@@ -383,17 +383,16 @@
 layout =
- 
  layout;
@@ -392,18 +392,16 @@
 layout;%0A
-%09%09
 %0A%09%09this.
@@ -554,35 +554,73 @@
 n().add(edge));%0A
+%0A
 %09%09
-%0A
+if (this.getChildren().size() %3E 0) %7B%0A%09
 %09%09layout.drawGra
@@ -621,32 +621,36 @@
 rawGraph(this);%0A
+%09%09%7D%0A
 %09%09model.finishUp
@@ -652,33 +652,32 @@
 ishUpdate();%0A%09%7D%0A
-%09
 %0A%09public void up
@@ -684,17 +684,16 @@
 date() %7B
-%09
 %0A%09%09model
@@ -844,18 +844,16 @@
 edge));%0A
-%09%09
 %0A%09%09model
@@ -998,11 +998,49 @@
 ));%0A
+%0A
 %09%09
-%0A
+if (this.getChildren().size() %3E 0) %7B%0A%09
 %09%09la
@@ -1063,16 +1063,20 @@
 (this);%0A
+%09%09%7D%0A
 %09%09model.
@@ -1094,17 +1094,16 @@
 e();%0A%09%7D%0A
-%09
 %0A%09public
@@ -1261,22 +1261,16 @@
 ffsetX) 
-%0A%09%09%09%09%09
 && x %3C v
@@ -1287,22 +1287,16 @@
 ightX() 
-%0A%09%09%09%09%09
 && y %3E (

fix exception caused by trying to layout empty graph