KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ParserAnnotation


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 /*
21  * ParserAnnontation.java
22  *
23  * Created on March 27, 2002, 6:53 PM
24  */

25
26 package org.netbeans.modules.java;
27
28 import java.beans.PropertyChangeEvent JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import org.netbeans.jmi.javamodel.ErrorType;
31 import org.netbeans.jmi.javamodel.ErrorTypeEnum;
32 import org.openide.text.Line;
33 import org.openide.text.Annotatable;
34 import org.openide.text.Line.Set;
35
36 /**
37  *
38  * @author th125165
39  */

40 class ParserAnnotation extends LineSetAnnotation implements PropertyChangeListener JavaDoc {
41
42     private final String JavaDoc error;
43     private final int line,column;
44     private int state;
45     private final ErrorType severity;
46     private Line docline;
47     private ParserAnnotation chained;
48     
49     private static final int STATE_NEW=1;
50     private static final int STATE_ATTACHED = 2;
51     private static final int STATE_DETACHED = 3;
52     
53     /** Creates a new instance of ParserAnnontation */
54     ParserAnnotation(int l, int c, ErrorType sev, String JavaDoc err) {
55         line=l;
56         column=c;
57         error = err;
58         state = STATE_NEW;
59         severity = sev;
60     }
61     
62     public String JavaDoc getAnnotationType() {
63         if (ErrorTypeEnum.ERROR.equals(getSeverity())) {
64             return "org-netbeans-modules-java-parser_annotation_err"; // NOI18N
65
} else {
66             return "org-netbeans-modules-java-parser_annotation_warn"; // NOI18N
67
}
68     }
69     
70     public String JavaDoc getShortDescription() {
71         // Localize this with NbBundle:
72
if (chained!=null)
73             return error+"\n\n"+chained.getShortDescription(); // NOI18N
74
return error;
75     }
76     
77     int getLine() {
78         return line;
79     }
80     
81     int getColumn() {
82         return column;
83     }
84     
85     String JavaDoc getError() {
86         return error;
87     }
88     
89     ErrorType getSeverity() {
90         return severity;
91     }
92     
93     void chain(ParserAnnotation anno) {
94         if (chained!=null)
95             chained.chain(anno);
96         else
97             chained=anno;
98     }
99     
100     private int getState() {
101         return state;
102     }
103     
104     protected void notifyAttached(final Annotatable toAnno) {
105         super.notifyAttached(toAnno);
106         docline.addPropertyChangeListener(this);
107         state = STATE_ATTACHED;
108     }
109     
110     protected void notifyDetached(Annotatable fromAnno) {
111         super.notifyDetached(fromAnno);
112         docline.removePropertyChangeListener(this);
113         state=STATE_DETACHED;
114     }
115     
116     public boolean equals(Object JavaDoc obj) {
117         boolean eq=shallowEquals(obj);
118         
119         if (!eq)
120             return false;
121         if (chained!=null)
122             return chained.equals(((ParserAnnotation)obj).chained);
123         return true;
124     }
125            
126     private boolean shallowEquals(Object JavaDoc obj) {
127         if (obj instanceof ParserAnnotation) {
128             ParserAnnotation ann=(ParserAnnotation)obj;
129             
130             if (this==obj)
131                 return true;
132             if (line!=ann.getLine())
133                 return false;
134             if (column!=ann.getColumn())
135                 return false;
136             if (!error.equals(ann.getError()))
137                 return false;
138             if (getState()==STATE_DETACHED || ann.getState()==STATE_DETACHED)
139                 return false;
140             return true;
141         }
142         return false;
143     }
144     
145     public void attachToLineSet(Set lines) {
146         char string[];
147         int start,end;
148         Line.Part part;
149         
150         try {
151             docline=lines.getCurrent(line-1);
152         } catch (IndexOutOfBoundsException JavaDoc ex) {
153             // the document has been changed and the line is deleted
154
return;
155         }
156         String JavaDoc annTxt = docline.getText();
157         if (annTxt == null) return; // document is already closed
158
string = annTxt.toCharArray();
159         start=0;
160         end=string.length-1;
161         while (start<=end && string[start]<=' ') {
162             start++;
163         }
164         while (start<=end && string[end]<=' ') {
165             end--;
166         }
167         if (start<=end)
168             part=docline.createPart(start,end-start+1);
169         else
170             part=docline.createPart(0,string.length);
171         attach(part);
172     }
173     
174     public void propertyChange(PropertyChangeEvent JavaDoc ev) {
175         String JavaDoc type = ev.getPropertyName();
176         if (type == null || type == Annotatable.PROP_TEXT) { // User edited the line, assume error should be cleared.
177
detach();
178         }
179     }
180 }
181
Popular Tags