Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4209431

Can't set custom content on System clipboard on NT

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1.7
    • client-libs
    • x86
    • windows_nt



      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)

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

            Unassigned Unassigned
            bcbeck Brian Beck (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: