0N/A/*
2362N/A * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing.filechooser;
0N/A
0N/A
0N/Aimport javax.swing.*;
0N/A
0N/Aimport java.awt.Image;
0N/Aimport java.io.File;
0N/Aimport java.io.FileNotFoundException;
0N/Aimport java.io.IOException;
0N/Aimport java.text.MessageFormat;
1915N/Aimport java.util.List;
1915N/Aimport java.util.ArrayList;
239N/Aimport java.lang.ref.WeakReference;
0N/Aimport java.beans.PropertyChangeListener;
0N/Aimport java.beans.PropertyChangeEvent;
1560N/Aimport java.security.AccessController;
1560N/Aimport java.security.PrivilegedAction;
0N/A
0N/Aimport sun.awt.shell.*;
0N/A
0N/A/**
0N/A * FileSystemView is JFileChooser's gateway to the
0N/A * file system. Since the JDK1.1 File API doesn't allow
0N/A * access to such information as root partitions, file type
0N/A * information, or hidden file bits, this class is designed
0N/A * to intuit as much OS-specific file system information as
0N/A * possible.
0N/A *
0N/A * <p>
0N/A *
0N/A * Java Licensees may want to provide a different implementation of
0N/A * FileSystemView to better handle a given operating system.
0N/A *
0N/A * @author Jeff Dinkins
0N/A */
0N/A
0N/A// PENDING(jeff) - need to provide a specification for
0N/A// how Mac/OS2/BeOS/etc file systems can modify FileSystemView
0N/A// to handle their particular type of file system.
0N/A
0N/Apublic abstract class FileSystemView {
0N/A
0N/A static FileSystemView windowsFileSystemView = null;
0N/A static FileSystemView unixFileSystemView = null;
0N/A //static FileSystemView macFileSystemView = null;
0N/A static FileSystemView genericFileSystemView = null;
239N/A
239N/A private boolean useSystemExtensionHiding =
239N/A UIManager.getDefaults().getBoolean("FileChooser.useSystemExtensionHiding");
0N/A
0N/A public static FileSystemView getFileSystemView() {
0N/A if(File.separatorChar == '\\') {
0N/A if(windowsFileSystemView == null) {
0N/A windowsFileSystemView = new WindowsFileSystemView();
0N/A }
0N/A return windowsFileSystemView;
0N/A }
0N/A
0N/A if(File.separatorChar == '/') {
0N/A if(unixFileSystemView == null) {
0N/A unixFileSystemView = new UnixFileSystemView();
0N/A }
0N/A return unixFileSystemView;
0N/A }
0N/A
0N/A // if(File.separatorChar == ':') {
0N/A // if(macFileSystemView == null) {
0N/A // macFileSystemView = new MacFileSystemView();
0N/A // }
0N/A // return macFileSystemView;
0N/A //}
0N/A
0N/A if(genericFileSystemView == null) {
0N/A genericFileSystemView = new GenericFileSystemView();
0N/A }
0N/A return genericFileSystemView;
0N/A }
0N/A
239N/A public FileSystemView() {
239N/A final WeakReference<FileSystemView> weakReference = new WeakReference<FileSystemView>(this);
239N/A
239N/A UIManager.addPropertyChangeListener(new PropertyChangeListener() {
239N/A public void propertyChange(PropertyChangeEvent evt) {
239N/A FileSystemView fileSystemView = weakReference.get();
239N/A
239N/A if (fileSystemView == null) {
239N/A // FileSystemView was destroyed
239N/A UIManager.removePropertyChangeListener(this);
239N/A } else {
239N/A if (evt.getPropertyName().equals("lookAndFeel")) {
239N/A fileSystemView.useSystemExtensionHiding =
239N/A UIManager.getDefaults().getBoolean("FileChooser.useSystemExtensionHiding");
239N/A }
239N/A }
239N/A }
239N/A });
239N/A }
239N/A
0N/A /**
0N/A * Determines if the given file is a root in the navigatable tree(s).
0N/A * Examples: Windows 98 has one root, the Desktop folder. DOS has one root
0N/A * per drive letter, <code>C:\</code>, <code>D:\</code>, etc. Unix has one root,
0N/A * the <code>"/"</code> directory.
0N/A *
0N/A * The default implementation gets information from the <code>ShellFolder</code> class.
0N/A *
0N/A * @param f a <code>File</code> object representing a directory
0N/A * @return <code>true</code> if <code>f</code> is a root in the navigatable tree.
0N/A * @see #isFileSystemRoot
0N/A */
0N/A public boolean isRoot(File f) {
0N/A if (f == null || !f.isAbsolute()) {
0N/A return false;
0N/A }
0N/A
0N/A File[] roots = getRoots();
625N/A for (File root : roots) {
625N/A if (root.equals(f)) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the file (directory) can be visited.
0N/A * Returns false if the directory cannot be traversed.
0N/A *
0N/A * @param f the <code>File</code>
0N/A * @return <code>true</code> if the file/directory can be traversed, otherwise <code>false</code>
0N/A * @see JFileChooser#isTraversable
0N/A * @see FileView#isTraversable
0N/A * @since 1.4
0N/A */
0N/A public Boolean isTraversable(File f) {
0N/A return Boolean.valueOf(f.isDirectory());
0N/A }
0N/A
0N/A /**
0N/A * Name of a file, directory, or folder as it would be displayed in
0N/A * a system file browser. Example from Windows: the "M:\" directory
0N/A * displays as "CD-ROM (M:)"
0N/A *
0N/A * The default implementation gets information from the ShellFolder class.
0N/A *
0N/A * @param f a <code>File</code> object
0N/A * @return the file name as it would be displayed by a native file chooser
0N/A * @see JFileChooser#getName
0N/A * @since 1.4
0N/A */
0N/A public String getSystemDisplayName(File f) {
1915N/A if (f == null) {
1915N/A return null;
1915N/A }
1915N/A
1915N/A String name = f.getName();
0N/A
1915N/A if (!name.equals("..") && !name.equals(".") &&
1915N/A (useSystemExtensionHiding || !isFileSystem(f) || isFileSystemRoot(f)) &&
1915N/A (f instanceof ShellFolder || f.exists())) {
1915N/A
1915N/A try {
0N/A name = getShellFolder(f).getDisplayName();
1915N/A } catch (FileNotFoundException e) {
1915N/A return null;
1915N/A }
1915N/A
1915N/A if (name == null || name.length() == 0) {
1915N/A name = f.getPath(); // e.g. "/"
0N/A }
0N/A }
1915N/A
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * Type description for a file, directory, or folder as it would be displayed in
0N/A * a system file browser. Example from Windows: the "Desktop" folder
0N/A * is desribed as "Desktop".
0N/A *
0N/A * Override for platforms with native ShellFolder implementations.
0N/A *
0N/A * @param f a <code>File</code> object
0N/A * @return the file type description as it would be displayed by a native file chooser
0N/A * or null if no native information is available.
0N/A * @see JFileChooser#getTypeDescription
0N/A * @since 1.4
0N/A */
0N/A public String getSystemTypeDescription(File f) {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Icon for a file, directory, or folder as it would be displayed in
0N/A * a system file browser. Example from Windows: the "M:\" directory
0N/A * displays a CD-ROM icon.
0N/A *
0N/A * The default implementation gets information from the ShellFolder class.
0N/A *
0N/A * @param f a <code>File</code> object
0N/A * @return an icon as it would be displayed by a native file chooser
0N/A * @see JFileChooser#getIcon
0N/A * @since 1.4
0N/A */
0N/A public Icon getSystemIcon(File f) {
1915N/A if (f == null) {
1915N/A return null;
1915N/A }
1915N/A
1915N/A ShellFolder sf;
1915N/A
1915N/A try {
1915N/A sf = getShellFolder(f);
1915N/A } catch (FileNotFoundException e) {
1915N/A return null;
1915N/A }
1915N/A
1915N/A Image img = sf.getIcon(false);
1915N/A
1915N/A if (img != null) {
1915N/A return new ImageIcon(img, sf.getFolderType());
0N/A } else {
1915N/A return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * On Windows, a file can appear in multiple folders, other than its
0N/A * parent directory in the filesystem. Folder could for example be the
0N/A * "Desktop" folder which is not the same as file.getParentFile().
0N/A *
0N/A * @param folder a <code>File</code> object repesenting a directory or special folder
0N/A * @param file a <code>File</code> object
0N/A * @return <code>true</code> if <code>folder</code> is a directory or special folder and contains <code>file</code>.
0N/A * @since 1.4
0N/A */
0N/A public boolean isParent(File folder, File file) {
0N/A if (folder == null || file == null) {
0N/A return false;
0N/A } else if (folder instanceof ShellFolder) {
0N/A File parent = file.getParentFile();
0N/A if (parent != null && parent.equals(folder)) {
0N/A return true;
0N/A }
0N/A File[] children = getFiles(folder, false);
625N/A for (File child : children) {
625N/A if (file.equals(child)) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A } else {
0N/A return folder.equals(file.getParentFile());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A *
0N/A * @param parent a <code>File</code> object repesenting a directory or special folder
0N/A * @param fileName a name of a file or folder which exists in <code>parent</code>
0N/A * @return a File object. This is normally constructed with <code>new
0N/A * File(parent, fileName)</code> except when parent and child are both
0N/A * special folders, in which case the <code>File</code> is a wrapper containing
0N/A * a <code>ShellFolder</code> object.
0N/A * @since 1.4
0N/A */
0N/A public File getChild(File parent, String fileName) {
0N/A if (parent instanceof ShellFolder) {
0N/A File[] children = getFiles(parent, false);
625N/A for (File child : children) {
625N/A if (child.getName().equals(fileName)) {
625N/A return child;
0N/A }
0N/A }
0N/A }
0N/A return createFileObject(parent, fileName);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Checks if <code>f</code> represents a real directory or file as opposed to a
0N/A * special folder such as <code>"Desktop"</code>. Used by UI classes to decide if
0N/A * a folder is selectable when doing directory choosing.
0N/A *
0N/A * @param f a <code>File</code> object
0N/A * @return <code>true</code> if <code>f</code> is a real file or directory.
0N/A * @since 1.4
0N/A */
0N/A public boolean isFileSystem(File f) {
0N/A if (f instanceof ShellFolder) {
0N/A ShellFolder sf = (ShellFolder)f;
0N/A // Shortcuts to directories are treated as not being file system objects,
0N/A // so that they are never returned by JFileChooser.
0N/A return sf.isFileSystem() && !(sf.isLink() && sf.isDirectory());
0N/A } else {
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates a new folder with a default folder name.
0N/A */
0N/A public abstract File createNewFolder(File containingDir) throws IOException;
0N/A
0N/A /**
0N/A * Returns whether a file is hidden or not.
0N/A */
0N/A public boolean isHiddenFile(File f) {
0N/A return f.isHidden();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Is dir the root of a tree in the file system, such as a drive
0N/A * or partition. Example: Returns true for "C:\" on Windows 98.
0N/A *
0N/A * @param dir a <code>File</code> object representing a directory
0N/A * @return <code>true</code> if <code>f</code> is a root of a filesystem
0N/A * @see #isRoot
0N/A * @since 1.4
0N/A */
0N/A public boolean isFileSystemRoot(File dir) {
0N/A return ShellFolder.isFileSystemRoot(dir);
0N/A }
0N/A
0N/A /**
0N/A * Used by UI classes to decide whether to display a special icon
0N/A * for drives or partitions, e.g. a "hard disk" icon.
0N/A *
0N/A * The default implementation has no way of knowing, so always returns false.
0N/A *
0N/A * @param dir a directory
0N/A * @return <code>false</code> always
0N/A * @since 1.4
0N/A */
0N/A public boolean isDrive(File dir) {
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Used by UI classes to decide whether to display a special icon
0N/A * for a floppy disk. Implies isDrive(dir).
0N/A *
0N/A * The default implementation has no way of knowing, so always returns false.
0N/A *
0N/A * @param dir a directory
0N/A * @return <code>false</code> always
0N/A * @since 1.4
0N/A */
0N/A public boolean isFloppyDrive(File dir) {
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Used by UI classes to decide whether to display a special icon
0N/A * for a computer node, e.g. "My Computer" or a network server.
0N/A *
0N/A * The default implementation has no way of knowing, so always returns false.
0N/A *
0N/A * @param dir a directory
0N/A * @return <code>false</code> always
0N/A * @since 1.4
0N/A */
0N/A public boolean isComputerNode(File dir) {
0N/A return ShellFolder.isComputerNode(dir);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns all root partitions on this system. For example, on
0N/A * Windows, this would be the "Desktop" folder, while on DOS this
0N/A * would be the A: through Z: drives.
0N/A */
0N/A public File[] getRoots() {
0N/A // Don't cache this array, because filesystem might change
0N/A File[] roots = (File[])ShellFolder.get("roots");
0N/A
0N/A for (int i = 0; i < roots.length; i++) {
0N/A if (isFileSystemRoot(roots[i])) {
0N/A roots[i] = createFileSystemRoot(roots[i]);
0N/A }
0N/A }
0N/A return roots;
0N/A }
0N/A
0N/A
0N/A // Providing default implementations for the remaining methods
0N/A // because most OS file systems will likely be able to use this
0N/A // code. If a given OS can't, override these methods in its
0N/A // implementation.
0N/A
0N/A public File getHomeDirectory() {
0N/A return createFileObject(System.getProperty("user.home"));
0N/A }
0N/A
0N/A /**
0N/A * Return the user's default starting directory for the file chooser.
0N/A *
0N/A * @return a <code>File</code> object representing the default
0N/A * starting folder
0N/A * @since 1.4
0N/A */
0N/A public File getDefaultDirectory() {
0N/A File f = (File)ShellFolder.get("fileChooserDefaultFolder");
0N/A if (isFileSystemRoot(f)) {
0N/A f = createFileSystemRoot(f);
0N/A }
0N/A return f;
0N/A }
0N/A
0N/A /**
0N/A * Returns a File object constructed in dir from the given filename.
0N/A */
0N/A public File createFileObject(File dir, String filename) {
0N/A if(dir == null) {
0N/A return new File(filename);
0N/A } else {
0N/A return new File(dir, filename);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a File object constructed from the given path string.
0N/A */
0N/A public File createFileObject(String path) {
0N/A File f = new File(path);
0N/A if (isFileSystemRoot(f)) {
0N/A f = createFileSystemRoot(f);
0N/A }
0N/A return f;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Gets the list of shown (i.e. not hidden) files.
0N/A */
0N/A public File[] getFiles(File dir, boolean useFileHiding) {
1915N/A List<File> files = new ArrayList<File>();
0N/A
0N/A // add all files in dir
1915N/A if (!(dir instanceof ShellFolder)) {
1915N/A try {
0N/A dir = getShellFolder(dir);
1915N/A } catch (FileNotFoundException e) {
1915N/A return new File[0];
0N/A }
1915N/A }
0N/A
1915N/A File[] names = ((ShellFolder) dir).listFiles(!useFileHiding);
0N/A
1915N/A if (names == null) {
1915N/A return new File[0];
1915N/A }
1915N/A
1915N/A for (File f : names) {
0N/A if (Thread.currentThread().isInterrupted()) {
0N/A break;
0N/A }
1915N/A
0N/A if (!(f instanceof ShellFolder)) {
0N/A if (isFileSystemRoot(f)) {
0N/A f = createFileSystemRoot(f);
0N/A }
0N/A try {
0N/A f = ShellFolder.getShellFolder(f);
0N/A } catch (FileNotFoundException e) {
0N/A // Not a valid file (wouldn't show in native file chooser)
0N/A // Example: C:\pagefile.sys
0N/A continue;
0N/A } catch (InternalError e) {
0N/A // Not a valid file (wouldn't show in native file chooser)
0N/A // Example C:\Winnt\Profiles\joe\history\History.IE5
0N/A continue;
0N/A }
0N/A }
0N/A if (!useFileHiding || !isHiddenFile(f)) {
1915N/A files.add(f);
0N/A }
0N/A }
0N/A
625N/A return files.toArray(new File[files.size()]);
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Returns the parent directory of <code>dir</code>.
0N/A * @param dir the <code>File</code> being queried
0N/A * @return the parent directory of <code>dir</code>, or
0N/A * <code>null</code> if <code>dir</code> is <code>null</code>
0N/A */
0N/A public File getParentDirectory(File dir) {
1915N/A if (dir == null || !dir.exists()) {
1915N/A return null;
1915N/A }
1915N/A
1915N/A ShellFolder sf;
1915N/A
1915N/A try {
1915N/A sf = getShellFolder(dir);
1915N/A } catch (FileNotFoundException e) {
1915N/A return null;
1915N/A }
1915N/A
1915N/A File psf = sf.getParentFile();
1915N/A
1915N/A if (psf == null) {
1915N/A return null;
1915N/A }
1915N/A
1915N/A if (isFileSystem(psf)) {
1915N/A File f = psf;
1915N/A if (!f.exists()) {
1915N/A // This could be a node under "Network Neighborhood".
1915N/A File ppsf = psf.getParentFile();
1915N/A if (ppsf == null || !isFileSystem(ppsf)) {
1915N/A // We're mostly after the exists() override for windows below.
1915N/A f = createFileSystemRoot(f);
0N/A }
0N/A }
1915N/A return f;
1915N/A } else {
1915N/A return psf;
0N/A }
0N/A }
0N/A
1915N/A /**
1915N/A * Throws {@code FileNotFoundException} if file not found or current thread was interrupted
1915N/A */
1915N/A ShellFolder getShellFolder(File f) throws FileNotFoundException {
1915N/A if (!(f instanceof ShellFolder) && !(f instanceof FileSystemRoot) && isFileSystemRoot(f)) {
0N/A f = createFileSystemRoot(f);
0N/A }
1915N/A
0N/A try {
0N/A return ShellFolder.getShellFolder(f);
0N/A } catch (InternalError e) {
0N/A System.err.println("FileSystemView.getShellFolder: f="+f);
0N/A e.printStackTrace();
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates a new <code>File</code> object for <code>f</code> with correct
0N/A * behavior for a file system root directory.
0N/A *
0N/A * @param f a <code>File</code> object representing a file system root
0N/A * directory, for example "/" on Unix or "C:\" on Windows.
0N/A * @return a new <code>File</code> object
0N/A * @since 1.4
0N/A */
0N/A protected File createFileSystemRoot(File f) {
0N/A return new FileSystemRoot(f);
0N/A }
0N/A
0N/A
0N/A
0N/A
0N/A static class FileSystemRoot extends File {
0N/A public FileSystemRoot(File f) {
0N/A super(f,"");
0N/A }
0N/A
0N/A public FileSystemRoot(String s) {
0N/A super(s);
0N/A }
0N/A
0N/A public boolean isDirectory() {
0N/A return true;
0N/A }
0N/A
0N/A public String getName() {
0N/A return getPath();
0N/A }
0N/A }
0N/A}
0N/A
0N/A/**
0N/A * FileSystemView that handles some specific unix-isms.
0N/A */
0N/Aclass UnixFileSystemView extends FileSystemView {
0N/A
0N/A private static final String newFolderString =
0N/A UIManager.getString("FileChooser.other.newFolder");
0N/A private static final String newFolderNextString =
0N/A UIManager.getString("FileChooser.other.newFolder.subsequent");
0N/A
0N/A /**
0N/A * Creates a new folder with a default folder name.
0N/A */
0N/A public File createNewFolder(File containingDir) throws IOException {
0N/A if(containingDir == null) {
0N/A throw new IOException("Containing directory is null:");
0N/A }
625N/A File newFolder;
0N/A // Unix - using OpenWindows' default folder name. Can't find one for Motif/CDE.
0N/A newFolder = createFileObject(containingDir, newFolderString);
0N/A int i = 1;
1915N/A while (newFolder.exists() && i < 100) {
0N/A newFolder = createFileObject(containingDir, MessageFormat.format(
1915N/A newFolderNextString, new Integer(i)));
0N/A i++;
0N/A }
0N/A
0N/A if(newFolder.exists()) {
0N/A throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
0N/A } else {
0N/A newFolder.mkdirs();
0N/A }
0N/A
0N/A return newFolder;
0N/A }
0N/A
0N/A public boolean isFileSystemRoot(File dir) {
1915N/A return dir != null && dir.getAbsolutePath().equals("/");
0N/A }
0N/A
0N/A public boolean isDrive(File dir) {
625N/A return isFloppyDrive(dir);
0N/A }
0N/A
0N/A public boolean isFloppyDrive(File dir) {
0N/A // Could be looking at the path for Solaris, but wouldn't be reliable.
0N/A // For example:
0N/A // return (dir != null && dir.getAbsolutePath().toLowerCase().startsWith("/floppy"));
0N/A return false;
0N/A }
0N/A
0N/A public boolean isComputerNode(File dir) {
0N/A if (dir != null) {
0N/A String parent = dir.getParent();
0N/A if (parent != null && parent.equals("/net")) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A}
0N/A
0N/A
0N/A/**
0N/A * FileSystemView that handles some specific windows concepts.
0N/A */
0N/Aclass WindowsFileSystemView extends FileSystemView {
0N/A
0N/A private static final String newFolderString =
0N/A UIManager.getString("FileChooser.win32.newFolder");
0N/A private static final String newFolderNextString =
0N/A UIManager.getString("FileChooser.win32.newFolder.subsequent");
0N/A
0N/A public Boolean isTraversable(File f) {
0N/A return Boolean.valueOf(isFileSystemRoot(f) || isComputerNode(f) || f.isDirectory());
0N/A }
0N/A
0N/A public File getChild(File parent, String fileName) {
0N/A if (fileName.startsWith("\\")
1915N/A && !fileName.startsWith("\\\\")
0N/A && isFileSystem(parent)) {
0N/A
0N/A //Path is relative to the root of parent's drive
0N/A String path = parent.getAbsolutePath();
0N/A if (path.length() >= 2
0N/A && path.charAt(1) == ':'
0N/A && Character.isLetter(path.charAt(0))) {
0N/A
0N/A return createFileObject(path.substring(0, 2) + fileName);
0N/A }
0N/A }
0N/A return super.getChild(parent, fileName);
0N/A }
0N/A
0N/A /**
0N/A * Type description for a file, directory, or folder as it would be displayed in
0N/A * a system file browser. Example from Windows: the "Desktop" folder
0N/A * is desribed as "Desktop".
0N/A *
0N/A * The Windows implementation gets information from the ShellFolder class.
0N/A */
0N/A public String getSystemTypeDescription(File f) {
1915N/A if (f == null) {
1915N/A return null;
1915N/A }
1915N/A
1915N/A try {
0N/A return getShellFolder(f).getFolderType();
1915N/A } catch (FileNotFoundException e) {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * @return the Desktop folder.
0N/A */
0N/A public File getHomeDirectory() {
0N/A return getRoots()[0];
0N/A }
0N/A
0N/A /**
0N/A * Creates a new folder with a default folder name.
0N/A */
0N/A public File createNewFolder(File containingDir) throws IOException {
0N/A if(containingDir == null) {
0N/A throw new IOException("Containing directory is null:");
0N/A }
0N/A // Using NT's default folder name
625N/A File newFolder = createFileObject(containingDir, newFolderString);
0N/A int i = 2;
1915N/A while (newFolder.exists() && i < 100) {
0N/A newFolder = createFileObject(containingDir, MessageFormat.format(
1915N/A newFolderNextString, new Integer(i)));
0N/A i++;
0N/A }
0N/A
0N/A if(newFolder.exists()) {
0N/A throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
0N/A } else {
0N/A newFolder.mkdirs();
0N/A }
0N/A
0N/A return newFolder;
0N/A }
0N/A
0N/A public boolean isDrive(File dir) {
0N/A return isFileSystemRoot(dir);
0N/A }
0N/A
1560N/A public boolean isFloppyDrive(final File dir) {
1560N/A String path = AccessController.doPrivileged(new PrivilegedAction<String>() {
1560N/A public String run() {
1560N/A return dir.getAbsolutePath();
1560N/A }
1560N/A });
1560N/A
1915N/A return path != null && (path.equals("A:\\") || path.equals("B:\\"));
0N/A }
0N/A
0N/A /**
0N/A * Returns a File object constructed from the given path string.
0N/A */
0N/A public File createFileObject(String path) {
0N/A // Check for missing backslash after drive letter such as "C:" or "C:filename"
0N/A if (path.length() >= 2 && path.charAt(1) == ':' && Character.isLetter(path.charAt(0))) {
0N/A if (path.length() == 2) {
0N/A path += "\\";
0N/A } else if (path.charAt(2) != '\\') {
0N/A path = path.substring(0, 2) + "\\" + path.substring(2);
0N/A }
0N/A }
0N/A return super.createFileObject(path);
0N/A }
0N/A
0N/A protected File createFileSystemRoot(File f) {
0N/A // Problem: Removable drives on Windows return false on f.exists()
0N/A // Workaround: Override exists() to always return true.
0N/A return new FileSystemRoot(f) {
0N/A public boolean exists() {
0N/A return true;
0N/A }
0N/A };
0N/A }
0N/A
0N/A}
0N/A
0N/A/**
0N/A * Fallthrough FileSystemView in case we can't determine the OS.
0N/A */
0N/Aclass GenericFileSystemView extends FileSystemView {
0N/A
0N/A private static final String newFolderString =
0N/A UIManager.getString("FileChooser.other.newFolder");
0N/A
0N/A /**
0N/A * Creates a new folder with a default folder name.
0N/A */
0N/A public File createNewFolder(File containingDir) throws IOException {
0N/A if(containingDir == null) {
0N/A throw new IOException("Containing directory is null:");
0N/A }
0N/A // Using NT's default folder name
625N/A File newFolder = createFileObject(containingDir, newFolderString);
0N/A
0N/A if(newFolder.exists()) {
0N/A throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
0N/A } else {
0N/A newFolder.mkdirs();
0N/A }
0N/A
0N/A return newFolder;
0N/A }
0N/A
0N/A}