Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native_NOTIOS/sun/awt/ApplicationDelegate.m
38829 views
/*1* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#import "ApplicationDelegate.h"2627#import "com_apple_eawt_Application.h"28#import "com_apple_eawt__AppDockIconHandler.h"29#import "com_apple_eawt__AppEventHandler.h"30#import "com_apple_eawt__AppMenuBarHandler.h"31#import "com_apple_eawt__AppMenuBarHandler.h"32#import "com_apple_eawt__AppMiscHandlers.h"3334#import <JavaNativeFoundation/JavaNativeFoundation.h>3536#import "CPopupMenu.h"37#import "ThreadUtilities.h"38#import "NSApplicationAWT.h"394041#pragma mark App Menu helpers4243// The following is a AWT convention?44#define PREFERENCES_TAG 424546static void addMenuItem(NSMenuItem* menuItem, NSInteger index) {47AWT_ASSERT_APPKIT_THREAD;4849NSMenu *menuBar = [[NSApplication sharedApplication] mainMenu];50NSMenu *appMenu = [[menuBar itemAtIndex:0] submenu];5152[appMenu insertItem:menuItem atIndex:index];53[appMenu insertItem:[NSMenuItem separatorItem] atIndex:index + 1]; // Add the following separator54}5556static void removeMenuItem(NSMenuItem* menuItem) {57AWT_ASSERT_APPKIT_THREAD;5859NSMenu *menuBar = [[NSApplication sharedApplication] mainMenu];60NSMenu *appMenu = [[menuBar itemAtIndex:0] submenu];6162NSInteger index = [appMenu indexOfItem:menuItem];63if (index < 0) return; // something went wrong6465[appMenu removeItemAtIndex:index + 1]; // Get the following separator66[appMenu removeItem:menuItem];67}6869@interface NSBundle (EAWTOverrides)70- (BOOL)_hasEAWTOverride:(NSString *)key;71@end727374@implementation NSBundle (EAWTOverrides)7576- (BOOL)_hasEAWTOverride:(NSString *)key {77return [[[[self objectForInfoDictionaryKey:@"Java"] objectForKey:@"EAWTOverride"] objectForKey:key] boolValue];78}7980@end818283// used by JavaRuntimeSupport.framework's [JRSAppKitAWT awtAppDelegate]84// to expose our app delegate to the SWT or other apps that have knoledge85// of Java's AWT and want to install their own app delegate that will delegate86// to the AWT for some operations8788@interface JavaAWTAppDelegateLoader : NSObject { }89@end9091@implementation JavaAWTAppDelegateLoader92+ (ApplicationDelegate *) awtAppDelegate {93return [ApplicationDelegate sharedDelegate];94}95@end969798@implementation ApplicationDelegate99100@synthesize fPreferencesMenu;101@synthesize fAboutMenu;102103@synthesize fDockMenu;104@synthesize fDefaultMenuBar;105106107+ (ApplicationDelegate *)sharedDelegate {108static ApplicationDelegate *sApplicationDelegate = nil;109static BOOL checked = NO;110111if (sApplicationDelegate != nil) return sApplicationDelegate;112if (checked) return nil;113114AWT_ASSERT_APPKIT_THREAD;115116// don't install the EAWT delegate if another kind of NSApplication is installed, like say, Safari117BOOL shouldInstall = NO;118if (NSApp != nil) {119if ([NSApp isMemberOfClass:[NSApplication class]]) shouldInstall = YES;120if ([NSApp isKindOfClass:[NSApplicationAWT class]]) shouldInstall = YES;121}122checked = YES;123if (!shouldInstall) return nil;124125sApplicationDelegate = [[ApplicationDelegate alloc] init];126return sApplicationDelegate;127}128129- (void)_updatePreferencesMenu:(BOOL)prefsAvailable enabled:(BOOL)prefsEnabled {130AWT_ASSERT_APPKIT_THREAD;131132if (prefsAvailable) {133// Make sure Prefs is around134if ([self.fPreferencesMenu menu] == nil) {135// Position of Prefs depends upon About availability.136NSInteger index = ([self.fAboutMenu menu] != nil) ? 2 : 0;137138addMenuItem(self.fPreferencesMenu, index);139}140141if (prefsEnabled) {142[self.fPreferencesMenu setEnabled:YES];143[self.fPreferencesMenu setTarget:self];144[self.fPreferencesMenu setAction:@selector(_preferencesMenuHandler)];145} else {146[self.fPreferencesMenu setEnabled:NO];147[self.fPreferencesMenu setTarget:nil];148[self.fPreferencesMenu setAction:nil];149}150} else {151if ([self.fPreferencesMenu menu] == nil) return;152153// Remove the preferences item154removeMenuItem(self.fPreferencesMenu);155}156}157158- (void)_updateAboutMenu:(BOOL)aboutAvailable enabled:(BOOL)aboutEnabled {159AWT_ASSERT_APPKIT_THREAD;160161if (aboutAvailable) {162// Make sure About is around163if ([self.fAboutMenu menu] == nil) {164addMenuItem(self.fAboutMenu, 0);165}166167if (aboutEnabled) {168[self.fAboutMenu setEnabled:YES];169[self.fAboutMenu setTarget:self];170[self.fAboutMenu setAction:@selector(_aboutMenuHandler)];171} else {172[self.fAboutMenu setEnabled:NO];173[self.fAboutMenu setTarget:nil];174[self.fAboutMenu setAction:nil];175}176} else {177if ([self.fAboutMenu menu] == nil) return;178179// Remove About item.180removeMenuItem(self.fAboutMenu);181}182}183184- (id) init {185AWT_ASSERT_APPKIT_THREAD;186187self = [super init];188if (!self) return self;189190// Prep for about and preferences menu191BOOL usingDefaultNib = YES;192if ([NSApp isKindOfClass:[NSApplicationAWT class]]) {193usingDefaultNib = [NSApp usingDefaultNib];194}195if (!usingDefaultNib) return self;196197NSMenu *menuBar = [[NSApplication sharedApplication] mainMenu];198NSMenu *appMenu = [[menuBar itemAtIndex:0] submenu];199200self.fPreferencesMenu = (NSMenuItem*)[appMenu itemWithTag:PREFERENCES_TAG];201self.fAboutMenu = (NSMenuItem*)[appMenu itemAtIndex:0];202203// If the java application has a bundle with an Info.plist file with204// a CFBundleDocumentTypes entry, then it is set up to handle Open Doc205// and Print Doc commands for these files. Therefore java AWT will206// cache Open Doc and Print Doc events that are sent prior to a207// listener being installed by the client java application.208NSBundle *bundle = [NSBundle mainBundle];209fHandlesDocumentTypes = [bundle objectForInfoDictionaryKey:@"CFBundleDocumentTypes"] != nil || [bundle _hasEAWTOverride:@"DocumentHandler"];210fHandlesURLTypes = [bundle objectForInfoDictionaryKey:@"CFBundleURLTypes"] != nil || [bundle _hasEAWTOverride:@"URLHandler"];211if (fHandlesURLTypes) {212[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self213andSelector:@selector(_handleOpenURLEvent:withReplyEvent:)214forEventClass:kInternetEventClass215andEventID:kAEGetURL];216}217218// By HIG, Preferences are not available unless there is a handler. By default in Mac OS X,219// there is not a handler, but it is in the nib file for convenience.220removeMenuItem(self.fPreferencesMenu);221222[self _updateAboutMenu:YES enabled:YES];223224// Now that the AppKit objects are known and set up, initialize the model data225BOOL aboutAvailable = ([self.fAboutMenu menu] != nil);226BOOL aboutEnabled = (aboutAvailable && [self.fAboutMenu isEnabled] && ([self.fAboutMenu target] != nil));227228BOOL prefsAvailable = ([self.fPreferencesMenu menu] != nil);229BOOL prefsEnabled = (prefsAvailable && [self.fPreferencesMenu isEnabled] && ([self.fPreferencesMenu target] != nil));230231JNIEnv *env = [ThreadUtilities getJNIEnv];232static JNF_CLASS_CACHE(sjc_AppMenuBarHandler, "com/apple/eawt/_AppMenuBarHandler");233static JNF_STATIC_MEMBER_CACHE(sjm_initMenuStates, sjc_AppMenuBarHandler, "initMenuStates", "(ZZZZ)V");234JNFCallStaticVoidMethod(env, sjm_initMenuStates, aboutAvailable, aboutEnabled, prefsAvailable, prefsEnabled);235236// register for the finish launching and system power off notifications by default237NSNotificationCenter *ctr = [NSNotificationCenter defaultCenter];238Class clz = [ApplicationDelegate class];239[ctr addObserver:clz selector:@selector(_willFinishLaunching) name:NSApplicationWillFinishLaunchingNotification object:nil];240[ctr addObserver:clz selector:@selector(_systemWillPowerOff) name:NSWorkspaceWillPowerOffNotification object:nil];241[ctr addObserver:clz selector:@selector(_appDidActivate) name:NSApplicationDidBecomeActiveNotification object:nil];242[ctr addObserver:clz selector:@selector(_appDidDeactivate) name:NSApplicationDidResignActiveNotification object:nil];243[ctr addObserver:clz selector:@selector(_appDidHide) name:NSApplicationDidHideNotification object:nil];244[ctr addObserver:clz selector:@selector(_appDidUnhide) name:NSApplicationDidUnhideNotification object:nil];245246return self;247}248249- (void)dealloc {250self.fPreferencesMenu = nil;251self.fAboutMenu = nil;252self.fDockMenu = nil;253self.fDefaultMenuBar = nil;254255[super dealloc];256}257258#pragma mark Callbacks from AppKit259260static JNF_CLASS_CACHE(sjc_AppEventHandler, "com/apple/eawt/_AppEventHandler");261262- (void)_handleOpenURLEvent:(NSAppleEventDescriptor *)openURLEvent withReplyEvent:(NSAppleEventDescriptor *)replyEvent {263AWT_ASSERT_APPKIT_THREAD;264if (!fHandlesURLTypes) return;265266NSString *url = [[openURLEvent paramDescriptorForKeyword:keyDirectObject] stringValue];267268//fprintf(stderr,"jm_handleOpenURL\n");269JNIEnv *env = [ThreadUtilities getJNIEnv];270jstring jURL = JNFNSToJavaString(env, url);271static JNF_STATIC_MEMBER_CACHE(jm_handleOpenURI, sjc_AppEventHandler, "handleOpenURI", "(Ljava/lang/String;)V");272JNFCallStaticVoidMethod(env, jm_handleOpenURI, jURL); // AWT_THREADING Safe (event)273(*env)->DeleteLocalRef(env, jURL);274275[replyEvent insertDescriptor:[NSAppleEventDescriptor nullDescriptor] atIndex:0];276}277278// Helper for both open file and print file methods279// Creates a Java list of files from a native list of files280- (jobject)_createFilePathArrayFrom:(NSArray *)filenames withEnv:(JNIEnv *)env {281static JNF_CLASS_CACHE(sjc_ArrayList, "java/util/ArrayList");282static JNF_CTOR_CACHE(jm_ArrayList_ctor, sjc_ArrayList, "(I)V");283static JNF_MEMBER_CACHE(jm_ArrayList_add, sjc_ArrayList, "add", "(Ljava/lang/Object;)Z");284285jobject jFileNamesArray = JNFNewObject(env, jm_ArrayList_ctor, (jint)[filenames count]); // AWT_THREADING Safe (known object)286for (NSString *filename in filenames) {287jstring jFileName = JNFNormalizedJavaStringForPath(env, filename);288JNFCallVoidMethod(env, jFileNamesArray, jm_ArrayList_add, jFileName);289}290291return jFileNamesArray;292}293294// Open file handler295- (void)application:(NSApplication *)theApplication openFiles:(NSArray *)fileNames {296AWT_ASSERT_APPKIT_THREAD;297if (!fHandlesDocumentTypes) {298[theApplication replyToOpenOrPrint:NSApplicationDelegateReplyCancel];299return;300}301302//fprintf(stderr,"jm_handleOpenFile\n");303JNIEnv *env = [ThreadUtilities getJNIEnv];304305// if these files were opened from a Spotlight query, try to get the search text from the current AppleEvent306NSAppleEventDescriptor *currentEvent = [[NSAppleEventManager sharedAppleEventManager] currentAppleEvent];307NSString *searchString = [[currentEvent paramDescriptorForKeyword:keyAESearchText] stringValue];308jstring jSearchString = JNFNSToJavaString(env, searchString);309310// convert the file names array311jobject jFileNamesArray = [self _createFilePathArrayFrom:fileNames withEnv:env];312313static JNF_STATIC_MEMBER_CACHE(jm_handleOpenFiles, sjc_AppEventHandler, "handleOpenFiles", "(Ljava/util/List;Ljava/lang/String;)V");314JNFCallStaticVoidMethod(env, jm_handleOpenFiles, jFileNamesArray, jSearchString);315(*env)->DeleteLocalRef(env, jFileNamesArray);316(*env)->DeleteLocalRef(env, jSearchString);317318[theApplication replyToOpenOrPrint:NSApplicationDelegateReplySuccess];319}320321// Print handler322- (NSApplicationPrintReply)application:(NSApplication *)application printFiles:(NSArray *)fileNames withSettings:(NSDictionary *)printSettings showPrintPanels:(BOOL)showPrintPanels {323AWT_ASSERT_APPKIT_THREAD;324if (!fHandlesDocumentTypes) return NSPrintingCancelled;325326//fprintf(stderr,"jm_handlePrintFile\n");327JNIEnv *env = [ThreadUtilities getJNIEnv];328jobject jFileNamesArray = [self _createFilePathArrayFrom:fileNames withEnv:env];329static JNF_STATIC_MEMBER_CACHE(jm_handlePrintFile, sjc_AppEventHandler, "handlePrintFiles", "(Ljava/util/List;)V");330JNFCallStaticVoidMethod(env, jm_handlePrintFile, jFileNamesArray); // AWT_THREADING Safe (event)331(*env)->DeleteLocalRef(env, jFileNamesArray);332333return NSPrintingSuccess;334}335336// Open app handler, registered in -init337+ (void)_notifyJava:(jint)notificationType {338AWT_ASSERT_APPKIT_THREAD;339340//fprintf(stderr,"jm_handleOpenApplication\n");341JNIEnv *env = [ThreadUtilities getJNIEnv];342static JNF_STATIC_MEMBER_CACHE(jm_handleNativeNotification, sjc_AppEventHandler, "handleNativeNotification", "(I)V");343JNFCallStaticVoidMethod(env, jm_handleNativeNotification, notificationType); // AWT_THREADING Safe (event)344}345346// About menu handler347- (void)_aboutMenuHandler {348[ApplicationDelegate _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_ABOUT];349}350351// Preferences handler352- (void)_preferencesMenuHandler {353[ApplicationDelegate _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_PREFS];354}355356// Open app handler, registered in -init357+ (void)_willFinishLaunching {358[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_OPEN_APP];359}360361// ReOpen app handler362- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag {363[ApplicationDelegate _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_REOPEN_APP];364return YES;365}366367// Quit handler368- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app {369[ApplicationDelegate _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_QUIT];370return NSTerminateLater;371}372373+ (void)_systemWillPowerOff {374[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_SHUTDOWN];375}376377+ (void)_appDidActivate {378[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_ACTIVE_APP_GAINED];379}380381+ (void)_appDidDeactivate {382[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_ACTIVE_APP_LOST];383}384385+ (void)_appDidHide {386[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_APP_HIDDEN];387}388389+ (void)_appDidUnhide {390[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_APP_SHOWN];391}392393+ (void)_sessionDidActivate {394[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_USER_SESSION_ACTIVE];395}396397+ (void)_sessionDidDeactivate {398[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_USER_SESSION_INACTIVE];399}400401+ (void)_screenDidSleep {402[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_SCREEN_SLEEP];403}404405+ (void)_screenDidWake {406[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_SCREEN_WAKE];407}408409+ (void)_systemDidSleep {410[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_SYSTEM_SLEEP];411}412413+ (void)_systemDidWake {414[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_SYSTEM_WAKE];415}416417+ (void)_registerForNotification:(NSNumber *)notificationTypeNum {418NSNotificationCenter *ctr = [[NSWorkspace sharedWorkspace] notificationCenter];419Class clz = [ApplicationDelegate class];420421jint notificationType = [notificationTypeNum intValue];422switch (notificationType) {423case com_apple_eawt__AppEventHandler_REGISTER_USER_SESSION:424[ctr addObserver:clz selector:@selector(_sessionDidActivate) name:NSWorkspaceSessionDidBecomeActiveNotification object:nil];425[ctr addObserver:clz selector:@selector(_sessionDidDeactivate) name:NSWorkspaceSessionDidResignActiveNotification object:nil];426break;427case com_apple_eawt__AppEventHandler_REGISTER_SCREEN_SLEEP:428[ctr addObserver:clz selector:@selector(_screenDidSleep) name:NSWorkspaceScreensDidSleepNotification object:nil];429[ctr addObserver:clz selector:@selector(_screenDidWake) name:NSWorkspaceScreensDidWakeNotification object:nil];430break;431case com_apple_eawt__AppEventHandler_REGISTER_SYSTEM_SLEEP:432[ctr addObserver:clz selector:@selector(_systemDidSleep) name:NSWorkspaceWillSleepNotification object:nil];433[ctr addObserver:clz selector:@selector(_systemDidWake) name:NSWorkspaceDidWakeNotification object:nil];434break;435default:436NSLog(@"EAWT attempting to register for unknown notification: %d", (int)notificationType);437break;438}439}440441// Retrieves the menu to be attached to the Dock icon (AppKit callback)442- (NSMenu *)applicationDockMenu:(NSApplication *)sender {443AWT_ASSERT_APPKIT_THREAD;444return self.fDockMenu;445}446447- (CMenuBar *)defaultMenuBar {448return [[self.fDefaultMenuBar retain] autorelease];449}450451452#pragma mark Helpers called on the main thread from Java453454// Sets a new NSImageView on the Dock tile455+ (void)_setDockIconImage:(NSImage *)image {456AWT_ASSERT_APPKIT_THREAD;457458NSDockTile *dockTile = [NSApp dockTile];459if (image == nil) {460[dockTile setContentView:nil];461return;462}463464// setup an image view for the dock tile465NSRect frame = NSMakeRect(0, 0, dockTile.size.width, dockTile.size.height);466NSImageView *dockImageView = [[NSImageView alloc] initWithFrame: frame];467[dockImageView setImageScaling:NSImageScaleProportionallyUpOrDown];468[dockImageView setImage:image];469470// add it to the NSDockTile471[dockTile setContentView: dockImageView];472[dockTile display];473474[dockImageView release];475}476477// Obtains the image of the Dock icon, either manually set, a drawn copy, or the default NSApplicationIcon478+ (NSImage *)_dockIconImage {479AWT_ASSERT_APPKIT_THREAD;480481NSDockTile *dockTile = [NSApp dockTile];482NSView *view = [dockTile contentView];483484if ([view isKindOfClass:[NSImageView class]]) {485NSImage *img = [((NSImageView *)view) image];486if (img) return img;487}488489if (view == nil) {490return [NSImage imageNamed:@"NSApplicationIcon"];491}492493NSRect frame = [view frame];494NSImage *image = [[NSImage alloc] initWithSize:frame.size];495[image lockFocus];496[view drawRect:frame];497[image unlockFocus];498[image autorelease];499return image;500}501502@end503504505#pragma mark Native JNI calls506507/*508* Class: com_apple_eawt_Application509* Method: nativeInitializeApplicationDelegate510* Signature: ()V511*/512JNIEXPORT void JNICALL Java_com_apple_eawt_Application_nativeInitializeApplicationDelegate513(JNIEnv *env, jclass clz)514{515JNF_COCOA_ENTER(env);516// Force initialization to happen on AppKit thread!517[ThreadUtilities performOnMainThreadWaiting:NO block:^(){518[ApplicationDelegate sharedDelegate];519}];520JNF_COCOA_EXIT(env);521}522523/*524* Class: com_apple_eawt__AppEventHandler525* Method: nativeOpenCocoaAboutWindow526* Signature: ()V527*/528JNIEXPORT void JNICALL Java_com_apple_eawt__1AppEventHandler_nativeOpenCocoaAboutWindow529(JNIEnv *env, jclass clz)530{531JNF_COCOA_ENTER(env);532533[ThreadUtilities performOnMainThreadWaiting:NO block:^(){534[NSApp orderFrontStandardAboutPanel:nil];535}];536537JNF_COCOA_EXIT(env);538}539540/*541* Class: com_apple_eawt__AppEventHandler542* Method: nativeReplyToAppShouldTerminate543* Signature: (Z)V544*/545JNIEXPORT void JNICALL Java_com_apple_eawt__1AppEventHandler_nativeReplyToAppShouldTerminate546(JNIEnv *env, jclass clz, jboolean doTerminate)547{548JNF_COCOA_ENTER(env);549550[ThreadUtilities performOnMainThreadWaiting:NO block:^(){551[NSApp replyToApplicationShouldTerminate:doTerminate];552}];553554JNF_COCOA_EXIT(env);555}556557/*558* Class: com_apple_eawt__AppEventHandler559* Method: nativeRegisterForNotification560* Signature: (I)V561*/562JNIEXPORT void JNICALL Java_com_apple_eawt__1AppEventHandler_nativeRegisterForNotification563(JNIEnv *env, jclass clz, jint notificationType)564{565JNF_COCOA_ENTER(env);566[ThreadUtilities performOnMainThread:@selector(_registerForNotification:)567on:[ApplicationDelegate class]568withObject:[NSNumber numberWithInt:notificationType]569waitUntilDone:NO]; // AWT_THREADING Safe (non-blocking)570JNF_COCOA_EXIT(env);571}572573/*574* Class: com_apple_eawt__AppDockIconHandler575* Method: nativeSetDockMenu576* Signature: (J)V577*/578JNIEXPORT void JNICALL Java_com_apple_eawt__1AppDockIconHandler_nativeSetDockMenu579(JNIEnv *env, jclass clz, jlong nsMenuPtr)580{581JNF_COCOA_ENTER(env);582583NSMenu *menu = (NSMenu *)jlong_to_ptr(nsMenuPtr);584[ThreadUtilities performOnMainThreadWaiting:YES block:^(){585[ApplicationDelegate sharedDelegate].fDockMenu = menu;586}];587588JNF_COCOA_EXIT(env);589}590591/*592* Class: com_apple_eawt__AppDockIconHandler593* Method: nativeSetDockIconImage594* Signature: (J)V595*/596JNIEXPORT void JNICALL Java_com_apple_eawt__1AppDockIconHandler_nativeSetDockIconImage597(JNIEnv *env, jclass clz, jlong nsImagePtr)598{599JNF_COCOA_ENTER(env);600601NSImage *_image = (NSImage *)jlong_to_ptr(nsImagePtr);602[ThreadUtilities performOnMainThread:@selector(_setDockIconImage:)603on:[ApplicationDelegate class]604withObject:_image605waitUntilDone:NO];606607JNF_COCOA_EXIT(env);608}609610/*611* Class: com_apple_eawt__AppDockIconHandler612* Method: nativeGetDockIconImage613* Signature: ()J614*/615JNIEXPORT jlong JNICALL Java_com_apple_eawt__1AppDockIconHandler_nativeGetDockIconImage616(JNIEnv *env, jclass clz)617{618__block NSImage *image = nil;619620JNF_COCOA_ENTER(env);621622[ThreadUtilities performOnMainThreadWaiting:YES block:^(){623image = [[ApplicationDelegate _dockIconImage] retain];624}];625626JNF_COCOA_EXIT(env);627628return ptr_to_jlong(image);629}630631/*632* Class: com_apple_eawt__AppDockIconHandler633* Method: nativeSetDockIconBadge634* Signature: (Ljava/lang/String;)V635*/636JNIEXPORT void JNICALL Java_com_apple_eawt__1AppDockIconHandler_nativeSetDockIconBadge637(JNIEnv *env, jclass clz, jstring badge)638{639JNF_COCOA_ENTER(env);640641NSString *badgeString = JNFJavaToNSString(env, badge);642[ThreadUtilities performOnMainThreadWaiting:NO block:^(){643NSDockTile *dockTile = [NSApp dockTile];644[dockTile setBadgeLabel:badgeString];645[dockTile display];646}];647648JNF_COCOA_EXIT(env);649}650651/*652* Class: com_apple_eawt__AppMiscHandlers653* Method: nativeRequestActivation654* Signature: (Z)V655*/656JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMiscHandlers_nativeRequestActivation657(JNIEnv *env, jclass clz, jboolean allWindows)658{659JNF_COCOA_ENTER(env);660661[ThreadUtilities performOnMainThreadWaiting:NO block:^(){662NSApplicationActivationOptions options = allWindows ? NSApplicationActivateAllWindows : 0;663options |= NSApplicationActivateIgnoringOtherApps; // without this, nothing happens!664[[NSRunningApplication currentApplication] activateWithOptions:options];665}];666667JNF_COCOA_EXIT(env);668}669670/*671* Class: com_apple_eawt__AppMiscHandlers672* Method: nativeRequestUserAttention673* Signature: (Z)V674*/675JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMiscHandlers_nativeRequestUserAttention676(JNIEnv *env, jclass clz, jboolean critical)677{678JNF_COCOA_ENTER(env);679680[ThreadUtilities performOnMainThreadWaiting:NO block:^(){681[NSApp requestUserAttention:critical ? NSCriticalRequest : NSInformationalRequest];682}];683684JNF_COCOA_EXIT(env);685}686687/*688* Class: com_apple_eawt__AppMiscHandlers689* Method: nativeOpenHelpViewer690* Signature: ()V691*/692JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMiscHandlers_nativeOpenHelpViewer693(JNIEnv *env, jclass clz)694{695JNF_COCOA_ENTER(env);696697[ThreadUtilities performOnMainThread:@selector(showHelp:)698on:NSApp699withObject:nil700waitUntilDone:NO];701702JNF_COCOA_EXIT(env);703}704705/*706* Class: com_apple_eawt__AppMiscHandlers707* Method: nativeEnableSuddenTermination708* Signature: ()V709*/710JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMiscHandlers_nativeEnableSuddenTermination711(JNIEnv *env, jclass clz)712{713JNF_COCOA_ENTER(env);714715[[NSProcessInfo processInfo] enableSuddenTermination]; // Foundation thread-safe716717JNF_COCOA_EXIT(env);718}719720/*721* Class: com_apple_eawt__AppMiscHandlers722* Method: nativeDisableSuddenTermination723* Signature: ()V724*/725JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMiscHandlers_nativeDisableSuddenTermination726(JNIEnv *env, jclass clz)727{728JNF_COCOA_ENTER(env);729730[[NSProcessInfo processInfo] disableSuddenTermination]; // Foundation thread-safe731732JNF_COCOA_EXIT(env);733}734735/*736* Class: com_apple_eawt__AppMenuBarHandler737* Method: nativeSetMenuState738* Signature: (IZZ)V739*/740JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMenuBarHandler_nativeSetMenuState741(JNIEnv *env, jclass clz, jint menuID, jboolean visible, jboolean enabled)742{743JNF_COCOA_ENTER(env);744745[ThreadUtilities performOnMainThreadWaiting:NO block:^(){746ApplicationDelegate *delegate = [ApplicationDelegate sharedDelegate];747switch (menuID) {748case com_apple_eawt__AppMenuBarHandler_MENU_ABOUT:749[delegate _updateAboutMenu:visible enabled:enabled];750break;751case com_apple_eawt__AppMenuBarHandler_MENU_PREFS:752[delegate _updatePreferencesMenu:visible enabled:enabled];753break;754}755}];756757JNF_COCOA_EXIT(env);758}759760/*761* Class: com_apple_eawt__AppMenuBarHandler762* Method: nativeSetDefaultMenuBar763* Signature: (J)V764*/765JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMenuBarHandler_nativeSetDefaultMenuBar766(JNIEnv *env, jclass clz, jlong cMenuBarPtr)767{768JNF_COCOA_ENTER(env);769770CMenuBar *menu = (CMenuBar *)jlong_to_ptr(cMenuBarPtr);771[ThreadUtilities performOnMainThreadWaiting:NO block:^(){772[ApplicationDelegate sharedDelegate].fDefaultMenuBar = menu;773}];774775JNF_COCOA_EXIT(env);776}777778779