1 19 20 25 26 package org.netbeans.modules.web.core.syntax.spi; 27 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import javax.swing.text.JTextComponent ; 33 import javax.swing.text.StyledDocument ; 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 49 public class ErrorAnnotation { 50 51 public static final int JSP_ERROR = 1; 52 53 54 private FileObject jspFo; 55 56 private ArrayList annotations; 57 58 59 public ErrorAnnotation(FileObject jspFo) { 60 this.jspFo = jspFo; 61 annotations = new ArrayList (); 62 } 63 64 69 public void annotate(ErrorInfo[] errors){ 70 ArrayList added, removed, unchanged; 71 Collection newAnnotations; 72 73 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 document = editor.getDocument(); 86 if (document == null) 87 return; 88 89 if(editor.getOpenedPanes()==null) 91 return; 92 93 JTextComponent component = editor.getOpenedPanes()[0]; 95 if (component != null){ 96 if (errors != null && errors.length > 0){ 97 org.netbeans.editor.Utilities.setStatusBoldText(component , " " + errors[0].getDescription()); } 100 else{ 101 org.netbeans.editor.Utilities.clearStatusText(component); 103 } 104 } 105 106 newAnnotations = getAnnotations(errors, document); 108 added=new ArrayList (newAnnotations); 110 added.removeAll(annotations); 111 unchanged=new ArrayList (annotations); 113 unchanged.retainAll(newAnnotations); 114 removed = annotations; 116 removed.removeAll(newAnnotations); 117 detachAnnotations(removed); 118 119 if (!added.isEmpty()) { 121 final ArrayList finalAdded = added; 122 final DataObject doJsp2 = doJsp; 123 Runnable docRenderer = new Runnable () { 124 public void run() { 125 LineCookie cookie = (LineCookie)doJsp2.getCookie(LineCookie.class); 126 Line.Set lines = cookie.getLineSet(); 127 128 for (Iterator 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 annotations=unchanged; 144 annotations.addAll(added); 145 146 } 147 148 150 private Collection getAnnotations(ErrorInfo[] errors, StyledDocument document) { 151 HashMap map = new HashMap (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; int column = err.getColumn(); 159 String 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 Integer lineInt = new Integer (line); 178 182 map.put(lineInt, ann); 183 } 185 return map.values(); 186 } 187 188 190 191 private static void detachAnnotations(Collection anns) { 192 Iterator 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 212 private String description; 213 214 217 private int line; 218 219 222 private int column; 223 224 227 private int type; 228 229 230 public ErrorInfo(String 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 240 public String getDescription() { 241 242 return this.description; 243 } 244 245 249 public int getLine() { 250 251 return this.line; 252 } 253 254 258 public int getColumn() { 259 260 return this.column; 261 } 262 263 267 public int getType() { 268 269 return this.type; 270 } 271 272 273 } 274 } 275 | Popular Tags |