1 22 23 package org.gjt.sp.jedit.io; 24 25 import java.awt.Component ; 27 import java.util.*; 28 import org.gjt.sp.jedit.msg.DynamicMenuChanged; 29 import org.gjt.sp.jedit.*; 30 32 41 public class FavoritesVFS extends VFS 42 { 43 public static final String PROTOCOL = "favorites"; 44 45 public FavoritesVFS() 47 { 48 super("favorites",DELETE_CAP | LOW_LATENCY_CAP, 49 new String [] { EA_TYPE }); 50 51 55 instance = this; 56 } 58 public String getParentOfPath(String path) 60 { 61 return PROTOCOL + ":"; 62 } 64 public VFSFile[] _listFiles(Object session, String url, 66 Component comp) 67 { 68 return getFavorites(); 69 } 71 public VFSFile _getFile(Object session, String path, 73 Component comp) 74 { 75 return new Favorite(path,VFSFile.DIRECTORY); 77 } 79 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(((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 } 105 public static void loadFavorites() 107 { 108 synchronized(lock) 109 { 110 favorites = new LinkedList(); 111 112 String 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 } 125 public static void addToFavorites(String 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 } 147 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 } 173 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 } 186 private static FavoritesVFS instance; 188 private static Object lock = new Object (); 189 private static List favorites; 190 192 static class Favorite extends VFSFile 194 { 195 Favorite(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 return null; 209 } 210 } 211 } } 213 | Popular Tags |