Mercurial > projects > dwt-addons
comparison dwtx/jface/text/source/DefaultAnnotationHover.d @ 145:02cd5f1224d3
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 24 Aug 2008 22:31:00 +0200 |
parents | b6bad70d540a |
children | f70d9508c95c |
comparison
equal
deleted
inserted
replaced
144:16a71f577815 | 145:02cd5f1224d3 |
---|---|
88 import dwtx.jface.text.Position; | 88 import dwtx.jface.text.Position; |
89 import dwtx.jface.text.source.projection.AnnotationBag; | 89 import dwtx.jface.text.source.projection.AnnotationBag; |
90 | 90 |
91 /** | 91 /** |
92 * Standard implementation of {@link dwtx.jface.text.source.IAnnotationHover}. | 92 * Standard implementation of {@link dwtx.jface.text.source.IAnnotationHover}. |
93 * | 93 * |
94 * @since 3.2 | 94 * @since 3.2 |
95 */ | 95 */ |
96 public class DefaultAnnotationHover : IAnnotationHover { | 96 public class DefaultAnnotationHover : IAnnotationHover { |
97 | 97 |
98 | 98 |
99 /** | 99 /** |
100 * Tells whether the line number should be shown when no annotation is found | 100 * Tells whether the line number should be shown when no annotation is found |
101 * under the cursor. | 101 * under the cursor. |
102 * | 102 * |
103 * @since 3.4 | 103 * @since 3.4 |
104 */ | 104 */ |
105 private bool fShowLineNumber; | 105 private bool fShowLineNumber; |
106 | 106 |
107 /** | 107 /** |
108 * Creates a new default annotation hover. | 108 * Creates a new default annotation hover. |
109 * | 109 * |
110 * @since 3.4 | 110 * @since 3.4 |
111 */ | 111 */ |
112 public this() { | 112 public this() { |
113 this(false); | 113 this(false); |
114 } | 114 } |
115 | 115 |
116 /** | 116 /** |
117 * Creates a new default annotation hover. | 117 * Creates a new default annotation hover. |
118 * | 118 * |
119 * @param showLineNumber <code>true</code> if the line number should be shown when no annotation is found | 119 * @param showLineNumber <code>true</code> if the line number should be shown when no annotation is found |
120 * @since 3.4 | 120 * @since 3.4 |
121 */ | 121 */ |
122 public this(bool showLineNumber) { | 122 public this(bool showLineNumber) { |
123 fShowLineNumber= showLineNumber; | 123 fShowLineNumber= showLineNumber; |
124 } | 124 } |
125 | 125 |
126 /* | 126 /* |
127 * @see dwtx.jface.text.source.IAnnotationHover#getHoverInfo(dwtx.jface.text.source.ISourceViewer, int) | 127 * @see dwtx.jface.text.source.IAnnotationHover#getHoverInfo(dwtx.jface.text.source.ISourceViewer, int) |
128 */ | 128 */ |
129 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) { | 129 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) { |
130 List javaAnnotations= getAnnotationsForLine(sourceViewer, lineNumber); | 130 List javaAnnotations= getAnnotationsForLine(sourceViewer, lineNumber); |
131 if (javaAnnotations !is null) { | 131 if (javaAnnotations !is null) { |
132 | 132 |
133 if (javaAnnotations.size() is 1) { | 133 if (javaAnnotations.size() is 1) { |
134 | 134 |
135 // optimization | 135 // optimization |
136 Annotation annotation= cast(Annotation) javaAnnotations.get(0); | 136 Annotation annotation= cast(Annotation) javaAnnotations.get(0); |
137 String message= annotation.getText(); | 137 String message= annotation.getText(); |
138 if (message !is null && message.trim().length() > 0) | 138 if (message !is null && message.trim().length() > 0) |
139 return formatSingleMessage(message); | 139 return formatSingleMessage(message); |
140 | 140 |
141 } else { | 141 } else { |
142 | 142 |
143 List messages= new ArrayList(); | 143 List messages= new ArrayList(); |
144 | 144 |
145 Iterator e= javaAnnotations.iterator(); | 145 Iterator e= javaAnnotations.iterator(); |
146 while (e.hasNext()) { | 146 while (e.hasNext()) { |
147 Annotation annotation= cast(Annotation) e.next(); | 147 Annotation annotation= cast(Annotation) e.next(); |
148 String message= annotation.getText(); | 148 String message= annotation.getText(); |
149 if (message !is null && message.trim().length() > 0) | 149 if (message !is null && message.trim().length() > 0) |
150 messages.add(message.trim()); | 150 messages.add(message.trim()); |
151 } | 151 } |
152 | 152 |
153 if (messages.size() is 1) | 153 if (messages.size() is 1) |
154 return formatSingleMessage(cast(String)messages.get(0)); | 154 return formatSingleMessage(cast(String)messages.get(0)); |
155 | 155 |
156 if (messages.size() > 1) | 156 if (messages.size() > 1) |
157 return formatMultipleMessages(messages); | 157 return formatMultipleMessages(messages); |
158 } | 158 } |
159 } | 159 } |
160 | 160 |
161 if (fShowLineNumber && lineNumber > -1) | 161 if (fShowLineNumber && lineNumber > -1) |
162 return JFaceTextMessages.getFormattedString("DefaultAnnotationHover.lineNumber", new String[] { Integer.toString(lineNumber + 1) }); //$NON-NLS-1$ | 162 return JFaceTextMessages.getFormattedString("DefaultAnnotationHover.lineNumber", Integer.toString(lineNumber + 1) ); //$NON-NLS-1$ |
163 | 163 |
164 return null; | 164 return null; |
165 } | 165 } |
166 | 166 |
167 /** | 167 /** |
168 * Tells whether the annotation should be included in | 168 * Tells whether the annotation should be included in |
169 * the computation. | 169 * the computation. |
170 * | 170 * |
171 * @param annotation the annotation to test | 171 * @param annotation the annotation to test |
172 * @return <code>true</code> if the annotation is included in the computation | 172 * @return <code>true</code> if the annotation is included in the computation |
173 */ | 173 */ |
174 protected bool isIncluded(Annotation annotation) { | 174 protected bool isIncluded(Annotation annotation) { |
175 return true; | 175 return true; |
176 } | 176 } |
177 | 177 |
178 /** | 178 /** |
179 * Hook method to format the given single message. | 179 * Hook method to format the given single message. |
180 * <p> | 180 * <p> |
181 * Subclasses can change this to create a different | 181 * Subclasses can change this to create a different |
182 * format like HTML. | 182 * format like HTML. |
183 * </p> | 183 * </p> |
184 * | 184 * |
185 * @param message the message to format | 185 * @param message the message to format |
186 * @return the formatted message | 186 * @return the formatted message |
187 */ | 187 */ |
188 protected String formatSingleMessage(String message) { | 188 protected String formatSingleMessage(String message) { |
189 return message; | 189 return message; |
190 } | 190 } |
191 | 191 |
192 /** | 192 /** |
193 * Hook method to formats the given messages. | 193 * Hook method to formats the given messages. |
194 * <p> | 194 * <p> |
195 * Subclasses can change this to create a different | 195 * Subclasses can change this to create a different |
196 * format like HTML. | 196 * format like HTML. |
197 * </p> | 197 * </p> |
198 * | 198 * |
199 * @param messages the messages to format | 199 * @param messages the messages to format |
200 * @return the formatted message | 200 * @return the formatted message |
201 */ | 201 */ |
202 protected String formatMultipleMessages(List messages) { | 202 protected String formatMultipleMessages(List messages) { |
203 StringBuffer buffer= new StringBuffer(); | 203 StringBuffer buffer= new StringBuffer(); |
204 buffer.append(JFaceTextMessages.getString("DefaultAnnotationHover.multipleMarkers")); //$NON-NLS-1$ | 204 buffer.append(JFaceTextMessages.getString("DefaultAnnotationHover.multipleMarkers")); //$NON-NLS-1$ |
205 | 205 |
206 Iterator e= messages.iterator(); | 206 Iterator e= messages.iterator(); |
207 while (e.hasNext()) { | 207 while (e.hasNext()) { |
208 buffer.append('\n'); | 208 buffer.append('\n'); |
209 String listItemText= cast(String) e.next(); | 209 String listItemText= cast(String) e.next(); |
210 buffer.append(JFaceTextMessages.getFormattedString("DefaultAnnotationHover.listItem", new String[] { listItemText })); //$NON-NLS-1$ | 210 buffer.append(JFaceTextMessages.getFormattedString("DefaultAnnotationHover.listItem", listItemText )); //$NON-NLS-1$ |
211 } | 211 } |
212 return buffer.toString(); | 212 return buffer.toString(); |
213 } | 213 } |
214 | 214 |
215 private bool isRulerLine(Position position, IDocument document, int line) { | 215 private bool isRulerLine(Position position, IDocument document, int line) { |
216 if (position.getOffset() > -1 && position.getLength() > -1) { | 216 if (position.getOffset() > -1 && position.getLength() > -1) { |
217 try { | 217 try { |
218 return line is document.getLineOfOffset(position.getOffset()); | 218 return line is document.getLineOfOffset(position.getOffset()); |
219 } catch (BadLocationException x) { | 219 } catch (BadLocationException x) { |
220 } | 220 } |
221 } | 221 } |
222 return false; | 222 return false; |
223 } | 223 } |
224 | 224 |
225 private IAnnotationModel getAnnotationModel(ISourceViewer viewer) { | 225 private IAnnotationModel getAnnotationModel(ISourceViewer viewer) { |
226 if ( cast(ISourceViewerExtension2)viewer ) { | 226 if ( cast(ISourceViewerExtension2)viewer ) { |
227 ISourceViewerExtension2 extension= cast(ISourceViewerExtension2) viewer; | 227 ISourceViewerExtension2 extension= cast(ISourceViewerExtension2) viewer; |
228 return extension.getVisualAnnotationModel(); | 228 return extension.getVisualAnnotationModel(); |
229 } | 229 } |
230 return viewer.getAnnotationModel(); | 230 return viewer.getAnnotationModel(); |
231 } | 231 } |
232 | 232 |
233 private bool isDuplicateAnnotation(Map messagesAtPosition, Position position, String message) { | 233 private bool isDuplicateAnnotation(Map messagesAtPosition, Position position, String message) { |
234 if (messagesAtPosition.containsKey(position)) { | 234 if (messagesAtPosition.containsKey(position)) { |
235 Object value= messagesAtPosition.get(position); | 235 Object value= messagesAtPosition.get(position); |
236 if (message.equals(value)) | 236 if (message.equals(value)) |
237 return true; | 237 return true; |
238 | 238 |
239 if ( cast(List)value ) { | 239 if ( cast(List)value ) { |
240 List messages= cast(List)value; | 240 List messages= cast(List)value; |
241 if (messages.contains(message)) | 241 if (messages.contains(message)) |
242 return true; | 242 return true; |
243 | 243 |
250 } | 250 } |
251 } else | 251 } else |
252 messagesAtPosition.put(position, message); | 252 messagesAtPosition.put(position, message); |
253 return false; | 253 return false; |
254 } | 254 } |
255 | 255 |
256 private bool includeAnnotation(Annotation annotation, Position position, HashMap messagesAtPosition) { | 256 private bool includeAnnotation(Annotation annotation, Position position, HashMap messagesAtPosition) { |
257 if (!isIncluded(annotation)) | 257 if (!isIncluded(annotation)) |
258 return false; | 258 return false; |
259 | 259 |
260 String text= annotation.getText(); | 260 String text= annotation.getText(); |
261 return (text !is null && !isDuplicateAnnotation(messagesAtPosition, position, text)); | 261 return (text !is null && !isDuplicateAnnotation(messagesAtPosition, position, text)); |
262 } | 262 } |
263 | 263 |
264 private List getAnnotationsForLine(ISourceViewer viewer, int line) { | 264 private List getAnnotationsForLine(ISourceViewer viewer, int line) { |
265 IAnnotationModel model= getAnnotationModel(viewer); | 265 IAnnotationModel model= getAnnotationModel(viewer); |
266 if (model is null) | 266 if (model is null) |
267 return null; | 267 return null; |
268 | 268 |
269 IDocument document= viewer.getDocument(); | 269 IDocument document= viewer.getDocument(); |
270 List javaAnnotations= new ArrayList(); | 270 List javaAnnotations= new ArrayList(); |
271 HashMap messagesAtPosition= new HashMap(); | 271 HashMap messagesAtPosition= new HashMap(); |
272 Iterator iterator= model.getAnnotationIterator(); | 272 Iterator iterator= model.getAnnotationIterator(); |
273 | 273 |
274 while (iterator.hasNext()) { | 274 while (iterator.hasNext()) { |
275 Annotation annotation= cast(Annotation) iterator.next(); | 275 Annotation annotation= cast(Annotation) iterator.next(); |
276 | 276 |
277 Position position= model.getPosition(annotation); | 277 Position position= model.getPosition(annotation); |
278 if (position is null) | 278 if (position is null) |
279 continue; | 279 continue; |
280 | 280 |
281 if (!isRulerLine(position, document, line)) | 281 if (!isRulerLine(position, document, line)) |
282 continue; | 282 continue; |
283 | 283 |
284 if ( cast(AnnotationBag)annotation ) { | 284 if ( cast(AnnotationBag)annotation ) { |
285 AnnotationBag bag= cast(AnnotationBag) annotation; | 285 AnnotationBag bag= cast(AnnotationBag) annotation; |
286 Iterator e= bag.iterator(); | 286 Iterator e= bag.iterator(); |
287 while (e.hasNext()) { | 287 while (e.hasNext()) { |
288 annotation= cast(Annotation) e.next(); | 288 annotation= cast(Annotation) e.next(); |
290 if (position !is null && includeAnnotation(annotation, position, messagesAtPosition)) | 290 if (position !is null && includeAnnotation(annotation, position, messagesAtPosition)) |
291 javaAnnotations.add(annotation); | 291 javaAnnotations.add(annotation); |
292 } | 292 } |
293 continue; | 293 continue; |
294 } | 294 } |
295 | 295 |
296 if (includeAnnotation(annotation, position, messagesAtPosition)) | 296 if (includeAnnotation(annotation, position, messagesAtPosition)) |
297 javaAnnotations.add(annotation); | 297 javaAnnotations.add(annotation); |
298 } | 298 } |
299 | 299 |
300 return javaAnnotations; | 300 return javaAnnotations; |
301 } | 301 } |
302 } | 302 } |