1 19 20 package org.netbeans.modules.editor.bookmarks; 21 22 import javax.swing.text.BadLocationException ; 23 import javax.swing.text.StyledDocument ; 24 import org.openide.ErrorManager; 25 import org.openide.text.Annotation; 26 import java.text.MessageFormat ; 27 import javax.swing.text.Document ; 28 import javax.swing.text.Position ; 29 import org.netbeans.lib.editor.bookmarks.spi.BookmarkImplementation; 30 import org.openide.text.NbDocument; 31 import org.openide.util.NbBundle; 32 33 38 39 public final class NbBookmarkImplementation extends Annotation 40 implements BookmarkImplementation { 41 42 static final String BOOKMARK_ANNOTATION_TYPE = "editor-bookmark"; 44 private final NbBookmarkManager manager; 45 46 private Position pos; 47 48 NbBookmarkImplementation(NbBookmarkManager manager, int offset) { 49 this.manager = manager; 50 Document doc = manager.getDocument(); 51 try { 52 pos = doc.createPosition(offset); 53 } catch (BadLocationException e) { 54 ErrorManager.getDefault().notify(e); 55 pos = doc.getStartPosition(); 56 } 57 if (doc instanceof StyledDocument ) { 58 NbDocument.addAnnotation((StyledDocument )doc, pos, -1, this); 59 } 60 } 61 62 public String getAnnotationType() { 63 return BOOKMARK_ANNOTATION_TYPE; 64 } 65 66 public String getShortDescription() { 67 String fmt = NbBundle.getBundle(NbBookmarkImplementation.class).getString("Bookmark_Tooltip"); int lineIndex = getLineIndex(); 69 return MessageFormat.format(fmt, new Object [] { new Integer (lineIndex + 1) }); 70 } 71 72 public int getOffset() { 73 return pos.getOffset(); 74 } 75 76 public int getLineIndex() { 77 return manager.getDocument().getDefaultRootElement().getElementIndex(getOffset()); 78 } 79 80 public void release() { 81 Document doc = manager.getDocument(); 82 if (doc instanceof StyledDocument ) { 83 NbDocument.removeAnnotation((StyledDocument )doc, this); 84 } 85 } 86 87 public String toString() { 88 return getShortDescription(); 89 } 90 } 91 92 | Popular Tags |