Added start of attachment preview!

This commit is contained in:
Andrew Lalis 2023-12-29 12:23:52 -05:00
parent 01d08154e0
commit 0eb2edfc8d
3 changed files with 44 additions and 9 deletions

View File

@ -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() {

View File

@ -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;

View File

@ -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<String> 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();
}
}
}
}