KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
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 JavaDoc fMatchesToAnnotations;
45     
46     public EditorAccessHighlighter(ISearchEditorAccess editorAccess) {
47         fEditorAcess= editorAccess;
48         fMatchesToAnnotations= new HashMap JavaDoc();
49     }
50
51     public void addHighlights(Match[] matches) {
52         Map JavaDoc mapsByAnnotationModel= new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc maps= mapsByAnnotationModel.keySet().iterator(); maps.hasNext();) {
75             IAnnotationModel model= (IAnnotationModel) maps.next();
76             Map JavaDoc positionMap= (Map JavaDoc) 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             // need to clone position, can't have it twice in a document.
88
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 JavaDoc getMap(Map JavaDoc mapsByAnnotationModel, Match match) {
102         IAnnotationModel model= fEditorAcess.getAnnotationModel(match);
103         if (model == null)
104             return null;
105         HashMap JavaDoc map= (HashMap JavaDoc) mapsByAnnotationModel.get(model);
106         if (map == null) {
107             map= new HashMap JavaDoc();
108             mapsByAnnotationModel.put(model, map);
109         }
110         return map;
111     }
112
113     private Set JavaDoc getSet(Map JavaDoc setsByAnnotationModel, Match match) {
114         IAnnotationModel model= fEditorAcess.getAnnotationModel(match);
115         if (model == null)
116             return null;
117         HashSet JavaDoc set= (HashSet JavaDoc) setsByAnnotationModel.get(model);
118         if (set == null) {
119             set= new HashSet JavaDoc();
120             setsByAnnotationModel.put(model, set);
121         }
122         return set;
123     }
124
125     public void removeHighlights(Match[] matches) {
126         Map JavaDoc setsByAnnotationModel= new HashMap JavaDoc();
127         for (int i= 0; i < matches.length; i++) {
128             Annotation annotation= (Annotation) fMatchesToAnnotations.remove(matches[i]);
129             if (annotation != null) {
130                 Set JavaDoc annotations= getSet(setsByAnnotationModel, matches[i]);
131                 if (annotations != null)
132                     annotations.add(annotation);
133             }
134         }
135
136         for (Iterator JavaDoc maps= setsByAnnotationModel.keySet().iterator(); maps.hasNext();) {
137             IAnnotationModel model= (IAnnotationModel) maps.next();
138             Set JavaDoc set= (Set JavaDoc) setsByAnnotationModel.get(model);
139             removeAnnotations(model, set);
140         }
141
142     }
143     
144     private void addAnnotations(IAnnotationModel model, Map JavaDoc annotationToPositionMap) {
145         if (model instanceof IAnnotationModelExtension) {
146             IAnnotationModelExtension ame= (IAnnotationModelExtension) model;
147             ame.replaceAnnotations(new Annotation[0], annotationToPositionMap);
148         } else {
149             for (Iterator JavaDoc 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     /*
158      * Removes annotations from the given annotation model. The default implementation works for editors that
159      * implement <code>ITextEditor</code>.
160      * Subclasses may override this method.
161      * @param annotations A set containing the annotations to be removed.
162      * @see Annotation
163      */

164     private void removeAnnotations(IAnnotationModel model, Set JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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