Example usage for java.awt.datatransfer DataFlavor javaFileListFlavor

List of usage examples for java.awt.datatransfer DataFlavor javaFileListFlavor

Introduction

In this page you can find the example usage for java.awt.datatransfer DataFlavor javaFileListFlavor.

Prototype

DataFlavor javaFileListFlavor

To view the source code for java.awt.datatransfer DataFlavor javaFileListFlavor.

Click Source Link

Document

To transfer a list of files to/from Java (and the underlying platform) a DataFlavor of this type/subtype and representation class of java.util.List is used.

Usage

From source file:pl.otros.logview.gui.actions.read.DragAndDropFilesHandler.java

@SuppressWarnings("unchecked")
private List<File> getListOfFiles(TransferSupport support) {
    try {/*  w w w. jav a2 s .  c o  m*/
        return (List<File>) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
    } catch (UnsupportedFlavorException e) {
        throw new RuntimeException("The dropped data is not supported. The unsupported DataFlavors are "
                + StringUtils.join(support.getDataFlavors(), ","), e);
    } catch (IOException e) {
        throw new RuntimeException("Cannot read the dropped data.", e);
    }
}

From source file:org.fseek.simon.swing.filetree.dnd.FileTransferHandler.java

private File[] getFile(Transferable transferable) {
    try {/*from w w  w.j av a  2s .co  m*/
        Collection col = (Collection) transferable.getTransferData(DataFlavor.javaFileListFlavor);
        ArrayList arrList = new ArrayList(col);
        return (File[]) arrList.toArray(new File[arrList.size()]);
    } catch (UnsupportedFlavorException | IOException ex) {
        //Logger.getLogger(FileTransferhandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:net.sf.jabref.gui.fieldeditors.FileListEditorTransferHandler.java

/**
 * This method is called to query whether the transfer can be imported.
 *
 * Will return true for urls, strings, javaFileLists
 *//*w  w  w.j a v a  2  s . c o  m*/
@Override
public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {

    // accept this if any input flavor matches any of our supported flavors
    for (DataFlavor inflav : transferFlavors) {
        if (inflav.match(urlFlavor) || inflav.match(stringFlavor)
                || inflav.match(DataFlavor.javaFileListFlavor)) {
            return true;
        }
    }

    // nope, never heard of this type
    return false;
}

From source file:net.sf.jabref.gui.UrlDragDrop.java

@Override
public void drop(DropTargetDropEvent dtde) {
    Transferable tsf = dtde.getTransferable();
    dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
    //try with an URL
    DataFlavor dtURL = null;/*w w w .j ava  2 s  .co  m*/
    try {
        dtURL = new DataFlavor("application/x-java-url; class=java.net.URL");
    } catch (ClassNotFoundException e) {
        LOGGER.warn("Could not find DropTargetDropEvent class.", e);
    }
    try {
        URL url = (URL) tsf.getTransferData(dtURL);
        JOptionChoice res = (JOptionChoice) JOptionPane.showInputDialog(editor, "",
                Localization.lang("Select action"), JOptionPane.QUESTION_MESSAGE, null,
                new JOptionChoice[] { new JOptionChoice(Localization.lang("Insert URL"), 0),
                        new JOptionChoice(Localization.lang("Download file"), 1) },
                new JOptionChoice(Localization.lang("Insert URL"), 0));
        if (res != null) {
            switch (res.getId()) {
            //insert URL
            case 0:
                feditor.setText(url.toString());
                editor.updateField(feditor);
                break;
            //download file
            case 1:
                try {
                    //auto filename:
                    File file = new File(new File(Globals.prefs.get("pdfDirectory")),
                            editor.getEntry().getCiteKey() + ".pdf");
                    frame.output(Localization.lang("Downloading..."));
                    MonitoredURLDownload.buildMonitoredDownload(editor, url).downloadToFile(file);
                    frame.output(Localization.lang("Download completed"));
                    feditor.setText(file.toURI().toURL().toString());
                    editor.updateField(feditor);

                } catch (IOException ioex) {
                    LOGGER.error("Error while downloading file.", ioex);
                    JOptionPane.showMessageDialog(editor, Localization.lang("File download"),
                            Localization.lang("Error while downloading file:" + ioex.getMessage()),
                            JOptionPane.ERROR_MESSAGE);
                }
                break;
            default:
                LOGGER.warn("Unknown selection (should not happen)");
                break;
            }
        }
        return;
    } catch (UnsupportedFlavorException nfe) {
        // not an URL then...
        LOGGER.warn("Could not parse URL.", nfe);
    } catch (IOException ioex) {
        LOGGER.warn("Could not perform drag and drop.", ioex);
    }

    try {
        //try with a File List
        @SuppressWarnings("unchecked")
        List<File> filelist = (List<File>) tsf.getTransferData(DataFlavor.javaFileListFlavor);
        if (filelist.size() > 1) {
            JOptionPane.showMessageDialog(editor, Localization.lang("Only one item is supported"),
                    Localization.lang("Drag and Drop Error"), JOptionPane.ERROR_MESSAGE);
            return;
        }
        File fl = filelist.get(0);
        feditor.setText(fl.toURI().toURL().toString());
        editor.updateField(feditor);

    } catch (UnsupportedFlavorException nfe) {
        JOptionPane.showMessageDialog(editor, Localization.lang("Operation not supported"),
                Localization.lang("Drag and Drop Error"), JOptionPane.ERROR_MESSAGE);
        LOGGER.warn("Could not perform drag and drop.", nfe);
    } catch (IOException ioex) {
        LOGGER.warn("Could not perform drag and drop.", ioex);
    }

}

From source file:net.sf.jabref.groups.EntryTableTransferHandler.java

/**
 * This method is called to query whether the transfer can be imported.
 *
 * Will return true for urls, strings, javaFileLists
 *//*from  w  w w.ja v  a  2  s.co  m*/
@Override
public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
    if (!EntryTableTransferHandler.DROP_ALLOWED) {
        return false;
    }

    // accept this if any input flavor matches any of our supported flavors
    for (DataFlavor inflav : transferFlavors) {
        if (inflav.match(urlFlavor) || inflav.match(stringFlavor)
                || inflav.match(DataFlavor.javaFileListFlavor)) {
            return true;
        }
    }

    // System.out.println("drop type forbidden");
    // nope, never heard of this type
    return false;
}

From source file:EditorDropTarget4.java

protected void checkTransferType(DropTargetDragEvent dtde) {
    // Accept a list of files, or data content that
    // amounts to plain text or a Unicode text string
    acceptableType = false;//ww w .  j  a va 2 s .  co  m
    draggingFile = false;
    if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        acceptableType = true;
        draggingFile = true;
    } else if (dtde.isDataFlavorSupported(DataFlavor.plainTextFlavor)
            || dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        acceptableType = true;
    }
    DnDUtils.debugPrintln("File type acceptable - " + acceptableType);
    DnDUtils.debugPrintln("Dragging a file - " + draggingFile);
}

From source file:org.feistymeow.dragdrop.ListTransferable.java

/**
 * This function will retrieve the file list from a standard file list flavor.
 *//*from   w w w . j av a 2s  . c  o  m*/
@SuppressWarnings("unchecked")
public static List<Object> processStandardFileList(Transferable tran) {
    if (tran == null)
        return null;
    logger.debug("trying java file list flavor.");
    try {
        return (List<Object>) tran.getTransferData(DataFlavor.javaFileListFlavor);
    } catch (Throwable cause) {
        logger.debug("failed to retrieve transfer data for standard java file list flavor.", cause);
    }
    return new ArrayList<Object>();
}

From source file:EditorDropTarget4.java

protected boolean dropFile(Transferable transferable)
        throws IOException, UnsupportedFlavorException, MalformedURLException {
    List fileList = (List) transferable.getTransferData(DataFlavor.javaFileListFlavor);
    File transferFile = (File) fileList.get(0);
    final URL transferURL = transferFile.toURL();
    DnDUtils.debugPrintln("File URL is " + transferURL);

    pane.setPage(transferURL);/*from   ww w .  j  a  v  a  2 s  . c  o m*/

    return true;
}

From source file:net.rptools.tokentool.ui.TokenCompositionPanel.java

public void drop(DropTargetDropEvent dtde) {

    Transferable transferable = dtde.getTransferable();
    dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

    try {//from  w  w w .j a  va  2 s.  c om
        if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            List<URL> urls = new FileTransferableHandler().getTransferObject(transferable);

            for (URL url : urls) {
                String baseName = java.net.URLDecoder.decode(FilenameUtils.getBaseName(url.getFile()), "UTF-8");
                TokenTool.getFrame().getControlPanel().setNamePrefixField(baseName);
            }
        }
    } catch (UnsupportedFlavorException | IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        Image image = new ImageTransferableHandler().getTransferObject(transferable);
        if (!(image instanceof BufferedImage)) {
            // Convert to buffered image
            image = ImageUtil.createCompatibleImage(image);
        }

        setToken((BufferedImage) image);
    } catch (UnsupportedFlavorException ufe) {
        ufe.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    fireCompositionChanged();
}

From source file:com.mirth.connect.client.ui.EditMessageDialog.java

public void dragEnter(DropTargetDragEvent dtde) {
    try {/*from  ww  w .j  a v a2  s. com*/
        Transferable tr = dtde.getTransferable();
        if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {

            dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);

            List<File> fileList = (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor);
            Iterator<File> iterator = fileList.iterator();
            while (iterator.hasNext()) {
                iterator.next();
            }
        } else {
            dtde.rejectDrag();
        }
    } catch (Exception e) {
        dtde.rejectDrag();
    }
}