Mercurial > projects > dwt-addons
annotate dwtx/jface/text/source/projection/ProjectionSummary.d @ 172:d994a8b2cdf7
again compile fixes
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 10 Sep 2008 23:14:02 +0200 |
parents | 862b05e0334a |
children |
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; |
167 | 28 import dwtx.dwtxhelper.JThread; |
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 |
167 | 58 JThread 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() { |
172 | 65 thread = new JThread( &run ); |
129 | 66 fProgressMonitor= new NullProgressMonitor(); // might be given by client in the future |
172 | 67 thread.setDaemon(true); |
158 | 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 | |
162 | 107 private Object fLock; |
129 | 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) { |
162 | 118 // super(); |
119 fLock= new Object(); | |
120 | |
129 | 121 fProjectionViewer= projectionViewer; |
122 fAnnotationAccess= annotationAccess; | |
123 } | |
124 | |
125 /** | |
126 * Adds the given annotation type. For now on, annotations of that type are | |
127 * also reflected in their enclosing collapsed regions. | |
128 * | |
129 * @param annotationType the annotation type to add | |
130 */ | |
131 public void addAnnotationType(String annotationType) { | |
132 synchronized(fLock) { | |
133 if (fConfiguredAnnotationTypes is null) { | |
134 fConfiguredAnnotationTypes= new ArrayList(); | |
135 fConfiguredAnnotationTypes.add(annotationType); | |
136 } else if (!fConfiguredAnnotationTypes.contains(annotationType)) | |
137 fConfiguredAnnotationTypes.add(annotationType); | |
138 } | |
139 } | |
140 | |
141 /** | |
142 * Removes the given annotation. Annotation of that type are no | |
143 * longer reflected in their enclosing collapsed region. | |
144 * | |
145 * @param annotationType the annotation type to remove | |
146 */ | |
147 public void removeAnnotationType(String annotationType) { | |
148 synchronized (fLock) { | |
149 if (fConfiguredAnnotationTypes !is null) { | |
150 fConfiguredAnnotationTypes.remove(annotationType); | |
151 if (fConfiguredAnnotationTypes.size() is 0) | |
152 fConfiguredAnnotationTypes= null; | |
153 } | |
154 } | |
155 } | |
156 | |
157 /** | |
158 * Forces an updated of the annotation summary. | |
159 */ | |
160 public void updateSummaries() { | |
161 synchronized (fLock) { | |
162 if (fConfiguredAnnotationTypes !is null) { | |
163 if (fSummarizer is null) | |
164 fSummarizer= new Summarizer(); | |
165 fSummarizer.reset(); | |
166 } | |
167 } | |
168 } | |
169 | |
170 private void internalUpdateSummaries(IProgressMonitor monitor) { | |
171 | |
172 Object previousLockObject= null; | |
173 fAnnotationModel= fProjectionViewer.getVisualAnnotationModel(); | |
174 if (fAnnotationModel is null) | |
175 return; | |
176 | |
177 try { | |
178 | |
179 | |
180 IDocument document= fProjectionViewer.getDocument(); | |
143 | 181 if ( cast(ISynchronizable)document && cast(ISynchronizable)fAnnotationModel ) { |
134 | 182 ISynchronizable sync= cast(ISynchronizable) fAnnotationModel; |
129 | 183 previousLockObject= sync.getLockObject(); |
134 | 184 sync.setLockObject((cast(ISynchronizable) document).getLockObject()); |
129 | 185 } |
186 | |
187 | |
188 removeSummaries(monitor); | |
158 | 189 |
129 | 190 if (isCanceled(monitor)) |
191 return; | |
192 | |
193 createSummaries(monitor); | |
194 | |
195 } finally { | |
196 | |
138 | 197 if ( cast(ISynchronizable)fAnnotationModel ) { |
134 | 198 ISynchronizable sync= cast(ISynchronizable) fAnnotationModel; |
129 | 199 sync.setLockObject(previousLockObject); |
200 } | |
201 fAnnotationModel= null; | |
202 | |
203 } | |
204 } | |
205 | |
206 private bool isCanceled(IProgressMonitor monitor) { | |
207 return monitor !is null && monitor.isCanceled(); | |
208 } | |
209 | |
210 private void removeSummaries(IProgressMonitor monitor) { | |
211 IAnnotationModelExtension extension= null; | |
212 List bags= null; | |
213 | |
138 | 214 if ( cast(IAnnotationModelExtension)fAnnotationModel ) { |
134 | 215 extension= cast(IAnnotationModelExtension) fAnnotationModel; |
129 | 216 bags= new ArrayList(); |
217 } | |
218 | |
219 Iterator e= fAnnotationModel.getAnnotationIterator(); | |
220 while (e.hasNext()) { | |
134 | 221 Annotation annotation= cast(Annotation) e.next(); |
138 | 222 if ( cast(AnnotationBag)annotation ) { |
129 | 223 if (bags is null) |
224 fAnnotationModel.removeAnnotation(annotation); | |
225 else | |
226 bags.add(annotation); | |
227 } | |
228 | |
229 if (isCanceled(monitor)) | |
230 return; | |
231 } | |
232 | |
233 if (bags !is null && bags.size() > 0) { | |
234 Annotation[] deletions= new Annotation[bags.size()]; | |
235 bags.toArray(deletions); | |
236 if (!isCanceled(monitor)) | |
237 extension.replaceAnnotations(deletions, null); | |
238 } | |
239 } | |
240 | |
241 private void createSummaries(IProgressMonitor monitor) { | |
242 ProjectionAnnotationModel model= fProjectionViewer.getProjectionAnnotationModel(); | |
243 if (model is null) | |
244 return; | |
245 | |
246 Map additions= new HashMap(); | |
247 | |
248 Iterator e= model.getAnnotationIterator(); | |
249 while (e.hasNext()) { | |
134 | 250 ProjectionAnnotation projection= cast(ProjectionAnnotation) e.next(); |
129 | 251 if (projection.isCollapsed()) { |
252 Position position= model.getPosition(projection); | |
253 if (position !is null) { | |
254 IRegion[] summaryRegions= fProjectionViewer.computeCollapsedRegions(position); | |
255 if (summaryRegions !is null) { | |
256 Position summaryAnchor= fProjectionViewer.computeCollapsedRegionAnchor(position); | |
257 if (summaryAnchor !is null) | |
258 createSummary(additions, summaryRegions, summaryAnchor); | |
259 } | |
260 } | |
261 } | |
262 | |
263 if (isCanceled(monitor)) | |
264 return; | |
265 } | |
266 | |
267 if (additions.size() > 0) { | |
138 | 268 if ( cast(IAnnotationModelExtension)fAnnotationModel ) { |
134 | 269 IAnnotationModelExtension extension= cast(IAnnotationModelExtension) fAnnotationModel; |
129 | 270 if (!isCanceled(monitor)) |
271 extension.replaceAnnotations(null, additions); | |
272 } else { | |
273 Iterator e1= additions.keySet().iterator(); | |
274 while (e1.hasNext()) { | |
134 | 275 AnnotationBag bag= cast(AnnotationBag) e1.next(); |
276 Position position= cast(Position) additions.get(bag); | |
129 | 277 if (isCanceled(monitor)) |
278 return; | |
279 fAnnotationModel.addAnnotation(bag, position); | |
280 } | |
281 } | |
282 } | |
283 } | |
284 | |
285 private void createSummary(Map additions, IRegion[] summaryRegions, Position summaryAnchor) { | |
286 | |
287 int size= 0; | |
288 Map map= null; | |
289 | |
290 synchronized (fLock) { | |
291 if (fConfiguredAnnotationTypes !is null) { | |
292 size= fConfiguredAnnotationTypes.size(); | |
293 map= new HashMap(); | |
294 for (int i= 0; i < size; i++) { | |
162 | 295 String type= stringcast( fConfiguredAnnotationTypes.get(i)); |
129 | 296 map.put(type, new AnnotationBag(type)); |
297 } | |
298 } | |
299 } | |
300 | |
301 if (map is null) | |
302 return; | |
303 | |
304 IAnnotationModel model= fProjectionViewer.getAnnotationModel(); | |
305 if (model is null) | |
306 return; | |
307 Iterator e= model.getAnnotationIterator(); | |
308 while (e.hasNext()) { | |
134 | 309 Annotation annotation= cast(Annotation) e.next(); |
129 | 310 AnnotationBag bag= findBagForType(map, annotation.getType()); |
311 if (bag !is null) { | |
312 Position position= model.getPosition(annotation); | |
313 if (includes(summaryRegions, position)) | |
314 bag.add(annotation); | |
315 } | |
316 } | |
317 | |
318 for (int i= 0; i < size; i++) { | |
134 | 319 AnnotationBag bag= cast(AnnotationBag) map.get(fConfiguredAnnotationTypes.get(i)); |
129 | 320 if (!bag.isEmpty()) |
321 additions.put(bag, new Position(summaryAnchor.getOffset(), summaryAnchor.getLength())); | |
322 } | |
323 } | |
324 | |
325 private AnnotationBag findBagForType(Map bagMap, String annotationType) { | |
134 | 326 AnnotationBag bag= cast(AnnotationBag) bagMap.get(annotationType); |
143 | 327 if (bag is null && cast(IAnnotationAccessExtension)fAnnotationAccess ) { |
134 | 328 IAnnotationAccessExtension extension= cast(IAnnotationAccessExtension) fAnnotationAccess; |
162 | 329 Object[] superTypes= extension.getSupertypes(stringcast(annotationType)); |
129 | 330 for (int i= 0; i < superTypes.length && bag is null; i++) { |
134 | 331 bag= cast(AnnotationBag) bagMap.get(superTypes[i]); |
129 | 332 } |
333 } | |
334 return bag; | |
335 } | |
336 | |
337 private bool includes(IRegion[] regions, Position position) { | |
338 for (int i= 0; i < regions.length; i++) { | |
339 IRegion region= regions[i]; | |
340 if (position !is null && !position.isDeleted() | |
341 && region.getOffset() <= position.getOffset() && position.getOffset() + position.getLength() <= region.getOffset() + region.getLength()) | |
342 return true; | |
343 } | |
344 return false; | |
345 } | |
346 } |