129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006, 2007 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.DefaultAnnotationHover;
|
|
14
|
131
|
15 import dwtx.jface.text.source.ISharedTextColors; // packageimport
|
|
16 import dwtx.jface.text.source.ILineRange; // packageimport
|
|
17 import dwtx.jface.text.source.IAnnotationPresentation; // packageimport
|
|
18 import dwtx.jface.text.source.IVerticalRulerInfoExtension; // packageimport
|
|
19 import dwtx.jface.text.source.ICharacterPairMatcher; // packageimport
|
|
20 import dwtx.jface.text.source.TextInvocationContext; // packageimport
|
|
21 import dwtx.jface.text.source.LineChangeHover; // packageimport
|
|
22 import dwtx.jface.text.source.IChangeRulerColumn; // packageimport
|
|
23 import dwtx.jface.text.source.IAnnotationMap; // packageimport
|
|
24 import dwtx.jface.text.source.IAnnotationModelListenerExtension; // packageimport
|
|
25 import dwtx.jface.text.source.ISourceViewerExtension2; // packageimport
|
|
26 import dwtx.jface.text.source.IAnnotationHover; // packageimport
|
|
27 import dwtx.jface.text.source.ContentAssistantFacade; // packageimport
|
|
28 import dwtx.jface.text.source.IAnnotationAccess; // packageimport
|
|
29 import dwtx.jface.text.source.IVerticalRulerExtension; // packageimport
|
|
30 import dwtx.jface.text.source.IVerticalRulerColumn; // packageimport
|
|
31 import dwtx.jface.text.source.LineNumberRulerColumn; // packageimport
|
|
32 import dwtx.jface.text.source.MatchingCharacterPainter; // packageimport
|
|
33 import dwtx.jface.text.source.IAnnotationModelExtension; // packageimport
|
|
34 import dwtx.jface.text.source.ILineDifferExtension; // packageimport
|
|
35 import dwtx.jface.text.source.DefaultCharacterPairMatcher; // packageimport
|
|
36 import dwtx.jface.text.source.LineNumberChangeRulerColumn; // packageimport
|
|
37 import dwtx.jface.text.source.IAnnotationAccessExtension; // packageimport
|
|
38 import dwtx.jface.text.source.ISourceViewer; // packageimport
|
|
39 import dwtx.jface.text.source.AnnotationModel; // packageimport
|
|
40 import dwtx.jface.text.source.ILineDifferExtension2; // packageimport
|
|
41 import dwtx.jface.text.source.IAnnotationModelListener; // packageimport
|
|
42 import dwtx.jface.text.source.IVerticalRuler; // packageimport
|
|
43 import dwtx.jface.text.source.SourceViewer; // packageimport
|
|
44 import dwtx.jface.text.source.SourceViewerConfiguration; // packageimport
|
|
45 import dwtx.jface.text.source.AnnotationBarHoverManager; // packageimport
|
|
46 import dwtx.jface.text.source.CompositeRuler; // packageimport
|
|
47 import dwtx.jface.text.source.ImageUtilities; // packageimport
|
|
48 import dwtx.jface.text.source.VisualAnnotationModel; // packageimport
|
|
49 import dwtx.jface.text.source.IAnnotationModel; // packageimport
|
|
50 import dwtx.jface.text.source.ISourceViewerExtension3; // packageimport
|
|
51 import dwtx.jface.text.source.ILineDiffInfo; // packageimport
|
|
52 import dwtx.jface.text.source.VerticalRulerEvent; // packageimport
|
|
53 import dwtx.jface.text.source.ChangeRulerColumn; // packageimport
|
|
54 import dwtx.jface.text.source.ILineDiffer; // packageimport
|
|
55 import dwtx.jface.text.source.AnnotationModelEvent; // packageimport
|
|
56 import dwtx.jface.text.source.AnnotationColumn; // packageimport
|
|
57 import dwtx.jface.text.source.AnnotationRulerColumn; // packageimport
|
|
58 import dwtx.jface.text.source.IAnnotationHoverExtension; // packageimport
|
|
59 import dwtx.jface.text.source.AbstractRulerColumn; // packageimport
|
|
60 import dwtx.jface.text.source.ISourceViewerExtension; // packageimport
|
|
61 import dwtx.jface.text.source.AnnotationMap; // packageimport
|
|
62 import dwtx.jface.text.source.IVerticalRulerInfo; // packageimport
|
|
63 import dwtx.jface.text.source.IAnnotationModelExtension2; // packageimport
|
|
64 import dwtx.jface.text.source.LineRange; // packageimport
|
|
65 import dwtx.jface.text.source.IAnnotationAccessExtension2; // packageimport
|
|
66 import dwtx.jface.text.source.VerticalRuler; // packageimport
|
|
67 import dwtx.jface.text.source.JFaceTextMessages; // packageimport
|
|
68 import dwtx.jface.text.source.IOverviewRuler; // packageimport
|
|
69 import dwtx.jface.text.source.Annotation; // packageimport
|
|
70 import dwtx.jface.text.source.IVerticalRulerListener; // packageimport
|
|
71 import dwtx.jface.text.source.ISourceViewerExtension4; // packageimport
|
|
72 import dwtx.jface.text.source.AnnotationPainter; // packageimport
|
|
73 import dwtx.jface.text.source.IAnnotationHoverExtension2; // packageimport
|
|
74 import dwtx.jface.text.source.OverviewRuler; // packageimport
|
|
75 import dwtx.jface.text.source.OverviewRulerHoverManager; // packageimport
|
|
76
|
|
77
|
129
|
78 import dwt.dwthelper.utils;
|
|
79
|
|
80 import java.util.ArrayList;
|
|
81 import java.util.HashMap;
|
|
82 import java.util.Iterator;
|
|
83 import java.util.List;
|
|
84 import java.util.Map;
|
|
85
|
|
86 import dwtx.jface.text.BadLocationException;
|
|
87 import dwtx.jface.text.IDocument;
|
|
88 import dwtx.jface.text.Position;
|
|
89 import dwtx.jface.text.source.projection.AnnotationBag;
|
|
90
|
|
91 /**
|
|
92 * Standard implementation of {@link dwtx.jface.text.source.IAnnotationHover}.
|
|
93 *
|
|
94 * @since 3.2
|
|
95 */
|
|
96 public class DefaultAnnotationHover : IAnnotationHover {
|
|
97
|
|
98
|
|
99 /**
|
|
100 * Tells whether the line number should be shown when no annotation is found
|
|
101 * under the cursor.
|
|
102 *
|
|
103 * @since 3.4
|
|
104 */
|
|
105 private bool fShowLineNumber;
|
|
106
|
|
107 /**
|
|
108 * Creates a new default annotation hover.
|
|
109 *
|
|
110 * @since 3.4
|
|
111 */
|
|
112 public DefaultAnnotationHover() {
|
|
113 this(false);
|
|
114 }
|
|
115
|
|
116 /**
|
|
117 * Creates a new default annotation hover.
|
|
118 *
|
|
119 * @param showLineNumber <code>true</code> if the line number should be shown when no annotation is found
|
|
120 * @since 3.4
|
|
121 */
|
|
122 public DefaultAnnotationHover(bool showLineNumber) {
|
|
123 fShowLineNumber= showLineNumber;
|
|
124 }
|
|
125
|
|
126 /*
|
|
127 * @see dwtx.jface.text.source.IAnnotationHover#getHoverInfo(dwtx.jface.text.source.ISourceViewer, int)
|
|
128 */
|
|
129 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
|
|
130 List javaAnnotations= getAnnotationsForLine(sourceViewer, lineNumber);
|
|
131 if (javaAnnotations !is null) {
|
|
132
|
|
133 if (javaAnnotations.size() is 1) {
|
|
134
|
|
135 // optimization
|
|
136 Annotation annotation= (Annotation) javaAnnotations.get(0);
|
|
137 String message= annotation.getText();
|
|
138 if (message !is null && message.trim().length() > 0)
|
|
139 return formatSingleMessage(message);
|
|
140
|
|
141 } else {
|
|
142
|
|
143 List messages= new ArrayList();
|
|
144
|
|
145 Iterator e= javaAnnotations.iterator();
|
|
146 while (e.hasNext()) {
|
|
147 Annotation annotation= (Annotation) e.next();
|
|
148 String message= annotation.getText();
|
|
149 if (message !is null && message.trim().length() > 0)
|
|
150 messages.add(message.trim());
|
|
151 }
|
|
152
|
|
153 if (messages.size() is 1)
|
|
154 return formatSingleMessage((String)messages.get(0));
|
|
155
|
|
156 if (messages.size() > 1)
|
|
157 return formatMultipleMessages(messages);
|
|
158 }
|
|
159 }
|
|
160
|
|
161 if (fShowLineNumber && lineNumber > -1)
|
|
162 return JFaceTextMessages.getFormattedString("DefaultAnnotationHover.lineNumber", new String[] { Integer.toString(lineNumber + 1) }); //$NON-NLS-1$
|
|
163
|
|
164 return null;
|
|
165 }
|
|
166
|
|
167 /**
|
|
168 * Tells whether the annotation should be included in
|
|
169 * the computation.
|
|
170 *
|
|
171 * @param annotation the annotation to test
|
|
172 * @return <code>true</code> if the annotation is included in the computation
|
|
173 */
|
|
174 protected bool isIncluded(Annotation annotation) {
|
|
175 return true;
|
|
176 }
|
|
177
|
|
178 /**
|
|
179 * Hook method to format the given single message.
|
|
180 * <p>
|
|
181 * Subclasses can change this to create a different
|
|
182 * format like HTML.
|
|
183 * </p>
|
|
184 *
|
|
185 * @param message the message to format
|
|
186 * @return the formatted message
|
|
187 */
|
|
188 protected String formatSingleMessage(String message) {
|
|
189 return message;
|
|
190 }
|
|
191
|
|
192 /**
|
|
193 * Hook method to formats the given messages.
|
|
194 * <p>
|
|
195 * Subclasses can change this to create a different
|
|
196 * format like HTML.
|
|
197 * </p>
|
|
198 *
|
|
199 * @param messages the messages to format
|
|
200 * @return the formatted message
|
|
201 */
|
|
202 protected String formatMultipleMessages(List messages) {
|
|
203 StringBuffer buffer= new StringBuffer();
|
|
204 buffer.append(JFaceTextMessages.getString("DefaultAnnotationHover.multipleMarkers")); //$NON-NLS-1$
|
|
205
|
|
206 Iterator e= messages.iterator();
|
|
207 while (e.hasNext()) {
|
|
208 buffer.append('\n');
|
|
209 String listItemText= (String) e.next();
|
|
210 buffer.append(JFaceTextMessages.getFormattedString("DefaultAnnotationHover.listItem", new String[] { listItemText })); //$NON-NLS-1$
|
|
211 }
|
|
212 return buffer.toString();
|
|
213 }
|
|
214
|
|
215 private bool isRulerLine(Position position, IDocument document, int line) {
|
|
216 if (position.getOffset() > -1 && position.getLength() > -1) {
|
|
217 try {
|
|
218 return line is document.getLineOfOffset(position.getOffset());
|
|
219 } catch (BadLocationException x) {
|
|
220 }
|
|
221 }
|
|
222 return false;
|
|
223 }
|
|
224
|
|
225 private IAnnotationModel getAnnotationModel(ISourceViewer viewer) {
|
|
226 if (viewer instanceof ISourceViewerExtension2) {
|
|
227 ISourceViewerExtension2 extension= (ISourceViewerExtension2) viewer;
|
|
228 return extension.getVisualAnnotationModel();
|
|
229 }
|
|
230 return viewer.getAnnotationModel();
|
|
231 }
|
|
232
|
|
233 private bool isDuplicateAnnotation(Map messagesAtPosition, Position position, String message) {
|
|
234 if (messagesAtPosition.containsKey(position)) {
|
|
235 Object value= messagesAtPosition.get(position);
|
|
236 if (message.equals(value))
|
|
237 return true;
|
|
238
|
|
239 if (value instanceof List) {
|
|
240 List messages= (List)value;
|
|
241 if (messages.contains(message))
|
|
242 return true;
|
|
243
|
|
244 messages.add(message);
|
|
245 } else {
|
|
246 ArrayList messages= new ArrayList();
|
|
247 messages.add(value);
|
|
248 messages.add(message);
|
|
249 messagesAtPosition.put(position, messages);
|
|
250 }
|
|
251 } else
|
|
252 messagesAtPosition.put(position, message);
|
|
253 return false;
|
|
254 }
|
|
255
|
|
256 private bool includeAnnotation(Annotation annotation, Position position, HashMap messagesAtPosition) {
|
|
257 if (!isIncluded(annotation))
|
|
258 return false;
|
|
259
|
|
260 String text= annotation.getText();
|
|
261 return (text !is null && !isDuplicateAnnotation(messagesAtPosition, position, text));
|
|
262 }
|
|
263
|
|
264 private List getAnnotationsForLine(ISourceViewer viewer, int line) {
|
|
265 IAnnotationModel model= getAnnotationModel(viewer);
|
|
266 if (model is null)
|
|
267 return null;
|
|
268
|
|
269 IDocument document= viewer.getDocument();
|
|
270 List javaAnnotations= new ArrayList();
|
|
271 HashMap messagesAtPosition= new HashMap();
|
|
272 Iterator iterator= model.getAnnotationIterator();
|
|
273
|
|
274 while (iterator.hasNext()) {
|
|
275 Annotation annotation= (Annotation) iterator.next();
|
|
276
|
|
277 Position position= model.getPosition(annotation);
|
|
278 if (position is null)
|
|
279 continue;
|
|
280
|
|
281 if (!isRulerLine(position, document, line))
|
|
282 continue;
|
|
283
|
|
284 if (annotation instanceof AnnotationBag) {
|
|
285 AnnotationBag bag= (AnnotationBag) annotation;
|
|
286 Iterator e= bag.iterator();
|
|
287 while (e.hasNext()) {
|
|
288 annotation= (Annotation) e.next();
|
|
289 position= model.getPosition(annotation);
|
|
290 if (position !is null && includeAnnotation(annotation, position, messagesAtPosition))
|
|
291 javaAnnotations.add(annotation);
|
|
292 }
|
|
293 continue;
|
|
294 }
|
|
295
|
|
296 if (includeAnnotation(annotation, position, messagesAtPosition))
|
|
297 javaAnnotations.add(annotation);
|
|
298 }
|
|
299
|
|
300 return javaAnnotations;
|
|
301 }
|
|
302 }
|