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