开发者

What is the required DataFlavor to copy files in Mac OSX

开发者 https://www.devze.com 2023-01-06 16:21 出处:网络
I\'m currently working on Java code that can copy files into the system clipboard. For Windows and Linux I already got it working. For OSX I tried several flavors but the \"Paste\" action in Finder n

I'm currently working on Java code that can copy files into the system clipboard.

For Windows and Linux I already got it working. For OSX I tried several flavors but the "Paste" action in Finder never cames active.

Any idea which DataFlavor settings are required for Finder?

Used flavors:

DataFlavor.javaFileListFlavor
URILIST_FLAVOR = new DataFlavor( "text/uri-list" );
XFILELIST_FLAVOR = new DataFlavor( "application/x-java-file-list" );
GNOMEFILELIST_FLAVOR = new DataFlavor( "x-special/gnome-copied-files" );

The method to return the data for the flavor:

public Object getTransferData( DataFlavor flavor ) throws UnsupportedFlavorException, IOException {
    if( FILELIST_FLAVOR.equals( flavor ) ) {
        if( List.class == flavor.getRepresentationClass() ) {
            return Arrays.asList( files );
        } else if( InputStream.class == flavor.getRepresentationClass() ) {
            return getStreamData( files, null );
        }
    } else if( DataFlavor.javaFileListFlavor.equals( flavor ) )开发者_开发知识库 {
        if( List.class == flavor.getRepresentationClass() ) {
            return locallist;
        } else if( InputStream.class == flavor.getRepresentationClass() ) {
            return getStreamData( files, null );
        }
    } else if( URILIST_FLAVOR.equals( flavor ) ) {
        if( List.class == flavor.getRepresentationClass() ) {
            return Arrays.asList( files );
        } else if( InputStream.class == flavor.getRepresentationClass() ) {
            return getStreamData( files, null );
        }
    } else if( GNOMEFILELIST_FLAVOR.equals( flavor ) ) {
        if( List.class == flavor.getRepresentationClass() ) {
            return Arrays.asList( files );
        } else if( InputStream.class == flavor.getRepresentationClass() ) {
            // FIXME support cut and copy
            return getStreamData( files, "copy" );
        }
    } else if( XFILELIST_FLAVOR.equals( flavor ) ) {
        if( List.class == flavor.getRepresentationClass() ) {
            return locallist;
        } else if( InputStream.class == flavor.getRepresentationClass() ) {
            return getStreamData( files, null );
        }
    }
    throw new UnsupportedFlavorException( flavor );
}

Thanks, André


Maybe you should try DataFlavor#javaFileListFlavor.

Excerpt from the API documentation:

DataFlavor#javaFileListFlavor (Link)

To transfer a list of files to/from Java (and the underlying platform) a DataFlavor of this type/subtype and representation class of java.util.List is used. Each element of the list is required/guaranteed to be of type java.io.File.


I did a small investigation and here is the answer:

It should work with

DataFlavor#javaFileListFlavor

but the copy-paste functionality for files is broken on the native side for all JDKs that are based on OpenJDK.

The OpenJDK implementation uses NSPasteboard.sendData method with the format NSFilenamesPboardType. Unfortunately, NSFilenamesPboardType is deprecated and removed after 10.14. So the functionality worked in 10.13 and before but is broken after the version.

There are a couple of possible solutions:

  1. I've found the NSPasteboardAPI library that provides proper native API. Here is the java implementation based on the library.

  2. I've fixed the problem in the JetBrains Runtime on the native side so it just works with DataFlavor#javaFileListFlavor as you expected.

It is funny that the problem exists for ~10 years at this moment. I believe it will be also fixed in some upcoming OpenJDK versions, but I am not sure when.

0

精彩评论

暂无评论...
验证码 换一张
取 消