Search in sources :

Example 91 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project android-classyshark by google.

the class FileTransferHandler method importData.

public boolean importData(TransferSupport ts) {
    try {
        @SuppressWarnings("rawtypes") List data = (List) ts.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
        if (data.size() < 1) {
            return false;
        }
        for (Object item : data) {
            File file = (File) item;
            if (isSupportedArchiveFile(file)) {
                CurrentFolderConfig.INSTANCE.setCurrentDirectory(file.getParentFile());
                RecentArchivesConfig.INSTANCE.addArchive(file.getName(), file.getParentFile());
                archiveDisplayer.displayArchive(file);
            }
        }
        return true;
    } catch (UnsupportedFlavorException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
}
Also used : List(java.util.List) IOException(java.io.IOException) File(java.io.File) FileChooserUtils.isSupportedArchiveFile(com.google.classyshark.gui.panel.io.FileChooserUtils.isSupportedArchiveFile) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 92 with UnsupportedFlavorException

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

the class TableTabClip method paste.

public void paste() {
    Clipboard clip = table.getToolkit().getSystemClipboard();
    Transferable xfer;
    try {
        xfer = clip.getContents(this);
    } catch (IllegalStateException | ArrayIndexOutOfBoundsException t) {
        // I don't know - the above was observed to throw an odd
        // ArrayIndexOutOfBounds
        // exception on a Linux computer using Sun's Java 5 JVM
        JOptionPane.showMessageDialog(table.getRootPane(), Strings.get("clipPasteSupportedError"), Strings.get("clipPasteErrorTitle"), JOptionPane.ERROR_MESSAGE);
        return;
    }
    Entry[][] entries;
    if (xfer.isDataFlavorSupported(binaryFlavor)) {
        try {
            Data data = (Data) xfer.getTransferData(binaryFlavor);
            entries = new Entry[data.contents.length][];
            for (int i = 0; i < entries.length; i++) {
                Entry[] row = new Entry[data.contents[i].length];
                for (int j = 0; j < row.length; j++) {
                    row[j] = Entry.parse(data.contents[i][j]);
                }
                entries[i] = row;
            }
        } catch (UnsupportedFlavorException e) {
            return;
        } catch (IOException e) {
            return;
        }
    } else if (xfer.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        try {
            String buf = (String) xfer.getTransferData(DataFlavor.stringFlavor);
            StringTokenizer lines = new StringTokenizer(buf, "\r\n");
            String first;
            if (!lines.hasMoreTokens())
                return;
            first = lines.nextToken();
            StringTokenizer toks = new StringTokenizer(first, "\t,");
            String[] headers = new String[toks.countTokens()];
            Entry[] firstEntries = new Entry[headers.length];
            boolean allParsed = true;
            for (int i = 0; toks.hasMoreTokens(); i++) {
                headers[i] = toks.nextToken();
                firstEntries[i] = Entry.parse(headers[i]);
                allParsed = allParsed && firstEntries[i] != null;
            }
            int rows = lines.countTokens();
            if (allParsed)
                rows++;
            entries = new Entry[rows][];
            int cur = 0;
            if (allParsed) {
                entries[0] = firstEntries;
                cur++;
            }
            while (lines.hasMoreTokens()) {
                toks = new StringTokenizer(lines.nextToken(), "\t");
                Entry[] ents = new Entry[toks.countTokens()];
                for (int i = 0; toks.hasMoreTokens(); i++) {
                    ents[i] = Entry.parse(toks.nextToken());
                }
                entries[cur] = ents;
                cur++;
            }
        } catch (UnsupportedFlavorException e) {
            return;
        } catch (IOException e) {
            return;
        }
    } else {
        JOptionPane.showMessageDialog(table.getRootPane(), Strings.get("clipPasteSupportedError"), Strings.get("clipPasteErrorTitle"), JOptionPane.ERROR_MESSAGE);
        return;
    }
    TableTabCaret caret = table.getCaret();
    int c0 = caret.getCursorCol();
    int c1 = caret.getMarkCol();
    int r0 = caret.getCursorRow();
    int r1 = caret.getMarkRow();
    if (r0 < 0 || r1 < 0 || c0 < 0 || c1 < 0)
        return;
    TruthTable model = table.getTruthTable();
    int rows = model.getRowCount();
    int inputs = model.getInputColumnCount();
    int outputs = model.getOutputColumnCount();
    if (c0 == c1 && r0 == r1) {
        if (r0 + entries.length > rows || c0 + entries[0].length > inputs + outputs) {
            JOptionPane.showMessageDialog(table.getRootPane(), Strings.get("clipPasteEndError"), Strings.get("clipPasteErrorTitle"), JOptionPane.ERROR_MESSAGE);
            return;
        }
    } else {
        if (r0 > r1) {
            int t = r0;
            r0 = r1;
            r1 = t;
        }
        if (c0 > c1) {
            int t = c0;
            c0 = c1;
            c1 = t;
        }
        if (r1 - r0 + 1 != entries.length || c1 - c0 + 1 != entries[0].length) {
            JOptionPane.showMessageDialog(table.getRootPane(), Strings.get("clipPasteSizeError"), Strings.get("clipPasteErrorTitle"), JOptionPane.ERROR_MESSAGE);
            return;
        }
    }
    for (int r = 0; r < entries.length; r++) {
        for (int c = 0; c < entries[0].length; c++) {
            if (c0 + c >= inputs) {
                model.setOutputEntry(r0 + r, c0 + c - inputs, entries[r][c]);
            }
        }
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Entry(com.cburch.logisim.analyze.model.Entry) StringTokenizer(java.util.StringTokenizer) TruthTable(com.cburch.logisim.analyze.model.TruthTable) Clipboard(java.awt.datatransfer.Clipboard)

Example 93 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project Openfire by igniterealtime.

the class DroppableTextPane method drop.

@Override
public void drop(DropTargetDropEvent dropTargetDropEvent) {
    try {
        Transferable transferable = dropTargetDropEvent.getTransferable();
        if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            dropTargetDropEvent.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            List fileList = (List) transferable.getTransferData(DataFlavor.javaFileListFlavor);
            Iterator iterator = fileList.iterator();
            while (iterator.hasNext()) {
                File file = (File) iterator.next();
                if (file.isFile()) {
                    fileDropped(file);
                }
                if (file.isDirectory()) {
                    directoryDropped(file);
                }
            }
            dropTargetDropEvent.getDropTargetContext().dropComplete(true);
        } else {
            dropTargetDropEvent.rejectDrop();
        }
    } catch (IOException | UnsupportedFlavorException io) {
        io.printStackTrace();
        dropTargetDropEvent.rejectDrop();
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) Iterator(java.util.Iterator) List(java.util.List) IOException(java.io.IOException) File(java.io.File) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 94 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project java-swing-tips by aterai.

the class FileChooserTransferHandler method importData.

@Override
public boolean importData(TransferSupport support) {
    try {
        JFileChooser fc = (JFileChooser) support.getComponent();
        List<?> list = (List<?>) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
        File[] files = new File[list.size()];
        for (int i = 0; i < list.size(); i++) {
            files[i] = (File) list.get(i);
        }
        if (fc.isMultiSelectionEnabled()) {
            fc.setSelectedFiles(files);
        } else {
            File f = files[0];
            if (f.isDirectory()) {
                fc.setCurrentDirectory(f);
            } else {
                fc.setSelectedFile(f);
            }
        }
        return true;
    } catch (IOException | UnsupportedFlavorException ex) {
        // ex.printStackTrace();
        return false;
    }
}
Also used : List(java.util.List) IOException(java.io.IOException) File(java.io.File) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 95 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project java-swing-tips by aterai.

the class TabTitleDropTargetListener method drop.

@SuppressWarnings("unchecked")
@Override
public void drop(DropTargetDropEvent e) {
    try {
        DropTargetContext c = e.getDropTargetContext();
        Component o = c.getComponent();
        Transferable t = e.getTransferable();
        DataFlavor[] f = t.getTransferDataFlavors();
        if (o instanceof JTabbedPane) {
            JTabbedPane jtp = (JTabbedPane) o;
            JScrollPane sp = (JScrollPane) jtp.getComponentAt(targetTabIndex);
            JViewport vp = sp.getViewport();
            JList<String> targetList = (JList<String>) SwingUtilities.getUnwrappedView(vp);
            JList<String> sourceList = (JList<String>) t.getTransferData(f[0]);
            DefaultListModel<String> tm = (DefaultListModel<String>) targetList.getModel();
            DefaultListModel<String> sm = (DefaultListModel<String>) sourceList.getModel();
            int[] indices = sourceList.getSelectedIndices();
            for (int j = indices.length - 1; j >= 0; j--) {
                tm.addElement(sm.remove(indices[j]));
            }
            e.dropComplete(true);
        } else {
            e.dropComplete(false);
        }
    } catch (UnsupportedFlavorException | IOException ex) {
        e.dropComplete(false);
    }
}
Also used : DropTargetContext(java.awt.dnd.DropTargetContext) Transferable(java.awt.datatransfer.Transferable) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) DataFlavor(java.awt.datatransfer.DataFlavor)

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