author | serb |
Fri, 22 Sep 2017 17:36:55 -0700 | |
changeset 47368 | 147cb4cf2556 |
parent 47216 | 71c04702a3d5 |
child 52249 | 715642098c0b |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.swing.filechooser; |
|
27 |
||
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 | 30 |
import java.io.File; |
31 |
import java.io.FileNotFoundException; |
|
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 | 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 | 47 |
|
48 |
/** |
|
49 |
* FileSystemView is JFileChooser's gateway to the |
|
50 |
* file system. Since the JDK1.1 File API doesn't allow |
|
51 |
* access to such information as root partitions, file type |
|
52 |
* information, or hidden file bits, this class is designed |
|
53 |
* to intuit as much OS-specific file system information as |
|
54 |
* possible. |
|
55 |
* |
|
56 |
* <p> |
|
57 |
* |
|
58 |
* Java Licensees may want to provide a different implementation of |
|
59 |
* FileSystemView to better handle a given operating system. |
|
60 |
* |
|
61 |
* @author Jeff Dinkins |
|
62 |
*/ |
|
63 |
||
64 |
// PENDING(jeff) - need to provide a specification for |
|
65 |
// how Mac/OS2/BeOS/etc file systems can modify FileSystemView |
|
66 |
// to handle their particular type of file system. |
|
67 |
||
68 |
public abstract class FileSystemView { |
|
69 |
||
70 |
static FileSystemView windowsFileSystemView = null; |
|
71 |
static FileSystemView unixFileSystemView = null; |
|
72 |
//static FileSystemView macFileSystemView = null; |
|
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 | 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 | 82 |
public static FileSystemView getFileSystemView() { |
83 |
if(File.separatorChar == '\\') { |
|
84 |
if(windowsFileSystemView == null) { |
|
85 |
windowsFileSystemView = new WindowsFileSystemView(); |
|
86 |
} |
|
87 |
return windowsFileSystemView; |
|
88 |
} |
|
89 |
||
90 |
if(File.separatorChar == '/') { |
|
91 |
if(unixFileSystemView == null) { |
|
92 |
unixFileSystemView = new UnixFileSystemView(); |
|
93 |
} |
|
94 |
return unixFileSystemView; |
|
95 |
} |
|
96 |
||
97 |
// if(File.separatorChar == ':') { |
|
98 |
// if(macFileSystemView == null) { |
|
99 |
// macFileSystemView = new MacFileSystemView(); |
|
100 |
// } |
|
101 |
// return macFileSystemView; |
|
102 |
//} |
|
103 |
||
104 |
if(genericFileSystemView == null) { |
|
105 |
genericFileSystemView = new GenericFileSystemView(); |
|
106 |
} |
|
107 |
return genericFileSystemView; |
|
108 |
} |
|
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 | 130 |
/** |
21278 | 131 |
* Determines if the given file is a root in the navigable tree(s). |
2 | 132 |
* Examples: Windows 98 has one root, the Desktop folder. DOS has one root |
133 |
* per drive letter, <code>C:\</code>, <code>D:\</code>, etc. Unix has one root, |
|
134 |
* the <code>"/"</code> directory. |
|
135 |
* |
|
136 |
* The default implementation gets information from the <code>ShellFolder</code> class. |
|
137 |
* |
|
138 |
* @param f a <code>File</code> object representing a directory |
|
21278 | 139 |
* @return <code>true</code> if <code>f</code> is a root in the navigable tree. |
2 | 140 |
* @see #isFileSystemRoot |
141 |
*/ |
|
142 |
public boolean isRoot(File f) { |
|
143 |
if (f == null || !f.isAbsolute()) { |
|
144 |
return false; |
|
145 |
} |
|
146 |
||
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 | 150 |
return true; |
151 |
} |
|
152 |
} |
|
153 |
return false; |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* Returns true if the file (directory) can be visited. |
|
158 |
* Returns false if the directory cannot be traversed. |
|
159 |
* |
|
160 |
* @param f the <code>File</code> |
|
161 |
* @return <code>true</code> if the file/directory can be traversed, otherwise <code>false</code> |
|
162 |
* @see JFileChooser#isTraversable |
|
163 |
* @see FileView#isTraversable |
|
164 |
* @since 1.4 |
|
165 |
*/ |
|
166 |
public Boolean isTraversable(File f) { |
|
167 |
return Boolean.valueOf(f.isDirectory()); |
|
168 |
} |
|
169 |
||
170 |
/** |
|
171 |
* Name of a file, directory, or folder as it would be displayed in |
|
172 |
* a system file browser. Example from Windows: the "M:\" directory |
|
173 |
* displays as "CD-ROM (M:)" |
|
174 |
* |
|
175 |
* The default implementation gets information from the ShellFolder class. |
|
176 |
* |
|
177 |
* @param f a <code>File</code> object |
|
178 |
* @return the file name as it would be displayed by a native file chooser |
|
179 |
* @see JFileChooser#getName |
|
180 |
* @since 1.4 |
|
181 |
*/ |
|
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 | 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 | 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 | 201 |
} |
202 |
} |
|
4281
b4474a4716dd
6868611: FileSystemView throws NullPointerException
rupashka
parents:
3507
diff
changeset
|
203 |
|
2 | 204 |
return name; |
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* Type description for a file, directory, or folder as it would be displayed in |
|
209 |
* a system file browser. Example from Windows: the "Desktop" folder |
|
21278 | 210 |
* is described as "Desktop". |
2 | 211 |
* |
212 |
* Override for platforms with native ShellFolder implementations. |
|
213 |
* |
|
214 |
* @param f a <code>File</code> object |
|
215 |
* @return the file type description as it would be displayed by a native file chooser |
|
216 |
* or null if no native information is available. |
|
217 |
* @see JFileChooser#getTypeDescription |
|
218 |
* @since 1.4 |
|
219 |
*/ |
|
220 |
public String getSystemTypeDescription(File f) { |
|
221 |
return null; |
|
222 |
} |
|
223 |
||
224 |
/** |
|
225 |
* Icon for a file, directory, or folder as it would be displayed in |
|
226 |
* a system file browser. Example from Windows: the "M:\" directory |
|
227 |
* displays a CD-ROM icon. |
|
228 |
* |
|
229 |
* The default implementation gets information from the ShellFolder class. |
|
230 |
* |
|
231 |
* @param f a <code>File</code> object |
|
232 |
* @return an icon as it would be displayed by a native file chooser |
|
233 |
* @see JFileChooser#getIcon |
|
234 |
* @since 1.4 |
|
235 |
*/ |
|
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 | 253 |
} else { |
4281
b4474a4716dd
6868611: FileSystemView throws NullPointerException
rupashka
parents:
3507
diff
changeset
|
254 |
return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon"); |
2 | 255 |
} |
256 |
} |
|
257 |
||
258 |
/** |
|
259 |
* On Windows, a file can appear in multiple folders, other than its |
|
260 |
* parent directory in the filesystem. Folder could for example be the |
|
261 |
* "Desktop" folder which is not the same as file.getParentFile(). |
|
262 |
* |
|
21278 | 263 |
* @param folder a <code>File</code> object representing a directory or special folder |
2 | 264 |
* @param file a <code>File</code> object |
265 |
* @return <code>true</code> if <code>folder</code> is a directory or special folder and contains <code>file</code>. |
|
266 |
* @since 1.4 |
|
267 |
*/ |
|
268 |
public boolean isParent(File folder, File file) { |
|
269 |
if (folder == null || file == null) { |
|
270 |
return false; |
|
271 |
} else if (folder instanceof ShellFolder) { |
|
272 |
File parent = file.getParentFile(); |
|
273 |
if (parent != null && parent.equals(folder)) { |
|
274 |
return true; |
|
275 |
} |
|
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 | 279 |
return true; |
280 |
} |
|
281 |
} |
|
282 |
return false; |
|
283 |
} else { |
|
284 |
return folder.equals(file.getParentFile()); |
|
285 |
} |
|
286 |
} |
|
287 |
||
288 |
/** |
|
289 |
* |
|
21278 | 290 |
* @param parent a <code>File</code> object representing a directory or special folder |
2 | 291 |
* @param fileName a name of a file or folder which exists in <code>parent</code> |
292 |
* @return a File object. This is normally constructed with <code>new |
|
293 |
* File(parent, fileName)</code> except when parent and child are both |
|
294 |
* special folders, in which case the <code>File</code> is a wrapper containing |
|
295 |
* a <code>ShellFolder</code> object. |
|
296 |
* @since 1.4 |
|
297 |
*/ |
|
298 |
public File getChild(File parent, String fileName) { |
|
299 |
if (parent instanceof ShellFolder) { |
|
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 | 304 |
} |
305 |
} |
|
306 |
} |
|
307 |
return createFileObject(parent, fileName); |
|
308 |
} |
|
309 |
||
310 |
||
311 |
/** |
|
312 |
* Checks if <code>f</code> represents a real directory or file as opposed to a |
|
313 |
* special folder such as <code>"Desktop"</code>. Used by UI classes to decide if |
|
314 |
* a folder is selectable when doing directory choosing. |
|
315 |
* |
|
316 |
* @param f a <code>File</code> object |
|
317 |
* @return <code>true</code> if <code>f</code> is a real file or directory. |
|
318 |
* @since 1.4 |
|
319 |
*/ |
|
320 |
public boolean isFileSystem(File f) { |
|
321 |
if (f instanceof ShellFolder) { |
|
322 |
ShellFolder sf = (ShellFolder)f; |
|
323 |
// Shortcuts to directories are treated as not being file system objects, |
|
324 |
// so that they are never returned by JFileChooser. |
|
325 |
return sf.isFileSystem() && !(sf.isLink() && sf.isDirectory()); |
|
326 |
} else { |
|
327 |
return true; |
|
328 |
} |
|
329 |
} |
|
330 |
||
331 |
/** |
|
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 | 337 |
*/ |
338 |
public abstract File createNewFolder(File containingDir) throws IOException; |
|
339 |
||
340 |
/** |
|
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 | 345 |
*/ |
346 |
public boolean isHiddenFile(File f) { |
|
347 |
return f.isHidden(); |
|
348 |
} |
|
349 |
||
350 |
||
351 |
/** |
|
352 |
* Is dir the root of a tree in the file system, such as a drive |
|
353 |
* or partition. Example: Returns true for "C:\" on Windows 98. |
|
354 |
* |
|
355 |
* @param dir a <code>File</code> object representing a directory |
|
356 |
* @return <code>true</code> if <code>f</code> is a root of a filesystem |
|
357 |
* @see #isRoot |
|
358 |
* @since 1.4 |
|
359 |
*/ |
|
360 |
public boolean isFileSystemRoot(File dir) { |
|
361 |
return ShellFolder.isFileSystemRoot(dir); |
|
362 |
} |
|
363 |
||
364 |
/** |
|
365 |
* Used by UI classes to decide whether to display a special icon |
|
366 |
* for drives or partitions, e.g. a "hard disk" icon. |
|
367 |
* |
|
368 |
* The default implementation has no way of knowing, so always returns false. |
|
369 |
* |
|
370 |
* @param dir a directory |
|
371 |
* @return <code>false</code> always |
|
372 |
* @since 1.4 |
|
373 |
*/ |
|
374 |
public boolean isDrive(File dir) { |
|
375 |
return false; |
|
376 |
} |
|
377 |
||
378 |
/** |
|
379 |
* Used by UI classes to decide whether to display a special icon |
|
380 |
* for a floppy disk. Implies isDrive(dir). |
|
381 |
* |
|
382 |
* The default implementation has no way of knowing, so always returns false. |
|
383 |
* |
|
384 |
* @param dir a directory |
|
385 |
* @return <code>false</code> always |
|
386 |
* @since 1.4 |
|
387 |
*/ |
|
388 |
public boolean isFloppyDrive(File dir) { |
|
389 |
return false; |
|
390 |
} |
|
391 |
||
392 |
/** |
|
393 |
* Used by UI classes to decide whether to display a special icon |
|
394 |
* for a computer node, e.g. "My Computer" or a network server. |
|
395 |
* |
|
396 |
* The default implementation has no way of knowing, so always returns false. |
|
397 |
* |
|
398 |
* @param dir a directory |
|
399 |
* @return <code>false</code> always |
|
400 |
* @since 1.4 |
|
401 |
*/ |
|
402 |
public boolean isComputerNode(File dir) { |
|
403 |
return ShellFolder.isComputerNode(dir); |
|
404 |
} |
|
405 |
||
406 |
||
407 |
/** |
|
408 |
* Returns all root partitions on this system. For example, on |
|
409 |
* Windows, this would be the "Desktop" folder, while on DOS this |
|
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 | 414 |
*/ |
415 |
public File[] getRoots() { |
|
416 |
// Don't cache this array, because filesystem might change |
|
417 |
File[] roots = (File[])ShellFolder.get("roots"); |
|
418 |
||
419 |
for (int i = 0; i < roots.length; i++) { |
|
420 |
if (isFileSystemRoot(roots[i])) { |
|
421 |
roots[i] = createFileSystemRoot(roots[i]); |
|
422 |
} |
|
423 |
} |
|
424 |
return roots; |
|
425 |
} |
|
426 |
||
427 |
||
428 |
// Providing default implementations for the remaining methods |
|
429 |
// because most OS file systems will likely be able to use this |
|
430 |
// code. If a given OS can't, override these methods in its |
|
431 |
// implementation. |
|
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 | 437 |
public File getHomeDirectory() { |
438 |
return createFileObject(System.getProperty("user.home")); |
|
439 |
} |
|
440 |
||
441 |
/** |
|
442 |
* Return the user's default starting directory for the file chooser. |
|
443 |
* |
|
444 |
* @return a <code>File</code> object representing the default |
|
445 |
* starting folder |
|
446 |
* @since 1.4 |
|
447 |
*/ |
|
448 |
public File getDefaultDirectory() { |
|
449 |
File f = (File)ShellFolder.get("fileChooserDefaultFolder"); |
|
450 |
if (isFileSystemRoot(f)) { |
|
451 |
f = createFileSystemRoot(f); |
|
452 |
} |
|
453 |
return f; |
|
454 |
} |
|
455 |
||
456 |
/** |
|
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 | 462 |
*/ |
463 |
public File createFileObject(File dir, String filename) { |
|
464 |
if(dir == null) { |
|
465 |
return new File(filename); |
|
466 |
} else { |
|
467 |
return new File(dir, filename); |
|
468 |
} |
|
469 |
} |
|
470 |
||
471 |
/** |
|
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 | 476 |
*/ |
477 |
public File createFileObject(String path) { |
|
478 |
File f = new File(path); |
|
479 |
if (isFileSystemRoot(f)) { |
|
480 |
f = createFileSystemRoot(f); |
|
481 |
} |
|
482 |
return f; |
|
483 |
} |
|
484 |
||
485 |
||
486 |
/** |
|
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 | 494 |
*/ |
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 | 497 |
|
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 | 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 | 504 |
} |
4281
b4474a4716dd
6868611: FileSystemView throws NullPointerException
rupashka
parents:
3507
diff
changeset
|
505 |
} |
2 | 506 |
|
4281
b4474a4716dd
6868611: FileSystemView throws NullPointerException
rupashka
parents:
3507
diff
changeset
|
507 |
File[] names = ((ShellFolder) dir).listFiles(!useFileHiding); |
2 | 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 | 514 |
if (Thread.currentThread().isInterrupted()) { |
515 |
break; |
|
516 |
} |
|
4281
b4474a4716dd
6868611: FileSystemView throws NullPointerException
rupashka
parents:
3507
diff
changeset
|
517 |
|
2 | 518 |
if (!(f instanceof ShellFolder)) { |
519 |
if (isFileSystemRoot(f)) { |
|
520 |
f = createFileSystemRoot(f); |
|
521 |
} |
|
522 |
try { |
|
523 |
f = ShellFolder.getShellFolder(f); |
|
524 |
} catch (FileNotFoundException e) { |
|
525 |
// Not a valid file (wouldn't show in native file chooser) |
|
526 |
// Example: C:\pagefile.sys |
|
527 |
continue; |
|
528 |
} catch (InternalError e) { |
|
529 |
// Not a valid file (wouldn't show in native file chooser) |
|
530 |
// Example C:\Winnt\Profiles\joe\history\History.IE5 |
|
531 |
continue; |
|
532 |
} |
|
533 |
} |
|
534 |
if (!useFileHiding || !isHiddenFile(f)) { |
|
4281
b4474a4716dd
6868611: FileSystemView throws NullPointerException
rupashka
parents:
3507
diff
changeset
|
535 |
files.add(f); |
2 | 536 |
} |
537 |
} |
|
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 | 540 |
} |
541 |
||
542 |
||
543 |
||
544 |
/** |
|
545 |
* Returns the parent directory of <code>dir</code>. |
|
546 |
* @param dir the <code>File</code> being queried |
|
547 |
* @return the parent directory of <code>dir</code>, or |
|
548 |
* <code>null</code> if <code>dir</code> is <code>null</code> |
|
549 |
*/ |
|
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 | 577 |
} |
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 | 582 |
} |
583 |
} |
|
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 | 653 |
f = createFileSystemRoot(f); |
654 |
} |
|
4281
b4474a4716dd
6868611: FileSystemView throws NullPointerException
rupashka
parents:
3507
diff
changeset
|
655 |
|
2 | 656 |
try { |
657 |
return ShellFolder.getShellFolder(f); |
|
658 |
} catch (InternalError e) { |
|
659 |
System.err.println("FileSystemView.getShellFolder: f="+f); |
|
660 |
e.printStackTrace(); |
|
661 |
return null; |
|
662 |
} |
|
663 |
} |
|
664 |
||
665 |
/** |
|
666 |
* Creates a new <code>File</code> object for <code>f</code> with correct |
|
667 |
* behavior for a file system root directory. |
|
668 |
* |
|
669 |
* @param f a <code>File</code> object representing a file system root |
|
670 |
* directory, for example "/" on Unix or "C:\" on Windows. |
|
671 |
* @return a new <code>File</code> object |
|
672 |
* @since 1.4 |
|
673 |
*/ |
|
674 |
protected File createFileSystemRoot(File f) { |
|
675 |
return new FileSystemRoot(f); |
|
676 |
} |
|
677 |
||
23697 | 678 |
@SuppressWarnings("serial") // Same-version serialization only |
2 | 679 |
static class FileSystemRoot extends File { |
680 |
public FileSystemRoot(File f) { |
|
681 |
super(f,""); |
|
682 |
} |
|
683 |
||
684 |
public FileSystemRoot(String s) { |
|
685 |
super(s); |
|
686 |
} |
|
687 |
||
688 |
public boolean isDirectory() { |
|
689 |
return true; |
|
690 |
} |
|
691 |
||
692 |
public String getName() { |
|
693 |
return getPath(); |
|
694 |
} |
|
695 |
} |
|
696 |
} |
|
697 |
||
698 |
/** |
|
699 |
* FileSystemView that handles some specific unix-isms. |
|
700 |
*/ |
|
701 |
class UnixFileSystemView extends FileSystemView { |
|
702 |
||
703 |
private static final String newFolderString = |
|
704 |
UIManager.getString("FileChooser.other.newFolder"); |
|
705 |
private static final String newFolderNextString = |
|
706 |
UIManager.getString("FileChooser.other.newFolder.subsequent"); |
|
707 |
||
708 |
/** |
|
709 |
* Creates a new folder with a default folder name. |
|
710 |
*/ |
|
711 |
public File createNewFolder(File containingDir) throws IOException { |
|
712 |
if(containingDir == null) { |
|
713 |
throw new IOException("Containing directory is null:"); |
|
714 |
} |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
715 |
File newFolder; |
2 | 716 |
// Unix - using OpenWindows' default folder name. Can't find one for Motif/CDE. |
717 |
newFolder = createFileObject(containingDir, newFolderString); |
|
718 |
int i = 1; |
|
4281
b4474a4716dd
6868611: FileSystemView throws NullPointerException
rupashka
parents:
3507
diff
changeset
|
719 |
while (newFolder.exists() && i < 100) { |
2 | 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 | 722 |
i++; |
723 |
} |
|
724 |
||
725 |
if(newFolder.exists()) { |
|
726 |
throw new IOException("Directory already exists:" + newFolder.getAbsolutePath()); |
|
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 | 731 |
} |
732 |
||
733 |
return newFolder; |
|
734 |
} |
|
735 |
||
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 | 738 |
} |
739 |
||
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 | 742 |
} |
743 |
||
744 |
public boolean isFloppyDrive(File dir) { |
|
745 |
// Could be looking at the path for Solaris, but wouldn't be reliable. |
|
746 |
// For example: |
|
747 |
// return (dir != null && dir.getAbsolutePath().toLowerCase().startsWith("/floppy")); |
|
748 |
return false; |
|
749 |
} |
|
750 |
||
751 |
public boolean isComputerNode(File dir) { |
|
752 |
if (dir != null) { |
|
753 |
String parent = dir.getParent(); |
|
754 |
if (parent != null && parent.equals("/net")) { |
|
755 |
return true; |
|
756 |
} |
|
757 |
} |
|
758 |
return false; |
|
759 |
} |
|
760 |
} |
|
761 |
||
762 |
||
763 |
/** |
|
764 |
* FileSystemView that handles some specific windows concepts. |
|
765 |
*/ |
|
766 |
class WindowsFileSystemView extends FileSystemView { |
|
767 |
||
768 |
private static final String newFolderString = |
|
769 |
UIManager.getString("FileChooser.win32.newFolder"); |
|
770 |
private static final String newFolderNextString = |
|
771 |
UIManager.getString("FileChooser.win32.newFolder.subsequent"); |
|
772 |
||
773 |
public Boolean isTraversable(File f) { |
|
774 |
return Boolean.valueOf(isFileSystemRoot(f) || isComputerNode(f) || f.isDirectory()); |
|
775 |
} |
|
776 |
||
777 |
public File getChild(File parent, String fileName) { |
|
778 |
if (fileName.startsWith("\\") |
|
4281
b4474a4716dd
6868611: FileSystemView throws NullPointerException
rupashka
parents:
3507
diff
changeset
|
779 |
&& !fileName.startsWith("\\\\") |
2 | 780 |
&& isFileSystem(parent)) { |
781 |
||
782 |
//Path is relative to the root of parent's drive |
|
783 |
String path = parent.getAbsolutePath(); |
|
784 |
if (path.length() >= 2 |
|
785 |
&& path.charAt(1) == ':' |
|
786 |
&& Character.isLetter(path.charAt(0))) { |
|
787 |
||
788 |
return createFileObject(path.substring(0, 2) + fileName); |
|
789 |
} |
|
790 |
} |
|
791 |
return super.getChild(parent, fileName); |
|
792 |
} |
|
793 |
||
794 |
/** |
|
795 |
* Type description for a file, directory, or folder as it would be displayed in |
|
796 |
* a system file browser. Example from Windows: the "Desktop" folder |
|
21278 | 797 |
* is described as "Desktop". |
2 | 798 |
* |
799 |
* The Windows implementation gets information from the ShellFolder class. |
|
800 |
*/ |
|
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 | 807 |
return getShellFolder(f).getFolderType(); |
4281
b4474a4716dd
6868611: FileSystemView throws NullPointerException
rupashka
parents:
3507
diff
changeset
|
808 |
} catch (FileNotFoundException e) { |
2 | 809 |
return null; |
810 |
} |
|
811 |
} |
|
812 |
||
813 |
/** |
|
814 |
* @return the Desktop folder. |
|
815 |
*/ |
|
816 |
public File getHomeDirectory() { |
|
25536 | 817 |
File[] roots = getRoots(); |
818 |
return (roots.length == 0) ? null : roots[0]; |
|
2 | 819 |
} |
820 |
||
821 |
/** |
|
822 |
* Creates a new folder with a default folder name. |
|
823 |
*/ |
|
824 |
public File createNewFolder(File containingDir) throws IOException { |
|
825 |
if(containingDir == null) { |
|
826 |
throw new IOException("Containing directory is null:"); |
|
827 |
} |
|
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 | 830 |
int i = 2; |
4281
b4474a4716dd
6868611: FileSystemView throws NullPointerException
rupashka
parents:
3507
diff
changeset
|
831 |
while (newFolder.exists() && i < 100) { |
2 | 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 | 834 |
i++; |
835 |
} |
|
836 |
||
837 |
if(newFolder.exists()) { |
|
838 |
throw new IOException("Directory already exists:" + newFolder.getAbsolutePath()); |
|
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 | 843 |
} |
844 |
||
845 |
return newFolder; |
|
846 |
} |
|
847 |
||
848 |
public boolean isDrive(File dir) { |
|
849 |
return isFileSystemRoot(dir); |
|
850 |
} |
|
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 | 860 |
} |
861 |
||
862 |
/** |
|
863 |
* Returns a File object constructed from the given path string. |
|
864 |
*/ |
|
865 |
public File createFileObject(String path) { |
|
866 |
// Check for missing backslash after drive letter such as "C:" or "C:filename" |
|
867 |
if (path.length() >= 2 && path.charAt(1) == ':' && Character.isLetter(path.charAt(0))) { |
|
868 |
if (path.length() == 2) { |
|
869 |
path += "\\"; |
|
870 |
} else if (path.charAt(2) != '\\') { |
|
871 |
path = path.substring(0, 2) + "\\" + path.substring(2); |
|
872 |
} |
|
873 |
} |
|
874 |
return super.createFileObject(path); |
|
875 |
} |
|
876 |
||
23697 | 877 |
@SuppressWarnings("serial") // anonymous class |
2 | 878 |
protected File createFileSystemRoot(File f) { |
879 |
// Problem: Removable drives on Windows return false on f.exists() |
|
880 |
// Workaround: Override exists() to always return true. |
|
881 |
return new FileSystemRoot(f) { |
|
882 |
public boolean exists() { |
|
883 |
return true; |
|
884 |
} |
|
885 |
}; |
|
886 |
} |
|
887 |
||
888 |
} |
|
889 |
||
890 |
/** |
|
891 |
* Fallthrough FileSystemView in case we can't determine the OS. |
|
892 |
*/ |
|
893 |
class GenericFileSystemView extends FileSystemView { |
|
894 |
||
895 |
private static final String newFolderString = |
|
896 |
UIManager.getString("FileChooser.other.newFolder"); |
|
897 |
||
898 |
/** |
|
899 |
* Creates a new folder with a default folder name. |
|
900 |
*/ |
|
901 |
public File createNewFolder(File containingDir) throws IOException { |
|
902 |
if(containingDir == null) { |
|
903 |
throw new IOException("Containing directory is null:"); |
|
904 |
} |
|
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 | 907 |
|
908 |
if(newFolder.exists()) { |
|
909 |
throw new IOException("Directory already exists:" + newFolder.getAbsolutePath()); |
|
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 | 914 |
} |
915 |
return newFolder; |
|
916 |
} |
|
917 |
||
918 |
} |