Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

Commit

Permalink
Bug Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Patr1ick committed Feb 7, 2020
1 parent f6cf016 commit 98e7d55
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ root.getChildren().add(v); // Add the Viewer to the root-Pane
#### ViewerType LIST and IMAGE
There are two types of viewer to display the PDF differently.
##### LIST
The ViewType LIST displays the pages of the PDF below each other in a ScrollPane.
The ViewType LIST displays the pages of the PDF below each other in a ScrollPane. I do not recommend this for PDFs with many pages, because Javafx will get some performance issue.
##### Image
The ViewType IMAGE displays each page of the PDF individually. You can navigate using buttons.
```java
Expand Down
3 changes: 3 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import javafx.stage.Stage;
import util.PDF;
import viewer.PDFViewer;
import viewer.Viewer;
import viewer.ViewerType;

import java.io.File;

public class Main extends Application {
Expand Down
11 changes: 8 additions & 3 deletions src/viewer/PDFViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ public void changed(ObservableValue<? extends Number> observable, Number oldValu
this.name.getStyleClass().add("path");

//PagePreview
this.pagePreview = new PagePreview(pdf, this.viewer);

new Thread(new Runnable() {
@Override
public void run() {
pagePreview = new PagePreview(pdf, viewer);
}
}).start();

//PageChooser
this.pageChooser = new PageChooser(pdf, this.viewer);
Expand Down Expand Up @@ -153,11 +159,10 @@ public void run() {

//Adding Nodes to Panes
this.menuBar.getMenus().addAll(this.file, this.control);

this.splitPane.getItems().addAll(this.pagePreview, this.viewer);
this.toolbar.getChildren().addAll(this.menuBar, this.name, this.pageChooser);
this.setTop(this.toolbar);
BorderPane.setMargin(this.toolbar, new Insets(2,1,10,1));
BorderPane.setMargin(this.toolbar, new Insets(2, 1, 10, 1));
this.setCenter(this.splitPane);
} else {
throw new NullPointerException("The parameter pdf and stage is null. Please check this parameter in the constructor.");
Expand Down
45 changes: 29 additions & 16 deletions src/viewer/Viewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.CacheHint;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
Expand Down Expand Up @@ -70,6 +71,8 @@ public class Viewer extends Pane {
private ViewerType viewerType = ViewerType.IMAGE;

public Viewer(PDF pdf) {
this.setCache(true);
this.setCacheHint(CacheHint.SPEED);
if (pdf != null) {
try {
this.pdf = pdf;
Expand All @@ -83,11 +86,8 @@ public Viewer(PDF pdf) {
this.listVBox.setAlignment(Pos.CENTER);
this.listVBox.setSpacing(5d);

this.pdfList = new ImageView[this.pdf.getNumberOfPages()];
for (int i = 0; i < this.pdfList.length; i++) {
this.pdfList[i] = new ImageView(this.pdf.getPageImage(i, this.scaleFactor));
this.listVBox.getChildren().add(this.pdfList[i]);
}
if (this.viewerType == ViewerType.LIST)
loadPDFasList();

//Panes
this.stackPane = new StackPane();
Expand All @@ -100,18 +100,16 @@ public Viewer(PDF pdf) {
this.scrollPane.setPannable(true);
this.scrollPane.setContent(this.stackPane);


//CSS
this.getStylesheets().add("resource/css/style.css");

//Images
this.img_add = new Image("resource/img/baseline_add_black_48dp.png", 40, 40, true, false);
this.img_remove = new Image("resource/img/baseline_remove_black_48dp.png", 40, 40, true, false);
this.img_left = new Image("resource/img/baseline_keyboard_arrow_left_black_48dp.png", 48, 48, true, false);
this.img_right = new Image("resource/img/baseline_keyboard_arrow_right_black_48dp.png", 48, 48, true, false);
this.img_first_page = new Image("resource/img/baseline_first_page_black_48dp.png", 48, 48, true, false);
this.img_last_page = new Image("resource/img/baseline_last_page_black_48dp.png", 48, 48, true, false);

this.img_add = new Image("resource/img/baseline_add_black_48dp.png", 40, 40, true, false, true);
this.img_remove = new Image("resource/img/baseline_remove_black_48dp.png", 40, 40, true, false, true);
this.img_left = new Image("resource/img/baseline_keyboard_arrow_left_black_48dp.png", 48, 48, true, false, true);
this.img_right = new Image("resource/img/baseline_keyboard_arrow_right_black_48dp.png", 48, 48, true, false, true);
this.img_first_page = new Image("resource/img/baseline_first_page_black_48dp.png", 48, 48, true, false, true);
this.img_last_page = new Image("resource/img/baseline_last_page_black_48dp.png", 48, 48, true, false, true);
//Nodes
this.nextPageLeft = new Button();
this.nextPageLeft.setMaxSize(50d, 50d);
Expand Down Expand Up @@ -200,7 +198,6 @@ public Viewer(PDF pdf) {
this.zoomTool.getChildren().addAll(this.zoomIn, this.zoomOut);

this.getChildren().addAll(this.scrollPane, this.zoomTool, this.nextPageLeft, this.nextPageRight);

//Events
this.setOnMouseEntered(event -> {
if (!disableZoomButtons) {
Expand Down Expand Up @@ -279,8 +276,6 @@ public Viewer(PDF pdf) {
updateViewer();
}
});


System.out.println("Max Page Numbers: " + this.pdf.getNumberOfPages());


Expand All @@ -304,9 +299,27 @@ public void setViewerType(ViewerType viewerType) {
this.getChildren().removeAll(this.nextPageRight, this.nextPageLeft, this.zoomTool);
this.stackPane.getChildren().remove(this.imageView);
this.stackPane.getChildren().add(this.listVBox);
loadPDFasList();
}
}

public void loadPDFasList() {
new Thread(() -> {
Platform.runLater(new Runnable() {
@Override
public void run() {
pdfList = new ImageView[pdf.getNumberOfPages()];
for (int i = 0; i < pdfList.length; i++) {
pdfList[i] = new ImageView(pdf.getPageImage(i, scaleFactor));
listVBox.getChildren().add(pdfList[i]);

}
}
});
}).start();

}

public void loadPDF(PDF pdf) {
if (pdf != null) {
this.pdf = pdf;
Expand Down
2 changes: 1 addition & 1 deletion src/viewer/nodes/PageChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void onPageSwitch(String param) {
this.textField.setMaxHeight(5d);
this.textField.getStyleClass().add("textFieldPageChooser");
this.textField.setOnAction(event -> {
viewer.loadPage(Integer.parseInt(this.textField.getText()));
viewer.loadPage(Integer.parseInt(this.textField.getText()) - 1);
});

this.getChildren().addAll(this.textField, this.label);
Expand Down
29 changes: 23 additions & 6 deletions src/viewer/nodes/PagePreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.CacheHint;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
Expand All @@ -21,6 +22,9 @@ public class PagePreview extends ScrollPane {
//PDF
private PDF pdf;

//Settings
private int maxLoadPicture = 500;

public PagePreview(PDF pdf, Viewer viewer) {
this.setHbarPolicy(ScrollBarPolicy.NEVER);
this.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
Expand All @@ -30,6 +34,9 @@ public PagePreview(PDF pdf, Viewer viewer) {
this.box = new VBox();
this.box.setAlignment(Pos.CENTER);
this.box.setSpacing(2d);
this.box.setCache(true);
this.box.setCacheShape(true);
this.box.setCacheHint(CacheHint.SPEED);
if (pdf != null && viewer != null) {
this.pdf = pdf;
this.viewer = viewer;
Expand All @@ -48,14 +55,20 @@ private void initButtons() {
public void run() {
for (int i = 0; i < pages.length; i++) {
pages[i] = new Button(" Page " + (i + 1));
ImageView imageView = new ImageView(pdf.getPageImage(i, 1.0f));
imageView.setSmooth(false);
imageView.setPreserveRatio(true);
imageView.setFitWidth(80d);
imageView.setFitHeight(80d);
pages[i].setGraphic(imageView);
if (pages.length < maxLoadPicture){
ImageView imageView = new ImageView(pdf.getPageImage(i, 0.1f));
imageView.setSmooth(false);
imageView.setPreserveRatio(true);
imageView.setFitWidth(80d);
imageView.setFitHeight(80d);
imageView.setCache(true);
imageView.setCacheHint(CacheHint.SPEED);
pages[i].setGraphic(imageView);
}
pages[i].setPrefSize(150d, 80d);
pages[i].getStyleClass().add("previewButtons");
pages[i].setCache(true);
pages[i].setCacheHint(CacheHint.SPEED);
int value = i;
pages[i].setOnAction(event -> {
viewer.loadPage(value);
Expand All @@ -73,6 +86,10 @@ public void loadPDF(PDF pdf) {
initButtons();
}

public void setMaxLoadPicture(int max){
this.maxLoadPicture = max;
}

public VBox getBox() {
return box;
}
Expand Down

0 comments on commit 98e7d55

Please sign in to comment.