Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/java/awt/FileDialog.java
41152 views
1
/*
2
* Copyright (c) 1995, 2021, 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
package java.awt;
27
28
import java.awt.peer.FileDialogPeer;
29
import java.io.File;
30
import java.io.FilenameFilter;
31
import java.io.IOException;
32
import java.io.ObjectInputStream;
33
import java.io.Serial;
34
35
import sun.awt.AWTAccessor;
36
37
/**
38
* The {@code FileDialog} class displays a dialog window
39
* from which the user can select a file.
40
* <p>
41
* Since it is a modal dialog, when the application calls
42
* its {@code show} method to display the dialog,
43
* it blocks the rest of the application until the user has
44
* chosen a file.
45
*
46
* @see Window#show
47
*
48
* @author Sami Shaio
49
* @author Arthur van Hoff
50
* @since 1.0
51
*/
52
public class FileDialog extends Dialog {
53
54
/**
55
* This constant value indicates that the purpose of the file
56
* dialog window is to locate a file from which to read.
57
*/
58
public static final int LOAD = 0;
59
60
/**
61
* This constant value indicates that the purpose of the file
62
* dialog window is to locate a file to which to write.
63
*/
64
public static final int SAVE = 1;
65
66
/**
67
* There are two {@code FileDialog} modes: {@code LOAD} and
68
* {@code SAVE}.
69
* This integer will represent one or the other.
70
* If the mode is not specified it will default to {@code LOAD}.
71
*
72
* @serial
73
* @see #getMode
74
* @see #setMode
75
* @see java.awt.FileDialog#LOAD
76
* @see java.awt.FileDialog#SAVE
77
*/
78
int mode;
79
80
/**
81
* The string specifying the directory to display
82
* in the file dialog. This variable may be {@code null}.
83
*
84
* @serial
85
* @see #getDirectory
86
* @see #setDirectory
87
*/
88
String dir;
89
90
/**
91
* The string specifying the initial value of the
92
* filename text field in the file dialog.
93
* This variable may be {@code null}.
94
*
95
* @serial
96
* @see #getFile
97
* @see #setFile
98
*/
99
String file;
100
101
/**
102
* Contains the File instances for all the files that the user selects.
103
*
104
* @serial
105
* @see #getFiles
106
* @since 1.7
107
*/
108
private File[] files;
109
110
/**
111
* Represents whether the file dialog allows the multiple file selection.
112
*
113
* @serial
114
* @see #setMultipleMode
115
* @see #isMultipleMode
116
* @since 1.7
117
*/
118
private boolean multipleMode = false;
119
120
/**
121
* The filter used as the file dialog's filename filter.
122
* The file dialog will only be displaying files whose
123
* names are accepted by this filter.
124
* This variable may be {@code null}.
125
*
126
* @serial
127
* @see #getFilenameFilter
128
* @see #setFilenameFilter
129
* @see FilenameFilter
130
*/
131
@SuppressWarnings("serial") // Not statically typed as Serializable
132
FilenameFilter filter;
133
134
private static final String base = "filedlg";
135
private static int nameCounter = 0;
136
137
/**
138
* Use serialVersionUID from JDK 1.1 for interoperability.
139
*/
140
@Serial
141
private static final long serialVersionUID = 5035145889651310422L;
142
143
144
static {
145
/* ensure that the necessary native libraries are loaded */
146
Toolkit.loadLibraries();
147
if (!GraphicsEnvironment.isHeadless()) {
148
initIDs();
149
}
150
}
151
152
static {
153
AWTAccessor.setFileDialogAccessor(
154
new AWTAccessor.FileDialogAccessor() {
155
public void setFiles(FileDialog fileDialog, File[] files) {
156
fileDialog.setFiles(files);
157
}
158
public void setFile(FileDialog fileDialog, String file) {
159
fileDialog.file = ("".equals(file)) ? null : file;
160
}
161
public void setDirectory(FileDialog fileDialog, String directory) {
162
fileDialog.dir = ("".equals(directory)) ? null : directory;
163
}
164
public boolean isMultipleMode(FileDialog fileDialog) {
165
synchronized (fileDialog.getObjectLock()) {
166
return fileDialog.multipleMode;
167
}
168
}
169
});
170
}
171
172
/**
173
* Initialize JNI field and method IDs for fields that may be
174
accessed from C.
175
*/
176
private static native void initIDs();
177
178
/**
179
* Creates a file dialog for loading a file. The title of the
180
* file dialog is initially empty. This is a convenience method for
181
* {@code FileDialog(parent, "", LOAD)}.
182
* <p>
183
* <strong>Note:</strong> Some platforms may not support
184
* showing the user-specified title in a file dialog.
185
* In this situation, either no title will be displayed in the file dialog's
186
* title bar or, on some systems, the file dialog's title bar will not be
187
* displayed.
188
*
189
* @param parent the owner of the dialog
190
* @since 1.1
191
*/
192
public FileDialog(Frame parent) {
193
this(parent, "", LOAD);
194
}
195
196
/**
197
* Creates a file dialog window with the specified title for loading
198
* a file. The files shown are those in the current directory.
199
* This is a convenience method for
200
* {@code FileDialog(parent, title, LOAD)}.
201
* <p>
202
* <strong>Note:</strong> Some platforms may not support
203
* showing the user-specified title in a file dialog.
204
* In this situation, either no title will be displayed in the file dialog's
205
* title bar or, on some systems, the file dialog's title bar will not be
206
* displayed.
207
*
208
* @param parent the owner of the dialog
209
* @param title the title of the dialog
210
*/
211
public FileDialog(Frame parent, String title) {
212
this(parent, title, LOAD);
213
}
214
215
/**
216
* Creates a file dialog window with the specified title for loading
217
* or saving a file.
218
* <p>
219
* If the value of {@code mode} is {@code LOAD}, then the
220
* file dialog is finding a file to read, and the files shown are those
221
* in the current directory. If the value of
222
* {@code mode} is {@code SAVE}, the file dialog is finding
223
* a place to write a file.
224
* <p>
225
* <strong>Note:</strong> Some platforms may not support
226
* showing the user-specified title in a file dialog.
227
* In this situation, either no title will be displayed in the file dialog's
228
* title bar or, on some systems, the file dialog's title bar will not be
229
* displayed.
230
*
231
* @param parent the owner of the dialog
232
* @param title the title of the dialog
233
* @param mode the mode of the dialog; either
234
* {@code FileDialog.LOAD} or {@code FileDialog.SAVE}
235
* @exception IllegalArgumentException if an illegal file
236
* dialog mode is supplied
237
* @see java.awt.FileDialog#LOAD
238
* @see java.awt.FileDialog#SAVE
239
*/
240
public FileDialog(Frame parent, String title, int mode) {
241
super(parent, title, true);
242
this.setMode(mode);
243
setLayout(null);
244
}
245
246
/**
247
* Creates a file dialog for loading a file. The title of the
248
* file dialog is initially empty. This is a convenience method for
249
* {@code FileDialog(parent, "", LOAD)}.
250
* <p>
251
* <strong>Note:</strong> Some platforms may not support
252
* showing the user-specified title in a file dialog.
253
* In this situation, either no title will be displayed in the file dialog's
254
* title bar or, on some systems, the file dialog's title bar will not be
255
* displayed.
256
*
257
* @param parent the owner of the dialog
258
* @exception java.lang.IllegalArgumentException if the {@code parent}'s
259
* {@code GraphicsConfiguration}
260
* is not from a screen device;
261
* @exception java.lang.IllegalArgumentException if {@code parent}
262
* is {@code null}; this exception is always thrown when
263
* {@code GraphicsEnvironment.isHeadless}
264
* returns {@code true}
265
* @see java.awt.GraphicsEnvironment#isHeadless
266
* @since 1.5
267
*/
268
public FileDialog(Dialog parent) {
269
this(parent, "", LOAD);
270
}
271
272
/**
273
* Creates a file dialog window with the specified title for loading
274
* a file. The files shown are those in the current directory.
275
* This is a convenience method for
276
* {@code FileDialog(parent, title, LOAD)}.
277
* <p>
278
* <strong>Note:</strong> Some platforms may not support
279
* showing the user-specified title in a file dialog.
280
* In this situation, either no title will be displayed in the file dialog's
281
* title bar or, on some systems, the file dialog's title bar will not be
282
* displayed.
283
*
284
* @param parent the owner of the dialog
285
* @param title the title of the dialog; a {@code null} value
286
* will be accepted without causing a
287
* {@code NullPointerException} to be thrown
288
* @exception java.lang.IllegalArgumentException if the {@code parent}'s
289
* {@code GraphicsConfiguration}
290
* is not from a screen device;
291
* @exception java.lang.IllegalArgumentException if {@code parent}
292
* is {@code null}; this exception is always thrown when
293
* {@code GraphicsEnvironment.isHeadless}
294
* returns {@code true}
295
* @see java.awt.GraphicsEnvironment#isHeadless
296
* @since 1.5
297
*/
298
public FileDialog(Dialog parent, String title) {
299
this(parent, title, LOAD);
300
}
301
302
/**
303
* Creates a file dialog window with the specified title for loading
304
* or saving a file.
305
* <p>
306
* If the value of {@code mode} is {@code LOAD}, then the
307
* file dialog is finding a file to read, and the files shown are those
308
* in the current directory. If the value of
309
* {@code mode} is {@code SAVE}, the file dialog is finding
310
* a place to write a file.
311
* <p>
312
* <strong>Note:</strong> Some platforms may not support
313
* showing the user-specified title in a file dialog.
314
* In this situation, either no title will be displayed in the file dialog's
315
* title bar or, on some systems, the file dialog's title bar will not be
316
* displayed.
317
*
318
* @param parent the owner of the dialog
319
* @param title the title of the dialog; a {@code null} value
320
* will be accepted without causing a
321
* {@code NullPointerException} to be thrown
322
* @param mode the mode of the dialog; either
323
* {@code FileDialog.LOAD} or {@code FileDialog.SAVE}
324
* @exception java.lang.IllegalArgumentException if an illegal
325
* file dialog mode is supplied;
326
* @exception java.lang.IllegalArgumentException if the {@code parent}'s
327
* {@code GraphicsConfiguration}
328
* is not from a screen device;
329
* @exception java.lang.IllegalArgumentException if {@code parent}
330
* is {@code null}; this exception is always thrown when
331
* {@code GraphicsEnvironment.isHeadless}
332
* returns {@code true}
333
* @see java.awt.GraphicsEnvironment#isHeadless
334
* @see java.awt.FileDialog#LOAD
335
* @see java.awt.FileDialog#SAVE
336
* @since 1.5
337
*/
338
public FileDialog(Dialog parent, String title, int mode) {
339
super(parent, title, true);
340
this.setMode(mode);
341
setLayout(null);
342
}
343
344
345
/**
346
* {@inheritDoc}
347
* <p>
348
* <strong>Note:</strong> Some platforms may not support
349
* showing the user-specified title in a file dialog.
350
* In this situation, either no title will be displayed in the file dialog's
351
* title bar or, on some systems, the file dialog's title bar will not be
352
* displayed.
353
*/
354
@Override
355
public void setTitle(String title) {
356
super.setTitle(title);
357
}
358
359
360
/**
361
* Constructs a name for this component. Called by {@code getName()}
362
* when the name is {@code null}.
363
*/
364
String constructComponentName() {
365
synchronized (FileDialog.class) {
366
return base + nameCounter++;
367
}
368
}
369
370
/**
371
* Creates the file dialog's peer. The peer allows us to change the look
372
* of the file dialog without changing its functionality.
373
*/
374
public void addNotify() {
375
synchronized(getTreeLock()) {
376
if (parent != null && parent.peer == null) {
377
parent.addNotify();
378
}
379
if (peer == null)
380
peer = getComponentFactory().createFileDialog(this);
381
super.addNotify();
382
}
383
}
384
385
/**
386
* Indicates whether this file dialog box is for loading from a file
387
* or for saving to a file.
388
*
389
* @return the mode of this file dialog window, either
390
* {@code FileDialog.LOAD} or
391
* {@code FileDialog.SAVE}
392
* @see java.awt.FileDialog#LOAD
393
* @see java.awt.FileDialog#SAVE
394
* @see java.awt.FileDialog#setMode
395
*/
396
public int getMode() {
397
return mode;
398
}
399
400
/**
401
* Sets the mode of the file dialog. If {@code mode} is not
402
* a legal value, an exception will be thrown and {@code mode}
403
* will not be set.
404
*
405
* @param mode the mode for this file dialog, either
406
* {@code FileDialog.LOAD} or
407
* {@code FileDialog.SAVE}
408
* @see java.awt.FileDialog#LOAD
409
* @see java.awt.FileDialog#SAVE
410
* @see java.awt.FileDialog#getMode
411
* @exception IllegalArgumentException if an illegal file
412
* dialog mode is supplied
413
* @since 1.1
414
*/
415
public void setMode(int mode) {
416
switch (mode) {
417
case LOAD:
418
case SAVE:
419
this.mode = mode;
420
break;
421
default:
422
throw new IllegalArgumentException("illegal file dialog mode");
423
}
424
}
425
426
/**
427
* Gets the directory of this file dialog.
428
*
429
* @return the (potentially {@code null} or invalid)
430
* directory of this {@code FileDialog}
431
* @see java.awt.FileDialog#setDirectory
432
*/
433
public String getDirectory() {
434
return dir;
435
}
436
437
/**
438
* Sets the directory of this file dialog window to be the
439
* specified directory. Specifying a {@code null} or an
440
* invalid directory implies an implementation-defined default.
441
* This default will not be realized, however, until the user
442
* has selected a file. Until this point, {@code getDirectory()}
443
* will return the value passed into this method.
444
* <p>
445
* Specifying "" as the directory is exactly equivalent to
446
* specifying {@code null} as the directory.
447
*
448
* @param dir the specified directory
449
* @see java.awt.FileDialog#getDirectory
450
*/
451
public void setDirectory(String dir) {
452
this.dir = (dir != null && dir.isEmpty()) ? null : dir;
453
FileDialogPeer peer = (FileDialogPeer)this.peer;
454
if (peer != null) {
455
peer.setDirectory(this.dir);
456
}
457
}
458
459
/**
460
* Gets the selected file of this file dialog. If the user
461
* selected {@code CANCEL}, the returned file is {@code null}.
462
*
463
* @return the currently selected file of this file dialog window,
464
* or {@code null} if none is selected
465
* @see java.awt.FileDialog#setFile
466
*/
467
public String getFile() {
468
return file;
469
}
470
471
/**
472
* Returns files that the user selects.
473
* <p>
474
* If the user cancels the file dialog,
475
* then the method returns an empty array.
476
*
477
* @return files that the user selects or an empty array
478
* if the user cancels the file dialog.
479
* @see #setFile(String)
480
* @see #getFile
481
* @since 1.7
482
*/
483
public File[] getFiles() {
484
synchronized (getObjectLock()) {
485
if (files != null) {
486
return files.clone();
487
} else {
488
return new File[0];
489
}
490
}
491
}
492
493
/**
494
* Stores the names of all the files that the user selects.
495
*
496
* Note that the method is private and it's intended to be used
497
* by the peers through the AWTAccessor API.
498
*
499
* @param files the array that contains the short names of
500
* all the files that the user selects.
501
*
502
* @see #getFiles
503
* @since 1.7
504
*/
505
private void setFiles(File[] files) {
506
synchronized (getObjectLock()) {
507
this.files = files;
508
}
509
}
510
511
/**
512
* Sets the selected file for this file dialog window to be the
513
* specified file. This file becomes the default file if it is set
514
* before the file dialog window is first shown.
515
* <p>
516
* When the dialog is shown, the specified file is selected. The kind of
517
* selection depends on the file existence, the dialog type, and the native
518
* platform. E.g., the file could be highlighted in the file list, or a
519
* file name editbox could be populated with the file name.
520
* <p>
521
* This method accepts either a full file path, or a file name with an
522
* extension if used together with the {@code setDirectory} method.
523
* <p>
524
* Specifying "" as the file is exactly equivalent to specifying
525
* {@code null} as the file.
526
*
527
* @param file the file being set
528
* @see #getFile
529
* @see #getFiles
530
*/
531
public void setFile(String file) {
532
this.file = (file != null && file.isEmpty()) ? null : file;
533
FileDialogPeer peer = (FileDialogPeer)this.peer;
534
if (peer != null) {
535
peer.setFile(this.file);
536
}
537
}
538
539
/**
540
* Enables or disables multiple file selection for the file dialog.
541
*
542
* @param enable if {@code true}, multiple file selection is enabled;
543
* {@code false} - disabled.
544
* @see #isMultipleMode
545
* @since 1.7
546
*/
547
public void setMultipleMode(boolean enable) {
548
synchronized (getObjectLock()) {
549
this.multipleMode = enable;
550
}
551
}
552
553
/**
554
* Returns whether the file dialog allows the multiple file selection.
555
*
556
* @return {@code true} if the file dialog allows the multiple
557
* file selection; {@code false} otherwise.
558
* @see #setMultipleMode
559
* @since 1.7
560
*/
561
public boolean isMultipleMode() {
562
synchronized (getObjectLock()) {
563
return multipleMode;
564
}
565
}
566
567
/**
568
* Determines this file dialog's filename filter. A filename filter
569
* allows the user to specify which files appear in the file dialog
570
* window. Filename filters do not function in Sun's reference
571
* implementation for Microsoft Windows.
572
*
573
* @return this file dialog's filename filter
574
* @see java.io.FilenameFilter
575
* @see java.awt.FileDialog#setFilenameFilter
576
*/
577
public FilenameFilter getFilenameFilter() {
578
return filter;
579
}
580
581
/**
582
* Sets the filename filter for this file dialog window to the
583
* specified filter.
584
* Filename filters do not function in Sun's reference
585
* implementation for Microsoft Windows.
586
*
587
* @param filter the specified filter
588
* @see java.io.FilenameFilter
589
* @see java.awt.FileDialog#getFilenameFilter
590
*/
591
public synchronized void setFilenameFilter(FilenameFilter filter) {
592
this.filter = filter;
593
FileDialogPeer peer = (FileDialogPeer)this.peer;
594
if (peer != null) {
595
peer.setFilenameFilter(filter);
596
}
597
}
598
599
/**
600
* Reads the {@code ObjectInputStream} and performs
601
* a backwards compatibility check by converting
602
* either a {@code dir} or a {@code file}
603
* equal to an empty string to {@code null}.
604
*
605
* @param s the {@code ObjectInputStream} to read
606
* @throws ClassNotFoundException if the class of a serialized object could
607
* not be found
608
* @throws IOException if an I/O error occurs
609
*/
610
@Serial
611
private void readObject(ObjectInputStream s)
612
throws ClassNotFoundException, IOException
613
{
614
s.defaultReadObject();
615
616
// 1.1 Compatibility: "" is not converted to null in 1.1
617
if (dir != null && dir.isEmpty()) {
618
dir = null;
619
}
620
if (file != null && file.isEmpty()) {
621
file = null;
622
}
623
}
624
625
/**
626
* Returns a string representing the state of this {@code FileDialog}
627
* window. This method is intended to be used only for debugging purposes,
628
* and the content and format of the returned string may vary between
629
* implementations. The returned string may be empty but may not be
630
* {@code null}.
631
*
632
* @return the parameter string of this file dialog window
633
*/
634
protected String paramString() {
635
String str = super.paramString();
636
str += ",dir= " + dir;
637
str += ",file= " + file;
638
return str + ((mode == LOAD) ? ",load" : ",save");
639
}
640
641
boolean postsOldMouseEvents() {
642
return false;
643
}
644
}
645
646