KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > io > FileRootsVFS


1 /*
2  * FileRootsVFS.java - Local root filesystems VFS
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2000, 2005 Slava Pestov
7  * Portions copyright (C) 2002 Kris Kopicki
8  * Portions copyright (C) 2002 Carmine Lucarelli
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  */

24
25 package org.gjt.sp.jedit.io;
26
27 //{{{ Imports
28
import javax.swing.filechooser.FileSystemView JavaDoc;
29 import java.awt.Component JavaDoc;
30 import java.io.File JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import org.gjt.sp.jedit.MiscUtilities;
33 import org.gjt.sp.jedit.OperatingSystem;
34 //}}}
35

36 /**
37  * A VFS that lists local root filesystems.
38  * @author Slava Pestov
39  * @version $Id: FileRootsVFS.java 8288 2006-12-30 13:34:22Z kpouer $
40  */

41 public class FileRootsVFS extends VFS
42 {
43     public static final String JavaDoc PROTOCOL = "roots";
44
45     //{{{ FileRootsVFS constructor
46
public FileRootsVFS()
47     {
48         super("roots",LOW_LATENCY_CAP,new String JavaDoc[] {
49             EA_TYPE });
50     } //}}}
51

52     //{{{ getParentOfPath() method
53
public String JavaDoc getParentOfPath(String JavaDoc path)
54     {
55         return PROTOCOL + ':';
56     } //}}}
57

58     //{{{ _listFiles() method
59
public VFSFile[] _listFiles(Object JavaDoc session, String JavaDoc url,
60         Component JavaDoc comp)
61     {
62         File JavaDoc[] roots = listRoots();
63
64         if(roots == null)
65             return null;
66
67         VFSFile[] rootDE = new VFSFile[roots.length];
68         for(int i = 0; i < roots.length; i++)
69             rootDE[i] = new Root(roots[i]);
70
71         return rootDE;
72     } //}}}
73

74     //{{{ _getFile() method
75
public VFSFile _getFile(Object JavaDoc session, String JavaDoc path,
76         Component JavaDoc comp)
77     {
78         return new Root(new File JavaDoc(path));
79     } //}}}
80

81     //{{{ Private members
82
private static FileSystemView JavaDoc fsView = FileSystemView.getFileSystemView();
83
84     //{{{ listRoots() method
85
private static File JavaDoc[] listRoots()
86     {
87         if (OperatingSystem.isMacOS())
88         {
89             // Nasty hardcoded values
90
File JavaDoc[] volumes = new File JavaDoc("/Volumes").listFiles();
91             LinkedList JavaDoc<File JavaDoc> roots = new LinkedList JavaDoc<File JavaDoc>();
92
93             roots.add(new File JavaDoc("/"));
94
95             for (int i=0; i<volumes.length; i++)
96             {
97                 // Make sure people don't do stupid things like putting files in /Volumes
98
if (volumes[i].isDirectory())
99                     roots.add(volumes[i]);
100             }
101
102             return roots.toArray(new File JavaDoc[roots.size()]);
103         }
104         else
105         {
106             File JavaDoc[] roots = File.listRoots();
107             File JavaDoc[] desktop = fsView.getRoots();
108
109             if(desktop == null)
110                 return roots;
111
112             File JavaDoc[] rootsPlus = new File JavaDoc[roots.length + desktop.length];
113             System.arraycopy(desktop, 0, rootsPlus, 0, desktop.length);
114             System.arraycopy(roots, 0, rootsPlus, 1, roots.length);
115             return rootsPlus;
116         }
117     } //}}}
118

119     //}}}
120

121     //{{{ Root class
122
static class Root extends VFSFile
123     {
124         Root(File JavaDoc file)
125         {
126             // REMIND: calling isDirectory() on a floppy drive
127
// displays stupid I/O error dialog box on Windows
128

129             String JavaDoc path = file.getPath();
130             setPath(path);
131             setDeletePath(path);
132             setSymlinkPath(path);
133
134             if(fsView.isFloppyDrive(file))
135             {
136                 setType(VFSFile.FILESYSTEM);
137                 setName(path);
138             }
139             else if(fsView.isDrive(file))
140             {
141                 setType(VFSFile.FILESYSTEM);
142                 setName(path + ' '
143                     + fsView.getSystemDisplayName(file));
144             }
145             else if(file.isDirectory())
146             {
147                 if(fsView.isFileSystemRoot(file))
148                     setType(VFSFile.DIRECTORY);
149                 else
150                     setType(VFSFile.FILESYSTEM);
151
152                 if(OperatingSystem.isMacOS())
153                     setName(MiscUtilities.getFileName(path));
154                 else
155                     setName(path);
156             }
157             else
158                 setType(VFSFile.FILE);
159         }
160
161         public String JavaDoc getExtendedAttribute(String JavaDoc name)
162         {
163             if(name.equals(EA_TYPE))
164                 return super.getExtendedAttribute(name);
165             else
166             {
167                 // don't want it to show "0 bytes" for size,
168
// etc.
169
return null;
170             }
171         }
172     } //}}}
173
}
174
Popular Tags