Search in sources :

Example 21 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project jdk8u_jdk by JetBrains.

the class SunDragSourceContextPeer method startDrag.

/**
     * initiate a DnD operation ...
     */
public void startDrag(DragSourceContext dsc, Cursor c, Image di, Point p) throws InvalidDnDOperationException {
    /* Fix for 4354044: don't initiate a drag if event sequence provided by
         * DragGestureRecognizer is empty */
    if (getTrigger().getTriggerEvent() == null) {
        throw new InvalidDnDOperationException("DragGestureEvent has a null trigger");
    }
    dragSourceContext = dsc;
    cursor = c;
    sourceActions = getDragSourceContext().getSourceActions();
    dragImage = di;
    dragImageOffset = p;
    Transferable transferable = getDragSourceContext().getTransferable();
    SortedMap<Long, DataFlavor> formatMap = DataTransferer.getInstance().getFormatsForTransferable(transferable, DataTransferer.adaptFlavorMap(getTrigger().getDragSource().getFlavorMap()));
    long[] formats = DataTransferer.getInstance().keysToLongArray(formatMap);
    startDrag(transferable, formats, formatMap);
    /*
         * Fix for 4613903.
         * Filter out all mouse events that are currently on the event queue.
         */
    discardingMouseEvents = true;
    EventQueue.invokeLater(new Runnable() {

        public void run() {
            discardingMouseEvents = false;
        }
    });
}
Also used : InvalidDnDOperationException(java.awt.dnd.InvalidDnDOperationException) Transferable(java.awt.datatransfer.Transferable) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 22 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project jdk8u_jdk by JetBrains.

the class XDataTransferer method getPlatformMappingsForNative.

/*
     * The XDnD protocol prescribes that the Atoms used as targets for data
     * transfer should have string names that represent the corresponding MIME
     * types.
     * To meet this requirement we check if the passed native format constitutes
     * a valid MIME and return a list of flavors to which the data in this MIME
     * type can be translated by the Data Transfer subsystem.
     */
public LinkedHashSet<DataFlavor> getPlatformMappingsForNative(String nat) {
    LinkedHashSet<DataFlavor> flavors = new LinkedHashSet<>();
    if (nat == null) {
        return flavors;
    }
    DataFlavor df = null;
    try {
        df = new DataFlavor(nat);
    } catch (Exception e) {
        // The string doesn't constitute a valid MIME type.
        return flavors;
    }
    DataFlavor value = df;
    final String primaryType = df.getPrimaryType();
    final String baseType = primaryType + "/" + df.getSubType();
    // See SystemFlavorMap.getFlavorsForNative() for details.
    if ("image".equals(primaryType)) {
        Iterator readers = ImageIO.getImageReadersByMIMEType(baseType);
        if (readers.hasNext()) {
            flavors.add(DataFlavor.imageFlavor);
        }
    }
    flavors.add(value);
    return flavors;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Iterator(java.util.Iterator) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 23 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project jdk8u_jdk by JetBrains.

the class WClipboard method setContentsNative.

@Override
protected void setContentsNative(Transferable contents) {
    // Don't use delayed Clipboard rendering for the Transferable's data.
    // If we did that, we would call Transferable.getTransferData on
    // the Toolkit thread, which is a security hole.
    //
    // Get all of the target formats into which the Transferable can be
    // translated. Then, for each format, translate the data and post
    // it to the Clipboard.
    Map<Long, DataFlavor> formatMap = WDataTransferer.getInstance().getFormatsForTransferable(contents, getDefaultFlavorTable());
    openClipboard(this);
    try {
        for (Long format : formatMap.keySet()) {
            DataFlavor flavor = formatMap.get(format);
            try {
                byte[] bytes = WDataTransferer.getInstance().translateTransferable(contents, flavor, format);
                publishClipboardData(format, bytes);
            } catch (IOException e) {
                // May remove this if-check when 5078787 is fixed.
                if (!(flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType) && e instanceof java.io.NotSerializableException)) {
                    e.printStackTrace();
                }
            }
        }
    } finally {
        closeClipboard();
    }
}
Also used : IOException(java.io.IOException) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 24 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project jabref by JabRef.

the class FileListEditorTransferHandler method importData.

@Override
public boolean importData(JComponent comp, Transferable t) {
    try {
        List<Path> files = new ArrayList<>();
        // This flavor is used for dragged file links in Windows:
        if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            @SuppressWarnings("unchecked") List<Path> transferedFiles = (List<Path>) t.getTransferData(DataFlavor.javaFileListFlavor);
            files.addAll(transferedFiles);
        }
        if (t.isDataFlavorSupported(urlFlavor)) {
            URL dropLink = (URL) t.getTransferData(urlFlavor);
            LOGGER.debug("URL: " + dropLink);
        }
        // under Gnome. The data consists of the file paths, one file per line:
        if (t.isDataFlavorSupported(stringFlavor)) {
            String dropStr = (String) t.getTransferData(stringFlavor);
            files.addAll(EntryTableTransferHandler.getFilesFromDraggedFilesString(dropStr));
        }
        SwingUtilities.invokeLater(() -> {
            for (Path file : files) {
                // Find the file's extension, if any:
                String name = file.toAbsolutePath().toString();
                FileHelper.getFileExtension(name).ifPresent(extension -> ExternalFileTypes.getInstance().getExternalFileTypeByExt(extension).ifPresent(fileType -> {
                    if (droppedFileHandler == null) {
                        droppedFileHandler = new DroppedFileHandler(frame, frame.getCurrentBasePanel());
                    }
                    droppedFileHandler.handleDroppedfile(name, fileType, entryContainer.getEntry());
                }));
            }
        });
        if (!files.isEmpty()) {
            // Found some files, return
            return true;
        }
    } catch (IOException ioe) {
        LOGGER.warn("Failed to read dropped data. ", ioe);
    } catch (UnsupportedFlavorException | ClassCastException ufe) {
        LOGGER.warn("Drop type error. ", ufe);
    }
    // all supported flavors failed
    StringBuilder logMessage = new StringBuilder("Cannot transfer input:");
    DataFlavor[] inflavs = t.getTransferDataFlavors();
    for (DataFlavor inflav : inflavs) {
        logMessage.append(' ').append(inflav);
    }
    LOGGER.warn(logMessage.toString());
    return false;
}
Also used : Path(java.nio.file.Path) Clipboard(java.awt.datatransfer.Clipboard) JComponent(javax.swing.JComponent) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) DataFlavor(java.awt.datatransfer.DataFlavor) URL(java.net.URL) Transferable(java.awt.datatransfer.Transferable) DnDConstants(java.awt.dnd.DnDConstants) IOException(java.io.IOException) EntryTableTransferHandler(org.jabref.gui.groups.EntryTableTransferHandler) ArrayList(java.util.ArrayList) FileHelper(org.jabref.model.util.FileHelper) List(java.util.List) SwingUtilities(javax.swing.SwingUtilities) DroppedFileHandler(org.jabref.gui.externalfiles.DroppedFileHandler) JabRefFrame(org.jabref.gui.JabRefFrame) TransferHandler(javax.swing.TransferHandler) Log(org.apache.commons.logging.Log) ExternalFileTypes(org.jabref.gui.externalfiletype.ExternalFileTypes) LogFactory(org.apache.commons.logging.LogFactory) Path(java.nio.file.Path) EntryContainer(org.jabref.gui.EntryContainer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DroppedFileHandler(org.jabref.gui.externalfiles.DroppedFileHandler) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) URL(java.net.URL) DataFlavor(java.awt.datatransfer.DataFlavor) ArrayList(java.util.ArrayList) List(java.util.List)

Example 25 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project beast-mcmc by beast-dev.

the class FileDrop method isDragOk.

// end dropListener
/** Determine if the dragged data is a file list. */
private boolean isDragOk(final java.io.PrintStream out, final java.awt.dnd.DropTargetDragEvent evt) {
    boolean ok = false;
    // Get data flavors being dragged
    java.awt.datatransfer.DataFlavor[] flavors = evt.getCurrentDataFlavors();
    // See if any of the flavors are a file list
    int i = 0;
    while (!ok && i < flavors.length) {
        // BEGIN 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
        // Is the flavor a file list?
        final DataFlavor curFlavor = flavors[i];
        if (curFlavor.equals(java.awt.datatransfer.DataFlavor.javaFileListFlavor) || curFlavor.isRepresentationClassReader()) {
            ok = true;
        }
        // END 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
        i++;
    }
    // If logging is enabled, show data flavors
    if (out != null) {
        if (flavors.length == 0)
            log(out, "FileDrop: no data flavors.");
        for (i = 0; i < flavors.length; i++) log(out, flavors[i].toString());
    }
    return ok;
}
Also used : DataFlavor(java.awt.datatransfer.DataFlavor)

Aggregations

DataFlavor (java.awt.datatransfer.DataFlavor)50 IOException (java.io.IOException)12 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)11 JPanel (javax.swing.JPanel)11 Transferable (java.awt.datatransfer.Transferable)10 JLabel (javax.swing.JLabel)9 List (java.util.List)8 Point (java.awt.Point)7 ArrayList (java.util.ArrayList)7 Clipboard (java.awt.datatransfer.Clipboard)6 NamedIcon (jmri.jmrit.catalog.NamedIcon)6 URL (java.net.URL)4 DragJLabel (jmri.jmrit.catalog.DragJLabel)4 File (java.io.File)3 BorderLayout (java.awt.BorderLayout)2 GraphicsConfiguration (java.awt.GraphicsConfiguration)2 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 Image (java.awt.Image)2 Insets (java.awt.Insets)2