001: /*
002: * FileRootsVFS.java - Local root filesystems VFS
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000, 2005 Slava Pestov
007: * Portions copyright (C) 2002 Kris Kopicki
008: * Portions copyright (C) 2002 Carmine Lucarelli
009: *
010: * This program is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU General Public License
012: * as published by the Free Software Foundation; either version 2
013: * of the License, or any later version.
014: *
015: * This program is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: * GNU General Public License for more details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with this program; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
023: */
024:
025: package org.gjt.sp.jedit.io;
026:
027: //{{{ Imports
028: import javax.swing.filechooser.FileSystemView;
029: import java.awt.Component;
030: import java.io.File;
031: import java.util.LinkedList;
032: import org.gjt.sp.jedit.MiscUtilities;
033: import org.gjt.sp.jedit.OperatingSystem;
034:
035: //}}}
036:
037: /**
038: * A VFS that lists local root filesystems.
039: * @author Slava Pestov
040: * @version $Id: FileRootsVFS.java 8288 2006-12-30 13:34:22Z kpouer $
041: */
042: public class FileRootsVFS extends VFS {
043: public static final String PROTOCOL = "roots";
044:
045: //{{{ FileRootsVFS constructor
046: public FileRootsVFS() {
047: super ("roots", LOW_LATENCY_CAP, new String[] { EA_TYPE });
048: } //}}}
049:
050: //{{{ getParentOfPath() method
051: public String getParentOfPath(String path) {
052: return PROTOCOL + ':';
053: } //}}}
054:
055: //{{{ _listFiles() method
056: public VFSFile[] _listFiles(Object session, String url,
057: Component comp) {
058: File[] roots = listRoots();
059:
060: if (roots == null)
061: return null;
062:
063: VFSFile[] rootDE = new VFSFile[roots.length];
064: for (int i = 0; i < roots.length; i++)
065: rootDE[i] = new Root(roots[i]);
066:
067: return rootDE;
068: } //}}}
069:
070: //{{{ _getFile() method
071: public VFSFile _getFile(Object session, String path, Component comp) {
072: return new Root(new File(path));
073: } //}}}
074:
075: //{{{ Private members
076: private static FileSystemView fsView = FileSystemView
077: .getFileSystemView();
078:
079: //{{{ listRoots() method
080: private static File[] listRoots() {
081: if (OperatingSystem.isMacOS()) {
082: // Nasty hardcoded values
083: File[] volumes = new File("/Volumes").listFiles();
084: LinkedList<File> roots = new LinkedList<File>();
085:
086: roots.add(new File("/"));
087:
088: for (int i = 0; i < volumes.length; i++) {
089: // Make sure people don't do stupid things like putting files in /Volumes
090: if (volumes[i].isDirectory())
091: roots.add(volumes[i]);
092: }
093:
094: return roots.toArray(new File[roots.size()]);
095: } else {
096: File[] roots = File.listRoots();
097: File[] desktop = fsView.getRoots();
098:
099: if (desktop == null)
100: return roots;
101:
102: File[] rootsPlus = new File[roots.length + desktop.length];
103: System.arraycopy(desktop, 0, rootsPlus, 0, desktop.length);
104: System.arraycopy(roots, 0, rootsPlus, 1, roots.length);
105: return rootsPlus;
106: }
107: } //}}}
108:
109: //}}}
110:
111: //{{{ Root class
112: static class Root extends VFSFile {
113: Root(File file) {
114: // REMIND: calling isDirectory() on a floppy drive
115: // displays stupid I/O error dialog box on Windows
116:
117: String path = file.getPath();
118: setPath(path);
119: setDeletePath(path);
120: setSymlinkPath(path);
121:
122: if (fsView.isFloppyDrive(file)) {
123: setType(VFSFile.FILESYSTEM);
124: setName(path);
125: } else if (fsView.isDrive(file)) {
126: setType(VFSFile.FILESYSTEM);
127: setName(path + ' ' + fsView.getSystemDisplayName(file));
128: } else if (file.isDirectory()) {
129: if (fsView.isFileSystemRoot(file))
130: setType(VFSFile.DIRECTORY);
131: else
132: setType(VFSFile.FILESYSTEM);
133:
134: if (OperatingSystem.isMacOS())
135: setName(MiscUtilities.getFileName(path));
136: else
137: setName(path);
138: } else
139: setType(VFSFile.FILE);
140: }
141:
142: public String getExtendedAttribute(String name) {
143: if (name.equals(EA_TYPE))
144: return super .getExtendedAttribute(name);
145: else {
146: // don't want it to show "0 bytes" for size,
147: // etc.
148: return null;
149: }
150: }
151: } //}}}
152: }
|