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:dnd.TextTransferHandler.java

/**
 * Perform the actual import.  This method supports both drag and
 * drop and cut/copy/paste./*from  w  w w  .java2 s. c o  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:DNDList.java

/**
 * a drop has occurred//from  w  w w .  j  a  va 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: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   ww  w . ja  v  a2 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:ru.scratch.ScratchListenClipboardAction.java

@Override
public void contentChanged(@Nullable Transferable oldTransferable, Transferable newTransferable) {
    if (!ScratchData.getInstance().isAppendContentFromClipboard())
        return;/* ww w. j  a va  2  s  . c o  m*/

    try {
        String oldClipboard = null;
        if (oldTransferable != null && oldTransferable.getTransferData(DataFlavor.stringFlavor) != null) {
            oldClipboard = oldTransferable.getTransferData(DataFlavor.stringFlavor).toString();
        }
        String clipboard = null;
        if (newTransferable != null && newTransferable.getTransferData(DataFlavor.stringFlavor) != null) {
            clipboard = newTransferable.getTransferData(DataFlavor.stringFlavor).toString();
        }
        if (clipboard != null && !StringUtils.equals(oldClipboard, clipboard)) {
            writeToScratch(clipboard);
        }
    } catch (UnsupportedFlavorException e) {
        LOG.info(e);
    } catch (IOException e) {
        LOG.info(e);
    }
}

From source file:DnDDemo2.java

public TextColor(String text, Color color) {
    String colorMimeType = DataFlavor.javaJVMLocalObjectMimeType + ";class=java.awt.Color";
    DataFlavor colorFlavor = null;
    try {/* w ww  .  j a  v a  2 s .  c om*/
        colorFlavor = new DataFlavor(colorMimeType);
    } catch (ClassNotFoundException e) {
    }
    flavors = new DataFlavor[2];
    flavors[0] = DataFlavor.stringFlavor;
    flavors[1] = colorFlavor;
    this.text = text;
    this.color = color;
}

From source file:org.fife.ui.rtextarea.RTATextTransferHandler.java

/**
 * Try to find a flavor that can be used to import a Transferable to a
 * specified text component.  /*ww  w  .  j a va 2 s .c o  m*/
 * The set of usable flavors are tried in the following order:
 * <ol>
 *     <li>First, an attempt is made to find a flavor matching the content
 *         tyep of the EditorKit for the component.
 *     <li>Second, an attempt to find a text/plain flavor is made.
 *     <li>Third, an attempt to find a flavor representing a String
 *         reference in the same VM is made.
 *     <li>Lastly, DataFlavor.stringFlavor is searched for.
 * </ol>
 *
 * @param flavors The flavors to check if c will accept them.
 * @param c The text component to see whether it will accept any of the
 *        specified data flavors as input.
 */
protected DataFlavor getImportFlavor(DataFlavor[] flavors, JTextComponent c) {

    DataFlavor refFlavor = null;
    DataFlavor stringFlavor = null;

    for (int i = 0; i < flavors.length; i++) {

        String mime = flavors[i].getMimeType();
        if (mime.startsWith("text/plain")) {
            return flavors[i];
        } else if (refFlavor == null && mime.startsWith("application/x-java-jvm-local-objectref")
                && flavors[i].getRepresentationClass() == String.class) {
            refFlavor = flavors[i];
        } else if (stringFlavor == null && flavors[i].equals(DataFlavor.stringFlavor)) {
            stringFlavor = flavors[i];
        }

    }

    if (refFlavor != null)
        return refFlavor;
    else if (stringFlavor != null)
        return stringFlavor;

    return null;

}

From source file:dnd.ListTransferHandler.java

public boolean importData(TransferHandler.TransferSupport info) {
    if (!info.isDrop()) {
        return false;
    }// w w w .j  a va 2  s. c  o  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:ComplexExample.java

public boolean isDataFlavorSupported(DataFlavor flavor) {
    if (flavor.equals(colorFlavor) || flavor.equals(DataFlavor.stringFlavor))
        return true;
    return false;
}

From source file:DragDropList.java

public boolean importData(TransferHandler.TransferSupport support) {
    if (!canImport(support)) {
        return false;
    }/*from www  .  ja  va 2 s  .  com*/

    Transferable transferable = support.getTransferable();
    String indexString;
    try {
        indexString = (String) transferable.getTransferData(DataFlavor.stringFlavor);
    } catch (Exception e) {
        return false;
    }

    int index = Integer.parseInt(indexString);
    JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
    int dropTargetIndex = dl.getIndex();

    System.out.println(dropTargetIndex + " : ");
    System.out.println("inserted");
    return true;
}

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

public void drop(DropTargetDropEvent e) {
    // Check DataFlavor
    DataFlavor[] dfs = e.getCurrentDataFlavors();
    DataFlavor tdf = null;//from  w  w w.  j  a  v a 2s . co 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;
        }
    }
    // Data Flavor available ?
    if (tdf != null) {
        // Accept COPY DnD only.
        if ((e.getSourceActions() & DnDConstants.ACTION_COPY) != 0) {
            e.acceptDrop(DnDConstants.ACTION_COPY);
        } else
            return;
        try {
            Transferable t = e.getTransferable();
            Object data = t.getTransferData(tdf);
            processDrop(data);
        } catch (IOException ioe) {
            log.info("Drop error", ioe);
            e.dropComplete(false);
            return;
        } catch (UnsupportedFlavorException ufe) {
            log.info("Drop error", ufe);
            e.dropComplete(false);
            return;
        } catch (Exception ex) {
            log.info("Drop error", ex);
            e.dropComplete(false);
            return;
        }
        e.dropComplete(true);
    }
}