001 /* 002 003 ============================================================================ 004 The Apache Software License, Version 1.1 005 ============================================================================ 006 007 Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. 008 009 Redistribution and use in source and binary forms, with or without modifica- 010 tion, are permitted provided that the following conditions are met: 011 012 1. Redistributions of source code must retain the above copyright notice, 013 this list of conditions and the following disclaimer. 014 015 2. Redistributions in binary form must reproduce the above copyright notice, 016 this list of conditions and the following disclaimer in the documentation 017 and/or other materials provided with the distribution. 018 019 3. The end-user documentation included with the redistribution, if any, must 020 include the following acknowledgment: "This product includes software 021 developed by the Apache Software Foundation (http://www.apache.org/)." 022 Alternately, this acknowledgment may appear in the software itself, if 023 and wherever such third-party acknowledgments normally appear. 024 025 4. The names "Batik" and "Apache Software Foundation" must not be 026 used to endorse or promote products derived from this software without 027 prior written permission. For written permission, please contact 028 apache@apache.org. 029 030 5. Products derived from this software may not be called "Apache", nor may 031 "Apache" appear in their name, without prior written permission of the 032 Apache Software Foundation. 033 034 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 035 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 036 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 037 APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 038 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- 039 DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 040 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 041 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 042 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 043 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 044 045 This software consists of voluntary contributions made by many individuals 046 on behalf of the Apache Software Foundation. For more information on the 047 Apache Software Foundation, please see <http://www.apache.org/>. 048 049 */ 050 051 package org.apache.batik.apps.svgbrowser; 052 053 import java.io.File; 054 import java.io.IOException; 055 import java.lang.reflect.Method; 056 import java.util.Vector; 057 058 import javax.swing.filechooser.FileSystemView; 059 060 /** 061 * Work around FileSystemView implementation bug on the Windows 062 * platform. See: 063 * 064 * <a href="http://forums.java.sun.com/thread.jsp?forum=38&thread=71491"> 065 * Using JFileChooser in WebStart-deployed application</a> 066 * 067 * @author <a href="mailto:vhardy@apache.org">Vincent Hardy</a> 068 * @version $Id: WindowsAltFileSystemView.java,v 1.3 2003/08/08 11:38:50 vhardy Exp $ 069 */ 070 071 // This class is necessary due to an annoying bug on Windows NT where 072 // instantiating a JFileChooser with the default FileSystemView will 073 // cause a "drive A: not ready" error every time. I grabbed the 074 // Windows FileSystemView impl from the 1.3 SDK and modified it so 075 // as to not use java.io.File.listRoots() to get fileSystem roots. 076 // java.io.File.listRoots() does a SecurityManager.checkRead() which 077 // causes the OS to try to access drive A: even when there is no disk, 078 // causing an annoying "abort, retry, ignore" popup message every time 079 // we instantiate a JFileChooser! 080 // 081 // Instead of calling listRoots() we use a straightforward alternate 082 // method of getting file system roots. 083 084 class WindowsAltFileSystemView extends FileSystemView { 085 public static final String EXCEPTION_CONTAINING_DIR_NULL 086 = "AltFileSystemView.exception.containing.dir.null"; 087 088 public static final String EXCEPTION_DIRECTORY_ALREADY_EXISTS 089 = "AltFileSystemView.exception.directory.already.exists"; 090 091 public static final String NEW_FOLDER_NAME = 092 " AltFileSystemView.new.folder.name"; 093 094 public static final String FLOPPY_DRIVE = 095 "AltFileSystemView.floppy.drive"; 096 097 private static final Object[] noArgs = {}; 098 private static final Class[] noArgTypes = {}; 099 100 private static Method listRootsMethod = null; 101 private static boolean listRootsMethodChecked = false; 102 103 /** 104 * Returns true if the given file is a root. 105 */ 106 public boolean isRoot(File f) { 107 if(!f.isAbsolute()) { 108 return false; 109 } 110 111 String parentPath = f.getParent(); 112 if(parentPath == null) { 113 return true; 114 } else { 115 File parent = new File(parentPath); 116 return parent.equals(f); 117 } 118 } 119 120 /** 121 * creates a new folder with a default folder name. 122 */ 123 public File createNewFolder(File containingDir) throws 124 IOException { 125 if(containingDir == null) { 126 throw new IOException(Resources.getString(EXCEPTION_CONTAINING_DIR_NULL)); 127 } 128 File newFolder = null; 129 // Using NT's default folder name 130 newFolder = createFileObject(containingDir, 131 Resources.getString(NEW_FOLDER_NAME)); 132 int i = 2; 133 while (newFolder.exists() && (i < 100)) { 134 newFolder = createFileObject 135 (containingDir, Resources.getString(NEW_FOLDER_NAME) + " (" + i + ")"); 136 i++; 137 } 138 139 if(newFolder.exists()) { 140 throw new IOException 141 (Resources.formatMessage(EXCEPTION_DIRECTORY_ALREADY_EXISTS, 142 new Object[]{newFolder.getAbsolutePath()})); 143 } else { 144 newFolder.mkdirs(); 145 } 146 147 return newFolder; 148 } 149 150 /** 151 * Returns whether a file is hidden or not. On Windows 152 * there is currently no way to get this information from 153 * io.File, therefore always return false. 154 */ 155 public boolean isHiddenFile(File f) { 156 return false; 157 } 158 159 /** 160 * Returns all root partitians on this system. On Windows, this 161 * will be the A: through Z: drives. 162 */ 163 public File[] getRoots() { 164 165 Vector rootsVector = new Vector(); 166 167 // Create the A: drive whether it is mounted or not 168 FileSystemRoot floppy = new FileSystemRoot(Resources.getString(FLOPPY_DRIVE) 169 + "\\"); 170 rootsVector.addElement(floppy); 171 172 // Run through all possible mount points and check 173 // for their existance. 174 for (char c = 'C'; c <= 'Z'; c++) { 175 char device[] = {c, ':', '\\'}; 176 String deviceName = new String(device); 177 File deviceFile = new FileSystemRoot(deviceName); 178 if (deviceFile != null && deviceFile.exists()) { 179 rootsVector.addElement(deviceFile); 180 } 181 } 182 File[] roots = new File[rootsVector.size()]; 183 rootsVector.copyInto(roots); 184 return roots; 185 } 186 187 class FileSystemRoot extends File { Rate188 public FileSystemRoot(File f) { 189 super(f, ""); 190 } 191 192 public FileSystemRoot(String s) { 193 super(s); 194 } 195 196 public boolean isDirectory() { 197 return true; 198 } 199 } 200 201 }