Source code: org/apache/batik/apps/svgbrowser/WindowsAltFileSystemView.java
1 /*
2
3 Copyright 2002-2003 The Apache Software Foundation
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16
17 */
18 package org.apache.batik.apps.svgbrowser;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.lang.reflect.Method;
23 import java.util.Vector;
24
25 import javax.swing.filechooser.FileSystemView;
26
27 /**
28 * Work around FileSystemView implementation bug on the Windows
29 * platform. See:
30 *
31 * <a href="http://forums.java.sun.com/thread.jsp?forum=38&thread=71491">
32 * Using JFileChooser in WebStart-deployed application</a>
33 *
34 * @author <a href="mailto:vhardy@apache.org">Vincent Hardy</a>
35 * @version $Id: WindowsAltFileSystemView.java,v 1.4 2004/08/18 07:12:27 vhardy Exp $
36 */
37
38 // This class is necessary due to an annoying bug on Windows NT where
39 // instantiating a JFileChooser with the default FileSystemView will
40 // cause a "drive A: not ready" error every time. I grabbed the
41 // Windows FileSystemView impl from the 1.3 SDK and modified it so
42 // as to not use java.io.File.listRoots() to get fileSystem roots.
43 // java.io.File.listRoots() does a SecurityManager.checkRead() which
44 // causes the OS to try to access drive A: even when there is no disk,
45 // causing an annoying "abort, retry, ignore" popup message every time
46 // we instantiate a JFileChooser!
47 //
48 // Instead of calling listRoots() we use a straightforward alternate
49 // method of getting file system roots.
50
51 class WindowsAltFileSystemView extends FileSystemView {
52 public static final String EXCEPTION_CONTAINING_DIR_NULL
53 = "AltFileSystemView.exception.containing.dir.null";
54
55 public static final String EXCEPTION_DIRECTORY_ALREADY_EXISTS
56 = "AltFileSystemView.exception.directory.already.exists";
57
58 public static final String NEW_FOLDER_NAME =
59 " AltFileSystemView.new.folder.name";
60
61 public static final String FLOPPY_DRIVE =
62 "AltFileSystemView.floppy.drive";
63
64 private static final Object[] noArgs = {};
65 private static final Class[] noArgTypes = {};
66
67 private static Method listRootsMethod = null;
68 private static boolean listRootsMethodChecked = false;
69
70 /**
71 * Returns true if the given file is a root.
72 */
73 public boolean isRoot(File f) {
74 if(!f.isAbsolute()) {
75 return false;
76 }
77
78 String parentPath = f.getParent();
79 if(parentPath == null) {
80 return true;
81 } else {
82 File parent = new File(parentPath);
83 return parent.equals(f);
84 }
85 }
86
87 /**
88 * creates a new folder with a default folder name.
89 */
90 public File createNewFolder(File containingDir) throws
91 IOException {
92 if(containingDir == null) {
93 throw new IOException(Resources.getString(EXCEPTION_CONTAINING_DIR_NULL));
94 }
95 File newFolder = null;
96 // Using NT's default folder name
97 newFolder = createFileObject(containingDir,
98 Resources.getString(NEW_FOLDER_NAME));
99 int i = 2;
100 while (newFolder.exists() && (i < 100)) {
101 newFolder = createFileObject
102 (containingDir, Resources.getString(NEW_FOLDER_NAME) + " (" + i + ")");
103 i++;
104 }
105
106 if(newFolder.exists()) {
107 throw new IOException
108 (Resources.formatMessage(EXCEPTION_DIRECTORY_ALREADY_EXISTS,
109 new Object[]{newFolder.getAbsolutePath()}));
110 } else {
111 newFolder.mkdirs();
112 }
113
114 return newFolder;
115 }
116
117 /**
118 * Returns whether a file is hidden or not. On Windows
119 * there is currently no way to get this information from
120 * io.File, therefore always return false.
121 */
122 public boolean isHiddenFile(File f) {
123 return false;
124 }
125
126 /**
127 * Returns all root partitians on this system. On Windows, this
128 * will be the A: through Z: drives.
129 */
130 public File[] getRoots() {
131
132 Vector rootsVector = new Vector();
133
134 // Create the A: drive whether it is mounted or not
135 FileSystemRoot floppy = new FileSystemRoot(Resources.getString(FLOPPY_DRIVE)
136 + "\\");
137 rootsVector.addElement(floppy);
138
139 // Run through all possible mount points and check
140 // for their existance.
141 for (char c = 'C'; c <= 'Z'; c++) {
142 char device[] = {c, ':', '\\'};
143 String deviceName = new String(device);
144 File deviceFile = new FileSystemRoot(deviceName);
145 if (deviceFile != null && deviceFile.exists()) {
146 rootsVector.addElement(deviceFile);
147 }
148 }
149 File[] roots = new File[rootsVector.size()];
150 rootsVector.copyInto(roots);
151 return roots;
152 }
153
154 class FileSystemRoot extends File {
155 public FileSystemRoot(File f) {
156 super(f, "");
157 }
158
159 public FileSystemRoot(String s) {
160 super(s);
161 }
162
163 public boolean isDirectory() {
164 return true;
165 }
166 }
167
168 }
169