From d3d78cb09cc08c650278b3b8025327e3a5412c46 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 17 Feb 2019 19:07:32 +0700 Subject: [PATCH] Refactor File DnD (and assume that "java.io.IOException: Owner failed to convert data" is normal during the DnD process and can be ignored) --- .../filebot/ui/transfer/FileTransferable.java | 32 +++++++++++++------ .../ui/transfer/TransferablePolicy.java | 3 +- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/source/net/filebot/ui/transfer/FileTransferable.java b/source/net/filebot/ui/transfer/FileTransferable.java index 12c86603..08e62c4f 100644 --- a/source/net/filebot/ui/transfer/FileTransferable.java +++ b/source/net/filebot/ui/transfer/FileTransferable.java @@ -8,6 +8,7 @@ import static net.filebot.util.FileUtilities.*; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; +import java.awt.dnd.InvalidDnDOperationException; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -88,6 +89,22 @@ public class FileTransferable implements Transferable { return isFileListFlavor(flavor); } + public static T getTransferData(Transferable tr, DataFlavor flavor, Class type) throws IOException, UnsupportedFlavorException { + Object transferData; + try { + transferData = tr.getTransferData(flavor); + } catch (IOException e) { + // java.io.IOException: Owner failed to convert data + throw new InvalidDnDOperationException(e.getMessage()); + } + + if (transferData != null && type.isInstance(transferData)) { + return type.cast(transferData); + } + + return null; + } + public static List getFilesFromTransferable(Transferable tr) throws IOException, UnsupportedFlavorException { // On Linux, if a file is dragged from a smb share to into a java application (e.g. Ubuntu Files to FileBot) // the application/x-java-file-list transfer data will be an empty list @@ -96,10 +113,7 @@ public class FileTransferable implements Transferable { if (useGVFS()) { if (tr.isDataFlavorSupported(FileTransferable.uriListFlavor)) { // file URI list flavor (Linux) - - Readable transferData = (Readable) tr.getTransferData(FileTransferable.uriListFlavor); - - try (Scanner scanner = new Scanner(transferData)) { + try (Scanner scanner = new Scanner(getTransferData(tr, FileTransferable.uriListFlavor, Readable.class))) { List files = new ArrayList(); while (scanner.hasNextLine()) { @@ -131,18 +145,16 @@ public class FileTransferable implements Transferable { // Windows / Mac and default handling if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { // file list flavor - Object transferable = tr.getTransferData(DataFlavor.javaFileListFlavor); - - if (transferable instanceof List) { - List files = (List) transferable; + List files = getTransferData(tr, DataFlavor.javaFileListFlavor, List.class); + if (files != null) { // Windows Explorer DnD / Selection Order is broken and will probably never be fixed, // so we provide an override for users that want to enforce alphanumeric sort order of files dragged in if (forceSortOrder) { return files.stream().sorted(HUMAN_NAME_ORDER).collect(toList()); + } else { + return files; } - - return files; } // on some platforms transferable data will not be available until the drop has been accepted diff --git a/source/net/filebot/ui/transfer/TransferablePolicy.java b/source/net/filebot/ui/transfer/TransferablePolicy.java index b9723d19..714f6cbf 100644 --- a/source/net/filebot/ui/transfer/TransferablePolicy.java +++ b/source/net/filebot/ui/transfer/TransferablePolicy.java @@ -27,9 +27,10 @@ public abstract class TransferablePolicy { // *after* the drop has been accepted, but canImport is called before that // just assume that the transferable will be accepted, accept will be called in importData again anyway + debug.log(Level.FINEST, e, e::getMessage); return true; } catch (Exception e) { - debug.log(Level.FINEST, e, e::getMessage); + debug.log(Level.WARNING, e, e::getMessage); return false; } }