1 11 package org.eclipse.ant.internal.ui.editor.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.ant.internal.ui.editor.AntEditorMessages; 20 import org.eclipse.jface.internal.text.html.HTMLPrinter; 21 import org.eclipse.jface.text.BadLocationException; 22 import org.eclipse.jface.text.IDocument; 23 import org.eclipse.jface.text.Position; 24 import org.eclipse.jface.text.source.Annotation; 25 import org.eclipse.jface.text.source.IAnnotationHover; 26 import org.eclipse.jface.text.source.IAnnotationModel; 27 import org.eclipse.jface.text.source.ISourceViewer; 28 29 30 34 public class XMLAnnotationHover implements IAnnotationHover { 35 36 39 private int compareRulerLine(Position position, IDocument document, int line) { 40 41 if (position.getOffset() > -1 && position.getLength() > -1) { 42 try { 43 int xmlAnnotationLine= document.getLineOfOffset(position.getOffset()); 44 if (line == xmlAnnotationLine) 45 return 1; 46 if (xmlAnnotationLine <= line && line <= document.getLineOfOffset(position.getOffset() + position.getLength())) 47 return 2; 48 } catch (BadLocationException x) { 49 } 50 } 51 52 return 0; 53 } 54 55 58 private List getXMLAnnotationsForLine(ISourceViewer viewer, int line) { 59 60 IDocument document= viewer.getDocument(); 61 IAnnotationModel model= viewer.getAnnotationModel(); 62 63 if (model == null) 64 return null; 65 66 List exact= new ArrayList (); 67 68 Iterator e= model.getAnnotationIterator(); 69 Map messagesAtPosition= new HashMap (); 70 while (e.hasNext()) { 71 Object o= e.next(); 72 if (o instanceof Annotation) { 73 Annotation a= (Annotation)o; 74 Position position= model.getPosition(a); 75 if (position == null) 76 continue; 77 78 if (isDuplicateXMLAnnotation(messagesAtPosition, position, a.getText())) 79 continue; 80 81 switch (compareRulerLine(position, document, line)) { 82 case 1: 83 exact.add(a); 84 break; 85 } 86 } 87 } 88 89 return exact; 90 } 91 92 private boolean isDuplicateXMLAnnotation(Map messagesAtPosition, Position position, String message) { 93 if (messagesAtPosition.containsKey(position)) { 94 Object value= messagesAtPosition.get(position); 95 if (message.equals(value)) 96 return true; 97 98 if (value instanceof List ) { 99 List messages= (List )value; 100 if (messages.contains(message)) { 101 return true; 102 } 103 messages.add(message); 104 } else { 105 ArrayList messages= new ArrayList (); 106 messages.add(value); 107 messages.add(message); 108 messagesAtPosition.put(position, messages); 109 } 110 } else 111 messagesAtPosition.put(position, message); 112 return false; 113 } 114 115 118 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) { 119 List xmlAnnotations= getXMLAnnotationsForLine(sourceViewer, lineNumber); 120 if (xmlAnnotations != null) { 121 122 if (xmlAnnotations.size() == 1) { 123 124 Annotation xmlAnnotation= (Annotation)xmlAnnotations.get(0); 126 String message= xmlAnnotation.getText(); 127 if (message != null && message.trim().length() > 0) { 128 return formatSingleMessage(message); 129 } 130 131 } else { 132 133 List messages= new ArrayList (xmlAnnotations.size()); 134 Iterator e= xmlAnnotations.iterator(); 135 while (e.hasNext()) { 136 Annotation xmlAnnotation= (Annotation)e.next(); 137 String message= xmlAnnotation.getText(); 138 if (message != null && message.trim().length() > 0) { 139 messages.add(message.trim()); 140 } 141 } 142 143 if (messages.size() == 1) { 144 return formatSingleMessage((String ) messages.get(0)); 145 } 146 147 if (messages.size() > 1) { 148 return formatMultipleMessages(messages); 149 } 150 } 151 } 152 153 return null; 154 } 155 156 159 private String formatSingleMessage(String message) { 160 StringBuffer buffer= new StringBuffer (); 161 HTMLPrinter.addPageProlog(buffer); 162 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message)); 163 HTMLPrinter.addPageEpilog(buffer); 164 return buffer.toString(); 165 } 166 167 170 private String formatMultipleMessages(List messages) { 171 StringBuffer buffer= new StringBuffer (); 172 HTMLPrinter.addPageProlog(buffer); 173 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(AntEditorMessages.getString("AntAnnotationHover.multipleMarkersAtThisLine"))); 175 HTMLPrinter.startBulletList(buffer); 176 Iterator e= messages.iterator(); 177 while (e.hasNext()) 178 HTMLPrinter.addBullet(buffer, HTMLPrinter.convertToHTMLContent((String ) e.next())); 179 HTMLPrinter.endBulletList(buffer); 180 181 HTMLPrinter.addPageEpilog(buffer); 182 return buffer.toString(); 183 } 184 } 185 | Popular Tags |