KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.search2.internal.ui.text;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import org.eclipse.core.resources.IFile;
20
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.source.IAnnotationModel;
23
24 import org.eclipse.ui.IEditorInput;
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.IFileEditorInput;
27 import org.eclipse.ui.IWorkbenchPart;
28 import org.eclipse.ui.texteditor.IDocumentProvider;
29 import org.eclipse.ui.texteditor.ITextEditor;
30
31 import org.eclipse.search.ui.ISearchResult;
32 import org.eclipse.search.ui.ISearchResultListener;
33 import org.eclipse.search.ui.SearchResultEvent;
34 import org.eclipse.search.ui.text.AbstractTextSearchResult;
35 import org.eclipse.search.ui.text.FilterUpdateEvent;
36 import org.eclipse.search.ui.text.IEditorMatchAdapter;
37 import org.eclipse.search.ui.text.ISearchEditorAccess;
38 import org.eclipse.search.ui.text.Match;
39 import org.eclipse.search.ui.text.MatchEvent;
40 import org.eclipse.search.ui.text.RemoveAllEvent;
41
42 public class EditorAnnotationManager implements ISearchResultListener {
43     
44     private ArrayList JavaDoc fResults;
45     private IEditorPart fEditor;
46     private Highlighter fHighlighter; // initialized lazy
47

48     public static final int HIGHLLIGHTER_ANY= 0;
49     public static final int HIGHLIGHTER_MARKER= 1;
50     public static final int HIGHLIGHTER_ANNOTATION= 2;
51     public static final int HIGHLIGHTER_EDITOR_ACCESS= 3;
52     private static int fgHighlighterType= HIGHLLIGHTER_ANY;
53     
54     
55     public EditorAnnotationManager(IEditorPart editorPart) {
56         Assert.isNotNull(editorPart);
57         fEditor= editorPart;
58         fHighlighter= null; // lazy initialization
59
fResults= new ArrayList JavaDoc(3);
60     }
61     
62
63     public static final void debugSetHighlighterType(int type) {
64         fgHighlighterType= type;
65     }
66
67
68     void dispose() {
69         removeAllAnnotations();
70         if (fHighlighter != null)
71             fHighlighter.dispose();
72         
73         for (int i= 0; i < fResults.size(); i++) {
74             ((AbstractTextSearchResult) fResults.get(i)).removeListener(this);
75         }
76         fResults.clear();
77     }
78     
79     public synchronized void doEditorInputChanged() {
80         removeAllAnnotations();
81         
82         if (fHighlighter != null) {
83             fHighlighter.dispose();
84             fHighlighter= null;
85         }
86         
87         for (int i= 0; i < fResults.size(); i++) {
88             AbstractTextSearchResult curr= (AbstractTextSearchResult) fResults.get(i);
89             addAnnotations(curr);
90         }
91     }
92
93     public synchronized void setSearchResults(List JavaDoc results) {
94         removeAllAnnotations();
95         for (int i= 0; i < fResults.size(); i++) {
96             ((AbstractTextSearchResult) fResults.get(i)).removeListener(this);
97         }
98         fResults.clear();
99         
100         for (int i= 0; i < results.size(); i++) {
101             addSearchResult((AbstractTextSearchResult) results.get(i));
102         }
103     }
104     
105     public synchronized void addSearchResult(AbstractTextSearchResult result) {
106         fResults.add(result);
107         result.addListener(this);
108         addAnnotations(result);
109     }
110     
111     public synchronized void removeSearchResult(AbstractTextSearchResult result) {
112         fResults.remove(result);
113         result.removeListener(this);
114         removeAnnotations(result);
115     }
116     
117
118     public synchronized void searchResultChanged(SearchResultEvent e) {
119         ISearchResult searchResult= e.getSearchResult();
120         if (searchResult instanceof AbstractTextSearchResult) {
121             AbstractTextSearchResult result= (AbstractTextSearchResult) searchResult;
122             if (e instanceof MatchEvent) {
123                 MatchEvent me= (MatchEvent) e;
124                 Match[] matchesInEditor= getMatchesInEditor(me.getMatches(), result);
125                 if (matchesInEditor != null) {
126                     if (me.getKind() == MatchEvent.ADDED) {
127                         addAnnotations(matchesInEditor);
128                     } else {
129                         removeAnnotations(matchesInEditor);
130                     }
131                 }
132             } else if (e instanceof RemoveAllEvent) {
133                 removeAnnotations(result);
134             } else if (e instanceof FilterUpdateEvent) {
135                 Match[] matchesInEditor= getMatchesInEditor(((FilterUpdateEvent) e).getUpdatedMatches(), result);
136                 if (matchesInEditor != null) {
137                     removeAnnotations(matchesInEditor);
138                     addAnnotations(matchesInEditor);
139                 }
140             }
141         }
142     }
143
144     private Match[] getMatchesInEditor(Match[] matches, AbstractTextSearchResult result) {
145         IEditorMatchAdapter adapter= result.getEditorMatchAdapter();
146         if (adapter == null) {
147             return null;
148         }
149         
150         // optimize the array-length == 1 case (most common)
151
if (matches.length == 1) {
152             return adapter.isShownInEditor(matches[0], fEditor) ? matches : null;
153         }
154         
155         ArrayList JavaDoc matchesInEditor= null; // lazy initialization
156
for (int i= 0; i < matches.length; i++) {
157             Match curr= matches[i];
158             if (adapter.isShownInEditor(curr, fEditor)) {
159                 if (matchesInEditor == null) {
160                     matchesInEditor= new ArrayList JavaDoc();
161                 }
162                 matchesInEditor.add(curr);
163             }
164         }
165         if (matchesInEditor != null) {
166             return (Match[]) matchesInEditor.toArray(new Match[matchesInEditor.size()]);
167         }
168         return null;
169     }
170
171     private void removeAllAnnotations() {
172         if (fHighlighter != null)
173             fHighlighter.removeAll();
174     }
175
176     private Highlighter createHighlighter() {
177         IEditorPart editor= fEditor;
178         if (fgHighlighterType != HIGHLLIGHTER_ANY) {
179             return debugCreateHighlighter(editor);
180         }
181         ISearchEditorAccess access= (ISearchEditorAccess) editor.getAdapter(ISearchEditorAccess.class);
182         if (access != null)
183             return new EditorAccessHighlighter(access);
184         IAnnotationModel model= getAnnotationModel(editor);
185         if (model != null)
186             return new AnnotationHighlighter(model, getDocument(editor));
187         IEditorInput input= editor.getEditorInput();
188         if (input instanceof IFileEditorInput) {
189             IFile file= ((IFileEditorInput)input).getFile();
190             if (file != null)
191                 return new MarkerHighlighter(file);
192         }
193         return new Highlighter(); // does nothing
194
}
195
196     private static Highlighter debugCreateHighlighter(IEditorPart editor) {
197         if (fgHighlighterType == HIGHLIGHTER_ANNOTATION) {
198             IAnnotationModel model= getAnnotationModel(editor);
199             if (model != null)
200                 return new AnnotationHighlighter(model, getDocument(editor));
201         } else if (fgHighlighterType == HIGHLIGHTER_MARKER) {
202             IEditorInput input= editor.getEditorInput();
203             if (input instanceof IFileEditorInput) {
204                 IFile file= ((IFileEditorInput)input).getFile();
205                 if (file != null)
206                     return new MarkerHighlighter(file);
207             }
208             
209         } else if (fgHighlighterType == HIGHLIGHTER_EDITOR_ACCESS) {
210             ISearchEditorAccess access= (ISearchEditorAccess) editor.getAdapter(ISearchEditorAccess.class);
211             if (access != null)
212                 return new EditorAccessHighlighter(access);
213         }
214         return null;
215     }
216
217     private void addAnnotations(AbstractTextSearchResult result) {
218         IEditorMatchAdapter matchAdapter= result.getEditorMatchAdapter();
219         if (matchAdapter == null)
220             return;
221         Match[] matches= matchAdapter.computeContainedMatches(result, fEditor);
222         if (matches == null || matches.length == 0)
223             return;
224         addAnnotations(matches);
225     }
226     
227     private void removeAnnotations(AbstractTextSearchResult result) {
228         removeAllAnnotations();
229         
230         for (int i= 0; i < fResults.size(); i++) {
231             AbstractTextSearchResult curr= (AbstractTextSearchResult) fResults.get(i);
232             if (curr != result) {
233                 addAnnotations(curr);
234             }
235         }
236     }
237
238     private void addAnnotations(Match[] matches) {
239         if (fHighlighter == null) {
240             fHighlighter= createHighlighter();
241         }
242         fHighlighter.addHighlights(matches);
243     }
244
245     private void removeAnnotations(Match[] matches) {
246         if (fHighlighter != null)
247             fHighlighter.removeHighlights(matches);
248     }
249
250     private static IAnnotationModel getAnnotationModel(IWorkbenchPart part) {
251         IAnnotationModel model= null;
252         model= (IAnnotationModel) part.getAdapter(IAnnotationModel.class);
253         if (model == null) {
254             ITextEditor textEditor= null;
255             if (part instanceof ITextEditor) {
256                 textEditor= (ITextEditor) part;
257             }
258             if (textEditor != null) {
259                 IDocumentProvider dp= textEditor.getDocumentProvider();
260                 if (dp != null)
261                     model= dp.getAnnotationModel(textEditor.getEditorInput());
262             }
263         }
264         return model;
265     }
266
267     private static IDocument getDocument(IWorkbenchPart part) {
268         IDocument doc= null;
269         doc= (IDocument) part.getAdapter(IDocument.class);
270         if (doc == null) {
271             ITextEditor textEditor= null;
272             if (part instanceof ITextEditor) {
273                 textEditor= (ITextEditor) part;
274             }
275             if (textEditor != null) {
276                 IDocumentProvider dp= textEditor.getDocumentProvider();
277                 if (dp != null)
278                     doc= dp.getDocument(textEditor.getEditorInput());
279             }
280         }
281         return doc;
282     }
283 }
284
Popular Tags