1 11 package org.eclipse.search.internal.ui; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.LinkedList ; 18 import java.util.Map ; 19 20 import org.eclipse.core.resources.IMarker; 21 import org.eclipse.core.resources.IMarkerDelta; 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.resources.IResourceChangeEvent; 24 import org.eclipse.core.resources.IResourceChangeListener; 25 import org.eclipse.core.resources.IResourceDelta; 26 27 import org.eclipse.core.runtime.Assert; 28 import org.eclipse.core.runtime.CoreException; 29 import org.eclipse.core.runtime.IProgressMonitor; 30 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Display; 33 import org.eclipse.swt.widgets.Shell; 34 35 import org.eclipse.jface.dialogs.MessageDialog; 36 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 37 import org.eclipse.jface.viewers.Viewer; 38 39 import org.eclipse.ui.actions.WorkspaceModifyOperation; 40 41 import org.eclipse.search.ui.IGroupByKeyComputer; 42 import org.eclipse.search.ui.SearchUI; 43 44 import org.eclipse.search.internal.ui.util.ExceptionHandler; 45 46 50 public class SearchManager implements IResourceChangeListener { 51 52 static final SearchManager fgDefault= new SearchManager(); 53 54 Search fCurrentSearch= null; 55 56 private SearchManager() { 57 SearchPlugin.getWorkspace().addResourceChangeListener(this); 58 } 59 60 private HashSet fListeners= new HashSet (); 61 private LinkedList fPreviousSearches= new LinkedList (); 62 private boolean fIsRemoveAll= false; 63 64 public static SearchManager getDefault() { 65 return fgDefault; 66 } 67 68 71 LinkedList getPreviousSearches() { 72 return fPreviousSearches; 73 } 74 77 ArrayList getCurrentResults() { 78 if (fCurrentSearch == null) 79 return new ArrayList (0); 80 return (ArrayList )fCurrentSearch.getResults(); 81 } 82 83 public Search getCurrentSearch() { 84 return fCurrentSearch; 85 } 86 87 void removeAllSearches() { 88 SearchPlugin.getWorkspace().removeResourceChangeListener(this); 89 WorkspaceModifyOperation op= new WorkspaceModifyOperation(null) { 90 protected void execute(IProgressMonitor monitor) throws CoreException { 91 monitor.beginTask(SearchMessages.SearchManager_updating, 100); 92 SearchPlugin.getWorkspace().getRoot().deleteMarkers(SearchUI.SEARCH_MARKER, true, IResource.DEPTH_INFINITE); 93 monitor.worked(100); 94 monitor.done(); 95 } 96 }; 97 boolean isAutoBuilding= SearchPlugin.setAutoBuilding(false); 98 try { 99 ProgressMonitorDialog dialog= new ProgressMonitorDialog(getShell()); 100 dialog.run(true, true, op); 101 } catch (InvocationTargetException ex) { 102 ExceptionHandler.handle(ex, SearchMessages.Search_Error_deleteMarkers_title, SearchMessages.Search_Error_deleteMarkers_message); 103 } catch (InterruptedException e) { 104 } finally { 106 SearchPlugin.getWorkspace().addResourceChangeListener(this); 107 SearchPlugin.setAutoBuilding(isAutoBuilding); 108 } 109 110 fPreviousSearches= new LinkedList (); 112 fCurrentSearch= null; 113 114 Iterator iter= fListeners.iterator(); 116 while (iter.hasNext()) { 117 SearchResultViewer viewer= (SearchResultViewer)iter.next(); 118 handleAllSearchesRemoved(viewer); 119 } 120 } 121 122 private void handleAllSearchesRemoved(SearchResultViewer viewer) { 123 viewer.handleAllSearchesRemoved(); 124 } 125 126 void setCurrentSearch(final Search search) { 127 if (fCurrentSearch == search) 128 return; 129 130 SearchPlugin.getWorkspace().removeResourceChangeListener(this); 131 WorkspaceModifyOperation op= new WorkspaceModifyOperation(null) { 132 protected void execute(IProgressMonitor monitor) throws CoreException { 133 internalSetCurrentSearch(search, monitor); 134 } 135 }; 136 boolean isAutoBuilding= SearchPlugin.setAutoBuilding(false); 137 try { 138 ProgressMonitorDialog dialog= new ProgressMonitorDialog(getShell()); 139 dialog.run(true, true, op); 140 } catch (InvocationTargetException ex) { 141 ExceptionHandler.handle(ex, SearchMessages.Search_Error_switchSearch_title, SearchMessages.Search_Error_switchSearch_message); 142 } catch (InterruptedException e) { 143 } finally { 145 SearchPlugin.setAutoBuilding(isAutoBuilding); 146 } 147 148 getPreviousSearches().remove(search); 149 getPreviousSearches().addFirst(search); 150 } 151 152 void internalSetCurrentSearch(final Search search, IProgressMonitor monitor) { 153 if (fCurrentSearch != null) 154 fCurrentSearch.backupMarkers(); 155 156 final Search previousSearch= fCurrentSearch; 157 fCurrentSearch= search; 158 monitor.beginTask(SearchMessages.SearchManager_updating, getCurrentResults().size() + 20); 159 160 try { 162 SearchPlugin.getWorkspace().getRoot().deleteMarkers(SearchUI.SEARCH_MARKER, true, IResource.DEPTH_INFINITE); 163 } catch (CoreException ex) { 164 ExceptionHandler.handle(ex, SearchMessages.Search_Error_deleteMarkers_title, SearchMessages.Search_Error_deleteMarkers_message); 165 } 166 monitor.worked(10); 167 168 Iterator iter= getCurrentResults().iterator(); 170 ArrayList emptyEntries= new ArrayList (10); 171 boolean filesChanged= false; 172 boolean filesDeleted= false; 173 IGroupByKeyComputer groupByKeyComputer= getCurrentSearch().getGroupByKeyComputer(); 174 while (iter.hasNext()) { 175 monitor.worked(1); 176 SearchResultViewEntry entry= (SearchResultViewEntry)iter.next(); 177 Iterator attrPerMarkerIter= entry.getAttributesPerMarker().iterator(); 178 entry.clearMarkerList(); 179 if (entry.getResource() == null || !entry.getResource().exists()) { 180 emptyEntries.add(entry); 181 filesDeleted= true; 182 continue; 183 } 184 while (attrPerMarkerIter.hasNext()) { 185 IMarker newMarker= null; 186 try { 187 newMarker= entry.getResource().createMarker(entry.getMarkerType()); 188 } catch (CoreException ex) { 189 ExceptionHandler.handle(ex, SearchMessages.Search_Error_createMarker_title, SearchMessages.Search_Error_createMarker_message); 190 continue; 191 } 192 try { 193 newMarker.setAttributes((Map )attrPerMarkerIter.next()); 194 if (groupByKeyComputer !=null && groupByKeyComputer.computeGroupByKey(newMarker) == null) { 195 filesDeleted= true; 196 newMarker.delete(); 197 continue; 198 } 199 } catch (CoreException ex) { 200 ExceptionHandler.handle(ex, SearchMessages.Search_Error_markerAttributeAccess_title, SearchMessages.Search_Error_markerAttributeAccess_message); 201 } 202 entry.add(newMarker); 203 } 204 if (entry.getMatchCount() == 0) 205 emptyEntries.add(entry); 206 else if (!filesChanged && entry.getResource().getModificationStamp() != entry.getModificationStamp()) 207 filesChanged= true; 208 } 209 getCurrentResults().removeAll(emptyEntries); 210 monitor.worked(10); 211 212 String warningMessage= null; 213 Display display= getDisplay(); 214 215 if (filesChanged) 216 warningMessage= SearchMessages.SearchManager_resourceChanged; 217 if (filesDeleted) { 218 if (warningMessage == null) 219 warningMessage= ""; else 221 warningMessage += "\n"; warningMessage += SearchMessages.SearchManager_resourceDeleted; 223 } 224 if (warningMessage != null) { 225 if (display != null && !display.isDisposed()) { 226 final String warningTitle= SearchMessages.SearchManager_resourceChangedWarning; 227 final String warningMsg= warningMessage; 228 display.syncExec(new Runnable () { 229 public void run() { 230 MessageDialog.openWarning(getShell(), warningTitle, warningMsg); 231 } 232 }); 233 } 234 } 235 236 iter= fListeners.iterator(); 238 if (display != null && !display.isDisposed()) { 239 final Viewer visibleViewer= ((SearchResultView)SearchUI.getSearchResultView()).getViewer(); 240 while (iter.hasNext()) { 241 final SearchResultViewer viewer= (SearchResultViewer)iter.next(); 242 display.syncExec(new Runnable () { 243 public void run() { 244 if (previousSearch != null && viewer == visibleViewer) 245 previousSearch.setSelection(viewer.getSelection()); 246 viewer.setInput(null); 247 viewer.setPageId(search.getPageId()); 248 viewer.setGotoMarkerAction(search.getGotoMarkerAction()); 249 viewer.setContextMenuTarget(search.getContextMenuContributor()); 250 viewer.setActionGroupFactory(null); 251 viewer.setInput(getCurrentResults()); 252 viewer.setActionGroupFactory(search.getActionGroupFactory()); 253 viewer.setSelection(fCurrentSearch.getSelection(), true); 254 } 255 }); 256 } 257 } 258 monitor.done(); 259 } 260 261 264 int getCurrentItemCount() { 265 if (fCurrentSearch != null) 266 return fCurrentSearch.getItemCount(); 267 return 0; 268 } 269 270 void removeAllResults() { 271 fIsRemoveAll= true; 272 try { 273 SearchPlugin.getWorkspace().getRoot().deleteMarkers(SearchUI.SEARCH_MARKER, true, IResource.DEPTH_INFINITE); 274 } catch (CoreException ex) { 275 ExceptionHandler.handle(ex, SearchMessages.Search_Error_deleteMarkers_title, SearchMessages.Search_Error_deleteMarkers_message); 276 fIsRemoveAll= false; 277 } 278 } 279 280 void addNewSearch(final Search newSearch) { 281 282 SearchPlugin.getWorkspace().removeResourceChangeListener(this); 283 284 Iterator iter= fListeners.iterator(); 286 Display display= getDisplay(); 287 if (display != null && !display.isDisposed()) { 288 final Viewer visibleViewer= ((SearchResultView)SearchUI.getSearchResultView()).getViewer(); 289 while (iter.hasNext()) { 290 final SearchResultViewer viewer= (SearchResultViewer)iter.next(); 291 display.syncExec(new Runnable () { 292 public void run() { 293 if (fCurrentSearch != null && viewer == visibleViewer) 294 fCurrentSearch.setSelection(viewer.getSelection()); 295 setNewSearch(viewer, newSearch); 296 } 297 }); 298 } 299 } 300 301 if (fCurrentSearch != null) { 302 if (fCurrentSearch.isSameSearch(newSearch)) 303 getPreviousSearches().remove(fCurrentSearch); 304 else 305 fCurrentSearch.backupMarkers(); 306 } 307 fCurrentSearch= newSearch; 308 getPreviousSearches().addFirst(fCurrentSearch); 309 310 try { 312 SearchPlugin.getWorkspace().getRoot().deleteMarkers(SearchUI.SEARCH_MARKER, true, IResource.DEPTH_INFINITE); 313 } catch (CoreException ex) { 314 ExceptionHandler.handle(ex, SearchMessages.Search_Error_deleteMarkers_title, SearchMessages.Search_Error_deleteMarkers_message); 315 } 316 } 317 318 void searchFinished(ArrayList results) { 319 Assert.isNotNull(results); 320 getCurrentSearch().setResults(results); 321 322 Display display= getDisplay(); 323 if (display == null || display.isDisposed()) 324 return; 325 326 if (Thread.currentThread() == display.getThread()) 327 handleNewSearchResult(); 328 else { 329 display.syncExec(new Runnable () { 330 public void run() { 331 handleNewSearchResult(); 332 } 333 }); 334 } 335 SearchPlugin.getWorkspace().addResourceChangeListener(this); 336 } 337 338 340 void addSearchChangeListener(SearchResultViewer viewer) { 341 fListeners.add(viewer); 342 } 343 344 void removeSearchChangeListener(SearchResultViewer viewer) { 345 Assert.isNotNull(viewer); 346 fListeners.remove(viewer); 347 } 348 349 private final void handleSearchMarkersChanged(IMarkerDelta[] markerDeltas) { 350 if (fIsRemoveAll) { 351 handleRemoveAll(); 352 fIsRemoveAll= false; 353 return; 354 } 355 356 Iterator iter= fListeners.iterator(); 357 while (iter.hasNext()) 358 ((SearchResultViewer)iter.next()).getControl().setRedraw(false); 359 360 for (int i=0; i < markerDeltas.length; i++) { 361 handleSearchMarkerChanged(markerDeltas[i]); 362 } 363 364 iter= fListeners.iterator(); 365 while (iter.hasNext()) 366 ((SearchResultViewer)iter.next()).getControl().setRedraw(true); 367 368 } 369 370 private void handleSearchMarkerChanged(IMarkerDelta markerDelta) { 371 int kind= markerDelta.getKind(); 372 if (((kind & IResourceDelta.REMOVED) != 0)) 374 handleRemoveMatch(markerDelta.getMarker()); 375 else if ((kind & IResourceDelta.CHANGED) != 0) 376 handleUpdateMatch(markerDelta.getMarker()); 377 } 378 379 private void handleRemoveAll() { 380 if (fCurrentSearch != null) 381 fCurrentSearch.removeResults(); 382 Iterator iter= fListeners.iterator(); 383 while (iter.hasNext()) 384 ((SearchResultViewer)iter.next()).handleRemoveAll(); 385 } 386 387 private void handleNewSearchResult() { 388 Iterator iter= fListeners.iterator(); 389 while (iter.hasNext()) { 390 SearchResultViewer viewer= (SearchResultViewer)iter.next(); 391 viewer.setInput(getCurrentResults()); 392 } 393 } 394 395 private void setNewSearch(SearchResultViewer viewer, Search search) { 396 viewer.setInput(null); 397 viewer.clearTitle(); 398 viewer.setPageId(search.getPageId()); 399 viewer.setGotoMarkerAction(search.getGotoMarkerAction()); 400 viewer.setContextMenuTarget(search.getContextMenuContributor()); 401 viewer.setActionGroupFactory(search.getActionGroupFactory()); 402 } 403 404 private void handleRemoveMatch(IMarker marker) { 405 SearchResultViewEntry entry= findEntry(marker); 406 if (entry != null) { 407 entry.remove(marker); 408 if (entry.getMatchCount() == 0) { 409 getCurrentResults().remove(entry); 410 Iterator iter= fListeners.iterator(); 411 while (iter.hasNext()) 412 ((SearchResultViewer)iter.next()).handleRemoveMatch(entry); 413 } 414 else { 415 Iterator iter= fListeners.iterator(); 416 while (iter.hasNext()) 417 ((SearchResultViewer)iter.next()).handleUpdateMatch(entry, true); 418 } 419 } 420 } 421 422 private void handleUpdateMatch(IMarker marker) { 423 SearchResultViewEntry entry= findEntry(marker); 424 if (entry != null) { 425 Iterator iter= fListeners.iterator(); 426 while (iter.hasNext()) 427 ((SearchResultViewer)iter.next()).handleUpdateMatch(entry, false); 428 } 429 } 430 431 private SearchResultViewEntry findEntry(IMarker marker) { 432 Iterator entries= getCurrentResults().iterator(); 433 while (entries.hasNext()) { 434 SearchResultViewEntry entry= (SearchResultViewEntry)entries.next(); 435 if (entry.contains(marker)) 436 return entry; 437 } 438 return null; 439 } 440 441 446 public final void resourceChanged(final IResourceChangeEvent event) { 447 if (event == null) 448 return; 449 450 final IMarkerDelta[] markerDeltas= event.findMarkerDeltas(SearchUI.SEARCH_MARKER, true); 451 if (markerDeltas == null || markerDeltas.length < 1) 452 return; 453 454 Display display= getDisplay(); 455 if (display == null || display.isDisposed()) 456 return; 457 458 Runnable runnable= new Runnable () { 459 public void run() { 460 if (getCurrentSearch() != null) { 461 handleSearchMarkersChanged(markerDeltas); 462 Iterator iter= fListeners.iterator(); 464 while (iter.hasNext()) { 465 SearchResultViewer viewer= (SearchResultViewer)iter.next(); 466 viewer.enableActions(); 467 viewer.updateTitle(); 468 } 469 } 470 } 471 }; 472 display.syncExec(runnable); 473 } 474 477 private Display getDisplay() { 478 Iterator iter= fListeners.iterator(); 479 while (iter.hasNext()) { 480 Control control= ((Viewer)iter.next()).getControl(); 481 if (control != null && !control.isDisposed()) { 482 Display display= control.getDisplay(); 483 if (display != null && !display.isDisposed()) 484 return display; 485 } 486 } 487 return null; 488 } 489 492 private Shell getShell() { 493 return SearchPlugin.getActiveWorkbenchShell(); 494 } 495 } 496 497 | Popular Tags |