KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > apps > svgbrowser > WindowsAltFileSystemView


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 JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import javax.swing.filechooser.FileSystemView JavaDoc;
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 JavaDoc {
52     public static final String JavaDoc EXCEPTION_CONTAINING_DIR_NULL
53         = "AltFileSystemView.exception.containing.dir.null";
54
55     public static final String JavaDoc EXCEPTION_DIRECTORY_ALREADY_EXISTS
56         = "AltFileSystemView.exception.directory.already.exists";
57
58     public static final String JavaDoc NEW_FOLDER_NAME =
59         " AltFileSystemView.new.folder.name";
60
61     public static final String JavaDoc FLOPPY_DRIVE =
62         "AltFileSystemView.floppy.drive";
63
64     private static final Object JavaDoc[] noArgs = {};
65     private static final Class JavaDoc[] noArgTypes = {};
66     
67     private static Method JavaDoc 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 JavaDoc f) {
74         if(!f.isAbsolute()) {
75             return false;
76         }
77         
78         String JavaDoc parentPath = f.getParent();
79         if(parentPath == null) {
80             return true;
81         } else {
82             File JavaDoc parent = new File JavaDoc(parentPath);
83             return parent.equals(f);
84         }
85     }
86     
87     /**
88      * creates a new folder with a default folder name.
89      */

90     public File JavaDoc createNewFolder(File JavaDoc containingDir) throws
91         IOException JavaDoc {
92         if(containingDir == null) {
93             throw new IOException JavaDoc(Resources.getString(EXCEPTION_CONTAINING_DIR_NULL));
94         }
95         File JavaDoc 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 JavaDoc
108                 (Resources.formatMessage(EXCEPTION_DIRECTORY_ALREADY_EXISTS,
109                                          new Object JavaDoc[]{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 JavaDoc 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 JavaDoc[] getRoots() {
131         
132         Vector JavaDoc rootsVector = new Vector JavaDoc();
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 JavaDoc deviceName = new String JavaDoc(device);
144             File JavaDoc deviceFile = new FileSystemRoot(deviceName);
145             if (deviceFile != null && deviceFile.exists()) {
146                 rootsVector.addElement(deviceFile);
147             }
148         }
149         File JavaDoc[] roots = new File JavaDoc[rootsVector.size()];
150         rootsVector.copyInto(roots);
151         return roots;
152     }
153     
154     class FileSystemRoot extends File JavaDoc {
155         public FileSystemRoot(File JavaDoc f) {
156             super(f, "");
157         }
158         
159         public FileSystemRoot(String JavaDoc s) {
160             super(s);
161         }
162         
163         public boolean isDirectory() {
164             return true;
165         }
166     }
167     
168 }
169
170
Popular Tags