PageRenderTime 233ms CodeModel.GetById 226ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/io/FileRootsVFS.java

#
Java | 174 lines | 107 code | 23 blank | 44 comment | 16 complexity | ef63e0ce143f8f91befe22478b7eb6c4 MD5 | raw file
  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
 25package org.gjt.sp.jedit.io;
 26
 27//{{{ Imports
 28import javax.swing.filechooser.FileSystemView;
 29import java.awt.Component;
 30import java.io.File;
 31import java.util.LinkedList;
 32import org.gjt.sp.jedit.MiscUtilities;
 33import org.gjt.sp.jedit.OperatingSystem;
 34import org.gjt.sp.util.Log;
 35//}}}
 36
 37/**
 38 * A VFS that lists local root filesystems.
 39 * @author Slava Pestov
 40 * @version $Id: FileRootsVFS.java 5181 2005-02-10 22:35:18Z spestov $
 41 */
 42public class FileRootsVFS extends VFS
 43{
 44	public static final String PROTOCOL = "roots";
 45
 46	//{{{ FileRootsVFS constructor
 47	public FileRootsVFS()
 48	{
 49		super("roots",LOW_LATENCY_CAP,new String[] {
 50			EA_TYPE });
 51	} //}}}
 52
 53	//{{{ getParentOfPath() method
 54	public String getParentOfPath(String path)
 55	{
 56		return PROTOCOL + ":";
 57	} //}}}
 58
 59	//{{{ _listFiles() method
 60	public VFSFile[] _listFiles(Object session, String url,
 61		Component comp)
 62	{
 63		File[] roots = listRoots();
 64
 65		if(roots == null)
 66			return null;
 67
 68		VFSFile[] rootDE = new VFSFile[roots.length];
 69		for(int i = 0; i < roots.length; i++)
 70			rootDE[i] = new Root(roots[i]);
 71
 72		return rootDE;
 73	} //}}}
 74
 75	//{{{ _getFile() method
 76	public VFSFile _getFile(Object session, String path,
 77		Component comp)
 78	{
 79		return new Root(new File(path));
 80	} //}}}
 81
 82	//{{{ Private members
 83	private static FileSystemView fsView = FileSystemView.getFileSystemView();
 84
 85	//{{{ listRoots() method
 86	private static File[] listRoots()
 87	{
 88		if (OperatingSystem.isMacOS())
 89		{
 90			// Nasty hardcoded values
 91			File[] volumes = new File("/Volumes").listFiles();
 92			LinkedList roots = new LinkedList();
 93
 94			roots.add(new File("/"));
 95
 96			for (int i=0; i<volumes.length; i++)
 97			{
 98				// Make sure people don't do stupid things like putting files in /Volumes
 99				if (volumes[i].isDirectory())
100					roots.add(volumes[i]);
101			}
102
103			return (File[])roots.toArray(new File[0]);
104		}
105		else
106		{
107			File[] roots = File.listRoots();
108			File[] desktop = fsView.getRoots();
109
110			if(desktop == null)
111				return roots;
112
113			File[] rootsPlus = new File[roots.length + desktop.length];
114			System.arraycopy(desktop, 0, rootsPlus, 0, desktop.length);
115			System.arraycopy(roots, 0, rootsPlus, 1, roots.length);
116			return rootsPlus;
117		}
118	} //}}}
119
120	//}}}
121
122	//{{{ Root class
123	static class Root extends VFSFile
124	{
125		Root(File file)
126		{
127			// REMIND: calling isDirectory() on a floppy drive
128			// displays stupid I/O error dialog box on Windows
129
130			String path = file.getPath();
131			setPath(path);
132			setDeletePath(path);
133			setSymlinkPath(path);
134
135			if(fsView.isFloppyDrive(file))
136			{
137				setType(VFSFile.FILESYSTEM);
138				setName(path);
139			}
140			else if(fsView.isDrive(file))
141			{
142				setType(VFSFile.FILESYSTEM);
143				setName(path + " "
144					+ fsView.getSystemDisplayName(file));
145			}
146			else if(file.isDirectory())
147			{
148				if(fsView.isFileSystemRoot(file))
149					setType(VFSFile.DIRECTORY);
150				else
151					setType(VFSFile.FILESYSTEM);
152
153				if(OperatingSystem.isMacOS())
154					setName(MiscUtilities.getFileName(path));
155				else
156					setName(path);
157			}
158			else
159				setType(VFSFile.FILE);
160		}
161
162		public String getExtendedAttribute(String name)
163		{
164			if(name.equals(EA_TYPE))
165				return super.getExtendedAttribute(name);
166			else
167			{
168				// don't want it to show "0 bytes" for size,
169				// etc.
170				return null;
171			}
172		}
173	} //}}}
174}