1 11 package org.eclipse.search.internal.ui.text; 12 13 import java.text.MessageFormat ; 14 import java.util.HashMap ; 15 16 import org.eclipse.core.resources.IMarker; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.resources.IResourceProxy; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 22 import org.eclipse.ui.actions.ActionGroup; 23 24 import org.eclipse.search.ui.IActionGroupFactory; 25 import org.eclipse.search.ui.ISearchResultView; 26 import org.eclipse.search.ui.SearchUI; 27 28 import org.eclipse.search.internal.core.text.ITextSearchResultCollector; 29 import org.eclipse.search.internal.ui.SearchMessages; 30 import org.eclipse.search.internal.ui.util.FileLabelProvider; 31 32 public class TextSearchResultCollector implements ITextSearchResultCollector { 33 34 private static final String MATCH= SearchMessages.getString("SearchResultCollector.match"); private static final String MATCHES= SearchMessages.getString("SearchResultCollector.matches"); private static final String DONE= SearchMessages.getString("SearchResultCollector.done"); 38 private IProgressMonitor fMonitor; 39 private ISearchResultView fView; 40 private TextSearchOperation fOperation; 41 private int fMatchCount= 0; 42 private Integer [] fMessageFormatArgs= new Integer [1]; 43 private long fLastUpdateTime; 44 45 46 private static class TextSearchActionGroupFactory implements IActionGroupFactory { 47 public ActionGroup createActionGroup(ISearchResultView part) { 48 return new TextSearchActionGroup(part); 49 } 50 } 51 52 55 public IProgressMonitor getProgressMonitor() { 56 return fMonitor; 57 } 58 59 void setProgressMonitor(IProgressMonitor pm) { 60 fMonitor= pm; 61 } 62 63 66 public void aboutToStart() throws CoreException { 67 fView= SearchUI.getSearchResultView(); 68 fMatchCount= 0; 69 fLastUpdateTime= 0; 70 if (fView != null) { 71 fView.searchStarted( 72 new TextSearchActionGroupFactory(), 73 fOperation.getSingularLabel(), 74 fOperation.getPluralLabelPattern(), 75 fOperation.getImageDescriptor(), 76 TextSearchPage.EXTENSION_POINT_ID, 77 new FileLabelProvider(FileLabelProvider.SHOW_LABEL_PATH), 78 new GotoMarkerAction(), 79 new GroupByKeyComputer(), 80 fOperation); 81 } 82 } 83 84 87 public void accept(final IResourceProxy proxy, String line, int start, int length, final int lineNumber) throws CoreException { 88 IResource resource= proxy.requestResource(); 89 IMarker marker= resource.createMarker(SearchUI.SEARCH_MARKER); 90 HashMap attributes= new HashMap (4); 91 attributes.put(SearchUI.LINE, line); 92 attributes.put(IMarker.CHAR_START, new Integer (start)); 93 attributes.put(IMarker.CHAR_END, new Integer (start + length)); 94 attributes.put(IMarker.LINE_NUMBER, new Integer (lineNumber)); 95 marker.setAttributes(attributes); 96 97 String description= resource.getFullPath().lastSegment(); 98 if (description == null) 99 description= ""; 101 fView.addMatch(description, resource, resource, marker); 102 103 fMatchCount++; 104 105 if (!getProgressMonitor().isCanceled() && System.currentTimeMillis() - fLastUpdateTime > 1000) { 106 getProgressMonitor().subTask(getFormattedMatchesString(fMatchCount)); 107 fLastUpdateTime= System.currentTimeMillis(); 108 } 109 } 110 111 114 public void done() { 115 if (!getProgressMonitor().isCanceled()) { 116 String matchesString= getFormattedMatchesString(fMatchCount); 117 getProgressMonitor().setTaskName(MessageFormat.format(DONE, new String []{matchesString})); 118 } 119 120 if (fView != null) 121 fView.searchFinished(); 122 123 fView= null; 125 fMonitor= null; 126 } 127 128 void setOperation(TextSearchOperation operation) { 129 fOperation= operation; 130 } 131 132 private String getFormattedMatchesString(int count) { 133 if (fMatchCount == 1) 134 return MATCH; 135 fMessageFormatArgs[0]= new Integer (count); 136 return MessageFormat.format(MATCHES, fMessageFormatArgs); 137 138 } 139 } 140 | Popular Tags |