Search in sources :

Example 61 with ProgressIndicator

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;
}
Also used : Disposable(com.intellij.openapi.Disposable) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) CalledInAny(org.jetbrains.annotations.CalledInAny) NotNull(org.jetbrains.annotations.NotNull)

Example 62 with ProgressIndicator

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;
}
Also used : ProgressManager(com.intellij.openapi.progress.ProgressManager) Application(com.intellij.openapi.application.Application) Executor(java.util.concurrent.Executor) ModalityState(com.intellij.openapi.application.ModalityState) CompletableFuture(java.util.concurrent.CompletableFuture) EdtInvocationManager(com.intellij.util.ui.EdtInvocationManager) Disposable(com.intellij.openapi.Disposable) ApplicationEx(com.intellij.openapi.application.ex.ApplicationEx) Nullable(org.jetbrains.annotations.Nullable) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ApplicationAdapter(com.intellij.openapi.application.ApplicationAdapter) BiConsumer(java.util.function.BiConsumer) ApplicationManager(com.intellij.openapi.application.ApplicationManager) ApplicationManagerEx(com.intellij.openapi.application.ex.ApplicationManagerEx) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) PooledThreadExecutor(org.jetbrains.ide.PooledThreadExecutor) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) NotNull(org.jetbrains.annotations.NotNull) CompletableFuture(java.util.concurrent.CompletableFuture) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) ApplicationAdapter(com.intellij.openapi.application.ApplicationAdapter) Application(com.intellij.openapi.application.Application) NotNull(org.jetbrains.annotations.NotNull)

Example 63 with ProgressIndicator

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);
        }
    });
}
Also used : ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ReadTask(com.intellij.openapi.progress.util.ReadTask)

Example 64 with ProgressIndicator

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());
}
Also used : Progressive(com.intellij.openapi.progress.Progressive) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator)

Example 65 with ProgressIndicator

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);
    }
}
Also used : Project(com.intellij.openapi.project.Project) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) File(java.io.File) ProjectManagerEx(com.intellij.openapi.project.ex.ProjectManagerEx)

Aggregations

ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)352 Task (com.intellij.openapi.progress.Task)139 NotNull (org.jetbrains.annotations.NotNull)98 VirtualFile (com.intellij.openapi.vfs.VirtualFile)96 Project (com.intellij.openapi.project.Project)78 File (java.io.File)52 IOException (java.io.IOException)48 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)44 Nullable (org.jetbrains.annotations.Nullable)43 ProgressManager (com.intellij.openapi.progress.ProgressManager)35 List (java.util.List)30 EmptyProgressIndicator (com.intellij.openapi.progress.EmptyProgressIndicator)28 Ref (com.intellij.openapi.util.Ref)27 VcsException (com.intellij.openapi.vcs.VcsException)25 ArrayList (java.util.ArrayList)23 ApplicationManager (com.intellij.openapi.application.ApplicationManager)22 Module (com.intellij.openapi.module.Module)21 Logger (com.intellij.openapi.diagnostic.Logger)20 java.util (java.util)20 Processor (com.intellij.util.Processor)18