1 package org.netbeans.modules.tasklist.javadoc.ext; 2 3 import java.beans.PropertyChangeListener ; 4 import java.util.List ; 5 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.logging.Logger ; 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 32 public class JavaDocOpenProjectsScanner implements Runnable { 33 34 private List errors = new ArrayList (); 35 36 private FileChangeListener listener; 37 38 private Queue jobs = new Queue(); 39 private boolean stopScanning; 40 41 44 public JavaDocOpenProjectsScanner() { 45 OpenProjects.getDefault().addPropertyChangeListener( 46 new PropertyChangeListener () { 47 public void propertyChange(java.beans.PropertyChangeEvent 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 76 public void run() { 77 jobs.put(OpenProjects.getDefault()); 78 while (true) { 79 Object obj = (Object ) 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 (); 89 } 90 } 91 92 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 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 137 private void scan(FileObject fo) { 138 if (fo.isFolder()) 139 scanFolder(fo); 140 else 141 scanFile(fo); 142 } 143 144 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 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 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 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 e) { 194 Logger log = Logger.getLogger( 195 "org.netbeans.modules.tasklist.javadoc"); 196 log.warning(e.getMessage()); 197 } 198 } 199 200 206 private void removeForFile(FileObject f) { 207 List removed = new ArrayList (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 228 private void removeForNonExistentFiles() { 229 List removed = new ArrayList (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 |