jdk/src/share/classes/javax/swing/filechooser/FileSystemView.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 462 5c2ae7cc263d
child 1301 15e81207e1f2
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 462
diff changeset
     2
 * Copyright 1998-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.filechooser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Vector;
462
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
    37
import java.lang.ref.WeakReference;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.awt.shell.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * FileSystemView is JFileChooser's gateway to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * file system. Since the JDK1.1 File API doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * access to such information as root partitions, file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * information, or hidden file bits, this class is designed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * to intuit as much OS-specific file system information as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Java Licensees may want to provide a different implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * FileSystemView to better handle a given operating system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Jeff Dinkins
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
// PENDING(jeff) - need to provide a specification for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
// how Mac/OS2/BeOS/etc file systems can modify FileSystemView
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
// to handle their particular type of file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public abstract class FileSystemView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static FileSystemView windowsFileSystemView = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static FileSystemView unixFileSystemView = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    //static FileSystemView macFileSystemView = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static FileSystemView genericFileSystemView = null;
462
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
    69
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
    70
    private boolean useSystemExtensionHiding =
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
    71
            UIManager.getDefaults().getBoolean("FileChooser.useSystemExtensionHiding");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static FileSystemView getFileSystemView() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if(File.separatorChar == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if(windowsFileSystemView == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                windowsFileSystemView = new WindowsFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return windowsFileSystemView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if(File.separatorChar == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if(unixFileSystemView == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                unixFileSystemView = new UnixFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return unixFileSystemView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // if(File.separatorChar == ':') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        //    if(macFileSystemView == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        //      macFileSystemView = new MacFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        //    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        //    return macFileSystemView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        //}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if(genericFileSystemView == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            genericFileSystemView = new GenericFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return genericFileSystemView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
462
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   101
    public FileSystemView() {
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   102
        final WeakReference<FileSystemView> weakReference = new WeakReference<FileSystemView>(this);
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   103
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   104
        UIManager.addPropertyChangeListener(new PropertyChangeListener() {
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   105
            public void propertyChange(PropertyChangeEvent evt) {
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   106
                FileSystemView fileSystemView = weakReference.get();
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   107
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   108
                if (fileSystemView == null) {
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   109
                    // FileSystemView was destroyed
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   110
                    UIManager.removePropertyChangeListener(this);
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   111
                } else {
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   112
                    if (evt.getPropertyName().equals("lookAndFeel")) {
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   113
                        fileSystemView.useSystemExtensionHiding =
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   114
                                UIManager.getDefaults().getBoolean("FileChooser.useSystemExtensionHiding");
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   115
                    }
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   116
                }
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   117
            }
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   118
        });
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   119
    }
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   120
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Determines if the given file is a root in the navigatable tree(s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Examples: Windows 98 has one root, the Desktop folder. DOS has one root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * per drive letter, <code>C:\</code>, <code>D:\</code>, etc. Unix has one root,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * the <code>"/"</code> directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * The default implementation gets information from the <code>ShellFolder</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param f a <code>File</code> object representing a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @return <code>true</code> if <code>f</code> is a root in the navigatable tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @see #isFileSystemRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public boolean isRoot(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (f == null || !f.isAbsolute()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        File[] roots = getRoots();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        for (int i = 0; i < roots.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (roots[i].equals(f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Returns true if the file (directory) can be visited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Returns false if the directory cannot be traversed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param f the <code>File</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @return <code>true</code> if the file/directory can be traversed, otherwise <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @see JFileChooser#isTraversable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @see FileView#isTraversable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public Boolean isTraversable(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return Boolean.valueOf(f.isDirectory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Name of a file, directory, or folder as it would be displayed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * a system file browser. Example from Windows: the "M:\" directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * displays as "CD-ROM (M:)"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * The default implementation gets information from the ShellFolder class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param f a <code>File</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @return the file name as it would be displayed by a native file chooser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @see JFileChooser#getName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public String getSystemDisplayName(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        String name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (f != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            name = f.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            if (!name.equals("..") && !name.equals(".") &&
462
5c2ae7cc263d 6688203: Memory leak and performance problems in the method getFileSystemView of FileSystemView
rupashka
parents: 2
diff changeset
   178
                (useSystemExtensionHiding ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                 !isFileSystem(f) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                 isFileSystemRoot(f)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                ((f instanceof ShellFolder) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                 f.exists())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                name = getShellFolder(f).getDisplayName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                if (name == null || name.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    name = f.getPath(); // e.g. "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Type description for a file, directory, or folder as it would be displayed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * a system file browser. Example from Windows: the "Desktop" folder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * is desribed as "Desktop".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Override for platforms with native ShellFolder implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param f a <code>File</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @return the file type description as it would be displayed by a native file chooser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * or null if no native information is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @see JFileChooser#getTypeDescription
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public String getSystemTypeDescription(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Icon for a file, directory, or folder as it would be displayed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * a system file browser. Example from Windows: the "M:\" directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * displays a CD-ROM icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * The default implementation gets information from the ShellFolder class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param f a <code>File</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @return an icon as it would be displayed by a native file chooser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @see JFileChooser#getIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public Icon getSystemIcon(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (f != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            ShellFolder sf = getShellFolder(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            Image img = sf.getIcon(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (img != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                return new ImageIcon(img, sf.getFolderType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * On Windows, a file can appear in multiple folders, other than its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * parent directory in the filesystem. Folder could for example be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * "Desktop" folder which is not the same as file.getParentFile().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param folder a <code>File</code> object repesenting a directory or special folder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param file a <code>File</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @return <code>true</code> if <code>folder</code> is a directory or special folder and contains <code>file</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public boolean isParent(File folder, File file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (folder == null || file == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } else if (folder instanceof ShellFolder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                File parent = file.getParentFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                if (parent != null && parent.equals(folder)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            File[] children = getFiles(folder, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            for (int i = 0; i < children.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                if (file.equals(children[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return folder.equals(file.getParentFile());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param parent a <code>File</code> object repesenting a directory or special folder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param fileName a name of a file or folder which exists in <code>parent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @return a File object. This is normally constructed with <code>new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * File(parent, fileName)</code> except when parent and child are both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * special folders, in which case the <code>File</code> is a wrapper containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * a <code>ShellFolder</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public File getChild(File parent, String fileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (parent instanceof ShellFolder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            File[] children = getFiles(parent, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            for (int i = 0; i < children.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                if (children[i].getName().equals(fileName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    return children[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return createFileObject(parent, fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Checks if <code>f</code> represents a real directory or file as opposed to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * special folder such as <code>"Desktop"</code>. Used by UI classes to decide if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * a folder is selectable when doing directory choosing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param f a <code>File</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @return <code>true</code> if <code>f</code> is a real file or directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public boolean isFileSystem(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (f instanceof ShellFolder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            ShellFolder sf = (ShellFolder)f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            // Shortcuts to directories are treated as not being file system objects,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // so that they are never returned by JFileChooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            return sf.isFileSystem() && !(sf.isLink() && sf.isDirectory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Creates a new folder with a default folder name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public abstract File createNewFolder(File containingDir) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Returns whether a file is hidden or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public boolean isHiddenFile(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return f.isHidden();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Is dir the root of a tree in the file system, such as a drive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * or partition. Example: Returns true for "C:\" on Windows 98.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @param dir a <code>File</code> object representing a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @return <code>true</code> if <code>f</code> is a root of a filesystem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @see #isRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public boolean isFileSystemRoot(File dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return ShellFolder.isFileSystemRoot(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Used by UI classes to decide whether to display a special icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * for drives or partitions, e.g. a "hard disk" icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * The default implementation has no way of knowing, so always returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param dir a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @return <code>false</code> always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public boolean isDrive(File dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Used by UI classes to decide whether to display a special icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * for a floppy disk. Implies isDrive(dir).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * The default implementation has no way of knowing, so always returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param dir a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return <code>false</code> always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public boolean isFloppyDrive(File dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Used by UI classes to decide whether to display a special icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * for a computer node, e.g. "My Computer" or a network server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * The default implementation has no way of knowing, so always returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param dir a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @return <code>false</code> always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public boolean isComputerNode(File dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return ShellFolder.isComputerNode(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Returns all root partitions on this system. For example, on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Windows, this would be the "Desktop" folder, while on DOS this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * would be the A: through Z: drives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public File[] getRoots() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // Don't cache this array, because filesystem might change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        File[] roots = (File[])ShellFolder.get("roots");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        for (int i = 0; i < roots.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if (isFileSystemRoot(roots[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                roots[i] = createFileSystemRoot(roots[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return roots;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    // Providing default implementations for the remaining methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    // because most OS file systems will likely be able to use this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    // code. If a given OS can't, override these methods in its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    // implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public File getHomeDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return createFileObject(System.getProperty("user.home"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Return the user's default starting directory for the file chooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @return a <code>File</code> object representing the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *         starting folder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public File getDefaultDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        File f = (File)ShellFolder.get("fileChooserDefaultFolder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (isFileSystemRoot(f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            f = createFileSystemRoot(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Returns a File object constructed in dir from the given filename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public File createFileObject(File dir, String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if(dir == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            return new File(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            return new File(dir, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Returns a File object constructed from the given path string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public File createFileObject(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        File f = new File(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (isFileSystemRoot(f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            f = createFileSystemRoot(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Gets the list of shown (i.e. not hidden) files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public File[] getFiles(File dir, boolean useFileHiding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        Vector files = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // add all files in dir
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        File[] names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (!(dir instanceof ShellFolder)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                dir = getShellFolder(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            names = ((ShellFolder)dir).listFiles(!useFileHiding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        File f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        int nameCount = (names == null) ? 0 : names.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        for (int i = 0; i < nameCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            if (Thread.currentThread().isInterrupted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            f = names[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            if (!(f instanceof ShellFolder)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                if (isFileSystemRoot(f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    f = createFileSystemRoot(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    f = ShellFolder.getShellFolder(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    // Not a valid file (wouldn't show in native file chooser)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    // Example: C:\pagefile.sys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                } catch (InternalError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    // Not a valid file (wouldn't show in native file chooser)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    // Example C:\Winnt\Profiles\joe\history\History.IE5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            if (!useFileHiding || !isHiddenFile(f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                files.addElement(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        return (File[])files.toArray(new File[files.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Returns the parent directory of <code>dir</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @param dir the <code>File</code> being queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @return the parent directory of <code>dir</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *   <code>null</code> if <code>dir</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    public File getParentDirectory(File dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (dir != null && dir.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            ShellFolder sf = getShellFolder(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            File psf = sf.getParentFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            if (psf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                if (isFileSystem(psf)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    File f = psf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    if (f != null && !f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        // This could be a node under "Network Neighborhood".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        File ppsf = psf.getParentFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        if (ppsf == null || !isFileSystem(ppsf)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                            // We're mostly after the exists() override for windows below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                            f = createFileSystemRoot(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    return psf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    ShellFolder getShellFolder(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (!(f instanceof ShellFolder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            && !(f instanceof FileSystemRoot)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            && isFileSystemRoot(f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            f = createFileSystemRoot(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            return ShellFolder.getShellFolder(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            System.err.println("FileSystemView.getShellFolder: f="+f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        } catch (InternalError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            System.err.println("FileSystemView.getShellFolder: f="+f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Creates a new <code>File</code> object for <code>f</code> with correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * behavior for a file system root directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @param f a <code>File</code> object representing a file system root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *          directory, for example "/" on Unix or "C:\" on Windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @return a new <code>File</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    protected File createFileSystemRoot(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return new FileSystemRoot(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    static class FileSystemRoot extends File {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        public FileSystemRoot(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            super(f,"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        public FileSystemRoot(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            super(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        public boolean isDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            return getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
 * FileSystemView that handles some specific unix-isms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
class UnixFileSystemView extends FileSystemView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    private static final String newFolderString =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            UIManager.getString("FileChooser.other.newFolder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    private static final String newFolderNextString  =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            UIManager.getString("FileChooser.other.newFolder.subsequent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * Creates a new folder with a default folder name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public File createNewFolder(File containingDir) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        if(containingDir == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            throw new IOException("Containing directory is null:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        File newFolder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        // Unix - using OpenWindows' default folder name. Can't find one for Motif/CDE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        newFolder = createFileObject(containingDir, newFolderString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        int i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        while (newFolder.exists() && (i < 100)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            newFolder = createFileObject(containingDir, MessageFormat.format(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    newFolderNextString, new Object[] { new Integer(i) }));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if(newFolder.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            newFolder.mkdirs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        return newFolder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    public boolean isFileSystemRoot(File dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        return (dir != null && dir.getAbsolutePath().equals("/"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    public boolean isDrive(File dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if (isFloppyDrive(dir)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    public boolean isFloppyDrive(File dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        // Could be looking at the path for Solaris, but wouldn't be reliable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        // For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        // return (dir != null && dir.getAbsolutePath().toLowerCase().startsWith("/floppy"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    public boolean isComputerNode(File dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (dir != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            String parent = dir.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            if (parent != null && parent.equals("/net")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
 * FileSystemView that handles some specific windows concepts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
class WindowsFileSystemView extends FileSystemView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    private static final String newFolderString =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            UIManager.getString("FileChooser.win32.newFolder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    private static final String newFolderNextString  =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            UIManager.getString("FileChooser.win32.newFolder.subsequent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    public Boolean isTraversable(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        return Boolean.valueOf(isFileSystemRoot(f) || isComputerNode(f) || f.isDirectory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    public File getChild(File parent, String fileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        if (fileName.startsWith("\\")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            && !(fileName.startsWith("\\\\"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            && isFileSystem(parent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            //Path is relative to the root of parent's drive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            String path = parent.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            if (path.length() >= 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                && path.charAt(1) == ':'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                && Character.isLetter(path.charAt(0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                return createFileObject(path.substring(0, 2) + fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        return super.getChild(parent, fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * Type description for a file, directory, or folder as it would be displayed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * a system file browser. Example from Windows: the "Desktop" folder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * is desribed as "Desktop".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * The Windows implementation gets information from the ShellFolder class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    public String getSystemTypeDescription(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        if (f != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            return getShellFolder(f).getFolderType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @return the Desktop folder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    public File getHomeDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        return getRoots()[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * Creates a new folder with a default folder name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    public File createNewFolder(File containingDir) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        if(containingDir == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            throw new IOException("Containing directory is null:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        File newFolder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        // Using NT's default folder name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        newFolder = createFileObject(containingDir, newFolderString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        int i = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        while (newFolder.exists() && (i < 100)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            newFolder = createFileObject(containingDir, MessageFormat.format(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                newFolderNextString, new Object[] { new Integer(i) }));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        if(newFolder.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            newFolder.mkdirs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        return newFolder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    public boolean isDrive(File dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        return isFileSystemRoot(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    public boolean isFloppyDrive(File dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        String path = dir.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        return (path != null && (path.equals("A:\\") || path.equals("B:\\")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * Returns a File object constructed from the given path string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    public File createFileObject(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        // Check for missing backslash after drive letter such as "C:" or "C:filename"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if (path.length() >= 2 && path.charAt(1) == ':' && Character.isLetter(path.charAt(0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if (path.length() == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                path += "\\";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            } else if (path.charAt(2) != '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                path = path.substring(0, 2) + "\\" + path.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        return super.createFileObject(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    protected File createFileSystemRoot(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        // Problem: Removable drives on Windows return false on f.exists()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        // Workaround: Override exists() to always return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        return new FileSystemRoot(f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            public boolean exists() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
 * Fallthrough FileSystemView in case we can't determine the OS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
class GenericFileSystemView extends FileSystemView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    private static final String newFolderString =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            UIManager.getString("FileChooser.other.newFolder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * Creates a new folder with a default folder name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    public File createNewFolder(File containingDir) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if(containingDir == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            throw new IOException("Containing directory is null:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        File newFolder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        // Using NT's default folder name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        newFolder = createFileObject(containingDir, newFolderString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        if(newFolder.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            newFolder.mkdirs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        return newFolder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
}