Search in sources :

Example 1 with ApplicationAdapter

use of com.intellij.openapi.application.ApplicationAdapter in project kotlin by JetBrains.

the class LongRunningReadTask method runWithWriteActionPriority.

/**
     * Execute action with immediate stop when write lock is required.
     *
     * {@link ProgressIndicatorUtils#runWithWriteActionPriority(Runnable)}
     *
     * @param indicator
     * @param action
     */
public static void runWithWriteActionPriority(@NotNull final ProgressIndicator indicator, @NotNull final Runnable action) {
    ApplicationAdapter listener = new ApplicationAdapter() {

        @Override
        public void beforeWriteActionStart(Object action) {
            indicator.cancel();
        }
    };
    final Application application = ApplicationManager.getApplication();
    try {
        application.addApplicationListener(listener);
        ProgressManager.getInstance().runProcess(new Runnable() {

            @Override
            public void run() {
                application.runReadAction(action);
            }
        }, indicator);
    } finally {
        application.removeApplicationListener(listener);
    }
}
Also used : ApplicationAdapter(com.intellij.openapi.application.ApplicationAdapter) Application(com.intellij.openapi.application.Application)

Example 2 with ApplicationAdapter

use of com.intellij.openapi.application.ApplicationAdapter in project intellij-community by JetBrains.

the class RefResolveServiceImpl method initListeners.

private void initListeners(@NotNull MessageBus messageBus, @NotNull PsiManager psiManager) {
    messageBus.connect().subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener() {

        @Override
        public void after(@NotNull List<? extends VFileEvent> events) {
            fileCount.set(0);
            List<VirtualFile> files = ContainerUtil.mapNotNull(events, new Function<VFileEvent, VirtualFile>() {

                @Override
                public VirtualFile fun(VFileEvent event) {
                    return event.getFile();
                }
            });
            queue(files, "VFS events " + events.size());
        }
    });
    psiManager.addPsiTreeChangeListener(new PsiTreeChangeAdapter() {

        @Override
        public void childrenChanged(@NotNull PsiTreeChangeEvent event) {
            PsiFile file = event.getFile();
            VirtualFile virtualFile = PsiUtilCore.getVirtualFile(file);
            if (virtualFile != null) {
                queue(Collections.singletonList(virtualFile), event);
            }
        }

        @Override
        public void propertyChanged(@NotNull PsiTreeChangeEvent event) {
            childrenChanged(event);
        }
    });
    messageBus.connect().subscribe(DumbService.DUMB_MODE, new DumbService.DumbModeListener() {

        @Override
        public void enteredDumbMode() {
            disable();
        }

        @Override
        public void exitDumbMode() {
            enable();
        }
    });
    messageBus.connect().subscribe(PowerSaveMode.TOPIC, new PowerSaveMode.Listener() {

        @Override
        public void powerSaveStateChanged() {
            if (PowerSaveMode.isEnabled()) {
                enable();
            } else {
                disable();
            }
        }
    });
    myApplication.addApplicationListener(new ApplicationAdapter() {

        @Override
        public void beforeWriteActionStart(@NotNull Object action) {
            disable();
        }

        @Override
        public void writeActionFinished(@NotNull Object action) {
            enable();
        }

        @Override
        public void applicationExiting() {
            disable();
        }
    }, this);
    VirtualFileManager.getInstance().addVirtualFileManagerListener(new VirtualFileManagerListener() {

        @Override
        public void beforeRefreshStart(boolean asynchronous) {
            disable();
        }

        @Override
        public void afterRefreshFinish(boolean asynchronous) {
            enable();
        }
    }, this);
    HeavyProcessLatch.INSTANCE.addListener(new HeavyProcessLatch.HeavyProcessListener() {

        @Override
        public void processStarted() {
        }

        @Override
        public void processFinished() {
            wakeUp();
        }
    }, this);
}
Also used : NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile) BulkFileListener(com.intellij.openapi.vfs.newvfs.BulkFileListener) PowerSaveMode(com.intellij.ide.PowerSaveMode) Function(com.intellij.util.Function) VFileEvent(com.intellij.openapi.vfs.newvfs.events.VFileEvent) HeavyProcessLatch(com.intellij.util.io.storage.HeavyProcessLatch) ApplicationAdapter(com.intellij.openapi.application.ApplicationAdapter)

Example 3 with ApplicationAdapter

use of com.intellij.openapi.application.ApplicationAdapter 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 4 with ApplicationAdapter

use of com.intellij.openapi.application.ApplicationAdapter in project intellij-community by JetBrains.

the class NoSwingUnderWriteAction method watchForEvents.

static void watchForEvents(Application application) {
    AtomicBoolean reported = new AtomicBoolean();
    IdeEventQueue.getInstance().addPostprocessor(e -> {
        if (application.isWriteAccessAllowed() && reported.compareAndSet(false, true)) {
            LOG.error("AWT events are not allowed inside write action: " + e);
        }
        return true;
    }, application);
    application.addApplicationListener(new ApplicationAdapter() {

        @Override
        public void afterWriteActionFinished(@NotNull Object action) {
            reported.set(false);
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ApplicationAdapter(com.intellij.openapi.application.ApplicationAdapter)

Example 5 with ApplicationAdapter

use of com.intellij.openapi.application.ApplicationAdapter in project intellij-community by JetBrains.

the class ProgressIndicatorUtils method runWithWriteActionPriority.

public static boolean runWithWriteActionPriority(@NotNull Runnable action, @NotNull ProgressIndicator progressIndicator) {
    final ApplicationEx application = (ApplicationEx) ApplicationManager.getApplication();
    if (application.isDispatchThread()) {
        throw new IllegalStateException("Must not call from EDT");
    }
    if (application.isWriteActionPending()) {
        // tryRunReadAction below would just run without really checking if a write action is pending
        if (!progressIndicator.isCanceled())
            progressIndicator.cancel();
        return false;
    }
    final ApplicationAdapter listener = new ApplicationAdapter() {

        @Override
        public void beforeWriteActionStart(@NotNull Object action) {
            if (!progressIndicator.isCanceled())
                progressIndicator.cancel();
        }
    };
    boolean succeededWithAddingListener = application.tryRunReadAction(() -> {
        // Even if writeLock.lock() acquisition is in progress at this point then runProcess will block wanting read action which is
        // also ok as last resort.
        application.addApplicationListener(listener);
    });
    if (!succeededWithAddingListener) {
        // second catch: writeLock.lock() acquisition is in progress or already acquired
        if (!progressIndicator.isCanceled())
            progressIndicator.cancel();
        return false;
    }
    final Ref<Boolean> wasCancelled = new Ref<>();
    try {
        ProgressManager.getInstance().runProcess(() -> {
            try {
                action.run();
            } catch (ProcessCanceledException ignore) {
                wasCancelled.set(Boolean.TRUE);
            }
        }, progressIndicator);
    } finally {
        application.removeApplicationListener(listener);
    }
    return wasCancelled.get() != Boolean.TRUE;
}
Also used : Ref(com.intellij.openapi.util.Ref) ApplicationEx(com.intellij.openapi.application.ex.ApplicationEx) ApplicationAdapter(com.intellij.openapi.application.ApplicationAdapter) NotNull(org.jetbrains.annotations.NotNull) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Aggregations

ApplicationAdapter (com.intellij.openapi.application.ApplicationAdapter)6 Application (com.intellij.openapi.application.Application)3 NotNull (org.jetbrains.annotations.NotNull)3 ApplicationEx (com.intellij.openapi.application.ex.ApplicationEx)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 Ref (com.intellij.openapi.util.Ref)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 PowerSaveMode (com.intellij.ide.PowerSaveMode)1 Disposable (com.intellij.openapi.Disposable)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 ModalityState (com.intellij.openapi.application.ModalityState)1 ApplicationManagerEx (com.intellij.openapi.application.ex.ApplicationManagerEx)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 ProgressManager (com.intellij.openapi.progress.ProgressManager)1 ProgressIndicatorBase (com.intellij.openapi.progress.util.ProgressIndicatorBase)1 EmptyRunnable (com.intellij.openapi.util.EmptyRunnable)1 BulkFileListener (com.intellij.openapi.vfs.newvfs.BulkFileListener)1 NewVirtualFile (com.intellij.openapi.vfs.newvfs.NewVirtualFile)1 VFileEvent (com.intellij.openapi.vfs.newvfs.events.VFileEvent)1 Function (com.intellij.util.Function)1