From c2d226178eb094ce4aa8eccfa5f436ee7ba4c9fb Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Thu, 26 May 2016 14:58:40 +0100 Subject: [PATCH] JAL-1552 refactor shared dataflavor hander JAL-1552 fix: fallback to text/plain if other flavors fail --- src/jalview/gui/AlignFrame.java | 74 +-------------------- src/jalview/gui/Desktop.java | 140 +++++++++++++++++++++++++++------------ 2 files changed, 100 insertions(+), 114 deletions(-) diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 3e3780a..401ccb7 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -113,7 +113,6 @@ import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; -import java.awt.dnd.DnDConstants; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetEvent; @@ -5030,80 +5029,11 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, public void drop(DropTargetDropEvent evt) { Transferable t = evt.getTransferable(); - java.util.List files = null; + java.util.List files = new ArrayList(), protocols = new ArrayList(); try { - DataFlavor uriListFlavor = new DataFlavor( - "text/uri-list;class=java.lang.String"); - if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) - { - Cache.log.debug("Drop handled as javaFileListFlavor"); - // Works on Windows and MacOSX - evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); - files = (java.util.List) t - .getTransferData(DataFlavor.javaFileListFlavor); - } - else if (t.isDataFlavorSupported(uriListFlavor)) - { - Cache.log.debug("Drop handled as uriListFlavor"); - // This is used by Unix drag system - evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); - String data = (String) t.getTransferData(uriListFlavor); - if (data == null) - { - Cache.log.debug("standard URIListFlavor (" + uriListFlavor - + ") doesn't resolve. trying others."); - // try 'best' dataflavor - data = (String) t.getTransferData(DataFlavor - .selectBestTextFlavor(t.getTransferDataFlavors())); - Cache.log.debug("Dataflavor " - + DataFlavor.selectBestTextFlavor(t - .getTransferDataFlavors()) + " returned " + data); - } - files = new java.util.ArrayList(1); - for (java.util.StringTokenizer st = new java.util.StringTokenizer( - data, "\r\n"); st.hasMoreTokens();) - { - String s = st.nextToken(); - if (s.startsWith("#")) - { - // the line is a comment (as per the RFC 2483) - continue; - } - - java.net.URI uri = new java.net.URI(s); - // check to see if we can handle this kind of URI - if (uri.getScheme().toLowerCase().startsWith("http")) - { - files.add(uri.toString()); - } - else - { - // otherwise preserve old behaviour: catch all for file objects - java.io.File file = new java.io.File(uri); - files.add(file.toString()); - } - } - if (files.size() < 1) - { - Cache.log - .debug("Couldn't resolve drop data with 'best text'. Here are the supported flavors:"); - if (data == null && Cache.log.isDebugEnabled()) - { - for (DataFlavor fl : t.getTransferDataFlavors()) - { - Cache.log.debug("Supported transfer dataflavor: " - + fl.toString()); - Object df = t.getTransferData(fl); - if (df != null) - { - Cache.log.debug("Retrieves: " + df); - } - } - } - } - } + Desktop.transferFromDropTarget(files, protocols, evt, t); } catch (Exception e) { e.printStackTrace(); diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index 7f36f7f..94f2e82 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -952,53 +952,15 @@ public class Desktop extends jalview.jbgui.GDesktop implements { boolean success = true; Transferable t = evt.getTransferable(); - java.util.List files = null; - java.util.List protocols = null; + java.util.List files = new ArrayList(); + java.util.List protocols = new ArrayList(); try { - DataFlavor uriListFlavor = new DataFlavor( - "text/uri-list;class=java.lang.String"); - if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) - { - // Works on Windows and MacOSX - evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); - files = (java.util.List) t - .getTransferData(DataFlavor.javaFileListFlavor); - } - else if (t.isDataFlavorSupported(uriListFlavor)) - { - // This is used by Unix drag system - evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); - String data = (String) t.getTransferData(uriListFlavor); - files = new java.util.ArrayList(1); - protocols = new java.util.ArrayList(1); - for (java.util.StringTokenizer st = new java.util.StringTokenizer( - data, "\r\n"); st.hasMoreTokens();) - { - String s = st.nextToken(); - if (s.startsWith("#")) - { - // the line is a comment (as per the RFC 2483) - continue; - } - java.net.URI uri = new java.net.URI(s); - if (uri.getScheme().toLowerCase().startsWith("http")) - { - protocols.add(FormatAdapter.URL); - files.add(uri.toString()); - } - else - { - // otherwise preserve old behaviour: catch all for file objects - java.io.File file = new java.io.File(uri); - protocols.add(FormatAdapter.FILE); - files.add(file.toString()); - } - } - } + Desktop.transferFromDropTarget(files, protocols, evt, t); } catch (Exception e) { + e.printStackTrace(); success = false; } @@ -3190,4 +3152,98 @@ public class Desktop extends jalview.jbgui.GDesktop implements return groovyConsole; } + public static void transferFromDropTarget(List files, + List protocols, DropTargetDropEvent evt, Transferable t) + throws Exception + { + + DataFlavor uriListFlavor = new DataFlavor( + "text/uri-list;class=java.lang.String"); + if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) + { + // Works on Windows and MacOSX + Cache.log.debug("Drop handled as javaFileListFlavor"); + evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); + files.addAll((java.util.List) t + .getTransferData(DataFlavor.javaFileListFlavor)); + } + else + { + // Unix like behaviour + boolean added = false; + String data = null; + if (t.isDataFlavorSupported(uriListFlavor)) + { + Cache.log.debug("Drop handled as uriListFlavor"); + // This is used by Unix drag system + evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); + data = (String) t.getTransferData(uriListFlavor); + } + if (data == null) + { + // fallback to text: workaround - on OSX where there's a JVM bug + Cache.log.debug("standard URIListFlavor failed. Trying text"); + // try text fallback + data = (String) t.getTransferData(new DataFlavor( + "text/plain;class=java.lang.String")); + if (Cache.log.isDebugEnabled()) + { + Cache.log.debug("fallback returned " + data); + } + } + while (protocols.size() < files.size()) + { + Cache.log.debug("Adding missing FILE protocol for " + + files.get(protocols.size())); + protocols.add(FormatAdapter.FILE); + } + for (java.util.StringTokenizer st = new java.util.StringTokenizer( + data, "\r\n"); st.hasMoreTokens();) + { + added = true; + String s = st.nextToken(); + if (s.startsWith("#")) + { + // the line is a comment (as per the RFC 2483) + continue; + } + java.net.URI uri = new java.net.URI(s); + if (uri.getScheme().toLowerCase().startsWith("http")) + { + protocols.add(FormatAdapter.URL); + files.add(uri.toString()); + } + else + { + // otherwise preserve old behaviour: catch all for file objects + java.io.File file = new java.io.File(uri); + protocols.add(FormatAdapter.FILE); + files.add(file.toString()); + } + } + if (Cache.log.isDebugEnabled()) + { + if (data == null || !added) + { + Cache.log + .debug("Couldn't resolve drop data. Here are the supported flavors:"); + for (DataFlavor fl : t.getTransferDataFlavors()) + { + Cache.log.debug("Supported transfer dataflavor: " + + fl.toString()); + evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); + Object df = t.getTransferData(fl); + if (df != null) + { + Cache.log.debug("Retrieves: " + df); + } + else + { + Cache.log.debug("Retrieved nothing"); + } + } + } + } + } + } } -- 1.7.10.2