KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.netbeans.modules.tasklist.javadoc.ext;
2
3 import java.beans.PropertyChangeListener JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.logging.Logger JavaDoc;
8 import org.netbeans.api.java.project.JavaProjectConstants;
9 import org.netbeans.api.project.Project;
10 import org.netbeans.api.project.ProjectUtils;
11 import org.netbeans.api.project.SourceGroup;
12 import org.netbeans.api.project.Sources;
13 import org.netbeans.api.project.ui.OpenProjects;
14 import org.netbeans.modules.tasklist.core.TLUtils;
15 import org.openide.ErrorManager;
16 import org.openide.filesystems.FileChangeAdapter;
17 import org.openide.filesystems.FileObject;
18 import org.netbeans.modules.tasklist.client.*;
19 import org.openide.filesystems.FileChangeListener;
20 import org.openide.filesystems.FileEvent;
21 import org.openide.filesystems.FileRenameEvent;
22 import org.openide.loaders.DataObject;
23 import org.openide.loaders.DataObjectNotFoundException;
24 import org.openide.util.NbBundle;
25 import org.openide.util.Queue;
26
27 /**
28  * Scans text constants in all open projects.
29  *
30  * @author tl
31  */

32 public class JavaDocOpenProjectsScanner implements Runnable JavaDoc {
33     /** <Suggestion> */
34     private List JavaDoc errors = new ArrayList JavaDoc();
35     
36     private FileChangeListener listener;
37     
38     private Queue jobs = new Queue();
39     private boolean stopScanning;
40     
41     /**
42      * Creates a new instance of I18NScanner
43      */

44     public JavaDocOpenProjectsScanner() {
45         OpenProjects.getDefault().addPropertyChangeListener(
46             new PropertyChangeListener JavaDoc() {
47                 public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
48                     if (evt.getPropertyName() ==
49                         OpenProjects.PROPERTY_OPEN_PROJECTS) {
50                         stopScanning = true;
51                         jobs.put(OpenProjects.getDefault());
52                     }
53                 }
54             }
55         );
56         listener = new FileChangeAdapter() {
57             public void fileDataCreated(FileEvent fe) {
58                 jobs.put(fe.getFile());
59             }
60             public void fileChanged(FileEvent fe) {
61                 jobs.put(fe.getFile());
62             }
63             public void fileDeleted(FileEvent fe) {
64                 removeForFile(fe.getFile());
65             }
66             public void fileRenamed(FileRenameEvent fe) {
67                 removeForNonExistentFiles();
68                 jobs.put(fe.getFile());
69             }
70         };
71     }
72     
73     /**
74      * Runs the scanner
75      */

76     public void run() {
77         jobs.put(OpenProjects.getDefault());
78         while (true) {
79             Object JavaDoc obj = (Object JavaDoc) jobs.get();
80             stopScanning = false;
81             if (obj instanceof OpenProjects)
82                 scan((OpenProjects) obj);
83             else if (obj instanceof Project)
84                 scan((Project) obj);
85             else if (obj instanceof FileObject)
86                 scan((FileObject) obj);
87             else
88                 throw new InternalError JavaDoc();
89         }
90     }
91
92     /**
93      * Scans open projects.
94      *
95      * @param p open projects
96      */

97     private void scan(OpenProjects p) {
98         StaticSuggestions ss = StaticSuggestions.getDefault();
99         
100         for (int i = 0; i < errors.size(); i++) {
101             Suggestion s = (Suggestion) errors.get(i);
102             ss.remove(s);
103         }
104         errors.clear();
105         
106         Project[] prjs = p.getOpenProjects();
107         for (int i = 0; i < prjs.length; i++) {
108             if (stopScanning)
109                 return;
110             scan(prjs[i]);
111         }
112     }
113     
114     /**
115      * Scans a project
116      *
117      * @param p project to be scanned
118      */

119     private void scan(Project p) {
120         Sources s = ProjectUtils.getSources(p);
121         SourceGroup[] sgs = s.getSourceGroups(
122             JavaProjectConstants.SOURCES_TYPE_JAVA);
123         for (int j = 0; j < sgs.length; j++) {
124             if (stopScanning)
125                 return;
126
127             SourceGroup sg = sgs[j];
128             scan(sgs[j].getRootFolder());
129         }
130     }
131     
132     /**
133      * Scans a directory or a file
134      *
135      * @param fo a file object
136      */

137     private void scan(FileObject fo) {
138         if (fo.isFolder())
139             scanFolder(fo);
140         else
141             scanFile(fo);
142     }
143     
144     /**
145      * Scans a directory
146      *
147      * @param dir a directory
148      */

149     private void scanFolder(FileObject dir) {
150         dir.removeFileChangeListener(listener);
151         dir.addFileChangeListener(listener);
152         FileObject[] f = dir.getChildren();
153         for (int i = 0; i < f.length; i++) {
154             if (stopScanning)
155                 return;
156             
157             if (!f[i].isValid() || f[i].isVirtual())
158                 continue;
159             
160             scan(f[i]);
161         }
162     }
163     
164     /**
165      * Scans a file
166      *
167      * @param f a file to be scanned
168      */

169     private void scanFile(FileObject f) {
170         if (!f.getExt().equalsIgnoreCase("java") ||
171             f.existsExt("form"))
172             return;
173         
174         StaticSuggestions ss = StaticSuggestions.getDefault();
175         
176         // remove old suggestions for this class
177
removeForFile(f);
178         
179         DataObject do_;
180         try {
181             do_ = DataObject.find(f);
182         } catch (DataObjectNotFoundException e) {
183             return;
184         }
185         
186         try {
187             List JavaDoc err = JavadocProxy.findErrors(do_);
188             SuggestionManager sm = SuggestionManager.getDefault();
189             for (int i = 0; i < err.size(); i++) {
190                 ss.add((Suggestion) err.get(i));
191             }
192             errors.addAll(err);
193         } catch (Throwable JavaDoc e) {
194             Logger JavaDoc log = Logger.getLogger(
195                     "org.netbeans.modules.tasklist.javadoc");
196             log.warning(e.getMessage());
197         }
198     }
199     
200     /**
201      * Removes all created suggestions for the specified file
202      * and saves them in <code>removed</code>
203      *
204      * @param f a FileObject
205      */

206     private void removeForFile(FileObject f) {
207         List JavaDoc removed = new ArrayList JavaDoc(10);
208         for (int i = 0; i < errors.size();) {
209             Suggestion s = (Suggestion) errors.get(i);
210             if (f.equals(s.getFileObject())) {
211                 if (removed != null)
212                     removed.add(errors.get(i));
213                 errors.remove(i);
214             } else {
215                 i++;
216             }
217         }
218         StaticSuggestions ss = StaticSuggestions.getDefault();
219         for (int i = 0; i < removed.size(); i++) {
220             Suggestion s = (Suggestion) removed.get(i);
221             ss.remove(s);
222         }
223     }
224     
225     /**
226      * Removes suggestions for non-existent files.
227      */

228     private void removeForNonExistentFiles() {
229         List JavaDoc removed = new ArrayList JavaDoc(10);
230         for (int i = 0; i < errors.size();) {
231             Suggestion s = (Suggestion) errors.get(i);
232             FileObject fo = s.getFileObject();
233             if (!fo.isValid()) {
234                 if (removed != null)
235                     removed.add(errors.get(i));
236                 errors.remove(i);
237             } else {
238                 i++;
239             }
240         }
241         StaticSuggestions ss = StaticSuggestions.getDefault();
242         for (int i = 0; i < removed.size(); i++) {
243             Suggestion s = (Suggestion) removed.get(i);
244             ss.remove(s);
245         }
246     }
247 }
248
Popular Tags