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:de.tor.tribes.ui.windows.AbstractDSWorkbenchFrame.java

@Override
public void drop(DropTargetDropEvent dtde) {
    if (dtde.isDataFlavorSupported(VillageTransferable.villageDataFlavor)
            || dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
    } else {/* www  .j  a  va 2s. co  m*/
        dtde.rejectDrop();
        return;
    }

    Transferable t = dtde.getTransferable();
    List<Village> v;
    MapPanel.getSingleton().setCurrentCursor(MapPanel.getSingleton().getCurrentCursor());
    try {
        v = (List<Village>) t.getTransferData(VillageTransferable.villageDataFlavor);
        fireVillagesDraggedEvent(v, dtde.getLocation());
    } catch (Exception ignored) {
    }
}

From source file:EditorDropTarget4.java

protected void checkTransferType(DropTargetDragEvent dtde) {
    // Accept a list of files, or data content that
    // amounts to plain text or a Unicode text string
    acceptableType = false;/*  w  w w .j av  a  2s .c  o  m*/
    draggingFile = false;
    if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        acceptableType = true;
        draggingFile = true;
    } else if (dtde.isDataFlavorSupported(DataFlavor.plainTextFlavor)
            || dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        acceptableType = true;
    }
    DnDUtils.debugPrintln("File type acceptable - " + acceptableType);
    DnDUtils.debugPrintln("Dragging a file - " + draggingFile);
}

From source file:com.igormaznitsa.ideamindmap.swing.PlainTextEditor.java

public PlainTextEditor(final Project project, final String text) {
    super(new BorderLayout());
    this.editor = new EmptyTextEditor(project);

    final JToolBar menu = new JToolBar();

    final JButton buttonImport = new JButton("Import", AllIcons.Buttons.IMPORT);
    final PlainTextEditor theInstance = this;

    buttonImport.addActionListener(new ActionListener() {
        @Override// w  ww. j  a  v a  2 s. c  o m
        public void actionPerformed(ActionEvent e) {
            final File home = new File(System.getProperty("user.home")); //NOI18N

            final File toOpen = IdeaUtils.chooseFile(theInstance, true,
                    BUNDLE.getString("PlainTextEditor.buttonLoadActionPerformed.title"), home,
                    TEXT_FILE_FILTER);

            if (toOpen != null) {
                try {
                    final String text = FileUtils.readFileToString(toOpen, "UTF-8"); //NOI18N
                    editor.setText(text);
                } catch (Exception ex) {
                    LOGGER.error("Error during text file loading", ex); //NOI18N
                    Messages.showErrorDialog(
                            BUNDLE.getString("PlainTextEditor.buttonLoadActionPerformed.msgError"), "Error");
                }
            }

        }
    });

    final JButton buttonExport = new JButton("Export", AllIcons.Buttons.EXPORT);
    buttonExport.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            final File home = new File(System.getProperty("user.home")); //NOI18N
            final File toSave = IdeaUtils.chooseFile(theInstance, true,
                    BUNDLE.getString("PlainTextEditor.buttonSaveActionPerformed.saveTitle"), home,
                    TEXT_FILE_FILTER);
            if (toSave != null) {
                try {
                    final String text = getText();
                    FileUtils.writeStringToFile(toSave, text, "UTF-8"); //NOI18N
                } catch (Exception ex) {
                    LOGGER.error("Error during text file saving", ex); //NOI18N
                    Messages.showErrorDialog(
                            BUNDLE.getString("PlainTextEditor.buttonSaveActionPerformed.msgError"), "Error");
                }
            }
        }
    });

    final JButton buttonCopy = new JButton("Copy", AllIcons.Buttons.COPY);
    buttonCopy.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            final StringSelection stringSelection = new StringSelection(editor.getSelectedText());
            final Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
            clpbrd.setContents(stringSelection, null);
        }
    });

    final JButton buttonPaste = new JButton("Paste", AllIcons.Buttons.PASTE);
    buttonPaste.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                final String clipboardText = (String) Toolkit.getDefaultToolkit().getSystemClipboard()
                        .getData(DataFlavor.stringFlavor);
                editor.replaceSelection(clipboardText);
            } catch (UnsupportedFlavorException ex) {
                // no text data in clipboard
            } catch (IOException ex) {
                LOGGER.error("Error during paste from clipboard", ex); //NOI18N
            }
        }
    });

    final JButton buttonClearAll = new JButton("Clear All", AllIcons.Buttons.CLEARALL);
    buttonClearAll.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            editor.clear();
        }
    });

    menu.add(buttonImport);
    menu.add(buttonExport);
    menu.add(buttonCopy);
    menu.add(buttonPaste);
    menu.add(buttonClearAll);

    this.add(menu, BorderLayout.NORTH);
    this.add(editor, BorderLayout.CENTER);

    // I made so strange trick to move the caret into the start of document, all other ways didn't work :(
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            editor.replaceSelection(text);
        }
    });
}

From source file:TextCutPaste.java

/**
 * Perform the actual import. This method supports both drag and drop and
 * cut/copy/paste.//from  w w w.  j a va2  s . co m
 */
public boolean importData(TransferHandler.TransferSupport support) {
    // If we can't handle the import, bail now.
    if (!canImport(support)) {
        return false;
    }

    // Fetch the data -- bail if this fails
    String data;
    try {
        data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
    } catch (UnsupportedFlavorException e) {
        return false;
    } catch (java.io.IOException e) {
        return false;
    }

    JTextField tc = (JTextField) support.getComponent();
    tc.replaceSelection(data);
    return true;
}

From source file:DropDemo.java

public boolean importData(TransferHandler.TransferSupport info) {
    if (!info.isDrop()) {
        return false;
    }//w w w .j a v a2  s  . co m

    JList list = (JList) info.getComponent();
    DefaultListModel listModel = (DefaultListModel) list.getModel();
    JList.DropLocation dl = (JList.DropLocation) info.getDropLocation();
    int index = dl.getIndex();
    boolean insert = dl.isInsert();

    // Get the string that is being dropped.
    Transferable t = info.getTransferable();
    String data;
    try {
        data = (String) t.getTransferData(DataFlavor.stringFlavor);
    } catch (Exception e) {
        return false;
    }

    // Perform the actual import.
    if (insert) {
        listModel.add(index, data);
    } else {
        listModel.set(index, data);
    }
    return true;
}

From source file:com.netease.dagger.BrowserEmulator.java

/**
 * Paste text from clipboard content/*  ww  w.j  a v  a 2s . co m*/
 */
public String getClipboardContent() {
    pause(stepInterval);
    try {
        String text = "";
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable data = clip.getContents(null);
        if (data != null) {
            if (data.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                text = (String) data.getTransferData(DataFlavor.stringFlavor);
            }
        }
        return text;
    } catch (Exception e) {
        e.printStackTrace();
        handleFailure("Failed to get clipboard content");
    }
    logger.info("Success to get clipboard content");
    return null;
}

From source file:cn.lambdalib.cgui.gui.component.TextBox.java

private String getClipboardContent() {
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    if (cb.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
        try {//  www .  j a  va2  s .c o m
            return (String) cb.getData(DataFlavor.stringFlavor);
        } catch (UnsupportedFlavorException | IOException e) {
            e.printStackTrace();
        }
    }
    return "";
}

From source file:EditorDropTarget3.java

protected boolean dropContent(Transferable transferable, DropTargetDropEvent dtde) {
    if (!pane.isEditable()) {
        // Can't drop content on a read-only text control
        return false;
    }/*from   w w w.  j  av  a  2  s. c  o m*/

    try {
        // Check for a match with the current content type
        DataFlavor[] flavors = dtde.getCurrentDataFlavors();

        DataFlavor selectedFlavor = null;

        // Look for either plain text or a String.
        for (int i = 0; i < flavors.length; i++) {
            DataFlavor flavor = flavors[i];
            DnDUtils.debugPrintln("Drop MIME type " + flavor.getMimeType() + " is available");
            if (flavor.equals(DataFlavor.plainTextFlavor) || flavor.equals(DataFlavor.stringFlavor)) {
                selectedFlavor = flavor;
                break;
            }
        }

        if (selectedFlavor == null) {
            // No compatible flavor - should never happen
            return false;

        }

        DnDUtils.debugPrintln("Selected flavor is " + selectedFlavor.getHumanPresentableName());

        // Get the transferable and then obtain the data
        Object data = transferable.getTransferData(selectedFlavor);

        DnDUtils.debugPrintln("Transfer data type is " + data.getClass().getName());

        String insertData = null;
        if (data instanceof InputStream) {
            // Plain text flavor
            String charSet = selectedFlavor.getParameter("charset");
            InputStream is = (InputStream) data;
            byte[] bytes = new byte[is.available()];
            is.read(bytes);
            try {
                insertData = new String(bytes, charSet);
            } catch (UnsupportedEncodingException e) {
                // Use the platform default encoding
                insertData = new String(bytes);
            }
        } else if (data instanceof String) {
            // String flavor
            insertData = (String) data;
        }

        if (insertData != null) {
            int selectionStart = pane.getCaretPosition();
            pane.replaceSelection(insertData);
            pane.select(selectionStart, selectionStart + insertData.length());
            return true;
        }
        return false;
    } catch (Exception e) {
        return false;
    }
}

From source file:EditorDropTarget4.java

protected boolean dropContent(Transferable transferable, DropTargetDropEvent dtde) {
    if (!pane.isEditable()) {
        // Can't drop content on a read-only text control
        return false;
    }// ww w.  ja va 2s . co  m

    try {
        // Check for a match with the current content type
        DataFlavor[] flavors = dtde.getCurrentDataFlavors();

        DataFlavor selectedFlavor = null;

        // Look for either plain text or a String.
        for (int i = 0; i < flavors.length; i++) {
            DataFlavor flavor = flavors[i];
            DnDUtils.debugPrintln("Drop MIME type " + flavor.getMimeType() + " is available");
            if (flavor.equals(DataFlavor.plainTextFlavor) || flavor.equals(DataFlavor.stringFlavor)) {
                selectedFlavor = flavor;
                break;
            }
        }

        if (selectedFlavor == null) {
            // No compatible flavor - should never happen
            return false;
        }

        DnDUtils.debugPrintln("Selected flavor is " + selectedFlavor.getHumanPresentableName());

        // Get the transferable and then obtain the data
        Object data = transferable.getTransferData(selectedFlavor);

        DnDUtils.debugPrintln("Transfer data type is " + data.getClass().getName());

        String insertData = null;
        if (data instanceof InputStream) {
            // Plain text flavor
            String charSet = selectedFlavor.getParameter("charset");
            InputStream is = (InputStream) data;
            byte[] bytes = new byte[is.available()];
            is.read(bytes);
            try {
                insertData = new String(bytes, charSet);
            } catch (UnsupportedEncodingException e) {
                // Use the platform default encoding
                insertData = new String(bytes);
            }
        } else if (data instanceof String) {
            // String flavor
            insertData = (String) data;
        }

        if (insertData != null) {
            int selectionStart = pane.getCaretPosition();
            pane.replaceSelection(insertData);
            pane.select(selectionStart, selectionStart + insertData.length());
            return true;
        }
        return false;
    } catch (Exception e) {
        return false;
    }
}

From source file:TextCutPaste.java

/**
 * We only support importing strings./*from  w  ww.  j av  a  2  s .  c  o m*/
 */
public boolean canImport(TransferHandler.TransferSupport support) {
    // we only import Strings
    if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        return false;
    }
    return true;
}