Search in sources :

Example 1 with Disposable

use of com.intellij.openapi.Disposable in project intellij-leiningen-plugin by derkork.

the class LeiningenProjectNavigator method addPanel.

private void addPanel() {
    initTree();
    myStructure = new LeiningenProjectStructure(myProject, myProjectsManager, myTree);
    JPanel panel = new LeiningenProjectNavigatorPanel(myProject, myTree);
    final ToolWindowManagerEx manager = ToolWindowManagerEx.getInstanceEx(myProject);
    myToolWindow = manager.registerToolWindow(TOOL_WINDOW_ID, panel, ToolWindowAnchor.RIGHT, myProject, true);
    myToolWindow.setIcon(LeiningenIcons.PROJECT_ICON);
    final ToolWindowManagerAdapter listener = new ToolWindowManagerAdapter() {

        boolean wasVisible = false;

        @Override
        public void stateChanged() {
            if (myToolWindow.isDisposed())
                return;
            boolean visible = myToolWindow.isVisible();
            if (!visible || visible == wasVisible)
                return;
            scheduleStructureUpdate();
            wasVisible = visible;
        }
    };
    manager.addToolWindowManagerListener(listener);
    Disposer.register(myProject, new Disposable() {

        public void dispose() {
            manager.removeToolWindowManagerListener(listener);
        }
    });
    scheduleStructureUpdate();
}
Also used : Disposable(com.intellij.openapi.Disposable) ToolWindowManagerAdapter(com.intellij.openapi.wm.ex.ToolWindowManagerAdapter) ToolWindowManagerEx(com.intellij.openapi.wm.ex.ToolWindowManagerEx)

Example 2 with Disposable

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

the class TerminalToolWindowPanel method initTerminal.

public void initTerminal(final ToolWindow toolWindow) {
    LocalTerminalDirectRunner terminalRunner = LocalTerminalDirectRunner.createTerminalRunner(myProject);
    toolWindow.setToHideOnEmptyContent(true);
    Content content = createTerminalInContentPanel(terminalRunner, toolWindow);
    toolWindow.getContentManager().addContent(content);
    ((ToolWindowManagerEx) ToolWindowManager.getInstance(myProject)).addToolWindowManagerListener(new ToolWindowManagerListener() {

        @Override
        public void toolWindowRegistered(@NotNull String id) {
        }

        @Override
        public void stateChanged() {
            ToolWindow window = ToolWindowManager.getInstance(myProject).getToolWindow(TerminalToolWindowFactory.TOOL_WINDOW_ID);
            if (window != null) {
                boolean visible = window.isVisible();
                if (visible && toolWindow.getContentManager().getContentCount() == 0) {
                    initTerminal(window);
                }
            }
        }
    });
    Disposer.register(myProject, new Disposable() {

        @Override
        public void dispose() {
            if (myTerminalWidget != null) {
                myTerminalWidget.dispose();
                myTerminalWidget = null;
            }
        }
    });
    if (myDockContainer == null) {
        myDockContainer = new TerminalDockContainer(toolWindow);
        Disposer.register(myProject, myDockContainer);
        DockManager.getInstance(myProject).register(myDockContainer);
    }
}
Also used : ToolWindowManagerListener(com.intellij.openapi.wm.ex.ToolWindowManagerListener) Disposable(com.intellij.openapi.Disposable) ToolWindow(com.intellij.openapi.wm.ToolWindow) Content(com.intellij.ui.content.Content) DockableContent(com.intellij.ui.docking.DockableContent) ToolWindowManagerEx(com.intellij.openapi.wm.ex.ToolWindowManagerEx)

Example 3 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 4 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 5 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)

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