JDK-4209431 : Can't set custom content on System clipboard on NT
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1.7
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1999-02-06
  • Updated: 1999-02-25
  • Resolved: 1999-02-25
Related Reports
Duplicate :  
Description

Name: bb33257			Date: 02/05/99


The following test defines custom content and places it on the 
clipboard.  When using a java.awt.datatransfer.Clipboard this
works fine, but when using the system clipboard on NT it fails:

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;

import java.awt.Toolkit;
import java.io.*;

public class TestSystemClipboardTransfer {

    /**
     * The DataFlavor for our simple contents.
     */
    private static final DataFlavor INTEGER_FLAVOR =
                new DataFlavor(Integer.class, "Just an Integer");

    /**
     * A simple Transferable which holds an Integer as content.
     * For convenience it implements ClipboardOwner.
     */
    private static class MyContents implements Transferable,
                                                ClipboardOwner {

        Integer value;

        public MyContents(Integer value) {
            this.value = value;
        }

        public synchronized DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[] { INTEGER_FLAVOR };
        }

        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return INTEGER_FLAVOR.equals(flavor);
        }

        public synchronized Object getTransferData(DataFlavor flavor)
                        throws UnsupportedFlavorException, IOException {
            if (!INTEGER_FLAVOR.equals(flavor)) {
                throw new UnsupportedFlavorException(flavor);
            }
            return value;
        }

        public void lostOwnership(Clipboard clipboard, Transferable contents) {
        }
    }

    /**
     * This method puts an instance of the above clipboard contents
     * on the clipboard, and then immediately reads the clipboard
     * to see whether the contents is there.
     *
     * The clipboard which is used is the system clipboard unless
     * "-privateclip" is passed as a command line argument.
     */
    public static void main(String[] args) throws Error, Exception {

        Clipboard clipboard;
        // Use either the System clipboard or a private clipboard
        if (args.length == 1 && args[0].equals("-privateclip")) {
            clipboard = new Clipboard("Private clipboard");
        }
        else {
            clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        }

        // Value to use as transfer data
        Integer value = new Integer(-42);

        // Put an instance of our contents on the clipboard.
        MyContents myContents = new MyContents(value);
        clipboard.setContents(myContents, myContents);

        // Now immediately get the clipboard contents and make sure
        // it is what we put on there.
        Transferable contents = clipboard.getContents(new Object());
        if (contents == null) {
            throw new Error("Contents should not be null!");
        }

        if (!contents.isDataFlavorSupported(INTEGER_FLAVOR)) {
            throw new Error("Contents should support our data flavor.");
        }

        Object data = contents.getTransferData(INTEGER_FLAVOR);
        if (!value.equals(data)) {
            throw new Error("Incorrect contents returned.");
        }

        System.out.println("PASSED");
    }
}

(Review ID: 53823)

======================================================================

Comments
WORK AROUND Name: bb33257 Date: 02/05/99 The workaround we used in an application is to wrap the system clipboard in our own clipboard class, which correctly keeps track of its contents. ======================================================================
11-06-2004