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);
}
}
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);
}
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;
}
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);
}
});
}
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;
}
Aggregations