Example usage for java.awt.datatransfer Transferable isDataFlavorSupported

List of usage examples for java.awt.datatransfer Transferable isDataFlavorSupported

Introduction

In this page you can find the example usage for java.awt.datatransfer Transferable isDataFlavorSupported.

Prototype

public boolean isDataFlavorSupported(DataFlavor flavor);

Source Link

Document

Returns whether or not the specified data flavor is supported for this object.

Usage

From source file:de.mycrobase.jcloudapp.Main.java

@SuppressWarnings("unchecked")
public boolean doUploadClipboard() {
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable t = cb.getContents(null);

    if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        try {//from   ww  w.j ava2  s .  c  om
            List<File> data = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
            uploadFilesFromClipboard(data);
        } catch (UnsupportedFlavorException ex) {
        } catch (IOException ex) {
        }
        return true;
    } else if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
        try {
            Image data = (Image) t.getTransferData(DataFlavor.imageFlavor);
            BufferedImage bi = (BufferedImage) data;
            uploadImageFromClipboard(bi);
        } catch (UnsupportedFlavorException ex) {
        } catch (IOException ex) {
        }
        return true;
    } else if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        try {
            String data = (String) t.getTransferData(DataFlavor.stringFlavor);
            if (isValidURL(data)) {
                createBookmarkFromClipboard(data);
            } else {
                uploadStringFromClipboard(data);
            }
        } catch (UnsupportedFlavorException ex) {
        } catch (IOException ex) {
        }
        return true;
    }
    return false;
}

From source file:HostPanel.java

/**
 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
 *///from  ww w .jav  a  2s  .  c  o m
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

    dropTargetScroll = new javax.swing.JScrollPane();
    dropTargetArea = new javax.swing.JTextArea();
    dropTargetTitle = new javax.swing.JLabel();
    globalProgress = new javax.swing.JProgressBar();
    currentProgress = new javax.swing.JProgressBar();
    transferButton = new javax.swing.JButton();
    dropTargetValue = new javax.swing.JLabel();
    dropTargetClear = new javax.swing.JLabel();
    statusDisplay1 = new javax.swing.JLabel();
    statusDisplay2 = new javax.swing.JLabel();

    setFocusable(false);

    dropTargetScroll.setFocusable(false);

    dropTargetArea.setDropTarget(new DropTarget() {
        @Override
        public synchronized void drop(DropTargetDropEvent evt) {
            if (transfer == null || transfer.isDone()) {
                evt.acceptDrop(DnDConstants.ACTION_LINK);
                Transferable transferable = evt.getTransferable();

                try {
                    if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                        ProcessDropTarget pdt = new ProcessDropTarget(
                                (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor));
                        pdt.execute();
                    }
                } catch (UnsupportedFlavorException | IOException ex) {
                    Logger.getLogger(HostPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    });
    dropTargetArea.setEditable(false);
    dropTargetArea.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background"));
    dropTargetArea.setFocusable(false);
    dropTargetScroll.setViewportView(dropTargetArea);

    dropTargetTitle.setText("Drag files to host onto box below");
    dropTargetTitle.setFocusable(false);

    globalProgress.setFocusable(false);
    globalProgress.setRequestFocusEnabled(false);
    globalProgress.setStringPainted(true);

    currentProgress.setFocusable(false);
    currentProgress.setRequestFocusEnabled(false);
    currentProgress.setStringPainted(true);

    transferButton.setText("Host");
    transferButton.setActionCommand("host");
    transferButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            transferButtonActionPerformed(evt);
        }
    });

    dropTargetValue.setText("Files to Share: 0");
    dropTargetValue.setFocusable(false);

    dropTargetClear.setText("Clear");
    dropTargetClear.setFocusable(false);
    dropTargetClear.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseReleased(java.awt.event.MouseEvent evt) {
            dropTargetClearMouseReleased(evt);
        }
    });

    statusDisplay1.setText("");

    statusDisplay2.setText("");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup().addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dropTargetScroll)
                            .addGroup(layout.createSequentialGroup().addGroup(
                                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(globalProgress, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                    165, Short.MAX_VALUE)
                                            .addComponent(currentProgress, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGap(18, 18, 18)
                                    .addGroup(layout
                                            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(statusDisplay1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                    359, Short.MAX_VALUE)
                                            .addComponent(statusDisplay2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGap(18, 18, 18)
                                    .addComponent(transferButton, javax.swing.GroupLayout.PREFERRED_SIZE, 120,
                                            javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup().addComponent(dropTargetTitle)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                            javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(dropTargetValue, javax.swing.GroupLayout.PREFERRED_SIZE, 136,
                                            javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18).addComponent(dropTargetClear)))
                    .addContainerGap()));
    layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
            javax.swing.GroupLayout.Alignment.TRAILING,
            layout.createSequentialGroup().addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(dropTargetTitle).addComponent(dropTargetValue)
                            .addComponent(dropTargetClear))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(dropTargetScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup().addGroup(layout
                                    .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(currentProgress, javax.swing.GroupLayout.PREFERRED_SIZE,
                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                            javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(statusDisplay1))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout
                                            .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(globalProgress,
                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(statusDisplay2)))
                            .addComponent(transferButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap()));
}

From source file:com.jk.framework.util.FakeRunnable.java

/**
 * Get the String residing on the clipboard.
 *
 * @return any text found on the Clipboard; if none found, return an empty
 *         String.//w  w  w  .j ava2 s . co m
 */
public static String pasteFromClipBoard() {
    String result = "";
    final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    // odd: the Object param of getContents is not currently used
    final Transferable contents = clipboard.getContents(null);
    final boolean hasTransferableText = contents != null
            && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
    if (hasTransferableText) {
        try {
            result = (String) contents.getTransferData(DataFlavor.stringFlavor);
        } catch (final UnsupportedFlavorException ex) {
            // highly unlikely since we are using a standard DataFlavor
            ex.printStackTrace();
        } catch (final IOException ex) {
            ex.printStackTrace();
        }
    }
    return result;
}

From source file:com.mirth.connect.client.ui.EditMessageDialog.java

public void dragEnter(DropTargetDragEvent dtde) {
    try {// w w  w  .j a  v  a  2 s  . c  o  m
        Transferable tr = dtde.getTransferable();
        if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {

            dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);

            List<File> fileList = (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor);
            Iterator<File> iterator = fileList.iterator();
            while (iterator.hasNext()) {
                iterator.next();
            }
        } else {
            dtde.rejectDrag();
        }
    } catch (Exception e) {
        dtde.rejectDrag();
    }
}

From source file:com.mirth.connect.client.ui.EditMessageDialog.java

public void drop(DropTargetDropEvent dtde) {
    try {/*www  .j  a  va  2s . com*/
        Transferable tr = dtde.getTransferable();
        if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {

            dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

            List<File> fileList = (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor);
            Iterator<File> iterator = fileList.iterator();
            while (iterator.hasNext()) {
                File file = iterator.next();
                messageContent.setText(
                        messageContent.getText() + FileUtils.readFileToString(file, UIConstants.CHARSET));
            }
        }
    } catch (Exception e) {
        dtde.rejectDrop();
    }
}

From source file:com.kstenschke.copypastestack.ToolWindow.java

/**
 * Refresh listed items from distinct merged sum of:
 * 0. option to preview the current clipboard contents
 * 1. previous clipboard items still in prefs
 * 2. current clipboard items//  w  w  w  .ja va2s . c  o m
 */
private void refreshClipboardList() {
    Transferable[] copiedItems = CopyPasteManager.getInstance().getAllContents();
    int amountItems = UtilsClipboard.getAmountStringItemsInTransferables(copiedItems);
    String[] itemsUnique = null;
    boolean hasClipboardContent = UtilsClipboard.hasContent();

    String[] copyItemsList = new String[amountItems];
    if (amountItems > 0 || hasClipboardContent) {
        // Add copied string items, historic and current
        int index = 0;
        for (Transferable currentItem : copiedItems) {
            if (currentItem.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                try {
                    String itemStr = currentItem.getTransferData(DataFlavor.stringFlavor).toString();
                    if (!itemStr.trim().isEmpty()) {
                        copyItemsList[index] = itemStr;
                        index++;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        // Tidy items: distinct items, none empty
        String[] copyItemsPref = Preferences.getItems();
        Object[] allItems = (copyItemsPref.length > 0) ? ArrayUtils.addAll(copyItemsList, copyItemsPref)
                : copyItemsList;
        itemsUnique = UtilsArray.tidy(allItems);
        if (itemsUnique.length > 0) {
            this.setClipboardListData(itemsUnique, false);
            this.sortClipboardListByTags(this.form.checkboxKeepSorted.isSelected());

            Preferences.saveCopyItems(itemsUnique);
        }
    }

    initStatusLabel(itemsUnique == null ? 0 : itemsUnique.length);
}

From source file:com.mirth.connect.client.ui.components.MirthTree.java

public void dragEnter(DropTargetDragEvent dtde) {
    try {/*from w ww . ja va 2  s .  c o  m*/
        Transferable tr = dtde.getTransferable();

        if (parent.currentContentPage == parent.channelEditPanel.transformerPane
                && tr.isDataFlavorSupported(supportedDropFlavor)) {
            dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
        } else {
            dtde.rejectDrag();
        }
    } catch (Exception e) {
        dtde.rejectDrag();
    }
}

From source file:net.sf.jabref.gui.groups.EntryTableTransferHandler.java

/**
 * 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 database.
 *
 *///from w ww .j  av a2  s .c  o m
@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");
            List<File> l = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
            return handleDraggedFiles(l, 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;
}

From source file:net.sf.jabref.groups.EntryTableTransferHandler.java

/**
 * 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 database.
 *
 *//*from  ww w .  j a  v  a2  s  .c om*/
@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");
            List<File> l = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
            return handleDraggedFiles(l, 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 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;
}

From source file:org.freeplane.features.clipboard.mindmapmode.MClipboardController.java

private IDataFlavorHandler getFlavorHandler(final Transferable t) {
    if (t.isDataFlavorSupported(MindMapNodesSelection.mindMapNodesFlavor)) {
        try {//from   w  ww  .j  av  a2 s. co m
            final String textFromClipboard = t.getTransferData(MindMapNodesSelection.mindMapNodesFlavor)
                    .toString();
            return new MindMapNodesFlavorHandler(textFromClipboard);
        } catch (final UnsupportedFlavorException e) {
        } catch (final IOException e) {
        }
    }
    if (t.isDataFlavorSupported(MindMapNodesSelection.fileListFlavor)) {
        try {
            final List<File> fileList = castToFileList(t.getTransferData(MindMapNodesSelection.fileListFlavor));
            if (!shouldIgnoreFileListFlavor(fileList))
                return new FileListFlavorHandler(fileList);
        } catch (final UnsupportedFlavorException e) {
        } catch (final IOException e) {
        }
    }
    final ResourceController resourceController = ResourceController.getResourceController();
    if (t.isDataFlavorSupported(MindMapNodesSelection.htmlFlavor)) {
        try {
            final String textFromClipboard = t.getTransferData(MindMapNodesSelection.htmlFlavor).toString();
            if (textFromClipboard.charAt(0) != 65533) {
                if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    final MTextController textController = (MTextController) TextController.getController();
                    final boolean richText = textController
                            .useRichTextInEditor(RESOURCES_REMIND_USE_RICH_TEXT_IN_NEW_NODES);
                    if (richText) {
                        final boolean structuredHtmlImport = resourceController
                                .getBooleanProperty("structured_html_import");
                        final IDataFlavorHandler htmlFlavorHandler;
                        if (structuredHtmlImport) {
                            htmlFlavorHandler = new StructuredHtmlFlavorHandler(textFromClipboard);
                        } else {
                            htmlFlavorHandler = new DirectHtmlFlavorHandler(textFromClipboard);
                        }
                        return htmlFlavorHandler;
                    }
                }
            }
        } catch (final UnsupportedFlavorException e) {
        } catch (final IOException e) {
        }
    }
    if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        try {
            final String plainTextFromClipboard = t.getTransferData(DataFlavor.stringFlavor).toString();
            return new StringFlavorHandler(plainTextFromClipboard);
        } catch (final UnsupportedFlavorException e) {
        } catch (final IOException e) {
        }
    }
    if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
        try {
            BufferedImage image = (BufferedImage) t.getTransferData(DataFlavor.imageFlavor);
            return new ImageFlavorHandler(image);
        } catch (final UnsupportedFlavorException e) {
        } catch (final IOException e) {
        }
    }
    return null;
}