package org.autoplot; import java.awt.Component; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.UnsupportedFlavorException; 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.io.IOException; 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.bookmarks.BookmarksException; import org.autoplot.dom.Plot; import org.autoplot.dom.PlotElement; import org.autoplot.layout.LayoutConstants; import org.autoplot.datasource.DataSetSelector; import org.xml.sax.SAXException; /** * 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; public UriDropTargetListener(DataSetSelector dss, ApplicationModel model) { this.model = model; this.dss = dss; try { nixFileDataFlavor = new DataFlavor("text/uri-list;class=java.lang.String"); } catch (ClassNotFoundException ex) { logger.log( Level.WARNING, ex.getMessage(), ex ); } } private DataFlavor nixFileDataFlavor; /** * 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 ) { try { 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)) { if ( !haveAcceptedDrop ) { dtde.acceptDrop(DnDConstants.ACTION_COPY); haveAcceptedDrop= true; } String data = ((String) dtde.getTransferable().getTransferData(DataFlavor.stringFlavor)).trim(); 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); } } } }