1 11 package org.eclipse.search2.internal.ui.text; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 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 fMatchesToAnnotations; 33 34 public MarkerHighlighter(IFile file) { 35 fFile= file; 36 fMatchesToAnnotations= new HashMap (); 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 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 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 attributes= new HashMap (4); 71 if (match.getBaseUnit() == Match.UNIT_CHARACTER) { 72 attributes.put(IMarker.CHAR_START, new Integer (position.getOffset())); 73 attributes.put(IMarker.CHAR_END, new Integer (position.getOffset()+position.getLength())); 74 } else { 75 attributes.put(IMarker.LINE_NUMBER, new Integer (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 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 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 |