KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tomcat5 > util > LogSupport


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 package org.netbeans.modules.tomcat5.util;
21
22 import java.io.File JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Collections JavaDoc;
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 /**
39  * <code>LogSupport</code> class for creating links in the output window.
40  *
41  * @author Stepan Herold
42  */

43 public class LogSupport {
44     private Map JavaDoc/*<Link, Link>*/ links = Collections.synchronizedMap(new HashMap JavaDoc());
45     private Annotation errAnnot;
46     
47     /**
48      * Return a link which implements <code>OutputListener</code> interface. Link
49      * is then used to represent a link in the output window. This class also
50      * handles error annotations which are shown after a line is clicked.
51      *
52      * @return link which implements <code>OutputListener</code> interface. Link
53      * is then used to represent a link in the output window.
54      */

55     public Link getLink(String JavaDoc errorMsg, String JavaDoc 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     /**
66      * Detach error annotation.
67      */

68     public void detachAnnotation() {
69         if (errAnnot != null) {
70             errAnnot.detach();
71         }
72     }
73     
74     /**
75      * <code>LineInfo</code> is used to store info about the parsed line.
76      */

77     public static class LineInfo {
78         private String JavaDoc path;
79         private int line;
80         private String JavaDoc message;
81         private boolean error;
82         private boolean accessible;
83         
84         /**
85          * <code>LineInfo</code> is used to store info about the parsed line.
86          *
87          * @param path path to file
88          * @param line line number where the error occurred
89          * @param message error message
90          * @param error represents the line an error?
91          * @param accessible is the file accessible?
92          */

93         public LineInfo(String JavaDoc path, int line, String JavaDoc 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 JavaDoc path() {
102             return path;
103         }
104         
105         public int line() {
106             return line;
107         }
108         
109         public String JavaDoc 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 JavaDoc toString() {
122             return "path=" + path + " line=" + line + " message=" + message
123                     + " isError=" + error + " isAccessible=" + accessible;
124         }
125     }
126     
127     /**
128      * Error annotation.
129      */

130     static class ErrorAnnotation extends Annotation {
131         private String JavaDoc shortDesc = null;
132         
133         public ErrorAnnotation(String JavaDoc desc) {
134             shortDesc = desc;
135         }
136         
137         public String JavaDoc getAnnotationType() {
138             return "org-netbeans-modules-tomcat5-error"; // NOI18N
139
}
140         
141         public String JavaDoc getShortDescription() {
142             return shortDesc;
143         }
144         
145     }
146     
147     /**
148      * <code>Link</code> is used to create a link in the output window. To create
149      * a link use the <code>getLink</code> method of the <code>LogSupport</code>
150      * class. This prevents from memory vast by returning already existing instance,
151      * if one with such values exists.
152      */

153     public class Link implements OutputListener {
154         private String JavaDoc msg;
155         private String JavaDoc path;
156         private int line;
157         
158         private int hashCode = 0;
159         
160         Link(String JavaDoc msg, String JavaDoc 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 JavaDoc 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         /**
193          * If the link is clicked, required file is opened in the editor and an
194          * <code>ErrorAnnotation</code> is attached.
195          */

196         public void outputLineAction(OutputEvent ev) {
197             FileObject sourceFile = GlobalPathRegistry.getDefault().findResource(path);
198             if (sourceFile == null) {
199                 sourceFile = FileUtil.toFileObject(new File JavaDoc(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 JavaDoc iobe) {
219                     return;
220                 }
221                 if (errAnnot != null) {
222                     errAnnot.detach();
223                 }
224                 String JavaDoc errorMsg = msg;
225                 if (errorMsg == null || errorMsg.equals("")) { //NOI18N
226
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         /**
236          * If a link is cleared, error annotation is detached and link cache is
237          * clared.
238          */

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