1 11 package org.eclipse.jface.text.source; 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.text.BadLocationException; 20 import org.eclipse.jface.text.IDocument; 21 import org.eclipse.jface.text.Position; 22 import org.eclipse.jface.text.source.projection.AnnotationBag; 23 24 29 public class DefaultAnnotationHover implements IAnnotationHover { 30 31 34 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) { 35 List javaAnnotations= getAnnotationsForLine(sourceViewer, lineNumber); 36 if (javaAnnotations != null) { 37 38 if (javaAnnotations.size() == 1) { 39 40 Annotation annotation= (Annotation) javaAnnotations.get(0); 42 String message= annotation.getText(); 43 if (message != null && message.trim().length() > 0) 44 return formatSingleMessage(message); 45 46 } else { 47 48 List messages= new ArrayList (); 49 50 Iterator e= javaAnnotations.iterator(); 51 while (e.hasNext()) { 52 Annotation annotation= (Annotation) e.next(); 53 String message= annotation.getText(); 54 if (message != null && message.trim().length() > 0) 55 messages.add(message.trim()); 56 } 57 58 if (messages.size() == 1) 59 return formatSingleMessage((String ) messages.get(0)); 60 61 if (messages.size() > 1) 62 return formatMultipleMessages(messages); 63 } 64 } 65 return null; 66 } 67 68 75 protected boolean isIncluded(Annotation annotation) { 76 return true; 77 } 78 79 89 protected String formatSingleMessage(String message) { 90 return message; 91 } 92 93 103 protected String formatMultipleMessages(List messages) { 104 StringBuffer buffer= new StringBuffer (); 105 buffer.append(JFaceTextMessages.getString("DefaultAnnotationHover.multipleMarkers")); 107 Iterator e= messages.iterator(); 108 while (e.hasNext()) { 109 buffer.append('\n'); 110 String listItemText= (String ) e.next(); 111 buffer.append(JFaceTextMessages.getFormattedString("DefaultAnnotationHover.listItem", new String [] { listItemText })); } 113 return buffer.toString(); 114 } 115 116 private boolean isRulerLine(Position position, IDocument document, int line) { 117 if (position.getOffset() > -1 && position.getLength() > -1) { 118 try { 119 return line == document.getLineOfOffset(position.getOffset()); 120 } catch (BadLocationException x) { 121 } 122 } 123 return false; 124 } 125 126 private IAnnotationModel getAnnotationModel(ISourceViewer viewer) { 127 if (viewer instanceof ISourceViewerExtension2) { 128 ISourceViewerExtension2 extension= (ISourceViewerExtension2) viewer; 129 return extension.getVisualAnnotationModel(); 130 } 131 return viewer.getAnnotationModel(); 132 } 133 134 private boolean isDuplicateAnnotation(Map messagesAtPosition, Position position, String message) { 135 if (messagesAtPosition.containsKey(position)) { 136 Object value= messagesAtPosition.get(position); 137 if (message.equals(value)) 138 return true; 139 140 if (value instanceof List ) { 141 List messages= (List )value; 142 if (messages.contains(message)) 143 return true; 144 145 messages.add(message); 146 } else { 147 ArrayList messages= new ArrayList (); 148 messages.add(value); 149 messages.add(message); 150 messagesAtPosition.put(position, messages); 151 } 152 } else 153 messagesAtPosition.put(position, message); 154 return false; 155 } 156 157 private boolean includeAnnotation(Annotation annotation, Position position, HashMap messagesAtPosition) { 158 if (!isIncluded(annotation)) 159 return false; 160 161 String text= annotation.getText(); 162 return (text != null && !isDuplicateAnnotation(messagesAtPosition, position, text)); 163 } 164 165 private List getAnnotationsForLine(ISourceViewer viewer, int line) { 166 IAnnotationModel model= getAnnotationModel(viewer); 167 if (model == null) 168 return null; 169 170 IDocument document= viewer.getDocument(); 171 List javaAnnotations= new ArrayList (); 172 HashMap messagesAtPosition= new HashMap (); 173 Iterator iterator= model.getAnnotationIterator(); 174 175 while (iterator.hasNext()) { 176 Annotation annotation= (Annotation) iterator.next(); 177 178 Position position= model.getPosition(annotation); 179 if (position == null) 180 continue; 181 182 if (!isRulerLine(position, document, line)) 183 continue; 184 185 if (annotation instanceof AnnotationBag) { 186 AnnotationBag bag= (AnnotationBag) annotation; 187 Iterator e= bag.iterator(); 188 while (e.hasNext()) { 189 annotation= (Annotation) e.next(); 190 position= model.getPosition(annotation); 191 if (position != null && includeAnnotation(annotation, position, messagesAtPosition)) 192 javaAnnotations.add(annotation); 193 } 194 continue; 195 } 196 197 if (includeAnnotation(annotation, position, messagesAtPosition)) 198 javaAnnotations.add(annotation); 199 } 200 201 return javaAnnotations; 202 } 203 } 204 | Popular Tags |