Search in sources :

Example 56 with UnsupportedFlavorException

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

the class EntryTableTransferHandler method importData.

/**
     * 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 library.
     *
     */
@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");
            @SuppressWarnings("unchecked") List<Path> files = ((List<File>) t.getTransferData(DataFlavor.javaFileListFlavor)).stream().map(File::toPath).collect(Collectors.toList());
            return handleDraggedFiles(files, 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;
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) URL(java.net.URL) DataFlavor(java.awt.datatransfer.DataFlavor) JTable(javax.swing.JTable) ArrayList(java.util.ArrayList) List(java.util.List)

Example 57 with UnsupportedFlavorException

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

the class DragSourceContext method readObject.

/**
     * Deserializes this <code>DragSourceContext</code>. This method first
     * performs default deserialization for all non-<code>transient</code>
     * fields. This object's <code>Transferable</code> and
     * <code>DragSourceListener</code> are then deserialized as well by using
     * the next two objects in the stream. If the resulting
     * <code>Transferable</code> is <code>null</code>, this object's
     * <code>Transferable</code> is set to a dummy <code>Transferable</code>
     * which supports no <code>DataFlavor</code>s.
     *
     * @since 1.4
     */
private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
    ObjectInputStream.GetField f = s.readFields();
    DragGestureEvent newTrigger = (DragGestureEvent) f.get("trigger", null);
    if (newTrigger == null) {
        throw new InvalidObjectException("Null trigger");
    }
    if (newTrigger.getDragSource() == null) {
        throw new InvalidObjectException("Null DragSource");
    }
    if (newTrigger.getComponent() == null) {
        throw new InvalidObjectException("Null trigger component");
    }
    int newSourceActions = f.get("sourceActions", 0) & (DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK);
    if (newSourceActions == DnDConstants.ACTION_NONE) {
        throw new InvalidObjectException("Invalid source actions");
    }
    int triggerActions = newTrigger.getDragAction();
    if (triggerActions != DnDConstants.ACTION_COPY && triggerActions != DnDConstants.ACTION_MOVE && triggerActions != DnDConstants.ACTION_LINK) {
        throw new InvalidObjectException("No drag action");
    }
    trigger = newTrigger;
    cursor = (Cursor) f.get("cursor", null);
    useCustomCursor = f.get("useCustomCursor", false);
    sourceActions = newSourceActions;
    transferable = (Transferable) s.readObject();
    listener = (DragSourceListener) s.readObject();
    // Implementation assumes 'transferable' is never null.
    if (transferable == null) {
        if (emptyTransferable == null) {
            emptyTransferable = new Transferable() {

                public DataFlavor[] getTransferDataFlavors() {
                    return new DataFlavor[0];
                }

                public boolean isDataFlavorSupported(DataFlavor flavor) {
                    return false;
                }

                public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
                    throw new UnsupportedFlavorException(flavor);
                }
            };
        }
        transferable = emptyTransferable;
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) InvalidObjectException(java.io.InvalidObjectException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Point(java.awt.Point) ObjectInputStream(java.io.ObjectInputStream) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 58 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project JMRI by JMRI.

the class DnDStringImportHandler method importData.

@Override
public boolean importData(JComponent comp, Transferable tr) {
    //if (log.isDebugEnabled()) log.debug("DnDStringImportHandler.importData ");
    DataFlavor[] flavors = tr.getTransferDataFlavors();
    if (!canImport(comp, flavors)) {
        return false;
    }
    try {
        String data = (String) tr.getTransferData(DataFlavor.stringFlavor);
        JTextField field = (JTextField) comp;
        field.setText(data);
        //Notify listeners drop happened
        field.firePropertyChange("DnDrop", 0, 1);
        return true;
    } catch (UnsupportedFlavorException ufe) {
        log.warn("DnDStringImportHandler.importData: " + ufe.getMessage());
    } catch (IOException ioe) {
        log.warn("DnDStringImportHandler.importData: " + ioe.getMessage());
    }
    return false;
}
Also used : IOException(java.io.IOException) JTextField(javax.swing.JTextField) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 59 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project JMRI by JMRI.

the class RosterEntrySelection method getRosterEntries.

/**
     * Get an ArrayList of RosterEntries from a RosterEntrySelection.
     *
     * @param t a Transferable object. This should be a RosterEntrySelection,
     *          but for simplicity, will accept any Transferable object.
     * @return the transfered roster entries
     * @throws java.awt.datatransfer.UnsupportedFlavorException if the
     *                                                          transferable is
     *                                                          incorrect
     * @throws java.io.IOException                              if unable to
     *                                                          transfer the
     *                                                          entries
     */
public static ArrayList<RosterEntry> getRosterEntries(Transferable t) throws UnsupportedFlavorException, IOException {
    if (t.isDataFlavorSupported(rosterEntryFlavor)) {
        @SuppressWarnings("unchecked") ArrayList<String> Ids = (ArrayList<String>) t.getTransferData(rosterEntryFlavor);
        ArrayList<RosterEntry> REs = new ArrayList<>(Ids.size());
        for (String Id : Ids) {
            RosterEntry re = Roster.getDefault().entryFromTitle(Id);
            if (re != null) {
                REs.add(re);
            }
        }
        return REs;
    }
    throw new UnsupportedFlavorException(t.getTransferDataFlavors()[0]);
}
Also used : ArrayList(java.util.ArrayList) RosterEntry(jmri.jmrit.roster.RosterEntry) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 60 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project JMRI by JMRI.

the class DropJLabel method drop.

@Override
public void drop(DropTargetDropEvent e) {
    try {
        Transferable tr = e.getTransferable();
        if (e.isDataFlavorSupported(_dataFlavor)) {
            NamedIcon newIcon = new NamedIcon((NamedIcon) tr.getTransferData(_dataFlavor));
            accept(e, newIcon);
        } else if (e.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String text = (String) tr.getTransferData(DataFlavor.stringFlavor);
            if (log.isDebugEnabled()) {
                log.debug("drop for stringFlavor " + text);
            }
            NamedIcon newIcon = new NamedIcon(text, text);
            accept(e, newIcon);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("DropJLabel.drop REJECTED!");
            }
            e.rejectDrop();
        }
    } catch (IOException ioe) {
        if (log.isDebugEnabled()) {
            log.debug("DropPanel.drop REJECTED!");
        }
        e.rejectDrop();
    } catch (UnsupportedFlavorException ufe) {
        if (log.isDebugEnabled()) {
            log.debug("DropJLabel.drop REJECTED!");
        }
        e.rejectDrop();
    }
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) Transferable(java.awt.datatransfer.Transferable) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Aggregations

UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)106 IOException (java.io.IOException)92 Transferable (java.awt.datatransfer.Transferable)55 List (java.util.List)32 DataFlavor (java.awt.datatransfer.DataFlavor)26 File (java.io.File)25 Point (java.awt.Point)18 ArrayList (java.util.ArrayList)15 Clipboard (java.awt.datatransfer.Clipboard)14 JTree (javax.swing.JTree)9 TreePath (javax.swing.tree.TreePath)9 JComponent (javax.swing.JComponent)7 TransferHandler (javax.swing.TransferHandler)7 DropTargetContext (java.awt.dnd.DropTargetContext)6 URL (java.net.URL)5 DefaultTableModel (javax.swing.table.DefaultTableModel)5 Image (java.awt.Image)4 ActionEvent (java.awt.event.ActionEvent)4 MouseEvent (java.awt.event.MouseEvent)4 NodeInterface (com.sldeditor.common.NodeInterface)3