001: /*
002: * FavoritesVFS.java - Stores frequently-visited directory locations
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000, 2004 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.io;
024:
025: //{{{ Imports
026: import java.awt.Component;
027: import java.util.*;
028: import org.gjt.sp.jedit.msg.DynamicMenuChanged;
029: import org.gjt.sp.jedit.*;
030:
031: //}}}
032:
033: /**
034: * A VFS used for remembering frequently-visited directories. Listing it
035: * returns the favorites list. The deletePath of each entry is the
036: * directory prefixed with "favorites:" so that right-clicking on a
037: * favorite and clicking 'delete' in the browser just deletes the
038: * favorite, and not the directory itself.
039: * @author Slava Pestov
040: * @version $Id: FavoritesVFS.java 5179 2005-02-05 20:34:41Z spestov $
041: */
042: public class FavoritesVFS extends VFS {
043: public static final String PROTOCOL = "favorites";
044:
045: //{{{ FavoritesVFS constructor
046: public FavoritesVFS() {
047: super ("favorites", DELETE_CAP | LOW_LATENCY_CAP,
048: new String[] { EA_TYPE });
049:
050: /* addToFavorites(), which is a static method
051: * (for convinience) needs an instance of the
052: * VFS to pass to VFSManager.sendVFSUpdate(),
053: * hence this hack. */
054: instance = this ;
055: } //}}}
056:
057: //{{{ getParentOfPath() method
058: public String getParentOfPath(String path) {
059: return PROTOCOL + ":";
060: } //}}}
061:
062: //{{{ _listFiles() method
063: public VFSFile[] _listFiles(Object session, String url,
064: Component comp) {
065: return getFavorites();
066: } //}}}
067:
068: //{{{ _getFile() method
069: public VFSFile _getFile(Object session, String path, Component comp) {
070: // does it matter that this doesn't set the type correctly?
071: return new Favorite(path, VFSFile.DIRECTORY);
072: } //}}}
073:
074: //{{{ _delete() method
075: public boolean _delete(Object session, String path, Component comp) {
076: synchronized (lock) {
077: path = path.substring(PROTOCOL.length() + 1);
078:
079: Iterator iter = favorites.iterator();
080: while (iter.hasNext()) {
081: if (((Favorite) iter.next()).getPath().equals(path)) {
082: iter.remove();
083: VFSManager.sendVFSUpdate(this , PROTOCOL + ":",
084: false);
085: EditBus.send(new DynamicMenuChanged("favorites"));
086: return true;
087: }
088: }
089: }
090:
091: return false;
092: } //}}}
093:
094: //{{{ loadFavorites() method
095: public static void loadFavorites() {
096: synchronized (lock) {
097: favorites = new LinkedList();
098:
099: String favorite;
100: int i = 0;
101: while ((favorite = jEdit.getProperty("vfs.favorite." + i)) != null) {
102: favorites.add(new Favorite(favorite, jEdit
103: .getIntegerProperty("vfs.favorite." + i
104: + ".type", VFSFile.DIRECTORY)));
105: i++;
106: }
107: }
108: } //}}}
109:
110: //{{{ addToFavorites() method
111: public static void addToFavorites(String path, int type) {
112: synchronized (lock) {
113: if (favorites == null)
114: loadFavorites();
115:
116: Iterator iter = favorites.iterator();
117: while (iter.hasNext()) {
118: if (((Favorite) iter.next()).getPath().equals(path))
119: return;
120: }
121:
122: favorites.add(new Favorite(path, type));
123:
124: VFSManager.sendVFSUpdate(instance, PROTOCOL + ":", false);
125: EditBus.send(new DynamicMenuChanged("favorites"));
126: }
127: } //}}}
128:
129: //{{{ saveFavorites() method
130: public static void saveFavorites() {
131: synchronized (lock) {
132: if (favorites == null)
133: return;
134:
135: int i = 0;
136: Iterator iter = favorites.iterator();
137: while (iter.hasNext()) {
138: Favorite e = ((Favorite) iter.next());
139: jEdit.setProperty("vfs.favorite." + i, e.getPath());
140: jEdit.setIntegerProperty("vfs.favorite." + i + ".type",
141: e.getType());
142:
143: i++;
144: }
145: jEdit.unsetProperty("vfs.favorite." + favorites.size());
146: jEdit.unsetProperty("vfs.favorite." + favorites.size()
147: + ".type");
148: }
149: } //}}}
150:
151: //{{{ getFavorites() method
152: public static VFSFile[] getFavorites() {
153: synchronized (lock) {
154: if (favorites == null)
155: loadFavorites();
156:
157: return (VFSFile[]) favorites.toArray(new VFSFile[favorites
158: .size()]);
159: }
160: } //}}}
161:
162: //{{{ Private members
163: private static FavoritesVFS instance;
164: private static Object lock = new Object();
165: private static List favorites;
166:
167: //}}}
168:
169: //{{{ Favorite class
170: static class Favorite extends VFSFile {
171: Favorite(String path, int type) {
172: super (path, path, PROTOCOL + ":" + path, type, 0, false);
173: }
174:
175: public String getExtendedAttribute(String name) {
176: if (name.equals(EA_TYPE))
177: return super .getExtendedAttribute(name);
178: else {
179: // don't want it to show "0 bytes" for size,
180: // etc.
181: return null;
182: }
183: }
184: } //}}}
185: }
|