Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/awt/CFileDialog.m
41152 views
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
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
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 = 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
171
NSString* filePath = (NSString*)CFURLCopyFileSystemPath((CFURLRef)url, kCFURLPOSIXPathStyle);
172
BOOL shouldEnableFile = [self askFilenameFilter:filePath];
173
[filePath release];
174
return shouldEnableFile;
175
}
176
177
- (BOOL) userClickedOK {
178
return fPanelResult == NSOKButton;
179
}
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
}
240
241