PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/io/FavoritesVFS.java

#
Java | 172 lines | 101 code | 22 blank | 49 comment | 12 complexity | 7e5ddf15f04aa5647b366fe35a52f4da 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 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.ArrayList;
  26. import org.gjt.sp.jedit.jEdit;
  27. //}}}
  28. /**
  29. * A VFS used for remembering frequently-visited directories. Listing it
  30. * returns the favorites list. The deletePath of each entry is the
  31. * directory prefixed with "favorites:" so that right-clicking on a
  32. * favorite and clicking 'delete' in the browser just deletes the
  33. * favorite, and not the directory itself.
  34. * @author Slava Pestov
  35. * @version $Id: FavoritesVFS.java 4202 2002-05-28 03:01:22Z spestov $
  36. */
  37. public class FavoritesVFS extends VFS
  38. {
  39. public static final String PROTOCOL = "favorites";
  40. //{{{ FavoritesVFS constructor
  41. public FavoritesVFS()
  42. {
  43. // BROWSE_CAP not set because we don't want the VFS browser
  44. // to create an item for this VFS in its 'Plugins' menu
  45. super("favorites",DELETE_CAP | LOW_LATENCY_CAP);
  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. synchronized(lock)
  62. {
  63. if(favorites == null)
  64. loadFavorites();
  65. VFS.DirectoryEntry[] retVal = new VFS.DirectoryEntry[favorites.size()];
  66. for(int i = 0; i < retVal.length; i++)
  67. {
  68. String favorite = (String)favorites.get(i);
  69. retVal[i] = _getDirectoryEntry(session,favorite,comp);
  70. }
  71. return retVal;
  72. }
  73. } //}}}
  74. //{{{ _getDirectoryEntry() method
  75. public DirectoryEntry _getDirectoryEntry(Object session, String path,
  76. Component comp)
  77. {
  78. return new VFS.DirectoryEntry(path,path,"favorites:" + path,
  79. VFS.DirectoryEntry.DIRECTORY,
  80. 0L,false);
  81. } //}}}
  82. //{{{ _delete() method
  83. public boolean _delete(Object session, String path, Component comp)
  84. {
  85. synchronized(lock)
  86. {
  87. path = path.substring(PROTOCOL.length() + 1);
  88. favorites.remove(path);
  89. VFSManager.sendVFSUpdate(this,PROTOCOL + ":",false);
  90. }
  91. return true;
  92. } //}}}
  93. //{{{ loadFavorites() method
  94. public static void loadFavorites()
  95. {
  96. favorites = new ArrayList();
  97. synchronized(lock)
  98. {
  99. String favorite;
  100. int i = 0;
  101. while((favorite = jEdit.getProperty("vfs.favorite." + i)) != null)
  102. {
  103. favorites.add(favorite);
  104. i++;
  105. }
  106. }
  107. } //}}}
  108. //{{{ addToFavorites() method
  109. public static void addToFavorites(String path)
  110. {
  111. synchronized(lock)
  112. {
  113. if(favorites == null)
  114. loadFavorites();
  115. if(!favorites.contains(path))
  116. favorites.add(path);
  117. VFSManager.sendVFSUpdate(instance,PROTOCOL + ":",false);
  118. }
  119. } //}}}
  120. //{{{ saveFavorites() method
  121. public static void saveFavorites()
  122. {
  123. synchronized(lock)
  124. {
  125. if(favorites == null)
  126. return;
  127. for(int i = 0; i < favorites.size(); i++)
  128. {
  129. jEdit.setProperty("vfs.favorite." + i,
  130. (String)favorites.get(i));
  131. }
  132. jEdit.unsetProperty("vfs.favorite." + favorites.size());
  133. }
  134. } //}}}
  135. //{{{ getFavorites() method
  136. public static Object[] getFavorites()
  137. {
  138. synchronized(lock)
  139. {
  140. if(favorites == null)
  141. loadFavorites();
  142. return favorites.toArray();
  143. }
  144. } //}}}
  145. //{{{ Private members
  146. private static FavoritesVFS instance;
  147. private static Object lock = new Object();
  148. private static ArrayList favorites;
  149. //}}}
  150. }