1 package org.netbeans.modules.tasklist.i18n; 2 3 import java.beans.PropertyChangeListener ; 4 import java.util.List ; 5 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import org.netbeans.api.java.project.JavaProjectConstants; 8 import org.netbeans.api.project.Project; 9 import org.netbeans.api.project.ProjectUtils; 10 import org.netbeans.api.project.SourceGroup; 11 import org.netbeans.api.project.Sources; 12 import org.netbeans.api.project.ui.OpenProjects; 13 import org.netbeans.modules.tasklist.core.TLUtils; 14 import org.openide.ErrorManager; 15 import org.openide.filesystems.FileChangeAdapter; 16 import org.openide.filesystems.FileObject; 17 import org.netbeans.modules.tasklist.client.*; 18 import org.openide.filesystems.FileChangeListener; 19 import org.openide.filesystems.FileEvent; 20 import org.openide.filesystems.FileRenameEvent; 21 import org.openide.loaders.DataObject; 22 import org.openide.loaders.DataObjectNotFoundException; 23 import org.openide.util.NbBundle; 24 import org.openide.util.Queue; 25 26 31 public class TranslateOpenProjectsScanner implements Runnable { 32 33 private List errors = new ArrayList (); 34 35 private FileChangeListener listener; 36 37 private Queue jobs = new Queue(); 38 private boolean stopScanning; 39 40 43 public TranslateOpenProjectsScanner() { 44 OpenProjects.getDefault().addPropertyChangeListener( 45 new PropertyChangeListener () { 46 public void propertyChange(java.beans.PropertyChangeEvent evt) { 47 if (evt.getPropertyName() == 48 OpenProjects.PROPERTY_OPEN_PROJECTS) { 49 stopScanning = true; 50 jobs.put(OpenProjects.getDefault()); 51 } 52 } 53 } 54 ); 55 listener = new FileChangeAdapter() { 56 public void fileDataCreated(FileEvent fe) { 57 TranslateSuggestionProvider.LOGGER.fine("f"); 58 jobs.put(fe.getFile()); 59 } 60 public void fileChanged(FileEvent fe) { 61 TranslateSuggestionProvider.LOGGER.fine("f"); 62 jobs.put(fe.getFile()); 63 } 64 public void fileDeleted(FileEvent fe) { 65 TranslateSuggestionProvider.LOGGER.fine("f"); 66 removeForFile(fe.getFile()); 67 } 68 public void fileRenamed(FileRenameEvent fe) { 69 TranslateSuggestionProvider.LOGGER.fine("f"); 70 removeForNonExistentFiles(); 71 jobs.put(fe.getFile()); 72 } 73 }; 74 } 75 76 79 public void run() { 80 jobs.put(OpenProjects.getDefault()); 81 while (true) { 82 Object obj = (Object ) jobs.get(); 83 stopScanning = false; 84 if (obj instanceof OpenProjects) 85 scan((OpenProjects) obj); 86 else if (obj instanceof Project) 87 scan((Project) obj); 88 else if (obj instanceof FileObject) 89 scan((FileObject) obj); 90 else 91 throw new InternalError (); 92 } 93 } 94 95 100 private void scan(OpenProjects p) { 101 StaticSuggestions ss = StaticSuggestions.getDefault(); 102 103 for (int i = 0; i < errors.size(); i++) { 104 Suggestion s = (Suggestion) errors.get(i); 105 ss.remove(s); 106 } 107 errors.clear(); 108 109 Project[] prjs = p.getOpenProjects(); 110 for (int i = 0; i < prjs.length; i++) { 111 if (stopScanning) 112 return; 113 scan(prjs[i]); 114 } 115 } 116 117 122 private void scan(Project p) { 123 TranslateSuggestionProvider.LOGGER.fine("search in prj " + p); 124 Sources s = ProjectUtils.getSources(p); 125 SourceGroup[] sgs = s.getSourceGroups( 126 JavaProjectConstants.SOURCES_TYPE_JAVA); 127 for (int j = 0; j < sgs.length; j++) { 128 if (stopScanning) 129 return; 130 131 SourceGroup sg = sgs[j]; 132 scan(sgs[j].getRootFolder()); 133 } 134 } 135 136 141 private void scan(FileObject fo) { 142 if (fo.isFolder()) 143 scanFolder(fo); 144 else 145 scanFile(fo); 146 } 147 148 153 private void scanFolder(FileObject dir) { 154 dir.removeFileChangeListener(listener); 155 dir.addFileChangeListener(listener); 156 FileObject[] f = dir.getChildren(); 157 for (int i = 0; i < f.length; i++) { 158 if (stopScanning) 159 return; 160 161 if (!f[i].isValid() || f[i].isVirtual()) 162 continue; 163 164 scan(f[i]); 165 } 166 } 167 168 173 private void scanFile(FileObject f) { 174 if (!f.getExt().equalsIgnoreCase("java") || 175 f.existsExt("form")) 176 return; 177 178 StaticSuggestions ss = StaticSuggestions.getDefault(); 179 180 removeForFile(f); 182 183 TranslateFileChecker.Error[] err = new TranslateFileChecker(f).run(); 184 TranslateSuggestionProvider.LOGGER.fine(err.length + " search in " + f); 185 186 SuggestionManager sm = SuggestionManager.getDefault(); 187 List e = new ArrayList (); 188 for (int i = 0; i < err.length; i++) { 189 AddI18NCommentPerformer action = new AddI18NCommentPerformer(); 190 SuggestionAgent problem = 191 sm.createSuggestion(f, TranslateSuggestionProvider.TYPE, 192 NbBundle.getMessage(TranslateOpenProjectsScanner.class, "ProblemText", err[i].constant), action, null); 194 try { 195 DataObject dataObject = DataObject.find(f); 196 problem.setLine(TLUtils.getLineByNumber(dataObject, 197 err[i].line + 1)); 198 } catch (DataObjectNotFoundException er) { 199 ErrorManager.getDefault().notify(er); 201 } 202 203 e.add(problem.getSuggestion()); 204 ss.add(problem.getSuggestion()); 205 } 206 errors.addAll(e); 207 } 208 209 215 private void removeForFile(FileObject f) { 216 List removed = new ArrayList (10); 217 for (int i = 0; i < errors.size();) { 218 Suggestion s = (Suggestion) errors.get(i); 219 if (f.equals(s.getFileObject())) { 220 if (removed != null) 221 removed.add(errors.get(i)); 222 errors.remove(i); 223 } else { 224 i++; 225 } 226 } 227 StaticSuggestions ss = StaticSuggestions.getDefault(); 228 for (int i = 0; i < removed.size(); i++) { 229 Suggestion s = (Suggestion) removed.get(i); 230 ss.remove(s); 231 } 232 } 233 234 237 private void removeForNonExistentFiles() { 238 List removed = new ArrayList (10); 239 for (int i = 0; i < errors.size();) { 240 Suggestion s = (Suggestion) errors.get(i); 241 FileObject fo = s.getFileObject(); 242 if (!fo.isValid()) { 243 if (removed != null) 244 removed.add(errors.get(i)); 245 errors.remove(i); 246 } else { 247 i++; 248 } 249 } 250 StaticSuggestions ss = StaticSuggestions.getDefault(); 251 for (int i = 0; i < removed.size(); i++) { 252 Suggestion s = (Suggestion) removed.get(i); 253 ss.remove(s); 254 } 255 } 256 } 257 | Popular Tags |