7124253: [macosx] Flavor change notification not coming
authorpchelko
Fri, 15 Nov 2013 17:40:34 +0400
changeset 21784 71e061011ef1
parent 21783 a3863a1446bf
child 21785 fc0bfa7d9d95
7124253: [macosx] Flavor change notification not coming Reviewed-by: anthony, serb
jdk/src/macosx/classes/sun/lwawt/macosx/CClipboard.java
jdk/src/macosx/native/sun/awt/CClipboard.h
jdk/src/macosx/native/sun/awt/CClipboard.m
--- a/jdk/src/macosx/classes/sun/lwawt/macosx/CClipboard.java	Fri Nov 15 14:05:49 2013 +0400
+++ b/jdk/src/macosx/classes/sun/lwawt/macosx/CClipboard.java	Fri Nov 15 17:40:34 2013 +0400
@@ -25,8 +25,10 @@
 
 package sun.lwawt.macosx;
 
+import java.awt.*;
 import java.awt.datatransfer.*;
 import java.io.IOException;
+import java.io.NotSerializableException;
 import java.util.*;
 
 import sun.awt.datatransfer.*;
@@ -65,12 +67,10 @@
         long[] formatArray = dataTransferer.getFormatsForTransferableAsArray(contents, flavorMap);
         declareTypes(formatArray, this);
 
-        Map<Long, DataFlavor> formatMap = DataTransferer.getInstance().getFormatsForTransferable(contents, flavorMap);
-
-        for (Iterator<Long> iter = formatMap.keySet().iterator(); iter.hasNext(); ) {
-            Long lFormat = iter.next();
-            long format = lFormat.longValue();
-            DataFlavor flavor = formatMap.get(lFormat);
+        Map<Long, DataFlavor> formatMap = dataTransferer.getFormatsForTransferable(contents, flavorMap);
+        for (Map.Entry<Long, DataFlavor> entry : formatMap.entrySet()) {
+            long format = entry.getKey();
+            DataFlavor flavor = entry.getValue();
 
             try {
                 byte[] bytes = DataTransferer.getInstance().translateTransferable(contents, flavor, format);
@@ -80,17 +80,27 @@
                 // javaJVMLocalObjectMimeType failed to serialize.
                 // May remove this if-check when 5078787 is fixed.
                 if (!(flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType) &&
-                      e instanceof java.io.NotSerializableException)) {
+                        e instanceof NotSerializableException)) {
                     e.printStackTrace();
                 }
             }
         }
+
+        notifyChanged();
     }
 
-    private void lostSelectionOwnershipImpl() {
+    private void notifyLostOwnership() {
         lostOwnershipImpl();
     }
 
+    private static void notifyChanged() {
+        CClipboard clipboard = (CClipboard) Toolkit.getDefaultToolkit().getSystemClipboard();
+        if (!clipboard.areFlavorListenersRegistered()) {
+            return;
+        }
+        clipboard.checkChange(clipboard.getClipboardFormats());
+    }
+
     protected native long[] getClipboardFormats();
     protected native byte[] getClipboardData(long format) throws IOException;
 
--- a/jdk/src/macosx/native/sun/awt/CClipboard.h	Fri Nov 15 14:05:49 2013 +0400
+++ b/jdk/src/macosx/native/sun/awt/CClipboard.h	Fri Nov 15 17:40:34 2013 +0400
@@ -43,6 +43,4 @@
 - (NSArray *) javaGetTypes;
 - (NSData *) javaGetDataForType:(NSString *)inFormat;
 
-- (void) pasteboardChangedOwner:(NSPasteboard *)sender;
-
 @end
--- a/jdk/src/macosx/native/sun/awt/CClipboard.m	Fri Nov 15 14:05:49 2013 +0400
+++ b/jdk/src/macosx/native/sun/awt/CClipboard.m	Fri Nov 15 17:40:34 2013 +0400
@@ -91,7 +91,9 @@
 {
     if (sClipboard == nil) {
         sClipboard = [[CClipboard alloc] init];
-        [[NSNotificationCenter defaultCenter] addObserver:sClipboard selector: @selector(checkPasteboard:) name: NSApplicationDidBecomeActiveNotification object: nil];
+        [[NSNotificationCenter defaultCenter] addObserver:sClipboard selector: @selector(checkPasteboard:)
+                                                     name: NSApplicationDidBecomeActiveNotification
+                                                   object: nil];
     }
 
     return sClipboard;
@@ -110,8 +112,6 @@
 
 - (void) javaDeclareTypes:(NSArray *)inTypes withOwner:(jobject)inClipboard jniEnv:(JNIEnv *)inEnv {
 
-    //NSLog(@"CClipboard javaDeclareTypes %@ withOwner", inTypes);
-
     @synchronized(self) {
         if (inClipboard != NULL) {
             if (fClipboardOwner != NULL) {
@@ -126,8 +126,6 @@
 - (void) _nativeDeclareTypes:(NSArray *)inTypes {
     AWT_ASSERT_APPKIT_THREAD;
 
-    //NSLog(@"CClipboard _nativeDeclareTypes %@ withOwner", inTypes);
-
     fChangeCount = [[NSPasteboard generalPasteboard] declareTypes:inTypes owner:self];
 }
 
@@ -136,8 +134,6 @@
 
     NSMutableArray *args = [NSMutableArray arrayWithCapacity:1];
     [ThreadUtilities performOnMainThread:@selector(_nativeGetTypes:) on:self withObject:args waitUntilDone:YES];
-
-    //NSLog(@"CClipboard getTypes returns %@", [args lastObject]);
     return [args lastObject];
 }
 
@@ -145,8 +141,6 @@
     AWT_ASSERT_APPKIT_THREAD;
 
     [args addObject:[[NSPasteboard generalPasteboard] types]];
-
-    //NSLog(@"CClipboard getTypes returns %@", [args lastObject]);
 }
 
 - (void) javaSetData:(NSData *)inData forType:(NSString *) inFormat {
@@ -154,25 +148,18 @@
     CClipboardUpdate *newUpdate = [[CClipboardUpdate alloc] initWithData:inData withFormat:inFormat];
     [ThreadUtilities performOnMainThread:@selector(_nativeSetData:) on:self withObject:newUpdate waitUntilDone:YES];
     [newUpdate release];
-
-    //NSLog(@"CClipboard javaSetData forType %@", inFormat);
 }
 
 - (void) _nativeSetData:(CClipboardUpdate *)newUpdate {
     AWT_ASSERT_APPKIT_THREAD;
 
     [[NSPasteboard generalPasteboard] setData:[newUpdate data] forType:[newUpdate format]];
-
-    //NSLog(@"CClipboard _nativeSetData setData %@", [newUpdate data]);
-    //NSLog(@"CClipboard _nativeSetData forType %@", [newUpdate format]);
 }
 
 - (NSData *) javaGetDataForType:(NSString *) inFormat {
 
     NSMutableArray *args = [NSMutableArray arrayWithObject:inFormat];
     [ThreadUtilities performOnMainThread:@selector(_nativeGetDataForType:) on:self withObject:args waitUntilDone:YES];
-
-    //NSLog(@"CClipboard javaGetDataForType %@ returns an NSData", inFormat);
     return [args lastObject];
 }
 
@@ -183,15 +170,11 @@
 
     if (returnValue) [args replaceObjectAtIndex:0 withObject:returnValue];
     else [args removeLastObject];
-
-    //NSLog(@"CClipboard _nativeGetDataForType");
 }
 
 - (void) checkPasteboard:(id)application {
     AWT_ASSERT_APPKIT_THREAD;
     
-    //NSLog(@"CClipboard checkPasteboard oldCount %d newCount %d newTypes %@", fChangeCount, [[NSPasteboard generalPasteboard] changeCount], [[NSPasteboard generalPasteboard] types]);
-    
     // This is called via NSApplicationDidBecomeActiveNotification.
     
     // If the change count on the general pasteboard is different than when we set it
@@ -199,27 +182,23 @@
     NSInteger newChangeCount = [[NSPasteboard generalPasteboard] changeCount];
     
     if (fChangeCount != newChangeCount) {
-        fChangeCount = newChangeCount;
-        
-        [self pasteboardChangedOwner:[NSPasteboard generalPasteboard]];
-    }
-}
+        fChangeCount = newChangeCount;    
 
-- (void)pasteboardChangedOwner:(NSPasteboard *)sender; {
-    AWT_ASSERT_APPKIT_THREAD;
+        // Notify that the content might be changed
+        static JNF_CLASS_CACHE(jc_CClipboard, "sun/lwawt/macosx/CClipboard");
+        static JNF_STATIC_MEMBER_CACHE(jm_contentChanged, jc_CClipboard, "notifyChanged", "()V");
+        JNIEnv *env = [ThreadUtilities getJNIEnv];
+        JNFCallStaticVoidMethod(env, jm_contentChanged);
 
-    static JNF_CLASS_CACHE(jc_CClipboard, "sun/lwawt/macosx/CClipboard");
-    static JNF_MEMBER_CACHE(jm_lostOwnership, jc_CClipboard, "lostSelectionOwnershipImpl", "()V");
-
-    //NSLog(@"CClipboard pasteboardChangedOwner");
-
-    // If we have a Java pasteboard owner, tell it that it doesn't own the pasteboard anymore.
-    @synchronized(self) {
-        if (fClipboardOwner) {
-            JNIEnv *env = [ThreadUtilities getJNIEnv];
-            JNFCallVoidMethod(env, fClipboardOwner, jm_lostOwnership); // AWT_THREADING Safe (event)
-            JNFDeleteGlobalRef(env, fClipboardOwner);
-            fClipboardOwner = NULL;
+        // If we have a Java pasteboard owner, tell it that it doesn't own the pasteboard anymore.
+        static JNF_MEMBER_CACHE(jm_lostOwnership, jc_CClipboard, "notifyLostOwnership", "()V");
+        @synchronized(self) {
+            if (fClipboardOwner) {
+                JNIEnv *env = [ThreadUtilities getJNIEnv];
+                JNFCallVoidMethod(env, fClipboardOwner, jm_lostOwnership); // AWT_THREADING Safe (event)
+                JNFDeleteGlobalRef(env, fClipboardOwner);
+                fClipboardOwner = NULL;
+            }
         }
     }
 }
@@ -265,9 +244,6 @@
     }
 
 JNF_COCOA_ENTER(env);
-
-    //NSLog(@"Java_sun_lwawt_macosx_CClipboard_setData");
-
     jint nBytes = (*env)->GetArrayLength(env, inBytes);
     jbyte *rawBytes = (*env)->GetPrimitiveArrayCritical(env, inBytes, NULL);
     NSData *bytesAsData = [NSData dataWithBytes:rawBytes length:nBytes];
@@ -288,8 +264,6 @@
     jlongArray returnValue = NULL;
 JNF_COCOA_ENTER(env);
 
-    //NSLog(@"Java_sun_lwawt_macosx_CClipboard_getClipboardFormats");
-
     NSArray *dataTypes = [[CClipboard sharedClipboard] javaGetTypes];
     NSUInteger nFormats = [dataTypes count];
     NSUInteger knownFormats = 0;
@@ -346,8 +320,6 @@
     // a byte array back to Java.  CDataTransferer will do that if necessary.
 JNF_COCOA_ENTER(env);
 
-    //NSLog(@"Java_sun_lwawt_macosx_CClipboard_getClipboardData");
-
     NSString *formatAsString = formatForIndex(format);
     NSData *clipData = [[CClipboard sharedClipboard] javaGetDataForType:formatAsString];