FileDocCategorySizeDatePackage
FileSystemView.javaAPI DocJava SE 5 API21818Fri Aug 26 14:58:02 BST 2005javax.swing.filechooser

FileSystemView

public abstract class FileSystemView extends Object
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.

Java Licensees may want to provide a different implementation of FileSystemView to better handle a given operating system.

version
1.46 04/27/04
author
Jeff Dinkins

Fields Summary
static FileSystemView
windowsFileSystemView
static FileSystemView
unixFileSystemView
static FileSystemView
genericFileSystemView
static boolean
useSystemExtensionsHiding
Constructors Summary
Methods Summary
public java.io.FilecreateFileObject(java.io.File dir, java.lang.String filename)
Returns a File object constructed in dir from the given filename.

	if(dir == null) {
	    return new File(filename);
	} else {
	    return new File(dir, filename);
	}
    
public java.io.FilecreateFileObject(java.lang.String path)
Returns a File object constructed from the given path string.

	File f = new File(path);
	if (isFileSystemRoot(f)) {
	    f = createFileSystemRoot(f);
	}
	return f;
    
protected java.io.FilecreateFileSystemRoot(java.io.File f)
Creates a new File object for f with correct behavior for a file system root directory.

param
f a File object representing a file system root directory, for example "/" on Unix or "C:\" on Windows.
return
a new File object

	return new FileSystemRoot(f);
    
public abstract java.io.FilecreateNewFolder(java.io.File containingDir)
Creates a new folder with a default folder name.

public java.io.FilegetChild(java.io.File parent, java.lang.String fileName)

param
parent a File object repesenting a directory or special folder
param
fileName a name of a file or folder which exists in parent
return
a File object. This is normally constructed with new File(parent, fileName) except when parent and child are both special folders, in which case the File is a wrapper containing a ShellFolder object.

	if (parent instanceof ShellFolder) {
	    File[] children = getFiles(parent, false);
	    for (int i = 0; i < children.length; i++) {
		if (children[i].getName().equals(fileName)) {
		    return children[i];
		}
	    }
	}
	return createFileObject(parent, fileName);
    
public java.io.FilegetDefaultDirectory()
Return the user's default starting directory for the file chooser.

return
a File object representing the default starting folder

	File f = (File)ShellFolder.get("fileChooserDefaultFolder");
	if (isFileSystemRoot(f)) {
	    f = createFileSystemRoot(f);
	}
	return f;
    
public static javax.swing.filechooser.FileSystemViewgetFileSystemView()

 

        
        useSystemExtensionsHiding = UIManager.getDefaults().getBoolean("FileChooser.useSystemExtensionHiding");
        UIManager.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent e) {
               if (e.getPropertyName().equals("lookAndFeel")) {
                   useSystemExtensionsHiding = UIManager.getDefaults().getBoolean("FileChooser.useSystemExtensionHiding");
               }
            }
        });

	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;
    
public java.io.File[]getFiles(java.io.File dir, boolean useFileHiding)
Gets the list of shown (i.e. not hidden) files.

	Vector files = new Vector();


	// add all files in dir
	File[] names;
	    if (!(dir instanceof ShellFolder)) {
		dir = getShellFolder(dir);
	    }

	    names = ((ShellFolder)dir).listFiles(!useFileHiding);
	File f;

	int nameCount = (names == null) ? 0 : names.length;
	for (int i = 0; i < nameCount; i++) {
	    if (Thread.currentThread().isInterrupted()) {
		break;
	    }
	    f = names[i];
	    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.addElement(f);
	    }
	}

	return (File[])files.toArray(new File[files.size()]);
    
public java.io.FilegetHomeDirectory()

	return createFileObject(System.getProperty("user.home"));
    
public java.io.FilegetParentDirectory(java.io.File dir)
Returns the parent directory of dir.

param
dir the File being queried
return
the parent directory of dir, or null if dir is null

	if (dir != null && dir.exists()) {
	    ShellFolder sf = getShellFolder(dir);
	    File psf = sf.getParentFile();
	    if (psf != null) {
		if (isFileSystem(psf)) {
		    File f = psf;
		    if (f != null && !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;
		}
	    }
	}
	return null;
    
public java.io.File[]getRoots()
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.

	// 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;
    
sun.awt.shell.ShellFoldergetShellFolder(java.io.File f)

	if (!(f instanceof ShellFolder)
	    && !(f instanceof FileSystemRoot)
	    && isFileSystemRoot(f)) {

	    f = createFileSystemRoot(f);
	}
	try {
	    return ShellFolder.getShellFolder(f);
	} catch (FileNotFoundException e) {
	    System.err.println("FileSystemView.getShellFolder: f="+f);
	    e.printStackTrace();
	    return null;
	} catch (InternalError e) {
	    System.err.println("FileSystemView.getShellFolder: f="+f);
	    e.printStackTrace();
	    return null;
	}
    
public java.lang.StringgetSystemDisplayName(java.io.File f)
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 File object
return
the file name as it would be displayed by a native file chooser
see
JFileChooser#getName

	String name = null;
	if (f != null) {
	    name = f.getName();
	    if (!name.equals("..") && !name.equals(".") &&
                (useSystemExtensionsHiding ||
                 !isFileSystem(f) ||
                 isFileSystemRoot(f)) &&
		((f instanceof ShellFolder) ||
		 f.exists())) {

		name = getShellFolder(f).getDisplayName();
		if (name == null || name.length() == 0) {
		    name = f.getPath();	// e.g. "/"
		}
	    }
	}
	return name;
    
public javax.swing.IcongetSystemIcon(java.io.File f)
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. The default implementation gets information from the ShellFolder class.

param
f a File object
return
an icon as it would be displayed by a native file chooser
see
JFileChooser#getIcon

	if (f != null) {
	    ShellFolder sf = getShellFolder(f);
	    Image img = sf.getIcon(false);
	    if (img != null) {
		return new ImageIcon(img, sf.getFolderType());
	    } else {
		return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
	    }
	} else {
	    return null;
	}
    
public java.lang.StringgetSystemTypeDescription(java.io.File f)
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 desribed as "Desktop". Override for platforms with native ShellFolder implementations.

param
f a File 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

	return null;
    
public booleanisComputerNode(java.io.File dir)
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
false always

	return ShellFolder.isComputerNode(dir);
    
public booleanisDrive(java.io.File 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
false always

	return false;
    
public booleanisFileSystem(java.io.File f)
Checks if f represents a real directory or file as opposed to a special folder such as "Desktop". Used by UI classes to decide if a folder is selectable when doing directory choosing.

param
f a File object
return
true if f is a real file or directory.

	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;
	}
    
public booleanisFileSystemRoot(java.io.File 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
f a File object representing a directory
return
true if f is a root of a filesystem
see
#isRoot

	return ShellFolder.isFileSystemRoot(dir);
    
public booleanisFloppyDrive(java.io.File dir)
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
false always

	return false;
    
public booleanisHiddenFile(java.io.File f)
Returns whether a file is hidden or not.

	return f.isHidden();
    
public booleanisParent(java.io.File folder, java.io.File file)
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 File object repesenting a directory or special folder
param
file a File object
return
true if folder is a directory or special folder and contains 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 (int i = 0; i < children.length; i++) {
		if (file.equals(children[i])) {
		    return true;
		}
	    }
	    return false;
	} else {
	    return folder.equals(file.getParentFile());
	}
    
public booleanisRoot(java.io.File f)
Determines if the given file is a root in the navigatable tree(s). Examples: Windows 98 has one root, the Desktop folder. DOS has one root per drive letter, C:\, D:\, etc. Unix has one root, the "/" directory. The default implementation gets information from the ShellFolder class.

param
f a File object representing a directory
return
true if f is a root in the navigatable tree.
see
#isFileSystemRoot

	if (f == null || !f.isAbsolute()) {
	    return false;
	}

	File[] roots = getRoots();
	for (int i = 0; i < roots.length; i++) {
	    if (roots[i].equals(f)) {
		return true;
	    }
	}
	return false;
    
public java.lang.BooleanisTraversable(java.io.File f)
Returns true if the file (directory) can be visited. Returns false if the directory cannot be traversed.

param
f the File
return
true if the file/directory can be traversed, otherwise false
see
JFileChooser#isTraversable
see
FileView#isTraversable

	return Boolean.valueOf(f.isDirectory());