Index: arch/arch-editor.xml
===================================================================
RCS file: /cvs/editor/arch/arch-editor.xml,v
retrieving revision 1.12
diff -u -r1.12 arch-editor.xml
--- arch/arch-editor.xml 4 Oct 2004 09:58:59 -0000 1.12
+++ arch/arch-editor.xml 28 Oct 2004 21:24:00 -0000
@@ -336,7 +336,12 @@
-->
- No.
+
+
+The editor uses this property to store a list of bookmarks that should be
+persisted between IDE restarts.
+The serialization is done by openide/editor.
+
Index: src/org/netbeans/modules/editor/NbEditorDocument.java
===================================================================
RCS file: /cvs/editor/src/org/netbeans/modules/editor/NbEditorDocument.java,v
retrieving revision 1.39
diff -u -r1.39 NbEditorDocument.java
--- src/org/netbeans/modules/editor/NbEditorDocument.java 18 Aug 2004 17:22:30 -0000 1.39
+++ src/org/netbeans/modules/editor/NbEditorDocument.java 28 Oct 2004 21:24:01 -0000
@@ -21,10 +21,13 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.text.AttributedCharacterIterator;
+import java.util.Iterator;
+import java.util.List;
import javax.swing.text.AttributeSet;
import javax.swing.JEditorPane;
import javax.swing.Timer;
import org.netbeans.editor.BaseKit;
+import org.netbeans.editor.Bookmarks;
import org.netbeans.editor.GuardedDocument;
import org.netbeans.editor.PrintContainer;
import org.netbeans.editor.Syntax;
@@ -74,6 +77,13 @@
/** Indent engine for the given kitClass. */
public static final String INDENT_ENGINE = "indentEngine"; // NOI18N
+
+ /**
+ * Persistent document's data property must be defined in the same way
+ * like in o.o.t.CloneableEditor.
+ */
+ private static final String persistentDataProperty = "persistentData";
+
/** Formatter being used. */
private Formatter formatter;
@@ -83,6 +93,8 @@
// #39718 hotfix
private WeakHashMap annoBlackList;
+
+ private boolean persistentBookmarksRestored;
public NbEditorDocument(Class kitClass) {
super(kitClass);
@@ -281,18 +293,54 @@
}
protected Dictionary createDocumentProperties(Dictionary origDocumentProperties) {
- return new LazyPropertyMap(origDocumentProperties) {
- public Object put(Object key, Object value) {
- Object origValue = super.put(key, value);
- if (Document.StreamDescriptionProperty.equals(key)) {
- if (origValue == null || !origValue.equals(value)) {
- fireStreamDescriptionChange();
- }
+ return new NbLazyPropertyMap(origDocumentProperties);
+ }
+
+ /**
+ * Notification that doc.getProperty(persistentDataProperty)
+ * was called.
+ *
+ * @param origValue original value stored in the property.
+ * @return either the origValue or an updated value. It will be returned
+ * to the requestor of the doc.getProperty(persistentDataProperty)
.
+ */
+ protected Object persistentDataGet(Object origValue) {
+ // First check if there is not other type value
+ // already assigned in the property (e.g. for testing purposes)
+ if (origValue == null || origValue instanceof List) {
+ List persistentBookmarks = new ArrayList();
+ Bookmarks bookmarks = getBookmarks();
+ Bookmarks.Bookmark bm = bookmarks.getNextLineBookmark(0);
+ while (bm != null) {
+ int bmLineIndex = bm.getLine();
+ persistentBookmarks.add(new PersistentBookmark(bmLineIndex));
+ bm = bookmarks.getNextLineBookmark(bmLineIndex + 1);
+ }
+ return persistentBookmarks;
+ }
+ return origValue;
+ }
+
+ /**
+ * Notification that doc.putProperty(persistentDataProperty, value)
+ * was called.
+ *
+ * @param origValue original value stored in the property.
+ * @return either the origValue or an updated value. It will be returned
+ * to the requestor of the doc.getProperty(persistentDataProperty)
.
+ */
+ protected void persistentDataPut(Object value) {
+ if (value instanceof List) {
+ if (!persistentBookmarksRestored) { // prevent duplicate restoring
+ // for multiple views over a single document
+ persistentBookmarksRestored = true;
+ List persistentBookmarks = (List)value;
+ for (Iterator it = persistentBookmarks.iterator(); it.hasNext();) {
+ PersistentBookmark bm = (PersistentBookmark)it.next();
+ NbEditorKit.NbToggleBookmarkAction.addBookmark(this, bm.getLineIndex());
}
-
- return origValue;
}
- };
+ }
}
/** Implementation of AnnotationDesc, which delegate to Annotation instance
@@ -392,4 +440,58 @@
}
+ private final class NbLazyPropertyMap extends LazyPropertyMap {
+
+ NbLazyPropertyMap(Dictionary origProperties) {
+ super(origProperties);
+ }
+
+ public Object get(Object key) {
+ Object val = super.get(key);
+
+ if (persistentDataProperty.equals(key)) {
+ Object newValue = persistentDataGet(val);
+ if (newValue != val) {
+ val = newValue;
+ super.put(key, val);
+ }
+ }
+ return val;
+ }
+
+ public Object put(Object key, Object value) {
+ Object origValue = super.put(key, value);
+
+ if (Document.StreamDescriptionProperty.equals(key)) {
+ if (origValue == null || !origValue.equals(value)) {
+ fireStreamDescriptionChange();
+ }
+ } else if (persistentDataProperty.equals(key)) {
+ persistentDataPut(value);
+ }
+
+ return origValue;
+ }
+
+ }
+
+ static final class PersistentBookmark implements java.io.Serializable {
+
+ static final long serialVersionUID = 1856269463976685784L;
+
+ private int lineIndex;
+
+ PersistentBookmark(int lineIndex) {
+ this.lineIndex = lineIndex;
+ }
+
+ public int getLineIndex() {
+ return lineIndex;
+ }
+
+ public String toString() {
+ return "lineIndex=" + lineIndex; // NOI18N
+ }
+
+ }
}
Index: src/org/netbeans/modules/editor/NbEditorKit.java
===================================================================
RCS file: /cvs/editor/src/org/netbeans/modules/editor/NbEditorKit.java,v
retrieving revision 1.76
diff -u -r1.76 NbEditorKit.java
--- src/org/netbeans/modules/editor/NbEditorKit.java 12 Oct 2004 08:25:40 -0000 1.76
+++ src/org/netbeans/modules/editor/NbEditorKit.java 28 Oct 2004 21:24:02 -0000
@@ -530,20 +530,23 @@
anno = bookmark.getAnno();
if (anno == null) {
- anno = new BookmarkAnnotation();
-
- Line lineObj = NbEditorUtilities.getLine(doc, caret.getDot(), false);
- if (lineObj == null) {
- target.getToolkit().beep();
- return;
- }
- anno.attach(lineObj);
-
- bookmarks.putBookmark(new Bookmark(anno));
+ addBookmark(doc, line);
} else {
anno.detach();
bookmarks.removeBookmark(bookmark);
}
+ }
+
+ static void addBookmark(BaseDocument doc, int lineIndex) {
+ Annotation anno = new BookmarkAnnotation();
+
+ Line lineObj = NbEditorUtilities.getLineFromIndex(doc, lineIndex, false);
+ if (lineObj == null) {
+ return;
+ }
+ anno.attach(lineObj);
+
+ doc.getBookmarks().putBookmark(new Bookmark(anno));
}
}
Index: src/org/netbeans/modules/editor/NbEditorUtilities.java
===================================================================
RCS file: /cvs/editor/src/org/netbeans/modules/editor/NbEditorUtilities.java,v
retrieving revision 1.15
diff -u -r1.15 NbEditorUtilities.java
--- src/org/netbeans/modules/editor/NbEditorUtilities.java 16 Feb 2004 10:23:15 -0000 1.15
+++ src/org/netbeans/modules/editor/NbEditorUtilities.java 28 Oct 2004 21:24:02 -0000
@@ -116,20 +116,30 @@
* @return the line object
*/
public static Line getLine(BaseDocument doc, int offset, boolean original) {
+ try {
+ return getLineFromIndex(doc, Utilities.getLineOffset(doc, offset), original);
+ } catch (BadLocationException e) {
+ return null;
+ }
+ }
+
+ /** Get the line object from the given line index.
+ * @param doc document for which the line is being retrieved
+ * @param index index of the line to return in the document.
+ * @param original whether to retrieve the original line (true) before
+ * the modifications were done or the current line (false)
+ * @return the line object
+ */
+ public static Line getLineFromIndex(BaseDocument doc, int lineIndex, boolean original) {
DataObject dob = getDataObject(doc);
if (dob != null) {
LineCookie lc = (LineCookie)dob.getCookie(LineCookie.class);
if (lc != null) {
Line.Set lineSet = lc.getLineSet();
if (lineSet != null) {
- try {
- int lineOffset = Utilities.getLineOffset(doc, offset);
- return original
- ? lineSet.getOriginal(lineOffset)
- : lineSet.getCurrent(lineOffset);
- } catch (BadLocationException e) {
- }
-
+ return original
+ ? lineSet.getOriginal(lineIndex)
+ : lineSet.getCurrent(lineIndex);
}
}
}