Mercurial > projects > dwt-addons
annotate dwtx/jface/text/source/DefaultAnnotationHover.d @ 153:f70d9508c95c
Fix java Collection imports
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 25 Aug 2008 00:27:31 +0200 |
parents | 02cd5f1224d3 |
children | 1a5b8f8129df |
rev | line source |
---|---|
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 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
145
diff
changeset
|
80 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
145
diff
changeset
|
81 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
145
diff
changeset
|
82 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
145
diff
changeset
|
83 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
145
diff
changeset
|
84 |
129 | 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}. | |
145 | 93 * |
129 | 94 * @since 3.2 |
95 */ | |
96 public class DefaultAnnotationHover : IAnnotationHover { | |
145 | 97 |
98 | |
129 | 99 /** |
100 * Tells whether the line number should be shown when no annotation is found | |
101 * under the cursor. | |
145 | 102 * |
129 | 103 * @since 3.4 |
104 */ | |
105 private bool fShowLineNumber; | |
106 | |
107 /** | |
108 * Creates a new default annotation hover. | |
145 | 109 * |
129 | 110 * @since 3.4 |
111 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
112 public this() { |
129 | 113 this(false); |
114 } | |
115 | |
116 /** | |
117 * Creates a new default annotation hover. | |
145 | 118 * |
129 | 119 * @param showLineNumber <code>true</code> if the line number should be shown when no annotation is found |
120 * @since 3.4 | |
121 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
122 public this(bool showLineNumber) { |
129 | 123 fShowLineNumber= showLineNumber; |
124 } | |
145 | 125 |
129 | 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) { | |
145 | 132 |
129 | 133 if (javaAnnotations.size() is 1) { |
145 | 134 |
129 | 135 // optimization |
134 | 136 Annotation annotation= cast(Annotation) javaAnnotations.get(0); |
129 | 137 String message= annotation.getText(); |
138 if (message !is null && message.trim().length() > 0) | |
139 return formatSingleMessage(message); | |
145 | 140 |
129 | 141 } else { |
145 | 142 |
129 | 143 List messages= new ArrayList(); |
145 | 144 |
129 | 145 Iterator e= javaAnnotations.iterator(); |
146 while (e.hasNext()) { | |
134 | 147 Annotation annotation= cast(Annotation) e.next(); |
129 | 148 String message= annotation.getText(); |
149 if (message !is null && message.trim().length() > 0) | |
150 messages.add(message.trim()); | |
151 } | |
145 | 152 |
129 | 153 if (messages.size() is 1) |
134 | 154 return formatSingleMessage(cast(String)messages.get(0)); |
145 | 155 |
129 | 156 if (messages.size() > 1) |
157 return formatMultipleMessages(messages); | |
158 } | |
159 } | |
160 | |
161 if (fShowLineNumber && lineNumber > -1) | |
145 | 162 return JFaceTextMessages.getFormattedString("DefaultAnnotationHover.lineNumber", Integer.toString(lineNumber + 1) ); //$NON-NLS-1$ |
129 | 163 |
164 return null; | |
165 } | |
145 | 166 |
129 | 167 /** |
168 * Tells whether the annotation should be included in | |
169 * the computation. | |
145 | 170 * |
129 | 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 } | |
145 | 177 |
129 | 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> | |
145 | 184 * |
129 | 185 * @param message the message to format |
186 * @return the formatted message | |
187 */ | |
188 protected String formatSingleMessage(String message) { | |
189 return message; | |
190 } | |
145 | 191 |
129 | 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> | |
145 | 198 * |
129 | 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$ | |
145 | 205 |
129 | 206 Iterator e= messages.iterator(); |
207 while (e.hasNext()) { | |
208 buffer.append('\n'); | |
134 | 209 String listItemText= cast(String) e.next(); |
145 | 210 buffer.append(JFaceTextMessages.getFormattedString("DefaultAnnotationHover.listItem", listItemText )); //$NON-NLS-1$ |
129 | 211 } |
212 return buffer.toString(); | |
213 } | |
145 | 214 |
129 | 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 } | |
145 | 224 |
129 | 225 private IAnnotationModel getAnnotationModel(ISourceViewer viewer) { |
138 | 226 if ( cast(ISourceViewerExtension2)viewer ) { |
134 | 227 ISourceViewerExtension2 extension= cast(ISourceViewerExtension2) viewer; |
129 | 228 return extension.getVisualAnnotationModel(); |
229 } | |
230 return viewer.getAnnotationModel(); | |
231 } | |
145 | 232 |
129 | 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; | |
145 | 238 |
138 | 239 if ( cast(List)value ) { |
134 | 240 List messages= cast(List)value; |
129 | 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 } | |
145 | 255 |
129 | 256 private bool includeAnnotation(Annotation annotation, Position position, HashMap messagesAtPosition) { |
257 if (!isIncluded(annotation)) | |
258 return false; | |
145 | 259 |
129 | 260 String text= annotation.getText(); |
261 return (text !is null && !isDuplicateAnnotation(messagesAtPosition, position, text)); | |
262 } | |
145 | 263 |
129 | 264 private List getAnnotationsForLine(ISourceViewer viewer, int line) { |
265 IAnnotationModel model= getAnnotationModel(viewer); | |
266 if (model is null) | |
267 return null; | |
145 | 268 |
129 | 269 IDocument document= viewer.getDocument(); |
270 List javaAnnotations= new ArrayList(); | |
271 HashMap messagesAtPosition= new HashMap(); | |
272 Iterator iterator= model.getAnnotationIterator(); | |
145 | 273 |
129 | 274 while (iterator.hasNext()) { |
134 | 275 Annotation annotation= cast(Annotation) iterator.next(); |
145 | 276 |
129 | 277 Position position= model.getPosition(annotation); |
278 if (position is null) | |
279 continue; | |
145 | 280 |
129 | 281 if (!isRulerLine(position, document, line)) |
282 continue; | |
145 | 283 |
138 | 284 if ( cast(AnnotationBag)annotation ) { |
134 | 285 AnnotationBag bag= cast(AnnotationBag) annotation; |
129 | 286 Iterator e= bag.iterator(); |
287 while (e.hasNext()) { | |
134 | 288 annotation= cast(Annotation) e.next(); |
129 | 289 position= model.getPosition(annotation); |
290 if (position !is null && includeAnnotation(annotation, position, messagesAtPosition)) | |
291 javaAnnotations.add(annotation); | |
292 } | |
293 continue; | |
294 } | |
145 | 295 |
129 | 296 if (includeAnnotation(annotation, position, messagesAtPosition)) |
297 javaAnnotations.add(annotation); | |
298 } | |
145 | 299 |
129 | 300 return javaAnnotations; |
301 } | |
302 } |