GNU Classpath (0.20) | |
Frames | No Frames |
1: /* FileSystemView.java -- 2: Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: package javax.swing.filechooser; 39: 40: import java.io.File; 41: import java.io.IOException; 42: import java.util.ArrayList; 43: 44: import javax.swing.Icon; 45: import javax.swing.JFileChooser; 46: 47: 48: /** 49: * The base class providing a view of the file system for use by the 50: * {@link JFileChooser} component. 51: */ 52: public abstract class FileSystemView 53: { 54: /** The instance returned by {@link #getFileSystemView()}. */ 55: private static FileSystemView defaultFileSystemView; 56: 57: /** 58: * Creates a new file object with the given name in the specified directory. 59: * 60: * @param dir the directory (<code>null</code> permitted). 61: * @param filename the file name. 62: * 63: * @return A new file object. 64: */ 65: public File createFileObject(File dir, String filename) 66: { 67: return new File(dir, filename); 68: } 69: 70: /** 71: * Creates a new file object from the specified path. 72: * 73: * @param path the path. 74: * 75: * @return A new file object. 76: */ 77: public File createFileObject(String path) 78: { 79: return new File(path); 80: } 81: 82: /** 83: * DOCUMENT ME! 84: * 85: * @param f DOCUMENT ME! 86: * 87: * @return DOCUMENT ME! 88: */ 89: protected File createFileSystemRoot(File f) 90: { 91: File[] roots = File.listRoots(); 92: if (roots == null) 93: return null; 94: return roots[0]; 95: } 96: 97: /** 98: * Creates a new folder with a unique name in the specified directory and 99: * returns a {@link File} object representing the new directory. 100: * 101: * @param containingDir the directory to contain the new folder 102: * (<code>null</code> not permitted). 103: * 104: * @return A {@link File} object representing the new directory. 105: * 106: * @throws IOException if an exception occurs while creating the new 107: * directory. 108: */ 109: public abstract File createNewFolder(File containingDir) 110: throws IOException; 111: 112: /** 113: * DOCUMENT ME! 114: * 115: * @param parent DOCUMENT ME! 116: * @param fileName DOCUMENT ME! 117: * 118: * @return DOCUMENT ME! 119: */ 120: public File getChild(File parent, String fileName) 121: { 122: // FIXME: Handle the case when parent and child are special folders. 123: return new File(parent, fileName); 124: } 125: 126: /** 127: * Returns the default directory. 128: * 129: * @return The default directory. 130: */ 131: public File getDefaultDirectory() 132: { 133: return getHomeDirectory(); 134: } 135: 136: /** 137: * Returns an array containing the files in the given directory. The 138: * <code>useFileHiding</code> controls whether or not hidden files are 139: * included in the result. 140: * 141: * @param dir the directory (if <code>null</code> 142: * @param useFileHiding a flag that controls whether or not hidden files are 143: * included in the result (pass in <code>true</code> to 144: * exclude hidden files). 145: * 146: * @return The files in the given directory (possibly <code>null</code>). 147: */ 148: public File[] getFiles(File dir, boolean useFileHiding) 149: { 150: if (dir == null || dir.listFiles() == null) 151: return null; 152: File[] files = dir.listFiles(); 153: if (! useFileHiding) 154: return files; 155: ArrayList trim = new ArrayList(); 156: for (int i = 0; i < files.length; i++) 157: if (! files[i].isHidden()) 158: trim.add(files[i]); 159: File[] value = (File[]) trim.toArray(new File[trim.size()]); 160: return value; 161: } 162: 163: /** 164: * Returns a default {@link FileSystemView} appropriate for the platform. 165: * 166: * @return A default {@link FileSystemView} appropriate for the platform. 167: */ 168: public static FileSystemView getFileSystemView() 169: { 170: if (defaultFileSystemView == null) 171: { 172: if (File.separator.equals("/")) 173: defaultFileSystemView = new UnixFileSystemView(); 174: // FIXME: need to implement additional views 175: // else if (File.Separator.equals("\")) 176: // return new Win32FileSystemView(); 177: // else 178: // return new GenericFileSystemView(); 179: } 180: return defaultFileSystemView; 181: } 182: 183: /** 184: * Returns the home directory for the current user. 185: * 186: * @return The home directory for the current user. 187: */ 188: public File getHomeDirectory() 189: { 190: return createFileObject(System.getProperty("user.home")); 191: } 192: 193: /** 194: * Returns the parent directory for the given file/directory. 195: * 196: * @param f the file/directory. 197: * 198: * @return The parent directory (or <code>null</code> if there is no parent 199: * directory). 200: */ 201: public File getParentDirectory(File f) 202: { 203: if (f == null) 204: return null; 205: return f.getParentFile(); 206: } 207: 208: /** 209: * Returns an array containing the file system roots. On Unix-like platforms, 210: * this array will contain just a single item ("/"), while other platforms 211: * may return multiple roots. 212: * <p> 213: * This method is implemented to return <code>null</code>, subclasses must 214: * override this method. 215: * 216: * @return An array containing the file system roots. 217: */ 218: public File[] getRoots() 219: { 220: // subclass 221: return null; 222: } 223: 224: /** 225: * Returns the name of a file as it would be displayed by the underlying 226: * system. This implementation returns <code>null</code>, subclasses must 227: * override. 228: * 229: * @param f the file. 230: * 231: * @return <code>null</code>. 232: */ 233: public String getSystemDisplayName(File f) 234: { 235: return null; 236: } 237: 238: /** 239: * Returns the icon that would be displayed for the given file by the 240: * underlying system. This implementation returns <code>null</code>, 241: * subclasses must override. 242: * 243: * @param f the file. 244: * 245: * @return <code>null</code>. 246: */ 247: public Icon getSystemIcon(File f) 248: { 249: return null; 250: } 251: 252: /** 253: * Returns the type description of a file that would be displayed by the 254: * underlying system. This implementation returns <code>null</code>, 255: * subclasses must override. 256: * 257: * @param f the file. 258: * 259: * @return <code>null</code>. 260: */ 261: public String getSystemTypeDescription(File f) 262: { 263: return null; 264: } 265: 266: /** 267: * DOCUMENT ME! 268: * 269: * @param dir DOCUMENT ME! 270: * 271: * @return DOCUMENT ME! 272: */ 273: public boolean isComputerNode(File dir) 274: { 275: return false; 276: } 277: 278: /** 279: * Returns <code>true</code> if the given directory represents a disk 280: * drive, and <code>false</code> otherwise. This default implementation 281: * always returns <code>false</code>. 282: * 283: * @param dir the directory. 284: * 285: * @return <code>false</code>. 286: */ 287: public boolean isDrive(File dir) 288: { 289: return false; 290: } 291: 292: /** 293: * Returns <code>true</code> if <code>f</code> is a file or directory, and 294: * <code>false</code> otherwise. 295: * 296: * @param f the file/directory. 297: * 298: * @return <code>true</code> if <code>f</code> is a file or directory, and 299: * <code>false</code> otherwise. 300: */ 301: public boolean isFileSystem(File f) 302: { 303: return (f.isFile() || f.isDirectory()); 304: } 305: 306: /** 307: * Returns <code>true</code> if the given directory is a file system root, 308: * and <code>false</code> otherwise. 309: * 310: * @param dir the directory. 311: * 312: * @return <code>true</code> if the given directory is a file system root, 313: * and <code>false</code> otherwise. 314: */ 315: public boolean isFileSystemRoot(File dir) 316: { 317: File[] roots = File.listRoots(); 318: if (roots == null || dir == null) 319: return false; 320: String filename = dir.getAbsolutePath(); 321: for (int i = 0; i < roots.length; i++) 322: if (roots[i].getAbsolutePath().equals(filename)) 323: return true; 324: return false; 325: } 326: 327: /** 328: * Returns <code>true</code> if the given directory represents a floppy 329: * drive, and <code>false</code> otherwise. This default implementation 330: * always returns <code>false</code>. 331: * 332: * @param dir the directory. 333: * 334: * @return <code>false</code>. 335: */ 336: public boolean isFloppyDrive(File dir) 337: { 338: return false; 339: } 340: 341: /** 342: * Returns <code>true</code> if the given file is hidden, and 343: * <code>false</code> otherwise. 344: * 345: * @param f the file. 346: * 347: * @return <code>true</code> if the given file is hidden, and 348: * <code>false</code> otherwise. 349: */ 350: public boolean isHiddenFile(File f) 351: { 352: return f.isHidden(); 353: } 354: 355: /** 356: * Returns <code>true</code> if <code>folder</code> is the parent of 357: * <code>file</code>, and <code>false</code> otherwise. 358: * 359: * @param folder the folder (<code>null</code> not permitted). 360: * @param file the file (<code>null</code> not permitted). 361: * 362: * @return <code>true</code> if <code>folder</code> is the parent of 363: * <code>file</code>, and <code>false</code> otherwise. 364: */ 365: public boolean isParent(File folder, File file) 366: { 367: File parent = file.getParentFile(); 368: if (parent == null) 369: return false; 370: return folder.equals(parent); 371: } 372: 373: /** 374: * DOCUMENT ME! 375: * 376: * @param f DOCUMENT ME! 377: * 378: * @return DOCUMENT ME! 379: */ 380: public boolean isRoot(File f) 381: { 382: // These are not file system roots. 383: return false; 384: } 385: 386: /** 387: * Returns <code>true</code> if the file is traversable, and 388: * <code>false</code> otherwise. Here, all directories are considered 389: * traversable, and files are considered non-traversable. 390: * 391: * @param f the file or directory (<code>null</code> not permitted). 392: * 393: * @return <code>true</code> if the file is traversable, and 394: * <code>false</code> otherwise. 395: */ 396: public Boolean isTraversable(File f) 397: { 398: // Tested. A directory where the user has no permission to rwx is still 399: // traversable. (No files are listed when you traverse the directory) 400: // My best guess is that as long as it's a directory, the file is 401: // traversable. 402: return Boolean.valueOf(f.isDirectory()); 403: } 404: }
GNU Classpath (0.20) |