extracted isInNonCancelableSection() method

This commit is contained in:
Alexey Kudravtsev
2016-11-10 14:04:12 +03:00
parent 01eb5665a3
commit a37be1782a
2 changed files with 19 additions and 7 deletions
@@ -22,9 +22,7 @@ import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.application.ex.ApplicationEx;
import com.intellij.openapi.application.ex.ApplicationManagerEx;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.*;
import com.intellij.openapi.util.EmptyRunnable;
import com.intellij.openapi.util.Ref;
import com.intellij.util.ui.EdtInvocationManager;
@@ -88,7 +86,7 @@ public class ProgressIndicatorUtils {
* This method attempts to run provided action synchronously in a read action, so that, if possible, it wouldn't impact any pending,
* executing or future write actions (for this to work effectively the action should invoke {@link ProgressManager#checkCanceled()} or
* {@link ProgressIndicator#checkCanceled()} often enough).
* It returns <code>true</code> if action was executed successfully. It returns <code>false</code> if the action was not
* It returns {@code true} if action was executed successfully. It returns {@code false} if the action was not
* executed successfully, i.e. if:
* <ul>
* <li>write action was in progress when the method was called</li>
@@ -104,7 +102,7 @@ public class ProgressIndicatorUtils {
}
public static boolean runWithWriteActionPriority(@NotNull final Runnable action,
@NotNull final ProgressIndicator progressIndicator) {
@NotNull final ProgressIndicator progressIndicator) {
final ApplicationEx application = (ApplicationEx)ApplicationManager.getApplication();
if (application.isWriteActionPending()) {
@@ -235,4 +233,18 @@ public class ProgressIndicatorUtils {
public static void yieldToPendingWriteActions() {
ApplicationManager.getApplication().invokeAndWait(EmptyRunnable.INSTANCE, ModalityState.any());
}
public static boolean isInNonCancelableSection() {
ProgressIndicator indicator = ProgressIndicatorProvider.getGlobalProgressIndicator();
while (indicator != null) {
if (indicator instanceof NonCancelableSection) return true;
if (indicator instanceof WrappedProgressIndicator) {
indicator = ((WrappedProgressIndicator)indicator).getOriginalProgressIndicator();
}
else {
break;
}
}
return false;
}
}
@@ -32,6 +32,7 @@ import com.intellij.openapi.components.impl.stores.StorageUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.progress.*;
import com.intellij.openapi.progress.util.ProgressIndicatorUtils;
import com.intellij.openapi.project.*;
import com.intellij.openapi.project.ex.ProjectManagerEx;
import com.intellij.openapi.startup.StartupManager;
@@ -386,8 +387,7 @@ public class ProjectManagerImpl extends ProjectManagerEx implements Disposable {
}
private static boolean canCancelProjectLoading() {
ProgressIndicator indicator = ProgressIndicatorProvider.getGlobalProgressIndicator();
return !(indicator instanceof NonCancelableSection);
return !ProgressIndicatorUtils.isInNonCancelableSection();
}
@Override