/** * 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; } }
/** * 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; } }
/** * 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 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. */ 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; }
/** * 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"); } }
/** 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; } }
/** Gets the list of shown (i.e. not hidden) files. */ 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()]); }
/** * Used by UI classes to decide whether to display a special icon for a computer node, e.g. "My * Computer" or a network server. * * <p>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); }
/** * 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); }