JDK-4683804 : Can't get and set clipboard contents in ClipboardOwner.lostOwnership()
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2002-05-13
  • Updated: 2008-11-05
  • Resolved: 2002-08-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.2 mantisFixed
Related Reports
Relates :  
Relates :  
Description

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
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mantis mantis-b02 FIXED IN: mantis mantis-b02 INTEGRATED IN: mantis mantis-b02 VERIFIED IN: mantis
17-09-2004

WORK AROUND Name: dsR10078 Date: 05/13/2002 ======================================================================
17-09-2004

PUBLIC COMMENTS .
17-09-2004

SUGGESTED FIX Name: agR10216 Date: 05/16/2002 In setContents() of Clipboard, WClipboard, X11Clipboard and SunClipboard.lostOwnership() we should save old values of the fields Clipboard.owner and Clipboard.contents, then assign new values to them and finally call ClipboardOwner.lostOwnership() with the saved old values. ###@###.### 2002-05-14 ======================================================================
14-05-2002

EVALUATION Name: agR10216 Date: 05/16/2002 The matter is that in setContents() of Clipboard, WClipboard, X11Clipboard and SunClipboard.lostOwnership() we call ClipboardOwner.lostOwnership() at first, then assign new values to the fields Clipboard.owner and Clipboard.contents. Thus we clobber the values of these fields if they were changed in ClipboardOwner.lostOwnership(). ###@###.### 2002-05-14 ======================================================================
14-05-2002