KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > bookmarks > PersistentBookmarks


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.bookmarks;
21
22 import java.net.URI JavaDoc;
23 import java.net.URISyntaxException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.WeakHashMap JavaDoc;
30 import javax.swing.text.Document JavaDoc;
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 JavaDoc;
41
42 /**
43  * Services to update or save bookmarks to persistent format.
44  *
45  * @author Miloslav Metelka
46  * @version 1.00
47  */

48
49 public class PersistentBookmarks {
50     
51     private static final int[] EMPTY_INT_ARRAY = new int[0];
52     
53     private static final Map JavaDoc project2bookmarksMap = new WeakHashMap JavaDoc();
54     
55     static synchronized void loadBookmarks(NbBookmarkManager manager) {
56         Document JavaDoc doc = manager.getDocument();
57         FileObject fo = NbEditorUtilities.getFileObject(doc);
58         if (fo != null) {
59             // Add the bookmarks if any
60
FileBookmarksMap map = findOwningBookmarksMap(fo);
61             if (map != null) {
62                 URL JavaDoc url;
63                 try {
64                     url = fo.getURL();
65                 } catch (FileStateInvalidException e) {
66                     // Ignore this file (could be deleted etc.)
67
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     /**
85      * Save the bookmarks for the given FileObject.
86      * <br>
87      * This is usually done upon bookmark toggling or when a file is being closed.
88      *
89      * <p>
90      * This method may also save bookmarks for other files for the same project
91      * as the bookmarks for all sources in the project are serialized into
92      * project's private.xml file.
93      */

94     static synchronized void saveBookmarks(NbBookmarkManager manager) {
95         Document JavaDoc doc = manager.getDocument();
96         FileObject fo = NbEditorUtilities.getFileObject(doc);
97         if (fo != null) {
98             int[] bookmarkLineIndexes = getLineIndexes(doc);
99             URL JavaDoc url;
100             try {
101                 url = fo.getURL();
102             } catch (FileStateInvalidException e) {
103                 // Ignore this file - could be deleted etc.
104
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 JavaDoc allPrjs = new ArrayList JavaDoc(project2bookmarksMap.keySet());
121         for (Iterator JavaDoc 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; // cannot modify it now anyway
129
}
130         FileBookmarksMap fileBookmarksMap = findBookmarksMap(prj);
131         if (fileBookmarksMap != null && fileBookmarksMap.isModified()) {
132             saveBookmarksMap(prj, fileBookmarksMap);
133         }
134     }
135     
136     private static int[] getLineIndexes(Document JavaDoc 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     /**
166      * Get a map of urls to line-numbers for the given project.
167      */

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 JavaDoc bookmarksElem = ac.getConfigurationFragment(
174                     BookmarksXMLHandler.EDITOR_BOOKMARKS_ELEM,
175                     BookmarksXMLHandler.EDITOR_BOOKMARKS_NAMESPACE_URI,
176                     false
177             );
178             URL JavaDoc projectFolderURL;
179             try {
180                 projectFolderURL = project.getProjectDirectory().getURL();
181             } catch (FileStateInvalidException e) {
182                 // Use global naming in such case
183
projectFolderURL = null;
184             }
185             // If bookmarks element exists load the data from it
186
if (bookmarksElem != null) {
187                 BookmarksXMLHandler.loadFileBookmarksMap(
188                         projectBookmarksMap, bookmarksElem,
189                         projectFolderURL
190                 );
191             }
192         }
193         return projectBookmarksMap;
194     }
195
196     /**
197      * Save the map of bookmarks into project's private xml file.
198      */

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 JavaDoc baseURI;
205             try {
206                 baseURI = new URI JavaDoc(project.getProjectDirectory().getURL().toExternalForm());
207             } catch (FileStateInvalidException e) {
208                 // Use global urls in such case
209
baseURI = null;
210             } catch (URISyntaxException JavaDoc e) {
211                 // Use global urls in such case
212
baseURI = null;
213             }
214             Element JavaDoc bookmarksElem = BookmarksXMLHandler.saveFileBookmarksMap(
215                     fileBookmarksMap, baseURI);
216             ac.putConfigurationFragment(
217                     bookmarksElem, false);
218         }
219     }
220
221 }
222
223
Popular Tags