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

Can't get and set clipboard contents in ClipboardOwner.lostOwnership()

XMLWordPrintable

    • mantis
    • generic
    • generic
    • Verified



      Name: dsR10078 Date: 05/13/2002

      ###@###.###
      The following test case reproduces the problem:
      ------------------------------------------------------------------------------------------
      import java.awt.datatransfer.*;

      public class CbOwner implements ClipboardOwner {
          static Clipboard clipboard = new Clipboard("cb");
          int m, id;

          public CbOwner(int m) { this.m = m; id = m; }

          public void lostOwnership(Clipboard cb, Transferable contents) {
              System.err.println(id + " lost clipboard ownership");

              Transferable t = clipboard.getContents(null);
              String msg = null;
              try {
                  msg = (String)t.getTransferData(DataFlavor.stringFlavor);
              } catch (Exception e) {
                   System.err.println(id + " LO: can't getTransferData: " + e);
              }
              System.err.println(id + " clipboard.getContents(): " + msg);
              m += 2;
              if (m > 5) System.exit(0);
              System.err.println(id + " clipboard.setContents(): " + m);
              clipboard.setContents(new StringSelection(m + ""), this);
          }

          public static void main(String[] args) throws Exception {
              CbOwner cbo1 = new CbOwner(0);
              System.err.println(cbo1.m + " clipboard.setContents(): " + cbo1.m);
              clipboard.setContents(new StringSelection(cbo1.m + ""), cbo1);
              CbOwner cbo2 = new CbOwner(1);
              System.err.println(cbo2.m + " clipboard.setContents(): " + cbo2.m);
              clipboard.setContents(new StringSelection(cbo2.m + ""), cbo2);
          }
      }
      ------------------------------------------------------------------------------------------
      The problem can be reproduced with 1.4.1-b10 on all platforms.
      Start the test.
      You get the following output:
      ----------------------------
      0 clipboard.setContents(): 0
      1 clipboard.setContents(): 1
      0 lost clipboard ownership
      0 clipboard.getContents(): 0
      0 clipboard.setContents(): 2
      ----------------------------
      and then the application terminates.

      We expect that clipboard.getContents() will return contents put into
      the clipboard by new clipboard owner because new clipboard owner
      has called clipboard.setContents() and thus has got clipboard
      ownership. But clipboard.getContents() returns what was previously
      put into the clipboard by the same instance of CbOwner.

      We also expect that calling setContents() with clipboard owner
      different from the current clipboard owner always triggers
      lostOwnership() on current clipboard owner.
      But the expected call to lostOwnership() does not occur if
      setContents() is called from lostOwnership() (as in the example above).

      If there were not this bug, we should get the following output:
      ----------------------------
      0 clipboard.setContents(): 0
      1 clipboard.setContents(): 1
      0 lost clipboard ownership
      0 clipboard.getContents(): 1
      0 clipboard.setContents(): 2
      1 lost clipboard ownership
      1 clipboard.getContents(): 2
      1 clipboard.setContents(): 3
      0 lost clipboard ownership
      0 clipboard.getContents(): 3
      0 clipboard.setContents(): 4
      1 lost clipboard ownership
      1 clipboard.getContents(): 4
      1 clipboard.setContents(): 5
      0 lost clipboard ownership
      0 clipboard.getContents(): 5
      ----------------------------

      Note, that this bug also manifests when we use system clipboard
      and clipboard owners are in one JVM or in JVM and other process
      (for example, another JVM).

      ###@###.### 2002-05-08
      ======================================================================

            agerasimsunw Alexander Gerasimov (Inactive)
            dassunw Das Das (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: