PageRenderTime 207ms CodeModel.GetById 197ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 178 lines | 105 code | 23 blank | 50 comment | 12 complexity | 1afe5b8f54db0e7b86478fc00653366d 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 3970 2002-01-16 09:21:51Z spestov $
 39 */
 40public class FavoritesVFS extends VFS
 41{
 42	public static final String PROTOCOL = "favorites";
 43
 44	//{{{ FavoritesVFS constructor
 45	public FavoritesVFS()
 46	{
 47		super("favorites");
 48
 49		/* addToFavorites(), which is a static method
 50		 * (for convinience) needs an instance of the
 51		 * VFS to pass to VFSManager.sendVFSUpdate(),
 52		 * hence this hack. */
 53		instance = this;
 54	} //}}}
 55
 56	//{{{ getCapabilities() method
 57	public int getCapabilities()
 58	{
 59		// BROWSE_CAP not set because we don't want the VFS browser
 60		// to create the default 'favorites' button on the tool bar
 61		return /* BROWSE_CAP | */ DELETE_CAP;
 62	} //}}}
 63
 64	//{{{ getParentOfPath() method
 65	public String getParentOfPath(String path)
 66	{
 67		return PROTOCOL + ":";
 68	} //}}}
 69
 70	//{{{ _listDirectory() method
 71	public VFS.DirectoryEntry[] _listDirectory(Object session, String url,
 72		Component comp)
 73	{
 74		synchronized(lock)
 75		{
 76			if(favorites == null)
 77				loadFavorites();
 78
 79			VFS.DirectoryEntry[] retVal = new VFS.DirectoryEntry[favorites.size()];
 80			for(int i = 0; i < retVal.length; i++)
 81			{
 82				String favorite = (String)favorites.get(i);
 83				retVal[i] = _getDirectoryEntry(session,favorite,comp);
 84			}
 85			return retVal;
 86		}
 87	} //}}}
 88
 89	//{{{ _getDirectoryEntry() method
 90	public DirectoryEntry _getDirectoryEntry(Object session, String path,
 91		Component comp)
 92	{
 93		return new VFS.DirectoryEntry(path,path,"favorites:" + path,
 94					VFS.DirectoryEntry.DIRECTORY,
 95					0L,false);
 96	} //}}}
 97
 98	//{{{ _delete() method
 99	public boolean _delete(Object session, String path, Component comp)
100	{
101		synchronized(lock)
102		{
103			path = path.substring(PROTOCOL.length() + 1);
104			favorites.remove(path);
105
106			VFSManager.sendVFSUpdate(this,PROTOCOL + ":",false);
107		}
108
109		return true;
110	} //}}}
111
112	//{{{ loadFavorites() method
113	public static void loadFavorites()
114	{
115		favorites = new ArrayList();
116
117		synchronized(lock)
118		{
119			String favorite;
120			int i = 0;
121			while((favorite = jEdit.getProperty("vfs.favorite." + i)) != null)
122			{
123				favorites.add(favorite);
124				i++;
125			}
126		}
127	} //}}}
128
129	//{{{ addToFavorites() method
130	public static void addToFavorites(String path)
131	{
132		synchronized(lock)
133		{
134			if(favorites == null)
135				loadFavorites();
136
137			if(!favorites.contains(path))
138				favorites.add(path);
139
140			VFSManager.sendVFSUpdate(instance,PROTOCOL + ":",false);
141		}
142	} //}}}
143
144	//{{{ saveFavorites() method
145	public static void saveFavorites()
146	{
147		synchronized(lock)
148		{
149			if(favorites == null)
150				return;
151
152			for(int i = 0; i < favorites.size(); i++)
153			{
154				jEdit.setProperty("vfs.favorite." + i,
155					(String)favorites.get(i));
156			}
157			jEdit.unsetProperty("vfs.favorite." + favorites.size());
158		}
159	} //}}}
160
161	//{{{ getFavorites() method
162	public static Object[] getFavorites()
163	{
164		synchronized(lock)
165		{
166			if(favorites == null)
167				loadFavorites();
168
169			return favorites.toArray();
170		}
171	} //}}}
172
173	//{{{ Private members
174	private static FavoritesVFS instance;
175	private static Object lock = new Object();
176	private static ArrayList favorites;
177	//}}}
178}