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.ProjectionRulerColumn;
|
|
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.ProjectionSummary; // packageimport
|
|
20 import dwtx.jface.text.source.projection.ProjectionAnnotationHover; // 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
|
|
29
|
|
30 import java.util.Iterator;
|
|
31
|
|
32 import dwt.DWT;
|
|
33 import dwt.events.MouseEvent;
|
|
34 import dwt.events.MouseMoveListener;
|
|
35 import dwt.events.MouseTrackAdapter;
|
|
36 import dwt.graphics.Color;
|
|
37 import dwt.widgets.Composite;
|
|
38 import dwt.widgets.Control;
|
|
39 import dwt.widgets.Display;
|
|
40 import dwtx.jface.text.BadLocationException;
|
|
41 import dwtx.jface.text.IDocument;
|
|
42 import dwtx.jface.text.Position;
|
|
43 import dwtx.jface.text.source.AnnotationRulerColumn;
|
|
44 import dwtx.jface.text.source.CompositeRuler;
|
|
45 import dwtx.jface.text.source.IAnnotationAccess;
|
|
46 import dwtx.jface.text.source.IAnnotationModel;
|
|
47 import dwtx.jface.text.source.IAnnotationModelExtension;
|
|
48
|
|
49
|
|
50 /**
|
|
51 * A ruler column for controlling the behavior of a
|
|
52 * {@link dwtx.jface.text.source.projection.ProjectionViewer}.
|
|
53 *
|
|
54 * @since 3.0
|
|
55 */
|
|
56 class ProjectionRulerColumn : AnnotationRulerColumn {
|
|
57
|
|
58 private ProjectionAnnotation fCurrentAnnotation;
|
|
59
|
|
60 /**
|
|
61 * Creates a new projection ruler column.
|
|
62 *
|
|
63 * @param model the column's annotation model
|
|
64 * @param width the width in pixels
|
|
65 * @param annotationAccess the annotation access
|
|
66 */
|
|
67 public ProjectionRulerColumn(IAnnotationModel model, int width, IAnnotationAccess annotationAccess) {
|
|
68 super(model, width, annotationAccess);
|
|
69 }
|
|
70
|
|
71 /**
|
|
72 * Creates a new projection ruler column.
|
|
73 *
|
|
74 * @param width the width in pixels
|
|
75 * @param annotationAccess the annotation access
|
|
76 */
|
|
77 public ProjectionRulerColumn(int width, IAnnotationAccess annotationAccess) {
|
|
78 super(width, annotationAccess);
|
|
79 }
|
|
80
|
|
81 /*
|
|
82 * @see dwtx.jface.text.source.AnnotationRulerColumn#mouseClicked(int)
|
|
83 */
|
|
84 protected void mouseClicked(int line) {
|
|
85 clearCurrentAnnotation();
|
|
86 ProjectionAnnotation annotation= findAnnotation(line, true);
|
|
87 if (annotation !is null) {
|
|
88 ProjectionAnnotationModel model= (ProjectionAnnotationModel) getModel();
|
|
89 model.toggleExpansionState(annotation);
|
|
90 }
|
|
91 }
|
|
92
|
|
93 /**
|
|
94 * Returns the projection annotation of the column's annotation
|
|
95 * model that contains the given line.
|
|
96 *
|
|
97 * @param line the line
|
|
98 * @param exact <code>true</code> if the annotation range must match exactly
|
|
99 * @return the projection annotation containing the given line
|
|
100 */
|
|
101 private ProjectionAnnotation findAnnotation(int line, bool exact) {
|
|
102
|
|
103 ProjectionAnnotation previousAnnotation= null;
|
|
104
|
|
105 IAnnotationModel model= getModel();
|
|
106 if (model !is null) {
|
|
107 IDocument document= getCachedTextViewer().getDocument();
|
|
108
|
|
109 int previousDistance= Integer.MAX_VALUE;
|
|
110
|
|
111 Iterator e= model.getAnnotationIterator();
|
|
112 while (e.hasNext()) {
|
|
113 Object next= e.next();
|
|
114 if (next instanceof ProjectionAnnotation) {
|
|
115 ProjectionAnnotation annotation= (ProjectionAnnotation) next;
|
|
116 Position p= model.getPosition(annotation);
|
|
117 if (p is null)
|
|
118 continue;
|
|
119
|
|
120 int distance= getDistance(annotation, p, document, line);
|
|
121 if (distance is -1)
|
|
122 continue;
|
|
123
|
|
124 if (!exact) {
|
|
125 if (distance < previousDistance) {
|
|
126 previousAnnotation= annotation;
|
|
127 previousDistance= distance;
|
|
128 }
|
|
129 } else if (distance is 0) {
|
|
130 previousAnnotation= annotation;
|
|
131 }
|
|
132 }
|
|
133 }
|
|
134 }
|
|
135
|
|
136 return previousAnnotation;
|
|
137 }
|
|
138
|
|
139 /**
|
|
140 * Returns the distance of the given line to the start line of the given position in the given document. The distance is
|
|
141 * <code>-1</code> when the line is not included in the given position.
|
|
142 *
|
|
143 * @param annotation the annotation
|
|
144 * @param position the position
|
|
145 * @param document the document
|
|
146 * @param line the line
|
|
147 * @return <code>-1</code> if line is not contained, a position number otherwise
|
|
148 */
|
|
149 private int getDistance(ProjectionAnnotation annotation, Position position, IDocument document, int line) {
|
|
150 if (position.getOffset() > -1 && position.getLength() > -1) {
|
|
151 try {
|
|
152 int startLine= document.getLineOfOffset(position.getOffset());
|
|
153 int endLine= document.getLineOfOffset(position.getOffset() + position.getLength());
|
|
154 if (startLine <= line && line < endLine) {
|
|
155 if (annotation.isCollapsed()) {
|
|
156 int captionOffset;
|
|
157 if (position instanceof IProjectionPosition)
|
|
158 captionOffset= ((IProjectionPosition) position).computeCaptionOffset(document);
|
|
159 else
|
|
160 captionOffset= 0;
|
|
161
|
|
162 int captionLine= document.getLineOfOffset(position.getOffset() + captionOffset);
|
|
163 if (startLine <= captionLine && captionLine < endLine)
|
|
164 return Math.abs(line - captionLine);
|
|
165 }
|
|
166 return line - startLine;
|
|
167 }
|
|
168 } catch (BadLocationException x) {
|
|
169 }
|
|
170 }
|
|
171 return -1;
|
|
172 }
|
|
173
|
|
174 private bool clearCurrentAnnotation() {
|
|
175 if (fCurrentAnnotation !is null) {
|
|
176 fCurrentAnnotation.setRangeIndication(false);
|
|
177 fCurrentAnnotation= null;
|
|
178 return true;
|
|
179 }
|
|
180 return false;
|
|
181 }
|
|
182
|
|
183 /*
|
|
184 * @see dwtx.jface.text.source.IVerticalRulerColumn#createControl(dwtx.jface.text.source.CompositeRuler, dwt.widgets.Composite)
|
|
185 */
|
|
186 public Control createControl(CompositeRuler parentRuler, Composite parentControl) {
|
|
187 Control control= super.createControl(parentRuler, parentControl);
|
|
188
|
|
189 // set background
|
|
190 Display display= parentControl.getDisplay();
|
|
191 Color background= display.getSystemColor(DWT.COLOR_LIST_BACKGROUND);
|
|
192 control.setBackground(background);
|
|
193
|
|
194 // install hover listener
|
|
195 control.addMouseTrackListener(new MouseTrackAdapter() {
|
|
196 public void mouseExit(MouseEvent e) {
|
|
197 if (clearCurrentAnnotation())
|
|
198 redraw();
|
|
199 }
|
|
200 });
|
|
201
|
|
202 // install mouse move listener
|
|
203 control.addMouseMoveListener(new MouseMoveListener() {
|
|
204 public void mouseMove(MouseEvent e) {
|
|
205 bool redraw= false;
|
|
206 ProjectionAnnotation annotation= findAnnotation(toDocumentLineNumber(e.y), false);
|
|
207 if (annotation !is fCurrentAnnotation) {
|
|
208 if (fCurrentAnnotation !is null) {
|
|
209 fCurrentAnnotation.setRangeIndication(false);
|
|
210 redraw= true;
|
|
211 }
|
|
212 fCurrentAnnotation= annotation;
|
|
213 if (fCurrentAnnotation !is null && !fCurrentAnnotation.isCollapsed()) {
|
|
214 fCurrentAnnotation.setRangeIndication(true);
|
|
215 redraw= true;
|
|
216 }
|
|
217 }
|
|
218 if (redraw)
|
|
219 redraw();
|
|
220 }
|
|
221 });
|
|
222 return control;
|
|
223 }
|
|
224
|
|
225 /*
|
|
226 * @see dwtx.jface.text.source.AnnotationRulerColumn#setModel(dwtx.jface.text.source.IAnnotationModel)
|
|
227 */
|
|
228 public void setModel(IAnnotationModel model) {
|
|
229 if (model instanceof IAnnotationModelExtension) {
|
|
230 IAnnotationModelExtension extension= (IAnnotationModelExtension) model;
|
|
231 model= extension.getAnnotationModel(ProjectionSupport.PROJECTION);
|
|
232 }
|
|
233 super.setModel(model);
|
|
234 }
|
|
235
|
|
236 /*
|
|
237 * @see dwtx.jface.text.source.AnnotationRulerColumn#isPropagatingMouseListener()
|
|
238 */
|
|
239 protected bool isPropagatingMouseListener() {
|
|
240 return false;
|
|
241 }
|
|
242
|
|
243 /*
|
|
244 * @see dwtx.jface.text.source.AnnotationRulerColumn#hasAnnotation(int)
|
|
245 */
|
|
246 protected bool hasAnnotation(int lineNumber) {
|
|
247 return findAnnotation(lineNumber, true) !is null;
|
|
248 }
|
|
249 }
|