4639N/A/*
4639N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4639N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4639N/A *
4639N/A * This code is free software; you can redistribute it and/or modify it
4639N/A * under the terms of the GNU General Public License version 2 only, as
4639N/A * published by the Free Software Foundation. Oracle designates this
4639N/A * particular file as subject to the "Classpath" exception as provided
4639N/A * by Oracle in the LICENSE file that accompanied this code.
4639N/A *
4639N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4639N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4639N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4639N/A * version 2 for more details (a copy is included in the LICENSE file that
4639N/A * accompanied this code).
4639N/A *
4639N/A * You should have received a copy of the GNU General Public License version
4639N/A * 2 along with this work; if not, write to the Free Software Foundation,
4639N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4639N/A *
4639N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4639N/A * or visit www.oracle.com if you need additional information or have any
4639N/A * questions.
4639N/A */
4639N/A
4639N/A#import <Cocoa/Cocoa.h>
4639N/A
4639N/A#import "QueuingApplicationDelegate.h"
4639N/A
4639N/A@interface NSBundle (EAWTOverrides)
4639N/A- (BOOL)_hasEAWTOverride:(NSString *)key;
4639N/A@end
4639N/A
4639N/A
4639N/A@implementation NSBundle (EAWTOverrides)
4639N/A
4639N/A- (BOOL)_hasEAWTOverride:(NSString *)key {
4639N/A return [[[[self objectForInfoDictionaryKey:@"Java"] objectForKey:@"EAWTOverride"] objectForKey:key] boolValue];
4639N/A}
4639N/A
4639N/A@end
4639N/A
4639N/A@implementation QueuingApplicationDelegate
4639N/A
5159N/A@synthesize realDelegate;
5159N/A@synthesize queue;
5159N/A
4639N/A+ (QueuingApplicationDelegate*) sharedDelegate
4639N/A{
4639N/A static QueuingApplicationDelegate * qad = nil;
4639N/A
4639N/A if (!qad) {
4639N/A qad = [QueuingApplicationDelegate new];
4639N/A }
4639N/A
4639N/A return qad;
4639N/A}
4639N/A
4639N/A- (id) init
4639N/A{
4639N/A self = [super init];
4639N/A if (!self) {
4639N/A return self;
4639N/A }
4639N/A
5159N/A self.queue = [NSMutableArray arrayWithCapacity: 0];
4639N/A
4639N/A // If the java application has a bundle with an Info.plist file with
4639N/A // a CFBundleDocumentTypes entry, then it is set up to handle Open Doc
4639N/A // and Print Doc commands for these files. Therefore java AWT will
4730N/A // cache Open Doc and Print Doc events that are sent prior to a
4639N/A // listener being installed by the client java application.
4639N/A NSBundle *bundle = [NSBundle mainBundle];
4639N/A fHandlesDocumentTypes = [bundle objectForInfoDictionaryKey:@"CFBundleDocumentTypes"] != nil || [bundle _hasEAWTOverride:@"DocumentHandler"];
4639N/A fHandlesURLTypes = [bundle objectForInfoDictionaryKey:@"CFBundleURLTypes"] != nil || [bundle _hasEAWTOverride:@"URLHandler"];
4639N/A if (fHandlesURLTypes) {
4639N/A [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self
4639N/A andSelector:@selector(_handleOpenURLEvent:withReplyEvent:)
4639N/A forEventClass:kInternetEventClass
4639N/A andEventID:kAEGetURL];
4639N/A }
4639N/A
4639N/A NSNotificationCenter *ctr = [NSNotificationCenter defaultCenter];
4639N/A [ctr addObserver:self selector:@selector(_willFinishLaunching) name:NSApplicationWillFinishLaunchingNotification object:nil];
4639N/A [ctr addObserver:self selector:@selector(_systemWillPowerOff) name:NSWorkspaceWillPowerOffNotification object:nil];
4639N/A [ctr addObserver:self selector:@selector(_appDidActivate) name:NSApplicationDidBecomeActiveNotification object:nil];
4639N/A [ctr addObserver:self selector:@selector(_appDidDeactivate) name:NSApplicationDidResignActiveNotification object:nil];
4639N/A [ctr addObserver:self selector:@selector(_appDidHide) name:NSApplicationDidHideNotification object:nil];
4639N/A [ctr addObserver:self selector:@selector(_appDidUnhide) name:NSApplicationDidUnhideNotification object:nil];
4639N/A
4639N/A return self;
4639N/A}
4639N/A
4639N/A- (void)dealloc
4639N/A{
4639N/A if (fHandlesURLTypes) {
4639N/A [[NSAppleEventManager sharedAppleEventManager] removeEventHandlerForEventClass: kInternetEventClass andEventID:kAEGetURL];
4639N/A }
4639N/A
4639N/A NSNotificationCenter *ctr = [NSNotificationCenter defaultCenter];
4639N/A Class clz = [QueuingApplicationDelegate class];
4639N/A [ctr removeObserver:clz];
4639N/A
5159N/A self.queue = nil;
5159N/A self.realDelegate = nil;
4639N/A
4639N/A [super dealloc];
4639N/A}
4639N/A
4639N/A
4639N/A- (void)_handleOpenURLEvent:(NSAppleEventDescriptor *)openURLEvent withReplyEvent:(NSAppleEventDescriptor *)replyEvent
4639N/A{
6121N/A // Make an explicit copy of the passed events as they may be invalidated by the time they're processed
6121N/A NSAppleEventDescriptor *openURLEventCopy = [openURLEvent copy];
6121N/A NSAppleEventDescriptor *replyEventCopy = [replyEvent copy];
6121N/A
5159N/A [self.queue addObject:[^(){
6121N/A [self.realDelegate _handleOpenURLEvent:openURLEventCopy withReplyEvent:replyEventCopy];
6121N/A [openURLEventCopy release];
6121N/A [replyEventCopy release];
5159N/A } copy]];
4639N/A}
4639N/A
4639N/A- (void)application:(NSApplication *)theApplication openFiles:(NSArray *)fileNames
4639N/A{
5159N/A [self.queue addObject:[^(){
5159N/A [self.realDelegate application:theApplication openFiles:fileNames];
5159N/A } copy]];
4639N/A}
4639N/A
4639N/A- (NSApplicationPrintReply)application:(NSApplication *)application printFiles:(NSArray *)fileNames withSettings:(NSDictionary *)printSettings showPrintPanels:(BOOL)showPrintPanels
4639N/A{
4639N/A if (!fHandlesDocumentTypes) {
4639N/A return NSPrintingCancelled;
4639N/A }
4639N/A
5159N/A [self.queue addObject:[^(){
5159N/A [self.realDelegate application:application printFiles:fileNames withSettings:printSettings showPrintPanels:showPrintPanels];
5159N/A } copy]];
4730N/A
4639N/A // well, a bit premature, but what else can we do?..
4639N/A return NSPrintingSuccess;
4639N/A}
4639N/A
4639N/A- (void)_willFinishLaunching
4639N/A{
5159N/A [self.queue addObject:[^(){
5159N/A [[self.realDelegate class] _willFinishLaunching];
5159N/A } copy]];
4639N/A}
4639N/A
4639N/A- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
4639N/A{
5159N/A [self.queue addObject:[^(){
5159N/A [self.realDelegate applicationShouldHandleReopen:theApplication hasVisibleWindows:flag];
5159N/A } copy]];
4639N/A return YES;
4639N/A}
4639N/A
4639N/A- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app
4639N/A{
5159N/A [self.queue addObject:[^(){
5159N/A [self.realDelegate applicationShouldTerminate:app];
5159N/A } copy]];
4639N/A return NSTerminateLater;
4639N/A}
4639N/A
4639N/A- (void)_systemWillPowerOff
4639N/A{
5159N/A [self.queue addObject:[^(){
5159N/A [[self.realDelegate class] _systemWillPowerOff];
5159N/A } copy]];
4639N/A}
4639N/A
4639N/A- (void)_appDidActivate
4639N/A{
5159N/A [self.queue addObject:[^(){
5159N/A [[self.realDelegate class] _appDidActivate];
5159N/A } copy]];
4639N/A}
4639N/A
4639N/A- (void)_appDidDeactivate
4639N/A{
5159N/A [self.queue addObject:[^(){
5159N/A [[self.realDelegate class] _appDidDeactivate];
5159N/A } copy]];
4639N/A}
4639N/A
4639N/A- (void)_appDidHide
4639N/A{
5159N/A [self.queue addObject:[^(){
5159N/A [[self.realDelegate class] _appDidHide];
5159N/A } copy]];
4639N/A}
4639N/A
4639N/A- (void)_appDidUnhide
4639N/A{
5159N/A [self.queue addObject:[^(){
5159N/A [[self.realDelegate class] _appDidUnhide];
5159N/A } copy]];
4639N/A}
4639N/A
4639N/A- (void)processQueuedEventsWithTargetDelegate:(id <NSApplicationDelegate>)delegate
4639N/A{
5159N/A self.realDelegate = delegate;
5159N/A
4639N/A NSUInteger i;
5159N/A NSUInteger count = [self.queue count];
4639N/A
4639N/A for (i = 0; i < count; i++) {
5159N/A void (^event)() = (void (^)())[self.queue objectAtIndex: i];
4639N/A event();
5159N/A [event release];
4639N/A }
4639N/A
5159N/A [self.queue removeAllObjects];
4639N/A}
4639N/A
4639N/A@end
4639N/A