1 11 package org.eclipse.jdt.internal.ui.text; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 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 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 messagesAtPosition, Position position, String message) { 79 if (messagesAtPosition.containsKey(position)) { 80 Object value= messagesAtPosition.get(position); 81 if (message.equals(value)) 82 return true; 83 84 if (value instanceof List ) { 85 List messages= (List )value; 86 if (messages.contains(message)) 87 return true; 88 else 89 messages.add(message); 90 } else { 91 ArrayList messages= new ArrayList (); 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 messagesAtPosition) { 102 AnnotationPreference preference= getAnnotationPreference(annotation); 103 if (preference == null) 104 return false; 105 106 if (OVERVIEW_RULER_HOVER.equals(fType)) { 107 String key= preference.getOverviewRulerPreferenceKey(); 108 if (key == null || !fStore.getBoolean(key)) 109 return false; 110 } else if (TEXT_RULER_HOVER.equals(fType)) { 111 String 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 key= preference.getVerticalRulerPreferenceKey(); 122 if (key != null && !fStore.getBoolean(key)) 124 return false; 125 } 126 127 String text= annotation.getText(); 128 return (text != null && !isDuplicateJavaAnnotation(messagesAtPosition, position, text)); 129 } 130 131 private List 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 javaAnnotations= new ArrayList (); 138 HashMap messagesAtPosition= new HashMap (); 139 Iterator 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 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 173 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) { 174 List javaAnnotations= getJavaAnnotationsForLine(sourceViewer, lineNumber); 175 if (javaAnnotations != null) { 176 177 if (javaAnnotations.size() == 1) { 178 179 Annotation annotation= (Annotation) javaAnnotations.get(0); 181 String message= annotation.getText(); 182 if (message != null && message.trim().length() > 0) 183 return formatSingleMessage(message); 184 185 } else { 186 187 List messages= new ArrayList (); 188 189 Iterator e= javaAnnotations.iterator(); 190 while (e.hasNext()) { 191 Annotation annotation= (Annotation) e.next(); 192 String 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 ) messages.get(0)); 199 200 if (messages.size() > 1) 201 return formatMultipleMessages(messages); 202 } 203 } 204 return null; 205 } 206 207 210 private String formatSingleMessage(String message) { 211 StringBuffer buffer= new StringBuffer (); 212 HTMLPrinter.addPageProlog(buffer); 213 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message)); 214 HTMLPrinter.addPageEpilog(buffer); 215 return buffer.toString(); 216 } 217 218 221 private String formatMultipleMessages(List messages) { 222 StringBuffer buffer= new StringBuffer (); 223 HTMLPrinter.addPageProlog(buffer); 224 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(JavaUIMessages.JavaAnnotationHover_multipleMarkersAtThisLine)); 225 226 HTMLPrinter.startBulletList(buffer); 227 Iterator e= messages.iterator(); 228 while (e.hasNext()) 229 HTMLPrinter.addBullet(buffer, HTMLPrinter.convertToHTMLContent((String ) e.next())); 230 HTMLPrinter.endBulletList(buffer); 231 232 HTMLPrinter.addPageEpilog(buffer); 233 return buffer.toString(); 234 } 235 236 242 private AnnotationPreference getAnnotationPreference(Annotation annotation) { 243 return EditorsUI.getAnnotationPreferenceLookup().getAnnotationPreference(annotation); 244 } 245 } 246 | Popular Tags |