[340003] Refactoring dialog hung at AbstractIndexManager.waitForConsistent
diff --git a/bundles/org.eclipse.jst.jsp.core/src/org/eclipse/jst/jsp/core/internal/JSPCorePlugin.java b/bundles/org.eclipse.jst.jsp.core/src/org/eclipse/jst/jsp/core/internal/JSPCorePlugin.java
index a3c184f..039626a 100644
--- a/bundles/org.eclipse.jst.jsp.core/src/org/eclipse/jst/jsp/core/internal/JSPCorePlugin.java
+++ b/bundles/org.eclipse.jst.jsp.core/src/org/eclipse/jst/jsp/core/internal/JSPCorePlugin.java
@@ -183,7 +183,6 @@
 			} catch(CoreException e) {
 				status = e.getStatus();
 			}
-			
 			return status;
 		}
 		
diff --git a/bundles/org.eclipse.jst.jsp.core/src/org/eclipse/jst/jsp/core/internal/java/search/JSPSearchSupport.java b/bundles/org.eclipse.jst.jsp.core/src/org/eclipse/jst/jsp/core/internal/java/search/JSPSearchSupport.java
index f17e430..7d1ed20 100644
--- a/bundles/org.eclipse.jst.jsp.core/src/org/eclipse/jst/jsp/core/internal/java/search/JSPSearchSupport.java
+++ b/bundles/org.eclipse.jst.jsp.core/src/org/eclipse/jst/jsp/core/internal/java/search/JSPSearchSupport.java
@@ -344,6 +344,9 @@
      * @param element
      * @param scope
      * @param requestor
+     * 
+     * @deprecated should use {@link #search(IJavaElement, IJavaSearchScope, SearchRequestor, IProgressMonitor)}
+     * so that operation can be canceled
      */
     public void searchRunnable(IJavaElement element, IJavaSearchScope scope, SearchRequestor requestor) {
     	this.searchRunnable(element, scope, requestor, new NullProgressMonitor());
diff --git a/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/java/refactoring/JSPMoveParticipant.java b/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/java/refactoring/JSPMoveParticipant.java
index e7e94e5..8763bf3 100644
--- a/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/java/refactoring/JSPMoveParticipant.java
+++ b/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/java/refactoring/JSPMoveParticipant.java
@@ -156,7 +156,7 @@
 			Object dest = elemArgsPair.fArgs.getDestination();
 			
 			if(dest instanceof IPackageFragment) {	
-				Change[] changes = createChangesFor(elemArgsPair.fElement, ((IPackageFragment)dest).getElementName());
+				Change[] changes = createChangesFor(elemArgsPair.fElement, ((IPackageFragment)dest).getElementName(), pm);
 				
 				/* add all new text changes to the local list of text changes so that
 				 * future iterations through the while loop will be aware of already
@@ -215,12 +215,12 @@
 	 * that while no NEW {@link Change}s maybe returned it is possible that
 	 * new {@link TextEdit}s will still added to existing {@link Change}s.
 	 */
-	protected Change[] createChangesFor(IJavaElement element, String newName) {
+	protected Change[] createChangesFor(IJavaElement element, String newName, IProgressMonitor monitor) {
 		Change[] changes;
 		BasicRefactorSearchRequestor requestor = getSearchRequestor(element, newName);
 		if(requestor != null) {
 			JSPSearchSupport support = JSPSearchSupport.getInstance();
-			support.searchRunnable(element, new JSPSearchScope(), requestor);
+			support.searchRunnable(element, new JSPSearchScope(), requestor, monitor);
 			changes = requestor.getChanges(this);
 		} else {
 			changes = new Change[0];
diff --git a/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/java/refactoring/JSPRenameParticipant.java b/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/java/refactoring/JSPRenameParticipant.java
index d11ec67..5bbf86f 100644
--- a/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/java/refactoring/JSPRenameParticipant.java
+++ b/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/java/refactoring/JSPRenameParticipant.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009 IBM Corporation and others.
+ * Copyright (c) 2009, 2011 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -150,7 +150,7 @@
 		Iterator iter = fElementAndArgumentPairs.values().iterator();
 		while(iter.hasNext()) {
 			ElementAndArgumentsPair elemArgsPair = (ElementAndArgumentsPair)iter.next();
-			Change[] changes = createChangesFor(elemArgsPair.fElement, elemArgsPair.fArgs.getNewName());
+			Change[] changes = createChangesFor(elemArgsPair.fElement, elemArgsPair.fArgs.getNewName(), pm);
 			
 			/* add all new text changes to the local list of text changes so that
 			 * future iterations through the while loop will be aware of already
@@ -207,12 +207,12 @@
 	 * that while no NEW {@link Change}s maybe returned it is possible that
 	 * new {@link TextEdit}s will still added to existing {@link Change}s.
 	 */
-	protected Change[] createChangesFor(IJavaElement element, String newName) {
+	protected Change[] createChangesFor(IJavaElement element, String newName, IProgressMonitor monitor) {
 		Change[] changes;
 		BasicRefactorSearchRequestor requestor = getSearchRequestor(element, newName);
 		if(requestor != null) {
 			JSPSearchSupport support = JSPSearchSupport.getInstance();
-			support.searchRunnable(element, new JSPSearchScope(), requestor);
+			support.searchRunnable(element, new JSPSearchScope(), requestor, monitor);
 			changes = requestor.getChanges(this);
 		} else {
 			changes = new Change[0];
diff --git a/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/java/search/JSPSearchQuery.java b/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/java/search/JSPSearchQuery.java
index 15ef838..76399eb 100644
--- a/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/java/search/JSPSearchQuery.java
+++ b/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/java/search/JSPSearchQuery.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2004, 2005 IBM Corporation and others.
+ * Copyright (c) 2004, 2011 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -13,6 +13,7 @@
 import java.text.MessageFormat;
 
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.jdt.core.IJavaElement;
@@ -47,7 +48,7 @@
 		return this;
 	}
 	
-	protected IStatus doQuery() {
+	protected IStatus doQuery(IProgressMonitor monitor) {
 		IStatus status = Status.OK_STATUS;
 		try {
 			JSPSearchSupport support = JSPSearchSupport.getInstance();
@@ -59,7 +60,7 @@
 			
 			// perform a searchs
 			// by passing in this jsp search query, requstor can add matches
-			support.searchRunnable(getJavaElement(), singleFileScope, new JSPSingleFileSearchRequestor(getInstance()));
+			support.searchRunnable(getJavaElement(), singleFileScope, new JSPSingleFileSearchRequestor(getInstance()), monitor);
 		}
 		catch (Exception e){
 			status = new Status(IStatus.ERROR, "org.eclipse.wst.sse.ui", IStatus.OK, "", null); //$NON-NLS-1$	//$NON-NLS-2$
diff --git a/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/indexing/AbstractIndexManager.java b/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/indexing/AbstractIndexManager.java
index 7897a8f..a7f0991 100644
--- a/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/indexing/AbstractIndexManager.java
+++ b/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/indexing/AbstractIndexManager.java
@@ -205,66 +205,70 @@
 		SubMonitor progress = SubMonitor.convert(monitor);
 		synchronized (this.fStartStopLock) {
 			this.fStarting = true;
-			
-			if(this.fState == STATE_DISABLED) {
-				//report status
-				progress.beginTask(NLS.bind(SSECoreMessages.IndexManager_0_starting, this.fName), 2);
-				
-				//start listening for resource change events
-				this.fResourceChangeListener.start();
-				
-				//check to see if a full re-index is required
-				boolean forcedFullReIndexNeeded = this.isForcedFullReIndexNeeded();
-				
-				/* start the indexing job only loading preserved state if not doing full index
-				 * if failed loading preserved state then force full re-index
-				 */
-				forcedFullReIndexNeeded = !this.fResourceEventProcessingJob.start(!forcedFullReIndexNeeded,
-						progress.newChild(1));
-				progress.setWorkRemaining(1);
-				
-				//don't bother processing saved delta if forced full re-index is needed
-				if(!forcedFullReIndexNeeded) {
-					/* if there is a delta attempt to process it
-					 * else need to do a full workspace index
+
+			try {
+				if(this.fState == STATE_DISABLED) {
+					//report status
+					progress.beginTask(NLS.bind(SSECoreMessages.IndexManager_0_starting, this.fName), 2);
+					
+					//start listening for resource change events
+					this.fResourceChangeListener.start();
+					
+					//check to see if a full re-index is required
+					boolean forcedFullReIndexNeeded = this.isForcedFullReIndexNeeded();
+					
+					/* start the indexing job only loading preserved state if not doing full index
+					 * if failed loading preserved state then force full re-index
 					 */
-					if(savedStateDelta != null) {
-						forcedFullReIndexNeeded = false;
-						try {
-							//deal with reporting progress
-							SubMonitor savedStateProgress = progress.newChild(1, SubMonitor.SUPPRESS_NONE);
-							
-							savedStateProgress.setTaskName(NLS.bind(SSECoreMessages.IndexManager_0_starting_1,
-									new String[] {this.fName, SSECoreMessages.IndexManager_processing_deferred_resource_changes}));
-							
-							//process delta
-							ResourceDeltaVisitor visitor = new ResourceDeltaVisitor(savedStateProgress,
-									AbstractIndexManager.SOURCE_SAVED_STATE);
-							savedStateDelta.accept(visitor);
-							
-							//process any remaining batched up resources to index
-							visitor.processBatchedResourceEvents();
-						} catch (CoreException e) {
+					forcedFullReIndexNeeded = !this.fResourceEventProcessingJob.start(!forcedFullReIndexNeeded,
+							progress.newChild(1));
+					progress.setWorkRemaining(1);
+					
+					//don't bother processing saved delta if forced full re-index is needed
+					if(!forcedFullReIndexNeeded) {
+						/* if there is a delta attempt to process it
+						 * else need to do a full workspace index
+						 */
+						if(savedStateDelta != null) {
+							forcedFullReIndexNeeded = false;
+							try {
+								//deal with reporting progress
+								SubMonitor savedStateProgress = progress.newChild(1, SubMonitor.SUPPRESS_NONE);
+								
+								savedStateProgress.setTaskName(NLS.bind(SSECoreMessages.IndexManager_0_starting_1,
+										new String[] {this.fName, SSECoreMessages.IndexManager_processing_deferred_resource_changes}));
+								
+								//process delta
+								ResourceDeltaVisitor visitor = new ResourceDeltaVisitor(savedStateProgress,
+										AbstractIndexManager.SOURCE_SAVED_STATE);
+								savedStateDelta.accept(visitor);
+								
+								//process any remaining batched up resources to index
+								visitor.processBatchedResourceEvents();
+							} catch (CoreException e) {
+								forcedFullReIndexNeeded = true;
+								Logger.logException(this.fName + ": Could not process saved state. " + //$NON-NLS-1$
+										"Forced to do a full workspace re-index.", e); //$NON-NLS-1$
+							}
+						} else {
 							forcedFullReIndexNeeded = true;
-							Logger.logException(this.fName + ": Could not process saved state. " + //$NON-NLS-1$
-									"Forced to do a full workspace re-index.", e); //$NON-NLS-1$
 						}
-					} else {
-						forcedFullReIndexNeeded = true;
 					}
+					progress.worked(1);
+					
+					//if need to process the entire workspace do so in another job
+					if(forcedFullReIndexNeeded){
+						this.fWorkspaceVisitorJob = new WorkspaceVisitorJob();
+						this.fWorkspaceVisitorJob.schedule();
+					}
+					
+					//update state
+					this.fState = STATE_ENABLED;
 				}
-				progress.worked(1);
-				
-				//if need to process the entire workspace do so in another job
-				if(forcedFullReIndexNeeded){
-					this.fWorkspaceVisitorJob = new WorkspaceVisitorJob();
-					this.fWorkspaceVisitorJob.schedule();
-				}
-				
-				//update state
-				this.fState = STATE_ENABLED;
+			} finally {
+				this.fStarting = false;
+				progress.done();
 			}
-			this.fStarting = false;
 		}
 	}
 	
@@ -437,6 +441,7 @@
 			Thread.currentThread().interrupt();
 		}
 		
+		progress.done();
 		return success;
 	}
 	
@@ -578,6 +583,7 @@
 				status = Status.OK_STATUS;
 			}
 			
+			monitor.done();
 			return status;
 		}
 		
@@ -1538,12 +1544,19 @@
 							//get the resource
 							IResource resource = null;
 							IPath resourcePath = new Path(new String(resourceLocoationBOS.toByteArray(), ENCODING_UTF16));
-							if(fileType == IResource.FILE) {
-								resource =
-									ResourcesPlugin.getWorkspace().getRoot().getFile(resourcePath);
+							if(!resourcePath.isRoot() && !resourcePath.toString().equals("")) { //$NON-NLS-1$
+								if(fileType == IResource.FILE) {
+									resource =
+										ResourcesPlugin.getWorkspace().getRoot().getFile(resourcePath);
+								} else {
+									resource =
+										ResourcesPlugin.getWorkspace().getRoot().getFolder(resourcePath);
+								}	
 							} else {
-								resource =
-									ResourcesPlugin.getWorkspace().getRoot().getFolder(resourcePath);
+								Logger.log(Logger.WARNING, "The AbstractIndexManager " +
+										AbstractIndexManager.this.fName +
+										" attempted to load an invlaid preserved resource event:\n" +
+										"(" + resourcePath + ")");
 							}
 							
 							//get the move path
@@ -1564,12 +1577,18 @@
 					}
 				} catch (FileNotFoundException e) {
 					Logger.logException(AbstractIndexManager.this.fName +
-							": Exception while opening file to read preserved resources to index.", //$NON-NLS-1$
+							": Exception while opening file to read preserved resources to index. Index manager will recover by re-indexing workspace.", //$NON-NLS-1$
 							e);
 					success = false;
 				} catch (IOException e) {
 					Logger.logException(AbstractIndexManager.this.fName +
-							": Exception while reading from file of preserved resources to index.", //$NON-NLS-1$
+							": Exception while reading from file of preserved resources to index. Index manager will recover by re-indexing workspace.", //$NON-NLS-1$
+							e);
+					success = false;
+				} catch(Exception e) {
+					//Purposely catching all exceptions here so that index manager can recover gracefully
+					Logger.logException(AbstractIndexManager.this.fName +
+							": Unexpected exception while reading from file of preserved resources to index. Index manager will recover by re-indexing workspace.", //$NON-NLS-1$
 							e);
 					success = false;
 				} finally {
@@ -1602,6 +1621,7 @@
 				}
 			}
 			
+			progress.done();
 			return success;
 		}
 		
diff --git a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/search/BasicSearchQuery.java b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/search/BasicSearchQuery.java
index d54823a..008eb33 100644
--- a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/search/BasicSearchQuery.java
+++ b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/search/BasicSearchQuery.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2001, 2009 IBM Corporation and others.
+ * Copyright (c) 2001, 2011 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -43,6 +43,11 @@
 	private AbstractTextSearchResult fResult = null;
 
 	/**
+	 * The progress monitor for the query
+	 */
+	private IProgressMonitor fProgressMonitor = null;
+
+	/**
 	 * <p>Construct a new basic query.</p>
 	 * 
 	 * <p><b>IMPORTANT: </b>It is very important that after
@@ -64,6 +69,7 @@
 	 * @see org.eclipse.search.ui.ISearchQuery#run(org.eclipse.core.runtime.IProgressMonitor)
 	 */
 	public IStatus run(IProgressMonitor monitor) {
+		fProgressMonitor = monitor;
 		return doQuery();
 	}
 	
@@ -153,8 +159,23 @@
 	 * will actually do something</p>
 	 * 
 	 * @return the status of the query when it has finished
+	 * 
+	 * @deprecated use {@link #doQuery(IProgressMonitor)} so that the operation is cancelable
 	 */
 	protected IStatus doQuery() {
+		return doQuery(fProgressMonitor);
+	}
+	
+	/**
+	 * <p>The actual work of the query, called by {@link #run(IProgressMonitor)}</p>
+	 * <p><i>Note: </i>This method should be overridden by implementers so that their query
+	 * will actually do something</p>
+	 * 
+	 * @param monitor {@link IProgressMonitor} used to track progress and cancel the operation
+	 * 
+	 * @return the status of the query when it has finished
+	 */
+	protected IStatus doQuery(IProgressMonitor monitor) {
 		return Status.OK_STATUS;
 	}
 	
diff --git a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/search/OccurrencesSearchQuery.java b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/search/OccurrencesSearchQuery.java
index b37babf..d5c8e0f 100644
--- a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/search/OccurrencesSearchQuery.java
+++ b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/search/OccurrencesSearchQuery.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2001, 2009 IBM Corporation and others.
+ * Copyright (c) 2001, 2011 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -145,14 +145,14 @@
 	 * 
 	 * @see org.eclipse.wst.sse.ui.internal.search.BasicSearchQuery#doQuery()
 	 */
-	protected IStatus doQuery() {
+	protected IStatus doQuery(IProgressMonitor monitor) {
 		IStatus status = Status.OK_STATUS;
 		FindRegions findRegions = new FindRegions(this.fDocument, this.fRegionText, this.fRegionType);
 		try {
 			// BUG158846 - deadlock if lock up entire workspace, so only lock
 			// up the file we are searching on
 			ISchedulingRule markerRule = ResourcesPlugin.getWorkspace().getRuleFactory().markerRule(getFile());
-			ResourcesPlugin.getWorkspace().run(findRegions, markerRule, IWorkspace.AVOID_UPDATE, null);
+			ResourcesPlugin.getWorkspace().run(findRegions, markerRule, IWorkspace.AVOID_UPDATE, monitor);
 		} catch (CoreException e) {
 			status = new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.OK, "", null); //$NON-NLS-1$
 		}