Mercurial > projects > dwt-addons
annotate dwtx/jface/text/source/projection/ProjectionSupport.d @ 162:1a5b8f8129df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 08 Sep 2008 00:51:37 +0200 |
parents | 25f1f92fa3df |
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.ProjectionSupport; | |
14 | |
131 | 15 import dwtx.jface.text.source.projection.ProjectionViewer; // packageimport |
16 import dwtx.jface.text.source.projection.IProjectionPosition; // packageimport | |
17 import dwtx.jface.text.source.projection.AnnotationBag; // packageimport | |
18 import dwtx.jface.text.source.projection.ProjectionSummary; // 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; |
28 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
146
diff
changeset
|
29 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
146
diff
changeset
|
30 |
129 | 31 |
32 import dwt.DWT; | |
33 import dwt.custom.StyledText; | |
34 import dwt.custom.StyledTextContent; | |
35 import dwt.graphics.Color; | |
36 import dwt.graphics.FontMetrics; | |
37 import dwt.graphics.GC; | |
38 import dwt.graphics.Point; | |
39 import dwt.graphics.RGB; | |
40 import dwt.widgets.Display; | |
41 import dwtx.jface.text.IInformationControlCreator; | |
42 import dwtx.jface.text.source.Annotation; | |
43 import dwtx.jface.text.source.AnnotationPainter; | |
44 import dwtx.jface.text.source.IAnnotationAccess; | |
45 import dwtx.jface.text.source.IAnnotationHover; | |
46 import dwtx.jface.text.source.IAnnotationModel; | |
47 import dwtx.jface.text.source.ISharedTextColors; | |
48 import dwtx.jface.text.source.ISourceViewer; | |
49 | |
50 /** | |
51 * Supports the configuration of projection capabilities a {@link dwtx.jface.text.source.projection.ProjectionViewer}. | |
52 * <p> | |
53 * This class is not intended to be subclassed. Clients are supposed to configure and use it as is.</p> | |
54 * | |
55 * @since 3.0 | |
56 * @noextend This class is not intended to be subclassed by clients. | |
57 */ | |
58 public class ProjectionSupport { | |
59 | |
60 /** | |
61 * Key of the projection annotation model inside the visual annotation | |
62 * model. Also internally used as key for the projection drawing strategy. | |
63 */ | |
162 | 64 private static Object PROJECTION_; |
65 public static Object PROJECTION(){ | |
66 if( PROJECTION_ is null ){ | |
67 synchronized(ProjectionSupport.classinfo){ | |
68 if( PROJECTION_ is null ){ | |
69 PROJECTION_ = new Object(); | |
70 } | |
71 } | |
72 } | |
73 return PROJECTION_; | |
74 } | |
129 | 75 |
76 private static class ProjectionAnnotationsPainter : AnnotationPainter { | |
77 | |
78 /** | |
79 * Creates a new painter indicating the location of collapsed regions. | |
80 * | |
81 * @param sourceViewer the source viewer for the painter | |
82 * @param access the annotation access | |
83 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
84 public this(ISourceViewer sourceViewer, IAnnotationAccess access) { |
129 | 85 super(sourceViewer, access); |
86 } | |
87 | |
88 /* | |
89 * @see dwtx.jface.text.source.AnnotationPainter#findAnnotationModel(dwtx.jface.text.source.ISourceViewer) | |
90 */ | |
91 protected IAnnotationModel findAnnotationModel(ISourceViewer sourceViewer) { | |
138 | 92 if ( cast(ProjectionViewer)sourceViewer ) { |
134 | 93 ProjectionViewer projectionViewer= cast(ProjectionViewer) sourceViewer; |
129 | 94 return projectionViewer.getProjectionAnnotationModel(); |
95 } | |
96 return null; | |
97 } | |
98 | |
99 /* | |
100 * @see dwtx.jface.text.source.AnnotationPainter#skip(dwtx.jface.text.source.Annotation) | |
101 */ | |
102 protected bool skip(Annotation annotation) { | |
138 | 103 if ( cast(ProjectionAnnotation)annotation ) |
134 | 104 return !(cast(ProjectionAnnotation) annotation).isCollapsed(); |
129 | 105 |
106 return super.skip(annotation); | |
107 } | |
108 } | |
109 | |
158 | 110 private static class ProjectionDrawingStrategy : AnnotationPainter_IDrawingStrategy { |
129 | 111 /* |
112 * @see dwtx.jface.text.source.AnnotationPainter.IDrawingStrategy#draw(dwt.graphics.GC, dwt.custom.StyledText, int, int, dwt.graphics.Color) | |
113 */ | |
114 public void draw(Annotation annotation, GC gc, StyledText textWidget, int offset, int length, Color color) { | |
138 | 115 if ( cast(ProjectionAnnotation)annotation ) { |
134 | 116 ProjectionAnnotation projectionAnnotation= cast(ProjectionAnnotation) annotation; |
129 | 117 if (projectionAnnotation.isCollapsed()) { |
118 | |
119 if (gc !is null) { | |
120 | |
121 StyledTextContent content= textWidget.getContent(); | |
122 int line= content.getLineAtOffset(offset); | |
123 int lineStart= content.getOffsetAtLine(line); | |
124 String text= content.getLine(line); | |
162 | 125 int lineLength= text is null ? 0 : text.length; |
129 | 126 int lineEnd= lineStart + lineLength; |
127 Point p= textWidget.getLocationAtOffset(lineEnd); | |
128 | |
129 Color c= gc.getForeground(); | |
130 gc.setForeground(color); | |
131 | |
132 FontMetrics metrics= gc.getFontMetrics(); | |
133 | |
134 // baseline: where the dots are drawn | |
135 int baseline= textWidget.getBaseline(offset); | |
136 // descent: number of pixels that the box extends over baseline | |
137 int descent= Math.min(2, textWidget.getLineHeight(offset) - baseline); | |
138 // ascent: so much does the box stand up from baseline | |
139 int ascent= metrics.getAscent(); | |
140 // leading: free space from line top to box upper line | |
141 int leading= baseline - ascent; | |
142 // height: height of the box | |
143 int height= ascent + descent; | |
144 | |
145 int width= metrics.getAverageCharWidth(); | |
146 gc.drawRectangle(p.x, p.y + leading, width, height); | |
147 int third= width/3; | |
148 int dotsVertical= p.y + baseline - 1; | |
149 gc.drawPoint(p.x + third, dotsVertical); | |
150 gc.drawPoint(p.x + width - third, dotsVertical); | |
151 | |
152 gc.setForeground(c); | |
153 | |
154 } else { | |
155 textWidget.redrawRange(offset, length, true); | |
156 } | |
157 } | |
158 } | |
159 } | |
160 } | |
161 | |
162 private class ProjectionListener : IProjectionListener { | |
163 | |
164 /* | |
165 * @see dwtx.jface.text.source.projection.IProjectionListener#projectionEnabled() | |
166 */ | |
167 public void projectionEnabled() { | |
168 doEnableProjection(); | |
169 } | |
170 | |
171 /* | |
172 * @see dwtx.jface.text.source.projection.IProjectionListener#projectionDisabled() | |
173 */ | |
174 public void projectionDisabled() { | |
175 doDisableProjection(); | |
176 } | |
177 } | |
178 | |
179 private ProjectionViewer fViewer; | |
180 private IAnnotationAccess fAnnotationAccess; | |
181 private ISharedTextColors fSharedTextColors; | |
182 private List fSummarizableTypes; | |
183 private IInformationControlCreator fInformationControlCreator; | |
184 private IInformationControlCreator fInformationPresenterControlCreator; | |
185 private ProjectionListener fProjectionListener; | |
186 private ProjectionAnnotationsPainter fPainter; | |
187 private ProjectionRulerColumn fColumn; | |
188 /** | |
189 * @since 3.1 | |
190 */ | |
158 | 191 private AnnotationPainter_IDrawingStrategy fDrawingStrategy; |
129 | 192 |
193 /** | |
194 * Creates new projection support for the given projection viewer. Initially, | |
195 * no annotation types are summarized. A default hover control creator and a | |
196 * default drawing strategy are used. | |
197 * | |
198 * @param viewer the projection viewer | |
199 * @param annotationAccess the annotation access | |
200 * @param sharedTextColors the shared text colors to use | |
201 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
202 public this(ProjectionViewer viewer, IAnnotationAccess annotationAccess, ISharedTextColors sharedTextColors) { |
129 | 203 fViewer= viewer; |
204 fAnnotationAccess= annotationAccess; | |
205 fSharedTextColors= sharedTextColors; | |
206 } | |
207 | |
208 /** | |
209 * Marks the given annotation type to be considered when creating summaries for | |
210 * collapsed regions of the projection viewer. | |
211 * <p> | |
212 * A summary is an annotation that gets created out of all annotations with a | |
213 * type that has been registered through this method and that are inside the | |
214 * folded region. | |
215 * </p> | |
216 * | |
217 * @param annotationType the annotation type to consider | |
218 */ | |
219 public void addSummarizableAnnotationType(String annotationType) { | |
220 if (fSummarizableTypes is null) { | |
221 fSummarizableTypes= new ArrayList(); | |
222 fSummarizableTypes.add(annotationType); | |
223 } else if (!fSummarizableTypes.contains(annotationType)) | |
224 fSummarizableTypes.add(annotationType); | |
225 } | |
226 | |
227 /** | |
228 * Marks the given annotation type to be ignored when creating summaries for | |
229 * collapsed regions of the projection viewer. This method has only an effect | |
230 * when <code>addSummarizableAnnotationType</code> has been called before for | |
231 * the give annotation type. | |
232 * <p> | |
233 * A summary is an annotation that gets created out of all annotations with a | |
234 * type that has been registered through this method and that are inside the | |
235 * folded region. | |
236 * </p> | |
237 * | |
238 * @param annotationType the annotation type to remove | |
239 */ | |
240 public void removeSummarizableAnnotationType(String annotationType) { | |
241 if (fSummarizableTypes !is null) | |
242 fSummarizableTypes.remove(annotationType); | |
243 if (fSummarizableTypes.size() is 0) | |
244 fSummarizableTypes= null; | |
245 } | |
246 | |
247 /** | |
248 * Sets the hover control creator that is used for the annotation hovers | |
249 * that are shown in the projection viewer's projection ruler column. | |
250 * | |
251 * @param creator the hover control creator | |
252 */ | |
253 public void setHoverControlCreator(IInformationControlCreator creator) { | |
254 fInformationControlCreator= creator; | |
255 } | |
145 | 256 |
129 | 257 /** |
258 * Sets the information presenter control creator that is used for the annotation | |
259 * hovers that are shown in the projection viewer's projection ruler column. | |
260 * | |
261 * @param creator the information presenter control creator | |
262 * @since 3.3 | |
263 */ | |
264 public void setInformationPresenterControlCreator(IInformationControlCreator creator) { | |
265 fInformationPresenterControlCreator= creator; | |
266 } | |
267 | |
268 /** | |
269 * Sets the drawing strategy that the projection support's annotation | |
270 * painter uses to draw the indication of collapsed regions onto the | |
271 * projection viewer's text widget. When <code>null</code> is passed in, | |
272 * the drawing strategy is reset to the default. In order to avoid any | |
273 * representation use {@link dwtx.jface.text.source.AnnotationPainter.NullStrategy}. | |
274 * | |
275 * @param strategy the drawing strategy or <code>null</code> to reset the | |
276 * strategy to the default | |
277 * @since 3.1 | |
278 */ | |
158 | 279 public void setAnnotationPainterDrawingStrategy(AnnotationPainter_IDrawingStrategy strategy) { |
129 | 280 fDrawingStrategy= strategy; |
281 } | |
282 | |
283 /** | |
284 * Returns the drawing strategy to be used by the support's annotation painter. | |
285 * | |
286 * @return the drawing strategy to be used by the support's annotation painter | |
287 * @since 3.1 | |
288 */ | |
158 | 289 private AnnotationPainter_IDrawingStrategy getDrawingStrategy() { |
129 | 290 if (fDrawingStrategy is null) |
291 fDrawingStrategy= new ProjectionDrawingStrategy(); | |
292 return fDrawingStrategy; | |
293 } | |
294 | |
295 /** | |
296 * Installs this projection support on its viewer. | |
297 */ | |
298 public void install() { | |
299 fViewer.setProjectionSummary(createProjectionSummary()); | |
300 | |
301 fProjectionListener= new ProjectionListener(); | |
302 fViewer.addProjectionListener(fProjectionListener); | |
303 } | |
304 | |
305 /** | |
306 * Disposes this projection support. | |
307 */ | |
308 public void dispose() { | |
309 if (fProjectionListener !is null) { | |
310 fViewer.removeProjectionListener(fProjectionListener); | |
311 fProjectionListener= null; | |
312 } | |
313 } | |
314 | |
315 /** | |
316 * Enables projection mode. If not yet done, installs the projection ruler | |
317 * column in the viewer's vertical ruler and installs a painter that | |
318 * indicate the locations of collapsed regions. | |
319 * | |
320 */ | |
321 protected void doEnableProjection() { | |
322 | |
323 if (fPainter is null) { | |
324 fPainter= new ProjectionAnnotationsPainter(fViewer, fAnnotationAccess); | |
325 fPainter.addDrawingStrategy(PROJECTION, getDrawingStrategy()); | |
162 | 326 fPainter.addAnnotationType(stringcast(ProjectionAnnotation.TYPE), PROJECTION); |
327 fPainter.setAnnotationTypeColor(stringcast(ProjectionAnnotation.TYPE), fSharedTextColors.getColor(getColor())); | |
129 | 328 fViewer.addPainter(fPainter); |
329 } | |
330 | |
331 if (fColumn is null) { | |
332 fColumn= new ProjectionRulerColumn(9, fAnnotationAccess); | |
162 | 333 fColumn.addAnnotationType(stringcast(ProjectionAnnotation.TYPE)); |
129 | 334 fColumn.setHover(createProjectionAnnotationHover()); |
335 fViewer.addVerticalRulerColumn(fColumn); | |
336 } | |
337 | |
338 fColumn.setModel(fViewer.getVisualAnnotationModel()); | |
339 } | |
340 | |
341 /** | |
342 * Removes the projection ruler column and the painter from the projection | |
343 * viewer. | |
344 */ | |
345 protected void doDisableProjection() { | |
346 if (fPainter !is null) { | |
347 fViewer.removePainter(fPainter); | |
348 fPainter.dispose(); | |
349 fPainter= null; | |
350 } | |
351 | |
352 if (fColumn !is null) { | |
353 fViewer.removeVerticalRulerColumn(fColumn); | |
354 fColumn= null; | |
355 } | |
356 } | |
357 | |
358 private ProjectionSummary createProjectionSummary() { | |
359 ProjectionSummary summary= new ProjectionSummary(fViewer, fAnnotationAccess); | |
360 if (fSummarizableTypes !is null) { | |
361 int size= fSummarizableTypes.size(); | |
362 for (int i= 0; i < size; i++) | |
162 | 363 summary.addAnnotationType(stringcast(fSummarizableTypes.get(i))); |
129 | 364 } |
365 return summary; | |
366 } | |
367 | |
368 private IAnnotationHover createProjectionAnnotationHover() { | |
369 ProjectionAnnotationHover hover= new ProjectionAnnotationHover(); | |
370 hover.setHoverControlCreator(fInformationControlCreator); | |
371 hover.setInformationPresenterControlCreator(fInformationPresenterControlCreator); | |
372 return hover; | |
373 } | |
374 | |
375 /** | |
376 * Implements the contract of {@link dwtx.core.runtime.IAdaptable#getAdapter(java.lang.Class)} | |
377 * by forwarding the adapter requests to the given viewer. | |
378 * | |
379 * @param viewer the viewer | |
380 * @param required the required class of the adapter | |
381 * @return the adapter or <code>null</code> | |
382 * | |
383 */ | |
158 | 384 public Object getAdapter(ISourceViewer viewer, ClassInfo required) { |
145 | 385 if (ProjectionAnnotationModel.classinfo ==/*eq*/ required) { |
138 | 386 if ( cast(ProjectionViewer)viewer ) { |
134 | 387 ProjectionViewer projectionViewer= cast(ProjectionViewer) viewer; |
129 | 388 return projectionViewer.getProjectionAnnotationModel(); |
389 } | |
390 } | |
391 return null; | |
392 } | |
393 | |
394 private RGB getColor() { | |
395 // TODO read out preference settings | |
396 Color c= Display.getDefault().getSystemColor(DWT.COLOR_DARK_GRAY); | |
397 return c.getRGB(); | |
398 } | |
399 } |