diff --git a/src/main/java/NTNU/IDATT1002/App.java b/src/main/java/NTNU/IDATT1002/App.java index 32710f8c35e7a4fc6bdd37f9bb3301f0438992cf..6441e9b91663087bc2fe7587c770008003f41b96 100644 --- a/src/main/java/NTNU/IDATT1002/App.java +++ b/src/main/java/NTNU/IDATT1002/App.java @@ -2,6 +2,7 @@ package NTNU.IDATT1002; import NTNU.IDATT1002.controllers.DataExchange; import javafx.application.Application; +import javafx.application.HostServices; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; @@ -13,11 +14,13 @@ import java.io.IOException; public class App extends Application { public static DataExchange ex; + public static HostServices hostServices; private static Scene scene; @Override public void start(Stage stage) throws IOException { ex = new DataExchange(); + ex.setHostServices(getHostServices()); scene = new Scene(loadFXML("login")); stage.setMaximized(true); stage.setScene(scene); diff --git a/src/main/java/NTNU/IDATT1002/controllers/DataExchange.java b/src/main/java/NTNU/IDATT1002/controllers/DataExchange.java index 03cab6ae4c587df37ef0e8f10f6b36db6db8ed55..8e3f78d487b29e9842af7930125bce080542b77e 100644 --- a/src/main/java/NTNU/IDATT1002/controllers/DataExchange.java +++ b/src/main/java/NTNU/IDATT1002/controllers/DataExchange.java @@ -1,5 +1,7 @@ package NTNU.IDATT1002.controllers; +import javafx.application.HostServices; + import java.io.File; import java.util.List; @@ -9,6 +11,8 @@ import java.util.List; * @version 1.0 22.03.2020 */ public class DataExchange { + + public HostServices hostServices; private String searchField; private List<File> uploadedFiles; private Long chosenAlbumId; @@ -17,6 +21,11 @@ public class DataExchange { public DataExchange(){ searchField = ""; } + + public HostServices getHostServices() { + return hostServices; + } + public List<File> getUploadedFiles() { return uploadedFiles; } @@ -33,6 +42,10 @@ public class DataExchange { return chosenImg; } + public void setHostServices(HostServices hostServices) { + this.hostServices = hostServices; + } + public void setUploadedFiles(List<File> uploadedFiles) { this.uploadedFiles = uploadedFiles; } diff --git a/src/main/java/NTNU/IDATT1002/controllers/ViewAlbum.java b/src/main/java/NTNU/IDATT1002/controllers/ViewAlbum.java index d0b186b94df1b628c7bf23952ffbfde16c1194ae..878dd1a07145966754e8dbf0b9637f18b06bc360 100644 --- a/src/main/java/NTNU/IDATT1002/controllers/ViewAlbum.java +++ b/src/main/java/NTNU/IDATT1002/controllers/ViewAlbum.java @@ -4,6 +4,7 @@ import NTNU.IDATT1002.App; import NTNU.IDATT1002.models.ImageAlbum; import NTNU.IDATT1002.service.ImageAlbumService; import NTNU.IDATT1002.utils.PdfDocument; +import javafx.application.HostServices; import javafx.event.ActionEvent; import javafx.scene.control.Button; import javafx.scene.control.TextField; @@ -11,7 +12,10 @@ import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Pane; import javafx.scene.text.Text; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.File; import java.io.IOException; /** @@ -44,6 +48,13 @@ public class ViewAlbum { public Button tbar_searchBtn; public Button tbar_albums; + private ImageAlbumService imageAlbumService; + + private static Logger logger = LoggerFactory.getLogger(ViewAlbum.class); + + public ViewAlbum() { + this.imageAlbumService = new ImageAlbumService(); + } /** * Method that changes scene to Main page @@ -139,13 +150,45 @@ public class ViewAlbum { //write method that loads the next 6 images in the album into the scrollbar-view } + /** + * Create and save album pdf to users downloads directory. + * + * @param actionEvent + */ public void createPdf(ActionEvent actionEvent) { - ImageAlbumService imageAlbumService = new ImageAlbumService(); Long currentAlbumId = App.ex.getChosenAlbumId(); - ImageAlbum imageAlbum = imageAlbumService.getImageAlbumById(currentAlbumId) .orElseThrow(IllegalArgumentException::new); - PdfDocument document = new PdfDocument(imageAlbum, "./Album.pdf"); + + String destinationFile = String.format("%s/downloads/%s.pdf", + System.getProperty("user.home"), + imageAlbum.getTitle()); + + PdfDocument document = new PdfDocument(imageAlbum, destinationFile); document.createPdfDocument(); + logger.info("[x] Saved PDF document to " + destinationFile); + + displayPdfLink(document.getPdfDocument()); + } + + /** + * Replace create album pdf button with a button to open the given document. + * + * @param pdfDocument the pdf document to be opened + */ + private void displayPdfLink(File pdfDocument) { + create_album_pdf.setText("Open PDF"); + create_album_pdf.setOnAction(actionEvent -> openPdfDocument(actionEvent, pdfDocument)); + } + + /** + * Open given file. + * + * @param actionEvent + * @param file the file to open + */ + private void openPdfDocument(ActionEvent actionEvent, File file) { + HostServices hostServices = App.ex.getHostServices(); + hostServices.showDocument(file.getAbsolutePath()); } } diff --git a/src/main/java/NTNU/IDATT1002/utils/PdfDocument.java b/src/main/java/NTNU/IDATT1002/utils/PdfDocument.java index 56d664426cea672e48582e34556df02749d41e44..f407ecd68061d80721f80d2fc8110ec93267484b 100644 --- a/src/main/java/NTNU/IDATT1002/utils/PdfDocument.java +++ b/src/main/java/NTNU/IDATT1002/utils/PdfDocument.java @@ -6,6 +6,7 @@ import NTNU.IDATT1002.models.Metadata; import com.itextpdf.text.*; import com.itextpdf.text.pdf.PdfWriter; +import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; @@ -47,6 +48,10 @@ public class PdfDocument { this.document = new Document(); } + public File getPdfDocument() { + return new File(DESTINATION_FILE); + } + /** * Create a new pdf document. */