From 7c50d01653bb568a4c4749d7617f9c77a85b004e Mon Sep 17 00:00:00 2001 From: LeeWyatt Date: Fri, 27 Oct 2023 02:16:17 +0900 Subject: [PATCH 1/2] Fix PhotoView --- .../main/java/com/dlsc/gemsfx/PhotoView.java | 50 +++++++++++++------ 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/PhotoView.java b/gemsfx/src/main/java/com/dlsc/gemsfx/PhotoView.java index d64d475a..ab29da33 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/PhotoView.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/PhotoView.java @@ -1,7 +1,14 @@ package com.dlsc.gemsfx; import com.dlsc.gemsfx.skins.PhotoViewSkin; -import javafx.beans.property.*; +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.DoubleProperty; +import javafx.beans.property.ObjectProperty; +import javafx.beans.property.ReadOnlyObjectProperty; +import javafx.beans.property.ReadOnlyObjectWrapper; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleDoubleProperty; +import javafx.beans.property.SimpleObjectProperty; import javafx.collections.MapChangeListener; import javafx.css.PseudoClass; import javafx.scene.Node; @@ -20,11 +27,10 @@ import org.kordamp.ikonli.javafx.FontIcon; import org.kordamp.ikonli.materialdesign.MaterialDesign; -import javax.imageio.ImageIO; -import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.Arrays; import java.util.List; import java.util.function.Supplier; import java.util.logging.Level; @@ -55,6 +61,8 @@ public class PhotoView extends Control { private static final PseudoClass EMPTY_PSEUDO_CLASS = PseudoClass.getPseudoClass("empty"); + private static final String[] SUPPORTED_EXTENSIONS = {".bmp", ".png", ".gif", ".jpg", ".jpeg"}; + public enum ClipShape { CIRCLE, RECTANGLE @@ -119,26 +127,38 @@ public PhotoView() { }); setOnDragOver(evt -> { - if (isEditable()) { - evt.acceptTransferModes(TransferMode.ANY); + if (isEditable() && evt.getDragboard().hasFiles()) { + List files = evt.getDragboard().getFiles(); + if (files == null) { + return; + } + // check if any of the files has a supported extension + boolean hasSupportedExtension = files.stream() + .anyMatch(file -> Arrays.stream(SUPPORTED_EXTENSIONS) + .anyMatch(extension -> file.getName().endsWith(extension))); + if (hasSupportedExtension) { + evt.acceptTransferModes(TransferMode.ANY); + } } }); setOnDragDropped(evt -> { - if (isEditable()) { + if (isEditable() && evt.getDragboard().hasFiles()) { Dragboard dragboard = evt.getDragboard(); List files = dragboard.getFiles(); if (files != null) { - try { - File file = files.get(0); - BufferedImage image = ImageIO.read(file); - if (image != null) { - setPhoto(new Image(file.toURI().toURL().toExternalForm(), true)); - } - } catch (IOException e) { - LOG.log(Level.SEVERE, "error when trying to use dropped image file", e); - } + // find the first file that has a supported extension + files.stream().filter(file -> Arrays.stream(SUPPORTED_EXTENSIONS) + .anyMatch(extension -> file.getName().endsWith(extension))) + .findFirst() + .ifPresent(supportedFile -> { + try { + setPhoto(new Image(supportedFile.toURI().toURL().toExternalForm(), true)); + } catch (IOException e) { + LOG.log(Level.SEVERE, "error when trying to use dropped image file", e); + } + }); } } }); From efd2f34120badb72acfb84d4a5aeee8f55c942c5 Mon Sep 17 00:00:00 2001 From: LeeWyatt Date: Fri, 27 Oct 2023 02:27:45 +0900 Subject: [PATCH 2/2] setDropCompleted(true/false) --- gemsfx/src/main/java/com/dlsc/gemsfx/PhotoView.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/PhotoView.java b/gemsfx/src/main/java/com/dlsc/gemsfx/PhotoView.java index ab29da33..bc92e7f9 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/PhotoView.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/PhotoView.java @@ -152,13 +152,17 @@ public PhotoView() { files.stream().filter(file -> Arrays.stream(SUPPORTED_EXTENSIONS) .anyMatch(extension -> file.getName().endsWith(extension))) .findFirst() - .ifPresent(supportedFile -> { + .ifPresentOrElse(supportedFile -> { try { setPhoto(new Image(supportedFile.toURI().toURL().toExternalForm(), true)); + evt.setDropCompleted(true); } catch (IOException e) { LOG.log(Level.SEVERE, "error when trying to use dropped image file", e); + evt.setDropCompleted(false); } - }); + }, () -> evt.setDropCompleted(false)); + } else { + evt.setDropCompleted(false); } } });