1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import java.text.MessageFormat ; 14 import java.util.HashMap ; 15 import org.eclipse.core.resources.IMarker; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.jdt.core.ICompilationUnit; 20 import org.eclipse.jdt.core.IJavaElement; 21 import org.eclipse.jdt.core.IMember; 22 import org.eclipse.jdt.core.JavaCore; 23 import org.eclipse.jdt.core.search.IJavaSearchResultCollector; 24 import org.eclipse.jdt.internal.ui.JavaPlugin; 25 import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog; 26 import org.eclipse.jdt.ui.JavaUI; 27 import org.eclipse.jface.dialogs.IDialogConstants; 28 import org.eclipse.jface.dialogs.MessageDialog; 29 import org.eclipse.search.ui.IActionGroupFactory; 30 import org.eclipse.search.ui.ISearchResultView; 31 import org.eclipse.search.ui.SearchUI; 32 import org.eclipse.swt.widgets.Shell; 33 import org.eclipse.ui.actions.ActionGroup; 34 import org.eclipse.ui.ide.IDE; 35 36 37 public class JavaSearchResultCollector implements IJavaSearchResultCollector { 38 39 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"); private static final Boolean POTENTIAL_MATCH_VALUE= new Boolean (true); 43 private static final String POTENTIAL_MATCH_DIALOG_ID= "Search.PotentialMatchDialog"; 45 private IProgressMonitor fMonitor; 46 private ISearchResultView fView; 47 private JavaSearchOperation fOperation; 48 private int fMatchCount= 0; 49 private int fPotentialMatchCount= 0; 50 private long fLastUpdateTime; 51 private Integer [] fMessageFormatArgs= new Integer [1]; 52 53 private class ActionGroupFactory implements IActionGroupFactory { 54 public ActionGroup createActionGroup(ISearchResultView part) { 55 return new SearchViewActionGroup(part); 56 } 57 } 58 59 public JavaSearchResultCollector() { 60 JavaPlugin.getDefault().getImageRegistry(); 62 } 63 64 67 public void aboutToStart() { 68 fPotentialMatchCount= 0; 69 fView= SearchUI.getSearchResultView(); 70 fMatchCount= 0; 71 fLastUpdateTime= 0; 72 if (fView != null) { 73 fView.searchStarted( 74 new ActionGroupFactory(), 75 fOperation.getSingularLabel(), 76 fOperation.getPluralLabelPattern(), 77 fOperation.getImageDescriptor(), 78 JavaSearchPage.EXTENSION_POINT_ID, 79 new JavaSearchResultLabelProvider(), 80 new GotoMarkerAction(), 81 new GroupByKeyComputer(), 82 fOperation); 83 } 84 } 85 86 89 public void accept(IResource resource, int start, int end, IJavaElement enclosingElement, int accuracy) throws CoreException { 90 if (enclosingElement == null || accuracy == POTENTIAL_MATCH && SearchUI.arePotentialMatchesIgnored()) 91 return; 92 93 IMarker marker= resource.createMarker(SearchUI.SEARCH_MARKER); 94 HashMap attributes; 95 Object groupKey= enclosingElement; 96 attributes= new HashMap (7); 97 if (accuracy == POTENTIAL_MATCH) { 98 fPotentialMatchCount++; 99 attributes.put(SearchUI.POTENTIAL_MATCH, POTENTIAL_MATCH_VALUE); 100 if (groupKey == null) 101 groupKey= "?:null"; else 103 groupKey= "?:" + enclosingElement.getHandleIdentifier(); } 105 ICompilationUnit cu= SearchUtil.findCompilationUnit(enclosingElement); 106 if (cu != null && cu.isWorkingCopy()) 107 attributes.put(IJavaSearchUIConstants.ATT_IS_WORKING_COPY, new Boolean (true)); 109 JavaCore.addJavaElementMarkerAttributes(attributes, enclosingElement); 110 attributes.put(IJavaSearchUIConstants.ATT_JE_HANDLE_ID, enclosingElement.getHandleIdentifier()); 111 attributes.put(IMarker.CHAR_START, new Integer (Math.max(start, 0))); 112 attributes.put(IMarker.CHAR_END, new Integer (Math.max(end, 0))); 113 if (enclosingElement instanceof IMember && ((IMember)enclosingElement).isBinary()) 114 attributes.put(IDE.EDITOR_ID_ATTR, JavaUI.ID_CF_EDITOR); 115 else 116 attributes.put(IDE.EDITOR_ID_ATTR, JavaUI.ID_CU_EDITOR); 117 marker.setAttributes(attributes); 118 119 if (fView != null) 120 fView.addMatch(enclosingElement.getElementName(), groupKey, resource, marker); 121 fMatchCount++; 122 if (!getProgressMonitor().isCanceled() && System.currentTimeMillis() - fLastUpdateTime > 1000) { 123 getProgressMonitor().subTask(getFormattedMatchesString(fMatchCount)); 124 fLastUpdateTime= System.currentTimeMillis(); 125 } 126 } 127 128 131 public void done() { 132 if (!getProgressMonitor().isCanceled()) { 133 String matchesString= getFormattedMatchesString(fMatchCount); 134 getProgressMonitor().setTaskName(MessageFormat.format(DONE, new String []{matchesString})); 135 } 136 137 if (fView != null) { 138 if (fPotentialMatchCount > 0) 139 explainPotentialMatch(fPotentialMatchCount); 140 fView.searchFinished(); 141 } 142 143 fView= null; 145 fMonitor= null; 146 } 147 148 private void explainPotentialMatch(final int potentialMatchCount) { 149 final Shell shell= fView.getSite().getShell(); 151 final String title; 152 if (potentialMatchCount == 1) 153 title= new String (SearchMessages.getString("Search.potentialMatchDialog.title.foundPotentialMatch")); else 155 title= new String (SearchMessages.getFormattedString("Search.potentialMatchDialog.title.foundPotentialMatches", "" + potentialMatchCount)); 157 shell.getDisplay().syncExec(new Runnable () { 158 public void run() { 159 OptionalMessageDialog.open( 160 POTENTIAL_MATCH_DIALOG_ID, shell, 162 title, 163 null, 164 SearchMessages.getString("Search.potentialMatchDialog.message"), MessageDialog.INFORMATION, 166 new String [] { IDialogConstants.OK_LABEL }, 167 0); 168 } 169 }); 170 } 171 172 175 public IProgressMonitor getProgressMonitor() { 176 return fMonitor; 177 } 178 179 void setProgressMonitor(IProgressMonitor pm) { 180 fMonitor= pm; 181 } 182 183 void setOperation(JavaSearchOperation operation) { 184 fOperation= operation; 185 } 186 187 private String getFormattedMatchesString(int count) { 188 if (fMatchCount == 1) 189 return MATCH; 190 fMessageFormatArgs[0]= new Integer (count); 191 return MessageFormat.format(MATCHES, fMessageFormatArgs); 192 193 } 194 } 195 | Popular Tags |