KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > javadoc > ext > JavadocProxy


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.tasklist.javadoc.ext;
21
22 import org.netbeans.modules.tasklist.providers.SuggestionContext;
23 import org.netbeans.modules.tasklist.javadoc.*;
24 import org.netbeans.modules.tasklist.client.SuggestionPerformer;
25 import org.netbeans.modules.tasklist.client.SuggestionAgent;
26 import org.netbeans.modules.tasklist.client.SuggestionManager;
27 import org.openide.cookies.SourceCookie;
28 import org.openide.src.*;
29 import org.openide.loaders.DataObject;
30 import org.openide.loaders.DataObjectNotFoundException;
31 import org.openide.text.NbDocument;
32 import org.openide.text.Line;
33 import org.openide.util.Utilities;
34
35 import javax.swing.text.StyledDocument JavaDoc;
36 import javax.swing.*;
37 import java.util.List JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.lang.reflect.Modifier JavaDoc;
41 import java.awt.*;
42
43 /**
44  * Entry point to copied functionality
45  *
46  * @author Petr Kuzel
47  * @author tl
48  */

49 public final class JavadocProxy {
50
51     /**
52      * Searches for JavaDoc errors.
53      *
54      * @param dobj a DataObject. SourceCookie is necessary to find any errors.
55      * @return List<Suggestion> or null
56      */

57     public static List JavaDoc findErrors(DataObject dobj) {
58         SourceCookie sc = (SourceCookie)dobj.getCookie(SourceCookie.class);
59
60         // We end up showing this panel for non-javadoc areas as well,
61
// such as Bundle.properties files. Make sure we only operate
62
// on files we have SourceCookies for!
63
if (sc == null)
64             return null;
65
66         AutoCommenter peer = new AutoCommenter();
67         peer.elements = new ArrayList JavaDoc();
68
69         SourceElement se = sc.getSource();
70         if ( se != null ) {
71             ClassElement[] ces = se.getAllClasses();
72             for( int j = 0; j < ces.length; j++ ){
73                 peer.addElements( ces[j] );
74             }
75         }
76
77         ArrayList JavaDoc tasks = new ArrayList JavaDoc(30);
78
79         // check for Javadoc for API (protected & public)
80
int modifierMask = Modifier.PUBLIC | Modifier.PROTECTED;
81         int errorMask = AutoCommenter.JDC_ERROR | AutoCommenter.JDC_MISSING;
82         boolean bpackage = false; // Modifier.PACKAGE
83

84         DefaultListModel model = peer.prepareListModel(modifierMask, bpackage, errorMask);
85
86         int n = model.size();
87         for (int i = 0; i < n; i++) {
88             Object JavaDoc value = model.get(i);
89             final AutoCommenter.Element element = (AutoCommenter.Element)value;
90             if (element.getErrorNumber() ==
91                 AutoCommenter.JDC_OK) {
92                 continue;
93             }
94
95             SuggestionPerformer action = new JavaDocSuggestionPerformer(
96                 new ElementProxy(element), dobj
97             );
98
99             DefaultListModel m2 = element.getErrorList();
100             int m2n = m2.size();
101             for (int j = 0; j < m2n; j++) {
102                 Object JavaDoc v2 = m2.get(j);
103                 String JavaDoc summary = element.getSrcElement().getName() +
104                     ": " + // NOI18N
105
v2.toString();
106
107                 MemberElement el = element.getSrcElement();
108 // TODO what's this
109
// if ((el instanceof MethodElement) &&
110
// (inheritsJavadoc((MethodElement)el))) {
111
// continue;
112
// }
113
SuggestionAgent s = SuggestionManager.getDefault().createSuggestion(
114                     null, DocSuggester.TYPE, summary, null, DocSuggester.TYPE);
115
116                 //LineCookie ck = el.getCookie
117
// TODO: get & add line position
118
// Line l = null;
119
//s.setLine(l);
120

121                 SourceCookie.Editor editor =
122                     (SourceCookie.Editor)dobj.getCookie(SourceCookie.Editor.class);
123                 javax.swing.text.Element JavaDoc textElement = editor.sourceToText(el);
124                 if (textElement != null) {
125                     StyledDocument JavaDoc document = editor.getDocument();
126                     if (document != null) {
127                         int offset = textElement.getStartOffset();
128                         int lineNumber = NbDocument.findLineNumber(document, offset);
129                         Line line = editor.getLineSet().getCurrent(lineNumber);
130                         s.setLine(line);
131                     }
132                 }
133                 /* Error checking - do something similar above
134                 try {
135                     javax.swing.text.Element textElement =
136                         editor.sourceToText((Element)offendingObject);
137
138                     if (textElement != null)
139                         {
140                             StyledDocument document = findDocument(editor);
141
142                             if (document != null) {
143                                 int offset = textElement.getStartOffset();
144                                 line = NbDocument.findLineNumber(document,
145                                                                  offset) + 1;
146                                 column = NbDocument.findLineColumn(document,
147                                                                    offset) + 1;
148                             }
149                         }
150                 } catch (IllegalArgumentException iae) {
151                     // found an element which doesn't have source
152                     // just don't set up a line and column
153                 }
154                 */

155
156                 if (element.isCorrectable()) {
157                     if (element.getErrorNumber() ==
158                         AutoCommenter.JDC_MISSING) {
159                         // isCorrectable() seems to lie. Missing javadocs
160
// are never correctable.
161
Image taskIcon = Utilities.loadImage("org/netbeans/modules/tasklist/javadoc/missing.gif"); // NOI18N
162
s.setIcon(taskIcon);
163                     } else {
164                         Image taskIcon = Utilities.loadImage("org/netbeans/modules/tasklist/javadoc/fixable-error.gif"); // NOI18N
165
s.setIcon(taskIcon);
166                         s.setAction(action);
167                     }
168                 } else if (element.getErrorNumber() ==
169                            AutoCommenter.JDC_MISSING) {
170                     Image taskIcon = Utilities.loadImage("org/netbeans/modules/tasklist/javadoc/missing.gif"); // NOI18N
171
s.setIcon(taskIcon);
172                 }
173
174                 tasks.add(s.getSuggestion());
175             }
176         }
177         return tasks;
178
179     }
180
181     public static class ElementProxy {
182
183         private AutoCommenter.Element peer;
184
185         public ElementProxy(AutoCommenter.Element peer) {
186             this.peer = peer;
187         }
188
189         /** List of human readable resolutions. */
190         public List JavaDoc getResolutionList() {
191             // TODO extract from the old omplementation
192
return Collections.EMPTY_LIST;
193         }
194
195         public JavaDoc getJavaDoc() {
196             return peer.getJavaDoc();
197         }
198
199         public void viewSource() {
200             peer.viewSource();
201         }
202
203         public void autoCorrect() throws SourceException {
204             peer.autoCorrect();
205         }
206
207     }
208
209
210 // private boolean inheritsJavadoc(MethodElement method) {
211
// ClassElement cl = method.getDeclaringClass();
212
// if (cl == null) {
213
// return false;
214
// }
215
// MethodParameter[] params = method.getParameters();
216
// Type[] types = new Type[params.length];
217
// for (int i = 0; i < params.length; i++) {
218
// types[i] = params[i].getType();
219
// }
220
// boolean[] checkedObject = new boolean[] {false};
221
// boolean found = inheritsJavadoc(cl, method, method.getName(), types, checkedObject);
222
// return found;
223
// }
224
//
225
// private boolean inheritsJavadoc(ClassElement cl, MethodElement self,
226
// Identifier method, Type[] arguments,
227
// boolean[] checkedObject) {
228
// checkedObject[0] |= cl.getName().getFullName().equals("java.lang.Object"); // NOI18N
229
//
230
// // See if the class itself contains the given method
231
// MethodElement mel = cl.getMethod(method, arguments);
232
// if ((mel != null) && (mel != self)) {
233
// // Check to see if the method has javadoc
234
// JavaDoc javadoc = mel.getJavaDoc();
235
// if ((javadoc != null) && !javadoc.isEmpty()) {
236
// return true;
237
// }
238
// }
239
//
240
// // Check interfaces
241
// Identifier[] interfaces = cl.getInterfaces();
242
// for (int j = 0; j < interfaces.length; j++) {
243
// ClassElement icl = ClassElement.forName(interfaces[j].getFullName());
244
// if (icl == null) {
245
// continue;
246
// }
247
// boolean found = inheritsJavadoc(icl, self, method, arguments, checkedObject);
248
// if (found) {
249
// return true;
250
// }
251
// }
252
//
253
// // Check super class
254
// Identifier scli = cl.getSuperclass();
255
// if (scli == null && !checkedObject[0]) {
256
// scli = Identifier.create("java.lang.Object"); // NOI18N
257
// }
258
// if (scli != null) {
259
// ClassElement scl = ClassElement.forName(scli.getFullName());
260
// if (scl != null) {
261
// boolean found = inheritsJavadoc(scl, self, method, arguments, checkedObject);
262
// if (found) {
263
// return true;
264
// }
265
// }
266
// }
267
// return false;
268
// }
269

270 }
271
Popular Tags