Example usage for java.awt.datatransfer DataFlavor plainTextFlavor

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

Introduction

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

Prototype

DataFlavor plainTextFlavor

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

Click Source Link

Document

The DataFlavor representing plain text with Unicode encoding, where:
 representationClass = InputStream mimeType            = "text/plain; charset=unicode" 
This DataFlavor has been deprecated because:
  • Its representation is an InputStream, an 8-bit based representation, while Unicode is a 16-bit character set
  • The charset "unicode" is not well-defined.

    Usage

    From source file:Main.java

    public static void main(String[] args) {
    
        DataFlavor df = DataFlavor.plainTextFlavor;
    
        System.out.println(df.toString());
    }
    

    From source file:TransferableColor.java

    /** Check whether a specified DataFlavor is available */
    public boolean isDataFlavorSupported(DataFlavor flavor) {
        if (flavor.equals(colorFlavor) || flavor.equals(DataFlavor.stringFlavor)
                || flavor.equals(DataFlavor.plainTextFlavor))
            return true;
        return false;
    }
    

    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.//from   ww  w.jav  a2s .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:EditorDropTarget2.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;/*from   ww w .  ja v  a 2 s .  c  om*/
        draggingFile = false;
    
        if (DnDUtils.isDebugEnabled()) {
            DataFlavor[] flavors = dtde.getCurrentDataFlavors();
            for (int i = 0; i < flavors.length; i++) {
                DataFlavor flavor = flavors[i];
                DnDUtils.debugPrintln("Drop MIME type " + flavor.getMimeType() + " is available");
            }
        }
    
        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);
    }
    

    From source file:EditorDropTarget2.java

    protected boolean dropContent(Transferable transferable, DropTargetDropEvent dtde) {
        if (!pane.isEditable()) {
            // Can't drop content on a read-only text control
            return false;
        }//from  www. j a  va 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];
    
                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 void checkTransferType(DropTargetDragEvent dtde) {
        // Accept a list of files, or data content that
        // amounts to plain text or a Unicode text string
        acceptableType = false;//from ww w .  j ava  2s.  c  om
        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:EditorDropTarget3.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 . j av a 2 s.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:EditorDropTarget4.java

    protected boolean dropContent(Transferable transferable, DropTargetDropEvent dtde) {
        if (!pane.isEditable()) {
            // Can't drop content on a read-only text control
            return false;
        }//  w  w  w. jav 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;
        }
    }