PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 178 lines | 105 code | 23 blank | 50 comment | 12 complexity | 1afe5b8f54db0e7b86478fc00653366d 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 3970 2002-01-16 09:21:51Z spestov $
  36. */
  37. public class FavoritesVFS extends VFS
  38. {
  39. public static final String PROTOCOL = "favorites";
  40. //{{{ FavoritesVFS constructor
  41. public FavoritesVFS()
  42. {
  43. super("favorites");
  44. /* addToFavorites(), which is a static method
  45. * (for convinience) needs an instance of the
  46. * VFS to pass to VFSManager.sendVFSUpdate(),
  47. * hence this hack. */
  48. instance = this;
  49. } //}}}
  50. //{{{ getCapabilities() method
  51. public int getCapabilities()
  52. {
  53. // BROWSE_CAP not set because we don't want the VFS browser
  54. // to create the default 'favorites' button on the tool bar
  55. return /* BROWSE_CAP | */ DELETE_CAP;
  56. } //}}}
  57. //{{{ getParentOfPath() method
  58. public String getParentOfPath(String path)
  59. {
  60. return PROTOCOL + ":";
  61. } //}}}
  62. //{{{ _listDirectory() method
  63. public VFS.DirectoryEntry[] _listDirectory(Object session, String url,
  64. Component comp)
  65. {
  66. synchronized(lock)
  67. {
  68. if(favorites == null)
  69. loadFavorites();
  70. VFS.DirectoryEntry[] retVal = new VFS.DirectoryEntry[favorites.size()];
  71. for(int i = 0; i < retVal.length; i++)
  72. {
  73. String favorite = (String)favorites.get(i);
  74. retVal[i] = _getDirectoryEntry(session,favorite,comp);
  75. }
  76. return retVal;
  77. }
  78. } //}}}
  79. //{{{ _getDirectoryEntry() method
  80. public DirectoryEntry _getDirectoryEntry(Object session, String path,
  81. Component comp)
  82. {
  83. return new VFS.DirectoryEntry(path,path,"favorites:" + path,
  84. VFS.DirectoryEntry.DIRECTORY,
  85. 0L,false);
  86. } //}}}
  87. //{{{ _delete() method
  88. public boolean _delete(Object session, String path, Component comp)
  89. {
  90. synchronized(lock)
  91. {
  92. path = path.substring(PROTOCOL.length() + 1);
  93. favorites.remove(path);
  94. VFSManager.sendVFSUpdate(this,PROTOCOL + ":",false);
  95. }
  96. return true;
  97. } //}}}
  98. //{{{ loadFavorites() method
  99. public static void loadFavorites()
  100. {
  101. favorites = new ArrayList();
  102. synchronized(lock)
  103. {
  104. String favorite;
  105. int i = 0;
  106. while((favorite = jEdit.getProperty("vfs.favorite." + i)) != null)
  107. {
  108. favorites.add(favorite);
  109. i++;
  110. }
  111. }
  112. } //}}}
  113. //{{{ addToFavorites() method
  114. public static void addToFavorites(String path)
  115. {
  116. synchronized(lock)
  117. {
  118. if(favorites == null)
  119. loadFavorites();
  120. if(!favorites.contains(path))
  121. favorites.add(path);
  122. VFSManager.sendVFSUpdate(instance,PROTOCOL + ":",false);
  123. }
  124. } //}}}
  125. //{{{ saveFavorites() method
  126. public static void saveFavorites()
  127. {
  128. synchronized(lock)
  129. {
  130. if(favorites == null)
  131. return;
  132. for(int i = 0; i < favorites.size(); i++)
  133. {
  134. jEdit.setProperty("vfs.favorite." + i,
  135. (String)favorites.get(i));
  136. }
  137. jEdit.unsetProperty("vfs.favorite." + favorites.size());
  138. }
  139. } //}}}
  140. //{{{ getFavorites() method
  141. public static Object[] getFavorites()
  142. {
  143. synchronized(lock)
  144. {
  145. if(favorites == null)
  146. loadFavorites();
  147. return favorites.toArray();
  148. }
  149. } //}}}
  150. //{{{ Private members
  151. private static FavoritesVFS instance;
  152. private static Object lock = new Object();
  153. private static ArrayList favorites;
  154. //}}}
  155. }