diff --git a/.gitignore b/.gitignore index 8616a71..dcd5948 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .vs/ bin/ target/ +build/ .classpath .project *.fxbuild \ No newline at end of file diff --git a/src/de/rogovskyy/reorderpdf/Main.fxml b/src/de/rogovskyy/reorderpdf/Main.fxml index aaeb8a2..d70f734 100644 --- a/src/de/rogovskyy/reorderpdf/Main.fxml +++ b/src/de/rogovskyy/reorderpdf/Main.fxml @@ -8,6 +8,8 @@ + + @@ -54,7 +56,12 @@ - + + + + + diff --git a/src/de/rogovskyy/reorderpdf/Main.java b/src/de/rogovskyy/reorderpdf/Main.java index 2b8ef23..5450d67 100644 --- a/src/de/rogovskyy/reorderpdf/Main.java +++ b/src/de/rogovskyy/reorderpdf/Main.java @@ -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(); @@ -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(); + } } diff --git a/src/de/rogovskyy/reorderpdf/MainController.java b/src/de/rogovskyy/reorderpdf/MainController.java index da871d8..ad27abf 100644 --- a/src/de/rogovskyy/reorderpdf/MainController.java +++ b/src/de/rogovskyy/reorderpdf/MainController.java @@ -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; @@ -27,6 +28,9 @@ public class MainController { private final PagesManager mgr = new PagesManager(); + @FXML + public Label infoTxt; + @FXML public ListView pagesView; @@ -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"), @@ -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(); @@ -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(); + } } diff --git a/src/de/rogovskyy/reorderpdf/application.css b/src/de/rogovskyy/reorderpdf/application.css deleted file mode 100644 index 83d6f33..0000000 --- a/src/de/rogovskyy/reorderpdf/application.css +++ /dev/null @@ -1 +0,0 @@ -/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */ \ No newline at end of file diff --git a/src/de/rogovskyy/reorderpdf/model/PDFReference.java b/src/de/rogovskyy/reorderpdf/model/PDFReference.java new file mode 100644 index 0000000..9776e52 --- /dev/null +++ b/src/de/rogovskyy/reorderpdf/model/PDFReference.java @@ -0,0 +1,7 @@ +package de.rogovskyy.reorderpdf.model; + +import org.apache.pdfbox.pdmodel.PDDocument; + +public interface PDFReference { + PDDocument getPdf(); +} diff --git a/src/de/rogovskyy/reorderpdf/model/PagesManager.java b/src/de/rogovskyy/reorderpdf/model/PagesManager.java index ae3808e..16eaa3a 100644 --- a/src/de/rogovskyy/reorderpdf/model/PagesManager.java +++ b/src/de/rogovskyy/reorderpdf/model/PagesManager.java @@ -8,6 +8,8 @@ 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; @@ -15,9 +17,9 @@ 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 documentPages = new SimpleListProperty<>( FXCollections.observableArrayList()); @@ -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 newPages = IntStream.range(0, pdf.getNumberOfPages()).mapToObj((i) -> new PDFPage(pdfPage.toString(), pdf, i)) - .collect(Collectors.toList()); + List 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) { @@ -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); + } + } + } + } + } diff --git a/src/de/rogovskyy/reorderpdf/model/PDFPage.java b/src/de/rogovskyy/reorderpdf/model/SinglePDFPage.java similarity index 85% rename from src/de/rogovskyy/reorderpdf/model/PDFPage.java rename to src/de/rogovskyy/reorderpdf/model/SinglePDFPage.java index 1df5180..64b3999 100644 --- a/src/de/rogovskyy/reorderpdf/model/PDFPage.java +++ b/src/de/rogovskyy/reorderpdf/model/SinglePDFPage.java @@ -11,8 +11,8 @@ 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; @@ -20,7 +20,7 @@ public class PDFPage implements DocumentPage { 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; diff --git a/src/de/rogovskyy/reorderpdf/model/WholeDocumentPage.java b/src/de/rogovskyy/reorderpdf/model/WholeDocumentPage.java index 1cb0923..3d96ed6 100644 --- a/src/de/rogovskyy/reorderpdf/model/WholeDocumentPage.java +++ b/src/de/rogovskyy/reorderpdf/model/WholeDocumentPage.java @@ -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;