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 static org.netbeans.modules.showtodos.TodoSourceParsing.parseForLines; |
42 |
import java.io.IOException; |
43 |
import java.lang.ref.Reference; |
44 |
import java.lang.ref.WeakReference; |
45 |
import java.util.ArrayList; |
46 |
import java.util.Collection; |
47 |
import java.util.List; |
48 |
import java.util.Map; |
49 |
import java.util.WeakHashMap; |
50 |
import javax.swing.event.DocumentEvent; |
51 |
import javax.swing.event.DocumentListener; |
52 |
import javax.swing.text.Document; |
53 |
import org.netbeans.editor.BaseDocument; |
54 |
import org.netbeans.lib.editor.util.swing.DocumentListenerPriority; |
55 |
import org.netbeans.lib.editor.util.swing.DocumentUtilities; |
56 |
import org.openide.cookies.EditorCookie; |
57 |
import org.openide.filesystems.FileObject; |
58 |
import org.openide.loaders.DataObject; |
59 |
import org.openide.text.Annotation; |
60 |
import org.openide.text.AnnotationProvider; |
61 |
import org.openide.text.Line; |
62 |
import org.openide.util.Exceptions; |
63 |
import org.openide.util.Lookup; |
64 |
import org.openide.util.RequestProcessor; |
65 |
import org.openide.util.lookup.ServiceProvider; |
66 |
|
67 |
/** |
68 |
* @author Michel Graciano |
69 |
*/ |
70 |
@ServiceProvider(service = AnnotationProvider.class) |
71 |
public class TodoAnnotationProvider implements AnnotationProvider { |
72 |
private static final RequestProcessor requestProcessor = |
73 |
new RequestProcessor("TODO Annotation Parser", 2); |
74 |
private final Map<FileObject, Collection<Annotation>> file2annotation = |
75 |
new WeakHashMap<FileObject, Collection<Annotation>>(); |
76 |
|
77 |
public void annotate(Line.Set lines, Lookup context) { |
78 |
final DataObject dataObj = context.lookup(DataObject.class); |
79 |
new ScanningDocumentListener().attachTo(dataObj); |
80 |
annotate(dataObj); |
81 |
} |
82 |
|
83 |
private void annotate(final DataObject dataObj) { |
84 |
final FileObject fo = dataObj.getPrimaryFile(); |
85 |
if (file2annotation.containsKey(fo)) { |
86 |
for (Annotation annotation : file2annotation.get(fo)) { |
87 |
annotation.detach(); |
88 |
} |
89 |
} |
90 |
|
91 |
final List<Annotation> annotations = new ArrayList<Annotation>(); |
92 |
final BaseDocument doc = documentFor(dataObj); |
93 |
for (Line line : parse(doc)) { |
94 |
final TodoAnnotation ann = new TodoAnnotation(line.getText().trim()); |
95 |
ann.attach(line); |
96 |
annotations.add(ann); |
97 |
} |
98 |
file2annotation.put(fo, annotations); |
99 |
} |
100 |
|
101 |
private List<Line> parse(final BaseDocument doc) { |
102 |
return parseForLines(doc); |
103 |
} |
104 |
|
105 |
private static BaseDocument documentFor(final DataObject ob) { |
106 |
try { |
107 |
EditorCookie ck = ob.getCookie(EditorCookie.class); |
108 |
if (ck != null) { |
109 |
return (BaseDocument)ck.openDocument(); |
110 |
} |
111 |
} catch (IOException ioe) { |
112 |
Exceptions.printStackTrace(ioe); |
113 |
} |
114 |
return null; |
115 |
} |
116 |
|
117 |
private class ScanningDocumentListener implements DocumentListener, Runnable { |
118 |
private static final int AUTO_SCANNING_DELAY = 2000; //ms |
119 |
private final RequestProcessor.Task parseTask; |
120 |
private Reference<DataObject> dobref; |
121 |
|
122 |
ScanningDocumentListener() { |
123 |
parseTask = requestProcessor.create(this); |
124 |
} |
125 |
|
126 |
public void insertUpdate(DocumentEvent e) { |
127 |
change(e); |
128 |
} |
129 |
|
130 |
public void changedUpdate(DocumentEvent e) { |
131 |
} |
132 |
|
133 |
public void removeUpdate(DocumentEvent e) { |
134 |
change(e); |
135 |
} |
136 |
|
137 |
private void attachTo(Document document) { |
138 |
DocumentUtilities.addDocumentListener(document, this, |
139 |
DocumentListenerPriority.AFTER_CARET_UPDATE); |
140 |
restartTimer(); |
141 |
} |
142 |
|
143 |
private void attachTo(DataObject dob) { |
144 |
dobref = new WeakReference<DataObject>(dob); |
145 |
attachTo(documentFor(dob)); |
146 |
} |
147 |
|
148 |
private void change(DocumentEvent e) { |
149 |
restartTimer(); |
150 |
} |
151 |
|
152 |
private void restartTimer() { |
153 |
parseTask.schedule(AUTO_SCANNING_DELAY); |
154 |
} |
155 |
|
156 |
public void run() { |
157 |
DataObject dob = dobref.get(); |
158 |
if (dob != null) { |
159 |
annotate(dob); |
160 |
} |
161 |
} |
162 |
} |
163 |
} |