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:Main.java

public void drop(DropTargetDropEvent e) {
    System.out.println("Dropping");

    try {/* w w w.  j a va2  s.  co m*/
        Transferable t = e.getTransferable();

        if (e.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            e.acceptDrop(e.getDropAction());

            String s;
            s = (String) t.getTransferData(DataFlavor.stringFlavor);

            target.setText(s);

            e.dropComplete(true);
        } else
            e.rejectDrop();
    } catch (java.io.IOException e2) {
    } catch (UnsupportedFlavorException e2) {
    }
}

From source file:TransferableColor.java

/** 
 * Transfer the data.  Given a specified DataFlavor, return an Object
 * appropriate for that flavor.  Throw UnsupportedFlavorException if we
 * don't support the requested flavor./* w  w  w. ja  v a  2s . c om*/
 */
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
    if (flavor.equals(colorFlavor))
        return color;
    else if (flavor.equals(DataFlavor.stringFlavor))
        return color.toString();
    else if (flavor.equals(DataFlavor.plainTextFlavor))
        return new ByteArrayInputStream(color.toString().getBytes("Unicode"));
    else
        throw new UnsupportedFlavorException(flavor);
}

From source file:MainClass.java

public void actionPerformed(ActionEvent evt) {
    String cmd = evt.getActionCommand();

    if (cmd.equals("copy")) {
        // Implement Copy operation
        String srcData = srcText.getText();
        if (srcData != null) {
            StringSelection contents = new StringSelection(srcData);
            clipboard.setContents(contents, this);
            pasteButton.setEnabled(true);
        }/*  ww  w.j  a v  a2 s .c  om*/
    } else if (cmd.equals("paste")) {
        // Implement Paste operation
        Transferable content = clipboard.getContents(this);
        if (content != null) {
            try {
                String dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
                dstText.append(dstData);
            } catch (Exception e) {
                System.out.println("Couldn't get contents in format: "
                        + DataFlavor.stringFlavor.getHumanPresentableName());
            }
        }
    }
}

From source file:javazoom.jlgui.player.amp.skin.DropTargetAdapter.java

protected boolean isDragOk(DropTargetDragEvent e) {
    // Check DataFlavor
    DataFlavor[] dfs = e.getCurrentDataFlavors();
    DataFlavor tdf = null;/*from  w ww  .  j av  a2s  .c  o  m*/
    for (int i = 0; i < dfs.length; i++) {
        if (DataFlavor.javaFileListFlavor.equals(dfs[i])) {
            tdf = dfs[i];
            break;
        } else if (DataFlavor.stringFlavor.equals(dfs[i])) {
            tdf = dfs[i];
            break;
        }
    }
    // Only file list allowed.
    if (tdf != null) {
        // Only DnD COPY allowed.
        if ((e.getSourceActions() & DnDConstants.ACTION_COPY) != 0) {
            return true;
        } else
            return false;
    } else
        return false;
}

From source file:de.drop_converter.plugins.binary_convert.Hex2Data.java

@Override
public boolean importData(TransferSupport support) throws ConverterException {
    try {//from  w w  w  .  ja v  a2s. c o m
        if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            List<File> files = (List<File>) support.getTransferable()
                    .getTransferData(DataFlavor.javaFileListFlavor);
            for (File file : files) {
                FileInputStream fis = null;
                OutputStream out = null;
                try {
                    out = getOutputStream(file, ".hex");
                    fis = new FileInputStream(file);
                    byte[] buffer = new byte[bufferSize];
                    int count = 0;
                    while (-1 != (count = fis.read(buffer))) {
                        if (count == bufferSize) {
                            out.write(Hex.decodeHex(new String(buffer).toCharArray()));
                        } else {
                            byte[] tmp = Arrays.copyOf(Hex.decodeHex(new String(buffer).toCharArray()), count);
                            out.write(tmp);
                        }
                    }
                } catch (Exception e) {
                    throw new ConverterException(e);
                } finally {
                    IOUtils.closeQuietly(out);
                    IOUtils.closeQuietly(fis);
                }
            }
            return true;
        } else if (support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
            OutputStream out = null;
            try {
                byte[] encode = Hex.decodeHex(data.toCharArray());
                out = getOutputStream(null, ".hex");
                out.write(encode);
            } catch (Exception e) {
                throw new ConverterException(e);
            } finally {
                IOUtils.closeQuietly(out);
            }
        }
    } catch (Exception e) {
        throw new ConverterException(e);
    }

    return false;
}

From source file:DragDropList.java

public boolean canImport(TransferHandler.TransferSupport support) {
    if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        return false;
    }/*from   w w w . j  a  va  2s .  c  o  m*/
    JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
    if (dl.getIndex() == -1) {
        return false;
    } else {
        return true;
    }
}

From source file:net.sf.jabref.external.TransferableFileLinkSelection.java

@Override
public boolean isDataFlavorSupported(DataFlavor dataFlavor) {
    LOGGER.debug("Query: " + dataFlavor.getHumanPresentableName() + " , "
            + dataFlavor.getDefaultRepresentationClass() + " , " + dataFlavor.getMimeType());
    return dataFlavor.equals(DataFlavor.javaFileListFlavor) || dataFlavor.equals(DataFlavor.stringFlavor);
}

From source file:net.sf.jabref.gui.fieldeditors.FileListEditorTransferHandler.java

/**
 *
 * @param frame/*w  w  w .j a  v a2 s .  co  m*/
 * @param entryContainer
 * @param textTransferHandler is an instance of javax.swing.plaf.basic.BasicTextUI.TextTransferHandler. That class is not visible. Therefore, we have to "cheat"
 */
public FileListEditorTransferHandler(JabRefFrame frame, EntryContainer entryContainer,
        TransferHandler textTransferHandler) {
    this.frame = frame;
    this.entryContainer = entryContainer;
    this.textTransferHandler = textTransferHandler;
    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 file link table", e);
    }
}

From source file:de.drop_converter.plugins.binary_convert.Data2Hex.java

@Override
public boolean importData(TransferSupport support) throws ConverterException {
    try {//  ww  w.j  a va2s .co  m
        if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            List<File> files = (List<File>) support.getTransferable()
                    .getTransferData(DataFlavor.javaFileListFlavor);
            for (File file : files) {
                FileInputStream fis = null;
                OutputStream out = null;
                try {
                    out = getOutputStream(file, ".hex");
                    fis = new FileInputStream(file);
                    byte[] buffer = new byte[bufferSize];
                    int count = 0;
                    while (-1 != (count = fis.read(buffer))) {
                        if (count == bufferSize) {
                            out.write(Hex.encodeHexString(buffer).getBytes("UTF-8"));
                        } else {
                            byte[] tmp = Arrays.copyOf(Hex.encodeHexString(buffer).getBytes("UTF-8"), count);
                            out.write(tmp);
                        }
                    }
                } catch (Exception e) {
                    throw new ConverterException(e);
                } finally {
                    IOUtils.closeQuietly(out);
                    IOUtils.closeQuietly(fis);
                }
            }
            return true;
        } else if (support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
            OutputStream out = null;
            try {
                byte[] encode = Hex.encodeHexString(data.getBytes()).getBytes();
                out = getOutputStream(null, ".hex");
                out.write(encode);
            } catch (Exception e) {
                throw new ConverterException(e);
            } finally {
                IOUtils.closeQuietly(out);
            }

        }
    } catch (Exception e) {
        throw new ConverterException(e);
    }

    return false;
}

From source file:ColorSink.java

public void pastecolor() {
    // Get the clipboard, and read its contents
    Clipboard c = this.getToolkit().getSystemClipboard();
    Transferable t = c.getContents(this);
    if (t == null) { // If nothing to paste
        this.getToolkit().beep(); // then beep and do nothing
        return;// www  .java 2s .co m
    }
    try {
        // If the clipboard contained a color, use it as the background color
        if (t.isDataFlavorSupported(TransferableColor.colorFlavor)) {
            Color color = (Color) t.getTransferData(TransferableColor.colorFlavor);
            this.setBackground(color);
        }
        // If the clipboard contained text, insert it.
        else if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String s = (String) t.getTransferData(DataFlavor.stringFlavor);
            this.replaceSelection(s);
        }
        // Otherwise, we don't know how to paste the data, so just beep.
        else
            this.getToolkit().beep();
    } catch (UnsupportedFlavorException ex) {
        this.getToolkit().beep();
    } catch (IOException ex) {
        this.getToolkit().beep();
    }
}