package gui.sub_controllers;

import gui.GraphDrawer;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.ScrollBar;

/**
 * Created by Jasper van Tilburg on 29-5-2017.
 *
 * Controls the panning functionality.
 */
public class PanningController {

    private final ScrollBar scrollbar;
    private final GraphDrawer drawer;
    private boolean active;

    /**
     * Constructor.
     * @param scrollBar Scrollbar.
     * @param drawer The graphdrawer.
     */
    public PanningController(ScrollBar scrollBar, GraphDrawer drawer) {
        this.scrollbar = scrollBar;
        this.drawer = drawer;
        initialize();
    }

    /**
     * Initializes the scrollbar and adds a listener to it.
     * The listener is only active when it is manually changed, not by zooming.
     */
    private void initialize() {
        scrollbar.setMax(drawer.getZoomLevel());
        scrollbar.setVisibleAmount(drawer.getZoomLevel());
        scrollbar.setValue(scrollbar.getMax() / 2);
        scrollbar.valueProperty().addListener(new ChangeListener<Number>() {
            public void changed(ObservableValue<? extends Number> ov,
                                Number oldVal, Number newVal) {
                if (active) {
                    drawer.moveShapes(drawer.getxDifference()
                            + (newVal.doubleValue() - oldVal.doubleValue()));
                }
            }
        });
    }

    /**
     * Change the scrollbar value and visible amount when zooming with the buttons.
     * @param factor Changing factor of the visible amount.
     */
    public void setScrollbarSize(double factor) {
        setScrollbarSize(factor, (int) (scrollbar.getValue()));
    }

    /**
     * Change the scrollbar value and visible amount when zooming in by scrolling.
     * @param factor Changing factor of the visible amount
     * @param column Column that is the centre of the zooming.
     */
    public void setScrollbarSize(double factor, int column) {
        active = false;
        double max = scrollbar.getMax();
        double amount = scrollbar.getVisibleAmount();
        if ((factor < 1 && amount < 1) || (factor > 1 && amount >= max)) {
            return;
        }
        amount = scrollbar.getVisibleAmount() * factor;
        scrollbar.setValue(column);
        scrollbar.setVisibleAmount(amount);
        active = true;
    }

}

@@ -1561,68 +1561,8 @@
 ns.%0A
-     * @param factor Changing factor of the visible amount.%0A
     
@@ -1598,29 +1598,16 @@
 barSize(
-double factor
 ) %7B%0A    
@@ -1627,24 +1627,16 @@
 barSize(
-factor, 
 (int) (s
@@ -1760,67 +1760,8 @@
 ng.%0A
-     * @param factor Changing factor of the visible amount%0A
     
@@ -1864,23 +1864,8 @@
 ize(
-double factor, 
 int 
@@ -1910,216 +1910,43 @@
     
-double max = scrollbar.getMax();%0A        double amount = scrollbar.getVisibleAmount();%0A        if ((factor %3C 1 && amount %3C 1) %7C%7C (factor %3E 1 && amount %3E= max)) %7B%0A            return;%0A        %7D%0A        amount =
+scrollbar.setValue(column);%0A       
  scr
@@ -1952,17 +1952,17 @@
 rollbar.
-g
+s
 etVisibl
@@ -1973,97 +1973,29 @@
 unt(
-) * factor;%0A        scrollbar.setValue(column);%0A        scrollbar.setVisibleAmount(amount
+drawer.getZoomLevel()
 );%0A 

Refactor setScrollbarSize