PageRenderTime 41ms CodeModel.GetById 28ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/io/FavoritesVFS.java

#
Java | 172 lines | 101 code | 22 blank | 49 comment | 12 complexity | 7e5ddf15f04aa5647b366fe35a52f4da MD5 | raw file
  1/*
  2 * FavoritesVFS.java - Stores frequently-visited directory locations
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.io;
 24
 25//{{{ Imports
 26import java.awt.Component;
 27import java.util.ArrayList;
 28import org.gjt.sp.jedit.jEdit;
 29//}}}
 30
 31/**
 32 * A VFS used for remembering frequently-visited directories. Listing it
 33 * returns the favorites list. The deletePath of each entry is the
 34 * directory prefixed with "favorites:" so that right-clicking on a
 35 * favorite and clicking 'delete' in the browser just deletes the
 36 * favorite, and not the directory itself.
 37 * @author Slava Pestov
 38 * @version $Id: FavoritesVFS.java 4202 2002-05-28 03:01:22Z spestov $
 39 */
 40public class FavoritesVFS extends VFS
 41{
 42	public static final String PROTOCOL = "favorites";
 43
 44	//{{{ FavoritesVFS constructor
 45	public FavoritesVFS()
 46	{
 47		// BROWSE_CAP not set because we don't want the VFS browser
 48		// to create an item for this VFS in its 'Plugins' menu
 49		super("favorites",DELETE_CAP | LOW_LATENCY_CAP);
 50
 51		/* addToFavorites(), which is a static method
 52		 * (for convinience) needs an instance of the
 53		 * VFS to pass to VFSManager.sendVFSUpdate(),
 54		 * hence this hack. */
 55		instance = this;
 56	} //}}}
 57
 58	//{{{ getParentOfPath() method
 59	public String getParentOfPath(String path)
 60	{
 61		return PROTOCOL + ":";
 62	} //}}}
 63
 64	//{{{ _listDirectory() method
 65	public VFS.DirectoryEntry[] _listDirectory(Object session, String url,
 66		Component comp)
 67	{
 68		synchronized(lock)
 69		{
 70			if(favorites == null)
 71				loadFavorites();
 72
 73			VFS.DirectoryEntry[] retVal = new VFS.DirectoryEntry[favorites.size()];
 74			for(int i = 0; i < retVal.length; i++)
 75			{
 76				String favorite = (String)favorites.get(i);
 77				retVal[i] = _getDirectoryEntry(session,favorite,comp);
 78			}
 79			return retVal;
 80		}
 81	} //}}}
 82
 83	//{{{ _getDirectoryEntry() method
 84	public DirectoryEntry _getDirectoryEntry(Object session, String path,
 85		Component comp)
 86	{
 87		return new VFS.DirectoryEntry(path,path,"favorites:" + path,
 88					VFS.DirectoryEntry.DIRECTORY,
 89					0L,false);
 90	} //}}}
 91
 92	//{{{ _delete() method
 93	public boolean _delete(Object session, String path, Component comp)
 94	{
 95		synchronized(lock)
 96		{
 97			path = path.substring(PROTOCOL.length() + 1);
 98			favorites.remove(path);
 99
100			VFSManager.sendVFSUpdate(this,PROTOCOL + ":",false);
101		}
102
103		return true;
104	} //}}}
105
106	//{{{ loadFavorites() method
107	public static void loadFavorites()
108	{
109		favorites = new ArrayList();
110
111		synchronized(lock)
112		{
113			String favorite;
114			int i = 0;
115			while((favorite = jEdit.getProperty("vfs.favorite." + i)) != null)
116			{
117				favorites.add(favorite);
118				i++;
119			}
120		}
121	} //}}}
122
123	//{{{ addToFavorites() method
124	public static void addToFavorites(String path)
125	{
126		synchronized(lock)
127		{
128			if(favorites == null)
129				loadFavorites();
130
131			if(!favorites.contains(path))
132				favorites.add(path);
133
134			VFSManager.sendVFSUpdate(instance,PROTOCOL + ":",false);
135		}
136	} //}}}
137
138	//{{{ saveFavorites() method
139	public static void saveFavorites()
140	{
141		synchronized(lock)
142		{
143			if(favorites == null)
144				return;
145
146			for(int i = 0; i < favorites.size(); i++)
147			{
148				jEdit.setProperty("vfs.favorite." + i,
149					(String)favorites.get(i));
150			}
151			jEdit.unsetProperty("vfs.favorite." + favorites.size());
152		}
153	} //}}}
154
155	//{{{ getFavorites() method
156	public static Object[] getFavorites()
157	{
158		synchronized(lock)
159		{
160			if(favorites == null)
161				loadFavorites();
162
163			return favorites.toArray();
164		}
165	} //}}}
166
167	//{{{ Private members
168	private static FavoritesVFS instance;
169	private static Object lock = new Object();
170	private static ArrayList favorites;
171	//}}}
172}