KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > syntax > spi > ErrorAnnotation


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  * ErrorAnnotation.java
22  *
23  * Created on November 9, 2004, 3:09 PM
24  */

25
26 package org.netbeans.modules.web.core.syntax.spi;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import javax.swing.text.JTextComponent JavaDoc;
33 import javax.swing.text.StyledDocument JavaDoc;
34 import org.netbeans.modules.editor.NbEditorDocument;
35 import org.netbeans.modules.web.core.syntax.JspParserErrorAnnotation;
36 import org.openide.cookies.EditorCookie;
37 import org.openide.cookies.LineCookie;
38 import org.openide.filesystems.FileObject;
39 import org.openide.loaders.DataObject;
40 import org.openide.loaders.DataObjectNotFoundException;
41 import org.openide.text.Annotation;
42 import org.openide.text.Line;
43
44
45 /**
46  *
47  * @author Petr Pisl
48  */

49 public class ErrorAnnotation {
50     
51     public static final int JSP_ERROR = 1;
52     
53     /** Jsp file, for which is the ErrorAnnotation */
54     private FileObject jspFo;
55     
56     private ArrayList JavaDoc annotations;
57     
58     /** Creates a new instance of ErrorAnnotation */
59     public ErrorAnnotation(FileObject jspFo) {
60         this.jspFo = jspFo;
61         annotations = new ArrayList JavaDoc();
62     }
63     
64     /** Adds annotation for the errors. If the error is already annotated, does nothing. If there are
65      * annotated erros, which are not in the input array, then these annotations are deleted.
66      *
67      *
68      */

69     public void annotate(ErrorInfo[] errors){
70         ArrayList JavaDoc added, removed, unchanged;
71         Collection JavaDoc newAnnotations;
72         
73         // obtain data object
74
DataObject doJsp;
75         try {
76             doJsp = DataObject.find(jspFo);
77         }
78         catch (DataObjectNotFoundException e){
79             return;
80         }
81         
82         EditorCookie editor = (EditorCookie)doJsp.getCookie(EditorCookie.class);
83         if (editor == null)
84             return;
85         StyledDocument JavaDoc document = editor.getDocument();
86         if (document == null)
87             return;
88         
89         // Fix issue #59568
90
if(editor.getOpenedPanes()==null)
91             return;
92         
93         // The approriate JText component
94
JTextComponent JavaDoc component = editor.getOpenedPanes()[0];
95         if (component != null){
96             if (errors != null && errors.length > 0){
97                 // Place the first error in the status bar
98
org.netbeans.editor.Utilities.setStatusBoldText(component , " " + errors[0].getDescription()); //NOI18N
99
}
100             else{
101                 // clear status bar
102
org.netbeans.editor.Utilities.clearStatusText(component);
103             }
104         }
105         
106         // create annotations from errors
107
newAnnotations = getAnnotations(errors, document);
108         // which annotations are really new
109
added=new ArrayList JavaDoc(newAnnotations);
110         added.removeAll(annotations);
111         // which annotations were here before
112
unchanged=new ArrayList JavaDoc(annotations);
113         unchanged.retainAll(newAnnotations);
114         // which annotations are obsolete
115
removed = annotations;
116         removed.removeAll(newAnnotations);
117         detachAnnotations(removed);
118
119         // are there new annotations?
120
if (!added.isEmpty()) {
121             final ArrayList JavaDoc finalAdded = added;
122             final DataObject doJsp2 = doJsp;
123             Runnable JavaDoc docRenderer = new Runnable JavaDoc() {
124                 public void run() {
125                     LineCookie cookie = (LineCookie)doJsp2.getCookie(LineCookie.class);
126                     Line.Set lines = cookie.getLineSet();
127
128                     for (Iterator JavaDoc i=finalAdded.iterator();i.hasNext();) {
129                         LineSetAnnotation ann=(LineSetAnnotation)i.next();
130                         ann.attachToLineSet(lines);
131                     }
132                 }
133             };
134
135             if (document != null) {
136                 document.render(docRenderer);
137             } else {
138                 docRenderer.run();
139             }
140         }
141         
142         // remember current annotations
143
annotations=unchanged;
144         annotations.addAll(added);
145        
146     }
147     
148     /** Transforms ErrosInfo to Annotation
149      */

150     private Collection JavaDoc getAnnotations(ErrorInfo[] errors, StyledDocument JavaDoc document) {
151         HashMap JavaDoc map = new HashMap JavaDoc(errors.length);
152         for (int i = 0; i < errors.length; i ++) {
153             ErrorInfo err = errors[i];
154             int line = err.getLine();
155
156             if (line<0)
157                 continue; // When error is outside the file, don't annotate it
158
int column = err.getColumn();
159             String JavaDoc message = err.getDescription();
160             LineSetAnnotation ann;
161             switch (err.getType()){
162                 case JSP_ERROR:
163                     ann = new JspParserErrorAnnotation(line, column, message, (NbEditorDocument)document);
164                     break;
165                 default:
166                     ann = new JspParserErrorAnnotation(line, column, message, (NbEditorDocument)document);
167                     break;
168             }
169            
170
171             // This is trying to ensure that annotations on the same
172
// line are "chained" (so we get a single annotation for
173
// multiple errors on a line).
174
// If we knew the errors were sorted by file & line number,
175
// this would be easy (and we wouldn't need to do the hashmap
176
// "sort"
177
Integer JavaDoc lineInt = new Integer JavaDoc(line);
178             /*LineSetAnnotation prev = (LineSetAnnotation)map.get(lineInt);
179             if (prev != null) {
180                 prev.chain(ann);
181             } else if (map.size() < maxErrors) {*/

182             map.put(lineInt, ann);
183             //}
184
}
185         return map.values();
186     }
187     
188     /** Removes obsolete annotations
189      */

190     
191     private static void detachAnnotations(Collection JavaDoc anns) {
192         Iterator JavaDoc i;
193
194         for (i=anns.iterator();i.hasNext();) {
195             Annotation ann=(Annotation)i.next();
196             if (ann.getAttachedAnnotatable() != null) {
197                 ann.detach();
198             }
199         }
200     }
201     
202     public abstract static class LineSetAnnotation extends Annotation {
203
204         public abstract void attachToLineSet(Line.Set lines);
205     }
206     
207     
208     public static class ErrorInfo {
209         /**
210          * Holds value of property description.
211          */

212         private String JavaDoc description;
213
214         /**
215          * Holds value of property line.
216          */

217         private int line;
218
219         /**
220          * Holds value of property column.
221          */

222         private int column;
223
224         /**
225          * Holds value of property type.
226          */

227         private int type;
228
229         
230         public ErrorInfo(String JavaDoc description, int line, int column, int type){
231             this.description = description;
232             this.line = line;
233             this.column = column;
234             this.type = type;
235         }
236         /**
237          * Getter for property description.
238          * @return Value of property description.
239          */

240         public String JavaDoc getDescription() {
241
242             return this.description;
243         }
244
245         /**
246          * Getter for property line.
247          * @return Value of property line.
248          */

249         public int getLine() {
250
251             return this.line;
252         }
253
254         /**
255          * Getter for property column.
256          * @return Value of property column.
257          */

258         public int getColumn() {
259
260             return this.column;
261         }
262
263         /**
264          * Getter for property type.
265          * @return Value of property type.
266          */

267         public int getType() {
268
269             return this.type;
270         }
271         
272         
273     }
274 }
275
Popular Tags