Search in sources :

Example 11 with Disposable

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);
}
Also used : Disposable(com.intellij.openapi.Disposable) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) InvocationTargetException(java.lang.reflect.InvocationTargetException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Alarm(com.intellij.util.Alarm) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with Disposable

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;
        }
    });
}
Also used : Disposable(com.intellij.openapi.Disposable) NamespaceContext(org.intellij.lang.xpath.context.NamespaceContext) TestOnly(org.jetbrains.annotations.TestOnly)

Example 13 with Disposable

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);
    }
}
Also used : Disposable(com.intellij.openapi.Disposable) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor)

Example 14 with Disposable

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));
        }
    });
}
Also used : Disposable(com.intellij.openapi.Disposable) TestOnly(org.jetbrains.annotations.TestOnly)

Example 15 with Disposable

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));
        }
    });
}
Also used : Disposable(com.intellij.openapi.Disposable) TestOnly(org.jetbrains.annotations.TestOnly)

Aggregations

Disposable (com.intellij.openapi.Disposable)282 NotNull (org.jetbrains.annotations.NotNull)52 Test (org.junit.Test)25 Project (com.intellij.openapi.project.Project)18 TestOnly (org.jetbrains.annotations.TestOnly)17 ArrayList (java.util.ArrayList)13 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 RelativePoint (com.intellij.ui.awt.RelativePoint)11 Nullable (org.jetbrains.annotations.Nullable)10 Document (com.intellij.openapi.editor.Document)9 File (java.io.File)9 ProcessHandler (com.intellij.execution.process.ProcessHandler)6 Editor (com.intellij.openapi.editor.Editor)6 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)6 VirtualFilePointer (com.intellij.openapi.vfs.pointers.VirtualFilePointer)6 Content (com.intellij.ui.content.Content)6 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)5 Alarm (com.intellij.util.Alarm)5 ActionEvent (java.awt.event.ActionEvent)5 ListSelectionEvent (javax.swing.event.ListSelectionEvent)5