Skip to content
Snippets Groups Projects
Commit ebf59792 authored by Eirik Steira's avatar Eirik Steira
Browse files

Add HostService to Dataexchange to open files, add logic to save pdf to...

Add HostService to Dataexchange to open files, add logic to save pdf to downloads folder and possibility to open it afterwards on the same button for generating it
parent 8cefd4ba
No related branches found
No related tags found
2 merge requests!104Weekly merge to Master,!82feat/save-and-open-pdf
......@@ -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);
......
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;
}
......
......@@ -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());
}
}
......@@ -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.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment