KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > JavaAnnotationHover


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.jface.preference.IPreferenceStore;
20
21 import org.eclipse.jface.text.Assert;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.Position;
25 import org.eclipse.jface.text.source.Annotation;
26 import org.eclipse.jface.text.source.IAnnotationHover;
27 import org.eclipse.jface.text.source.IAnnotationModel;
28 import org.eclipse.jface.text.source.ISourceViewer;
29 import org.eclipse.jface.text.source.ISourceViewerExtension2;
30 import org.eclipse.jface.text.source.projection.AnnotationBag;
31
32 import org.eclipse.ui.editors.text.EditorsUI;
33 import org.eclipse.ui.texteditor.AnnotationPreference;
34
35 import org.eclipse.jdt.internal.ui.JavaPlugin;
36 import org.eclipse.jdt.internal.ui.JavaUIMessages;
37
38
39 /**
40  * Determines all markers for the given line and collects, concatenates, and formates
41  * their messages.
42  */

43 public class JavaAnnotationHover implements IAnnotationHover {
44
45     private static class JavaAnnotationHoverType {
46     }
47
48     public static final JavaAnnotationHoverType OVERVIEW_RULER_HOVER= new JavaAnnotationHoverType();
49     public static final JavaAnnotationHoverType TEXT_RULER_HOVER= new JavaAnnotationHoverType();
50     public static final JavaAnnotationHoverType VERTICAL_RULER_HOVER= new JavaAnnotationHoverType();
51
52     private IPreferenceStore fStore= JavaPlugin.getDefault().getCombinedPreferenceStore();
53     private JavaAnnotationHoverType fType;
54
55     public JavaAnnotationHover(JavaAnnotationHoverType type) {
56         Assert.isTrue(OVERVIEW_RULER_HOVER.equals(type) || TEXT_RULER_HOVER.equals(type) || VERTICAL_RULER_HOVER.equals(type));
57         fType= type;
58     }
59
60     private boolean isRulerLine(Position position, IDocument document, int line) {
61         if (position.getOffset() > -1 && position.getLength() > -1) {
62             try {
63                 return line == document.getLineOfOffset(position.getOffset());
64             } catch (BadLocationException x) {
65             }
66         }
67         return false;
68     }
69
70     private IAnnotationModel getAnnotationModel(ISourceViewer viewer) {
71         if (viewer instanceof ISourceViewerExtension2) {
72             ISourceViewerExtension2 extension= (ISourceViewerExtension2) viewer;
73             return extension.getVisualAnnotationModel();
74         }
75         return viewer.getAnnotationModel();
76     }
77
78     private boolean isDuplicateJavaAnnotation(Map JavaDoc messagesAtPosition, Position position, String JavaDoc message) {
79         if (messagesAtPosition.containsKey(position)) {
80             Object JavaDoc value= messagesAtPosition.get(position);
81             if (message.equals(value))
82                 return true;
83
84             if (value instanceof List JavaDoc) {
85                 List JavaDoc messages= (List JavaDoc)value;
86                 if (messages.contains(message))
87                     return true;
88                 else
89                     messages.add(message);
90             } else {
91                 ArrayList JavaDoc messages= new ArrayList JavaDoc();
92                 messages.add(value);
93                 messages.add(message);
94                 messagesAtPosition.put(position, messages);
95             }
96         } else
97             messagesAtPosition.put(position, message);
98         return false;
99     }
100
101     private boolean includeAnnotation(Annotation annotation, Position position, HashMap JavaDoc messagesAtPosition) {
102         AnnotationPreference preference= getAnnotationPreference(annotation);
103         if (preference == null)
104             return false;
105
106         if (OVERVIEW_RULER_HOVER.equals(fType)) {
107             String JavaDoc key= preference.getOverviewRulerPreferenceKey();
108             if (key == null || !fStore.getBoolean(key))
109                 return false;
110         } else if (TEXT_RULER_HOVER.equals(fType)) {
111             String JavaDoc key= preference.getTextPreferenceKey();
112             if (key != null) {
113                 if (!fStore.getBoolean(key))
114                     return false;
115             } else {
116                 key= preference.getHighlightPreferenceKey();
117                 if (key == null || !fStore.getBoolean(key))
118                     return false;
119             }
120         } else if (VERTICAL_RULER_HOVER.equals(fType)) {
121             String JavaDoc key= preference.getVerticalRulerPreferenceKey();
122             // backward compatibility
123
if (key != null && !fStore.getBoolean(key))
124                 return false;
125         }
126
127         String JavaDoc text= annotation.getText();
128         return (text != null && !isDuplicateJavaAnnotation(messagesAtPosition, position, text));
129     }
130
131     private List JavaDoc getJavaAnnotationsForLine(ISourceViewer viewer, int line) {
132         IAnnotationModel model= getAnnotationModel(viewer);
133         if (model == null)
134             return null;
135
136         IDocument document= viewer.getDocument();
137         List JavaDoc javaAnnotations= new ArrayList JavaDoc();
138         HashMap JavaDoc messagesAtPosition= new HashMap JavaDoc();
139         Iterator JavaDoc iterator= model.getAnnotationIterator();
140
141         while (iterator.hasNext()) {
142             Annotation annotation= (Annotation) iterator.next();
143
144             Position position= model.getPosition(annotation);
145             if (position == null)
146                 continue;
147
148             if (!isRulerLine(position, document, line))
149                 continue;
150
151             if (annotation instanceof AnnotationBag) {
152                 AnnotationBag bag= (AnnotationBag) annotation;
153                 Iterator JavaDoc e= bag.iterator();
154                 while (e.hasNext()) {
155                     annotation= (Annotation) e.next();
156                     position= model.getPosition(annotation);
157                     if (position != null && includeAnnotation(annotation, position, messagesAtPosition))
158                         javaAnnotations.add(annotation);
159                 }
160                 continue;
161             }
162
163             if (includeAnnotation(annotation, position, messagesAtPosition))
164                 javaAnnotations.add(annotation);
165         }
166
167         return javaAnnotations;
168     }
169
170     /*
171      * @see IVerticalRulerHover#getHoverInfo(ISourceViewer, int)
172      */

173     public String JavaDoc getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
174         List JavaDoc javaAnnotations= getJavaAnnotationsForLine(sourceViewer, lineNumber);
175         if (javaAnnotations != null) {
176
177             if (javaAnnotations.size() == 1) {
178
179                 // optimization
180
Annotation annotation= (Annotation) javaAnnotations.get(0);
181                 String JavaDoc message= annotation.getText();
182                 if (message != null && message.trim().length() > 0)
183                     return formatSingleMessage(message);
184
185             } else {
186
187                 List JavaDoc messages= new ArrayList JavaDoc();
188
189                 Iterator JavaDoc e= javaAnnotations.iterator();
190                 while (e.hasNext()) {
191                     Annotation annotation= (Annotation) e.next();
192                     String JavaDoc message= annotation.getText();
193                     if (message != null && message.trim().length() > 0)
194                         messages.add(message.trim());
195                 }
196
197                 if (messages.size() == 1)
198                     return formatSingleMessage((String JavaDoc) messages.get(0));
199
200                 if (messages.size() > 1)
201                     return formatMultipleMessages(messages);
202             }
203         }
204         return null;
205     }
206
207     /*
208      * Formats a message as HTML text.
209      */

210     private String JavaDoc formatSingleMessage(String JavaDoc message) {
211         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
212         HTMLPrinter.addPageProlog(buffer);
213         HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
214         HTMLPrinter.addPageEpilog(buffer);
215         return buffer.toString();
216     }
217
218     /*
219      * Formats several message as HTML text.
220      */

221     private String JavaDoc formatMultipleMessages(List JavaDoc messages) {
222         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
223         HTMLPrinter.addPageProlog(buffer);
224         HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(JavaUIMessages.JavaAnnotationHover_multipleMarkersAtThisLine));
225
226         HTMLPrinter.startBulletList(buffer);
227         Iterator JavaDoc e= messages.iterator();
228         while (e.hasNext())
229             HTMLPrinter.addBullet(buffer, HTMLPrinter.convertToHTMLContent((String JavaDoc) e.next()));
230         HTMLPrinter.endBulletList(buffer);
231
232         HTMLPrinter.addPageEpilog(buffer);
233         return buffer.toString();
234     }
235
236     /**
237      * Returns the annotation preference for the given annotation.
238      *
239      * @param annotation the annotation
240      * @return the annotation preference or <code>null</code> if none
241      */

242     private AnnotationPreference getAnnotationPreference(Annotation annotation) {
243         return EditorsUI.getAnnotationPreferenceLookup().getAnnotationPreference(annotation);
244     }
245 }
246
Popular Tags