1 19 20 package org.netbeans.modules.editor.bookmarks; 21 22 import javax.swing.event.ChangeEvent ; 23 import javax.swing.event.ChangeListener ; 24 import javax.swing.text.Document ; 25 import javax.swing.text.Element ; 26 import org.netbeans.lib.editor.bookmarks.api.BookmarkList; 27 import org.netbeans.lib.editor.bookmarks.spi.BookmarkImplementation; 28 import org.netbeans.lib.editor.bookmarks.spi.BookmarkManager; 29 import org.netbeans.lib.editor.bookmarks.spi.BookmarkManagerFactory; 30 import org.netbeans.lib.editor.bookmarks.spi.BookmarkManagerSupport; 31 import org.openide.util.WeakListeners; 32 33 38 39 public final class NbBookmarkManager implements BookmarkManager { 40 41 private static final String BOOKMARK_ANNOTATION_TYPE = "editor-bookmark"; 43 private BookmarkManagerSupport support; 44 45 public NbBookmarkManager() { 46 } 47 48 private transient ChangeListener bookmarksModuleListener = new ChangeListener () { 49 public void stateChanged(ChangeEvent ev) { 50 BookmarkList bl = support.getBookmarkList(); 51 bl.getDocument().putProperty(BookmarkList.class, null); 52 bl.removeAllBookmarks(); 53 } 54 }; 55 56 public void init(BookmarkManagerSupport support) { 57 this.support = support; 58 EditorBookmarksModule.addChangeListener(WeakListeners.change(bookmarksModuleListener, EditorBookmarksModule.class)); 59 PersistentBookmarks.loadBookmarks(this); 60 } 61 62 void addLoadedBookmark(int lineIndex) { 63 Document doc = getDocument(); 65 Element lineRoot = doc.getDefaultRootElement(); 66 int lineCount = lineRoot.getElementCount(); 67 if (lineIndex < lineCount) { 68 Element lineElem = lineRoot.getElement(lineIndex); 69 int offset = lineElem.getStartOffset(); 70 support.addBookmark(createBookmarkImplementation(offset)); 71 } } 73 74 public Document getDocument() { 75 return support.getBookmarkList().getDocument(); 76 } 77 78 public BookmarkImplementation createBookmarkImplementation(int offset) { 79 return new NbBookmarkImplementation(this, offset); 80 } 81 82 public void saveBookmarks() { 83 PersistentBookmarks.saveBookmarks(this); 84 } 85 86 public static final class Factory implements BookmarkManagerFactory { 87 88 public BookmarkManager createBookmarkManager(Document doc) { 89 DocumentUnmodifiedListener.init(doc); return new NbBookmarkManager(); 91 } 92 } 93 94 } 95 | Popular Tags |