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