Bug 515504 - rebuildIndexshould erase the new index db if it is disabled

Change-Id: I67914ec51aa67d0ff516a124bb643e6b1b6b5ed7
diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/indexer/Indexer.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/indexer/Indexer.java
index a175a36..44a20be 100644
--- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/indexer/Indexer.java
+++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/indexer/Indexer.java
@@ -1046,18 +1046,24 @@
 	}
 
 	public void rebuildIndex(IProgressMonitor monitor) throws CoreException {
-		if (!JavaIndex.isEnabled()) {
-			return;
-		}
 		SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
 
+		this.rescanJob.cancel();
+		try {
+			this.rescanJob.join(0, subMonitor.split(1));
+		} catch (InterruptedException e) {
+			// Nothing to do.
+		}
 		this.nd.acquireWriteLock(subMonitor.split(1));
 		try {
 			this.nd.clear(subMonitor.split(2));
 		} finally {
 			this.nd.releaseWriteLock();
 		}
-		rescan(subMonitor.split(98));
+		if (!JavaIndex.isEnabled()) {
+			return;
+		}
+		rescan(subMonitor.split(97));
 	}
 
 	public void requestRebuildIndex() {