Java Code Examples for java.awt.datatransfer.Transferable#isDataFlavorSupported()

The following examples show how to use java.awt.datatransfer.Transferable#isDataFlavorSupported() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataTransferer.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private String getBestCharsetForTextFormat(Long lFormat,
    Transferable localeTransferable) throws IOException
{
    String charset = null;
    if (localeTransferable != null &&
        isLocaleDependentTextFormat(lFormat) &&
        localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor))
    {
        try {
            charset = new String(
                (byte[])localeTransferable.getTransferData(javaTextEncodingFlavor),
                "UTF-8"
            );
        } catch (UnsupportedFlavorException cannotHappen) {
        }
    } else {
        charset = getCharsetForTextFormat(lFormat);
    }
    if (charset == null) {
        // Only happens when we have a custom text type.
        charset = getDefaultTextCharset();
    }
    return charset;
}
 
Example 2
Source File: DataTransferer.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
protected String getBestCharsetForTextFormat(Long lFormat,
    Transferable localeTransferable) throws IOException
{
    String charset = null;
    if (localeTransferable != null &&
        isLocaleDependentTextFormat(lFormat) &&
        localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor)) {
        try {
            byte[] charsetNameBytes = (byte[])localeTransferable
                    .getTransferData(javaTextEncodingFlavor);
            charset = new String(charsetNameBytes, StandardCharsets.UTF_8);
        } catch (UnsupportedFlavorException cannotHappen) {
        }
    } else {
        charset = getCharsetForTextFormat(lFormat);
    }
    if (charset == null) {
        // Only happens when we have a custom text type.
        charset = Charset.defaultCharset().name();
    }
    return charset;
}
 
Example 3
Source File: ArtWorkController.java    From AudioBookConverter with GNU General Public License v2.0 6 votes vote down vote up
public void pasteImage(ActionEvent actionEvent) {
    try {
        Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
        if (transferable != null) {
            if (transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                java.awt.Image image = (java.awt.Image) transferable.getTransferData(DataFlavor.imageFlavor);
                Image fimage = awtImageToFX(image);
                ConverterApplication.getContext().addPosterIfMissingWithDelay(new ArtWorkImage(fimage));
            } else if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                java.util.List<String> artFiles = (java.util.List<String>) transferable.getTransferData(DataFlavor.javaFileListFlavor);

                artFiles.stream().filter(s -> ArrayUtils.contains(ArtWork.IMAGE_EXTENSIONS, FilenameUtils.getExtension(s))).forEach(f -> {
                    ConverterApplication.getContext().addPosterIfMissingWithDelay(new ArtWorkBean(f));
                });
            }
        }
    } catch (Exception e) {
        logger.error("Failed to load from clipboard", e);
        e.printStackTrace();
    }
}
 
Example 4
Source File: DnDSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Remove a toolbar button represented by the given Transferable.
 */
private void removeButton( Transferable t ) {
    try {
        Object o = null;
        if( t.isDataFlavorSupported(buttonDataFlavor) ) {
            o = t.getTransferData(buttonDataFlavor);
        }
        if( null != o && o instanceof DataObject ) {
            ((DataObject) o).delete();
            sourceToolbar.repaint();
        }
    } catch( UnsupportedFlavorException e ) {
        log.log( Level.INFO, null, e );
    } catch( IOException ioE ) {
        log.log( Level.INFO, null, ioE );
    }
}
 
Example 5
Source File: DnDSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean handleDropImpl(Transferable t) {       
    try {
        Object o;
        if( t.isDataFlavorSupported( actionDataFlavor ) ) {
            o = t.getTransferData( actionDataFlavor );
            if( o instanceof Node ) {
                DataObject dobj = ((Node)o).getLookup().lookup( DataObject.class );
                return addButton( dobj, dropTargetButtonIndex, insertBefore );
            }
        } else {
            o = t.getTransferData( buttonDataFlavor );
            if( o instanceof DataObject ) {
                return moveButton( (DataObject)o, dropTargetButtonIndex, insertBefore );
            }
        }
    } catch( UnsupportedFlavorException e ) {
        log.log( Level.INFO, null, e );
    } catch( IOException ioE ) {
        log.log( Level.INFO, null, ioE );
    }
    return false;
}
 
Example 6
Source File: WDataTransferer.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public byte[] translateTransferable(Transferable contents,
                                    DataFlavor flavor,
                                    long format) throws IOException
{
    byte[] bytes = null;
    if (format == CF_HTML) {
        if (contents.isDataFlavorSupported(DataFlavor.selectionHtmlFlavor)) {
            // if a user provides data represented by
            // DataFlavor.selectionHtmlFlavor format, we use this
            // type to store the data in the native clipboard
            bytes = super.translateTransferable(contents,
                    DataFlavor.selectionHtmlFlavor,
                    format);
        } else if (contents.isDataFlavorSupported(DataFlavor.allHtmlFlavor)) {
            // if we cannot get data represented by the
            // DataFlavor.selectionHtmlFlavor format
            // but the DataFlavor.allHtmlFlavor format is avialable
            // we belive that the user knows how to represent
            // the data and how to mark up selection in a
            // system specific manner. Therefor, we use this data
            bytes = super.translateTransferable(contents,
                    DataFlavor.allHtmlFlavor,
                    format);
        } else {
            // handle other html flavor types, including custom and
            // fragment ones
            bytes = HTMLCodec.convertToHTMLFormat(super.translateTransferable(contents, flavor, format));
        }
    } else {
        // we handle non-html types basing on  their
        // flavors
        bytes = super.translateTransferable(contents, flavor, format);
    }
    return bytes;
}
 
Example 7
Source File: SunClipboard.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @see java.awt.Clipboard#isDataFlavorAvailable
 * @since 1.5
 */
public boolean isDataFlavorAvailable(DataFlavor flavor) {
    if (flavor == null) {
        throw new NullPointerException("flavor");
    }

    Transferable cntnts = getContextContents();
    if (cntnts != null) {
        return cntnts.isDataFlavorSupported(flavor);
    }

    long[] formats = getClipboardFormatsOpenClose();

    return formatArrayAsDataFlavorSet(formats).contains(flavor);
}
 
Example 8
Source File: DoTypeProcessor.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static String getClipboardContent() {
    String result = "";
    final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    final Transferable contents = clipboard.getContents(null);
    if (contents != null && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        try {
            result = (String) contents.getTransferData(DataFlavor.stringFlavor);
        }
        catch (final UnsupportedFlavorException | IOException ex) {
        }
    }
    return result;
}
 
Example 9
Source File: SunDropTargetContextPeer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return if the flavor is supported
 */

public boolean isDataFlavorSupported(DataFlavor df) {
    Transferable localTransferable = local;

    if (localTransferable != null) {
        return localTransferable.isDataFlavorSupported(df);
    } else {
        return DataTransferer.getInstance().getFlavorsForFormats
            (currentT, DataTransferer.adaptFlavorMap
                (currentDT.getFlavorMap())).
            containsKey(df);
    }
}
 
Example 10
Source File: GUIUtil.java    From CXTouch with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return the file list on clipboard.
 */
public static java.util.List<File> getClipFile() {
    Transferable transfer = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
    if (!transfer.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        return null;
    }
    java.util.List<File> fileList;
    try {
        fileList = (java.util.List<File>) transfer.getTransferData(DataFlavor.javaFileListFlavor);
        return fileList;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return null;
    }
}
 
Example 11
Source File: SunClipboard.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @see java.awt.Clipboard#isDataFlavorAvailable
 * @since 1.5
 */
public boolean isDataFlavorAvailable(DataFlavor flavor) {
    if (flavor == null) {
        throw new NullPointerException("flavor");
    }

    Transferable cntnts = getContextContents();
    if (cntnts != null) {
        return cntnts.isDataFlavorSupported(flavor);
    }

    long[] formats = getClipboardFormatsOpenClose();

    return formatArrayAsDataFlavorSet(formats).contains(flavor);
}
 
Example 12
Source File: SunDropTargetContextPeer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return if the flavor is supported
 */

public boolean isDataFlavorSupported(DataFlavor df) {
    Transferable localTransferable = local;

    if (localTransferable != null) {
        return localTransferable.isDataFlavorSupported(df);
    } else {
        return DataTransferer.getInstance().getFlavorsForFormats
            (currentT, DataTransferer.adaptFlavorMap
                (currentDT.getFlavorMap())).
            containsKey(df);
    }
}
 
Example 13
Source File: WDataTransferer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public byte[] translateTransferable(Transferable contents,
                                    DataFlavor flavor,
                                    long format) throws IOException
{
    byte[] bytes = null;
    if (format == CF_HTML) {
        if (contents.isDataFlavorSupported(DataFlavor.selectionHtmlFlavor)) {
            // if a user provides data represented by
            // DataFlavor.selectionHtmlFlavor format, we use this
            // type to store the data in the native clipboard
            bytes = super.translateTransferable(contents,
                    DataFlavor.selectionHtmlFlavor,
                    format);
        } else if (contents.isDataFlavorSupported(DataFlavor.allHtmlFlavor)) {
            // if we cannot get data represented by the
            // DataFlavor.selectionHtmlFlavor format
            // but the DataFlavor.allHtmlFlavor format is avialable
            // we belive that the user knows how to represent
            // the data and how to mark up selection in a
            // system specific manner. Therefor, we use this data
            bytes = super.translateTransferable(contents,
                    DataFlavor.allHtmlFlavor,
                    format);
        } else {
            // handle other html flavor types, including custom and
            // fragment ones
            bytes = HTMLCodec.convertToHTMLFormat(super.translateTransferable(contents, flavor, format));
        }
    } else {
        // we handle non-html types basing on  their
        // flavors
        bytes = super.translateTransferable(contents, flavor, format);
    }
    return bytes;
}
 
Example 14
Source File: WDataTransferer.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public byte[] translateTransferable(Transferable contents,
                                    DataFlavor flavor,
                                    long format) throws IOException
{
    byte[] bytes = null;
    if (format == CF_HTML) {
        if (contents.isDataFlavorSupported(DataFlavor.selectionHtmlFlavor)) {
            // if a user provides data represented by
            // DataFlavor.selectionHtmlFlavor format, we use this
            // type to store the data in the native clipboard
            bytes = super.translateTransferable(contents,
                    DataFlavor.selectionHtmlFlavor,
                    format);
        } else if (contents.isDataFlavorSupported(DataFlavor.allHtmlFlavor)) {
            // if we cannot get data represented by the
            // DataFlavor.selectionHtmlFlavor format
            // but the DataFlavor.allHtmlFlavor format is avialable
            // we belive that the user knows how to represent
            // the data and how to mark up selection in a
            // system specific manner. Therefor, we use this data
            bytes = super.translateTransferable(contents,
                    DataFlavor.allHtmlFlavor,
                    format);
        } else {
            // handle other html flavor types, including custom and
            // fragment ones
            bytes = HTMLCodec.convertToHTMLFormat(super.translateTransferable(contents, flavor, format));
        }
    } else {
        // we handle non-html types basing on  their
        // flavors
        bytes = super.translateTransferable(contents, flavor, format);
    }
    return bytes;
}
 
Example 15
Source File: ImageUtils.java    From markdown-image-kit with MIT License 5 votes vote down vote up
/**
 * Gets image from clipboard.
 *
 * @return the image from clipboard
 */
@Nullable
public static Image getImageFromClipboard() {
    Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
    if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
        try {
            return (Image) transferable.getTransferData(DataFlavor.imageFlavor);
        } catch (IOException | UnsupportedFlavorException ignored) {
            // 如果 clipboard 没有图片, 不处理
        }
    }
    return null;
}
 
Example 16
Source File: SunDropTargetContextPeer.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return if the flavor is supported
 */

public boolean isDataFlavorSupported(DataFlavor df) {
    Transferable localTransferable = local;

    if (localTransferable != null) {
        return localTransferable.isDataFlavorSupported(df);
    } else {
        return DataTransferer.getInstance().getFlavorsForFormats
            (currentT, DataTransferer.adaptFlavorMap
                (currentDT.getFlavorMap())).
            containsKey(df);
    }
}
 
Example 17
Source File: CImageChooser.java    From Open-Lowcode with Eclipse Public License 2.0 5 votes vote down vote up
private static java.awt.Image getImageFromClipboard() {
	Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
	if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
		try {
			return (java.awt.Image) transferable.getTransferData(DataFlavor.imageFlavor);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	return null;
}
 
Example 18
Source File: TypeDropper.java    From constellation with Apache License 2.0 4 votes vote down vote up
@Override
public BiConsumer<Graph, DropInfo> drop(final DropTargetDropEvent dtde) {
    final Transferable transferable = dtde.getTransferable();
    if (transferable.isDataFlavorSupported(VX_DATA_FLAVOR) || transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        try {
            final String data;
            if (transferable.isDataFlavorSupported(VX_DATA_FLAVOR)) {
                final InputStream in = new ByteArrayInputStream(((ByteBuffer) transferable.getTransferData(VX_DATA_FLAVOR)).array());
                final ObjectInputStream oin = new ObjectInputStream(in);
                data = (String) oin.readObject();
            } else {
                final String t = (String) transferable.getTransferData(DataFlavor.stringFlavor);

                // Do we have the correct indicator?
                if (t != null && t.startsWith(INDICATOR)) {
                    // Skip the leading "indicator=".
                    data = t.substring(INDICATOR.length());
                } else {
                    data = null;
                }
            }

            if (data != null) {
                return (graph, dropInfo) -> {
                    PluginExecution.withPlugin(new SimpleEditPlugin("Drag and Drop: Type to Vertex") {
                        @Override
                        public void edit(final GraphWriteMethods wg, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
                            final int ix = data.indexOf(':');
                            if (ix != -1) {
                                final String attrLabel = data.substring(0, ix);
                                final String value = data.substring(ix + 1);
                                final int attrId = wg.getAttribute(GraphElementType.VERTEX, attrLabel);
                                if (attrId != Graph.NOT_FOUND) {
                                    final int vxId;

                                    if (dropInfo.isIsVertex()) {
                                        vxId = dropInfo.id;

                                        // If the vertex is selected, modify all of the selected vertices.
                                        // Otherwise, just modify the dropped-on vertex.
                                        final int selectedId = VisualConcept.VertexAttribute.SELECTED.ensure(wg);
                                        if (wg.getBooleanValue(selectedId, vxId)) {
                                            final GraphIndexResult gir = GraphIndexUtilities.filterElements(wg, selectedId, true);
                                            while (true) {
                                                final int selVxId = gir.getNextElement();
                                                if (selVxId == Graph.NOT_FOUND) {
                                                    break;
                                                }

                                                wg.setStringValue(attrId, selVxId, data);
                                                if (graph.getSchema() != null) {
                                                    graph.getSchema().completeVertex(wg, selVxId);
                                                }
                                            }
                                        } else {
                                            wg.setStringValue(attrId, vxId, data);
                                            if (graph.getSchema() != null) {
                                                graph.getSchema().completeVertex(wg, vxId);
                                            }
                                        }
                                    } else {
                                        vxId = wg.addVertex();
                                        final int xId = VisualConcept.VertexAttribute.X.ensure(wg);
                                        final int yId = VisualConcept.VertexAttribute.Y.ensure(wg);
                                        final int zId = VisualConcept.VertexAttribute.Z.ensure(wg);
                                        wg.setStringValue(attrId, vxId, value);
                                        wg.setFloatValue(xId, vxId, dropInfo.location.getX());
                                        wg.setFloatValue(yId, vxId, dropInfo.location.getY());
                                        wg.setFloatValue(zId, vxId, dropInfo.location.getZ());

                                        if (graph.getSchema() != null) {
                                            graph.getSchema().newVertex(wg, vxId);
                                            graph.getSchema().completeVertex(wg, vxId);
                                        }
                                    }

                                    ConstellationLoggerHelper.importPropertyBuilder(
                                            this,
                                            Arrays.asList(data),
                                            null,
                                            ConstellationLoggerHelper.SUCCESS
                                    );
                                }
                            }
                        }
                    }).executeLater(graph);
                };
            }
        } catch (final UnsupportedFlavorException | IOException | ClassNotFoundException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    return null;
}
 
Example 19
Source File: DragAndDropHandler.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Perform the drop operation and add the dragged item into the given category.
 *
 * @param targetCategory Lookup of the category that accepts the drop.
 * @param item Transferable holding the item being dragged.
 * @param dndAction Drag'n'drop action type.
 * @param dropIndex Zero-based position where the dragged item should be dropped.
 *
 * @return True if the drop has been successful, false otherwise.
 */
public boolean doDrop( Lookup targetCategory, Transferable item, int dndAction, int dropIndex ) {
    Node categoryNode = (Node)targetCategory.lookup( Node.class );
    try {
        //first check if we're reordering items within the same category
        if( item.isDataFlavorSupported( PaletteController.ITEM_DATA_FLAVOR ) ) {
            Lookup itemLookup = (Lookup)item.getTransferData( PaletteController.ITEM_DATA_FLAVOR );
            if( null != itemLookup ) {
                Node itemNode = (Node)itemLookup.lookup( Node.class );
                if( null != itemNode ) {
                    Index order = (Index)categoryNode.getCookie( Index.class );
                    if( null != order && order.indexOf( itemNode ) >= 0 ) {
                        //the drop item comes from the targetCategory so let's 
                        //just change the order of items
                        return moveItem( targetCategory, itemLookup, dropIndex );
                    }
                }
            }
        }
        PasteType paste = categoryNode.getDropType( item, dndAction, dropIndex );
        if( null != paste ) {
            Node[] itemsBefore = categoryNode.getChildren().getNodes( DefaultModel.canBlock() );
            paste.paste();
            Node[] itemsAfter = categoryNode.getChildren().getNodes( DefaultModel.canBlock() );
            
            if( itemsAfter.length == itemsBefore.length+1 ) {
                int currentIndex = -1;
                Node newItem = null;
                for( int i=itemsAfter.length-1; i>=0; i-- ) {
                    newItem = itemsAfter[i];
                    currentIndex = i;
                    for( int j=0; j<itemsBefore.length; j++ ) {
                        if( newItem.equals( itemsBefore[j] ) ) {
                            newItem = null;
                            break;
                        }
                    }
                    if( null != newItem ) {
                        break;
                    }
                }
                if( null != newItem && dropIndex >= 0 ) {
                    if( currentIndex < dropIndex )
                        dropIndex++;
                    moveItem( targetCategory, newItem.getLookup(), dropIndex );
                }
            }
            return true;
        }
        if( isTextDnDEnabled && null != DataFlavor.selectBestTextFlavor(item.getTransferDataFlavors()) ) {
            importTextIntoPalette( targetCategory, item, dropIndex );
            return false; //return false to retain the original dragged text in its source
        }
    } catch( IOException ioE ) {
        Logger.getLogger( DragAndDropHandler.class.getName() ).log( Level.INFO, null, ioE );
    } catch( UnsupportedFlavorException e ) {
        Logger.getLogger( DragAndDropHandler.class.getName() ).log( Level.INFO, null, e );
    }
    return false;
}
 
Example 20
Source File: TableTabClip.java    From Logisim with GNU General Public License v3.0 4 votes vote down vote up
public boolean canPaste() {
	Clipboard clip = table.getToolkit().getSystemClipboard();
	Transferable xfer = clip.getContents(this);
	return xfer.isDataFlavorSupported(binaryFlavor);
}