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