1 /* 2 * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #import <sys/stat.h> 27 #import <Cocoa/Cocoa.h> 28 #import <JavaNativeFoundation/JavaNativeFoundation.h> 29 30 #import "CFileDialog.h" 31 #import "ThreadUtilities.h" 32 33 #import "java_awt_FileDialog.h" 34 #import "sun_lwawt_macosx_CFileDialog.h" 35 36 @implementation CFileDialog 37 38 - (id)initWithFilter:(jboolean)inHasFilter 39 fileDialog:(jobject)inDialog 40 title:(NSString *)inTitle 41 directory:(NSString *)inPath 42 file:(NSString *)inFile 43 mode:(jint)inMode 44 multipleMode:(BOOL)inMultipleMode 45 shouldNavigate:(BOOL)inNavigateApps 46 canChooseDirectories:(BOOL)inChooseDirectories 47 withEnv:(JNIEnv*)env; 48 { 49 if (self = [super init]) { 50 fHasFileFilter = inHasFilter; 51 fFileDialog = JNFNewGlobalRef(env, inDialog); 52 fDirectory = inPath; 53 [fDirectory retain]; 54 fFile = inFile; 55 [fFile retain]; 56 fTitle = inTitle; 57 [fTitle retain]; 58 fMode = inMode; 59 fMultipleMode = inMultipleMode; 60 fNavigateApps = inNavigateApps; 61 fChooseDirectories = inChooseDirectories; 62 fPanelResult = NSCancelButton; 63 } 64 65 return self; 66 } 67 68 -(void) disposer { 69 if (fFileDialog != NULL) { 70 JNIEnv *env = [ThreadUtilities getJNIEnvUncached]; 71 JNFDeleteGlobalRef(env, fFileDialog); 72 fFileDialog = NULL; 73 } 74 } 75 76 -(void) dealloc { 77 [fDirectory release]; 78 fDirectory = nil; 79 80 [fFile release]; 81 fFile = nil; 82 83 [fTitle release]; 84 fTitle = nil; 85 86 [fURLs release]; 87 fURLs = nil; 88 89 [super dealloc]; 90 } 91 92 - (void)safeSaveOrLoad { 93 NSSavePanel *thePanel = nil; 94 95 /* 96 * 8013553: turns off extension hiding for the native file dialog. 97 * This way is used because setExtensionHidden(NO) doesn't work 98 * as expected. 99 */ 100 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 101 [defaults setBool:NO forKey:@"NSNavLastUserSetHideExtensionButtonState"]; 102 103 if (fMode == java_awt_FileDialog_SAVE) { 104 thePanel = [NSSavePanel savePanel]; 105 [thePanel setAllowsOtherFileTypes:YES]; 106 } else { 107 thePanel = [NSOpenPanel openPanel]; 108 } 109 110 if (thePanel != nil) { 111 [thePanel setTitle:fTitle]; 112 113 if (fNavigateApps) { 114 [thePanel setTreatsFilePackagesAsDirectories:YES]; 115 } 116 117 if (fMode == java_awt_FileDialog_LOAD) { 118 NSOpenPanel *openPanel = (NSOpenPanel *)thePanel; 119 [openPanel setAllowsMultipleSelection:fMultipleMode]; 120 [openPanel setCanChooseFiles:!fChooseDirectories]; 121 [openPanel setCanChooseDirectories:fChooseDirectories]; 122 [openPanel setCanCreateDirectories:YES]; 123 } 124 125 [thePanel setDelegate:self]; 126 fPanelResult = [thePanel runModalForDirectory:fDirectory file:fFile]; 127 [thePanel setDelegate:nil]; 128 129 if ([self userClickedOK]) { 130 if (fMode == java_awt_FileDialog_LOAD) { 131 NSOpenPanel *openPanel = (NSOpenPanel *)thePanel; 132 fURLs = [openPanel URLs]; 133 } else { 134 fURLs = [NSArray arrayWithObject:[thePanel URL]]; 135 } 136 [fURLs retain]; 137 } 138 } 139 140 [self disposer]; 141 } 142 143 - (BOOL) askFilenameFilter:(NSString *)filename { 144 JNIEnv *env = [ThreadUtilities getJNIEnv]; 145 jstring jString = JNFNormalizedJavaStringForPath(env, filename); 146 147 static JNF_CLASS_CACHE(jc_CFileDialog, "sun/lwawt/macosx/CFileDialog"); 148 static JNF_MEMBER_CACHE(jm_queryFF, jc_CFileDialog, "queryFilenameFilter", "(Ljava/lang/String;)Z"); 149 BOOL returnValue = JNFCallBooleanMethod(env, fFileDialog, jm_queryFF, jString); // AWT_THREADING Safe (AWTRunLoopMode) 150 (*env)->DeleteLocalRef(env, jString); 151 152 return returnValue; 153 } 154 155 - (BOOL)panel:(id)sender shouldEnableURL:(NSURL *)url { 156 if (!fHasFileFilter) return YES; // no filter, no problem! 157 158 // check if it's not a normal file 159 NSNumber *isFile = nil; 160 if ([url getResourceValue:&isFile forKey:NSURLIsRegularFileKey error:nil]) { 161 if (![isFile boolValue]) return YES; // always show directories and non-file entities (browsing servers/mounts, etc) 162 } 163 164 // if in directory-browsing mode, don't offer files 165 if ((fMode != java_awt_FileDialog_LOAD) && (fMode != java_awt_FileDialog_SAVE)) { 166 return NO; 167 } 168 169 // ask the file filter up in Java 170 NSString* filePath = (NSString*)CFURLCopyFileSystemPath((CFURLRef)url, kCFURLPOSIXPathStyle); 171 BOOL shouldEnableFile = [self askFilenameFilter:filePath]; 172 [filePath release]; 173 return shouldEnableFile; 174 } 175 176 - (BOOL) userClickedOK { 177 return fPanelResult == NSOKButton; 178 } 179 180 - (NSArray *)URLs { 181 return [[fURLs retain] autorelease]; 182 } 183 @end 184 185 /* 186 * Class: sun_lwawt_macosx_CFileDialog 187 * Method: nativeRunFileDialog 188 * Signature: (Ljava/lang/String;ILjava/io/FilenameFilter; 189 * Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String; 190 */ 191 JNIEXPORT jobjectArray JNICALL 192 Java_sun_lwawt_macosx_CFileDialog_nativeRunFileDialog 193 (JNIEnv *env, jobject peer, jstring title, jint mode, jboolean multipleMode, 194 jboolean navigateApps, jboolean chooseDirectories, jboolean hasFilter, 195 jstring directory, jstring file) 196 { 197 jobjectArray returnValue = NULL; 198 199 JNF_COCOA_ENTER(env); 200 NSString *dialogTitle = JNFJavaToNSString(env, title); 201 if ([dialogTitle length] == 0) { 202 dialogTitle = @" "; 203 } 204 205 CFileDialog *dialogDelegate = [[CFileDialog alloc] initWithFilter:hasFilter 206 fileDialog:peer 207 title:dialogTitle 208 directory:JNFJavaToNSString(env, directory) 209 file:JNFJavaToNSString(env, file) 210 mode:mode 211 multipleMode:multipleMode 212 shouldNavigate:navigateApps 213 canChooseDirectories:chooseDirectories 214 withEnv:env]; 215 216 [JNFRunLoop performOnMainThread:@selector(safeSaveOrLoad) 217 on:dialogDelegate 218 withObject:nil 219 waitUntilDone:YES]; 220 221 if ([dialogDelegate userClickedOK]) { 222 NSArray *urls = [dialogDelegate URLs]; 223 jsize count = [urls count]; 224 225 static JNF_CLASS_CACHE(jc_String, "java/lang/String"); 226 returnValue = JNFNewObjectArray(env, &jc_String, count); 227 228 [urls enumerateObjectsUsingBlock:^(id url, NSUInteger index, BOOL *stop) { 229 jstring filename = JNFNormalizedJavaStringForPath(env, [url path]); 230 (*env)->SetObjectArrayElement(env, returnValue, index, filename); 231 (*env)->DeleteLocalRef(env, filename); 232 }]; 233 } 234 235 [dialogDelegate release]; 236 JNF_COCOA_EXIT(env); 237 return returnValue; 238 }