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