129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 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.ProjectionAnnotation;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18
|
|
19
|
|
20 import dwt.DWT;
|
|
21 import dwt.graphics.FontMetrics;
|
|
22 import dwt.graphics.GC;
|
|
23 import dwt.graphics.Image;
|
|
24 import dwt.graphics.Rectangle;
|
|
25 import dwt.widgets.Canvas;
|
|
26 import dwt.widgets.Display;
|
|
27 import dwtx.jface.resource.ImageDescriptor;
|
|
28 import dwtx.jface.text.source.Annotation;
|
|
29 import dwtx.jface.text.source.IAnnotationPresentation;
|
|
30 import dwtx.jface.text.source.ImageUtilities;
|
|
31
|
|
32 /**
|
|
33 * Annotation used to represent the projection of a master document onto a
|
|
34 * {@link dwtx.jface.text.projection.ProjectionDocument}. A projection
|
|
35 * annotation can be either expanded or collapsed. If expanded it corresponds to
|
|
36 * a segment of the projection document. If collapsed, it represents a region of
|
|
37 * the master document that does not have a corresponding segment in the
|
|
38 * projection document.
|
|
39 * <p>
|
|
40 * Clients may subclass or use as is.
|
|
41 * </p>
|
|
42 *
|
|
43 * @since 3.0
|
|
44 */
|
|
45 public class ProjectionAnnotation : Annotation , IAnnotationPresentation {
|
|
46
|
|
47 private static class DisplayDisposeRunnable : Runnable {
|
|
48
|
|
49 public void run() {
|
|
50 if (fgCollapsedImage !is null) {
|
|
51 fgCollapsedImage.dispose();
|
|
52 fgCollapsedImage= null;
|
|
53 }
|
|
54 if (fgExpandedImage !is null) {
|
|
55 fgExpandedImage.dispose();
|
|
56 fgExpandedImage= null;
|
|
57 }
|
|
58 }
|
|
59 }
|
|
60
|
|
61 /**
|
|
62 * The type of projection annotations.
|
|
63 */
|
|
64 public static final String TYPE= "dwtx.projection"; //$NON-NLS-1$
|
|
65
|
|
66
|
|
67 private static final int COLOR= DWT.COLOR_GRAY;
|
|
68 private static Image fgCollapsedImage;
|
|
69 private static Image fgExpandedImage;
|
|
70
|
|
71
|
|
72 /** The state of this annotation */
|
|
73 private bool fIsCollapsed= false;
|
|
74 /** Indicates whether this annotation should be painted as range */
|
|
75 private bool fIsRangeIndication= false;
|
|
76
|
|
77 /**
|
|
78 * Creates a new expanded projection annotation.
|
|
79 */
|
|
80 public ProjectionAnnotation() {
|
|
81 this(false);
|
|
82 }
|
|
83
|
|
84 /**
|
|
85 * Creates a new projection annotation. When <code>isCollapsed</code>
|
|
86 * is <code>true</code> the annotation is initially collapsed.
|
|
87 *
|
|
88 * @param isCollapsed <code>true</code> if the annotation should initially be collapsed, <code>false</code> otherwise
|
|
89 */
|
|
90 public ProjectionAnnotation(bool isCollapsed) {
|
|
91 super(TYPE, false, null);
|
|
92 fIsCollapsed= isCollapsed;
|
|
93 }
|
|
94
|
|
95 /**
|
|
96 * Enables and disables the range indication for this annotation.
|
|
97 *
|
|
98 * @param rangeIndication the enable state for the range indication
|
|
99 */
|
|
100 public void setRangeIndication(bool rangeIndication) {
|
|
101 fIsRangeIndication= rangeIndication;
|
|
102 }
|
|
103
|
|
104 private void drawRangeIndication(GC gc, Canvas canvas, Rectangle r) {
|
|
105 final int MARGIN= 3;
|
|
106
|
|
107 /* cap the height - at least on GTK, large numbers are converted to
|
|
108 * negatives at some point */
|
|
109 int height= Math.min(r.y + r.height - MARGIN, canvas.getSize().y);
|
|
110
|
|
111 gc.setForeground(canvas.getDisplay().getSystemColor(COLOR));
|
|
112 gc.setLineWidth(0); // NOTE: 0 means width is 1 but with optimized performance
|
|
113 gc.drawLine(r.x + 4, r.y + 12, r.x + 4, height);
|
|
114 gc.drawLine(r.x + 4, height, r.x + r.width - MARGIN, height);
|
|
115 }
|
|
116
|
|
117 /*
|
|
118 * @see dwtx.jface.text.source.IAnnotationPresentation#paint(dwt.graphics.GC, dwt.widgets.Canvas, dwt.graphics.Rectangle)
|
|
119 */
|
|
120 public void paint(GC gc, Canvas canvas, Rectangle rectangle) {
|
|
121 Image image= getImage(canvas.getDisplay());
|
|
122 if (image !is null) {
|
|
123 ImageUtilities.drawImage(image, gc, canvas, rectangle, DWT.CENTER, DWT.TOP);
|
|
124 if (fIsRangeIndication) {
|
|
125 FontMetrics fontMetrics= gc.getFontMetrics();
|
|
126 int delta= (fontMetrics.getHeight() - image.getBounds().height)/2;
|
|
127 rectangle.y += delta;
|
|
128 rectangle.height -= delta;
|
|
129 drawRangeIndication(gc, canvas, rectangle);
|
|
130 }
|
|
131 }
|
|
132 }
|
|
133
|
|
134 /*
|
|
135 * @see dwtx.jface.text.source.IAnnotationPresentation#getLayer()
|
|
136 */
|
|
137 public int getLayer() {
|
|
138 return IAnnotationPresentation.DEFAULT_LAYER;
|
|
139 }
|
|
140
|
|
141 private Image getImage(Display display) {
|
|
142 initializeImages(display);
|
|
143 return isCollapsed() ? fgCollapsedImage : fgExpandedImage;
|
|
144 }
|
|
145
|
|
146 private void initializeImages(Display display) {
|
|
147 if (fgCollapsedImage is null) {
|
|
148
|
|
149 ImageDescriptor descriptor= ImageDescriptor.createFromFile(ProjectionAnnotation.class, "images/collapsed.gif"); //$NON-NLS-1$
|
|
150 fgCollapsedImage= descriptor.createImage(display);
|
|
151 descriptor= ImageDescriptor.createFromFile(ProjectionAnnotation.class, "images/expanded.gif"); //$NON-NLS-1$
|
|
152 fgExpandedImage= descriptor.createImage(display);
|
|
153
|
|
154 display.disposeExec(new DisplayDisposeRunnable());
|
|
155 }
|
|
156 }
|
|
157
|
|
158 /**
|
|
159 * Returns the state of this annotation.
|
|
160 *
|
|
161 * @return <code>true</code> if collapsed
|
|
162 */
|
|
163 public bool isCollapsed() {
|
|
164 return fIsCollapsed;
|
|
165 }
|
|
166
|
|
167 /**
|
|
168 * Marks this annotation as being collapsed.
|
|
169 */
|
|
170 public void markCollapsed() {
|
|
171 fIsCollapsed= true;
|
|
172 }
|
|
173
|
|
174 /**
|
|
175 * Marks this annotation as being unfolded.
|
|
176 */
|
|
177 public void markExpanded() {
|
|
178 fIsCollapsed= false;
|
|
179 }
|
|
180 }
|