KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.BadLocationException JavaDoc;
23 import javax.swing.text.StyledDocument JavaDoc;
24 import org.openide.ErrorManager;
25 import org.openide.text.Annotation;
26 import java.text.MessageFormat JavaDoc;
27 import javax.swing.text.Document JavaDoc;
28 import javax.swing.text.Position JavaDoc;
29 import org.netbeans.lib.editor.bookmarks.spi.BookmarkImplementation;
30 import org.openide.text.NbDocument;
31 import org.openide.util.NbBundle;
32
33 /**
34  * Interface to a bookmark.
35  *
36  * @author Miloslav Metelka
37  */

38
39 public final class NbBookmarkImplementation extends Annotation
40 implements BookmarkImplementation {
41
42     static final String JavaDoc BOOKMARK_ANNOTATION_TYPE = "editor-bookmark"; // NOI18N
43

44     private final NbBookmarkManager manager;
45     
46     private Position JavaDoc pos;
47     
48     NbBookmarkImplementation(NbBookmarkManager manager, int offset) {
49         this.manager = manager;
50         Document JavaDoc doc = manager.getDocument();
51         try {
52             pos = doc.createPosition(offset);
53         } catch (BadLocationException JavaDoc e) {
54             ErrorManager.getDefault().notify(e);
55             pos = doc.getStartPosition();
56         }
57         if (doc instanceof StyledDocument JavaDoc) {
58             NbDocument.addAnnotation((StyledDocument JavaDoc)doc, pos, -1, this);
59         }
60     }
61     
62     public String JavaDoc getAnnotationType() {
63         return BOOKMARK_ANNOTATION_TYPE;
64     }
65
66     public String JavaDoc getShortDescription() {
67         String JavaDoc fmt = NbBundle.getBundle(NbBookmarkImplementation.class).getString("Bookmark_Tooltip"); // NOI18N
68
int lineIndex = getLineIndex();
69         return MessageFormat.format(fmt, new Object JavaDoc[] { new Integer JavaDoc(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 JavaDoc doc = manager.getDocument();
82         if (doc instanceof StyledDocument JavaDoc) {
83             NbDocument.removeAnnotation((StyledDocument JavaDoc)doc, this);
84         }
85     }
86     
87     public String JavaDoc toString() {
88         return getShortDescription();
89     }
90 }
91
92
Popular Tags