129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.source.projection.ProjectionSummary;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18 import java.util.ArrayList;
|
|
19 import java.util.HashMap;
|
|
20 import java.util.Iterator;
|
|
21 import java.util.List;
|
|
22 import java.util.Map;
|
|
23
|
|
24 import dwtx.core.runtime.IProgressMonitor;
|
|
25 import dwtx.core.runtime.NullProgressMonitor;
|
|
26 import dwtx.jface.text.IDocument;
|
|
27 import dwtx.jface.text.IRegion;
|
|
28 import dwtx.jface.text.ISynchronizable;
|
|
29 import dwtx.jface.text.Position;
|
|
30 import dwtx.jface.text.source.Annotation;
|
|
31 import dwtx.jface.text.source.IAnnotationAccess;
|
|
32 import dwtx.jface.text.source.IAnnotationAccessExtension;
|
|
33 import dwtx.jface.text.source.IAnnotationModel;
|
|
34 import dwtx.jface.text.source.IAnnotationModelExtension;
|
|
35
|
|
36
|
|
37 /**
|
|
38 * Strategy for managing annotation summaries for collapsed ranges.
|
|
39 *
|
|
40 * @since 3.0
|
|
41 */
|
|
42 class ProjectionSummary {
|
|
43
|
|
44 private class Summarizer : Thread {
|
|
45
|
|
46 private bool fReset= true;
|
|
47
|
|
48 /**
|
|
49 * Creates a new thread.
|
|
50 */
|
|
51 public Summarizer() {
|
|
52 fProgressMonitor= new NullProgressMonitor(); // might be given by client in the future
|
|
53 setDaemon(true);
|
|
54 start();
|
|
55 }
|
|
56
|
|
57 /**
|
|
58 * Resets the thread.
|
|
59 */
|
|
60 public void reset() {
|
|
61 synchronized (fLock) {
|
|
62 fReset= true;
|
|
63 fProgressMonitor.setCanceled(true);
|
|
64 }
|
|
65 }
|
|
66
|
|
67 /*
|
|
68 * @see java.lang.Thread#run()
|
|
69 */
|
|
70 public void run() {
|
|
71 while (true) {
|
|
72 synchronized (fLock) {
|
|
73 if (!fReset)
|
|
74 break;
|
|
75 fReset= false;
|
|
76 fProgressMonitor.setCanceled(false);
|
|
77 }
|
|
78 internalUpdateSummaries(fProgressMonitor);
|
|
79 }
|
|
80
|
|
81 synchronized (fLock) {
|
|
82 fSummarizer= null;
|
|
83 }
|
|
84 }
|
|
85 }
|
|
86
|
|
87
|
|
88 private ProjectionViewer fProjectionViewer;
|
|
89 private IAnnotationModel fAnnotationModel;
|
|
90 private IAnnotationAccess fAnnotationAccess;
|
|
91 private List fConfiguredAnnotationTypes;
|
|
92
|
|
93 private Object fLock= new Object();
|
|
94 private IProgressMonitor fProgressMonitor;
|
|
95 private volatile Summarizer fSummarizer;
|
|
96
|
|
97 /**
|
|
98 * Creates a new projection summary.
|
|
99 *
|
|
100 * @param projectionViewer the projection viewer
|
|
101 * @param annotationAccess the annotation access
|
|
102 */
|
|
103 public ProjectionSummary(ProjectionViewer projectionViewer, IAnnotationAccess annotationAccess) {
|
|
104 super();
|
|
105 fProjectionViewer= projectionViewer;
|
|
106 fAnnotationAccess= annotationAccess;
|
|
107 }
|
|
108
|
|
109 /**
|
|
110 * Adds the given annotation type. For now on, annotations of that type are
|
|
111 * also reflected in their enclosing collapsed regions.
|
|
112 *
|
|
113 * @param annotationType the annotation type to add
|
|
114 */
|
|
115 public void addAnnotationType(String annotationType) {
|
|
116 synchronized(fLock) {
|
|
117 if (fConfiguredAnnotationTypes is null) {
|
|
118 fConfiguredAnnotationTypes= new ArrayList();
|
|
119 fConfiguredAnnotationTypes.add(annotationType);
|
|
120 } else if (!fConfiguredAnnotationTypes.contains(annotationType))
|
|
121 fConfiguredAnnotationTypes.add(annotationType);
|
|
122 }
|
|
123 }
|
|
124
|
|
125 /**
|
|
126 * Removes the given annotation. Annotation of that type are no
|
|
127 * longer reflected in their enclosing collapsed region.
|
|
128 *
|
|
129 * @param annotationType the annotation type to remove
|
|
130 */
|
|
131 public void removeAnnotationType(String annotationType) {
|
|
132 synchronized (fLock) {
|
|
133 if (fConfiguredAnnotationTypes !is null) {
|
|
134 fConfiguredAnnotationTypes.remove(annotationType);
|
|
135 if (fConfiguredAnnotationTypes.size() is 0)
|
|
136 fConfiguredAnnotationTypes= null;
|
|
137 }
|
|
138 }
|
|
139 }
|
|
140
|
|
141 /**
|
|
142 * Forces an updated of the annotation summary.
|
|
143 */
|
|
144 public void updateSummaries() {
|
|
145 synchronized (fLock) {
|
|
146 if (fConfiguredAnnotationTypes !is null) {
|
|
147 if (fSummarizer is null)
|
|
148 fSummarizer= new Summarizer();
|
|
149 fSummarizer.reset();
|
|
150 }
|
|
151 }
|
|
152 }
|
|
153
|
|
154 private void internalUpdateSummaries(IProgressMonitor monitor) {
|
|
155
|
|
156 Object previousLockObject= null;
|
|
157 fAnnotationModel= fProjectionViewer.getVisualAnnotationModel();
|
|
158 if (fAnnotationModel is null)
|
|
159 return;
|
|
160
|
|
161 try {
|
|
162
|
|
163
|
|
164 IDocument document= fProjectionViewer.getDocument();
|
|
165 if (document instanceof ISynchronizable && fAnnotationModel instanceof ISynchronizable) {
|
|
166 ISynchronizable sync= (ISynchronizable) fAnnotationModel;
|
|
167 previousLockObject= sync.getLockObject();
|
|
168 sync.setLockObject(((ISynchronizable) document).getLockObject());
|
|
169 }
|
|
170
|
|
171
|
|
172 removeSummaries(monitor);
|
|
173
|
|
174 if (isCanceled(monitor))
|
|
175 return;
|
|
176
|
|
177 createSummaries(monitor);
|
|
178
|
|
179 } finally {
|
|
180
|
|
181 if (fAnnotationModel instanceof ISynchronizable) {
|
|
182 ISynchronizable sync= (ISynchronizable) fAnnotationModel;
|
|
183 sync.setLockObject(previousLockObject);
|
|
184 }
|
|
185 fAnnotationModel= null;
|
|
186
|
|
187 }
|
|
188 }
|
|
189
|
|
190 private bool isCanceled(IProgressMonitor monitor) {
|
|
191 return monitor !is null && monitor.isCanceled();
|
|
192 }
|
|
193
|
|
194 private void removeSummaries(IProgressMonitor monitor) {
|
|
195 IAnnotationModelExtension extension= null;
|
|
196 List bags= null;
|
|
197
|
|
198 if (fAnnotationModel instanceof IAnnotationModelExtension) {
|
|
199 extension= (IAnnotationModelExtension) fAnnotationModel;
|
|
200 bags= new ArrayList();
|
|
201 }
|
|
202
|
|
203 Iterator e= fAnnotationModel.getAnnotationIterator();
|
|
204 while (e.hasNext()) {
|
|
205 Annotation annotation= (Annotation) e.next();
|
|
206 if (annotation instanceof AnnotationBag) {
|
|
207 if (bags is null)
|
|
208 fAnnotationModel.removeAnnotation(annotation);
|
|
209 else
|
|
210 bags.add(annotation);
|
|
211 }
|
|
212
|
|
213 if (isCanceled(monitor))
|
|
214 return;
|
|
215 }
|
|
216
|
|
217 if (bags !is null && bags.size() > 0) {
|
|
218 Annotation[] deletions= new Annotation[bags.size()];
|
|
219 bags.toArray(deletions);
|
|
220 if (!isCanceled(monitor))
|
|
221 extension.replaceAnnotations(deletions, null);
|
|
222 }
|
|
223 }
|
|
224
|
|
225 private void createSummaries(IProgressMonitor monitor) {
|
|
226 ProjectionAnnotationModel model= fProjectionViewer.getProjectionAnnotationModel();
|
|
227 if (model is null)
|
|
228 return;
|
|
229
|
|
230 Map additions= new HashMap();
|
|
231
|
|
232 Iterator e= model.getAnnotationIterator();
|
|
233 while (e.hasNext()) {
|
|
234 ProjectionAnnotation projection= (ProjectionAnnotation) e.next();
|
|
235 if (projection.isCollapsed()) {
|
|
236 Position position= model.getPosition(projection);
|
|
237 if (position !is null) {
|
|
238 IRegion[] summaryRegions= fProjectionViewer.computeCollapsedRegions(position);
|
|
239 if (summaryRegions !is null) {
|
|
240 Position summaryAnchor= fProjectionViewer.computeCollapsedRegionAnchor(position);
|
|
241 if (summaryAnchor !is null)
|
|
242 createSummary(additions, summaryRegions, summaryAnchor);
|
|
243 }
|
|
244 }
|
|
245 }
|
|
246
|
|
247 if (isCanceled(monitor))
|
|
248 return;
|
|
249 }
|
|
250
|
|
251 if (additions.size() > 0) {
|
|
252 if (fAnnotationModel instanceof IAnnotationModelExtension) {
|
|
253 IAnnotationModelExtension extension= (IAnnotationModelExtension) fAnnotationModel;
|
|
254 if (!isCanceled(monitor))
|
|
255 extension.replaceAnnotations(null, additions);
|
|
256 } else {
|
|
257 Iterator e1= additions.keySet().iterator();
|
|
258 while (e1.hasNext()) {
|
|
259 AnnotationBag bag= (AnnotationBag) e1.next();
|
|
260 Position position= (Position) additions.get(bag);
|
|
261 if (isCanceled(monitor))
|
|
262 return;
|
|
263 fAnnotationModel.addAnnotation(bag, position);
|
|
264 }
|
|
265 }
|
|
266 }
|
|
267 }
|
|
268
|
|
269 private void createSummary(Map additions, IRegion[] summaryRegions, Position summaryAnchor) {
|
|
270
|
|
271 int size= 0;
|
|
272 Map map= null;
|
|
273
|
|
274 synchronized (fLock) {
|
|
275 if (fConfiguredAnnotationTypes !is null) {
|
|
276 size= fConfiguredAnnotationTypes.size();
|
|
277 map= new HashMap();
|
|
278 for (int i= 0; i < size; i++) {
|
|
279 String type= (String) fConfiguredAnnotationTypes.get(i);
|
|
280 map.put(type, new AnnotationBag(type));
|
|
281 }
|
|
282 }
|
|
283 }
|
|
284
|
|
285 if (map is null)
|
|
286 return;
|
|
287
|
|
288 IAnnotationModel model= fProjectionViewer.getAnnotationModel();
|
|
289 if (model is null)
|
|
290 return;
|
|
291 Iterator e= model.getAnnotationIterator();
|
|
292 while (e.hasNext()) {
|
|
293 Annotation annotation= (Annotation) e.next();
|
|
294 AnnotationBag bag= findBagForType(map, annotation.getType());
|
|
295 if (bag !is null) {
|
|
296 Position position= model.getPosition(annotation);
|
|
297 if (includes(summaryRegions, position))
|
|
298 bag.add(annotation);
|
|
299 }
|
|
300 }
|
|
301
|
|
302 for (int i= 0; i < size; i++) {
|
|
303 AnnotationBag bag= (AnnotationBag) map.get(fConfiguredAnnotationTypes.get(i));
|
|
304 if (!bag.isEmpty())
|
|
305 additions.put(bag, new Position(summaryAnchor.getOffset(), summaryAnchor.getLength()));
|
|
306 }
|
|
307 }
|
|
308
|
|
309 private AnnotationBag findBagForType(Map bagMap, String annotationType) {
|
|
310 AnnotationBag bag= (AnnotationBag) bagMap.get(annotationType);
|
|
311 if (bag is null && fAnnotationAccess instanceof IAnnotationAccessExtension) {
|
|
312 IAnnotationAccessExtension extension= (IAnnotationAccessExtension) fAnnotationAccess;
|
|
313 Object[] superTypes= extension.getSupertypes(annotationType);
|
|
314 for (int i= 0; i < superTypes.length && bag is null; i++) {
|
|
315 bag= (AnnotationBag) bagMap.get(superTypes[i]);
|
|
316 }
|
|
317 }
|
|
318 return bag;
|
|
319 }
|
|
320
|
|
321 private bool includes(IRegion[] regions, Position position) {
|
|
322 for (int i= 0; i < regions.length; i++) {
|
|
323 IRegion region= regions[i];
|
|
324 if (position !is null && !position.isDeleted()
|
|
325 && region.getOffset() <= position.getOffset() && position.getOffset() + position.getLength() <= region.getOffset() + region.getLength())
|
|
326 return true;
|
|
327 }
|
|
328 return false;
|
|
329 }
|
|
330 }
|