KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.filebuffers.IFileBuffer;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IMarker;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.IWorkspace;
21 import org.eclipse.core.resources.IWorkspaceRunnable;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.jface.text.Position;
25 import org.eclipse.search.internal.ui.SearchPlugin;
26 import org.eclipse.search.ui.NewSearchUI;
27 import org.eclipse.search.ui.text.Match;
28 import org.eclipse.search2.internal.ui.InternalSearchUI;
29
30 public class MarkerHighlighter extends Highlighter {
31     private IFile fFile;
32     private Map JavaDoc fMatchesToAnnotations;
33     
34     public MarkerHighlighter(IFile file) {
35         fFile= file;
36         fMatchesToAnnotations= new HashMap JavaDoc();
37     }
38
39     public void addHighlights(final Match[] matches) {
40         try {
41             SearchPlugin.getWorkspace().run(new IWorkspaceRunnable() {
42                 public void run(IProgressMonitor monitor) throws CoreException {
43                     for (int i = 0; i < matches.length; i++) {
44                         IMarker marker;
45                         marker = createMarker(matches[i]);
46                         if (marker != null)
47                             fMatchesToAnnotations.put(matches[i], marker);
48                     }
49                 }
50             }, fFile, IWorkspace.AVOID_UPDATE, null);
51         } catch (CoreException e) {
52             // just log the thing. There's nothing we can do anyway.
53
SearchPlugin.log(e.getStatus());
54         }
55     }
56     
57     private IMarker createMarker(Match match) throws CoreException {
58         Position position= InternalSearchUI.getInstance().getPositionTracker().getCurrentPosition(match);
59         if (position == null) {
60             if (match.getOffset() < 0 || match.getLength() < 0)
61                 return null;
62             position= new Position(match.getOffset(), match.getLength());
63         } else {
64             // need to clone position, can't have it twice in a document.
65
position= new Position(position.getOffset(), position.getLength());
66         }
67         IMarker marker= match.isFiltered()
68             ? fFile.createMarker(SearchPlugin.FILTERED_SEARCH_MARKER)
69             : fFile.createMarker(NewSearchUI.SEARCH_MARKER);
70         HashMap JavaDoc attributes= new HashMap JavaDoc(4);
71         if (match.getBaseUnit() == Match.UNIT_CHARACTER) {
72             attributes.put(IMarker.CHAR_START, new Integer JavaDoc(position.getOffset()));
73             attributes.put(IMarker.CHAR_END, new Integer JavaDoc(position.getOffset()+position.getLength()));
74         } else {
75             attributes.put(IMarker.LINE_NUMBER, new Integer JavaDoc(position.getOffset()));
76         }
77         marker.setAttributes(attributes);
78         return marker;
79     }
80     
81     public void removeHighlights(Match[] matches) {
82         for (int i= 0; i < matches.length; i++) {
83             IMarker marker= (IMarker) fMatchesToAnnotations.remove(matches[i]);
84             if (marker != null) {
85                 try {
86                     marker.delete();
87                 } catch (CoreException e) {
88                     // just log the thing. There's nothing we can do anyway.
89
SearchPlugin.log(e.getStatus());
90                 }
91             }
92         }
93     }
94
95     public void removeAll() {
96         try {
97             fFile.deleteMarkers(NewSearchUI.SEARCH_MARKER, true, IResource.DEPTH_INFINITE);
98             fFile.deleteMarkers(SearchPlugin.FILTERED_SEARCH_MARKER, true, IResource.DEPTH_INFINITE);
99             fMatchesToAnnotations.clear();
100         } catch (CoreException e) {
101             // just log the thing. There's nothing we can do anyway.
102
SearchPlugin.log(e.getStatus());
103         }
104     }
105
106     protected void handleContentReplaced(IFileBuffer buffer) {
107         if (!buffer.getLocation().equals(fFile.getFullPath()))
108             return;
109         Match[] matches= new Match[fMatchesToAnnotations.keySet().size()];
110         fMatchesToAnnotations.keySet().toArray(matches);
111         removeAll();
112         addHighlights(matches);
113     }
114 }
115
Popular Tags