package testing;

import java.util.ArrayList;
import java.util.List;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import layout.panes.PhysicalVBox;

/**
 * An abstract template for example applications. This class creates the basic
 * prerequisites (layout and simulation) along with a GUI to start, stop and
 * reset the simulation.
 *
 * @author Christoph Burschka &lt;christoph@burschka.de&gt;
 */
public class VBoxExample extends Example {

    private final List<Node> circles;
    private final List<Node> lines;
    private final static int NODE_COUNT = 5;
    private final static int NODE_SIZE = 20;

    /**
     * Super-constructor for all examples.
     */
    public VBoxExample() {
        canvas = new PhysicalVBox(30);
        Button add = new Button("Add node");
        Button remove = new Button("Remove node");
        menu.getItems().add(add);
        menu.getItems().add(remove);
        add.setOnAction(e -> {
            addCircle();
        });
        remove.setOnAction(e -> {
            removeCircle();
        });

        setSimulation(((PhysicalVBox) canvas).simulation);

        circles = new ArrayList<>();

        lines = new ArrayList<>();
        for (int i = 0; i < NODE_COUNT; i++) {
            addCircle();
        }

        getSimulation().setFriction(2);
        ((PhysicalVBox) canvas).setStrength(20);

        canvas.setLayoutX((WIDTH / 2));
        canvas.setLayoutY((2 * HEIGHT / 3));
        root.setCenter(canvas);

        canvas.toBack();
    }

    /**
     * Reset the simulation. This is called once during the setup, and whenever
     * the reset button is pressed.
     */
    @Override
    public void reset() {
        for (Node circle : circles) {
            circle.setTranslateX((Math.random() - 0.5) * WIDTH);
            circle.setTranslateY((Math.random() - 0.5) * HEIGHT);
        }
    }

    /**
     * Get the window title.
     *
     * @return the string that the title will be set to.
     */
    @Override
    public String getTitle() {
        return "VBox";
    }

    private void addCircle() {
        int i = circles.size();
        Circle circle = new Circle(NODE_SIZE);
        circle.setFill(Color.hsb(360.0 * (i % NODE_COUNT) / NODE_COUNT, 1.0, 0.5));
        circles.add(circle);
        canvas.getChildren().add(circle);

        if (i > 0) {
            Line line = new Line();
            line.setFill(Color.BLACK);
            line.setStroke(Color.BLACK);
            line.startXProperty().bind(circles.get(i - 1).layoutXProperty().add(circles.get(i - 1).translateXProperty()));
            line.startYProperty().bind(circles.get(i - 1).layoutYProperty().add(circles.get(i - 1).translateYProperty()));
            line.endXProperty().bind(circle.layoutXProperty().add(circle.translateXProperty()));
            line.endYProperty().bind(circle.layoutYProperty().add(circle.translateYProperty()));
            lines.add(line);
            line.setManaged(false);

            canvas.getChildren().add(line);
            line.toBack();
        }
    }

    private void removeCircle() {
        int i = circles.size() - 1;
        Node circle = circles.get(i);
        Node line = lines.get(i - 1);
        canvas.getChildren().removeAll(circle, line);
        circles.remove(i);
        lines.remove(i - 1);
    }
}

@@ -58,24 +58,52 @@
 .util.List;%0A
+import javafx.geometry.Pos;%0A
 import javaf
@@ -116,16 +116,16 @@
 e.Node;%0A
-
 import j
@@ -152,16 +152,49 @@
 Button;%0A
+import javafx.scene.layout.VBox;%0A
 import j
@@ -1520,16 +1520,65 @@
 th(20);%0A
+        ((VBox) canvas).setAlignment(Pos.CENTER);
 %0A       
@@ -1596,17 +1596,16 @@
 LayoutX(
-(
 WIDTH / 
@@ -1606,17 +1606,16 @@
 DTH / 2)
-)
 ;%0A      
@@ -1638,13 +1638,8 @@
 utY(
-(2 * 
 HEIG
@@ -1647,10 +1647,9 @@
 T / 
-3)
+2
 );%0A 
@@ -1679,17 +1679,16 @@
 anvas);%0A
-%0A
         
@@ -3320,24 +3320,25 @@
 () - 1;%0A
+%0A
         
 Node cir
@@ -3333,38 +3333,25 @@
     
-Node circle = circles.get(i);%0A
+if (i %3E 0) %7B%0A    
     
@@ -3384,32 +3384,36 @@
 i - 1);%0A        
+ 
+   
 canvas.getChildr
@@ -3427,50 +3427,163 @@
 move
-All(circle, line);%0A        circles
+(line);%0A            lines.remove(i - 1);%0A        %7D%0A        if (i %3E= 0) %7B%0A            Node circle = circles.get(i);%0A            canvas.getChildren()
 .remove(
 i);%0A
@@ -3578,17 +3578,22 @@
 .remove(
-i
+circle
 );%0A     
@@ -3587,35 +3587,41 @@
 ircle);%0A        
-lin
+    circl
 es.remove(i - 1)
@@ -3611,27 +3611,33 @@
 les.remove(i
- - 1);
+);%0A        %7D
 %0A    %7D%0A%7D%0A

Fix VBoxExample positioning.