From 0eb2edfc8d7583917488de0cd09b6292015c1240 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Fri, 29 Dec 2023 12:23:52 -0500 Subject: [PATCH] Added start of attachment preview! --- .../control/TransactionViewController.java | 10 +---- .../control/TransactionsViewController.java | 1 - .../control/component/AttachmentPreview.java | 42 +++++++++++++++++++ 3 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/andrewlalis/perfin/control/component/AttachmentPreview.java diff --git a/src/main/java/com/andrewlalis/perfin/control/TransactionViewController.java b/src/main/java/com/andrewlalis/perfin/control/TransactionViewController.java index 19b374a..22e8008 100644 --- a/src/main/java/com/andrewlalis/perfin/control/TransactionViewController.java +++ b/src/main/java/com/andrewlalis/perfin/control/TransactionViewController.java @@ -1,5 +1,6 @@ package com.andrewlalis.perfin.control; +import com.andrewlalis.perfin.control.component.AttachmentPreview; import com.andrewlalis.perfin.data.CurrencyUtil; import com.andrewlalis.perfin.data.DateUtil; import com.andrewlalis.perfin.model.CreditAndDebitAccounts; @@ -69,14 +70,7 @@ public class TransactionViewController { Platform.runLater(() -> attachmentsList.setAll(attachments)); }); }); - BindingUtil.mapContent(attachmentsHBox.getChildren(), attachmentsList, attachment -> { - VBox vbox = new VBox( - new Label(attachment.getFilename()), - new Label(attachment.getContentType()) - ); - // TODO: Custom attachment preview element. - return vbox; - }); + BindingUtil.mapContent(attachmentsHBox.getChildren(), attachmentsList, AttachmentPreview::new); } @FXML public void deleteTransaction() { diff --git a/src/main/java/com/andrewlalis/perfin/control/TransactionsViewController.java b/src/main/java/com/andrewlalis/perfin/control/TransactionsViewController.java index 6407eb2..03bff07 100644 --- a/src/main/java/com/andrewlalis/perfin/control/TransactionsViewController.java +++ b/src/main/java/com/andrewlalis/perfin/control/TransactionsViewController.java @@ -10,7 +10,6 @@ import com.andrewlalis.perfin.data.pagination.PageRequest; import com.andrewlalis.perfin.data.pagination.Sort; import com.andrewlalis.perfin.model.Profile; import com.andrewlalis.perfin.model.Transaction; -import javafx.application.Platform; import javafx.beans.property.ObjectProperty; import javafx.beans.property.SimpleObjectProperty; import javafx.beans.value.ObservableValue; diff --git a/src/main/java/com/andrewlalis/perfin/control/component/AttachmentPreview.java b/src/main/java/com/andrewlalis/perfin/control/component/AttachmentPreview.java new file mode 100644 index 0000000..824d4d6 --- /dev/null +++ b/src/main/java/com/andrewlalis/perfin/control/component/AttachmentPreview.java @@ -0,0 +1,42 @@ +package com.andrewlalis.perfin.control.component; + +import com.andrewlalis.perfin.model.TransactionAttachment; +import javafx.scene.control.Label; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.layout.BorderPane; +import javafx.scene.layout.VBox; +import javafx.scene.paint.Color; +import javafx.scene.shape.Rectangle; + +import java.io.IOException; +import java.nio.file.Files; +import java.util.Set; + +/** + * A small component that shows the basic information about an attachment, + * like its name, type, and a preview image if possible. + */ +public class AttachmentPreview extends BorderPane { + public AttachmentPreview(TransactionAttachment attachment) { + Label nameLabel = new Label(attachment.getFilename()); + Label typeLabel = new Label(attachment.getContentType()); + typeLabel.setStyle("-fx-font-size: x-small;"); + setBottom(new VBox(nameLabel, typeLabel)); + + Rectangle placeholder = new Rectangle(64.0, 64.0); + placeholder.setFill(Color.WHITE); + setCenter(placeholder); + + Set imageTypes = Set.of("image/png", "image/jpeg", "image/gif", "image/bmp"); + if (imageTypes.contains(attachment.getContentType())) { + try (var in = Files.newInputStream(attachment.getPath())) { + Image img = new Image(in, 64.0, 64.0, true, true); + setCenter(new ImageView(img)); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } +}