114 }
115
116 /**
117 * Determines if the given file is a root in the navigatable tree(s).
118 * Examples: Windows 98 has one root, the Desktop folder. DOS has one root
119 * per drive letter, <code>C:\</code>, <code>D:\</code>, etc. Unix has one root,
120 * the <code>"/"</code> directory.
121 *
122 * The default implementation gets information from the <code>ShellFolder</code> class.
123 *
124 * @param f a <code>File</code> object representing a directory
125 * @return <code>true</code> if <code>f</code> is a root in the navigatable tree.
126 * @see #isFileSystemRoot
127 */
128 public boolean isRoot(File f) {
129 if (f == null || !f.isAbsolute()) {
130 return false;
131 }
132
133 File[] roots = getRoots();
134 for (int i = 0; i < roots.length; i++) {
135 if (roots[i].equals(f)) {
136 return true;
137 }
138 }
139 return false;
140 }
141
142 /**
143 * Returns true if the file (directory) can be visited.
144 * Returns false if the directory cannot be traversed.
145 *
146 * @param f the <code>File</code>
147 * @return <code>true</code> if the file/directory can be traversed, otherwise <code>false</code>
148 * @see JFileChooser#isTraversable
149 * @see FileView#isTraversable
150 * @since 1.4
151 */
152 public Boolean isTraversable(File f) {
153 return Boolean.valueOf(f.isDirectory());
154 }
155
230
231 /**
232 * On Windows, a file can appear in multiple folders, other than its
233 * parent directory in the filesystem. Folder could for example be the
234 * "Desktop" folder which is not the same as file.getParentFile().
235 *
236 * @param folder a <code>File</code> object repesenting a directory or special folder
237 * @param file a <code>File</code> object
238 * @return <code>true</code> if <code>folder</code> is a directory or special folder and contains <code>file</code>.
239 * @since 1.4
240 */
241 public boolean isParent(File folder, File file) {
242 if (folder == null || file == null) {
243 return false;
244 } else if (folder instanceof ShellFolder) {
245 File parent = file.getParentFile();
246 if (parent != null && parent.equals(folder)) {
247 return true;
248 }
249 File[] children = getFiles(folder, false);
250 for (int i = 0; i < children.length; i++) {
251 if (file.equals(children[i])) {
252 return true;
253 }
254 }
255 return false;
256 } else {
257 return folder.equals(file.getParentFile());
258 }
259 }
260
261 /**
262 *
263 * @param parent a <code>File</code> object repesenting a directory or special folder
264 * @param fileName a name of a file or folder which exists in <code>parent</code>
265 * @return a File object. This is normally constructed with <code>new
266 * File(parent, fileName)</code> except when parent and child are both
267 * special folders, in which case the <code>File</code> is a wrapper containing
268 * a <code>ShellFolder</code> object.
269 * @since 1.4
270 */
271 public File getChild(File parent, String fileName) {
272 if (parent instanceof ShellFolder) {
273 File[] children = getFiles(parent, false);
274 for (int i = 0; i < children.length; i++) {
275 if (children[i].getName().equals(fileName)) {
276 return children[i];
277 }
278 }
279 }
280 return createFileObject(parent, fileName);
281 }
282
283
284 /**
285 * Checks if <code>f</code> represents a real directory or file as opposed to a
286 * special folder such as <code>"Desktop"</code>. Used by UI classes to decide if
287 * a folder is selectable when doing directory choosing.
288 *
289 * @param f a <code>File</code> object
290 * @return <code>true</code> if <code>f</code> is a real file or directory.
291 * @since 1.4
292 */
293 public boolean isFileSystem(File f) {
294 if (f instanceof ShellFolder) {
295 ShellFolder sf = (ShellFolder)f;
296 // Shortcuts to directories are treated as not being file system objects,
422 return new File(dir, filename);
423 }
424 }
425
426 /**
427 * Returns a File object constructed from the given path string.
428 */
429 public File createFileObject(String path) {
430 File f = new File(path);
431 if (isFileSystemRoot(f)) {
432 f = createFileSystemRoot(f);
433 }
434 return f;
435 }
436
437
438 /**
439 * Gets the list of shown (i.e. not hidden) files.
440 */
441 public File[] getFiles(File dir, boolean useFileHiding) {
442 Vector files = new Vector();
443
444
445 // add all files in dir
446 File[] names;
447 if (!(dir instanceof ShellFolder)) {
448 dir = getShellFolder(dir);
449 }
450
451 names = ((ShellFolder)dir).listFiles(!useFileHiding);
452 File f;
453
454 int nameCount = (names == null) ? 0 : names.length;
455 for (int i = 0; i < nameCount; i++) {
456 if (Thread.currentThread().isInterrupted()) {
457 break;
458 }
459 f = names[i];
460 if (!(f instanceof ShellFolder)) {
461 if (isFileSystemRoot(f)) {
462 f = createFileSystemRoot(f);
463 }
464 try {
465 f = ShellFolder.getShellFolder(f);
466 } catch (FileNotFoundException e) {
467 // Not a valid file (wouldn't show in native file chooser)
468 // Example: C:\pagefile.sys
469 continue;
470 } catch (InternalError e) {
471 // Not a valid file (wouldn't show in native file chooser)
472 // Example C:\Winnt\Profiles\joe\history\History.IE5
473 continue;
474 }
475 }
476 if (!useFileHiding || !isHiddenFile(f)) {
477 files.addElement(f);
478 }
479 }
480
481 return (File[])files.toArray(new File[files.size()]);
482 }
483
484
485
486 /**
487 * Returns the parent directory of <code>dir</code>.
488 * @param dir the <code>File</code> being queried
489 * @return the parent directory of <code>dir</code>, or
490 * <code>null</code> if <code>dir</code> is <code>null</code>
491 */
492 public File getParentDirectory(File dir) {
493 if (dir != null && dir.exists()) {
494 ShellFolder sf = getShellFolder(dir);
495 File psf = sf.getParentFile();
496 if (psf != null) {
497 if (isFileSystem(psf)) {
498 File f = psf;
499 if (f != null && !f.exists()) {
500 // This could be a node under "Network Neighborhood".
501 File ppsf = psf.getParentFile();
568 }
569 }
570
571 /**
572 * FileSystemView that handles some specific unix-isms.
573 */
574 class UnixFileSystemView extends FileSystemView {
575
576 private static final String newFolderString =
577 UIManager.getString("FileChooser.other.newFolder");
578 private static final String newFolderNextString =
579 UIManager.getString("FileChooser.other.newFolder.subsequent");
580
581 /**
582 * Creates a new folder with a default folder name.
583 */
584 public File createNewFolder(File containingDir) throws IOException {
585 if(containingDir == null) {
586 throw new IOException("Containing directory is null:");
587 }
588 File newFolder = null;
589 // Unix - using OpenWindows' default folder name. Can't find one for Motif/CDE.
590 newFolder = createFileObject(containingDir, newFolderString);
591 int i = 1;
592 while (newFolder.exists() && (i < 100)) {
593 newFolder = createFileObject(containingDir, MessageFormat.format(
594 newFolderNextString, new Object[] { new Integer(i) }));
595 i++;
596 }
597
598 if(newFolder.exists()) {
599 throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
600 } else {
601 newFolder.mkdirs();
602 }
603
604 return newFolder;
605 }
606
607 public boolean isFileSystemRoot(File dir) {
608 return (dir != null && dir.getAbsolutePath().equals("/"));
609 }
610
611 public boolean isDrive(File dir) {
612 if (isFloppyDrive(dir)) {
613 return true;
614 } else {
615 return false;
616 }
617 }
618
619 public boolean isFloppyDrive(File dir) {
620 // Could be looking at the path for Solaris, but wouldn't be reliable.
621 // For example:
622 // return (dir != null && dir.getAbsolutePath().toLowerCase().startsWith("/floppy"));
623 return false;
624 }
625
626 public boolean isComputerNode(File dir) {
627 if (dir != null) {
628 String parent = dir.getParent();
629 if (parent != null && parent.equals("/net")) {
630 return true;
631 }
632 }
633 return false;
634 }
635 }
636
679 } else {
680 return null;
681 }
682 }
683
684 /**
685 * @return the Desktop folder.
686 */
687 public File getHomeDirectory() {
688 File[] roots = getRoots();
689 return (roots.length == 0) ? null : roots[0];
690 }
691
692 /**
693 * Creates a new folder with a default folder name.
694 */
695 public File createNewFolder(File containingDir) throws IOException {
696 if(containingDir == null) {
697 throw new IOException("Containing directory is null:");
698 }
699 File newFolder = null;
700 // Using NT's default folder name
701 newFolder = createFileObject(containingDir, newFolderString);
702 int i = 2;
703 while (newFolder.exists() && (i < 100)) {
704 newFolder = createFileObject(containingDir, MessageFormat.format(
705 newFolderNextString, new Object[] { new Integer(i) }));
706 i++;
707 }
708
709 if(newFolder.exists()) {
710 throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
711 } else {
712 newFolder.mkdirs();
713 }
714
715 return newFolder;
716 }
717
718 public boolean isDrive(File dir) {
719 return isFileSystemRoot(dir);
720 }
721
749 };
750 }
751
752 }
753
754 /**
755 * Fallthrough FileSystemView in case we can't determine the OS.
756 */
757 class GenericFileSystemView extends FileSystemView {
758
759 private static final String newFolderString =
760 UIManager.getString("FileChooser.other.newFolder");
761
762 /**
763 * Creates a new folder with a default folder name.
764 */
765 public File createNewFolder(File containingDir) throws IOException {
766 if(containingDir == null) {
767 throw new IOException("Containing directory is null:");
768 }
769 File newFolder = null;
770 // Using NT's default folder name
771 newFolder = createFileObject(containingDir, newFolderString);
772
773 if(newFolder.exists()) {
774 throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
775 } else {
776 newFolder.mkdirs();
777 }
778
779 return newFolder;
780 }
781
782 }
|
114 }
115
116 /**
117 * Determines if the given file is a root in the navigatable tree(s).
118 * Examples: Windows 98 has one root, the Desktop folder. DOS has one root
119 * per drive letter, <code>C:\</code>, <code>D:\</code>, etc. Unix has one root,
120 * the <code>"/"</code> directory.
121 *
122 * The default implementation gets information from the <code>ShellFolder</code> class.
123 *
124 * @param f a <code>File</code> object representing a directory
125 * @return <code>true</code> if <code>f</code> is a root in the navigatable tree.
126 * @see #isFileSystemRoot
127 */
128 public boolean isRoot(File f) {
129 if (f == null || !f.isAbsolute()) {
130 return false;
131 }
132
133 File[] roots = getRoots();
134 for (File root : roots) {
135 if (root.equals(f)) {
136 return true;
137 }
138 }
139 return false;
140 }
141
142 /**
143 * Returns true if the file (directory) can be visited.
144 * Returns false if the directory cannot be traversed.
145 *
146 * @param f the <code>File</code>
147 * @return <code>true</code> if the file/directory can be traversed, otherwise <code>false</code>
148 * @see JFileChooser#isTraversable
149 * @see FileView#isTraversable
150 * @since 1.4
151 */
152 public Boolean isTraversable(File f) {
153 return Boolean.valueOf(f.isDirectory());
154 }
155
230
231 /**
232 * On Windows, a file can appear in multiple folders, other than its
233 * parent directory in the filesystem. Folder could for example be the
234 * "Desktop" folder which is not the same as file.getParentFile().
235 *
236 * @param folder a <code>File</code> object repesenting a directory or special folder
237 * @param file a <code>File</code> object
238 * @return <code>true</code> if <code>folder</code> is a directory or special folder and contains <code>file</code>.
239 * @since 1.4
240 */
241 public boolean isParent(File folder, File file) {
242 if (folder == null || file == null) {
243 return false;
244 } else if (folder instanceof ShellFolder) {
245 File parent = file.getParentFile();
246 if (parent != null && parent.equals(folder)) {
247 return true;
248 }
249 File[] children = getFiles(folder, false);
250 for (File child : children) {
251 if (file.equals(child)) {
252 return true;
253 }
254 }
255 return false;
256 } else {
257 return folder.equals(file.getParentFile());
258 }
259 }
260
261 /**
262 *
263 * @param parent a <code>File</code> object repesenting a directory or special folder
264 * @param fileName a name of a file or folder which exists in <code>parent</code>
265 * @return a File object. This is normally constructed with <code>new
266 * File(parent, fileName)</code> except when parent and child are both
267 * special folders, in which case the <code>File</code> is a wrapper containing
268 * a <code>ShellFolder</code> object.
269 * @since 1.4
270 */
271 public File getChild(File parent, String fileName) {
272 if (parent instanceof ShellFolder) {
273 File[] children = getFiles(parent, false);
274 for (File child : children) {
275 if (child.getName().equals(fileName)) {
276 return child;
277 }
278 }
279 }
280 return createFileObject(parent, fileName);
281 }
282
283
284 /**
285 * Checks if <code>f</code> represents a real directory or file as opposed to a
286 * special folder such as <code>"Desktop"</code>. Used by UI classes to decide if
287 * a folder is selectable when doing directory choosing.
288 *
289 * @param f a <code>File</code> object
290 * @return <code>true</code> if <code>f</code> is a real file or directory.
291 * @since 1.4
292 */
293 public boolean isFileSystem(File f) {
294 if (f instanceof ShellFolder) {
295 ShellFolder sf = (ShellFolder)f;
296 // Shortcuts to directories are treated as not being file system objects,
422 return new File(dir, filename);
423 }
424 }
425
426 /**
427 * Returns a File object constructed from the given path string.
428 */
429 public File createFileObject(String path) {
430 File f = new File(path);
431 if (isFileSystemRoot(f)) {
432 f = createFileSystemRoot(f);
433 }
434 return f;
435 }
436
437
438 /**
439 * Gets the list of shown (i.e. not hidden) files.
440 */
441 public File[] getFiles(File dir, boolean useFileHiding) {
442 Vector<File> files = new Vector<File>();
443
444
445 // add all files in dir
446 File[] names;
447 if (!(dir instanceof ShellFolder)) {
448 dir = getShellFolder(dir);
449 }
450
451 names = ((ShellFolder)dir).listFiles(!useFileHiding);
452 File f;
453
454 int nameCount = (names == null) ? 0 : names.length;
455 for (int i = 0; i < nameCount; i++) {
456 if (Thread.currentThread().isInterrupted()) {
457 break;
458 }
459 f = names[i];
460 if (!(f instanceof ShellFolder)) {
461 if (isFileSystemRoot(f)) {
462 f = createFileSystemRoot(f);
463 }
464 try {
465 f = ShellFolder.getShellFolder(f);
466 } catch (FileNotFoundException e) {
467 // Not a valid file (wouldn't show in native file chooser)
468 // Example: C:\pagefile.sys
469 continue;
470 } catch (InternalError e) {
471 // Not a valid file (wouldn't show in native file chooser)
472 // Example C:\Winnt\Profiles\joe\history\History.IE5
473 continue;
474 }
475 }
476 if (!useFileHiding || !isHiddenFile(f)) {
477 files.addElement(f);
478 }
479 }
480
481 return files.toArray(new File[files.size()]);
482 }
483
484
485
486 /**
487 * Returns the parent directory of <code>dir</code>.
488 * @param dir the <code>File</code> being queried
489 * @return the parent directory of <code>dir</code>, or
490 * <code>null</code> if <code>dir</code> is <code>null</code>
491 */
492 public File getParentDirectory(File dir) {
493 if (dir != null && dir.exists()) {
494 ShellFolder sf = getShellFolder(dir);
495 File psf = sf.getParentFile();
496 if (psf != null) {
497 if (isFileSystem(psf)) {
498 File f = psf;
499 if (f != null && !f.exists()) {
500 // This could be a node under "Network Neighborhood".
501 File ppsf = psf.getParentFile();
568 }
569 }
570
571 /**
572 * FileSystemView that handles some specific unix-isms.
573 */
574 class UnixFileSystemView extends FileSystemView {
575
576 private static final String newFolderString =
577 UIManager.getString("FileChooser.other.newFolder");
578 private static final String newFolderNextString =
579 UIManager.getString("FileChooser.other.newFolder.subsequent");
580
581 /**
582 * Creates a new folder with a default folder name.
583 */
584 public File createNewFolder(File containingDir) throws IOException {
585 if(containingDir == null) {
586 throw new IOException("Containing directory is null:");
587 }
588 File newFolder;
589 // Unix - using OpenWindows' default folder name. Can't find one for Motif/CDE.
590 newFolder = createFileObject(containingDir, newFolderString);
591 int i = 1;
592 while (newFolder.exists() && (i < 100)) {
593 newFolder = createFileObject(containingDir, MessageFormat.format(
594 newFolderNextString, new Object[] { new Integer(i) }));
595 i++;
596 }
597
598 if(newFolder.exists()) {
599 throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
600 } else {
601 newFolder.mkdirs();
602 }
603
604 return newFolder;
605 }
606
607 public boolean isFileSystemRoot(File dir) {
608 return (dir != null && dir.getAbsolutePath().equals("/"));
609 }
610
611 public boolean isDrive(File dir) {
612 return isFloppyDrive(dir);
613 }
614
615 public boolean isFloppyDrive(File dir) {
616 // Could be looking at the path for Solaris, but wouldn't be reliable.
617 // For example:
618 // return (dir != null && dir.getAbsolutePath().toLowerCase().startsWith("/floppy"));
619 return false;
620 }
621
622 public boolean isComputerNode(File dir) {
623 if (dir != null) {
624 String parent = dir.getParent();
625 if (parent != null && parent.equals("/net")) {
626 return true;
627 }
628 }
629 return false;
630 }
631 }
632
675 } else {
676 return null;
677 }
678 }
679
680 /**
681 * @return the Desktop folder.
682 */
683 public File getHomeDirectory() {
684 File[] roots = getRoots();
685 return (roots.length == 0) ? null : roots[0];
686 }
687
688 /**
689 * Creates a new folder with a default folder name.
690 */
691 public File createNewFolder(File containingDir) throws IOException {
692 if(containingDir == null) {
693 throw new IOException("Containing directory is null:");
694 }
695 // Using NT's default folder name
696 File newFolder = createFileObject(containingDir, newFolderString);
697 int i = 2;
698 while (newFolder.exists() && (i < 100)) {
699 newFolder = createFileObject(containingDir, MessageFormat.format(
700 newFolderNextString, new Object[] { new Integer(i) }));
701 i++;
702 }
703
704 if(newFolder.exists()) {
705 throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
706 } else {
707 newFolder.mkdirs();
708 }
709
710 return newFolder;
711 }
712
713 public boolean isDrive(File dir) {
714 return isFileSystemRoot(dir);
715 }
716
744 };
745 }
746
747 }
748
749 /**
750 * Fallthrough FileSystemView in case we can't determine the OS.
751 */
752 class GenericFileSystemView extends FileSystemView {
753
754 private static final String newFolderString =
755 UIManager.getString("FileChooser.other.newFolder");
756
757 /**
758 * Creates a new folder with a default folder name.
759 */
760 public File createNewFolder(File containingDir) throws IOException {
761 if(containingDir == null) {
762 throw new IOException("Containing directory is null:");
763 }
764 // Using NT's default folder name
765 File newFolder = createFileObject(containingDir, newFolderString);
766
767 if(newFolder.exists()) {
768 throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
769 } else {
770 newFolder.mkdirs();
771 }
772
773 return newFolder;
774 }
775
776 }
|