PageRenderTime 37ms CodeModel.GetById 24ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 166 lines | 98 code | 18 blank | 50 comment | 4 complexity | 815357d754ed6b889a13393780534142 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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.Vector;
 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 3905 2001-11-23 09:08:49Z 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			VFS.DirectoryEntry[] retVal = new VFS.DirectoryEntry[favorites.size()];
 77			for(int i = 0; i < retVal.length; i++)
 78			{
 79				String favorite = (String)favorites.elementAt(i);
 80				retVal[i] = _getDirectoryEntry(session,favorite,comp);
 81			}
 82			return retVal;
 83		}
 84	} //}}}
 85
 86	//{{{ _getDirectoryEntry() method
 87	public DirectoryEntry _getDirectoryEntry(Object session, String path,
 88		Component comp)
 89	{
 90		return new VFS.DirectoryEntry(path,path,"favorites:" + path,
 91					VFS.DirectoryEntry.DIRECTORY,
 92					0L,false);
 93	} //}}}
 94
 95	//{{{ _delete() method
 96	public boolean _delete(Object session, String path, Component comp)
 97	{
 98		synchronized(lock)
 99		{
100			path = path.substring(PROTOCOL.length() + 1);
101			favorites.removeElement(path);
102
103			VFSManager.sendVFSUpdate(this,PROTOCOL + ":",false);
104		}
105
106		return true;
107	} //}}}
108
109	//{{{ loadFavorites() method
110	public static void loadFavorites()
111	{
112		synchronized(lock)
113		{
114			String favorite;
115			int i = 0;
116			while((favorite = jEdit.getProperty("vfs.favorite." + i)) != null)
117			{
118				favorites.addElement(favorite);
119				i++;
120			}
121		}
122	} //}}}
123
124	//{{{ addToFavorites() method
125	public static void addToFavorites(String path)
126	{
127		synchronized(lock)
128		{
129			if(!favorites.contains(path))
130				favorites.addElement(path);
131
132			VFSManager.sendVFSUpdate(instance,PROTOCOL + ":",false);
133		}
134	} //}}}
135
136	//{{{ saveFavorites() method
137	public static void saveFavorites()
138	{
139		synchronized(lock)
140		{
141			for(int i = 0; i < favorites.size(); i++)
142			{
143				jEdit.setProperty("vfs.favorite." + i,
144					(String)favorites.elementAt(i));
145			}
146			jEdit.unsetProperty("vfs.favorite." + favorites.size());
147		}
148	} //}}}
149
150	//{{{ getFavorites() method
151	public static String[] getFavorites()
152	{
153		synchronized(lock)
154		{
155			String[] retVal = new String[favorites.size()];
156			favorites.copyInto(retVal);
157			return retVal;
158		}
159	} //}}}
160
161	//{{{ Private members
162	private static FavoritesVFS instance;
163	private static Object lock = new Object();
164	private static Vector favorites = new Vector();
165	//}}}
166}