1 11 package org.eclipse.pde.internal.ui.editor.text; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.resources.IMarker; 17 import org.eclipse.jface.text.BadLocationException; 18 import org.eclipse.jface.text.IDocument; 19 import org.eclipse.jface.text.Position; 20 import org.eclipse.jface.text.source.IAnnotationHover; 21 import org.eclipse.jface.text.source.IAnnotationModel; 22 import org.eclipse.jface.text.source.ISourceViewer; 23 import org.eclipse.ui.texteditor.MarkerAnnotation; 24 25 public class AnnotationHover implements IAnnotationHover { 26 27 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) { 28 String [] messages = getMessagesForLine(sourceViewer, lineNumber); 29 30 if (messages.length == 0) 31 return null; 32 33 StringBuffer buffer = new StringBuffer (); 34 for (int i = 0; i < messages.length; i++) { 35 buffer.append(messages[i]); 36 if (i < messages.length - 1) 37 buffer.append(System.getProperty("line.separator")); } 39 return buffer.toString(); 40 } 41 42 private String [] getMessagesForLine(ISourceViewer viewer, int line) { 43 IDocument document = viewer.getDocument(); 44 IAnnotationModel model = viewer.getAnnotationModel(); 45 46 if (model == null) 47 return new String [0]; 48 49 ArrayList messages = new ArrayList (); 50 51 Iterator iter = model.getAnnotationIterator(); 52 while (iter.hasNext()) { 53 Object object = iter.next(); 54 if (object instanceof MarkerAnnotation) { 55 MarkerAnnotation annotation = (MarkerAnnotation) object; 56 if (compareRulerLine(model.getPosition(annotation), 57 document, 58 line)) { 59 IMarker marker = annotation.getMarker(); 60 String message = 61 marker.getAttribute(IMarker.MESSAGE, (String ) null); 62 if (message != null && message.trim().length() > 0) 63 messages.add(message); 64 } 65 } 66 } 67 return (String []) messages.toArray(new String [messages.size()]); 68 } 69 70 private boolean compareRulerLine( 71 Position position, 72 IDocument document, 73 int line) { 74 75 try { 76 if (position.getOffset() > -1 && position.getLength() > -1) { 77 return document.getLineOfOffset(position.getOffset()) == line; 78 } 79 } catch (BadLocationException e) { 80 } 81 return false; 82 } 83 } 84 | Popular Tags |