use of com.intellij.openapi.Disposable in project intellij-community by JetBrains.
the class EDTGuard method create.
@NotNull
public static <T, O extends Watchable> T create(@NotNull final O target, final ProcessHandler process) {
final Pair<LinkedBlockingQueue<Call>, LinkedBlockingQueue<Call.Result>> queue = Pair.create(new LinkedBlockingQueue<Call>(10), new LinkedBlockingQueue<Call.Result>());
final Thread thread = new Thread("Async Invocation Thread for " + process) {
@Override
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
final Call call = queue.first.take();
if (call != null) {
queue.second.offer(call.invoke());
}
}
} catch (InterruptedException e) {
// break
}
}
};
thread.start();
final AtomicBoolean ref = new AtomicBoolean();
final Disposable d = new Disposable() {
boolean disposed;
@Override
public void dispose() {
if (!disposed) {
disposed = true;
ref.set(true);
thread.interrupt();
}
}
};
process.addProcessListener(new ProcessAdapter() {
@Override
public void processTerminated(ProcessEvent event) {
synchronized (d) {
Disposer.dispose(d);
}
}
@Override
public void processWillTerminate(ProcessEvent event, boolean willBeDestroyed) {
if (!willBeDestroyed) {
synchronized (d) {
Disposer.dispose(d);
}
}
}
});
final Alarm alarm = new Alarm(Alarm.ThreadToUse.POOLED_THREAD, d);
final Alarm alarm2 = new Alarm(Alarm.ThreadToUse.POOLED_THREAD, alarm);
final Runnable watchdog = () -> ref.set(true);
final Runnable ping = new Runnable() {
@Override
public void run() {
synchronized (d) {
if (alarm.isDisposed()) {
return;
}
alarm2.addRequest(watchdog, 200);
try {
ref.set(!target.ping());
} catch (Exception e) {
ref.set(true);
} finally {
alarm2.cancelRequest(watchdog);
alarm.addRequest(this, 500);
}
}
}
};
alarm.addRequest(ping, 500);
final EDTGuard guard = new EDTGuard(target, queue, ref);
final ClassLoader classLoader = target.getClass().getClassLoader();
final Class<?>[] interfaces = target.getClass().getInterfaces();
//noinspection unchecked
return (T) Proxy.newProxyInstance(classLoader, interfaces, guard);
}
use of com.intellij.openapi.Disposable in project intellij-community by JetBrains.
the class TestNamespaceContext method install.
@TestOnly
public static void install(Disposable parent) {
final NamespaceContext old = ContextProvider.DefaultProvider.NULL_NAMESPACE_CONTEXT;
ContextProvider.DefaultProvider.NULL_NAMESPACE_CONTEXT = TestNamespaceContext.INSTANCE;
Disposer.register(parent, new Disposable() {
@Override
public void dispose() {
ContextProvider.DefaultProvider.NULL_NAMESPACE_CONTEXT = old;
}
});
}
use of com.intellij.openapi.Disposable in project intellij-community by JetBrains.
the class BreadcrumbsInitializingActivity method reinitBreadcrumbsComponent.
private static void reinitBreadcrumbsComponent(@NotNull final FileEditorManager fileEditorManager, @NotNull VirtualFile file) {
if (isSuitable(fileEditorManager.getProject(), file)) {
FileEditor[] fileEditors = fileEditorManager.getAllEditors(file);
for (final FileEditor fileEditor : fileEditors) {
if (fileEditor instanceof TextEditor && fileEditor.isValid()) {
Editor editor = ((TextEditor) fileEditor).getEditor();
final BreadcrumbsXmlWrapper existingWrapper = BreadcrumbsXmlWrapper.getBreadcrumbsComponent(editor);
if (existingWrapper != null) {
existingWrapper.queueUpdate();
continue;
}
final BreadcrumbsXmlWrapper wrapper = new BreadcrumbsXmlWrapper(editor);
final JComponent c = wrapper.getComponent();
fileEditorManager.addTopComponent(fileEditor, c);
Disposer.register(fileEditor, new Disposable() {
@Override
public void dispose() {
disposeWrapper(fileEditorManager, fileEditor, wrapper);
}
});
}
}
} else {
removeBreadcrumbs(fileEditorManager, file);
}
}
use of com.intellij.openapi.Disposable in project intellij-community by JetBrains.
the class ExternalResourceManagerExImpl method addTestResource.
@TestOnly
public static void addTestResource(final String url, final String location, Disposable parentDisposable) {
final ExternalResourceManagerExImpl instance = (ExternalResourceManagerExImpl) getInstance();
ApplicationManager.getApplication().runWriteAction(() -> instance.addResource(url, location));
Disposer.register(parentDisposable, new Disposable() {
@Override
public void dispose() {
ApplicationManager.getApplication().runWriteAction(() -> instance.removeResource(url));
}
});
}
use of com.intellij.openapi.Disposable in project intellij-community by JetBrains.
the class ExternalResourceManagerExImpl method registerResourceTemporarily.
@TestOnly
public static void registerResourceTemporarily(final String url, final String location, Disposable disposable) {
ApplicationManager.getApplication().runWriteAction(() -> getInstance().addResource(url, location));
Disposer.register(disposable, new Disposable() {
@Override
public void dispose() {
ApplicationManager.getApplication().runWriteAction(() -> getInstance().removeResource(url));
}
});
}
Aggregations