Example usage for java.awt.datatransfer DataFlavor stringFlavor

List of usage examples for java.awt.datatransfer DataFlavor stringFlavor

Introduction

In this page you can find the example usage for java.awt.datatransfer DataFlavor stringFlavor.

Prototype

DataFlavor stringFlavor

To view the source code for java.awt.datatransfer DataFlavor stringFlavor.

Click Source Link

Document

The DataFlavor representing a Java Unicode String class, where:
 representationClass = java.lang.String mimeType            = "application/x-java-serialized-object" 

Usage

From source file:TextTransferTest.java

/**
 * Pastes the text from the system clipboard into the text area.
 *///from  ww  w .j  a v  a 2s . c om
private void paste() {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    DataFlavor flavor = DataFlavor.stringFlavor;
    if (clipboard.isDataFlavorAvailable(flavor)) {
        try {
            String text = (String) clipboard.getData(flavor);
            textArea.replaceSelection(text);
        } catch (UnsupportedFlavorException e) {
            JOptionPane.showMessageDialog(this, e);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, e);
        }
    }
}

From source file:pl.otros.logview.gui.Log4jPatternParserEditor.java

private void enableDragAndDrop() {
    logFileContent.setDragEnabled(true);
    final TransferHandler defaultTransferHandler = logFileContent.getTransferHandler();
    TransferHandler transferHandler = new TransferHandler() {

        @Override//from w ww.j  a va2 s .  c  o m
        public boolean importData(TransferSupport support) {
            if (isText(support)) {
                try {
                    String transferData = (String) support.getTransferable()
                            .getTransferData(DataFlavor.stringFlavor);
                    if (transferData.startsWith("file://")) {
                        String firstLine = transferData;
                        if (firstLine.indexOf('\n') > 0) {
                            firstLine = firstLine.substring(0, firstLine.indexOf('\n') - 1);
                        }
                        loadLogContent(VFS.getManager().resolveFile(firstLine));
                    } else {
                        defaultTransferHandler.importData(support);
                    }
                    return true;

                } catch (UnsupportedFlavorException e) {
                    LOGGER.warning("Can't import data, UnsupportedFlavorException: " + e.getMessage());
                } catch (IOException e) {
                    LOGGER.warning("Can't import data, IOException: " + e.getMessage());
                }
            }

            if (isListOfFiles(support)) {
                try {
                    List data = (List) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
                    if (data.size() > 0) {
                        File file = (File) data.get(0);
                        loadLogContent(VFS.getManager().resolveFile(file.getAbsolutePath()));
                        return true;
                    }
                } catch (UnsupportedFlavorException e) {
                    LOGGER.warning("Can't import data, UnsupportedFlavorException: " + e.getMessage());
                } catch (IOException e) {
                    LOGGER.warning("Can't import data, IOException: " + e.getMessage());
                }
            }

            return defaultTransferHandler.importData(support);
        }

        @Override
        public boolean canImport(TransferSupport support) {
            if (isText(support) || isListOfFiles(support)) {
                return true;
            }
            return defaultTransferHandler.canImport(support);
        }

        private boolean isListOfFiles(TransferSupport support) {
            return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
        }

        private boolean isText(TransferSupport support) {
            return DataFlavor.selectBestTextFlavor(support.getDataFlavors()) != null;
        }

    };
    logFileContent.setTransferHandler(transferHandler);
    loadLog.setTransferHandler(transferHandler);
    logFileContentLabel.setTransferHandler(transferHandler);
}

From source file:ComplexExample.java

public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
    if (flavor.equals(colorFlavor))
        return color;
    else if (flavor.equals(DataFlavor.stringFlavor))
        return color.toString();
    else/*w w  w.j  a  v  a 2  s. c om*/
        throw new UnsupportedFlavorException(flavor);
}

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

/**
 * Construct the transfer handler./*  ww w .j av  a  2 s  . c o m*/
 *
 * @param entryTable The table this transfer handler should operate on. This argument is allowed to equal null, in
 *            which case the transfer handler can assume that it works for a JabRef instance with no databases open,
 *            attached to the empty tabbed pane.
 * @param frame The JabRefFrame instance.
 * @param panel The BasePanel this transferhandler works for.
 */
public EntryTableTransferHandler(MainTable entryTable, JabRefFrame frame, BasePanel panel) {
    this.entryTable = entryTable;
    this.frame = frame;
    this.panel = panel;
    stringFlavor = DataFlavor.stringFlavor;
    try {
        urlFlavor = new DataFlavor("application/x-java-url; class=java.net.URL");
    } catch (ClassNotFoundException e) {
        LOGGER.info("Unable to configure drag and drop for main table", e);
    }
}

From source file:Main.java

@Override
public boolean canImport(TransferHandler.TransferSupport support) {
    if (!support.isDrop()) {
        return false;
    }//  www.j  a v  a 2  s  .co  m
    return support.isDataFlavorSupported(DataFlavor.stringFlavor);
}

From source file:Dropper.java

/**
 * @see javax.swing.TransferHandler#canImport(javax.swing.JComponent,
 *      java.awt.datatransfer.DataFlavor[])
 *//*  w  w  w.j a  v a 2 s.  c om*/
public boolean canImport(JComponent arg0, DataFlavor[] arg1) {
    for (int i = 0; i < arg1.length; i++) {
        DataFlavor flavor = arg1[i];
        if (flavor.equals(DataFlavor.javaFileListFlavor)) {
            System.out.println("canImport: JavaFileList FLAVOR: " + flavor);
            return true;
        }
        if (flavor.equals(DataFlavor.stringFlavor)) {
            System.out.println("canImport: String FLAVOR: " + flavor);
            return true;
        }
        System.err.println("canImport: Rejected Flavor: " + flavor);
    }
    // Didn't find any that match, so:
    return false;
}

From source file:de.tor.tribes.util.ClipboardWatch.java

@Override
public void run() {
    logger.info("Starting ClipboardMonitor");
    String lastHash = null;//from w  w w  . j a  va 2  s .  c o m
    while (true) {
        if (DSWorkbenchMainFrame.getSingleton().isWatchClipboard()) {
            try {
                Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
                String data = (String) t.getTransferData(DataFlavor.stringFlavor);
                String currentHash = SecurityAdapter.hashStringMD5(data);
                boolean validData = false;
                if ((data.length() > 10) && (lastHash == null || !currentHash.equals(lastHash))) {
                    if (PluginManager.getSingleton().executeReportParser(data)) {
                        //report parsed, clean clipboard
                        logger.info("Report successfully parsed.");
                        playNotification();
                        validData = true;
                    } else if (PluginManager.getSingleton().executeTroopsParser(data)) {
                        logger.info("Troops successfully parsed.");
                        SystrayHelper.showInfoMessage("Truppen erfolgreich eingelesen");
                        playNotification();
                        //at least one village was found, so clean the clipboard
                        validData = true;
                    } else if (PluginManager.getSingleton().executeGroupParser(data)) {
                        logger.info("Groups successfully parsed.");
                        SystrayHelper.showInfoMessage("Gruppen erfolgreich eingelesen");
                        playNotification();
                        validData = true;
                    } else if (PluginManager.getSingleton().executeSupportParser(data)) {
                        logger.info("Support successfully parsed.");
                        SystrayHelper.showInfoMessage("Untersttzungen erfolgreich eingelesen");
                        playNotification();
                        validData = true;
                    } else if (PluginManager.getSingleton().executeNonPAPlaceParser(data)) {
                        logger.info("Place info successfully parsed.");
                        SystrayHelper.showInfoMessage("Truppen aus Versammlungsplatz erfolgreich eingelesen");
                        playNotification();
                        validData = true;
                    }
                }
                if (validData) {
                    lastHash = currentHash;
                }
            } catch (Exception e) {
                //no usable data
                //  e.printStackTrace();
            }
        } else {
            //clipboard watch is disabled, sleep 9 + 1 seconds
            try {
                Thread.sleep(9000);
            } catch (Exception e) {
            }
        }
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
        }
    }
}

From source file:org.kalypso.ogc.gml.om.table.command.PasteFromClipboardHandler.java

@Override
public Object execute(final ExecutionEvent event) {
    final Shell shell = HandlerUtil.getActiveShell(event);

    try {/*  w  w w . ja va  2  s  . c  o m*/
        trstring = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getContents(this)
                .getTransferData(DataFlavor.stringFlavor);
        // if clipboard content is not text or that content is empty, pop error message
        if (trstring == null || trstring.trim().length() == 0) {
            MessageDialog.openError(shell,
                    Messages.getString("org.kalypso.ogc.gml.om.table.command.PasteFromClipboardHandler.0"), //$NON-NLS-1$
                    Messages.getString("org.kalypso.ogc.gml.om.table.command.PasteFromClipboardHandler.1")); //$NON-NLS-1$
            return null;
        }
    } catch (final Exception e) {
        MessageDialog.openError(shell,
                Messages.getString("org.kalypso.ogc.gml.om.table.command.PasteFromClipboardHandler.0"), //$NON-NLS-1$
                Messages.getString("org.kalypso.ogc.gml.om.table.command.PasteFromClipboardHandler.1")); //$NON-NLS-1$
        return null;
    }

    m_tupleResult = ToolbarCommandUtils.findTupleResult(event);

    final TableViewer tupleResultViewer = ToolbarCommandUtils.findTableViewer(event);
    if (tupleResultViewer == null) {
        MessageDialog.openError(shell,
                Messages.getString("org.kalypso.ogc.gml.om.table.command.PasteFromClipboardHandler.0"), //$NON-NLS-1$
                Messages.getString("org.kalypso.ogc.gml.om.table.command.PasteFromClipboardHandler.5")); //$NON-NLS-1$
        return null;
    }

    final IContentProvider contentProvider = tupleResultViewer.getContentProvider();
    if (contentProvider instanceof TupleResultContentProvider)
        resultContentProvider = (TupleResultContentProvider) contentProvider;
    else if (contentProvider instanceof LastLineContentProvider) {
        final IStructuredContentProvider wrappedProvider = ((LastLineContentProvider) contentProvider)
                .getWrappedProvider();
        if (wrappedProvider instanceof TupleResultContentProvider)
            resultContentProvider = (TupleResultContentProvider) wrappedProvider;
    }

    if (resultContentProvider == null)
        return null;

    int firstIndex = m_tupleResult.size();
    final IStructuredSelection selection = (IStructuredSelection) tupleResultViewer.getSelection();
    if (!selection.isEmpty()) {
        final Object firstElement = selection.getFirstElement();
        firstIndex = m_tupleResult.indexOf(firstElement);
        if (selection.size() > 1) {
            m_tupleResult.removeAll(selection.toList());

            final int indexToSelect = Math.min(firstIndex, m_tupleResult.size() - 1);
            if (indexToSelect != -1)
                tupleResultViewer.setSelection(new StructuredSelection(m_tupleResult.get(indexToSelect)));
        }

    }
    parseRecords(shell, firstIndex);

    return null;
}

From source file:Main.java

@Override
public boolean importData(TransferHandler.TransferSupport support) {
    if (!this.canImport(support)) {
        return false;
    }//from  www .j a va 2  s. co  m
    Transferable t = support.getTransferable();
    String data = null;
    try {
        data = (String) t.getTransferData(DataFlavor.stringFlavor);
        if (data == null) {
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    JList.DropLocation dropLocation = (JList.DropLocation) support.getDropLocation();
    int dropIndex = dropLocation.getIndex();
    JList<String> targetList = (JList<String>) support.getComponent();
    DefaultListModel<String> listModel = (DefaultListModel<String>) targetList.getModel();
    if (dropLocation.isInsert()) {
        listModel.add(dropIndex, data);
    } else {
        listModel.set(dropIndex, data);
    }
    return true;
}

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   ww  w.j av a  2  s  . c om
            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;
}