Skip to content

Commit

Permalink
Better resource cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ARogovskyy committed Apr 4, 2019
1 parent 660bb03 commit 1c71995
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.vs/
bin/
target/
build/
.classpath
.project
*.fxbuild
9 changes: 8 additions & 1 deletion src/de/rogovskyy/reorderpdf/Main.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.control.Label?>

<BorderPane prefHeight="646.0" prefWidth="973.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.rogovskyy.reorderpdf.MainController">
<top>
Expand Down Expand Up @@ -54,7 +56,12 @@
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<ListView fx:id="pagesView" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
<StackPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<ListView fx:id="pagesView" />
<Label fx:id="infoTxt" alignment="CENTER" prefHeight="210.0" prefWidth="147.0" text="After adding PDF or image files, you can reorder them freely." textAlignment="CENTER" wrapText="true" />
</children>
</StackPane>
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
Expand Down
15 changes: 12 additions & 3 deletions src/de/rogovskyy/reorderpdf/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {
private MainController mainController;

@Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane) FXMLLoader.load(getClass().getResource("Main.fxml"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
Parent root = loader.load();
mainController = loader.getController();
Scene scene = new Scene(root, 800, 600);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("ReorderPDF");
primaryStage.show();
Expand All @@ -24,4 +27,10 @@ public void start(Stage primaryStage) {
public static void main(String[] args) {
launch(args);
}

@Override
public void stop() throws Exception {
if (mainController != null)
mainController.close();
}
}
13 changes: 11 additions & 2 deletions src/de/rogovskyy/reorderpdf/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
Expand All @@ -27,6 +28,9 @@
public class MainController {
private final PagesManager mgr = new PagesManager();

@FXML
public Label infoTxt;

@FXML
public ListView<DocumentPage> pagesView;

Expand Down Expand Up @@ -62,7 +66,8 @@ public void initialize() {
downBtn.disableProperty().bind(dependsOnSelected((i, s) -> i == -1 || i == mgr.getDocumentPages().size() - 1));
removeBtn.disableProperty().bind(dependsOnSelected((i, s) -> s == null));
saveBtn.disableProperty().bind(Bindings.equal(Bindings.size(mgr.documentPagesProperty()), 0));

infoTxt.visibleProperty().bind(Bindings.equal(Bindings.size(mgr.documentPagesProperty()), 0));

addBtn.setOnAction((ae) -> {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().addAll(new ExtensionFilter("PDF File", "*.pdf"),
Expand Down Expand Up @@ -125,7 +130,7 @@ public void initialize() {

removeBtn.setOnAction((ae) -> {
int i = pagesView.getSelectionModel().getSelectedIndex();
mgr.getDocumentPages().remove(i);
mgr.remove(i);
if (mgr.getDocumentPages().size() > 0) {
pagesView.getSelectionModel().select(i);
pagesView.requestFocus();
Expand All @@ -149,4 +154,8 @@ private Background getBackgroundFromSelectedItm() {
new BackgroundImage(selected.getImage(), BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT,
BackgroundPosition.CENTER, new BackgroundSize(0, 0, false, false, true, false)));
}

public void close() {
mgr.close();
}
}
1 change: 0 additions & 1 deletion src/de/rogovskyy/reorderpdf/application.css

This file was deleted.

7 changes: 7 additions & 0 deletions src/de/rogovskyy/reorderpdf/model/PDFReference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.rogovskyy.reorderpdf.model;

import org.apache.pdfbox.pdmodel.PDDocument;

public interface PDFReference {
PDDocument getPdf();
}
46 changes: 42 additions & 4 deletions src/de/rogovskyy/reorderpdf/model/PagesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.pdmodel.PDDocument;

import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class PagesManager {
private static final Log log = LogFactory.getLog(PagesManager.class);
public static final String[] SUPPORTED_IMG_EXTS = { ".jpg", ".jpeg", ".png" };

// TODO add pdf closing
private final SimpleListProperty<DocumentPage> documentPages = new SimpleListProperty<>(
FXCollections.observableArrayList());

Expand Down Expand Up @@ -67,14 +69,14 @@ public void unpackPDF(int itemIndex) {
// convert them first and add all at once to update UI only once
WholeDocumentPage pdfPage = ((WholeDocumentPage) page);
PDDocument pdf = pdfPage.getPdf();
List<PDFPage> newPages = IntStream.range(0, pdf.getNumberOfPages()).mapToObj((i) -> new PDFPage(pdfPage.toString(), pdf, i))
.collect(Collectors.toList());
List<SinglePDFPage> newPages = IntStream.range(0, pdf.getNumberOfPages())
.mapToObj((i) -> new SinglePDFPage(pdfPage.toString(), pdf, i)).collect(Collectors.toList());
documentPages.remove(itemIndex);
documentPages.addAll(itemIndex, newPages);
}

public void save(File selectedFile) throws IOException {
if(documentPages.size()==0)
if (documentPages.size() == 0)
throw new IOException("You need to add at least one page");
PDDocument pdf = new PDDocument();
for (DocumentPage documentPage : documentPages) {
Expand All @@ -83,4 +85,40 @@ public void save(File selectedFile) throws IOException {
pdf.save(selectedFile);
}

public void remove(int i) {
if (i < 0 || i >= documentPages.size())
throw new IndexOutOfBoundsException();
PDDocument referencedDoc;
DocumentPage page = documentPages.get(i);
if (page instanceof PDFReference)
referencedDoc = ((PDFReference) page).getPdf();
else
referencedDoc = null;

documentPages.remove(i);

// if the removed page referenced a PDDocument, we need to make sure to close it if no references
// to it are left
if (referencedDoc != null && !documentPages.stream()
.anyMatch((p) -> p instanceof PDFReference && ((PDFReference) p).getPdf() == referencedDoc)) {
try {
referencedDoc.close();
} catch (IOException e) {
log.debug("Error while closing PDF document", e);
}
}
}

public void close() {
for (DocumentPage page : documentPages) {
if(page instanceof PDFReference) {
try {
((PDFReference) page).getPdf().close();
} catch (IOException e) {
log.debug("Error while closing PDF document", e);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;

public class PDFPage implements DocumentPage {
private static final Log log = LogFactory.getLog(PDFPage.class);
public class SinglePDFPage implements DocumentPage, PDFReference {
private static final Log log = LogFactory.getLog(SinglePDFPage.class);

private final String name;
private final PDDocument pdf;
private final int pageNum;
private final PDPage page;
private final Image rendered;

public PDFPage(String name, PDDocument pdf, int pageNum) {
public SinglePDFPage(String name, PDDocument pdf, int pageNum) {
this.name = name;
this.pdf = pdf;
this.pageNum = pageNum;
Expand Down
2 changes: 1 addition & 1 deletion src/de/rogovskyy/reorderpdf/model/WholeDocumentPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;

public class WholeDocumentPage implements DocumentPage{
public class WholeDocumentPage implements DocumentPage, PDFReference{
private static final Log log = LogFactory.getLog(WholeDocumentPage.class);

private final String docName;
Expand Down

0 comments on commit 1c71995

Please sign in to comment.