< prev index next >

src/java.desktop/macosx/native/libawt_lwawt/awt/CFileDialog.m

Print this page
rev 54094 : 8257853: Remove dependencies on JNF's JNI utility functions in AWT and 2D code
rev 54096 : 8259651: [macOS] Replace JNF_COCOA_ENTER/EXIT macros
rev 54097 : 8259869: [macOS] Remove desktop module dependencies on JNF Reference APIs
rev 54098 : 8260616: Removing remaining JNF dependencies in the java.desktop module
8259729: Missed JNFInstanceOf -> IsInstanceOf conversion


   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 


 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


 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 }


   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 

  29 #import "ThreadUtilities.h"
  30 #import "JNIUtilities.h"
  31 #import "CFileDialog.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 = (*env)->NewGlobalRef(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         (*env)->DeleteGlobalRef(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 


 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 = NormalizedPathJavaStringFromNSString(env, filename);
 146 
 147     DECLARE_CLASS_RETURN(jc_CFileDialog, "sun/lwawt/macosx/CFileDialog", NO);
 148     DECLARE_METHOD_RETURN(jm_queryFF, jc_CFileDialog, "queryFilenameFilter", "(Ljava/lang/String;)Z", NO);
 149     BOOL returnValue = (*env)->CallBooleanMethod(env, fFileDialog, jm_queryFF, jString);
 150     CHECK_EXCEPTION();
 151     (*env)->DeleteLocalRef(env, jString);
 152 
 153     return returnValue;
 154 }
 155 
 156 - (BOOL)panel:(id)sender shouldEnableURL:(NSURL *)url {
 157     if (!fHasFileFilter) return YES; // no filter, no problem!
 158 
 159     // check if it's not a normal file
 160     NSNumber *isFile = nil;
 161     if ([url getResourceValue:&isFile forKey:NSURLIsRegularFileKey error:nil]) {
 162         if (![isFile boolValue]) return YES; // always show directories and non-file entities (browsing servers/mounts, etc)
 163     }
 164 
 165     // if in directory-browsing mode, don't offer files
 166     if ((fMode != java_awt_FileDialog_LOAD) && (fMode != java_awt_FileDialog_SAVE)) {
 167         return NO;
 168     }
 169 
 170     // ask the file filter up in Java


 180 
 181 - (NSArray *)URLs {
 182     return [[fURLs retain] autorelease];
 183 }
 184 @end
 185 
 186 /*
 187  * Class:     sun_lwawt_macosx_CFileDialog
 188  * Method:    nativeRunFileDialog
 189  * Signature: (Ljava/lang/String;ILjava/io/FilenameFilter;
 190  *             Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;
 191  */
 192 JNIEXPORT jobjectArray JNICALL
 193 Java_sun_lwawt_macosx_CFileDialog_nativeRunFileDialog
 194 (JNIEnv *env, jobject peer, jstring title, jint mode, jboolean multipleMode,
 195  jboolean navigateApps, jboolean chooseDirectories, jboolean hasFilter,
 196  jstring directory, jstring file)
 197 {
 198     jobjectArray returnValue = NULL;
 199 
 200 JNI_COCOA_ENTER(env);
 201     NSString *dialogTitle = JavaStringToNSString(env, title);
 202     if ([dialogTitle length] == 0) {
 203         dialogTitle = @" ";
 204     }
 205 
 206     CFileDialog *dialogDelegate = [[CFileDialog alloc] initWithFilter:hasFilter
 207                                                            fileDialog:peer
 208                                                                 title:dialogTitle
 209                                                             directory:JavaStringToNSString(env, directory)
 210                                                                  file:JavaStringToNSString(env, file)
 211                                                                  mode:mode
 212                                                          multipleMode:multipleMode
 213                                                        shouldNavigate:navigateApps
 214                                                  canChooseDirectories:chooseDirectories
 215                                                               withEnv:env];
 216 
 217     [ThreadUtilities performOnMainThread:@selector(safeSaveOrLoad)
 218                                  on:dialogDelegate
 219                          withObject:nil
 220                       waitUntilDone:YES];
 221 
 222     if ([dialogDelegate userClickedOK]) {
 223         NSArray *urls = [dialogDelegate URLs];
 224         jsize count = [urls count];
 225 
 226         DECLARE_CLASS_RETURN(jc_String, "java/lang/String", NULL);
 227         returnValue = (*env)->NewObjectArray(env, count, jc_String, NULL);
 228 
 229         [urls enumerateObjectsUsingBlock:^(id url, NSUInteger index, BOOL *stop) {
 230             jstring filename = NormalizedPathJavaStringFromNSString(env, [url path]);
 231             (*env)->SetObjectArrayElement(env, returnValue, index, filename);
 232             (*env)->DeleteLocalRef(env, filename);
 233         }];
 234     }
 235 
 236     [dialogDelegate release];
 237 JNI_COCOA_EXIT(env);
 238     return returnValue;
 239 }
< prev index next >