How do I share clipboard `DataFlavor` with another instance of the IDE?

Answered

How do I share clipboard DataFlavorwith another instance of the IDE?

In my plugin I created a data flavour to store link and reference information to accompany the clipboard text. That way if you copy a block of markdown text which contains reference links, images or other referencing elements but not the definition of those references, on paste the references are added to the file.

Additionally, the flavour has information about the targets of links so that when they are pasted into a file in another directory or project the links will be adjusted to correctly resolve to the right target.

It works if the copy/paste is in the same IDE instance. If I copy in one IDE and paste it into another IDE then the flavour is not in the Transferable, although I can see it in the OS X clipboard viewer app.

What am I missing to be able to share DataFlavour across IDE instances?

The flavour is defined as TextBlockTransferableDataand it is added to the clipboard with the plain text of the selection.

public class MdJsonTransferableData implements TextBlockTransferableData, Serializable {
    @NotNull public static final DataFlavor FLAVOR = FileCopyPasteUtil.createDataFlavor("text/x-markdown-navigator", MdJsonTransferableData.class, true);

    private final String myJson;
    private transient int myStartOffset;
    private transient int myEndOffset;
    @Nullable private transient Reader myDelegate;

    public String getJson() {
        return myJson;
    }

    @Override
    public DataFlavor getFlavor() {
        return FLAVOR;
    }

    @Override
    public int getOffsetCount() {
        return 2;
    }

    @Override
    public int getOffsets(final int[] offsets, final int index) {
        offsets[index] = myStartOffset;
        offsets[index + 1] = myEndOffset;
        return index + 2;
    }

    @Override
    public int setOffsets(final int[] offsets, final int index) {
        myStartOffset = offsets[index];
        myEndOffset = offsets[index + 1];
        return index + 2;
    }

    public MdJsonTransferableData(int startOffset, int endOffset, String json) {
        myStartOffset = startOffset;
        myEndOffset = endOffset;
        myJson = json;
    }

    @Override
    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException();
        }
    }
}
1 comment
Comment actions Permalink

Figured it out. Need to have the flavor registered properly to be convertible from/to native format. The FileCopyPasteUtil.createDataFlavor does not do it for the DataFlavorto be usable in another JVM instance.

public class MdJsonTransferableData implements TextBlockTransferableData, Serializable {
    @NotNull public static final DataFlavor FLAVOR;
    static {
        DataFlavor flavor;

        try {
            flavor = new DataFlavor("text/x-json-markdown-navigator; class=" + MdJsonTransferableData.class.getName(), "Markdown Navigator Extras", MdJsonTransferableData.class.getClassLoader());
            final FlavorMap map = SystemFlavorMap.getDefaultFlavorMap();
            if (map instanceof SystemFlavorMap) {
                String nat = SystemFlavorMap.encodeDataFlavor(flavor);
                ((SystemFlavorMap) map).addUnencodedNativeForFlavor(flavor, nat);
                ((SystemFlavorMap) map).addFlavorForUnencodedNative(nat, flavor);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            flavor = DataFlavor.stringFlavor;
        }

        FLAVOR = flavor;
    }

    private final String myJson;
    private transient int myStartOffset;
    private transient int myEndOffset;

    public String getJson() {
        return myJson;
    }

    @Override
    public DataFlavor getFlavor() {
        return FLAVOR;
    }

    @Override
    public int getOffsetCount() {
        return 2;
    }

    @Override
    public int getOffsets(final int[] offsets, final int index) {
        offsets[index] = myStartOffset;
        offsets[index + 1] = myEndOffset;
        return index + 2;
    }

    @Override
    public int setOffsets(final int[] offsets, final int index) {
        myStartOffset = offsets[index];
        myEndOffset = offsets[index + 1];
        return index + 2;
    }

    public MdJsonTransferableData(int startOffset, int endOffset, String json) {
        myStartOffset = startOffset;
        myEndOffset = endOffset;
        myJson = json;
    }

    @Override
    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException();
        }
    }
}
0

Please sign in to leave a comment.