public void drop(DropTargetDropEvent dtde) { dtde.acceptDrop(DnDConstants.ACTION_COPY); DataFlavor[] flavors = null; try { Transferable t = dtde.getTransferable(); iterator = null; flavors = t.getTransferDataFlavors(); if (IJ.debugMode) IJ.log("DragAndDrop.drop: " + flavors.length + " flavors"); for (int i = 0; i < flavors.length; i++) { if (IJ.debugMode) IJ.log(" flavor[" + i + "]: " + flavors[i].getMimeType()); if (flavors[i].isFlavorJavaFileListType()) { Object data = t.getTransferData(DataFlavor.javaFileListFlavor); iterator = ((List) data).iterator(); break; } else if (flavors[i].isFlavorTextType()) { Object ob = t.getTransferData(flavors[i]); if (!(ob instanceof String)) continue; String s = ob.toString().trim(); if (IJ.isLinux() && s.length() > 1 && (int) s.charAt(1) == 0) s = fixLinuxString(s); ArrayList list = new ArrayList(); if (s.indexOf("href=\"") != -1 || s.indexOf("src=\"") != -1) { s = parseHTML(s); if (IJ.debugMode) IJ.log(" url: " + s); list.add(s); this.iterator = list.iterator(); break; } BufferedReader br = new BufferedReader(new StringReader(s)); String tmp; while (null != (tmp = br.readLine())) { tmp = java.net.URLDecoder.decode(tmp.replaceAll("\\+", "%2b"), "UTF-8"); if (tmp.startsWith("file://")) tmp = tmp.substring(7); if (IJ.debugMode) IJ.log(" content: " + tmp); if (tmp.startsWith("http://")) list.add(s); else list.add(new File(tmp)); } this.iterator = list.iterator(); break; } } if (iterator != null) { Thread thread = new Thread(this, "DrawAndDrop"); thread.setPriority(Math.max(thread.getPriority() - 1, Thread.MIN_PRIORITY)); thread.start(); } } catch (Exception e) { dtde.dropComplete(false); return; } dtde.dropComplete(true); if (flavors == null || flavors.length == 0) { if (IJ.isMacOSX()) IJ.error( "First drag and drop ignored. Please try again. You can avoid this\n" + "problem by dragging to the toolbar instead of the status bar."); else IJ.error("Drag and drop failed"); } }
/** * Returns the contents of the specified transferable. * * @param tr transferable * @return contents */ @SuppressWarnings("unchecked") public static ArrayList<Object> contents(final Transferable tr) { final ArrayList<Object> list = new ArrayList<>(); try { if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { for (final File fl : (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor)) list.add(fl); } else if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) { list.add(tr.getTransferData(DataFlavor.stringFlavor)); } } catch (final Exception ex) { Util.stack(ex); } return list; }
/** * Returns an object which represents the data to be transferred. The class of the object returned * is defined by the representation class of the flavor. * * @param flavor the requested flavor for the data * @see DataFlavor#getRepresentationClass * @exception IOException if the data is no longer available in the requested flavor. * @exception UnsupportedFlavorException if the requested data flavor is not supported. */ @Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { Transferable t = (Transferable) transferables.get(flavor); if (t == null) { throw new UnsupportedFlavorException(flavor); } return t.getTransferData(flavor); }
/** * Gets a Reader for a text flavor, decoded, if necessary, for the expected charset (encoding). * The supported representation classes are <code>java.io.Reader</code>, <code>java.lang.String * </code>, <code>java.nio.CharBuffer</code>, <code>[C</code>, <code>java.io.InputStream</code>, * <code>java.nio.ByteBuffer</code>, and <code>[B</code>. * * <p>Because text flavors which do not support the charset parameter are encoded in a * non-standard format, this method should not be called for such flavors. However, in order to * maintain backward-compatibility, if this method is called for such a flavor, this method will * treat the flavor as though it supports the charset parameter and attempt to decode it * accordingly. See <code>selectBestTextFlavor</code> for a list of text flavors which do not * support the charset parameter. * * @param transferable the <code>Transferable</code> whose data will be requested in this flavor * @return a <code>Reader</code> to read the <code>Transferable</code>'s data * @exception IllegalArgumentException if the representation class is not one of the seven listed * above * @exception IllegalArgumentException if the <code>Transferable</code> has <code>null</code> data * @exception NullPointerException if the <code>Transferable</code> is <code>null</code> * @exception UnsupportedEncodingException if this flavor's representation is <code> * java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>, or <code>[B</code> and this * flavor's encoding is not supported by this implementation of the Java platform * @exception UnsupportedFlavorException if the <code>Transferable</code> does not support this * flavor * @exception IOException if the data cannot be read because of an I/O error * @see #selectBestTextFlavor * @since 1.3 */ public Reader getReaderForText(Transferable transferable) throws UnsupportedFlavorException, IOException { Object transferObject = transferable.getTransferData(this); if (transferObject == null) { throw new IllegalArgumentException("getTransferData() returned null"); } if (transferObject instanceof Reader) { return (Reader) transferObject; } else if (transferObject instanceof String) { return new StringReader((String) transferObject); } else if (transferObject instanceof CharBuffer) { CharBuffer buffer = (CharBuffer) transferObject; int size = buffer.remaining(); char[] chars = new char[size]; buffer.get(chars, 0, size); return new CharArrayReader(chars); } else if (transferObject instanceof char[]) { return new CharArrayReader((char[]) transferObject); } InputStream stream = null; if (transferObject instanceof InputStream) { stream = (InputStream) transferObject; } else if (transferObject instanceof ByteBuffer) { ByteBuffer buffer = (ByteBuffer) transferObject; int size = buffer.remaining(); byte[] bytes = new byte[size]; buffer.get(bytes, 0, size); stream = new ByteArrayInputStream(bytes); } else if (transferObject instanceof byte[]) { stream = new ByteArrayInputStream((byte[]) transferObject); } if (stream == null) { throw new IllegalArgumentException( "transfer data is not Reader, String, CharBuffer, char array, InputStream, ByteBuffer, or byte array"); } String encoding = getParameter("charset"); return (encoding == null) ? new InputStreamReader(stream) : new InputStreamReader(stream, encoding); }
/** * Paste the contents of the clipboard into the console buffer * * @return true if clipboard contents pasted */ public boolean paste() throws IOException { Clipboard clipboard; try { // May throw ugly exception on system without X clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); } catch (Exception e) { return false; } if (clipboard == null) { return false; } Transferable transferable = clipboard.getContents(null); if (transferable == null) { return false; } try { Object content = transferable.getTransferData(DataFlavor.plainTextFlavor); /* * This fix was suggested in bug #1060649 at * http://sourceforge.net/tracker/index.php?func=detail&aid=1060649&group_id=64033&atid=506056 * to get around the deprecated DataFlavor.plainTextFlavor, but it * raises a UnsupportedFlavorException on Mac OS X */ if (content == null) { try { content = new DataFlavor().getReaderForText(transferable); } catch (Exception e) { } } if (content == null) { return false; } String value; if (content instanceof Reader) { // TODO: we might want instead connect to the input stream // so we can interpret individual lines value = ""; String line = null; for (BufferedReader read = new BufferedReader((Reader) content); (line = read.readLine()) != null; ) { if (value.length() > 0) { value += "\n"; } value += line; } } else { value = content.toString(); } if (value == null) { return true; } putString(value); return true; } catch (UnsupportedFlavorException ufe) { if (debugger != null) debug(ufe + ""); return false; } }