forked from openjdk/jdk
/
FileSystemView.java
978 lines (863 loc) · 31.4 KB
/
FileSystemView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
/*
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.swing.filechooser;
import java.awt.Image;
import java.awt.image.AbstractMultiResolutionImage;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import jdk.internal.ref.CleanerFactory;
import sun.awt.shell.ShellFolder;
/**
* FileSystemView is JFileChooser's gateway to the
* file system. Since the JDK1.1 File API doesn't allow
* access to such information as root partitions, file type
* information, or hidden file bits, this class is designed
* to intuit as much OS-specific file system information as
* possible.
*
* <p>
*
* Java Licensees may want to provide a different implementation of
* FileSystemView to better handle a given operating system.
*
* @author Jeff Dinkins
*/
// PENDING(jeff) - need to provide a specification for
// how Mac/OS2/BeOS/etc file systems can modify FileSystemView
// to handle their particular type of file system.
public abstract class FileSystemView {
static FileSystemView windowsFileSystemView = null;
static FileSystemView unixFileSystemView = null;
//static FileSystemView macFileSystemView = null;
static FileSystemView genericFileSystemView = null;
private boolean useSystemExtensionHiding =
UIManager.getDefaults().getBoolean("FileChooser.useSystemExtensionHiding");
/**
* Returns the file system view.
* @return the file system view
*/
public static FileSystemView getFileSystemView() {
if(File.separatorChar == '\\') {
if(windowsFileSystemView == null) {
windowsFileSystemView = new WindowsFileSystemView();
}
return windowsFileSystemView;
}
if(File.separatorChar == '/') {
if(unixFileSystemView == null) {
unixFileSystemView = new UnixFileSystemView();
}
return unixFileSystemView;
}
// if(File.separatorChar == ':') {
// if(macFileSystemView == null) {
// macFileSystemView = new MacFileSystemView();
// }
// return macFileSystemView;
//}
if(genericFileSystemView == null) {
genericFileSystemView = new GenericFileSystemView();
}
return genericFileSystemView;
}
/**
* Constructs a FileSystemView.
*/
public FileSystemView() {
final WeakReference<FileSystemView> weakReference = new WeakReference<>(this);
final PropertyChangeListener pcl = evt -> {
final FileSystemView fsv = weakReference.get();
if (fsv != null && evt.getPropertyName().equals("lookAndFeel")) {
fsv.useSystemExtensionHiding =
UIManager.getDefaults().getBoolean(
"FileChooser.useSystemExtensionHiding");
}
};
UIManager.addPropertyChangeListener(pcl);
CleanerFactory.cleaner().register(this, () -> {
UIManager.removePropertyChangeListener(pcl);
});
}
/**
* Determines if the given file is a root in the navigable tree(s).
* Examples: Windows 98 has one root, the Desktop folder. DOS has one root
* per drive letter, <code>C:\</code>, <code>D:\</code>, etc. Unix has one root,
* the <code>"/"</code> directory.
*
* The default implementation gets information from the <code>ShellFolder</code> class.
*
* @param f a <code>File</code> object representing a directory
* @return <code>true</code> if <code>f</code> is a root in the navigable tree.
* @see #isFileSystemRoot
*/
public boolean isRoot(File f) {
if (f == null || !f.isAbsolute()) {
return false;
}
File[] roots = getRoots();
for (File root : roots) {
if (root.equals(f)) {
return true;
}
}
return false;
}
/**
* Returns true if the file (directory) can be visited.
* Returns false if the directory cannot be traversed.
*
* @param f the <code>File</code>
* @return <code>true</code> if the file/directory can be traversed, otherwise <code>false</code>
* @see JFileChooser#isTraversable
* @see FileView#isTraversable
* @since 1.4
*/
public Boolean isTraversable(File f) {
return Boolean.valueOf(f.isDirectory());
}
/**
* Name of a file, directory, or folder as it would be displayed in
* a system file browser. Example from Windows: the "M:\" directory
* displays as "CD-ROM (M:)"
*
* The default implementation gets information from the ShellFolder class.
*
* @param f a <code>File</code> object
* @return the file name as it would be displayed by a native file chooser
* @see JFileChooser#getName
* @since 1.4
*/
public String getSystemDisplayName(File f) {
if (f == null) {
return null;
}
String name = f.getName();
if (!name.equals("..") && !name.equals(".") &&
(useSystemExtensionHiding || !isFileSystem(f) || isFileSystemRoot(f)) &&
(f instanceof ShellFolder || f.exists())) {
try {
name = getShellFolder(f).getDisplayName();
} catch (FileNotFoundException e) {
return null;
}
if (name == null || name.length() == 0) {
name = f.getPath(); // e.g. "/"
}
}
return name;
}
/**
* Type description for a file, directory, or folder as it would be displayed in
* a system file browser. Example from Windows: the "Desktop" folder
* is described as "Desktop".
*
* Override for platforms with native ShellFolder implementations.
*
* @param f a <code>File</code> object
* @return the file type description as it would be displayed by a native file chooser
* or null if no native information is available.
* @see JFileChooser#getTypeDescription
* @since 1.4
*/
public String getSystemTypeDescription(File f) {
return null;
}
/**
* Icon for a file, directory, or folder as it would be displayed in
* a system file browser. Example from Windows: the "M:\" directory
* displays a CD-ROM icon.
* <p>
* The default implementation gets information from the ShellFolder class.
*
* @param f a <code>File</code> object
* @return an icon as it would be displayed by a native file chooser
* @see JFileChooser#getIcon
* @since 1.4
*/
public Icon getSystemIcon(File f) {
if (f == null) {
return null;
}
ShellFolder sf;
try {
sf = getShellFolder(f);
} catch (FileNotFoundException e) {
return null;
}
Image img = sf.getIcon(false);
if (img != null) {
return new ImageIcon(img, sf.getFolderType());
} else {
return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
}
}
/**
* Returns an icon for a file, directory, or folder as it would be displayed
* in a system file browser for the requested size.
* <p>
* The default implementation gets information from the
* {@code ShellFolder} class. Whenever possible, the icon
* returned will be a multi-resolution icon image,
* which will allow better scaling with different
* magnification factors.
* <p>
* Example: <pre>
* FileSystemView fsv = FileSystemView.getFileSystemView();
* Icon icon = fsv.getSystemIcon("application.exe", 64);
* JLabel label = new JLabel(icon);
* </pre>
*
* @param f a <code>File</code> object
* @param size width and height of the icon in virtual pixels
* @return an icon as it would be displayed by a native file chooser
* or null if invalid parameters are passed such as pointer to a
* non-existing file.
* @see JFileChooser#getIcon
* @see AbstractMultiResolutionImage
* @since 17
*/
public Icon getSystemIcon(File f, int size) {
if (f == null) {
return null;
}
ShellFolder sf;
try {
sf = ShellFolder.getShellFolder(f);
} catch (FileNotFoundException e) {
return null;
}
Image img = sf.getIcon(size);
if (img != null) {
return new ImageIcon(img, sf.getFolderType());
} else {
return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon"
: "FileView.fileIcon");
}
}
/**
* On Windows, a file can appear in multiple folders, other than its
* parent directory in the filesystem. Folder could for example be the
* "Desktop" folder which is not the same as file.getParentFile().
*
* @param folder a <code>File</code> object representing a directory or special folder
* @param file a <code>File</code> object
* @return <code>true</code> if <code>folder</code> is a directory or special folder and contains <code>file</code>.
* @since 1.4
*/
public boolean isParent(File folder, File file) {
if (folder == null || file == null) {
return false;
} else if (folder instanceof ShellFolder) {
File parent = file.getParentFile();
if (parent != null && parent.equals(folder)) {
return true;
}
File[] children = getFiles(folder, false);
for (File child : children) {
if (file.equals(child)) {
return true;
}
}
return false;
} else {
return folder.equals(file.getParentFile());
}
}
/**
*
* @param parent a <code>File</code> object representing a directory or special folder
* @param fileName a name of a file or folder which exists in <code>parent</code>
* @return a File object. This is normally constructed with <code>new
* File(parent, fileName)</code> except when parent and child are both
* special folders, in which case the <code>File</code> is a wrapper containing
* a <code>ShellFolder</code> object.
* @since 1.4
*/
public File getChild(File parent, String fileName) {
if (parent instanceof ShellFolder) {
File[] children = getFiles(parent, false);
for (File child : children) {
if (child.getName().equals(fileName)) {
return child;
}
}
}
return createFileObject(parent, fileName);
}
/**
* Checks if <code>f</code> represents a real directory or file as opposed to a
* special folder such as <code>"Desktop"</code>. Used by UI classes to decide if
* a folder is selectable when doing directory choosing.
*
* @param f a <code>File</code> object
* @return <code>true</code> if <code>f</code> is a real file or directory.
* @since 1.4
*/
public boolean isFileSystem(File f) {
if (f instanceof ShellFolder) {
ShellFolder sf = (ShellFolder)f;
// Shortcuts to directories are treated as not being file system objects,
// so that they are never returned by JFileChooser.
return sf.isFileSystem() && !(sf.isLink() && sf.isDirectory());
} else {
return true;
}
}
/**
* Creates a new folder with a default folder name.
*
* @param containingDir a {@code File} object denoting directory to contain the new folder
* @return a {@code File} object denoting the newly created folder
* @throws IOException if new folder could not be created
*/
public abstract File createNewFolder(File containingDir) throws IOException;
/**
* Returns whether a file is hidden or not.
*
* @param f a {@code File} object
* @return true if the given {@code File} denotes a hidden file
*/
public boolean isHiddenFile(File f) {
return f.isHidden();
}
/**
* Is dir the root of a tree in the file system, such as a drive
* or partition. Example: Returns true for "C:\" on Windows 98.
*
* @param dir a <code>File</code> object representing a directory
* @return <code>true</code> if <code>f</code> is a root of a filesystem
* @see #isRoot
* @since 1.4
*/
public boolean isFileSystemRoot(File dir) {
return ShellFolder.isFileSystemRoot(dir);
}
/**
* Used by UI classes to decide whether to display a special icon
* for drives or partitions, e.g. a "hard disk" icon.
*
* The default implementation has no way of knowing, so always returns false.
*
* @param dir a directory
* @return <code>false</code> always
* @since 1.4
*/
public boolean isDrive(File dir) {
return false;
}
/**
* Used by UI classes to decide whether to display a special icon
* for a floppy disk. Implies isDrive(dir).
*
* The default implementation has no way of knowing, so always returns false.
*
* @param dir a directory
* @return <code>false</code> always
* @since 1.4
*/
public boolean isFloppyDrive(File dir) {
return false;
}
/**
* Used by UI classes to decide whether to display a special icon
* for a computer node, e.g. "My Computer" or a network server.
*
* The default implementation has no way of knowing, so always returns false.
*
* @param dir a directory
* @return <code>false</code> always
* @since 1.4
*/
public boolean isComputerNode(File dir) {
return ShellFolder.isComputerNode(dir);
}
/**
* Returns all root partitions on this system. For example, on
* Windows, this would be the "Desktop" folder, while on DOS this
* would be the A: through Z: drives.
*
* @return an array of {@code File} objects representing all root partitions
* on this system
*/
public File[] getRoots() {
// Don't cache this array, because filesystem might change
File[] roots = (File[])ShellFolder.get("roots");
for (int i = 0; i < roots.length; i++) {
if (isFileSystemRoot(roots[i])) {
roots[i] = createFileSystemRoot(roots[i]);
}
}
return roots;
}
// Providing default implementations for the remaining methods
// because most OS file systems will likely be able to use this
// code. If a given OS can't, override these methods in its
// implementation.
/**
* Returns the home directory.
* @return the home directory
*/
public File getHomeDirectory() {
return createFileObject(System.getProperty("user.home"));
}
/**
* Return the user's default starting directory for the file chooser.
*
* @return a <code>File</code> object representing the default
* starting folder
* @since 1.4
*/
public File getDefaultDirectory() {
File f = (File)ShellFolder.get("fileChooserDefaultFolder");
if (isFileSystemRoot(f)) {
f = createFileSystemRoot(f);
}
return f;
}
/**
* Returns a File object constructed in dir from the given filename.
*
* @param dir an abstract pathname denoting a directory
* @param filename a {@code String} representation of a pathname
* @return a {@code File} object created from {@code dir} and {@code filename}
*/
public File createFileObject(File dir, String filename) {
if(dir == null) {
return new File(filename);
} else {
return new File(dir, filename);
}
}
/**
* Returns a File object constructed from the given path string.
*
* @param path {@code String} representation of path
* @return a {@code File} object created from the given {@code path}
*/
public File createFileObject(String path) {
File f = new File(path);
if (isFileSystemRoot(f)) {
f = createFileSystemRoot(f);
}
return f;
}
/**
* Gets the list of shown (i.e. not hidden) files.
*
* @param dir the root directory of files to be returned
* @param useFileHiding determine if hidden files are returned
* @return an array of {@code File} objects representing files and
* directories in the given {@code dir}. It includes hidden
* files if {@code useFileHiding} is false.
*/
public File[] getFiles(File dir, boolean useFileHiding) {
List<File> files = new ArrayList<File>();
// add all files in dir
if (!(dir instanceof ShellFolder)) {
try {
dir = getShellFolder(dir);
} catch (FileNotFoundException e) {
return new File[0];
}
}
File[] names = ((ShellFolder) dir).listFiles(!useFileHiding);
if (names == null) {
return new File[0];
}
for (File f : names) {
if (Thread.currentThread().isInterrupted()) {
break;
}
if (!(f instanceof ShellFolder)) {
if (isFileSystemRoot(f)) {
f = createFileSystemRoot(f);
}
try {
f = ShellFolder.getShellFolder(f);
} catch (FileNotFoundException e) {
// Not a valid file (wouldn't show in native file chooser)
// Example: C:\pagefile.sys
continue;
} catch (InternalError e) {
// Not a valid file (wouldn't show in native file chooser)
// Example C:\Winnt\Profiles\joe\history\History.IE5
continue;
}
}
if (!useFileHiding || !isHiddenFile(f)) {
files.add(f);
}
}
return files.toArray(new File[files.size()]);
}
/**
* Returns the parent directory of <code>dir</code>.
* @param dir the <code>File</code> being queried
* @return the parent directory of <code>dir</code>, or
* <code>null</code> if <code>dir</code> is <code>null</code>
*/
public File getParentDirectory(File dir) {
if (dir == null || !dir.exists()) {
return null;
}
ShellFolder sf;
try {
sf = getShellFolder(dir);
} catch (FileNotFoundException e) {
return null;
}
File psf = sf.getParentFile();
if (psf == null) {
return null;
}
if (isFileSystem(psf)) {
File f = psf;
if (!f.exists()) {
// This could be a node under "Network Neighborhood".
File ppsf = psf.getParentFile();
if (ppsf == null || !isFileSystem(ppsf)) {
// We're mostly after the exists() override for windows below.
f = createFileSystemRoot(f);
}
}
return f;
} else {
return psf;
}
}
/**
* Returns an array of files representing the values which will be shown
* in the file chooser selector.
*
* @return an array of {@code File} objects. The array returned may be
* possibly empty if there are no appropriate permissions.
* @since 9
*/
public File[] getChooserComboBoxFiles() {
return (File[]) ShellFolder.get("fileChooserComboBoxFolders");
}
/**
* Returns an array of files representing the values to show by default in
* the file chooser shortcuts panel.
*
* @return an array of {@code File} objects. The array returned may be
* possibly empty if there are no appropriate permissions.
* @since 12
*/
final public File[] getChooserShortcutPanelFiles() {
return (File[]) ShellFolder.get("fileChooserShortcutPanelFolders");
}
/**
* Returns whether the specified file denotes a shell interpreted link which
* can be obtained by the {@link #getLinkLocation(File)}.
*
* @param file a file
* @return whether this is a link
* @throws NullPointerException if {@code file} equals {@code null}
* @throws SecurityException if the caller does not have necessary
* permissions
* @see #getLinkLocation(File)
* @since 9
*/
public boolean isLink(File file) {
if (file == null) {
throw new NullPointerException("file is null");
}
try {
return ShellFolder.getShellFolder(file).isLink();
} catch (FileNotFoundException e) {
return false;
}
}
/**
* Returns the regular file referenced by the specified link file if
* the specified file is a shell interpreted link.
* Returns {@code null} if the specified file is not
* a shell interpreted link.
*
* @param file a file
* @return the linked file or {@code null}.
* @throws FileNotFoundException if the linked file does not exist
* @throws NullPointerException if {@code file} equals {@code null}
* @throws SecurityException if the caller does not have necessary
* permissions
* @since 9
*/
public File getLinkLocation(File file) throws FileNotFoundException {
if (file == null) {
throw new NullPointerException("file is null");
}
ShellFolder shellFolder;
try {
shellFolder = ShellFolder.getShellFolder(file);
} catch (FileNotFoundException e) {
return null;
}
return shellFolder.isLink() ? shellFolder.getLinkLocation() : null;
}
/**
* Throws {@code FileNotFoundException} if file not found or current thread was interrupted
*/
ShellFolder getShellFolder(File f) throws FileNotFoundException {
if (!(f instanceof ShellFolder) && !(f instanceof FileSystemRoot) && isFileSystemRoot(f)) {
f = createFileSystemRoot(f);
}
try {
return ShellFolder.getShellFolder(f);
} catch (InternalError e) {
System.err.println("FileSystemView.getShellFolder: f="+f);
e.printStackTrace();
return null;
}
}
/**
* Creates a new <code>File</code> object for <code>f</code> with correct
* behavior for a file system root directory.
*
* @param f a <code>File</code> object representing a file system root
* directory, for example "/" on Unix or "C:\" on Windows.
* @return a new <code>File</code> object
* @since 1.4
*/
protected File createFileSystemRoot(File f) {
return new FileSystemRoot(f);
}
@SuppressWarnings("serial") // Same-version serialization only
static class FileSystemRoot extends File {
public FileSystemRoot(File f) {
super(f,"");
}
public FileSystemRoot(String s) {
super(s);
}
public boolean isDirectory() {
return true;
}
public String getName() {
return getPath();
}
}
}
/**
* FileSystemView that handles some specific unix-isms.
*/
class UnixFileSystemView extends FileSystemView {
private static final String newFolderString =
UIManager.getString("FileChooser.other.newFolder");
private static final String newFolderNextString =
UIManager.getString("FileChooser.other.newFolder.subsequent");
/**
* Creates a new folder with a default folder name.
*/
public File createNewFolder(File containingDir) throws IOException {
if(containingDir == null) {
throw new IOException("Containing directory is null:");
}
File newFolder;
// Unix - using OpenWindows' default folder name. Can't find one for Motif/CDE.
newFolder = createFileObject(containingDir, newFolderString);
int i = 1;
while (newFolder.exists() && i < 100) {
newFolder = createFileObject(containingDir, MessageFormat.format(
newFolderNextString, i));
i++;
}
if(newFolder.exists()) {
throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
} else {
if(!newFolder.mkdirs()) {
throw new IOException(newFolder.getAbsolutePath());
}
}
return newFolder;
}
public boolean isFileSystemRoot(File dir) {
return dir != null && dir.getAbsolutePath().equals("/");
}
public boolean isDrive(File dir) {
return isFloppyDrive(dir);
}
public boolean isFloppyDrive(File dir) {
// Could be looking at the path for Solaris, but wouldn't be reliable.
// For example:
// return (dir != null && dir.getAbsolutePath().toLowerCase().startsWith("/floppy"));
return false;
}
public boolean isComputerNode(File dir) {
if (dir != null) {
String parent = dir.getParent();
if (parent != null && parent.equals("/net")) {
return true;
}
}
return false;
}
}
/**
* FileSystemView that handles some specific windows concepts.
*/
class WindowsFileSystemView extends FileSystemView {
private static final String newFolderString =
UIManager.getString("FileChooser.win32.newFolder");
private static final String newFolderNextString =
UIManager.getString("FileChooser.win32.newFolder.subsequent");
public Boolean isTraversable(File f) {
return Boolean.valueOf(isFileSystemRoot(f) || isComputerNode(f) || f.isDirectory());
}
public File getChild(File parent, String fileName) {
if (fileName.startsWith("\\")
&& !fileName.startsWith("\\\\")
&& isFileSystem(parent)) {
//Path is relative to the root of parent's drive
String path = parent.getAbsolutePath();
if (path.length() >= 2
&& path.charAt(1) == ':'
&& Character.isLetter(path.charAt(0))) {
return createFileObject(path.substring(0, 2) + fileName);
}
}
return super.getChild(parent, fileName);
}
/**
* Type description for a file, directory, or folder as it would be displayed in
* a system file browser. Example from Windows: the "Desktop" folder
* is described as "Desktop".
*
* The Windows implementation gets information from the ShellFolder class.
*/
public String getSystemTypeDescription(File f) {
if (f == null) {
return null;
}
try {
return getShellFolder(f).getFolderType();
} catch (FileNotFoundException e) {
return null;
}
}
/**
* @return the Desktop folder.
*/
public File getHomeDirectory() {
File[] roots = getRoots();
return (roots.length == 0) ? null : roots[0];
}
/**
* Creates a new folder with a default folder name.
*/
public File createNewFolder(File containingDir) throws IOException {
if(containingDir == null) {
throw new IOException("Containing directory is null:");
}
// Using NT's default folder name
File newFolder = createFileObject(containingDir, newFolderString);
int i = 2;
while (newFolder.exists() && i < 100) {
newFolder = createFileObject(containingDir, MessageFormat.format(
newFolderNextString, i));
i++;
}
if(newFolder.exists()) {
throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
} else {
if(!newFolder.mkdirs()) {
throw new IOException(newFolder.getAbsolutePath());
}
}
return newFolder;
}
public boolean isDrive(File dir) {
return isFileSystemRoot(dir);
}
public boolean isFloppyDrive(final File dir) {
String path = AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return dir.getAbsolutePath();
}
});
return path != null && (path.equals("A:\\") || path.equals("B:\\"));
}
/**
* Returns a File object constructed from the given path string.
*/
public File createFileObject(String path) {
// Check for missing backslash after drive letter such as "C:" or "C:filename"
if (path.length() >= 2 && path.charAt(1) == ':' && Character.isLetter(path.charAt(0))) {
if (path.length() == 2) {
path += "\\";
} else if (path.charAt(2) != '\\') {
path = path.substring(0, 2) + "\\" + path.substring(2);
}
}
return super.createFileObject(path);
}
@SuppressWarnings("serial") // anonymous class
protected File createFileSystemRoot(File f) {
// Problem: Removable drives on Windows return false on f.exists()
// Workaround: Override exists() to always return true.
return new FileSystemRoot(f) {
public boolean exists() {
return true;
}
};
}
}
/**
* Fallthrough FileSystemView in case we can't determine the OS.
*/
class GenericFileSystemView extends FileSystemView {
private static final String newFolderString =
UIManager.getString("FileChooser.other.newFolder");
/**
* Creates a new folder with a default folder name.
*/
public File createNewFolder(File containingDir) throws IOException {
if(containingDir == null) {
throw new IOException("Containing directory is null:");
}
// Using NT's default folder name
File newFolder = createFileObject(containingDir, newFolderString);
if(newFolder.exists()) {
throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
} else {
if(!newFolder.mkdirs()) {
throw new IOException(newFolder.getAbsolutePath());
}
}
return newFolder;
}
}