Path: blob/master/src/java.desktop/macosx/native/libosxapp/QueuingApplicationDelegate.m
41152 views
/*1* Copyright (c) 2011, 2013, 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 <Cocoa/Cocoa.h>2627#import "QueuingApplicationDelegate.h"2829@interface NSBundle (EAWTOverrides)30- (BOOL)_hasEAWTOverride:(NSString *)key;31@end323334@implementation NSBundle (EAWTOverrides)3536- (BOOL)_hasEAWTOverride:(NSString *)key {37return [[[[self objectForInfoDictionaryKey:@"Java"] objectForKey:@"EAWTOverride"] objectForKey:key] boolValue];38}3940@end4142@implementation QueuingApplicationDelegate4344@synthesize realDelegate;45@synthesize queue;4647+ (QueuingApplicationDelegate*) sharedDelegate48{49static QueuingApplicationDelegate * qad = nil;5051if (!qad) {52qad = [QueuingApplicationDelegate new];53}5455return qad;56}5758- (id) init59{60self = [super init];61if (!self) {62return self;63}6465self.queue = [NSMutableArray arrayWithCapacity: 0];6667// If the java application has a bundle with an Info.plist file with68// a CFBundleDocumentTypes entry, then it is set up to handle Open Doc69// and Print Doc commands for these files. Therefore java AWT will70// cache Open Doc and Print Doc events that are sent prior to a71// listener being installed by the client java application.72NSBundle *bundle = [NSBundle mainBundle];73fHandlesDocumentTypes = [bundle objectForInfoDictionaryKey:@"CFBundleDocumentTypes"] != nil || [bundle _hasEAWTOverride:@"DocumentHandler"];74fHandlesURLTypes = [bundle objectForInfoDictionaryKey:@"CFBundleURLTypes"] != nil || [bundle _hasEAWTOverride:@"URLHandler"];75if (fHandlesURLTypes) {76[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self77andSelector:@selector(_handleOpenURLEvent:withReplyEvent:)78forEventClass:kInternetEventClass79andEventID:kAEGetURL];80}8182NSNotificationCenter *ctr = [NSNotificationCenter defaultCenter];83[ctr addObserver:self selector:@selector(_willFinishLaunching) name:NSApplicationWillFinishLaunchingNotification object:nil];84[ctr addObserver:self selector:@selector(_systemWillPowerOff) name:NSWorkspaceWillPowerOffNotification object:nil];85[ctr addObserver:self selector:@selector(_appDidActivate) name:NSApplicationDidBecomeActiveNotification object:nil];86[ctr addObserver:self selector:@selector(_appDidDeactivate) name:NSApplicationDidResignActiveNotification object:nil];87[ctr addObserver:self selector:@selector(_appDidHide) name:NSApplicationDidHideNotification object:nil];88[ctr addObserver:self selector:@selector(_appDidUnhide) name:NSApplicationDidUnhideNotification object:nil];8990return self;91}9293- (void)dealloc94{95if (fHandlesURLTypes) {96[[NSAppleEventManager sharedAppleEventManager] removeEventHandlerForEventClass: kInternetEventClass andEventID:kAEGetURL];97}9899NSNotificationCenter *ctr = [NSNotificationCenter defaultCenter];100Class clz = [QueuingApplicationDelegate class];101[ctr removeObserver:clz];102103self.queue = nil;104self.realDelegate = nil;105106[super dealloc];107}108109110- (void)_handleOpenURLEvent:(NSAppleEventDescriptor *)openURLEvent withReplyEvent:(NSAppleEventDescriptor *)replyEvent111{112// Make an explicit copy of the passed events as they may be invalidated by the time they're processed113NSAppleEventDescriptor *openURLEventCopy = [openURLEvent copy];114NSAppleEventDescriptor *replyEventCopy = [replyEvent copy];115116[self.queue addObject:[^(){117[self.realDelegate _handleOpenURLEvent:openURLEventCopy withReplyEvent:replyEventCopy];118[openURLEventCopy release];119[replyEventCopy release];120} copy]];121}122123- (void)application:(NSApplication *)theApplication openFiles:(NSArray *)fileNames124{125[self.queue addObject:[^(){126[self.realDelegate application:theApplication openFiles:fileNames];127} copy]];128}129130- (NSApplicationPrintReply)application:(NSApplication *)application printFiles:(NSArray *)fileNames withSettings:(NSDictionary *)printSettings showPrintPanels:(BOOL)showPrintPanels131{132if (!fHandlesDocumentTypes) {133return NSPrintingCancelled;134}135136[self.queue addObject:[^(){137[self.realDelegate application:application printFiles:fileNames withSettings:printSettings showPrintPanels:showPrintPanels];138} copy]];139140// well, a bit premature, but what else can we do?..141return NSPrintingSuccess;142}143144- (void)_willFinishLaunching145{146[self.queue addObject:[^(){147[[self.realDelegate class] _willFinishLaunching];148} copy]];149}150151- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag152{153[self.queue addObject:[^(){154[self.realDelegate applicationShouldHandleReopen:theApplication hasVisibleWindows:flag];155} copy]];156return YES;157}158159- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app160{161[self.queue addObject:[^(){162[self.realDelegate applicationShouldTerminate:app];163} copy]];164return NSTerminateLater;165}166167- (void)_systemWillPowerOff168{169[self.queue addObject:[^(){170[[self.realDelegate class] _systemWillPowerOff];171} copy]];172}173174- (void)_appDidActivate175{176[self.queue addObject:[^(){177[[self.realDelegate class] _appDidActivate];178} copy]];179}180181- (void)_appDidDeactivate182{183[self.queue addObject:[^(){184[[self.realDelegate class] _appDidDeactivate];185} copy]];186}187188- (void)_appDidHide189{190[self.queue addObject:[^(){191[[self.realDelegate class] _appDidHide];192} copy]];193}194195- (void)_appDidUnhide196{197[self.queue addObject:[^(){198[[self.realDelegate class] _appDidUnhide];199} copy]];200}201202- (void)processQueuedEventsWithTargetDelegate:(id <NSApplicationDelegate>)delegate203{204self.realDelegate = delegate;205206NSUInteger i;207NSUInteger count = [self.queue count];208209for (i = 0; i < count; i++) {210void (^event)() = (void (^)())[self.queue objectAtIndex: i];211event();212[event release];213}214215[self.queue removeAllObjects];216}217218@end219220221222