comparison dwtx/jface/text/source/projection/ProjectionRulerColumn.d @ 129:eb30df5ca28b

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