package org.autoplot; import java.awt.Component; import java.awt.datatransfer.DataFlavor; import java.awt.dnd.DnDConstants; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import org.das2.graph.DasCanvasComponent; import org.das2.graph.DasPlot; import org.das2.util.LoggerManager; import org.autoplot.bookmarks.Bookmark; import org.autoplot.dom.Plot; import org.autoplot.dom.PlotElement; import org.autoplot.layout.LayoutConstants; import org.autoplot.datasource.DataSetSelector; /** * DropTarget allows URIs to be dropped onto plots of the page. * @author jbf */ public class UriDropTargetListener implements DropTargetListener { private static final Logger logger= LoggerManager.getLogger("autoplot.gui.droptarget"); DataSetSelector dss; ApplicationModel model; List supportedFlavors; public UriDropTargetListener(DataSetSelector dss, ApplicationModel model) { this.model = model; this.dss = dss; supportedFlavors= new ArrayList<>(); supportedFlavors.add("text/uri-list;class=java.lang.String"); supportedFlavors.add("application/x-java-url;class=java.net.URL"); supportedFlavors.add("application/x-java-file-list;class=java.util.List"); //TODO: make sure this list is consistent with getURI code, and eventually make into loop. } /** * get the URI when a reference is dropped on to Autoplot. This is quite * platform-specific, how the drop appears, and a number of different * transfer types are queried to see if a URI can be found. * * @param dtde the drop target. * @return the URI or null. */ private String getURI( DropTargetDropEvent dtde ) { boolean haveAcceptedDrop= false; Bookmark item = null; List items = null; if ( logger.isLoggable(Level.FINE) ) { for ( DataFlavor df: dtde.getCurrentDataFlavors() ) { logger.log(Level.FINE, "drop data flavor: {0} {1}", new Object[]{df.getMimeType(), df.getHumanPresentableName()}); } } if (dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) { try { dtde.acceptDrop(DnDConstants.ACTION_COPY); haveAcceptedDrop= true; logger.fine("looking, try using DataFlavor.stringFlavor to get uri"); String data = ((String) dtde.getTransferable().getTransferData(DataFlavor.stringFlavor)).trim(); logger.log(Level.FINER, "got string data: {0}", data); if (data.length() > 19 && data.startsWith(" 14 && data.startsWith(" pe = model.dom.getController().getPlotElementsFor(plot); if (pe.isEmpty()) { model.showMessage("no plot elements here", "no plot elements", JOptionPane.WARNING_MESSAGE); return; } model.dom.getController().setPlotElement(pe.get(0)); // set the focus dss.setValue(uri); dss.maybePlot(true); } } } }