KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search2 > internal > ui > text > AnnotationHighlighter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.search2.internal.ui.text;
12
13 import java.util.Collection JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
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 JavaDoc fMatchesToAnnotations;
46     
47     public AnnotationHighlighter(IAnnotationModel model, IDocument document) {
48         fModel= model;
49         fDocument= document;
50         fMatchesToAnnotations= new HashMap JavaDoc();
51     }
52
53     public void addHighlights(Match[] matches) {
54         HashMap JavaDoc map= new HashMap JavaDoc(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             // need to clone position, can't have it twice in a document.
79
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                     // ignore, match must be outdated
86
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 JavaDoc annotations= new HashSet JavaDoc(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 JavaDoc matchSet= fMatchesToAnnotations.values();
109         removeAnnotations(matchSet);
110         fMatchesToAnnotations.clear();
111     }
112     
113     private void addAnnotations(Map JavaDoc annotationToPositionMap) {
114         if (fModel instanceof IAnnotationModelExtension) {
115             IAnnotationModelExtension ame= (IAnnotationModelExtension) fModel;
116             ame.replaceAnnotations(new Annotation[0], annotationToPositionMap);
117         } else {
118             for (Iterator JavaDoc 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     /**
127      * Removes annotations from the given annotation model. The default implementation works for editors that
128      * implement <code>ITextEditor</code>.
129      * Subclasses may override this method.
130      * @param annotations A set containing the annotations to be removed.
131      * @see Annotation
132      */

133     private void removeAnnotations(Collection JavaDoc 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 JavaDoc 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 JavaDoc allMatches= fMatchesToAnnotations.keySet();
153             Match[] matchesCopy= (Match[]) allMatches.toArray(new Match[allMatches.size()]);
154             removeAll();
155             addHighlights(matchesCopy);
156         }
157     }
158 }
159
Popular Tags