1 11 package org.eclipse.search2.internal.ui.text; 12 13 import java.util.Collection ; 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import org.eclipse.core.filebuffers.IFileBuffer; 22 import org.eclipse.core.filebuffers.ITextFileBuffer; 23 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.core.runtime.Status; 26 27 import org.eclipse.jface.text.BadLocationException; 28 import org.eclipse.jface.text.IDocument; 29 import org.eclipse.jface.text.Position; 30 import org.eclipse.jface.text.source.Annotation; 31 import org.eclipse.jface.text.source.IAnnotationModel; 32 import org.eclipse.jface.text.source.IAnnotationModelExtension; 33 34 import org.eclipse.search.ui.text.Match; 35 36 import org.eclipse.search.internal.ui.SearchPlugin; 37 38 import org.eclipse.search2.internal.ui.InternalSearchUI; 39 import org.eclipse.search2.internal.ui.SearchMessages; 40 41 42 public class AnnotationHighlighter extends Highlighter { 43 private IAnnotationModel fModel; 44 private IDocument fDocument; 45 private Map fMatchesToAnnotations; 46 47 public AnnotationHighlighter(IAnnotationModel model, IDocument document) { 48 fModel= model; 49 fDocument= document; 50 fMatchesToAnnotations= new HashMap (); 51 } 52 53 public void addHighlights(Match[] matches) { 54 HashMap map= new HashMap (matches.length); 55 for (int i= 0; i < matches.length; i++) { 56 int offset= matches[i].getOffset(); 57 int length= matches[i].getLength(); 58 if (offset >= 0 && length >= 0) { 59 Position position= createPosition(matches[i]); 60 if (position != null) { 61 Annotation annotation= matches[i].isFiltered() 62 ? new Annotation(SearchPlugin.FILTERED_SEARCH_ANNOTATION_TYPE, true, null) 63 : new Annotation(SearchPlugin.SEARCH_ANNOTATION_TYPE, true, null); 64 fMatchesToAnnotations.put(matches[i], annotation); 65 map.put(annotation, position); 66 } 67 } 68 } 69 addAnnotations(map); 70 71 } 72 73 private Position createPosition(Match match) { 74 Position position= InternalSearchUI.getInstance().getPositionTracker().getCurrentPosition(match); 75 if (position == null) 76 position= new Position(match.getOffset(), match.getLength()); 77 else 78 position= new Position(position.getOffset(), position.getLength()); 80 if (match.getBaseUnit() == Match.UNIT_LINE) { 81 if (fDocument != null) { 82 try { 83 position= PositionTracker.convertToCharacterPosition(position, fDocument); 84 } catch (BadLocationException e) { 85 return null; 87 } 88 } else { 89 SearchPlugin.log(new Status(IStatus.ERROR, SearchPlugin.getID(), 0, SearchMessages.AnnotationHighlighter_error_noDocument, null)); 90 return null; 91 } 92 } 93 return position; 94 } 95 96 public void removeHighlights(Match[] matches) { 97 HashSet annotations= new HashSet (matches.length); 98 for (int i= 0; i < matches.length; i++) { 99 Annotation annotation= (Annotation) fMatchesToAnnotations.remove(matches[i]); 100 if (annotation != null) { 101 annotations.add(annotation); 102 } 103 } 104 removeAnnotations(annotations); 105 } 106 107 public void removeAll() { 108 Collection matchSet= fMatchesToAnnotations.values(); 109 removeAnnotations(matchSet); 110 fMatchesToAnnotations.clear(); 111 } 112 113 private void addAnnotations(Map annotationToPositionMap) { 114 if (fModel instanceof IAnnotationModelExtension) { 115 IAnnotationModelExtension ame= (IAnnotationModelExtension) fModel; 116 ame.replaceAnnotations(new Annotation[0], annotationToPositionMap); 117 } else { 118 for (Iterator elements= annotationToPositionMap.keySet().iterator(); elements.hasNext();) { 119 Annotation element= (Annotation) elements.next(); 120 Position p= (Position) annotationToPositionMap.get(element); 121 fModel.addAnnotation(element, p); 122 } 123 } 124 } 125 126 133 private void removeAnnotations(Collection annotations) { 134 if (fModel instanceof IAnnotationModelExtension) { 135 IAnnotationModelExtension ame= (IAnnotationModelExtension) fModel; 136 Annotation[] annotationArray= new Annotation[annotations.size()]; 137 ame.replaceAnnotations((Annotation[]) annotations.toArray(annotationArray), Collections.EMPTY_MAP); 138 } else { 139 for (Iterator iter= annotations.iterator(); iter.hasNext();) { 140 Annotation element= (Annotation) iter.next(); 141 fModel.removeAnnotation(element); 142 } 143 } 144 } 145 146 protected void handleContentReplaced(IFileBuffer buffer) { 147 if (!(buffer instanceof ITextFileBuffer)) 148 return; 149 150 ITextFileBuffer textBuffer= (ITextFileBuffer) buffer; 151 if (fDocument != null && fDocument.equals(textBuffer.getDocument())) { 152 Set allMatches= fMatchesToAnnotations.keySet(); 153 Match[] matchesCopy= (Match[]) allMatches.toArray(new Match[allMatches.size()]); 154 removeAll(); 155 addHighlights(matchesCopy); 156 } 157 } 158 } 159 | Popular Tags |