Search in sources :

Example 6 with Transferable

use of java.awt.datatransfer.Transferable in project binnavi by google.

the class AbstractTreeTransferHandler method dragEnter.

@Override
public final void dragEnter(final DropTargetDragEvent dtde) {
    final Point pt = dtde.getLocation();
    final int action = dtde.getDropAction();
    if (drawImage) {
        paintImage(pt);
    }
    final Transferable transferable = dtde.getTransferable();
    if (!transferable.isDataFlavorSupported(TransferableNode.NODE_FLAVOR)) {
        if (canPerformAction(tree, dtde.getCurrentDataFlavorsAsList().get(0), dtde.getTransferable(), action, pt)) {
            dtde.acceptDrag(action);
        } else {
            dtde.rejectDrag();
        }
    } else {
        if (canPerformAction(tree, draggedNode, action, pt)) {
            dtde.acceptDrag(action);
        } else {
            dtde.rejectDrag();
        }
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) Point(java.awt.Point) Point(java.awt.Point)

Example 7 with Transferable

use of java.awt.datatransfer.Transferable in project binnavi by google.

the class AbstractTreeTransferHandler method drop.

@Override
public final void drop(final DropTargetDropEvent dtde) {
    if (drawImage) {
        clearImage();
    }
    final int action = dtde.getDropAction();
    final Transferable transferable = dtde.getTransferable();
    final Point pt = dtde.getLocation();
    if (transferable.isDataFlavorSupported(TransferableNode.NODE_FLAVOR) && canPerformAction(tree, draggedNode, action, pt)) {
        boolean gotData = false;
        DefaultMutableTreeNode node = null;
        try {
            node = (DefaultMutableTreeNode) transferable.getTransferData(TransferableNode.NODE_FLAVOR);
            gotData = true;
        } catch (final IOException e) {
            // TODO: This should be properly logged
            System.out.println(e);
        } catch (final UnsupportedFlavorException e) {
            // TODO: This should be properly logged
            System.out.println(e);
        }
        if (gotData) {
            final TreePath pathTarget = tree.getPathForLocation(pt.x, pt.y);
            final DefaultMutableTreeNode newParentNode = (DefaultMutableTreeNode) pathTarget.getLastPathComponent();
            if (executeDrop(tree, node, newParentNode, action)) {
                dtde.acceptDrop(action);
                dtde.dropComplete(true);
                return;
            }
        }
    } else if (canPerformAction(tree, dtde.getCurrentDataFlavors()[0], dtde.getTransferable(), action, pt)) {
        final TreePath pathTarget = tree.getPathForLocation(pt.x, pt.y);
        final DefaultMutableTreeNode newParentNode = (DefaultMutableTreeNode) pathTarget.getLastPathComponent();
        if (executeDrop(tree, dtde.getTransferable(), newParentNode, action)) {
            dtde.acceptDrop(action);
            dtde.dropComplete(true);
            return;
        }
    }
    dtde.rejectDrop();
    dtde.dropComplete(false);
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreePath(javax.swing.tree.TreePath) Transferable(java.awt.datatransfer.Transferable) Point(java.awt.Point) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Point(java.awt.Point)

Example 8 with Transferable

use of java.awt.datatransfer.Transferable in project zaproxy by zaproxy.

the class ScanProgressDialog method getCopyToClipboardButton.

private JButton getCopyToClipboardButton() {
    if (copyToClipboardButton == null) {
        copyToClipboardButton = new JButton(Constant.messages.getString("ascan.progress.copyclipboard.button.label"));
        copyToClipboardButton.setToolTipText(Constant.messages.getString("ascan.progress.copyclipboard.button.tooltip"));
        copyToClipboardButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                // Mimics the implementation of BasicTableUI.TableTransferHandler.createTransferable(JComponent) but copies
                // all rows (including column names), not just selected rows/columns (which are none in this case).
                StringBuilder plainContent = new StringBuilder();
                StringBuilder htmlContent = new StringBuilder();
                htmlContent.append("<html>\n<body>\n<table>\n");
                TableModel tableModel = getMainPanel().getModel();
                htmlContent.append("<tr>\n");
                for (int col = 0; col < tableModel.getColumnCount(); col++) {
                    String val = tableModel.getColumnName(col);
                    plainContent.append(val).append('\t');
                    htmlContent.append("  <td>").append(val).append("</td>\n");
                }
                plainContent.deleteCharAt(plainContent.length() - 1).append("\n");
                htmlContent.append("</tr>\n");
                for (int row = 0; row < tableModel.getRowCount(); row++) {
                    htmlContent.append("<tr>\n");
                    for (int col = 0; col < tableModel.getColumnCount(); col++) {
                        Object obj = tableModel.getValueAt(row, col);
                        String val = (obj == null) ? "" : obj.toString();
                        plainContent.append(val).append('\t');
                        htmlContent.append("  <td>").append(val).append("</td>\n");
                    }
                    plainContent.deleteCharAt(plainContent.length() - 1).append("\n");
                    htmlContent.append("</tr>\n");
                }
                plainContent.deleteCharAt(plainContent.length() - 1);
                htmlContent.append("</table>\n</body>\n</html>");
                Transferable transferable = new BasicTransferable(plainContent.toString(), htmlContent.toString());
                try {
                    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferable, null);
                } catch (IllegalStateException e) {
                    View.getSingleton().showWarningDialog(ScanProgressDialog.this, Constant.messages.getString("ascan.progress.copyclipboard.error"));
                    log.warn("Failed to copy the contents to clipboard:", e);
                }
            }
        });
    }
    return copyToClipboardButton;
}
Also used : BasicTransferable(org.jdesktop.swingx.plaf.basic.core.BasicTransferable) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) Transferable(java.awt.datatransfer.Transferable) BasicTransferable(org.jdesktop.swingx.plaf.basic.core.BasicTransferable) TableModel(javax.swing.table.TableModel)

Example 9 with Transferable

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

the class ImageSelection method checkImage.

void checkImage(DropTargetDropEvent dtde) {
    final Transferable t = dtde.getTransferable();
    if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
        dtde.acceptDrop(DnDConstants.ACTION_COPY);
        Image im;
        try {
            im = (Image) t.getTransferData(DataFlavor.imageFlavor);
            System.err.println("getTransferData was successful");
        } catch (Exception e) {
            System.err.println("Can't getTransferData: " + e);
            dtde.dropComplete(false);
            notifyTransferSuccess(false);
            return;
        }
        if (im == null) {
            System.err.println("getTransferData returned null");
            dtde.dropComplete(false);
            notifyTransferSuccess(false);
        } else if (areImagesIdentical(image, im)) {
            dtde.dropComplete(true);
            notifyTransferSuccess(true);
        } else {
            System.err.println("transferred image is different from initial image");
            dtde.dropComplete(false);
            notifyTransferSuccess(false);
        }
    } else {
        System.err.println("imageFlavor is not supported by Transferable");
        dtde.rejectDrop();
        notifyTransferSuccess(false);
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) BufferedImage(java.awt.image.BufferedImage) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 10 with Transferable

use of java.awt.datatransfer.Transferable in project zaproxy by zaproxy.

the class ExtensionStdMenus method getClipboardContents.

private String getClipboardContents() {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable contents = clipboard.getContents(null);
    if (contents != null && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        try {
            return (String) contents.getTransferData(DataFlavor.stringFlavor);
        } catch (UnsupportedFlavorException | IOException e) {
            log.error("Unable to get data from clipboard");
        }
    }
    return "";
}
Also used : Transferable(java.awt.datatransfer.Transferable) Clipboard(java.awt.datatransfer.Clipboard) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Aggregations

Transferable (java.awt.datatransfer.Transferable)89 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)28 IOException (java.io.IOException)27 List (java.util.List)15 ArrayList (java.util.ArrayList)13 Clipboard (java.awt.datatransfer.Clipboard)11 DataFlavor (java.awt.datatransfer.DataFlavor)11 File (java.io.File)10 Point (java.awt.Point)8 StringSelection (java.awt.datatransfer.StringSelection)5 Project (com.intellij.openapi.project.Project)4 ClipboardOwner (java.awt.datatransfer.ClipboardOwner)4 TransferHandler (javax.swing.TransferHandler)4 Editor (com.intellij.openapi.editor.Editor)3 ActionEvent (java.awt.event.ActionEvent)3 JComponent (javax.swing.JComponent)3 TreePath (javax.swing.tree.TreePath)3 DataContext (com.intellij.openapi.actionSystem.DataContext)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2