Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2009 Sun Microsystems, Inc. |
38 |
*/ |
39 |
package org.netbeans.modules.showtodos; |
40 |
|
41 |
import java.beans.PropertyChangeEvent; |
42 |
import java.beans.PropertyChangeListener; |
43 |
import javax.swing.text.BadLocationException; |
44 |
import static org.netbeans.modules.showtodos.TodoSourceParsing.parseForAnnotatable; |
45 |
import java.io.IOException; |
46 |
import java.lang.ref.Reference; |
47 |
import java.lang.ref.WeakReference; |
48 |
import java.util.ArrayList; |
49 |
import java.util.Collection; |
50 |
import java.util.List; |
51 |
import java.util.Map; |
52 |
import java.util.WeakHashMap; |
53 |
import javax.swing.event.DocumentEvent; |
54 |
import javax.swing.event.DocumentListener; |
55 |
import javax.swing.text.Document; |
56 |
import org.netbeans.editor.BaseDocument; |
57 |
import org.netbeans.lib.editor.util.swing.DocumentListenerPriority; |
58 |
import org.netbeans.lib.editor.util.swing.DocumentUtilities; |
59 |
import org.netbeans.modules.tasklist.todo.settings.Settings; |
60 |
import org.openide.cookies.EditorCookie; |
61 |
import org.openide.filesystems.FileObject; |
62 |
import org.openide.loaders.DataObject; |
63 |
import org.openide.text.Annotatable; |
64 |
import org.openide.text.Annotation; |
65 |
import org.openide.text.AnnotationProvider; |
66 |
import org.openide.text.Line; |
67 |
import org.openide.util.Exceptions; |
68 |
import org.openide.util.Lookup; |
69 |
import org.openide.util.RequestProcessor; |
70 |
import org.openide.util.WeakListeners; |
71 |
import org.openide.util.lookup.ServiceProvider; |
72 |
|
73 |
/** |
74 |
* @author Michel Graciano |
75 |
*/ |
76 |
@ServiceProvider(service = AnnotationProvider.class) |
77 |
public class TodoAnnotationProvider implements AnnotationProvider { |
78 |
private static final RequestProcessor requestProcessor = |
79 |
new RequestProcessor("TODO Annotation Parser", 2); |
80 |
private final Map<FileObject, Collection<Annotation>> file2annotation = |
81 |
new WeakHashMap<FileObject, Collection<Annotation>>(); |
82 |
|
83 |
public void annotate(Line.Set lines, Lookup context) { |
84 |
final DataObject dataObj = context.lookup(DataObject.class); |
85 |
new ScanningListener().attachTo(dataObj); |
86 |
annotate(dataObj); |
87 |
} |
88 |
|
89 |
private void annotate(final DataObject dataObj) { |
90 |
try { |
91 |
final FileObject fo = dataObj.getPrimaryFile(); |
92 |
if (file2annotation.containsKey(fo)) { |
93 |
for (Annotation annotation : file2annotation.get(fo)) { |
94 |
annotation.detach(); |
95 |
} |
96 |
} |
97 |
final List<Annotation> annotations = |
98 |
new ArrayList<Annotation>(); |
99 |
final BaseDocument doc = documentFor(dataObj); |
100 |
for (Annotatable annotatable : parse(doc)) { |
101 |
final TodoAnnotation ann = |
102 |
new TodoAnnotation(annotatable.getText()); |
103 |
ann.attach(annotatable); |
104 |
annotations.add(ann); |
105 |
} |
106 |
file2annotation.put(fo, annotations); |
107 |
} catch (BadLocationException ex) { |
108 |
Exceptions.printStackTrace(ex); |
109 |
} |
110 |
} |
111 |
|
112 |
private List<Annotatable> parse(final BaseDocument doc) throws |
113 |
BadLocationException { |
114 |
return parseForAnnotatable(doc); |
115 |
} |
116 |
|
117 |
private static BaseDocument documentFor(final DataObject ob) { |
118 |
try { |
119 |
EditorCookie ck = ob.getCookie(EditorCookie.class); |
120 |
if (ck != null) { |
121 |
return (BaseDocument)ck.openDocument(); |
122 |
} |
123 |
} catch (IOException ioe) { |
124 |
Exceptions.printStackTrace(ioe); |
125 |
} |
126 |
return null; |
127 |
} |
128 |
|
129 |
private class ScanningListener implements DocumentListener, |
130 |
PropertyChangeListener, Runnable { |
131 |
private static final int AUTO_SCANNING_DELAY = 2000; //ms |
132 |
private final RequestProcessor.Task parseTask; |
133 |
private Reference<DataObject> dobref; |
134 |
|
135 |
ScanningListener() { |
136 |
parseTask = requestProcessor.create(this); |
137 |
} |
138 |
|
139 |
public void insertUpdate(DocumentEvent e) { |
140 |
change(); |
141 |
} |
142 |
|
143 |
public void changedUpdate(DocumentEvent e) { |
144 |
} |
145 |
|
146 |
public void removeUpdate(DocumentEvent e) { |
147 |
change(); |
148 |
} |
149 |
|
150 |
public void propertyChange(PropertyChangeEvent evt) { |
151 |
if (Settings.PROP_SCAN_COMMENTS_ONLY.equals(evt.getPropertyName())) { |
152 |
change(); |
153 |
} |
154 |
} |
155 |
|
156 |
private void attachTo(Document document) { |
157 |
DocumentUtilities.addDocumentListener(document, this, |
158 |
DocumentListenerPriority.AFTER_CARET_UPDATE); |
159 |
Settings.getDefault().addPropertyChangeListener(WeakListeners. |
160 |
propertyChange(this, Settings.getDefault())); |
161 |
restartTimer(); |
162 |
} |
163 |
|
164 |
private void attachTo(DataObject dob) { |
165 |
dobref = new WeakReference<DataObject>(dob); |
166 |
attachTo(documentFor(dob)); |
167 |
} |
168 |
|
169 |
private void change() { |
170 |
restartTimer(); |
171 |
} |
172 |
|
173 |
private void restartTimer() { |
174 |
parseTask.schedule(AUTO_SCANNING_DELAY); |
175 |
} |
176 |
|
177 |
public void run() { |
178 |
DataObject dob = dobref.get(); |
179 |
if (dob != null) { |
180 |
annotate(dob); |
181 |
} |
182 |
} |
183 |
} |
184 |
} |