< prev index next >

src/java.desktop/macosx/native/libosxui/ScreenMenu.m

Print this page
rev 54094 : 8257853: Remove dependencies on JNF's JNI utility functions in AWT and 2D code
rev 54096 : 8259651: [macOS] Replace JNF_COCOA_ENTER/EXIT macros
rev 54097 : 8259869: [macOS] Remove desktop module dependencies on JNF Reference APIs
rev 54098 : 8260616: Removing remaining JNF dependencies in the java.desktop module
8259729: Missed JNFInstanceOf -> IsInstanceOf conversion

@@ -29,17 +29,19 @@
 #import "java_awt_Event.h"
 #import "java_awt_event_KeyEvent.h"
 #import "java_awt_event_InputEvent.h"
 #import "java_awt_event_MouseEvent.h"
 
-#import <JavaNativeFoundation/JavaNativeFoundation.h>
 #import <JavaRuntimeSupport/JavaRuntimeSupport.h>
 
 #import "ThreadUtilities.h"
 #import "CMenuBar.h"
+#import "JNIUtilities.h"
 
-static JNF_CLASS_CACHE(sjc_ScreenMenu, "com/apple/laf/ScreenMenu");
+static jclass sjc_ScreenMenu = NULL;
+#define GET_SCREENMENU_CLASS() \
+     GET_CLASS(sjc_ScreenMenu, "com/apple/laf/ScreenMenu");
 
 static jint ns2awtModifiers(NSUInteger keyMods) {
     jint result = 0;
     if (keyMods & NSShiftKeyMask)        result |= java_awt_Event_SHIFT_MASK;
     if (keyMods & NSControlKeyMask)        result |= java_awt_Event_CTRL_MASK;

@@ -60,95 +62,111 @@
 
 @interface NativeToJavaDelegate : NSObject <JRSMenuDelegate, NSMenuDelegate>
 {
 @public
     NSMenu *nsmenu;
-    JNFJObjectWrapper *javaObjectWrapper;
+    jobject javaObject;
 }
 
 @property (nonatomic, retain) NSMenu *nsmenu;
-@property (nonatomic, retain) JNFJObjectWrapper *javaObjectWrapper;
+@property (nonatomic) jobject javaObject;
 
-- (id)initFromMenu:(NSMenu *)menu javaObj:(JNFJObjectWrapper *)obj;
+- (id)initFromMenu:(NSMenu *)menu javaObj:(jobject)obj;
 - (NSMenu*)menu;
 @end
 
 
 @implementation NativeToJavaDelegate
 
 @synthesize nsmenu;
-@synthesize javaObjectWrapper;
+@synthesize javaObject;
 
-- (id)initFromMenu:(NSMenu *)aMenu javaObj:(JNFJObjectWrapper *)obj
+- (id)initFromMenu:(NSMenu *)aMenu javaObj:(jobject)obj
 {
     self = [super init];
     if (self) {
         self.nsmenu = aMenu;
-        self.javaObjectWrapper = obj;
+        self.javaObject = obj;
     }
     return self;
 }
 
 - (NSMenu *)menu {
     return self.nsmenu;
 }
 
 - (void)menuWillOpen:(NSMenu *)menu
 {
-    if (self.javaObjectWrapper == nil) {
+    if (self.javaObject == nil) {
 #ifdef DEBUG
         NSLog(@"_javaObject is NULL: (%s - %s : %d)", __FILE__, __FUNCTION__, __LINE__);
 #endif
         return;
     }
 
     JNIEnv *env = [ThreadUtilities getJNIEnv];
-JNF_COCOA_ENTER(env);
+JNI_COCOA_ENTER(env);
     //NSLog(@"menuWillOpen %@", [menu title]);
-    static JNF_MEMBER_CACHE(jm_ScreenMenu_invokeOpenLater, sjc_ScreenMenu, "invokeOpenLater", "()V");
-    JNFCallVoidMethod(env, [self.javaObjectWrapper jObject], jm_ScreenMenu_invokeOpenLater); // AWT_THREADING Safe (AWTRunLoopMode)
-JNF_COCOA_EXIT(env);
+    GET_SCREENMENU_CLASS();
+    DECLARE_METHOD(jm_ScreenMenu_invokeOpenLater, sjc_ScreenMenu, "invokeOpenLater", "()V");
+    (*env)->CallVoidMethod(env, self.javaObject, jm_ScreenMenu_invokeOpenLater);
+    CHECK_EXCEPTION();
+JNI_COCOA_EXIT(env);
 
 }
 
 - (void)menuDidClose:(NSMenu *)menu
 {
-    if (self.javaObjectWrapper == nil) {
+    if (self.javaObject == nil) {
 #ifdef DEBUG
         NSLog(@"_javaObject is NULL: (%s - %s : %d)", __FILE__, __FUNCTION__, __LINE__);
 #endif
         return;
     }
 
     JNIEnv *env = [ThreadUtilities getJNIEnv];
-JNF_COCOA_ENTER(env);
+JNI_COCOA_ENTER(env);
     //NSLog(@"menuDidClose %@", [menu title]);
-    static JNF_MEMBER_CACHE(jm_ScreenMenu_invokeMenuClosing, sjc_ScreenMenu, "invokeMenuClosing", "()V");
-    JNFCallVoidMethod(env, [self.javaObjectWrapper jObject], jm_ScreenMenu_invokeMenuClosing); // AWT_THREADING Safe (AWTRunLoopMode)
-JNF_COCOA_EXIT(env);
+    GET_SCREENMENU_CLASS();
+    DECLARE_METHOD(jm_ScreenMenu_invokeMenuClosing, sjc_ScreenMenu, "invokeMenuClosing", "()V");
+    (*env)->CallVoidMethod(env, self.javaObject, jm_ScreenMenu_invokeMenuClosing);
+    CHECK_EXCEPTION();
+JNI_COCOA_EXIT(env);
 }
 
 
 - (void)handleJavaMenuItemTargetedAtIndex:(NSUInteger)menuIndex rect:(NSRect)rect
 {
-    if (self.javaObjectWrapper == nil) {
+    if (self.javaObject== nil) {
 #ifdef DEBUG
         NSLog(@"_javaObject is NULL: (%s - %s : %d)", __FILE__, __FUNCTION__, __LINE__);
 #endif
         return;
     }
 
     JNIEnv *env = [ThreadUtilities getJNIEnv];
-JNF_COCOA_ENTER(env);
+JNI_COCOA_ENTER(env);
     // Send that to Java so we can test which item was hit.
-    static JNF_MEMBER_CACHE(jm_ScreenMenu_updateSelectedItem, sjc_ScreenMenu, "handleItemTargeted", "(IIIII)V");
-    JNFCallVoidMethod(env, [self.javaObjectWrapper jObject], jm_ScreenMenu_updateSelectedItem, menuIndex,
-                    NSMinY(rect), NSMinX(rect), NSMaxY(rect), NSMaxX(rect)); // AWT_THREADING Safe (AWTRunLoopMode)
+    GET_SCREENMENU_CLASS();
+    DECLARE_METHOD(jm_ScreenMenu_updateSelectedItem, sjc_ScreenMenu, "handleItemTargeted", "(IIIII)V");
+    (*env)->CallVoidMethod(env, self.javaObject, jm_ScreenMenu_updateSelectedItem, menuIndex,
+                    NSMinY(rect), NSMinX(rect), NSMaxY(rect), NSMaxX(rect));
+    CHECK_EXCEPTION();
 
-JNF_COCOA_EXIT(env);
+JNI_COCOA_EXIT(env);
 }
 
+/*
+ * The input is an NSTimeInterval (a double representing seconds and fractions of seconds)
+ * 0.0 means midnight Jan 1, 2001.
+ * The output is a Java long representing time in milliseconds since midnight Jan 1st 1970.
+ * There is a Cocoa constant representing that difference : NSTimeIntervalSince1970
+ */
+static jlong NSTimeIntervalToJavaMilliseconds(NSTimeInterval interval) {
+    NSTimeInterval interval1970 = interval + NSTimeIntervalSince1970;
+    return (jlong)(interval1970 * 1000);
+}
 
 // Called from event handler callback
 - (void)handleJavaMouseEvent:(NSEvent *)event
 {
     NSInteger kind = [event type];

@@ -176,18 +194,21 @@
 
     // Convert the event modifiers into Java modifiers
     jint javaModifiers = ns2awtModifiers([event modifierFlags]) | ns2awtMouseButton([event buttonNumber]);
 
     // Get the event time
-    jlong javaWhen = JNFNSTimeIntervalToJavaMillis([event timestamp]);
+    jlong javaWhen = NSTimeIntervalToJavaMilliseconds([event timestamp]);
 
     // Call the mouse event handler, which will generate Java mouse events.
     JNIEnv *env = [ThreadUtilities getJNIEnv];
-JNF_COCOA_ENTER(env);
-    static JNF_MEMBER_CACHE(jm_ScreenMenu_handleMouseEvent, sjc_ScreenMenu, "handleMouseEvent", "(IIIIJ)V");
-    JNFCallVoidMethod(env, [self.javaObjectWrapper jObject], jm_ScreenMenu_handleMouseEvent, javaKind, javaX, javaY, javaModifiers, javaWhen); // AWT_THREADING Safe (AWTRunLoopMode)
-JNF_COCOA_EXIT(env);
+JNI_COCOA_ENTER(env);
+    GET_SCREENMENU_CLASS();
+    DECLARE_METHOD(jm_ScreenMenu_handleMouseEvent, sjc_ScreenMenu, "handleMouseEvent", "(IIIIJ)V");
+    (*env)->CallVoidMethod(env, self.javaObject, jm_ScreenMenu_handleMouseEvent,
+             javaKind, javaX, javaY, javaModifiers, javaWhen);
+    CHECK_EXCEPTION();
+JNI_COCOA_EXIT(env);
 }
 
 @end
 
 

@@ -199,27 +220,27 @@
 JNIEXPORT jlong JNICALL Java_com_apple_laf_ScreenMenu_addMenuListeners
 (JNIEnv *env, jclass clz, jobject listener, jlong nativeMenu)
 {
     NativeToJavaDelegate *delegate = nil;
 
-JNF_COCOA_ENTER(env);
+JNI_COCOA_ENTER(env);
 
-    JNFJObjectWrapper *wrapper = [JNFJObjectWrapper wrapperWithJObject:listener withEnv:env];
+    jobject listenerRef = (*env)->NewGlobalRef(env, listener);
     NSMenu *menu = jlong_to_ptr(nativeMenu);
 
-    delegate = [[[NativeToJavaDelegate alloc] initFromMenu:menu javaObj:wrapper] autorelease];
+    delegate = [[[NativeToJavaDelegate alloc] initFromMenu:menu javaObj:listenerRef] autorelease];
     CFRetain(delegate); // GC
 
-    [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^{
+    [ThreadUtilities performOnMainThreadWaiting:YES block:^{
         NSMenu *menu = delegate.nsmenu;
         if ([menu isJavaMenu]) {
             [menu setDelegate:delegate];
             [menu setJavaMenuDelegate:delegate];
         }
     }];
 
-JNF_COCOA_EXIT(env);
+JNI_COCOA_EXIT(env);
 
     return ptr_to_jlong(delegate);
 }
 
 /*

@@ -230,21 +251,23 @@
 JNIEXPORT void JNICALL Java_com_apple_laf_ScreenMenu_removeMenuListeners
 (JNIEnv *env, jclass clz, jlong fModelPtr)
 {
     if (fModelPtr == 0L) return;
 
-JNF_COCOA_ENTER(env);
+JNI_COCOA_ENTER(env);
 
     NativeToJavaDelegate *delegate = (NativeToJavaDelegate *)jlong_to_ptr(fModelPtr);
 
-    [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^{
+    [ThreadUtilities performOnMainThreadWaiting:YES block:^{
         NSMenu *menu = delegate.nsmenu;
         [menu setJavaMenuDelegate:nil];
         [menu setDelegate:nil];
         delegate.nsmenu = nil;
-        delegate.javaObjectWrapper = nil;
     }];
 
+    (*env)->DeleteGlobalRef(env, delegate.javaObject);
+    delegate.javaObject = nil;
+
     CFRelease(delegate); // GC
 
-JNF_COCOA_EXIT(env);
+JNI_COCOA_EXIT(env);
 }
< prev index next >