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:Dropper.java

/**
 * Do the actual import./*  w  w  w .j  a  va  2 s.  c o m*/
 * 
 * @see javax.swing.TransferHandler#importData(javax.swing.JComponent,
 *      java.awt.datatransfer.Transferable)
 */
public boolean importData(JComponent comp, Transferable t) {
    DataFlavor[] flavors = t.getTransferDataFlavors();
    System.out.println("Trying to import:" + t);
    System.out.println("... which has " + flavors.length + " flavors.");
    for (int i = 0; i < flavors.length; i++) {
        DataFlavor flavor = flavors[i];
        try {
            if (flavor.equals(DataFlavor.javaFileListFlavor)) {
                System.out.println("importData: FileListFlavor");

                List l = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
                Iterator iter = l.iterator();
                while (iter.hasNext()) {
                    File file = (File) iter.next();
                    System.out.println("GOT FILE: " + file.getCanonicalPath());
                    // Now do something with the file...
                }
                return true;
            } else if (flavor.equals(DataFlavor.stringFlavor)) {
                System.out.println("importData: String Flavor");
                String fileOrURL = (String) t.getTransferData(flavor);
                System.out.println("GOT STRING: " + fileOrURL);
                try {
                    URL url = new URL(fileOrURL);
                    System.out.println("Valid URL: " + url.toString());
                    // Do something with the contents...
                    return true;
                } catch (MalformedURLException ex) {
                    System.err.println("Not a valid URL");
                    return false;
                }
                // now do something with the String.

            } else {
                System.out.println("importData rejected: " + flavor);
                // Don't return; try next flavor.
            }
        } catch (IOException ex) {
            System.err.println("IOError getting data: " + ex);
        } catch (UnsupportedFlavorException e) {
            System.err.println("Unsupported Flavor: " + e);
        }
    }
    // If you get here, I didn't like the flavor.
    Toolkit.getDefaultToolkit().beep();
    return false;
}

From source file:EditorDropTarget.java

protected void checkTransferType(DropTargetDragEvent dtde) {
    // Only accept a list of files
    acceptableType = dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor);

    DnDUtils.debugPrintln("File type acceptable - " + acceptableType);
}

From source file:ColorSink.java

/** This method is invoked when the user drops something on us */
public void drop(DropTargetDropEvent e) {
    this.setBorder(null); // Restore the default border
    Transferable t = e.getTransferable(); // Get the data that was dropped

    // Check for types of data that we support
    if (t.isDataFlavorSupported(TransferableColor.colorFlavor)) {
        // If it was a color, accept it, and use it as the background color
        e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
        try {/*  w w  w .  ja v a  2  s  .  co  m*/
            Color c = (Color) t.getTransferData(TransferableColor.colorFlavor);
            this.setBackground(c);
            e.dropComplete(true);
        } catch (Exception ex) {
            e.dropComplete(false);
        }
    } else if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        // If it was a file list, accept it, read the first file in the list
        // and display the file contents
        e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
        try {
            List files = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
            File f = (File) files.get(0);
            BufferedReader in = new BufferedReader(new FileReader(f));
            String s;
            this.setText("");
            while ((s = in.readLine()) != null)
                this.append(s);
            e.dropComplete(true);
        } catch (Exception ex) {
            e.dropComplete(false);
        }
    } else { // If it wasn't a color or a file list, reject it.
        e.rejectDrop();
        return;
    }
}

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

/**
 * This method is called when stuff is drag to the component.
 *
 * Imports the dropped URL or plain text as a new entry in the current database.
 *
 *//* w ww .j a v a  2  s  .  c  om*/
@Override
public boolean importData(JComponent comp, Transferable t) {

    // If the drop target is the main table, we want to record which
    // row the item was dropped on, to identify the entry if needed:
    int dropRow = -1;
    if (comp instanceof JTable) {
        dropRow = ((JTable) comp).getSelectedRow();
    }

    try {

        // This flavor is used for dragged file links in Windows:
        if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            // JOptionPane.showMessageDialog(null, "Received
            // javaFileListFlavor");
            List<File> l = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
            return handleDraggedFiles(l, dropRow);
        } else if (t.isDataFlavorSupported(urlFlavor)) {
            URL dropLink = (URL) t.getTransferData(urlFlavor);
            return handleDropTransfer(dropLink);
        } else if (t.isDataFlavorSupported(stringFlavor)) {
            String dropStr = (String) t.getTransferData(stringFlavor);
            LOGGER.debug("Received stringFlavor: " + dropStr);
            return handleDropTransfer(dropStr, dropRow);
        }
    } catch (IOException ioe) {
        LOGGER.error("Failed to read dropped data", ioe);
    } catch (UnsupportedFlavorException ufe) {
        LOGGER.error("Drop type error", ufe);
    }

    // all supported flavors failed
    LOGGER.info("Can't transfer input: ");
    DataFlavor[] inflavs = t.getTransferDataFlavors();
    for (DataFlavor inflav : inflavs) {
        LOGGER.info("  " + inflav);
    }

    return false;
}

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

/**
 * This method is called when stuff is drag to the component.
 *
 * Imports the dropped URL or plain text as a new entry in the current database.
 *
 *//*from w  w w.  ja v  a2s  .co m*/
@Override
public boolean importData(JComponent comp, Transferable t) {

    // If the drop target is the main table, we want to record which
    // row the item was dropped on, to identify the entry if needed:
    int dropRow = -1;
    if (comp instanceof JTable) {
        dropRow = ((JTable) comp).getSelectedRow();
    }

    try {

        // This flavor is used for dragged file links in Windows:
        if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            // JOptionPane.showMessageDialog(null, "Received
            // javaFileListFlavor");
            List<File> l = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
            return handleDraggedFiles(l, dropRow);
        } else if (t.isDataFlavorSupported(urlFlavor)) {
            URL dropLink = (URL) t.getTransferData(urlFlavor);
            return handleDropTransfer(dropLink);
        } else if (t.isDataFlavorSupported(stringFlavor)) {
            String dropStr = (String) t.getTransferData(stringFlavor);
            LOGGER.debug("Received stringFlavor: " + dropStr);
            return handleDropTransfer(dropStr, dropRow);
        }
    } catch (IOException ioe) {
        LOGGER.error("Failed to read dropped data", ioe);
    } catch (UnsupportedFlavorException | ClassCastException ufe) {
        LOGGER.error("Drop type error", ufe);
    }

    // all supported flavors failed
    LOGGER.info("Can't transfer input: ");
    DataFlavor[] inflavs = t.getTransferDataFlavors();
    for (DataFlavor inflav : inflavs) {
        LOGGER.info("  " + inflav);
    }

    return false;
}

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

public void drop(DropTargetDropEvent dtde) {
    try {//ww  w  . j a v  a  2  s.  com
        Transferable tr = dtde.getTransferable();

        if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            File file = ((List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor)).get(0);

            String dataType = PlatformUI.MIRTH_FRAME.displayNameToDataType.get(getDataType());
            DataTypeClientPlugin dataTypePlugin = LoadedExtensions.getInstance().getDataTypePlugins()
                    .get(dataType);
            if (dataTypePlugin.isBinary()) {
                byte[] content = FileUtils.readFileToByteArray(file);

                // The plugin should decide how to convert the byte array to string
                pasteBox.setText(dataTypePlugin.getTemplateString(content));
            } else {
                pasteBox.setText(FileUtils.readFileToString(file, UIConstants.CHARSET));
            }

            parent.modified = true;
        }
    } catch (Exception e) {
        dtde.rejectDrop();
    }
}

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

/**
 * a helper method that can process transfer data from either a java file list or a URI list.
 *//* ww w .  ja v  a2s .  c  o  m*/
@SuppressWarnings("unchecked")
static public List<Object> extractData(Transferable tran) {
    if (tran == null)
        return null;
    if (tran.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        logger.debug("extractData seeing java files flavor.");
        try {
            return (List<Object>) tran.getTransferData(DataFlavor.javaFileListFlavor);
        } catch (Throwable cause) {
            logger.error("extractData caught exception for java file list.", cause);
            return null;
        }
    } else if (tran.isDataFlavorSupported(ListTransferable.getURIListFlavor1())
            || tran.isDataFlavorSupported(ListTransferable.getURIListFlavor2())) {
        logger.debug("extractData seeing uri list flavor.");
        try {
            return textURIListToFileList((String) tran.getTransferData(getURIListFlavor1()));
        } catch (Throwable cause) {
            logger.error("extractData caught exception for URI list.", cause);
            return null;
        }
    }
    logger.error("extractData: Transferable did not support known data flavor.");
    return null;
}

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

public boolean importData(TransferSupport support, int action) {
    Transferable transferable = support.getTransferable();
    DataFlavor[] dataFlavors = support.getDataFlavors();
    File[] transferData = null;//w  ww .ja  v a 2 s.c om
    //check if we support one of the data flavors
    for (DataFlavor dataFlavor : dataFlavors) {
        String mimeType = dataFlavor.getMimeType();
        if (mimeType.equals(urlFlavor.getMimeType())) {
            transferData = getFromURL(transferable);
        } else if (mimeType.equals(DataFlavor.javaFileListFlavor.getMimeType())) {
            transferData = getFile(transferable);
        } else if (mimeType.equals(uriList.getMimeType())) {
            transferData = getFromFileString(transferable);
        }
        if (transferData != null) {
            return hasFile.importFile(transferData, action, support.getDropLocation());
        }
    }
    if (transferData == null) {
        Logger.getLogger(FileTransferHandler.class.getName()).log(Level.INFO, "Unsupported flavor: {0}",
                dataFlavors[0].getMimeType());
    }
    return false;
}

From source file:se.trixon.toolbox.geotagger.GeotaggerTopComponent.java

private void init() {
    openButton.setIcon(Pict.Actions.DOCUMENT_OPEN.get(TOOLBAR_ICON_SIZE));
    editToggleButton.setIcon(Pict.Actions.DOCUMENT_EDIT.get(TOOLBAR_ICON_SIZE));

    closeButton.setIcon(Pict.Actions.WINDOW_CLOSE.get(TOOLBAR_ICON_SIZE));
    closeButton.setEnabled(false);//  w  w w  .ja v  a 2 s .c o  m

    startButton.setIcon(Pict.Actions.ARROW_RIGHT.get(ICON_SIZE));
    startButton.setToolTipText(Dict.START.getString());
    startButton.setEnabled(false);

    saveLogButton.setIcon(Pict.Actions.DOCUMENT_SAVE.get(ICON_SIZE));
    saveLogButton.setEnabled(false);

    helpButton.setIcon(Pict.Actions.HELP_CONTENTS.get(ICON_SIZE));
    helpButton.setToolTipText(Dict.HELP.getString());

    DropTarget dropTarget = new DropTarget() {
        @Override
        public synchronized void drop(DropTargetDropEvent evt) {
            try {
                evt.acceptDrop(DnDConstants.ACTION_COPY);
                List<File> droppedFiles = (List<File>) evt.getTransferable()
                        .getTransferData(DataFlavor.javaFileListFlavor);
                if (droppedFiles.get(0).isFile()) {
                    openFile(droppedFiles.get(0));
                }
            } catch (UnsupportedFlavorException | IOException ex) {
            }
        }
    };

    scrollPane.setDropTarget(dropTarget);

    mTableModel = new GeotagTableModel();
    table.setModel(mTableModel);
    mTableRowSorter = new TableRowSorter<>(mTableModel);
    table.setRowSorter(mTableRowSorter);

    TableColumnModel columnModel = table.getColumnModel();

    NumericTableCellRenderer cooNumericTableCellRenderer = new NumericTableCellRenderer(Double.class, 6);
    NumericTableCellRenderer altNumericTableCellRenderer = new NumericTableCellRenderer(Double.class, 4);

    columnModel.getColumn(GeotagTableModel.COLUMN_LATITUDE).setCellRenderer(cooNumericTableCellRenderer);
    columnModel.getColumn(GeotagTableModel.COLUMN_LONGITUDE).setCellRenderer(cooNumericTableCellRenderer);
    columnModel.getColumn(GeotagTableModel.COLUMN_ALTITUDE).setCellRenderer(altNumericTableCellRenderer);

    columnModel.getColumn(GeotagTableModel.COLUMN_NAME).setPreferredWidth(200);
    columnModel.getColumn(GeotagTableModel.COLUMN_LATITUDE).setPreferredWidth(100);
    columnModel.getColumn(GeotagTableModel.COLUMN_LONGITUDE).setPreferredWidth(100);
    columnModel.getColumn(GeotagTableModel.COLUMN_ALTITUDE).setPreferredWidth(50);
}

From source file:EditorDropTarget2.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;//from ww  w  . j a  va  2  s  . c  om
    draggingFile = false;

    if (DnDUtils.isDebugEnabled()) {
        DataFlavor[] flavors = dtde.getCurrentDataFlavors();
        for (int i = 0; i < flavors.length; i++) {
            DataFlavor flavor = flavors[i];
            DnDUtils.debugPrintln("Drop MIME type " + flavor.getMimeType() + " is available");
        }
    }

    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);
}