src/macosx/native/sun/osxapp/NSApplicationAWT.m

Print this page




  35 
  36 static BOOL sUsingDefaultNIB = YES;
  37 static NSString *SHARED_FRAMEWORK_BUNDLE = @"/System/Library/Frameworks/JavaVM.framework";
  38 static id <NSApplicationDelegate> applicationDelegate = nil;
  39 static QueuingApplicationDelegate * qad = nil;
  40 
  41 // Flag used to indicate to the Plugin2 event synthesis code to do a postEvent instead of sendEvent
  42 BOOL postEventDuringEventSynthesis = NO;
  43 
  44 @implementation NSApplicationAWT
  45 
  46 - (id) init
  47 {
  48     // Headless: NO
  49     // Embedded: NO
  50     // Multiple Calls: NO
  51     //  Caller: +[NSApplication sharedApplication]
  52 
  53 AWT_ASSERT_APPKIT_THREAD;
  54     fApplicationName = nil;
  55     fUseDefaultIcon = NO;
  56 
  57     // NSApplication will call _RegisterApplication with the application's bundle, but there may not be one.
  58     // So, we need to call it ourselves to ensure the app is set up properly.
  59     [self registerWithProcessManager];
  60 
  61     return [super init];
  62 }
  63 
  64 - (void)dealloc
  65 {
  66     [fApplicationName release];
  67     fApplicationName = nil;
  68 
  69     [super dealloc];
  70 }
  71 //- (void)finalize { [super finalize]; }
  72 
  73 - (void)finishLaunching
  74 {
  75 AWT_ASSERT_APPKIT_THREAD;


 130 }
 131 
 132 - (void) registerWithProcessManager
 133 {
 134     // Headless: NO
 135     // Embedded: NO
 136     // Multiple Calls: NO
 137     //  Caller: -[NSApplicationAWT init]
 138 
 139 AWT_ASSERT_APPKIT_THREAD;
 140     JNIEnv *env = [ThreadUtilities getJNIEnv];
 141 
 142     char envVar[80];
 143 
 144     // The following environment variable is set from the -Xdock:name param. It should be UTF8.
 145     snprintf(envVar, sizeof(envVar), "APP_NAME_%d", getpid());
 146     char *appName = getenv(envVar);
 147     if (appName != NULL) {
 148         fApplicationName = [NSString stringWithUTF8String:appName];
 149         unsetenv(envVar);
 150 
 151         // If this environment variable was set we were launched from the command line, so we
 152         // should use a generic app icon if one wasn't set.
 153         fUseDefaultIcon = YES;
 154     }
 155 
 156     // If it wasn't specified as an argument, see if it was specified as a system property.
 157     if (fApplicationName == nil) {
 158         fApplicationName = [PropertiesUtilities javaSystemPropertyForKey:@"apple.awt.application.name" withEnv:env];
 159     }
 160 
 161     // If we STILL don't have it, the app name is retrieved from an environment variable (set in java.c) It should be UTF8.
 162     if (fApplicationName == nil) {
 163         char mainClassEnvVar[80];
 164         snprintf(mainClassEnvVar, sizeof(mainClassEnvVar), "JAVA_MAIN_CLASS_%d", getpid());
 165         char *mainClass = getenv(mainClassEnvVar);
 166         if (mainClass != NULL) {
 167             fApplicationName = [NSString stringWithUTF8String:mainClass];
 168             unsetenv(mainClassEnvVar);
 169 
 170             NSRange lastPeriod = [fApplicationName rangeOfString:@"." options:NSBackwardsSearch];
 171             if (lastPeriod.location != NSNotFound) {
 172                 fApplicationName = [fApplicationName substringFromIndex:lastPeriod.location + 1];
 173             }
 174             // If this environment variable was set we were launched from the command line, so we
 175             // should use a generic app icon if one wasn't set.
 176             fUseDefaultIcon = YES;
 177         }
 178     }
 179 
 180     // The dock name is nil for double-clickable Java apps (bundled and Web Start apps)
 181     // When that happens get the display name, and if that's not available fall back to
 182     // CFBundleName.
 183     NSBundle *mainBundle = [NSBundle mainBundle];
 184     if (fApplicationName == nil) {
 185         fApplicationName = (NSString *)[mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"];
 186 
 187         if (fApplicationName == nil) {
 188             fApplicationName = (NSString *)[mainBundle objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey];
 189 
 190             if (fApplicationName == nil) {
 191                 fApplicationName = (NSString *)[mainBundle objectForInfoDictionaryKey: (NSString *)kCFBundleExecutableKey];
 192 
 193                 if (fApplicationName == nil) {
 194                     // Name of last resort is the last part of the applicatoin name without the .app (consistent with CopyProcessName)
 195                     fApplicationName = [[mainBundle bundlePath] lastPathComponent];
 196 


 249 
 250 - (void) setDockIconWithEnv:(JNIEnv *)env {
 251     NSString *theIconPath = nil;
 252 
 253     // The following environment variable is set in java.c. It is probably UTF8.
 254     char envVar[80];
 255     snprintf(envVar, sizeof(envVar), "APP_ICON_%d", getpid());
 256     char *appIcon = getenv(envVar);
 257     if (appIcon != NULL) {
 258         theIconPath = [NSString stringWithUTF8String:appIcon];
 259         unsetenv(envVar);
 260     }
 261 
 262     if (theIconPath == nil) {
 263         theIconPath = [PropertiesUtilities javaSystemPropertyForKey:@"apple.awt.application.icon" withEnv:env];
 264     }
 265 
 266     // If the icon file wasn't specified as an argument and we need to get an icon
 267     // we'll use the generic java app icon.
 268     NSString *defaultIconPath = [NSString stringWithFormat:@"%@%@", SHARED_FRAMEWORK_BUNDLE, @"/Resources/GenericApp.icns"];
 269     if (fUseDefaultIcon && (theIconPath == nil)) {
 270         theIconPath = defaultIconPath;
 271     }
 272 
 273     // Set up the dock icon if we have an icon name.
 274     if (theIconPath != nil) {
 275         NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile:theIconPath];
 276 
 277         // If we failed for some reason fall back to the default icon.
 278         if (iconImage == nil) {
 279             iconImage = [[NSImage alloc] initWithContentsOfFile:defaultIconPath];
 280         }
 281 
 282         [NSApp setApplicationIconImage:iconImage];
 283         [iconImage release];
 284     }
 285 }
 286 
 287 + (void) runAWTLoopWithApp:(NSApplication*)app {
 288     NSAutoreleasePool *pool = [NSAutoreleasePool new];
 289 




  35 
  36 static BOOL sUsingDefaultNIB = YES;
  37 static NSString *SHARED_FRAMEWORK_BUNDLE = @"/System/Library/Frameworks/JavaVM.framework";
  38 static id <NSApplicationDelegate> applicationDelegate = nil;
  39 static QueuingApplicationDelegate * qad = nil;
  40 
  41 // Flag used to indicate to the Plugin2 event synthesis code to do a postEvent instead of sendEvent
  42 BOOL postEventDuringEventSynthesis = NO;
  43 
  44 @implementation NSApplicationAWT
  45 
  46 - (id) init
  47 {
  48     // Headless: NO
  49     // Embedded: NO
  50     // Multiple Calls: NO
  51     //  Caller: +[NSApplication sharedApplication]
  52 
  53 AWT_ASSERT_APPKIT_THREAD;
  54     fApplicationName = nil;

  55 
  56     // NSApplication will call _RegisterApplication with the application's bundle, but there may not be one.
  57     // So, we need to call it ourselves to ensure the app is set up properly.
  58     [self registerWithProcessManager];
  59 
  60     return [super init];
  61 }
  62 
  63 - (void)dealloc
  64 {
  65     [fApplicationName release];
  66     fApplicationName = nil;
  67 
  68     [super dealloc];
  69 }
  70 //- (void)finalize { [super finalize]; }
  71 
  72 - (void)finishLaunching
  73 {
  74 AWT_ASSERT_APPKIT_THREAD;


 129 }
 130 
 131 - (void) registerWithProcessManager
 132 {
 133     // Headless: NO
 134     // Embedded: NO
 135     // Multiple Calls: NO
 136     //  Caller: -[NSApplicationAWT init]
 137 
 138 AWT_ASSERT_APPKIT_THREAD;
 139     JNIEnv *env = [ThreadUtilities getJNIEnv];
 140 
 141     char envVar[80];
 142 
 143     // The following environment variable is set from the -Xdock:name param. It should be UTF8.
 144     snprintf(envVar, sizeof(envVar), "APP_NAME_%d", getpid());
 145     char *appName = getenv(envVar);
 146     if (appName != NULL) {
 147         fApplicationName = [NSString stringWithUTF8String:appName];
 148         unsetenv(envVar);




 149     }
 150 
 151     // If it wasn't specified as an argument, see if it was specified as a system property.
 152     if (fApplicationName == nil) {
 153         fApplicationName = [PropertiesUtilities javaSystemPropertyForKey:@"apple.awt.application.name" withEnv:env];
 154     }
 155 
 156     // If we STILL don't have it, the app name is retrieved from an environment variable (set in java.c) It should be UTF8.
 157     if (fApplicationName == nil) {
 158         char mainClassEnvVar[80];
 159         snprintf(mainClassEnvVar, sizeof(mainClassEnvVar), "JAVA_MAIN_CLASS_%d", getpid());
 160         char *mainClass = getenv(mainClassEnvVar);
 161         if (mainClass != NULL) {
 162             fApplicationName = [NSString stringWithUTF8String:mainClass];
 163             unsetenv(mainClassEnvVar);
 164 
 165             NSRange lastPeriod = [fApplicationName rangeOfString:@"." options:NSBackwardsSearch];
 166             if (lastPeriod.location != NSNotFound) {
 167                 fApplicationName = [fApplicationName substringFromIndex:lastPeriod.location + 1];
 168             }



 169         }
 170     }
 171 
 172     // The dock name is nil for double-clickable Java apps (bundled and Web Start apps)
 173     // When that happens get the display name, and if that's not available fall back to
 174     // CFBundleName.
 175     NSBundle *mainBundle = [NSBundle mainBundle];
 176     if (fApplicationName == nil) {
 177         fApplicationName = (NSString *)[mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"];
 178 
 179         if (fApplicationName == nil) {
 180             fApplicationName = (NSString *)[mainBundle objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey];
 181 
 182             if (fApplicationName == nil) {
 183                 fApplicationName = (NSString *)[mainBundle objectForInfoDictionaryKey: (NSString *)kCFBundleExecutableKey];
 184 
 185                 if (fApplicationName == nil) {
 186                     // Name of last resort is the last part of the applicatoin name without the .app (consistent with CopyProcessName)
 187                     fApplicationName = [[mainBundle bundlePath] lastPathComponent];
 188 


 241 
 242 - (void) setDockIconWithEnv:(JNIEnv *)env {
 243     NSString *theIconPath = nil;
 244 
 245     // The following environment variable is set in java.c. It is probably UTF8.
 246     char envVar[80];
 247     snprintf(envVar, sizeof(envVar), "APP_ICON_%d", getpid());
 248     char *appIcon = getenv(envVar);
 249     if (appIcon != NULL) {
 250         theIconPath = [NSString stringWithUTF8String:appIcon];
 251         unsetenv(envVar);
 252     }
 253 
 254     if (theIconPath == nil) {
 255         theIconPath = [PropertiesUtilities javaSystemPropertyForKey:@"apple.awt.application.icon" withEnv:env];
 256     }
 257 
 258     // If the icon file wasn't specified as an argument and we need to get an icon
 259     // we'll use the generic java app icon.
 260     NSString *defaultIconPath = [NSString stringWithFormat:@"%@%@", SHARED_FRAMEWORK_BUNDLE, @"/Resources/GenericApp.icns"];
 261     if (([NSApp applicationIconImage] == nil) && (theIconPath == nil)) {
 262         theIconPath = defaultIconPath;
 263     }
 264 
 265     // Set up the dock icon if we have an icon name.
 266     if (theIconPath != nil) {
 267         NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile:theIconPath];
 268 
 269         // If we failed for some reason fall back to the default icon.
 270         if (iconImage == nil) {
 271             iconImage = [[NSImage alloc] initWithContentsOfFile:defaultIconPath];
 272         }
 273 
 274         [NSApp setApplicationIconImage:iconImage];
 275         [iconImage release];
 276     }
 277 }
 278 
 279 + (void) runAWTLoopWithApp:(NSApplication*)app {
 280     NSAutoreleasePool *pool = [NSAutoreleasePool new];
 281