Search in sources :

Example 31 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project openchemlib by Actelion.

the class TextClipboardHandler method pasteText.

public static String pasteText() {
    Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable clipboardContents = systemClipboard.getContents(null);
    if (clipboardContents != null) {
        try {
            if (clipboardContents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return (String) clipboardContents.getTransferData(DataFlavor.stringFlavor);
            }
        } catch (UnsupportedFlavorException ufe) {
            ufe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return null;
}
Also used : Transferable(java.awt.datatransfer.Transferable) Clipboard(java.awt.datatransfer.Clipboard) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 32 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project bioformats by openmicroscopy.

the class ShortcutTransferHandler method importData.

@Override
public boolean importData(JComponent comp, Transferable t) {
    try {
        // search for compatible data flavors (lists, files and strings)
        DataFlavor[] flavors = t.getTransferDataFlavors();
        int fileIndex = -1, stringIndex = -1, listIndex = -1;
        for (int i = 0; i < flavors.length; i++) {
            if (fileIndex >= 0 && stringIndex >= 0 && listIndex >= 0)
                break;
            Class<?> c = flavors[i].getRepresentationClass();
            if (fileIndex < 0 && c == File.class)
                fileIndex = i;
            if (stringIndex < 0 && c == String.class)
                stringIndex = i;
            if (listIndex < 0 && c == List.class)
                listIndex = i;
        }
        // convert data into list of objects
        List<?> list = null;
        if (listIndex >= 0) {
            list = (List<?>) t.getTransferData(flavors[listIndex]);
        } else if (fileIndex >= 0) {
            File f = (File) t.getTransferData(flavors[fileIndex]);
            list = Arrays.asList(f);
        } else if (stringIndex >= 0) {
            String s = (String) t.getTransferData(flavors[stringIndex]);
            list = Arrays.asList(s.split("[ \t\n\r\f]"));
        // StringTokenizer st = new StringTokenizer(s);
        // while (st.hasMoreTokens()) list.add(st.nextToken());
        }
        if (list == null) {
            // no compatible data flavors found
            return false;
        }
        // process each item on the list
        final String[] ids = new String[list.size()];
        for (int i = 0; i < ids.length; i++) {
            Object item = list.get(i);
            String id = null;
            if (item instanceof File) {
                File f = (File) item;
                id = f.getAbsolutePath();
            } else if (item instanceof String) {
                id = (String) item;
            }
            if (id == null) {
                System.err.println("Warning: ignoring item #" + i + ": " + item);
            } else {
                // convert "file://" URLs into path names
                ids[i] = id.replaceAll("^file:/*", "/");
                if (!new File(ids[i]).exists()) {
                    ids[i] = URLDecoder.decode(ids[i], Constants.ENCODING);
                }
            }
        }
        // open each item
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < ids.length; i++) shortcutPanel.open(ids[i]);
            }
        });
    } catch (UnsupportedFlavorException e) {
        e.printStackTrace();
        // dump list of supported flavors, for debugging
        DataFlavor[] df = t.getTransferDataFlavors();
        System.err.println("Supported flavors:");
        for (int i = 0; i < df.length; i++) {
            System.err.println("\t#" + i + ": " + df[i]);
        }
        ij.IJ.error(e.toString());
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        ij.IJ.error(e.toString());
        return false;
    }
    return true;
}
Also used : IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) DataFlavor(java.awt.datatransfer.DataFlavor) List(java.util.List) File(java.io.File)

Example 33 with UnsupportedFlavorException

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

the class DockingImportTransferHandler method importData.

@Override
public boolean importData(TransferHandler.TransferSupport info) {
    // Point point = info.getDropLocation().getDropPoint();
    // DockingPickRecord rec = overlayPanel.sampleImportPoint(point);
    // Clear overlay
    // overlayPanel.sampleImportPoint(null);
    // if (rec == null)
    // {
    // return false;
    // }
    Transferable xfer = info.getTransferable();
    DockingTransferType data = null;
    try {
        data = (DockingTransferType) xfer.getTransferData(DockingTransferType.FLAVOR);
    } catch (UnsupportedFlavorException | IOException ex) {
        getLogger(DockingImportTransferHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (data == null) {
        return false;
    }
    overlayPanel.importContent(data);
    // Clear overlay
    overlayPanel.sampleImportPoint(null);
    return true;
}
Also used : Transferable(java.awt.datatransfer.Transferable) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 34 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project logisim-evolution by reds-heig.

the class Clip method paste.

public void paste() {
    Clipboard clip = editor.getToolkit().getSystemClipboard();
    Transferable xfer = clip.getContents(this);
    int[] data;
    if (xfer.isDataFlavorSupported(binaryFlavor)) {
        try {
            data = (int[]) xfer.getTransferData(binaryFlavor);
        } catch (UnsupportedFlavorException e) {
            return;
        } catch (IOException e) {
            return;
        }
    } else if (xfer.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        String buf;
        try {
            buf = (String) xfer.getTransferData(DataFlavor.stringFlavor);
        } catch (UnsupportedFlavorException e) {
            return;
        } catch (IOException e) {
            return;
        }
        try {
            data = HexFile.parse(new StringReader(buf));
        } catch (IOException e) {
            JOptionPane.showMessageDialog(editor.getRootPane(), e.getMessage(), // Strings.get("hexPasteSupportedError"),
            Strings.get("hexPasteErrorTitle"), JOptionPane.ERROR_MESSAGE);
            return;
        }
    } else {
        JOptionPane.showMessageDialog(editor.getRootPane(), Strings.get("hexPasteSupportedError"), Strings.get("hexPasteErrorTitle"), JOptionPane.ERROR_MESSAGE);
        return;
    }
    Caret caret = editor.getCaret();
    long p0 = caret.getMark();
    long p1 = caret.getDot();
    if (p0 == p1) {
        HexModel model = editor.getModel();
        if (p0 + data.length - 1 <= model.getLastOffset()) {
            model.set(p0, data);
        } else {
            JOptionPane.showMessageDialog(editor.getRootPane(), Strings.get("hexPasteEndError"), Strings.get("hexPasteErrorTitle"), JOptionPane.ERROR_MESSAGE);
        }
    } else {
        if (p0 < 0 || p1 < 0)
            return;
        if (p0 > p1) {
            long t = p0;
            p0 = p1;
            p1 = t;
        }
        p1++;
        HexModel model = editor.getModel();
        if (p1 - p0 == data.length) {
            model.set(p0, data);
        } else {
            JOptionPane.showMessageDialog(editor.getRootPane(), Strings.get("hexPasteSizeError"), Strings.get("hexPasteErrorTitle"), JOptionPane.ERROR_MESSAGE);
        }
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) StringReader(java.io.StringReader) Clipboard(java.awt.datatransfer.Clipboard) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Caret(com.cburch.hex.Caret) HexModel(com.cburch.hex.HexModel)

Example 35 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project artisynth_core by artisynth.

the class ReadlinePanel method addPopupMenu.

private void addPopupMenu() {
    JPopupMenu menu = new JPopupMenu();
    Action copy = new DefaultEditorKit.CopyAction();
    copy.putValue(Action.NAME, "Copy");
    menu.add(copy);
    /**
     * Insert clipboard text into caret position
     */
    Action n = new TextAction("Paste") {

        private static final long serialVersionUID = 12452542145L;

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                String data = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
                adjustCaretIfNecessary();
                doInsert(data, getCaretPosition());
            } catch (HeadlessException | UnsupportedFlavorException | IOException e1) {
                e1.printStackTrace();
            }
        }
    };
    menu.add(n);
    setComponentPopupMenu(menu);
}
Also used : Action(javax.swing.Action) TextAction(javax.swing.text.TextAction) HeadlessException(java.awt.HeadlessException) ActionEvent(java.awt.event.ActionEvent) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) JPopupMenu(javax.swing.JPopupMenu) TextAction(javax.swing.text.TextAction)

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