KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > io > FavoritesVFS


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, 2004 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
23 package org.gjt.sp.jedit.io;
24
25 //{{{ Imports
26
import java.awt.Component JavaDoc;
27 import java.util.*;
28 import org.gjt.sp.jedit.msg.DynamicMenuChanged;
29 import 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 5179 2005-02-05 20:34:41Z spestov $
40  */

41 public class FavoritesVFS extends VFS
42 {
43     public static final String JavaDoc PROTOCOL = "favorites";
44
45     //{{{ FavoritesVFS constructor
46
public FavoritesVFS()
47     {
48         super("favorites",DELETE_CAP | LOW_LATENCY_CAP,
49             new String JavaDoc[] { 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 JavaDoc getParentOfPath(String JavaDoc path)
60     {
61         return PROTOCOL + ":";
62     } //}}}
63

64     //{{{ _listFiles() method
65
public VFSFile[] _listFiles(Object JavaDoc session, String JavaDoc url,
66         Component JavaDoc comp)
67     {
68         return getFavorites();
69     } //}}}
70

71     //{{{ _getFile() method
72
public VFSFile _getFile(Object JavaDoc session, String JavaDoc path,
73         Component JavaDoc comp)
74     {
75         // does it matter that this doesn't set the type correctly?
76
return new Favorite(path,VFSFile.DIRECTORY);
77     } //}}}
78

79     //{{{ _delete() method
80
public boolean _delete(Object JavaDoc session, String JavaDoc path, Component JavaDoc 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(((Favorite)iter.next()).getPath()
90                     .equals(path))
91                 {
92                     iter.remove();
93                     VFSManager.sendVFSUpdate(this,PROTOCOL
94                         + ":",false);
95                     EditBus.send(new DynamicMenuChanged(
96                         "favorites"));
97                     return true;
98                 }
99             }
100         }
101
102         return false;
103     } //}}}
104

105     //{{{ loadFavorites() method
106
public static void loadFavorites()
107     {
108         synchronized(lock)
109         {
110             favorites = new LinkedList();
111
112             String JavaDoc favorite;
113             int i = 0;
114             while((favorite = jEdit.getProperty("vfs.favorite." + i)) != null)
115             {
116                 favorites.add(new Favorite(favorite,
117                     jEdit.getIntegerProperty("vfs.favorite."
118                     + i + ".type",
119                     VFSFile.DIRECTORY)));
120                 i++;
121             }
122         }
123     } //}}}
124

125     //{{{ addToFavorites() method
126
public static void addToFavorites(String JavaDoc path, int type)
127     {
128         synchronized(lock)
129         {
130             if(favorites == null)
131                 loadFavorites();
132
133             Iterator iter = favorites.iterator();
134             while(iter.hasNext())
135             {
136                 if(((Favorite)iter.next()).getPath().equals(path))
137                     return;
138             }
139
140             favorites.add(new Favorite(path,type));
141
142             VFSManager.sendVFSUpdate(instance,PROTOCOL + ":",false);
143             EditBus.send(new DynamicMenuChanged("favorites"));
144         }
145     } //}}}
146

147     //{{{ saveFavorites() method
148
public static void saveFavorites()
149     {
150         synchronized(lock)
151         {
152             if(favorites == null)
153                 return;
154
155             int i = 0;
156             Iterator iter = favorites.iterator();
157             while(iter.hasNext())
158             {
159                 Favorite e = ((Favorite)iter.next());
160                 jEdit.setProperty("vfs.favorite." + i,
161                     e.getPath());
162                 jEdit.setIntegerProperty("vfs.favorite." + i
163                     + ".type",e.getType());
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 VFSFile[] getFavorites()
175     {
176         synchronized(lock)
177         {
178             if(favorites == null)
179                 loadFavorites();
180
181             return (VFSFile[])favorites.toArray(
182                 new VFSFile[favorites.size()]);
183         }
184     } //}}}
185

186     //{{{ Private members
187
private static FavoritesVFS instance;
188     private static Object JavaDoc lock = new Object JavaDoc();
189     private static List favorites;
190     //}}}
191

192     //{{{ Favorite class
193
static class Favorite extends VFSFile
194     {
195         Favorite(String JavaDoc path, int type)
196         {
197             super(path,path,PROTOCOL + ":" + path,type,0,false);
198         }
199
200         public String JavaDoc getExtendedAttribute(String JavaDoc 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
}
213
Popular Tags