KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > docscan > SourceTasksScanner


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.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 JavaDoc;
40 import java.lang.reflect.Method JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Enumeration JavaDoc;
44 import java.io.IOException JavaDoc;
45
46 /**
47  * Scans project for source todos action performer.
48  *
49  * @author Petr Kuzel
50  */

51 final class SourceTasksScanner {
52
53     /**
54      * Scans for all project tasks in asynchronous threads (a scanner
55      * thread and AWT thread).
56      *
57      * @param view requestor
58      */

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     /**
68      * Scan selected folders for TODOs.
69      *
70      * @param view target consumer
71      * @param folders contetx to scan
72      * @return interruptible handle
73      */

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                     // block until previous AWT events get processed
101
// it does not survive nested invokeAndWaits
102
SwingUtilities.invokeAndWait(
103                         new Runnable JavaDoc() {
104                             public void run() {
105                                 view.setCursor(Utilities.createProgressCursor(view));
106                             }
107                         }
108                     );
109                 } catch (InterruptedException JavaDoc ignore) {
110                     // XXX
111
} catch (InvocationTargetException JavaDoc e) {
112                     ErrorManager.getDefault().notify(e);
113                 }
114                 scanProjectSuggestions(list, view, this);
115
116             } finally {
117                 SwingUtilities.invokeLater(
118                     new Runnable JavaDoc() {
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 JavaDoc(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 JavaDoc 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         // HACK XXX ProjectCookie is deprecated without replacement
187
// access it's registration file directly
188
FileSystem fs = Repository.getDefault().getDefaultFileSystem();
189         FileObject registration = fs.findResource("Services/Hidden/org-netbeans-modules-projects-ProjectCookieImpl.instance");
190         if (registration == null) {
191             // it's not installed or some incomaptible version
192
return false;
193         } else {
194             try {
195                 DataObject dobj = DataObject.find(registration);
196                 InstanceCookie ic = (InstanceCookie) dobj.getCookie(InstanceCookie.class);
197                 Object JavaDoc obj = ic.instanceCreate();
198                 Method JavaDoc method = obj.getClass().getMethod("projectDesktop", new Class JavaDoc[0]);
199                 Node node = (Node) method.invoke(obj, new Object JavaDoc[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 JavaDoc e) {
213                 return false;
214             } catch (ClassNotFoundException JavaDoc e) {
215                 return false;
216             } catch (NoSuchMethodException JavaDoc e) {
217                 return false;
218             } catch (IllegalAccessException JavaDoc e) {
219                 return false;
220             } catch (InvocationTargetException JavaDoc e) {
221                 return false;
222             }
223         }
224     }
225
226 }
227
Popular Tags