1 19 20 package org.netbeans.modules.tomcat5.util; 21 22 import java.io.File ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 import java.util.Collections ; 26 import org.netbeans.api.java.classpath.GlobalPathRegistry; 27 import org.openide.ErrorManager; 28 import org.openide.cookies.EditorCookie; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileUtil; 31 import org.openide.loaders.DataObject; 32 import org.openide.loaders.DataObjectNotFoundException; 33 import org.openide.text.Annotation; 34 import org.openide.text.Line; 35 import org.openide.util.NbBundle; 36 import org.openide.windows.*; 37 38 43 public class LogSupport { 44 private Map links = Collections.synchronizedMap(new HashMap ()); 45 private Annotation errAnnot; 46 47 55 public Link getLink(String errorMsg, String path, int line) { 56 Link newLink = new Link(errorMsg, path, line); 57 Link cachedLink = (Link)links.get(newLink); 58 if (cachedLink != null) { 59 return cachedLink; 60 } 61 links.put(newLink, newLink); 62 return newLink; 63 } 64 65 68 public void detachAnnotation() { 69 if (errAnnot != null) { 70 errAnnot.detach(); 71 } 72 } 73 74 77 public static class LineInfo { 78 private String path; 79 private int line; 80 private String message; 81 private boolean error; 82 private boolean accessible; 83 84 93 public LineInfo(String path, int line, String message, boolean error, boolean accessible) { 94 this.path = path; 95 this.line = line; 96 this.message = message; 97 this.error = error; 98 this.accessible = accessible; 99 } 100 101 public String path() { 102 return path; 103 } 104 105 public int line() { 106 return line; 107 } 108 109 public String message() { 110 return message; 111 } 112 113 public boolean isError() { 114 return error; 115 } 116 117 public boolean isAccessible() { 118 return accessible; 119 } 120 121 public String toString() { 122 return "path=" + path + " line=" + line + " message=" + message 123 + " isError=" + error + " isAccessible=" + accessible; 124 } 125 } 126 127 130 static class ErrorAnnotation extends Annotation { 131 private String shortDesc = null; 132 133 public ErrorAnnotation(String desc) { 134 shortDesc = desc; 135 } 136 137 public String getAnnotationType() { 138 return "org-netbeans-modules-tomcat5-error"; } 140 141 public String getShortDescription() { 142 return shortDesc; 143 } 144 145 } 146 147 153 public class Link implements OutputListener { 154 private String msg; 155 private String path; 156 private int line; 157 158 private int hashCode = 0; 159 160 Link(String msg, String path, int line) { 161 this.msg = msg; 162 this.path = path; 163 this.line = line; 164 } 165 166 public int hashCode() { 167 if (hashCode == 0) { 168 int result = 17; 169 result = 37 * result + line; 170 result = 37 * result + (path != null ? path.hashCode() : 0); 171 result = 37 * result + (msg != null ? msg.hashCode() : 0); 172 hashCode = result; 173 } 174 return hashCode; 175 } 176 177 public boolean equals(Object obj) { 178 if (this == obj) { 179 return true; 180 } 181 if (obj instanceof Link) { 182 Link anotherLink = (Link)obj; 183 if ((((msg != null) && msg.equals(anotherLink.msg)) || (msg == anotherLink.msg)) 184 && (((path != null) && path.equals(anotherLink.path)) || (path == anotherLink.path)) 185 && line == anotherLink.line) { 186 return true; 187 } 188 } 189 return false; 190 } 191 192 196 public void outputLineAction(OutputEvent ev) { 197 FileObject sourceFile = GlobalPathRegistry.getDefault().findResource(path); 198 if (sourceFile == null) { 199 sourceFile = FileUtil.toFileObject(new File (path)); 200 } 201 DataObject dataObject = null; 202 if (sourceFile != null) { 203 try { 204 dataObject = DataObject.find(sourceFile); 205 } catch(DataObjectNotFoundException ex) { 206 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 207 } 208 } 209 if (dataObject != null) { 210 EditorCookie editorCookie = (EditorCookie)dataObject.getCookie(EditorCookie.class); 211 if (editorCookie == null) { 212 return; 213 } 214 editorCookie.open(); 215 Line errorLine = null; 216 try { 217 errorLine = editorCookie.getLineSet().getCurrent(line - 1); 218 } catch (IndexOutOfBoundsException iobe) { 219 return; 220 } 221 if (errAnnot != null) { 222 errAnnot.detach(); 223 } 224 String errorMsg = msg; 225 if (errorMsg == null || errorMsg.equals("")) { errorMsg = NbBundle.getMessage(Link.class, "MSG_ExceptionOccurred"); 227 } 228 errAnnot = new ErrorAnnotation(errorMsg); 229 errAnnot.attach(errorLine); 230 errAnnot.moveToFront(); 231 errorLine.show(Line.SHOW_TRY_SHOW); 232 } 233 } 234 235 239 public void outputLineCleared(OutputEvent ev) { 240 if (errAnnot != null) { 241 errAnnot.detach(); 242 } 243 if (!links.isEmpty()) { 244 links.clear(); 245 } 246 } 247 248 public void outputLineSelected(OutputEvent ev) { 249 } 250 } 251 } 252 | Popular Tags |