KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.event.ChangeEvent JavaDoc;
23 import javax.swing.event.ChangeListener JavaDoc;
24 import javax.swing.text.Document JavaDoc;
25 import javax.swing.text.Element JavaDoc;
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 /**
34  * Interface to a bookmark.
35  *
36  * @author Miloslav Metelka
37  */

38
39 public final class NbBookmarkManager implements BookmarkManager {
40
41     private static final String JavaDoc BOOKMARK_ANNOTATION_TYPE = "editor-bookmark"; // NOI18N
42

43     private BookmarkManagerSupport support;
44     
45     public NbBookmarkManager() {
46     }
47
48     private transient ChangeListener JavaDoc bookmarksModuleListener = new ChangeListener JavaDoc() {
49         public void stateChanged(ChangeEvent JavaDoc 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         // First obtain offset for the line number
64
Document JavaDoc doc = getDocument();
65         Element JavaDoc lineRoot = doc.getDefaultRootElement();
66         int lineCount = lineRoot.getElementCount();
67         if (lineIndex < lineCount) {
68             Element JavaDoc lineElem = lineRoot.getElement(lineIndex);
69             int offset = lineElem.getStartOffset();
70             support.addBookmark(createBookmarkImplementation(offset));
71         } // bookmarks past end of document are not restored
72
}
73     
74     public Document JavaDoc 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 JavaDoc doc) {
89             DocumentUnmodifiedListener.init(doc); // start listening on notifyUnmodified()
90
return new NbBookmarkManager();
91         }
92     }
93     
94 }
95
Popular Tags