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;
public void drop(DropTargetDropEvent evt)
{
Transferable t = evt.getTransferable();
- java.util.List files = null;
+ java.util.List<String> files = new ArrayList<String>(), protocols = new ArrayList<String>();
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();
{
boolean success = true;
Transferable t = evt.getTransferable();
- java.util.List files = null;
- java.util.List protocols = null;
+ java.util.List<String> files = new ArrayList<String>();
+ java.util.List<String> protocols = new ArrayList<String>();
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;
}
return groovyConsole;
}
+ public static void transferFromDropTarget(List<String> files,
+ List<String> 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<String>) 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");
+ }
+ }
+ }
+ }
+ }
+ }
}