1 19 20 package org.netbeans.modules.tasklist.docscan; 21 22 import org.netbeans.modules.tasklist.suggestions.SuggestionList; 23 import org.netbeans.modules.tasklist.suggestions.SuggestionsScanner; 24 import org.netbeans.modules.tasklist.core.Background; 25 import org.netbeans.modules.tasklist.core.CancellableRunnable; 26 import org.openide.util.RequestProcessor; 27 import org.openide.util.Utilities; 28 import org.openide.util.Cancellable; 29 import org.openide.ErrorManager; 30 import org.openide.nodes.Node; 31 import org.openide.cookies.InstanceCookie; 32 import org.openide.filesystems.Repository; 33 import org.openide.filesystems.FileSystem; 34 import org.openide.filesystems.FileObject; 35 import org.openide.loaders.DataObject; 36 import org.openide.loaders.DataObjectNotFoundException; 37 38 import javax.swing.*; 39 import java.lang.reflect.InvocationTargetException ; 40 import java.lang.reflect.Method ; 41 import java.util.List ; 42 import java.util.ArrayList ; 43 import java.util.Enumeration ; 44 import java.io.IOException ; 45 46 51 final class SourceTasksScanner { 52 53 59 public static Background scanTasksAsync(final SourceTasksView view) { 60 61 final SuggestionList list = (SuggestionList) view.getList(); 62 63 Bg bg = new Bg(view, list, null); 64 return Background.execute(bg); 65 } 66 67 74 public static Background scanTasksAsync(SourceTasksView view, DataObject.Container[] folders) { 75 final SuggestionList list = (SuggestionList) view.getList(); 76 77 Bg bg = new Bg(view, list, folders); 78 return Background.execute(bg); 79 80 } 81 82 83 static class Bg implements CancellableRunnable { 84 85 private Cancellable cancellable; 86 87 private final SourceTasksView view; 88 private final SuggestionList list; 89 private final DataObject.Container[] ctx; 90 91 Bg(SourceTasksView view, SuggestionList list, DataObject.Container[] ctx) { 92 this.view = view; 93 this.list = list; 94 this.ctx = ctx; 95 } 96 97 public void run() { 98 try { 99 try { 100 SwingUtilities.invokeAndWait( 103 new Runnable () { 104 public void run() { 105 view.setCursor(Utilities.createProgressCursor(view)); 106 } 107 } 108 ); 109 } catch (InterruptedException ignore) { 110 } catch (InvocationTargetException e) { 112 ErrorManager.getDefault().notify(e); 113 } 114 scanProjectSuggestions(list, view, this); 115 116 } finally { 117 SwingUtilities.invokeLater( 118 new Runnable () { 119 public void run() { 120 view.statistics(list.size()); 121 view.setCursor(null); 122 } 123 } 124 ); 125 } 126 } 127 128 public boolean cancel() { 129 if (cancellable != null) { 130 return cancellable.cancel(); 131 } 132 return false; 133 } 134 }; 135 136 static void scanProjectSuggestions(final SuggestionList list, final SourceTasksAction.ScanProgressMonitor view, Bg bg) { 137 DataObject.Container projectFolders[] = bg.ctx; 138 139 if (projectFolders == null) { 140 List project = new ArrayList (23); 141 boolean enabled = false; 142 143 if ("project".equals(System.getProperty("todos.project", "repository"))) { 144 enabled = project(project); 145 } 146 147 if (enabled == false) { 148 project.clear(); 149 repository(project); 150 } 151 152 projectFolders = new DataObject.Container[project.size()]; 153 project.toArray(projectFolders); 154 } 155 156 SuggestionsScanner c = SuggestionsScanner.getDefault(); 157 c.setUsabilityLimit(Settings.getDefault().getUsabilityLimit()); 158 bg.cancellable = c; 159 c.scan(projectFolders, list, view, new SourceTasksProviderAcceptor()); 160 } 161 162 static void repository(List folders) { 163 Repository repository = Repository.getDefault(); 164 Enumeration en = repository.fileSystems(); 165 166 while (en.hasMoreElements()) { 167 FileSystem next = (FileSystem) en.nextElement(); 168 if (next.isDefault() || next.isHidden() || next.isReadOnly()) { 169 continue; 170 } 171 if (next.isValid()) { 172 FileObject fo = next.getRoot(); 173 try { 174 DataObject dobj = DataObject.find(fo); 175 if (dobj instanceof DataObject.Container) { 176 folders.add(dobj); 177 } 178 } catch (DataObjectNotFoundException e) { 179 e.printStackTrace(); 180 } 181 } 182 } 183 } 184 185 static boolean project(List folders) { 186 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 189 FileObject registration = fs.findResource("Services/Hidden/org-netbeans-modules-projects-ProjectCookieImpl.instance"); 190 if (registration == null) { 191 return false; 193 } else { 194 try { 195 DataObject dobj = DataObject.find(registration); 196 InstanceCookie ic = (InstanceCookie) dobj.getCookie(InstanceCookie.class); 197 Object obj = ic.instanceCreate(); 198 Method method = obj.getClass().getMethod("projectDesktop", new Class [0]); 199 Node node = (Node) method.invoke(obj, new Object [0]); 200 DataObject prjDO = (DataObject) node.getCookie(DataObject.class); 201 if (prjDO instanceof DataObject.Container) { 202 DataObject[] kids = ((DataObject.Container)prjDO).getChildren(); 203 for (int i=0; i<kids.length; i++) { 204 if (kids[i] instanceof DataObject.Container) { 205 folders.add(kids[i]); 206 } 207 } 208 } 209 return true; 210 } catch (DataObjectNotFoundException e) { 211 return false; 212 } catch (IOException e) { 213 return false; 214 } catch (ClassNotFoundException e) { 215 return false; 216 } catch (NoSuchMethodException e) { 217 return false; 218 } catch (IllegalAccessException e) { 219 return false; 220 } catch (InvocationTargetException e) { 221 return false; 222 } 223 } 224 } 225 226 } 227 | Popular Tags |