Added hover visual to attachment previews.

This commit is contained in:
Andrew Lalis 2023-12-29 22:58:29 -05:00
parent 7f85591567
commit 7d7f80676a
1 changed files with 19 additions and 5 deletions

View File

@ -4,8 +4,7 @@ import com.andrewlalis.perfin.model.TransactionAttachment;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.*;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
@ -23,27 +22,42 @@ public class AttachmentPreview extends BorderPane {
public static final double HEIGHT = IMAGE_SIZE + LABEL_SIZE; public static final double HEIGHT = IMAGE_SIZE + LABEL_SIZE;
public AttachmentPreview(TransactionAttachment attachment) { public AttachmentPreview(TransactionAttachment attachment) {
BorderPane contentContainer = new BorderPane();
Label nameLabel = new Label(attachment.getFilename()); Label nameLabel = new Label(attachment.getFilename());
nameLabel.setStyle("-fx-font-size: small;"); nameLabel.setStyle("-fx-font-size: small;");
VBox nameContainer = new VBox(nameLabel); VBox nameContainer = new VBox(nameLabel);
nameContainer.setPrefHeight(LABEL_SIZE); nameContainer.setPrefHeight(LABEL_SIZE);
nameContainer.setMaxHeight(LABEL_SIZE); nameContainer.setMaxHeight(LABEL_SIZE);
nameContainer.setMinHeight(LABEL_SIZE); nameContainer.setMinHeight(LABEL_SIZE);
setBottom(nameContainer); contentContainer.setBottom(nameContainer);
Rectangle placeholder = new Rectangle(IMAGE_SIZE, IMAGE_SIZE); Rectangle placeholder = new Rectangle(IMAGE_SIZE, IMAGE_SIZE);
placeholder.setFill(Color.WHITE); placeholder.setFill(Color.WHITE);
setCenter(placeholder); contentContainer.setCenter(placeholder);
Set<String> imageTypes = Set.of("image/png", "image/jpeg", "image/gif", "image/bmp"); Set<String> imageTypes = Set.of("image/png", "image/jpeg", "image/gif", "image/bmp");
if (imageTypes.contains(attachment.getContentType())) { if (imageTypes.contains(attachment.getContentType())) {
try (var in = Files.newInputStream(attachment.getPath())) { try (var in = Files.newInputStream(attachment.getPath())) {
Image img = new Image(in, IMAGE_SIZE, IMAGE_SIZE, true, true); Image img = new Image(in, IMAGE_SIZE, IMAGE_SIZE, true, true);
setCenter(new ImageView(img)); contentContainer.setCenter(new ImageView(img));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
BorderPane hoverIndicatorPane = new BorderPane();
hoverIndicatorPane.prefWidthProperty().bind(contentContainer.widthProperty());
hoverIndicatorPane.prefHeightProperty().bind(contentContainer.heightProperty());
hoverIndicatorPane.setBackground(new Background(new BackgroundFill(Color.rgb(186, 210, 255, 0.5), null, null)));
hoverIndicatorPane.visibleProperty().bind(this.hoverProperty());
StackPane stackPane = new StackPane(contentContainer, hoverIndicatorPane);
this.setCenter(stackPane);
this.setOnMouseClicked(event -> {
if (this.isHover()) {
System.out.println("Opening attachment: " + attachment.getFilename());
}
});
} }
} }