PageRenderTime 45ms CodeModel.GetById 30ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/io/FavoritesVFS.java

#
Java | 212 lines | 136 code | 25 blank | 51 comment | 10 complexity | a5f72c538fc0454ef405fe0b13985db6 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, 2003 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.*;
 28import org.gjt.sp.jedit.msg.DynamicMenuChanged;
 29import org.gjt.sp.jedit.*;
 30//}}}
 31
 32/**
 33 * A VFS used for remembering frequently-visited directories. Listing it
 34 * returns the favorites list. The deletePath of each entry is the
 35 * directory prefixed with "favorites:" so that right-clicking on a
 36 * favorite and clicking 'delete' in the browser just deletes the
 37 * favorite, and not the directory itself.
 38 * @author Slava Pestov
 39 * @version $Id: FavoritesVFS.java 4841 2003-07-28 21:08:04Z spestov $
 40 */
 41public class FavoritesVFS extends VFS
 42{
 43	public static final String PROTOCOL = "favorites";
 44
 45	//{{{ FavoritesVFS constructor
 46	public FavoritesVFS()
 47	{
 48		super("favorites",DELETE_CAP | LOW_LATENCY_CAP,
 49			new String[] { EA_TYPE });
 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		return getFavorites();
 69	} //}}}
 70
 71	//{{{ _getDirectoryEntry() method
 72	public DirectoryEntry _getDirectoryEntry(Object session, String path,
 73		Component comp)
 74	{
 75		// does it matter that this doesn't set the type correctly?
 76		return new FavoritesEntry(path,VFS.DirectoryEntry.DIRECTORY);
 77	} //}}}
 78
 79	//{{{ _delete() method
 80	public boolean _delete(Object session, String path, Component comp)
 81	{
 82		synchronized(lock)
 83		{
 84			path = path.substring(PROTOCOL.length() + 1);
 85
 86			Iterator iter = favorites.iterator();
 87			while(iter.hasNext())
 88			{
 89				if(((FavoritesEntry)iter.next()).path.equals(path))
 90				{
 91					iter.remove();
 92					VFSManager.sendVFSUpdate(this,PROTOCOL
 93						+ ":",false);
 94					EditBus.send(new DynamicMenuChanged(
 95						"favorites"));
 96					return true;
 97				}
 98			}
 99		}
100
101		return false;
102	} //}}}
103
104	//{{{ loadFavorites() method
105	public static void loadFavorites()
106	{
107		favorites = new LinkedList();
108
109		synchronized(lock)
110		{
111			String favorite;
112			int i = 0;
113			while((favorite = jEdit.getProperty("vfs.favorite." + i)) != null)
114			{
115				favorites.add(new FavoritesEntry(favorite,
116					jEdit.getIntegerProperty("vfs.favorite."
117					+ i + ".type",
118					VFS.DirectoryEntry.DIRECTORY)));
119				i++;
120			}
121		}
122	} //}}}
123
124	//{{{ addToFavorites() method
125	public static void addToFavorites(String path, int type)
126	{
127		synchronized(lock)
128		{
129			if(favorites == null)
130				loadFavorites();
131
132			Iterator iter = favorites.iterator();
133			while(iter.hasNext())
134			{
135				if(((FavoritesEntry)iter.next()).path.equals(path))
136					return;
137			}
138
139			favorites.add(new FavoritesEntry(path,type));
140
141			VFSManager.sendVFSUpdate(instance,PROTOCOL + ":",false);
142			EditBus.send(new DynamicMenuChanged("favorites"));
143		}
144	} //}}}
145
146	//{{{ saveFavorites() method
147	public static void saveFavorites()
148	{
149		synchronized(lock)
150		{
151			if(favorites == null)
152				return;
153
154			int i = 0;
155			Iterator iter = favorites.iterator();
156			while(iter.hasNext())
157			{
158				FavoritesEntry e = ((FavoritesEntry)
159					iter.next());
160				jEdit.setProperty("vfs.favorite." + i,
161					e.path);
162				jEdit.setIntegerProperty("vfs.favorite." + i
163					+ ".type",e.type);
164
165				i++;
166			}
167			jEdit.unsetProperty("vfs.favorite." + favorites.size());
168			jEdit.unsetProperty("vfs.favorite." + favorites.size()
169				+ ".type");
170		}
171	} //}}}
172
173	//{{{ getFavorites() method
174	public static VFS.DirectoryEntry[] getFavorites()
175	{
176		synchronized(lock)
177		{
178			if(favorites == null)
179				loadFavorites();
180
181			return (VFS.DirectoryEntry[])favorites.toArray(
182				new VFS.DirectoryEntry[favorites.size()]);
183		}
184	} //}}}
185
186	//{{{ Private members
187	private static FavoritesVFS instance;
188	private static Object lock = new Object();
189	private static List favorites;
190	//}}}
191
192	//{{{ FavoritesEntry class
193	static class FavoritesEntry extends VFS.DirectoryEntry
194	{
195		FavoritesEntry(String path, int type)
196		{
197			super(path,path,PROTOCOL + ":" + path,type,0,false);
198		}
199
200		public String getExtendedAttribute(String name)
201		{
202			if(name.equals(EA_TYPE))
203				return super.getExtendedAttribute(name);
204			else
205			{
206				// don't want it to show "0 bytes" for size,
207				// etc.
208				return null;
209			}
210		}
211	} //}}}
212}