< prev index next >

src/java.desktop/share/classes/javax/swing/filechooser/FileSystemView.java

Print this page




 114         UIManager.addPropertyChangeListener(new PropertyChangeListener() {
 115             public void propertyChange(PropertyChangeEvent evt) {
 116                 FileSystemView fileSystemView = weakReference.get();
 117 
 118                 if (fileSystemView == null) {
 119                     // FileSystemView was destroyed
 120                     UIManager.removePropertyChangeListener(this);
 121                 } else {
 122                     if (evt.getPropertyName().equals("lookAndFeel")) {
 123                         fileSystemView.useSystemExtensionHiding =
 124                                 UIManager.getDefaults().getBoolean("FileChooser.useSystemExtensionHiding");
 125                     }
 126                 }
 127             }
 128         });
 129     }
 130 
 131     /**
 132      * Determines if the given file is a root in the navigable tree(s).
 133      * Examples: Windows 98 has one root, the Desktop folder. DOS has one root
 134      * per drive letter, <code>C:\</code>, <code>D:\</code>, etc. Unix has one root,
 135      * the <code>"/"</code> directory.
 136      *
 137      * The default implementation gets information from the <code>ShellFolder</code> class.
 138      *
 139      * @param f a <code>File</code> object representing a directory
 140      * @return <code>true</code> if <code>f</code> is a root in the navigable tree.
 141      * @see #isFileSystemRoot
 142      */
 143     public boolean isRoot(File f) {
 144         if (f == null || !f.isAbsolute()) {
 145             return false;
 146         }
 147 
 148         File[] roots = getRoots();
 149         for (File root : roots) {
 150             if (root.equals(f)) {
 151                 return true;
 152             }
 153         }
 154         return false;
 155     }
 156 
 157     /**
 158      * Returns true if the file (directory) can be visited.
 159      * Returns false if the directory cannot be traversed.
 160      *
 161      * @param f the <code>File</code>
 162      * @return <code>true</code> if the file/directory can be traversed, otherwise <code>false</code>
 163      * @see JFileChooser#isTraversable
 164      * @see FileView#isTraversable
 165      * @since 1.4
 166      */
 167     public Boolean isTraversable(File f) {
 168         return Boolean.valueOf(f.isDirectory());
 169     }
 170 
 171     /**
 172      * Name of a file, directory, or folder as it would be displayed in
 173      * a system file browser. Example from Windows: the "M:\" directory
 174      * displays as "CD-ROM (M:)"
 175      *
 176      * The default implementation gets information from the ShellFolder class.
 177      *
 178      * @param f a <code>File</code> object
 179      * @return the file name as it would be displayed by a native file chooser
 180      * @see JFileChooser#getName
 181      * @since 1.4
 182      */
 183     public String getSystemDisplayName(File f) {
 184         if (f == null) {
 185             return null;
 186         }
 187 
 188         String name = f.getName();
 189 
 190         if (!name.equals("..") && !name.equals(".") &&
 191                 (useSystemExtensionHiding || !isFileSystem(f) || isFileSystemRoot(f)) &&
 192                 (f instanceof ShellFolder || f.exists())) {
 193 
 194             try {
 195                 name = getShellFolder(f).getDisplayName();
 196             } catch (FileNotFoundException e) {
 197                 return null;
 198             }
 199 
 200             if (name == null || name.length() == 0) {
 201                 name = f.getPath(); // e.g. "/"
 202             }
 203         }
 204 
 205         return name;
 206     }
 207 
 208     /**
 209      * Type description for a file, directory, or folder as it would be displayed in
 210      * a system file browser. Example from Windows: the "Desktop" folder
 211      * is described as "Desktop".
 212      *
 213      * Override for platforms with native ShellFolder implementations.
 214      *
 215      * @param f a <code>File</code> object
 216      * @return the file type description as it would be displayed by a native file chooser
 217      * or null if no native information is available.
 218      * @see JFileChooser#getTypeDescription
 219      * @since 1.4
 220      */
 221     public String getSystemTypeDescription(File f) {
 222         return null;
 223     }
 224 
 225     /**
 226      * Icon for a file, directory, or folder as it would be displayed in
 227      * a system file browser. Example from Windows: the "M:\" directory
 228      * displays a CD-ROM icon.
 229      *
 230      * The default implementation gets information from the ShellFolder class.
 231      *
 232      * @param f a <code>File</code> object
 233      * @return an icon as it would be displayed by a native file chooser
 234      * @see JFileChooser#getIcon
 235      * @since 1.4
 236      */
 237     public Icon getSystemIcon(File f) {
 238         if (f == null) {
 239             return null;
 240         }
 241 
 242         ShellFolder sf;
 243 
 244         try {
 245             sf = getShellFolder(f);
 246         } catch (FileNotFoundException e) {
 247             return null;
 248         }
 249 
 250         Image img = sf.getIcon(false);
 251 
 252         if (img != null) {
 253             return new ImageIcon(img, sf.getFolderType());
 254         } else {
 255             return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
 256         }
 257     }
 258 
 259     /**
 260      * On Windows, a file can appear in multiple folders, other than its
 261      * parent directory in the filesystem. Folder could for example be the
 262      * "Desktop" folder which is not the same as file.getParentFile().
 263      *
 264      * @param folder a <code>File</code> object representing a directory or special folder
 265      * @param file a <code>File</code> object
 266      * @return <code>true</code> if <code>folder</code> is a directory or special folder and contains <code>file</code>.
 267      * @since 1.4
 268      */
 269     public boolean isParent(File folder, File file) {
 270         if (folder == null || file == null) {
 271             return false;
 272         } else if (folder instanceof ShellFolder) {
 273                 File parent = file.getParentFile();
 274                 if (parent != null && parent.equals(folder)) {
 275                     return true;
 276                 }
 277             File[] children = getFiles(folder, false);
 278             for (File child : children) {
 279                 if (file.equals(child)) {
 280                     return true;
 281                 }
 282             }
 283             return false;
 284         } else {
 285             return folder.equals(file.getParentFile());
 286         }
 287     }
 288 
 289     /**
 290      *
 291      * @param parent a <code>File</code> object representing a directory or special folder
 292      * @param fileName a name of a file or folder which exists in <code>parent</code>
 293      * @return a File object. This is normally constructed with <code>new
 294      * File(parent, fileName)</code> except when parent and child are both
 295      * special folders, in which case the <code>File</code> is a wrapper containing
 296      * a <code>ShellFolder</code> object.
 297      * @since 1.4
 298      */
 299     public File getChild(File parent, String fileName) {
 300         if (parent instanceof ShellFolder) {
 301             File[] children = getFiles(parent, false);
 302             for (File child : children) {
 303                 if (child.getName().equals(fileName)) {
 304                     return child;
 305                 }
 306             }
 307         }
 308         return createFileObject(parent, fileName);
 309     }
 310 
 311 
 312     /**
 313      * Checks if <code>f</code> represents a real directory or file as opposed to a
 314      * special folder such as <code>"Desktop"</code>. Used by UI classes to decide if
 315      * a folder is selectable when doing directory choosing.
 316      *
 317      * @param f a <code>File</code> object
 318      * @return <code>true</code> if <code>f</code> is a real file or directory.
 319      * @since 1.4
 320      */
 321     public boolean isFileSystem(File f) {
 322         if (f instanceof ShellFolder) {
 323             ShellFolder sf = (ShellFolder)f;
 324             // Shortcuts to directories are treated as not being file system objects,
 325             // so that they are never returned by JFileChooser.
 326             return sf.isFileSystem() && !(sf.isLink() && sf.isDirectory());
 327         } else {
 328             return true;
 329         }
 330     }
 331 
 332     /**
 333      * Creates a new folder with a default folder name.
 334      *
 335      * @param containingDir a {@code File} object denoting directory to contain the new folder
 336      * @return a {@code File} object denoting the newly created folder
 337      * @throws IOException if new folder could not be created
 338      */
 339     public abstract File createNewFolder(File containingDir) throws IOException;
 340 
 341     /**
 342      * Returns whether a file is hidden or not.
 343      *
 344      * @param f a {@code File} object
 345      * @return true if the given {@code File} denotes a hidden file
 346      */
 347     public boolean isHiddenFile(File f) {
 348         return f.isHidden();
 349     }
 350 
 351 
 352     /**
 353      * Is dir the root of a tree in the file system, such as a drive
 354      * or partition. Example: Returns true for "C:\" on Windows 98.
 355      *
 356      * @param dir a <code>File</code> object representing a directory
 357      * @return <code>true</code> if <code>f</code> is a root of a filesystem
 358      * @see #isRoot
 359      * @since 1.4
 360      */
 361     public boolean isFileSystemRoot(File dir) {
 362         return ShellFolder.isFileSystemRoot(dir);
 363     }
 364 
 365     /**
 366      * Used by UI classes to decide whether to display a special icon
 367      * for drives or partitions, e.g. a "hard disk" icon.
 368      *
 369      * The default implementation has no way of knowing, so always returns false.
 370      *
 371      * @param dir a directory
 372      * @return <code>false</code> always
 373      * @since 1.4
 374      */
 375     public boolean isDrive(File dir) {
 376         return false;
 377     }
 378 
 379     /**
 380      * Used by UI classes to decide whether to display a special icon
 381      * for a floppy disk. Implies isDrive(dir).
 382      *
 383      * The default implementation has no way of knowing, so always returns false.
 384      *
 385      * @param dir a directory
 386      * @return <code>false</code> always
 387      * @since 1.4
 388      */
 389     public boolean isFloppyDrive(File dir) {
 390         return false;
 391     }
 392 
 393     /**
 394      * Used by UI classes to decide whether to display a special icon
 395      * for a computer node, e.g. "My Computer" or a network server.
 396      *
 397      * The default implementation has no way of knowing, so always returns false.
 398      *
 399      * @param dir a directory
 400      * @return <code>false</code> always
 401      * @since 1.4
 402      */
 403     public boolean isComputerNode(File dir) {
 404         return ShellFolder.isComputerNode(dir);
 405     }
 406 
 407 
 408     /**
 409      * Returns all root partitions on this system. For example, on
 410      * Windows, this would be the "Desktop" folder, while on DOS this
 411      * would be the A: through Z: drives.
 412      *
 413      * @return an array of {@code File} objects representing all root partitions
 414      *         on this system
 415      */
 416     public File[] getRoots() {
 417         // Don't cache this array, because filesystem might change
 418         File[] roots = (File[])ShellFolder.get("roots");
 419 
 420         for (int i = 0; i < roots.length; i++) {


 425         return roots;
 426     }
 427 
 428 
 429     // Providing default implementations for the remaining methods
 430     // because most OS file systems will likely be able to use this
 431     // code. If a given OS can't, override these methods in its
 432     // implementation.
 433 
 434     /**
 435      * Returns the home directory.
 436      * @return the home directory
 437      */
 438     public File getHomeDirectory() {
 439         return createFileObject(System.getProperty("user.home"));
 440     }
 441 
 442     /**
 443      * Return the user's default starting directory for the file chooser.
 444      *
 445      * @return a <code>File</code> object representing the default
 446      *         starting folder
 447      * @since 1.4
 448      */
 449     public File getDefaultDirectory() {
 450         File f = (File)ShellFolder.get("fileChooserDefaultFolder");
 451         if (isFileSystemRoot(f)) {
 452             f = createFileSystemRoot(f);
 453         }
 454         return f;
 455     }
 456 
 457     /**
 458      * Returns a File object constructed in dir from the given filename.
 459      *
 460      * @param dir an abstract pathname denoting a directory
 461      * @param filename a {@code String} representation of a pathname
 462      * @return a {@code File} object created from {@code dir} and {@code filename}
 463      */
 464     public File createFileObject(File dir, String filename) {
 465         if(dir == null) {


 526                     // Not a valid file (wouldn't show in native file chooser)
 527                     // Example: C:\pagefile.sys
 528                     continue;
 529                 } catch (InternalError e) {
 530                     // Not a valid file (wouldn't show in native file chooser)
 531                     // Example C:\Winnt\Profiles\joe\history\History.IE5
 532                     continue;
 533                 }
 534             }
 535             if (!useFileHiding || !isHiddenFile(f)) {
 536                 files.add(f);
 537             }
 538         }
 539 
 540         return files.toArray(new File[files.size()]);
 541     }
 542 
 543 
 544 
 545     /**
 546      * Returns the parent directory of <code>dir</code>.
 547      * @param dir the <code>File</code> being queried
 548      * @return the parent directory of <code>dir</code>, or
 549      *   <code>null</code> if <code>dir</code> is <code>null</code>
 550      */
 551     public File getParentDirectory(File dir) {
 552         if (dir == null || !dir.exists()) {
 553             return null;
 554         }
 555 
 556         ShellFolder sf;
 557 
 558         try {
 559             sf = getShellFolder(dir);
 560         } catch (FileNotFoundException e) {
 561             return null;
 562         }
 563 
 564         File psf = sf.getParentFile();
 565 
 566         if (psf == null) {
 567             return null;
 568         }
 569 


 584     }
 585 
 586     /**
 587      * Throws {@code FileNotFoundException} if file not found or current thread was interrupted
 588      */
 589     ShellFolder getShellFolder(File f) throws FileNotFoundException {
 590         if (!(f instanceof ShellFolder) && !(f instanceof FileSystemRoot) && isFileSystemRoot(f)) {
 591             f = createFileSystemRoot(f);
 592         }
 593 
 594         try {
 595             return ShellFolder.getShellFolder(f);
 596         } catch (InternalError e) {
 597             System.err.println("FileSystemView.getShellFolder: f="+f);
 598             e.printStackTrace();
 599             return null;
 600         }
 601     }
 602 
 603     /**
 604      * Creates a new <code>File</code> object for <code>f</code> with correct
 605      * behavior for a file system root directory.
 606      *
 607      * @param f a <code>File</code> object representing a file system root
 608      *          directory, for example "/" on Unix or "C:\" on Windows.
 609      * @return a new <code>File</code> object
 610      * @since 1.4
 611      */
 612     protected File createFileSystemRoot(File f) {
 613         return new FileSystemRoot(f);
 614     }
 615 
 616     @SuppressWarnings("serial") // Same-version serialization only
 617     static class FileSystemRoot extends File {
 618         public FileSystemRoot(File f) {
 619             super(f,"");
 620         }
 621 
 622         public FileSystemRoot(String s) {
 623             super(s);
 624         }
 625 
 626         public boolean isDirectory() {
 627             return true;
 628         }
 629 




 114         UIManager.addPropertyChangeListener(new PropertyChangeListener() {
 115             public void propertyChange(PropertyChangeEvent evt) {
 116                 FileSystemView fileSystemView = weakReference.get();
 117 
 118                 if (fileSystemView == null) {
 119                     // FileSystemView was destroyed
 120                     UIManager.removePropertyChangeListener(this);
 121                 } else {
 122                     if (evt.getPropertyName().equals("lookAndFeel")) {
 123                         fileSystemView.useSystemExtensionHiding =
 124                                 UIManager.getDefaults().getBoolean("FileChooser.useSystemExtensionHiding");
 125                     }
 126                 }
 127             }
 128         });
 129     }
 130 
 131     /**
 132      * Determines if the given file is a root in the navigable tree(s).
 133      * Examples: Windows 98 has one root, the Desktop folder. DOS has one root
 134      * per drive letter, {@code C:\}, {@code D:\}, etc. Unix has one root,
 135      * the {@code "/"} directory.
 136      *
 137      * The default implementation gets information from the {@code ShellFolder} class.
 138      *
 139      * @param f a {@code File} object representing a directory
 140      * @return {@code true} if {@code f} is a root in the navigable tree.
 141      * @see #isFileSystemRoot
 142      */
 143     public boolean isRoot(File f) {
 144         if (f == null || !f.isAbsolute()) {
 145             return false;
 146         }
 147 
 148         File[] roots = getRoots();
 149         for (File root : roots) {
 150             if (root.equals(f)) {
 151                 return true;
 152             }
 153         }
 154         return false;
 155     }
 156 
 157     /**
 158      * Returns true if the file (directory) can be visited.
 159      * Returns false if the directory cannot be traversed.
 160      *
 161      * @param f the {@code File}
 162      * @return {@code true} if the file/directory can be traversed, otherwise {@code false}
 163      * @see JFileChooser#isTraversable
 164      * @see FileView#isTraversable
 165      * @since 1.4
 166      */
 167     public Boolean isTraversable(File f) {
 168         return Boolean.valueOf(f.isDirectory());
 169     }
 170 
 171     /**
 172      * Name of a file, directory, or folder as it would be displayed in
 173      * a system file browser. Example from Windows: the "M:\" directory
 174      * displays as "CD-ROM (M:)"
 175      *
 176      * The default implementation gets information from the ShellFolder class.
 177      *
 178      * @param f a {@code File} object
 179      * @return the file name as it would be displayed by a native file chooser
 180      * @see JFileChooser#getName
 181      * @since 1.4
 182      */
 183     public String getSystemDisplayName(File f) {
 184         if (f == null) {
 185             return null;
 186         }
 187 
 188         String name = f.getName();
 189 
 190         if (!name.equals("..") && !name.equals(".") &&
 191                 (useSystemExtensionHiding || !isFileSystem(f) || isFileSystemRoot(f)) &&
 192                 (f instanceof ShellFolder || f.exists())) {
 193 
 194             try {
 195                 name = getShellFolder(f).getDisplayName();
 196             } catch (FileNotFoundException e) {
 197                 return null;
 198             }
 199 
 200             if (name == null || name.length() == 0) {
 201                 name = f.getPath(); // e.g. "/"
 202             }
 203         }
 204 
 205         return name;
 206     }
 207 
 208     /**
 209      * Type description for a file, directory, or folder as it would be displayed in
 210      * a system file browser. Example from Windows: the "Desktop" folder
 211      * is described as "Desktop".
 212      *
 213      * Override for platforms with native ShellFolder implementations.
 214      *
 215      * @param f a {@code File} object
 216      * @return the file type description as it would be displayed by a native file chooser
 217      * or null if no native information is available.
 218      * @see JFileChooser#getTypeDescription
 219      * @since 1.4
 220      */
 221     public String getSystemTypeDescription(File f) {
 222         return null;
 223     }
 224 
 225     /**
 226      * Icon for a file, directory, or folder as it would be displayed in
 227      * a system file browser. Example from Windows: the "M:\" directory
 228      * displays a CD-ROM icon.
 229      *
 230      * The default implementation gets information from the ShellFolder class.
 231      *
 232      * @param f a {@code File} object
 233      * @return an icon as it would be displayed by a native file chooser
 234      * @see JFileChooser#getIcon
 235      * @since 1.4
 236      */
 237     public Icon getSystemIcon(File f) {
 238         if (f == null) {
 239             return null;
 240         }
 241 
 242         ShellFolder sf;
 243 
 244         try {
 245             sf = getShellFolder(f);
 246         } catch (FileNotFoundException e) {
 247             return null;
 248         }
 249 
 250         Image img = sf.getIcon(false);
 251 
 252         if (img != null) {
 253             return new ImageIcon(img, sf.getFolderType());
 254         } else {
 255             return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
 256         }
 257     }
 258 
 259     /**
 260      * On Windows, a file can appear in multiple folders, other than its
 261      * parent directory in the filesystem. Folder could for example be the
 262      * "Desktop" folder which is not the same as file.getParentFile().
 263      *
 264      * @param folder a {@code File} object representing a directory or special folder
 265      * @param file a {@code File} object
 266      * @return {@code true} if {@code folder} is a directory or special folder and contains {@code file}.
 267      * @since 1.4
 268      */
 269     public boolean isParent(File folder, File file) {
 270         if (folder == null || file == null) {
 271             return false;
 272         } else if (folder instanceof ShellFolder) {
 273                 File parent = file.getParentFile();
 274                 if (parent != null && parent.equals(folder)) {
 275                     return true;
 276                 }
 277             File[] children = getFiles(folder, false);
 278             for (File child : children) {
 279                 if (file.equals(child)) {
 280                     return true;
 281                 }
 282             }
 283             return false;
 284         } else {
 285             return folder.equals(file.getParentFile());
 286         }
 287     }
 288 
 289     /**
 290      *
 291      * @param parent a {@code File} object representing a directory or special folder
 292      * @param fileName a name of a file or folder which exists in {@code parent}
 293      * @return a File object. This is normally constructed with
 294      * {@code new File(parent, fileName)} except when parent and child are both
 295      * special folders, in which case the {@code File} is a wrapper containing
 296      * a {@code ShellFolder} object.
 297      * @since 1.4
 298      */
 299     public File getChild(File parent, String fileName) {
 300         if (parent instanceof ShellFolder) {
 301             File[] children = getFiles(parent, false);
 302             for (File child : children) {
 303                 if (child.getName().equals(fileName)) {
 304                     return child;
 305                 }
 306             }
 307         }
 308         return createFileObject(parent, fileName);
 309     }
 310 
 311 
 312     /**
 313      * Checks if {@code f} represents a real directory or file as opposed to a
 314      * special folder such as {@code "Desktop"}. Used by UI classes to decide if
 315      * a folder is selectable when doing directory choosing.
 316      *
 317      * @param f a {@code File} object
 318      * @return {@code true} if {@code f} is a real file or directory.
 319      * @since 1.4
 320      */
 321     public boolean isFileSystem(File f) {
 322         if (f instanceof ShellFolder) {
 323             ShellFolder sf = (ShellFolder)f;
 324             // Shortcuts to directories are treated as not being file system objects,
 325             // so that they are never returned by JFileChooser.
 326             return sf.isFileSystem() && !(sf.isLink() && sf.isDirectory());
 327         } else {
 328             return true;
 329         }
 330     }
 331 
 332     /**
 333      * Creates a new folder with a default folder name.
 334      *
 335      * @param containingDir a {@code File} object denoting directory to contain the new folder
 336      * @return a {@code File} object denoting the newly created folder
 337      * @throws IOException if new folder could not be created
 338      */
 339     public abstract File createNewFolder(File containingDir) throws IOException;
 340 
 341     /**
 342      * Returns whether a file is hidden or not.
 343      *
 344      * @param f a {@code File} object
 345      * @return true if the given {@code File} denotes a hidden file
 346      */
 347     public boolean isHiddenFile(File f) {
 348         return f.isHidden();
 349     }
 350 
 351 
 352     /**
 353      * Is dir the root of a tree in the file system, such as a drive
 354      * or partition. Example: Returns true for "C:\" on Windows 98.
 355      *
 356      * @param dir a {@code File} object representing a directory
 357      * @return {@code true} if {@code f} is a root of a filesystem
 358      * @see #isRoot
 359      * @since 1.4
 360      */
 361     public boolean isFileSystemRoot(File dir) {
 362         return ShellFolder.isFileSystemRoot(dir);
 363     }
 364 
 365     /**
 366      * Used by UI classes to decide whether to display a special icon
 367      * for drives or partitions, e.g. a "hard disk" icon.
 368      *
 369      * The default implementation has no way of knowing, so always returns false.
 370      *
 371      * @param dir a directory
 372      * @return {@code false} always
 373      * @since 1.4
 374      */
 375     public boolean isDrive(File dir) {
 376         return false;
 377     }
 378 
 379     /**
 380      * Used by UI classes to decide whether to display a special icon
 381      * for a floppy disk. Implies isDrive(dir).
 382      *
 383      * The default implementation has no way of knowing, so always returns false.
 384      *
 385      * @param dir a directory
 386      * @return {@code false} always
 387      * @since 1.4
 388      */
 389     public boolean isFloppyDrive(File dir) {
 390         return false;
 391     }
 392 
 393     /**
 394      * Used by UI classes to decide whether to display a special icon
 395      * for a computer node, e.g. "My Computer" or a network server.
 396      *
 397      * The default implementation has no way of knowing, so always returns false.
 398      *
 399      * @param dir a directory
 400      * @return {@code false} always
 401      * @since 1.4
 402      */
 403     public boolean isComputerNode(File dir) {
 404         return ShellFolder.isComputerNode(dir);
 405     }
 406 
 407 
 408     /**
 409      * Returns all root partitions on this system. For example, on
 410      * Windows, this would be the "Desktop" folder, while on DOS this
 411      * would be the A: through Z: drives.
 412      *
 413      * @return an array of {@code File} objects representing all root partitions
 414      *         on this system
 415      */
 416     public File[] getRoots() {
 417         // Don't cache this array, because filesystem might change
 418         File[] roots = (File[])ShellFolder.get("roots");
 419 
 420         for (int i = 0; i < roots.length; i++) {


 425         return roots;
 426     }
 427 
 428 
 429     // Providing default implementations for the remaining methods
 430     // because most OS file systems will likely be able to use this
 431     // code. If a given OS can't, override these methods in its
 432     // implementation.
 433 
 434     /**
 435      * Returns the home directory.
 436      * @return the home directory
 437      */
 438     public File getHomeDirectory() {
 439         return createFileObject(System.getProperty("user.home"));
 440     }
 441 
 442     /**
 443      * Return the user's default starting directory for the file chooser.
 444      *
 445      * @return a {@code File} object representing the default
 446      *         starting folder
 447      * @since 1.4
 448      */
 449     public File getDefaultDirectory() {
 450         File f = (File)ShellFolder.get("fileChooserDefaultFolder");
 451         if (isFileSystemRoot(f)) {
 452             f = createFileSystemRoot(f);
 453         }
 454         return f;
 455     }
 456 
 457     /**
 458      * Returns a File object constructed in dir from the given filename.
 459      *
 460      * @param dir an abstract pathname denoting a directory
 461      * @param filename a {@code String} representation of a pathname
 462      * @return a {@code File} object created from {@code dir} and {@code filename}
 463      */
 464     public File createFileObject(File dir, String filename) {
 465         if(dir == null) {


 526                     // Not a valid file (wouldn't show in native file chooser)
 527                     // Example: C:\pagefile.sys
 528                     continue;
 529                 } catch (InternalError e) {
 530                     // Not a valid file (wouldn't show in native file chooser)
 531                     // Example C:\Winnt\Profiles\joe\history\History.IE5
 532                     continue;
 533                 }
 534             }
 535             if (!useFileHiding || !isHiddenFile(f)) {
 536                 files.add(f);
 537             }
 538         }
 539 
 540         return files.toArray(new File[files.size()]);
 541     }
 542 
 543 
 544 
 545     /**
 546      * Returns the parent directory of {@code dir}.
 547      * @param dir the {@code File} being queried
 548      * @return the parent directory of {@code dir}, or
 549      *   {@code null} if {@code dir} is {@code null}
 550      */
 551     public File getParentDirectory(File dir) {
 552         if (dir == null || !dir.exists()) {
 553             return null;
 554         }
 555 
 556         ShellFolder sf;
 557 
 558         try {
 559             sf = getShellFolder(dir);
 560         } catch (FileNotFoundException e) {
 561             return null;
 562         }
 563 
 564         File psf = sf.getParentFile();
 565 
 566         if (psf == null) {
 567             return null;
 568         }
 569 


 584     }
 585 
 586     /**
 587      * Throws {@code FileNotFoundException} if file not found or current thread was interrupted
 588      */
 589     ShellFolder getShellFolder(File f) throws FileNotFoundException {
 590         if (!(f instanceof ShellFolder) && !(f instanceof FileSystemRoot) && isFileSystemRoot(f)) {
 591             f = createFileSystemRoot(f);
 592         }
 593 
 594         try {
 595             return ShellFolder.getShellFolder(f);
 596         } catch (InternalError e) {
 597             System.err.println("FileSystemView.getShellFolder: f="+f);
 598             e.printStackTrace();
 599             return null;
 600         }
 601     }
 602 
 603     /**
 604      * Creates a new {@code File} object for {@code f} with correct
 605      * behavior for a file system root directory.
 606      *
 607      * @param f a {@code File} object representing a file system root
 608      *          directory, for example "/" on Unix or "C:\" on Windows.
 609      * @return a new {@code File} object
 610      * @since 1.4
 611      */
 612     protected File createFileSystemRoot(File f) {
 613         return new FileSystemRoot(f);
 614     }
 615 
 616     @SuppressWarnings("serial") // Same-version serialization only
 617     static class FileSystemRoot extends File {
 618         public FileSystemRoot(File f) {
 619             super(f,"");
 620         }
 621 
 622         public FileSystemRoot(String s) {
 623             super(s);
 624         }
 625 
 626         public boolean isDirectory() {
 627             return true;
 628         }
 629 


< prev index next >