use of com.intellij.openapi.progress.ProgressIndicator in project intellij-community by JetBrains.
the class BackgroundTaskUtil method executeOnPooledThread.
@NotNull
@CalledInAny
public static ProgressIndicator executeOnPooledThread(@NotNull Consumer<ProgressIndicator> task, @NotNull Disposable parent, @NotNull ModalityState modalityState) {
ProgressIndicator indicator = new EmptyProgressIndicator(modalityState);
Disposable disposable = new Disposable() {
@Override
public void dispose() {
if (indicator.isRunning())
indicator.cancel();
}
};
Disposer.register(parent, disposable);
indicator.start();
ApplicationManager.getApplication().executeOnPooledThread(() -> {
ProgressManager.getInstance().executeProcessUnderProgress(() -> {
try {
task.consume(indicator);
} finally {
indicator.stop();
Disposer.dispose(disposable);
}
}, indicator);
});
return indicator;
}
use of com.intellij.openapi.progress.ProgressIndicator in project intellij-community by JetBrains.
the class ProgressIndicatorUtils method scheduleWithWriteActionPriority.
@NotNull
public static CompletableFuture<?> scheduleWithWriteActionPriority(@NotNull final ProgressIndicator progressIndicator, @NotNull final Executor executor, @NotNull final ReadTask readTask) {
final Application application = ApplicationManager.getApplication();
// invoke later even if on EDT
// to avoid tasks eagerly restarting immediately, allocating many pooled threads
// which get cancelled too soon when a next write action arrives in the same EDT batch
// (can happen when processing multiple VFS events or writing multiple files on save)
// use SwingUtilities instead of application.invokeLater
// to tolerate any immediate modality changes (e.g. https://youtrack.jetbrains.com/issue/IDEA-135180)
CompletableFuture<?> future = new CompletableFuture<>();
//noinspection SSBasedInspection
EdtInvocationManager.getInstance().invokeLater(() -> {
if (application.isDisposed() || progressIndicator.isCanceled()) {
future.complete(null);
return;
}
final ApplicationAdapter listener = new ApplicationAdapter() {
@Override
public void beforeWriteActionStart(@NotNull Object action) {
if (!progressIndicator.isCanceled()) {
progressIndicator.cancel();
readTask.onCanceled(progressIndicator);
}
}
};
application.addApplicationListener(listener);
future.whenComplete((BiConsumer<Object, Throwable>) (o, throwable) -> application.removeApplicationListener(listener));
try {
executor.execute(new Runnable() {
@Override
public void run() {
final ReadTask.Continuation continuation;
try {
continuation = runUnderProgress(progressIndicator, readTask);
} catch (Throwable e) {
future.completeExceptionally(e);
throw e;
}
if (continuation == null) {
future.complete(null);
} else {
application.invokeLater(new Runnable() {
@Override
public void run() {
try {
if (!progressIndicator.isCanceled()) {
continuation.getAction().run();
}
} finally {
future.complete(null);
}
}
@Override
public String toString() {
return "continuation of " + readTask;
}
}, continuation.getModalityState());
}
}
@Override
public String toString() {
return readTask.toString();
}
});
} catch (RuntimeException | Error e) {
application.removeApplicationListener(listener);
future.completeExceptionally(e);
throw e;
}
});
return future;
}
use of com.intellij.openapi.progress.ProgressIndicator in project intellij-community by JetBrains.
the class EditorNotificationsImpl method updateNotifications.
@Override
public void updateNotifications(@NotNull final VirtualFile file) {
UIUtil.invokeLaterIfNeeded(() -> {
ProgressIndicator indicator = getCurrentProgress(file);
if (indicator != null) {
indicator.cancel();
}
file.putUserData(CURRENT_UPDATES, null);
if (myProject.isDisposed() || !file.isValid()) {
return;
}
indicator = new ProgressIndicatorBase();
final ReadTask task = createTask(indicator, file);
if (task == null)
return;
file.putUserData(CURRENT_UPDATES, new WeakReference<>(indicator));
if (ApplicationManager.getApplication().isUnitTestMode()) {
ReadTask.Continuation continuation = task.performInReadAction(indicator);
if (continuation != null) {
continuation.getAction().run();
}
} else {
ProgressIndicatorUtils.scheduleWithWriteActionPriority(indicator, ourExecutor, task);
}
});
}
use of com.intellij.openapi.progress.ProgressIndicator in project intellij-community by JetBrains.
the class TreeUiTest method testBatchUpdate.
public void testBatchUpdate() throws Exception {
buildStructure(myRoot);
myElementUpdate.clear();
final NodeElement[] toExpand = new NodeElement[] { new NodeElement("com"), new NodeElement("jetbrains"), new NodeElement("org"), new NodeElement("xUnit") };
final ActionCallback done = new ActionCallback();
final Ref<ProgressIndicator> indicatorRef = new Ref<>();
final Ref<ActionCallback> ready = new Ref<>();
myElementUpdateHook = new ElementUpdateHook() {
@Override
public void onElementAction(String action, Object element) {
if (new NodeElement("jetbrains").equals(element) && ready.get() == null) {
ActionCallback readyCallback = new ActionCallback();
assertFalse(getBuilder().getUi().isReady());
getBuilder().getReady(this).notify(readyCallback);
ready.set(readyCallback);
}
}
};
invokeLaterIfNeeded(() -> getBuilder().batch(new Progressive() {
@Override
public void run(@NotNull ProgressIndicator indicator) {
indicatorRef.set(indicator);
expandNext(toExpand, 0, indicator, done);
}
}).notify(done));
waitBuilderToCome(o -> done.isProcessed());
assertTrue(done.isDone());
assertNotNull(ready.get());
assertTrue(ready.get().isDone());
assertTree("-/\n" + " -com\n" + " +intellij\n" + " -jetbrains\n" + " +fabrique\n" + " -org\n" + " +eclipse\n" + " -xUnit\n" + " runner\n");
assertFalse(indicatorRef.get().isCanceled());
}
use of com.intellij.openapi.progress.ProgressIndicator in project intellij-community by JetBrains.
the class ProjectOpeningTest method testCancelOnLoadingModules.
public void testCancelOnLoadingModules() throws Exception {
File foo = PlatformTestCase.createTempDir("foo");
Project project = null;
try {
ProjectManagerEx manager = ProjectManagerEx.getInstanceEx();
project = manager.createProject(null, foo.getPath());
project.save();
closeProject(project);
ApplicationManager.getApplication().getMessageBus().connect(getTestRootDisposable()).subscribe(ProjectLifecycleListener.TOPIC, new ProjectLifecycleListener() {
@Override
public void projectComponentsInitialized(@NotNull Project project) {
ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
assertNotNull(indicator);
indicator.cancel();
indicator.checkCanceled();
}
});
project = manager.loadAndOpenProject(foo.getPath());
assertFalse(project.isOpen());
assertTrue(project.isDisposed());
} finally {
closeProject(project);
}
}
Aggregations