KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > Annotatable


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 package org.openide.text;
20
21 import java.beans.PropertyChangeSupport JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24
25 /** Classes which are capable of holding annotations must
26  * extend this abstract class. The examples of these classes are
27  * Line or Line.Part. It allows to add/remove
28  * Annotation(s) to this class. There is also support for
29  * listening on changes of the properties like deleted or
30  * count of attached annotations.
31  *
32  * @author David Konecny, Jaroslav Tulach
33  * @since 1.20
34  */

35 public abstract class Annotatable extends Object JavaDoc {
36     /** Property name of the count of annotations */
37     public static final String JavaDoc PROP_ANNOTATION_COUNT = "annotationCount"; // NOI18N
38

39     /** Property name for the deleted attribute */
40     public static final String JavaDoc PROP_DELETED = "deleted"; // NOI18N
41

42     /** Property name for the content of the annotatable
43      * @since 1.35
44      */

45     public static final String JavaDoc PROP_TEXT = "text"; // NOI18N
46

47     /** Support for property change listeners*/
48     private PropertyChangeSupport JavaDoc propertyChangeSupport;
49
50     /** Count of all annotations attached to this instance. */
51     private int annotationCount;
52
53     /** List of all annotations attached to this annotatable object */
54     private List JavaDoc<Annotation> attachedAnnotations;
55
56     /** Whether the Annotatable object was deleted during
57      * the editting of document or not. */

58     private boolean deleted;
59
60     public Annotatable() {
61         deleted = false;
62         annotationCount = 0;
63         propertyChangeSupport = new PropertyChangeSupport JavaDoc(this);
64         attachedAnnotations = new LinkedList JavaDoc<Annotation>();
65     }
66
67     /** Add annotation to this Annotatable class
68      * @param anno annotation which will be attached to this class */

69     protected void addAnnotation(Annotation anno) {
70         annotationCount++;
71         attachedAnnotations.add(anno);
72         propertyChangeSupport.firePropertyChange(PROP_ANNOTATION_COUNT, annotationCount - 1, annotationCount);
73     }
74
75     /** Remove annotation to this Annotatable class
76      * @param anno annotation which will be detached from this class */

77     protected void removeAnnotation(Annotation anno) {
78         annotationCount--;
79         attachedAnnotations.remove(anno);
80         propertyChangeSupport.firePropertyChange(PROP_ANNOTATION_COUNT, annotationCount + 1, annotationCount);
81     }
82
83     /** Gets the list of all annotations attached to this annotatable object
84      * @since 1.27 */

85     List JavaDoc<? extends Annotation> getAnnotations() {
86         return attachedAnnotations;
87     }
88
89     /** Add listeners on changes of annotatable properties
90      * @param l change listener*/

91     final public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
92         propertyChangeSupport.addPropertyChangeListener(l);
93     }
94
95     /** Remove listeners on changes of annotatable properties
96      * @param l change listener*/

97     final public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
98         propertyChangeSupport.removePropertyChangeListener(l);
99     }
100
101     /** Fire property change to registered listeners. */
102     final protected void firePropertyChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
103         propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
104     }
105
106     /** Whether this Annotatable object was removed or not.
107      * @return whether the Annotatable object was removed or not
108      */

109     final public boolean isDeleted() {
110         return deleted;
111     }
112
113     /** Get content of the annotatable. The listeners can listen
114      * on changes of PROP_TEXT property to learn that content of Annotatable
115      * is changing.
116      * @return text representing the content of annotatable. The return value can be null,
117      * what means that document is closed.
118      * @since 1.35
119      */

120     abstract public String JavaDoc getText();
121
122     /** Setter for property deleted.
123      * @param deleted New value of property deleted.
124      */

125     void setDeleted(boolean deleted) {
126         if (this.deleted != deleted) {
127             this.deleted = deleted;
128             propertyChangeSupport.firePropertyChange(PROP_DELETED, !deleted, deleted);
129         }
130     }
131
132     /** The count of already attached annotations. Modules can use
133      * this property to learn whether to this instance are
134      * already attached some annotations or not.
135      * @return count of attached annotations
136      */

137     final public int getAnnotationCount() {
138         return annotationCount;
139     }
140 }
141
Popular Tags