1 11 package org.eclipse.search2.internal.ui.text; 12 13 import java.util.Collections ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 import java.util.Set ; 19 20 import org.eclipse.core.filebuffers.IFileBuffer; 21 import org.eclipse.core.filebuffers.ITextFileBuffer; 22 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.Status; 25 26 import org.eclipse.jface.text.BadLocationException; 27 import org.eclipse.jface.text.IDocument; 28 import org.eclipse.jface.text.Position; 29 import org.eclipse.jface.text.source.Annotation; 30 import org.eclipse.jface.text.source.IAnnotationModel; 31 import org.eclipse.jface.text.source.IAnnotationModelExtension; 32 33 import org.eclipse.search.ui.text.ISearchEditorAccess; 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 EditorAccessHighlighter extends Highlighter { 43 private ISearchEditorAccess fEditorAcess; 44 private Map fMatchesToAnnotations; 45 46 public EditorAccessHighlighter(ISearchEditorAccess editorAccess) { 47 fEditorAcess= editorAccess; 48 fMatchesToAnnotations= new HashMap (); 49 } 50 51 public void addHighlights(Match[] matches) { 52 Map mapsByAnnotationModel= new HashMap (); 53 for (int i= 0; i < matches.length; i++) { 54 int offset= matches[i].getOffset(); 55 int length= matches[i].getLength(); 56 if (offset >= 0 && length >= 0) { 57 try { 58 Position position= createPosition(matches[i]); 59 if (position != null) { 60 Map map= getMap(mapsByAnnotationModel, matches[i]); 61 if (map != null) { 62 Annotation annotation= matches[i].isFiltered() 63 ? new Annotation(SearchPlugin.FILTERED_SEARCH_ANNOTATION_TYPE, true, null) 64 : new Annotation(SearchPlugin.SEARCH_ANNOTATION_TYPE, true, null); 65 fMatchesToAnnotations.put(matches[i], annotation); 66 map.put(annotation, position); 67 } 68 } 69 } catch (BadLocationException e) { 70 SearchPlugin.log(new Status(IStatus.ERROR, SearchPlugin.getID(), 0, SearchMessages.EditorAccessHighlighter_error_badLocation, e)); 71 } 72 } 73 } 74 for (Iterator maps= mapsByAnnotationModel.keySet().iterator(); maps.hasNext();) { 75 IAnnotationModel model= (IAnnotationModel) maps.next(); 76 Map positionMap= (Map ) mapsByAnnotationModel.get(model); 77 addAnnotations(model, positionMap); 78 } 79 80 } 81 82 private Position createPosition(Match match) throws BadLocationException { 83 Position position= InternalSearchUI.getInstance().getPositionTracker().getCurrentPosition(match); 84 if (position == null) 85 position= new Position(match.getOffset(), match.getLength()); 86 else 87 position= new Position(position.getOffset(), position.getLength()); 89 if (match.getBaseUnit() == Match.UNIT_LINE) { 90 IDocument doc= fEditorAcess.getDocument(match); 91 if (doc != null) { 92 position= PositionTracker.convertToCharacterPosition(position, doc); 93 } else { 94 SearchPlugin.log(new Status(IStatus.ERROR, SearchPlugin.getID(), 0, SearchMessages.AnnotationHighlighter_error_noDocument, null)); 95 return null; 96 } 97 } 98 return position; 99 } 100 101 private Map getMap(Map mapsByAnnotationModel, Match match) { 102 IAnnotationModel model= fEditorAcess.getAnnotationModel(match); 103 if (model == null) 104 return null; 105 HashMap map= (HashMap ) mapsByAnnotationModel.get(model); 106 if (map == null) { 107 map= new HashMap (); 108 mapsByAnnotationModel.put(model, map); 109 } 110 return map; 111 } 112 113 private Set getSet(Map setsByAnnotationModel, Match match) { 114 IAnnotationModel model= fEditorAcess.getAnnotationModel(match); 115 if (model == null) 116 return null; 117 HashSet set= (HashSet ) setsByAnnotationModel.get(model); 118 if (set == null) { 119 set= new HashSet (); 120 setsByAnnotationModel.put(model, set); 121 } 122 return set; 123 } 124 125 public void removeHighlights(Match[] matches) { 126 Map setsByAnnotationModel= new HashMap (); 127 for (int i= 0; i < matches.length; i++) { 128 Annotation annotation= (Annotation) fMatchesToAnnotations.remove(matches[i]); 129 if (annotation != null) { 130 Set annotations= getSet(setsByAnnotationModel, matches[i]); 131 if (annotations != null) 132 annotations.add(annotation); 133 } 134 } 135 136 for (Iterator maps= setsByAnnotationModel.keySet().iterator(); maps.hasNext();) { 137 IAnnotationModel model= (IAnnotationModel) maps.next(); 138 Set set= (Set ) setsByAnnotationModel.get(model); 139 removeAnnotations(model, set); 140 } 141 142 } 143 144 private void addAnnotations(IAnnotationModel model, Map annotationToPositionMap) { 145 if (model instanceof IAnnotationModelExtension) { 146 IAnnotationModelExtension ame= (IAnnotationModelExtension) model; 147 ame.replaceAnnotations(new Annotation[0], annotationToPositionMap); 148 } else { 149 for (Iterator elements= annotationToPositionMap.keySet().iterator(); elements.hasNext();) { 150 Annotation element= (Annotation) elements.next(); 151 Position p= (Position) annotationToPositionMap.get(element); 152 model.addAnnotation(element, p); 153 } 154 } 155 } 156 157 164 private void removeAnnotations(IAnnotationModel model, Set annotations) { 165 if (model instanceof IAnnotationModelExtension) { 166 IAnnotationModelExtension ame= (IAnnotationModelExtension) model; 167 Annotation[] annotationArray= new Annotation[annotations.size()]; 168 ame.replaceAnnotations((Annotation[]) annotations.toArray(annotationArray), Collections.EMPTY_MAP); 169 } else { 170 for (Iterator iter= annotations.iterator(); iter.hasNext();) { 171 Annotation element= (Annotation) iter.next(); 172 model.removeAnnotation(element); 173 } 174 } 175 } 176 177 public void removeAll() { 178 Set matchSet= fMatchesToAnnotations.keySet(); 179 Match[] matches= new Match[matchSet.size()]; 180 removeHighlights((Match[]) matchSet.toArray(matches)); 181 } 182 183 protected void handleContentReplaced(IFileBuffer buffer) { 184 if (!(buffer instanceof ITextFileBuffer)) 185 return; 186 IDocument document= null; 187 ITextFileBuffer textBuffer= (ITextFileBuffer) buffer; 188 for (Iterator matches = fMatchesToAnnotations.keySet().iterator(); matches.hasNext();) { 189 Match match = (Match) matches.next(); 190 document= fEditorAcess.getDocument(match); 191 if (document != null) 192 break; 193 } 194 195 if (document != null && document.equals(textBuffer.getDocument())) { 196 Match[] matches= new Match[fMatchesToAnnotations.keySet().size()]; 197 fMatchesToAnnotations.keySet().toArray(matches); 198 removeAll(); 199 addHighlights(matches); 200 } 201 } 202 } 203 | Popular Tags |