package mainWindow;

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.rendering.PDFRenderer;

import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class MainWindowController {
	
	@FXML AnchorPane anchorPane;
	@FXML ImageView pdfContainer;
	@FXML Button prevButton;
	@FXML Button nextButton;
	@FXML TextField pageNumber;
	
	private int pageNo = 0;
	private PDDocument pdfFile;

	@FXML
	private void clickOpen() throws IOException {
		File selectedFile = openFileChoser();
		openPDFFile(selectedFile);
	}

	private void openPDFFile(File selectedFile) {
		
		if (selectedFile == null){
			return;
		}
		try {
			pdfFile = PDDocument.load(selectedFile);
		} catch (InvalidPasswordException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		openPDFPage(pageNo);
		
		anchorPaneListeners();
		navButtonListeners();
		
	}

	private void navButtonListeners() {
		prevButton.setOnAction(new EventHandler<ActionEvent>() {
		    @Override public void handle(ActionEvent e) {
		    	if (pageNo != 0){
		    		pageNo--;
		    		openPDFPage(pageNo);
		    	}
		    }
		});
		
		nextButton.setOnAction(new EventHandler<ActionEvent>() {
		    @Override public void handle(ActionEvent e) {
		    	if (pageNo != pdfFile.getNumberOfPages()){
		    		pageNo++;
		        	openPDFPage(pageNo);
		    	}
		    }
		});
		
		pageNumber.setOnAction(new EventHandler<ActionEvent>(){
			@Override public void handle(ActionEvent e){
				try {
					pageNo = Integer.parseInt(pageNumber.getText());
				} catch (NumberFormatException exception){
					
				}
				openPDFPage(pageNo);
			}
		});
	}
	
	private void openPDFPage(int pageNo){
		PDFRenderer renderer = new PDFRenderer(pdfFile);
        Image image = null;
        try {
			image = SwingFXUtils.toFXImage(renderer.renderImage(pageNo), null);
		} catch (Exception e) {
			e.printStackTrace();
		}
        pdfContainer.setImage(image);
        pageNumber.setText("" + pageNo);
	}

	private void anchorPaneListeners() {
		anchorPane.widthProperty().addListener((obs, oldVal, newVal) -> {			
    		pdfContainer.setX(pdfContainer.getX() + newVal.doubleValue() - oldVal.doubleValue());	
    		pdfContainer.setFitWidth(pdfContainer.getFitWidth() + newVal.doubleValue() - oldVal.doubleValue());
			pdfContainer.preserveRatioProperty();
		});
		
		anchorPane.heightProperty().addListener((obs, oldVal, newVal) -> {
			pdfContainer.setFitHeight(pdfContainer.getFitHeight() + newVal.doubleValue() - oldVal.doubleValue());
			pdfContainer.preserveRatioProperty();
		});
	}

	private File openFileChoser() {
		FileChooser fileChooser = new FileChooser();
		fileChooser.setTitle("Open PDF File");
		fileChooser.getExtensionFilters().add(new ExtensionFilter("PDF Files", "*.pdf"));
		File selectedFile = fileChooser.showOpenDialog(anchorPane.getScene().getWindow());
		return selectedFile;
	}
}

@@ -512,16 +512,92 @@
 geView;%0A
+import javafx.scene.input.ScrollEvent;%0Aimport javafx.scene.input.ZoomEvent;%0A
 import j
@@ -1356,24 +1356,64 @@
 pageNo);%0A%09%09%0A
+%09%09pdfContainer.preserveRatioProperty();%0A
 %09%09anchorPane
@@ -1451,16 +1451,627 @@
 rs();%0A%09%09
+scrollListeners();%0A%09%7D%0A%0A%09private void zoomListeners() %7B%0A%09%09pdfContainer.setOnZoomStarted(new EventHandler%3CZoomEvent%3E()%7B%0A%09%09%09@Override%0A%09%09%09public void handle(ZoomEvent event) %7B%0A%09%09%09%09pdfContainer.setScaleX(event.getZoomFactor() * pdfContainer.getScaleX());%0A%09%09%09%7D%0A%09%09%7D);%09%0A%09%7D%0A%09%0A%09private void scrollListeners()%7B%0A%09%09pdfContainer.setOnScroll(new EventHandler%3CScrollEvent%3E()%7B%0A%09%09%09@Override%0A%09%09%09public void handle(ScrollEvent event) %7B%0A%09%09%09%09if (event.getDeltaY() %3E 0 && pageNo != 0)%0A%09%09%09%09%09openPDFPage(--pageNo);%0A%09%09%09%09else if (event.getDeltaY() %3C 0 && pageNo != pdfFile.getNumberOfPages())%0A%09%09%09%09%09openPDFPage(++pageNo);%09%09%09%09%0A%09%09%09%7D%09%09%09%0A%09%09%7D);
 %0A%09%7D%0A%0A%09pr
@@ -2244,26 +2244,8 @@
 0)%7B%0A
-%09%09    %09%09pageNo--;%0A
 %09%09  
@@ -2252,32 +2252,34 @@
   %09%09openPDFPage(
+--
 pageNo);%0A%09%09    %09
@@ -2462,26 +2462,8 @@
 ))%7B%0A
-%09%09    %09%09pageNo++;%0A
 %09%09  
@@ -2473,32 +2473,34 @@
    %09openPDFPage(
+++
 pageNo);%0A%09%09    %09
@@ -3439,49 +3439,11 @@
 ());
-%0A
 %09%09%09
-pdfContainer.preserveRatioProperty();
 %0A%09%09%7D
@@ -3626,49 +3626,8 @@
 ));%0A
-%09%09%09pdfContainer.preserveRatioProperty();%0A
 %09%09%7D)

Add support for mouse scrolling pages