86 // of Java's AWT and want to install their own app delegate that will delegate
87 // to the AWT for some operations
88
89 @interface JavaAWTAppDelegateLoader : NSObject { }
90 @end
91
92 @implementation JavaAWTAppDelegateLoader
93 + (ApplicationDelegate *) awtAppDelegate {
94 return [ApplicationDelegate sharedDelegate];
95 }
96 @end
97
98
99 @implementation ApplicationDelegate
100
101 @synthesize fPreferencesMenu;
102 @synthesize fAboutMenu;
103
104 @synthesize fDockMenu;
105 @synthesize fDefaultMenuBar;
106
107
108 + (ApplicationDelegate *)sharedDelegate {
109 static ApplicationDelegate *sApplicationDelegate = nil;
110 static BOOL checked = NO;
111
112 if (sApplicationDelegate != nil) return sApplicationDelegate;
113 if (checked) return nil;
114
115 AWT_ASSERT_APPKIT_THREAD;
116
117 // don't install the EAWT delegate if another kind of NSApplication is installed, like say, Safari
118 BOOL shouldInstall = NO;
119 if (NSApp != nil) {
120 if ([NSApp isMemberOfClass:[NSApplication class]]) shouldInstall = YES;
121 if ([NSApp isKindOfClass:[NSApplicationAWT class]]) shouldInstall = YES;
122 }
123 checked = YES;
124 if (!shouldInstall) return nil;
125
211 fHandlesURLTypes = [bundle objectForInfoDictionaryKey:@"CFBundleURLTypes"] != nil || [bundle _hasEAWTOverride:@"URLHandler"];
212 if (fHandlesURLTypes) {
213 [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self
214 andSelector:@selector(_handleOpenURLEvent:withReplyEvent:)
215 forEventClass:kInternetEventClass
216 andEventID:kAEGetURL];
217 }
218
219 // By HIG, Preferences are not available unless there is a handler. By default in Mac OS X,
220 // there is not a handler, but it is in the nib file for convenience.
221 removeMenuItem(self.fPreferencesMenu);
222
223 [self _updateAboutMenu:YES enabled:YES];
224
225 // Now that the AppKit objects are known and set up, initialize the model data
226 BOOL aboutAvailable = ([self.fAboutMenu menu] != nil);
227 BOOL aboutEnabled = (aboutAvailable && [self.fAboutMenu isEnabled] && ([self.fAboutMenu target] != nil));
228
229 BOOL prefsAvailable = ([self.fPreferencesMenu menu] != nil);
230 BOOL prefsEnabled = (prefsAvailable && [self.fPreferencesMenu isEnabled] && ([self.fPreferencesMenu target] != nil));
231
232 JNIEnv *env = [ThreadUtilities getJNIEnv];
233 static JNF_CLASS_CACHE(sjc_AppMenuBarHandler, "com/apple/eawt/_AppMenuBarHandler");
234 static JNF_STATIC_MEMBER_CACHE(sjm_initMenuStates, sjc_AppMenuBarHandler, "initMenuStates", "(ZZZZ)V");
235 JNFCallStaticVoidMethod(env, sjm_initMenuStates, aboutAvailable, aboutEnabled, prefsAvailable, prefsEnabled);
236
237 // register for the finish launching and system power off notifications by default
238 NSNotificationCenter *ctr = [NSNotificationCenter defaultCenter];
239 Class clz = [ApplicationDelegate class];
240 [ctr addObserver:clz selector:@selector(_willFinishLaunching) name:NSApplicationWillFinishLaunchingNotification object:nil];
241 [ctr addObserver:clz selector:@selector(_systemWillPowerOff) name:NSWorkspaceWillPowerOffNotification object:nil];
242 [ctr addObserver:clz selector:@selector(_appDidActivate) name:NSApplicationDidBecomeActiveNotification object:nil];
243 [ctr addObserver:clz selector:@selector(_appDidDeactivate) name:NSApplicationDidResignActiveNotification object:nil];
244 [ctr addObserver:clz selector:@selector(_appDidHide) name:NSApplicationDidHideNotification object:nil];
245 [ctr addObserver:clz selector:@selector(_appDidUnhide) name:NSApplicationDidUnhideNotification object:nil];
246
247 return self;
248 }
249
250 - (void)dealloc {
483 NSView *view = [dockTile contentView];
484
485 if ([view isKindOfClass:[NSImageView class]]) {
486 NSImage *img = [((NSImageView *)view) image];
487 if (img) return img;
488 }
489
490 if (view == nil) {
491 return [NSImage imageNamed:@"NSApplicationIcon"];
492 }
493
494 NSRect frame = [view frame];
495 NSImage *image = [[NSImage alloc] initWithSize:frame.size];
496 [image lockFocus];
497 [view drawRect:frame];
498 [image unlockFocus];
499 [image autorelease];
500 return image;
501 }
502
503 @end
504
505
506 #pragma mark Native JNI calls
507
508 /*
509 * Class: com_apple_eawt_Application
510 * Method: nativeInitializeApplicationDelegate
511 * Signature: ()V
512 */
513 JNIEXPORT void JNICALL Java_com_apple_eawt_Application_nativeInitializeApplicationDelegate
514 (JNIEnv *env, jclass clz)
515 {
516 JNF_COCOA_ENTER(env);
517 // Force initialization to happen on AppKit thread!
518 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
519 [ApplicationDelegate sharedDelegate];
520 }];
521 JNF_COCOA_EXIT(env);
522 }
|
86 // of Java's AWT and want to install their own app delegate that will delegate
87 // to the AWT for some operations
88
89 @interface JavaAWTAppDelegateLoader : NSObject { }
90 @end
91
92 @implementation JavaAWTAppDelegateLoader
93 + (ApplicationDelegate *) awtAppDelegate {
94 return [ApplicationDelegate sharedDelegate];
95 }
96 @end
97
98
99 @implementation ApplicationDelegate
100
101 @synthesize fPreferencesMenu;
102 @synthesize fAboutMenu;
103
104 @synthesize fDockMenu;
105 @synthesize fDefaultMenuBar;
106 @synthesize isAppActive;
107
108
109 + (ApplicationDelegate *)sharedDelegate {
110 static ApplicationDelegate *sApplicationDelegate = nil;
111 static BOOL checked = NO;
112
113 if (sApplicationDelegate != nil) return sApplicationDelegate;
114 if (checked) return nil;
115
116 AWT_ASSERT_APPKIT_THREAD;
117
118 // don't install the EAWT delegate if another kind of NSApplication is installed, like say, Safari
119 BOOL shouldInstall = NO;
120 if (NSApp != nil) {
121 if ([NSApp isMemberOfClass:[NSApplication class]]) shouldInstall = YES;
122 if ([NSApp isKindOfClass:[NSApplicationAWT class]]) shouldInstall = YES;
123 }
124 checked = YES;
125 if (!shouldInstall) return nil;
126
212 fHandlesURLTypes = [bundle objectForInfoDictionaryKey:@"CFBundleURLTypes"] != nil || [bundle _hasEAWTOverride:@"URLHandler"];
213 if (fHandlesURLTypes) {
214 [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self
215 andSelector:@selector(_handleOpenURLEvent:withReplyEvent:)
216 forEventClass:kInternetEventClass
217 andEventID:kAEGetURL];
218 }
219
220 // By HIG, Preferences are not available unless there is a handler. By default in Mac OS X,
221 // there is not a handler, but it is in the nib file for convenience.
222 removeMenuItem(self.fPreferencesMenu);
223
224 [self _updateAboutMenu:YES enabled:YES];
225
226 // Now that the AppKit objects are known and set up, initialize the model data
227 BOOL aboutAvailable = ([self.fAboutMenu menu] != nil);
228 BOOL aboutEnabled = (aboutAvailable && [self.fAboutMenu isEnabled] && ([self.fAboutMenu target] != nil));
229
230 BOOL prefsAvailable = ([self.fPreferencesMenu menu] != nil);
231 BOOL prefsEnabled = (prefsAvailable && [self.fPreferencesMenu isEnabled] && ([self.fPreferencesMenu target] != nil));
232 isAppActive = NO;
233
234 JNIEnv *env = [ThreadUtilities getJNIEnv];
235 static JNF_CLASS_CACHE(sjc_AppMenuBarHandler, "com/apple/eawt/_AppMenuBarHandler");
236 static JNF_STATIC_MEMBER_CACHE(sjm_initMenuStates, sjc_AppMenuBarHandler, "initMenuStates", "(ZZZZ)V");
237 JNFCallStaticVoidMethod(env, sjm_initMenuStates, aboutAvailable, aboutEnabled, prefsAvailable, prefsEnabled);
238
239 // register for the finish launching and system power off notifications by default
240 NSNotificationCenter *ctr = [NSNotificationCenter defaultCenter];
241 Class clz = [ApplicationDelegate class];
242 [ctr addObserver:clz selector:@selector(_willFinishLaunching) name:NSApplicationWillFinishLaunchingNotification object:nil];
243 [ctr addObserver:clz selector:@selector(_systemWillPowerOff) name:NSWorkspaceWillPowerOffNotification object:nil];
244 [ctr addObserver:clz selector:@selector(_appDidActivate) name:NSApplicationDidBecomeActiveNotification object:nil];
245 [ctr addObserver:clz selector:@selector(_appDidDeactivate) name:NSApplicationDidResignActiveNotification object:nil];
246 [ctr addObserver:clz selector:@selector(_appDidHide) name:NSApplicationDidHideNotification object:nil];
247 [ctr addObserver:clz selector:@selector(_appDidUnhide) name:NSApplicationDidUnhideNotification object:nil];
248
249 return self;
250 }
251
252 - (void)dealloc {
485 NSView *view = [dockTile contentView];
486
487 if ([view isKindOfClass:[NSImageView class]]) {
488 NSImage *img = [((NSImageView *)view) image];
489 if (img) return img;
490 }
491
492 if (view == nil) {
493 return [NSImage imageNamed:@"NSApplicationIcon"];
494 }
495
496 NSRect frame = [view frame];
497 NSImage *image = [[NSImage alloc] initWithSize:frame.size];
498 [image lockFocus];
499 [view drawRect:frame];
500 [image unlockFocus];
501 [image autorelease];
502 return image;
503 }
504
505 - (void)applicationWillBecomeActive:(id)application {
506 isAppActive = YES;
507 }
508
509 - (void)applicationWillResignActive:(id)application {
510 isAppActive = NO;
511 }
512
513 @end
514
515
516 #pragma mark Native JNI calls
517
518 /*
519 * Class: com_apple_eawt_Application
520 * Method: nativeInitializeApplicationDelegate
521 * Signature: ()V
522 */
523 JNIEXPORT void JNICALL Java_com_apple_eawt_Application_nativeInitializeApplicationDelegate
524 (JNIEnv *env, jclass clz)
525 {
526 JNF_COCOA_ENTER(env);
527 // Force initialization to happen on AppKit thread!
528 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
529 [ApplicationDelegate sharedDelegate];
530 }];
531 JNF_COCOA_EXIT(env);
532 }
|