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:net.sf.jabref.gui.ClipBoardManager.java

/**
 * Get the String residing on the clipboard.
 *
 * @return any text found on the Clipboard; if none found, return an
 * empty String./*from  w  ww  .j  a v a 2 s  . c  o m*/
 */
public String getClipboardContents() {
    String result = "";
    //odd: the Object param of getContents is not currently used
    Transferable contents = CLIPBOARD.getContents(null);
    if (contents != null && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        try {
            result = (String) contents.getTransferData(DataFlavor.stringFlavor);
        } catch (UnsupportedFlavorException | IOException e) {
            //highly unlikely since we are using a standard DataFlavor
            LOGGER.info("problem with getting clipboard contents", e);
        }
    }
    return result;
}

From source file:DragImage.java

public boolean importData(JComponent comp, Transferable t) {
    if (comp instanceof JLabel) {
        JLabel label = (JLabel) comp;
        if (t.isDataFlavorSupported(flavors[0])) {
            try {
                image = (Image) t.getTransferData(flavors[0]);
                ImageIcon icon = new ImageIcon(image);
                label.setIcon(icon);/*from w ww .  j  ava  2 s  .com*/
                return true;
            } catch (UnsupportedFlavorException ignored) {
            } catch (IOException ignored) {
            }
        }
    }
    return false;
}

From source file:net.sf.jabref.gui.ClipBoardManager.java

public List<BibEntry> extractBibEntriesFromClipboard() {
    // Get clipboard contents, and see if TransferableBibtexEntry is among the content flavors offered
    Transferable content = CLIPBOARD.getContents(null);

    List<BibEntry> result = new ArrayList<>();
    if (content.isDataFlavorSupported(TransferableBibtexEntry.entryFlavor)) {
        // We have determined that the clipboard data is a set of entries.
        try {/*from  w w w  . java2  s.  c  o m*/
            result = (List<BibEntry>) content.getTransferData(TransferableBibtexEntry.entryFlavor);
        } catch (UnsupportedFlavorException | ClassCastException ex) {
            LOGGER.warn("Could not paste this type", ex);
        } catch (IOException ex) {
            LOGGER.warn("Could not paste", ex);
        }
    } else if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        try {
            String data = (String) content.getTransferData(DataFlavor.stringFlavor);
            // fetch from doi
            if (DOI.build(data).isPresent()) {
                LOGGER.info("Found DOI in clipboard");
                Optional<BibEntry> entry = new DOItoBibTeXFetcher().getEntryFromDOI(new DOI(data).getDOI());
                entry.ifPresent(result::add);
            } else {
                // parse bibtex string
                BibtexParser bp = new BibtexParser(new StringReader(data));
                BibDatabase db = bp.parse().getDatabase();
                LOGGER.info("Parsed " + db.getEntryCount() + " entries from clipboard text");
                if (db.hasEntries()) {
                    result = db.getEntries();
                }
            }
        } catch (UnsupportedFlavorException ex) {
            LOGGER.warn("Could not parse this type", ex);
        } catch (IOException ex) {
            LOGGER.warn("Data is no longer available in the requested flavor", ex);
        }

    }
    return result;
}

From source file:org.sikuli.script.App.java

/**
 * evaluates the current textual content of the system clipboard
 *
 * @return the textual content or empty string if not possible
 *///from   ww w. ja va2 s . com
public static String getClipboard() {
    Transferable content = null;
    try {
        content = Clipboard.getSystemClipboard().getContents(null);
    } catch (Exception ex) {
        Debug.error("Env.getClipboard: clipboard not available:\n%s", ex.getMessage());
    }
    if (content != null) {
        try {
            if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return (String) content.getTransferData(DataFlavor.stringFlavor);
            }
        } catch (UnsupportedFlavorException ex) {
            Debug.error("Env.getClipboard: UnsupportedFlavorException: " + content);
        } catch (IOException ex) {
            Debug.error("Env.getClipboard: IOException:\n%s", ex.getMessage());
        }
    }
    return "";
}

From source file:ColorSink.java

/** This method is invoked when the user drops something on us */
public void drop(DropTargetDropEvent e) {
    this.setBorder(null); // Restore the default border
    Transferable t = e.getTransferable(); // Get the data that was dropped

    // Check for types of data that we support
    if (t.isDataFlavorSupported(TransferableColor.colorFlavor)) {
        // If it was a color, accept it, and use it as the background color
        e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
        try {/*from w w  w.  j  a v a 2s.  com*/
            Color c = (Color) t.getTransferData(TransferableColor.colorFlavor);
            this.setBackground(c);
            e.dropComplete(true);
        } catch (Exception ex) {
            e.dropComplete(false);
        }
    } else if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        // If it was a file list, accept it, read the first file in the list
        // and display the file contents
        e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
        try {
            List files = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
            File f = (File) files.get(0);
            BufferedReader in = new BufferedReader(new FileReader(f));
            String s;
            this.setText("");
            while ((s = in.readLine()) != null)
                this.append(s);
            e.dropComplete(true);
        } catch (Exception ex) {
            e.dropComplete(false);
        }
    } else { // If it wasn't a color or a file list, reject it.
        e.rejectDrop();
        return;
    }
}

From source file:DNDList.java

/**
 * a drop has occurred//  w  w  w .j a  v a 2 s .co  m
 * 
 */

public void drop(DropTargetDropEvent event) {

    try {
        Transferable transferable = event.getTransferable();

        // we accept only Strings
        if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {

            event.acceptDrop(DnDConstants.ACTION_MOVE);
            String s = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            addElement(s);
            event.getDropTargetContext().dropComplete(true);
        } else {
            event.rejectDrop();
        }
    } catch (Exception exception) {
        System.err.println("Exception" + exception.getMessage());
        event.rejectDrop();
    }
}

From source file:com.mirth.connect.client.ui.editors.JavaScriptEditorDialog.java

public void dragEnter(DropTargetDragEvent dtde) {
    try {/*from ww w .ja va2  s. co  m*/
        Transferable tr = dtde.getTransferable();
        if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {

            dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);

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

From source file:com.mirth.connect.client.ui.editors.JavaScriptEditorDialog.java

public void drop(DropTargetDropEvent dtde) {
    try {/*from  ww  w .  ja  va2s .  co  m*/
        Transferable tr = dtde.getTransferable();
        if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {

            dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

            java.util.List fileList = (java.util.List) tr.getTransferData(DataFlavor.javaFileListFlavor);
            Iterator iterator = fileList.iterator();
            while (iterator.hasNext()) {
                File file = (File) iterator.next();

                scriptContent.setText(
                        scriptContent.getText() + FileUtils.readFileToString(file, UIConstants.CHARSET));
            }
        }
    } catch (Exception e) {
        dtde.rejectDrop();
    }
}

From source file:net.pandoragames.far.ui.swing.component.UndoHistoryPopupMenu.java

private void init(SwingConfig config, ComponentRepository componentRepository) {
    // COPY/*from ww w . j  ava 2 s .  c  om*/
    JMenuItem copy = new JMenuItem(config.getLocalizer().localize("label.copy"));
    copy.setAccelerator(KeyStroke.getKeyStroke("control C"));
    copy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String selection = textComponent.getSelectedText();
            if (selection != null) {
                StringSelection textTransfer = new StringSelection(selection);
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(textTransfer, null);
            }
        }
    });
    this.add(copy);
    // PASTE
    JMenuItem paste = new JMenuItem(config.getLocalizer().localize("label.paste"));
    paste.setAccelerator(KeyStroke.getKeyStroke("control V"));
    paste.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            Transferable transfer = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);

            try {
                if (transfer != null && transfer.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    String text = (String) transfer.getTransferData(DataFlavor.stringFlavor);
                    String selected = textComponent.getSelectedText();
                    if (selected == null) {
                        int insertAt = textComponent.getCaretPosition();
                        textComponent.getDocument().insertString(insertAt, text, null);
                    } else {
                        int start = textComponent.getSelectionStart();
                        int end = textComponent.getSelectionEnd();
                        textComponent.getDocument().remove(start, end - start);
                        textComponent.getDocument().insertString(start, text, null);
                    }
                }
            } catch (UnsupportedFlavorException e) {
                LogFactory.getLog(this.getClass()).error("UnsupportedFlavorException reading from clipboard",
                        e);
            } catch (IOException iox) {
                LogFactory.getLog(this.getClass()).error("IOException reading from clipboard", iox);
            } catch (BadLocationException blx) {
                LogFactory.getLog(this.getClass()).error("BadLocationException reading from clipboard", blx);
            }
        }
    });
    this.add(paste);
    // UNDO
    Action undoAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_UNDO);
    if (undoAction != null) {
        undoAction.putValue(Action.NAME, config.getLocalizer().localize("label.undo"));
        this.add(undoAction);
    }
    // REDO
    Action redoAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_REDO);
    if (redoAction != null) {
        redoAction.putValue(Action.NAME, config.getLocalizer().localize("label.redo"));
        this.add(redoAction);
    }
    // PREVIOUS
    Action prevAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_PREVIOUS);
    if (prevAction != null) {
        prevAction.putValue(Action.NAME, config.getLocalizer().localize("label.previous"));
        this.add(prevAction);
    }
    // NEXT
    Action nextAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_NEXT);
    if (nextAction != null) {
        nextAction.putValue(Action.NAME, config.getLocalizer().localize("label.next"));
        this.add(nextAction);
    }
}

From source file:org.pentaho.reporting.designer.core.util.dnd.GenericDNDHandler.java

/**
 * Called when a drag operation is ongoing, while the mouse pointer is still over the operable part of the drop site
 * for the <code>DropTarget</code> registered with this listener.
 *
 * @param dtde the <code>DropTargetDragEvent</code>
 *//*www . j a v a  2s.  co m*/

public void dragOver(final DropTargetDragEvent dtde) {
    final Transferable transferable = dtde.getTransferable();

    for (int i = 0; i < acceptedFlavors.length; i++) {
        final DataFlavor acceptedFlavor = acceptedFlavors[i];
        if (transferable.isDataFlavorSupported(acceptedFlavor)) {
            // a transfer from the palette.
            try {
                transferData = transferable.getTransferData(acceptedFlavor);
                position = dtde.getLocation();
                flavor = acceptedFlavor;
                final int result = updateDragOver(dtde);
                if (result > 0) {
                    dtde.acceptDrag(DnDConstants.ACTION_COPY);
                } else {
                    transferData = null;
                    position = null;
                    flavor = null;
                    dtde.rejectDrag();
                }
                break;
            } catch (Exception e) {
                if (logger.isDebugEnabled()) {
                    logger.debug("ReportPanel.dragOver ", e); // NON-NLS
                }
                transferData = null;
                position = null;
                flavor = null;
                dtde.rejectDrag();
            }
        }
    }
}