comparison org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionSupport.d @ 12:bc29606a740c

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