1 19 20 package org.netbeans.modules.editor.bookmarks; 21 22 import java.net.URI ; 23 import java.net.URISyntaxException ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.WeakHashMap ; 30 import javax.swing.text.Document ; 31 import org.netbeans.api.project.FileOwnerQuery; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.api.project.ProjectManager; 34 import org.netbeans.lib.editor.bookmarks.api.Bookmark; 35 import org.netbeans.lib.editor.bookmarks.api.BookmarkList; 36 import org.netbeans.modules.editor.NbEditorUtilities; 37 import org.netbeans.spi.project.AuxiliaryConfiguration; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileStateInvalidException; 40 import org.w3c.dom.Element ; 41 42 48 49 public class PersistentBookmarks { 50 51 private static final int[] EMPTY_INT_ARRAY = new int[0]; 52 53 private static final Map project2bookmarksMap = new WeakHashMap (); 54 55 static synchronized void loadBookmarks(NbBookmarkManager manager) { 56 Document doc = manager.getDocument(); 57 FileObject fo = NbEditorUtilities.getFileObject(doc); 58 if (fo != null) { 59 FileBookmarksMap map = findOwningBookmarksMap(fo); 61 if (map != null) { 62 URL url; 63 try { 64 url = fo.getURL(); 65 } catch (FileStateInvalidException e) { 66 url = null; 68 } 69 70 if (url != null) { 71 FileBookmarks fileBookmarks = map.get(url); 72 if (fileBookmarks != null) { 73 int bookmarkCount = fileBookmarks.getBookmarkCount(); 74 for (int i = 0; i < bookmarkCount; i++) { 75 int lineIndex = fileBookmarks.getBookmarkLineIndex(i); 76 manager.addLoadedBookmark(lineIndex); 77 } 78 } 79 } 80 } 81 } 82 } 83 84 94 static synchronized void saveBookmarks(NbBookmarkManager manager) { 95 Document doc = manager.getDocument(); 96 FileObject fo = NbEditorUtilities.getFileObject(doc); 97 if (fo != null) { 98 int[] bookmarkLineIndexes = getLineIndexes(doc); 99 URL url; 100 try { 101 url = fo.getURL(); 102 } catch (FileStateInvalidException e) { 103 url = null; 105 } 106 107 if (url != null) { 108 FileBookmarks bookmarks = new FileBookmarks( 109 url, bookmarkLineIndexes); 110 Project owner = FileOwnerQuery.getOwner(fo); 111 FileBookmarksMap fileBookmarksMap = findBookmarksMap(owner); 112 if (fileBookmarksMap != null) { 113 fileBookmarksMap.put(bookmarks); 114 } 115 } 116 } 117 } 118 119 static synchronized void saveAllProjectBookmarks() { 120 List allPrjs = new ArrayList (project2bookmarksMap.keySet()); 121 for (Iterator it = allPrjs.iterator(); it.hasNext();) { 122 saveProjectBookmarks((Project)it.next()); 123 } 124 } 125 126 static void saveProjectBookmarks(Project prj) { 127 if (!ProjectManager.getDefault().isValid(prj)) { 128 return; } 130 FileBookmarksMap fileBookmarksMap = findBookmarksMap(prj); 131 if (fileBookmarksMap != null && fileBookmarksMap.isModified()) { 132 saveBookmarksMap(prj, fileBookmarksMap); 133 } 134 } 135 136 private static int[] getLineIndexes(Document doc) { 137 BookmarkList bookmarkList = BookmarkList.get(doc); 138 int bookmarkCount = bookmarkList.getBookmarkCount(); 139 int[] lineIndexesArray = new int[bookmarkCount]; 140 for (int i = 0; i < bookmarkCount; i++) { 141 Bookmark bookmark = bookmarkList.getBookmark(i); 142 lineIndexesArray[i] = bookmark.getLineIndex(); 143 } 144 return lineIndexesArray; 145 } 146 147 private static FileBookmarksMap findOwningBookmarksMap(FileObject fo) { 148 return findBookmarksMap(FileOwnerQuery.getOwner(fo)); 149 } 150 151 private static FileBookmarksMap findBookmarksMap(Project project) { 152 FileBookmarksMap map = null; 153 if (project != null) { 154 map = (FileBookmarksMap)project2bookmarksMap.get(project); 155 if (map == null) { 156 map = loadBookmarksMap(project); 157 if (map != null) { 158 project2bookmarksMap.put(project, map); 159 } 160 } 161 } 162 return map; 163 } 164 165 168 private static FileBookmarksMap loadBookmarksMap(Project project) { 169 AuxiliaryConfiguration ac = (AuxiliaryConfiguration)project.getLookup().lookup( 170 AuxiliaryConfiguration.class); 171 FileBookmarksMap projectBookmarksMap = new FileBookmarksMap(); 172 if (ac != null) { 173 Element bookmarksElem = ac.getConfigurationFragment( 174 BookmarksXMLHandler.EDITOR_BOOKMARKS_ELEM, 175 BookmarksXMLHandler.EDITOR_BOOKMARKS_NAMESPACE_URI, 176 false 177 ); 178 URL projectFolderURL; 179 try { 180 projectFolderURL = project.getProjectDirectory().getURL(); 181 } catch (FileStateInvalidException e) { 182 projectFolderURL = null; 184 } 185 if (bookmarksElem != null) { 187 BookmarksXMLHandler.loadFileBookmarksMap( 188 projectBookmarksMap, bookmarksElem, 189 projectFolderURL 190 ); 191 } 192 } 193 return projectBookmarksMap; 194 } 195 196 199 private static void saveBookmarksMap(Project project, 200 FileBookmarksMap fileBookmarksMap) { 201 AuxiliaryConfiguration ac = (AuxiliaryConfiguration)project.getLookup().lookup( 202 AuxiliaryConfiguration.class); 203 if (ac != null) { 204 URI baseURI; 205 try { 206 baseURI = new URI (project.getProjectDirectory().getURL().toExternalForm()); 207 } catch (FileStateInvalidException e) { 208 baseURI = null; 210 } catch (URISyntaxException e) { 211 baseURI = null; 213 } 214 Element bookmarksElem = BookmarksXMLHandler.saveFileBookmarksMap( 215 fileBookmarksMap, baseURI); 216 ac.putConfigurationFragment( 217 bookmarksElem, false); 218 } 219 } 220 221 } 222 223 | Popular Tags |