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