Search in sources :

Example 1 with PopupUpdateProcessor

use of com.intellij.ui.popup.PopupUpdateProcessor in project intellij-community by JetBrains.

the class ShowImplementationsAction method showImplementations.

protected void showImplementations(@NotNull PsiElement[] impls, @NotNull final Project project, final String text, final Editor editor, final PsiFile file, final PsiElement element, boolean invokedFromEditor, boolean invokedByShortcut) {
    if (impls.length == 0)
        return;
    FeatureUsageTracker.getInstance().triggerFeatureUsed(CODEASSISTS_QUICKDEFINITION_FEATURE);
    if (LookupManager.getInstance(project).getActiveLookup() != null) {
        FeatureUsageTracker.getInstance().triggerFeatureUsed(CODEASSISTS_QUICKDEFINITION_LOOKUP_FEATURE);
    }
    int index = 0;
    if (invokedFromEditor && file != null && impls.length > 1) {
        final VirtualFile virtualFile = file.getVirtualFile();
        final PsiFile containingFile = impls[0].getContainingFile();
        if (virtualFile != null && containingFile != null && virtualFile.equals(containingFile.getVirtualFile())) {
            final PsiFile secondContainingFile = impls[1].getContainingFile();
            if (secondContainingFile != containingFile) {
                index = 1;
            }
        }
    }
    final Ref<UsageView> usageView = new Ref<>();
    final String title = CodeInsightBundle.message("implementation.view.title", text);
    JBPopup popup = SoftReference.dereference(myPopupRef);
    if (popup != null && popup.isVisible() && popup instanceof AbstractPopup) {
        final ImplementationViewComponent component = (ImplementationViewComponent) ((AbstractPopup) popup).getComponent();
        ((AbstractPopup) popup).setCaption(title);
        component.update(impls, index);
        updateInBackground(editor, element, component, title, (AbstractPopup) popup, usageView);
        if (invokedByShortcut) {
            ((AbstractPopup) popup).focusPreferredComponent();
        }
        return;
    }
    final ImplementationViewComponent component = new ImplementationViewComponent(impls, index);
    if (component.hasElementsToShow()) {
        final PopupUpdateProcessor updateProcessor = new PopupUpdateProcessor(project) {

            @Override
            public void updatePopup(Object lookupItemObject) {
                final PsiElement element = lookupItemObject instanceof PsiElement ? (PsiElement) lookupItemObject : DocumentationManager.getInstance(project).getElementFromLookup(editor, file);
                updateElementImplementations(element, editor, project, file);
            }
        };
        popup = JBPopupFactory.getInstance().createComponentPopupBuilder(component, component.getPreferredFocusableComponent()).setProject(project).addListener(updateProcessor).addUserData(updateProcessor).setDimensionServiceKey(project, DocumentationManager.JAVADOC_LOCATION_AND_SIZE, false).setResizable(true).setMovable(true).setRequestFocus(invokedFromEditor && LookupManager.getActiveLookup(editor) == null).setTitle(title).setCouldPin(popup1 -> {
            usageView.set(component.showInUsageView());
            popup1.cancel();
            myTaskRef = null;
            return false;
        }).setCancelCallback(() -> {
            ImplementationsUpdaterTask task = SoftReference.dereference(myTaskRef);
            if (task != null) {
                task.cancelTask();
            }
            return Boolean.TRUE;
        }).createPopup();
        updateInBackground(editor, element, component, title, (AbstractPopup) popup, usageView);
        PopupPositionManager.positionPopupInBestPosition(popup, editor, DataManager.getInstance().getDataContext());
        component.setHint(popup, title);
        myPopupRef = new WeakReference<>(popup);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DocumentationManager(com.intellij.codeInsight.documentation.DocumentationManager) java.util(java.util) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NonNls(org.jetbrains.annotations.NonNls) Computable(com.intellij.openapi.util.Computable) ReadAction(com.intellij.openapi.application.ReadAction) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) CodeInsightBundle(com.intellij.codeInsight.CodeInsightBundle) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PopupUpdateProcessor(com.intellij.ui.popup.PopupUpdateProcessor) ImplementationSearcher(com.intellij.codeInsight.navigation.ImplementationSearcher) FeatureUsageTracker(com.intellij.featureStatistics.FeatureUsageTracker) Project(com.intellij.openapi.project.Project) PopupPositionManager(com.intellij.ui.popup.PopupPositionManager) Logger(com.intellij.openapi.diagnostic.Logger) WeakReference(java.lang.ref.WeakReference) DataManager(com.intellij.ide.DataManager) BackgroundableProcessIndicator(com.intellij.openapi.progress.impl.BackgroundableProcessIndicator) TextEditor(com.intellij.openapi.fileEditor.TextEditor) ProgressManager(com.intellij.openapi.progress.ProgressManager) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) SoftReference(com.intellij.reference.SoftReference) LookupManager(com.intellij.codeInsight.lookup.LookupManager) AbstractPopup(com.intellij.ui.popup.AbstractPopup) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) JBPopup(com.intellij.openapi.ui.popup.JBPopup) BackgroundUpdaterTask(com.intellij.codeInsight.navigation.BackgroundUpdaterTask) TestOnly(org.jetbrains.annotations.TestOnly) Reference(java.lang.ref.Reference) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) UsageView(com.intellij.usages.UsageView) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) SymbolPresentationUtil(com.intellij.psi.presentation.java.SymbolPresentationUtil) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) ImplementationViewComponent(com.intellij.codeInsight.hint.ImplementationViewComponent) Ref(com.intellij.openapi.util.Ref) AbstractPopup(com.intellij.ui.popup.AbstractPopup) ImplementationViewComponent(com.intellij.codeInsight.hint.ImplementationViewComponent) UsageView(com.intellij.usages.UsageView) Ref(com.intellij.openapi.util.Ref) PopupUpdateProcessor(com.intellij.ui.popup.PopupUpdateProcessor) JBPopup(com.intellij.openapi.ui.popup.JBPopup) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement)

Example 2 with PopupUpdateProcessor

use of com.intellij.ui.popup.PopupUpdateProcessor in project intellij-community by JetBrains.

the class DocumentationManager method showJavaDocInfo.

public void showJavaDocInfo(final Editor editor, @Nullable final PsiFile file, boolean requestFocus, @Nullable final Runnable closeCallback) {
    myEditor = editor;
    final Project project = getProject(file);
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    final PsiElement list = ParameterInfoController.findArgumentList(file, editor.getCaretModel().getOffset(), -1);
    PsiElement expressionList = null;
    if (list != null) {
        LookupEx lookup = LookupManager.getInstance(myProject).getActiveLookup();
        if (lookup != null) {
            // take completion variants for documentation then
            expressionList = null;
        } else {
            expressionList = list;
        }
    }
    final PsiElement originalElement = getContextElement(editor, file);
    PsiElement element = assertSameProject(findTargetElement(editor, file));
    if (element == null && expressionList != null) {
        element = expressionList;
    }
    //file == null for text field editor
    if (element == null && file == null)
        return;
    if (element == null) {
        // look if we are within a javadoc comment
        element = assertSameProject(originalElement);
        if (element == null)
            return;
        PsiComment comment = PsiTreeUtil.getParentOfType(element, PsiComment.class);
        if (comment == null)
            return;
        element = comment instanceof PsiDocCommentBase ? ((PsiDocCommentBase) comment).getOwner() : comment.getParent();
        if (element == null)
            return;
    //if (!(element instanceof PsiDocCommentOwner)) return null;
    }
    final PopupUpdateProcessor updateProcessor = new PopupUpdateProcessor(project) {

        @Override
        public void updatePopup(Object lookupIteObject) {
            if (lookupIteObject == null) {
                return;
            }
            if (lookupIteObject instanceof PsiElement) {
                doShowJavaDocInfo((PsiElement) lookupIteObject, false, this, originalElement, closeCallback);
                return;
            }
            DocumentationProvider documentationProvider = getProviderFromElement(file);
            PsiElement element = documentationProvider.getDocumentationElementForLookupItem(PsiManager.getInstance(myProject), lookupIteObject, originalElement);
            if (element == null)
                return;
            if (myEditor != null) {
                final PsiFile file = element.getContainingFile();
                if (file != null) {
                    Editor editor = myEditor;
                    showJavaDocInfo(myEditor, file, false);
                    myEditor = editor;
                }
            } else {
                doShowJavaDocInfo(element, false, this, originalElement, closeCallback);
            }
        }
    };
    doShowJavaDocInfo(element, requestFocus, updateProcessor, originalElement, closeCallback);
}
Also used : Project(com.intellij.openapi.project.Project) PopupUpdateProcessor(com.intellij.ui.popup.PopupUpdateProcessor) LookupEx(com.intellij.codeInsight.lookup.LookupEx) Editor(com.intellij.openapi.editor.Editor)

Example 3 with PopupUpdateProcessor

use of com.intellij.ui.popup.PopupUpdateProcessor in project intellij-community by JetBrains.

the class DocumentationManager method showInPopup.

private void showInPopup(@NotNull final PsiElement element, boolean requestFocus, PopupUpdateProcessor updateProcessor, final PsiElement originalElement, @Nullable final Runnable closeCallback) {
    final DocumentationComponent component = myTestDocumentationComponent == null ? new DocumentationComponent(this) : myTestDocumentationComponent;
    component.setNavigateCallback(psiElement -> {
        final AbstractPopup jbPopup = (AbstractPopup) getDocInfoHint();
        if (jbPopup != null) {
            final String title = getTitle(psiElement, false);
            jbPopup.setCaption(title);
            AccessibleContextUtil.setName(component, title);
        }
    });
    Processor<JBPopup> pinCallback = popup -> {
        createToolWindow(element, originalElement);
        myToolWindow.setAutoHide(false);
        popup.cancel();
        return false;
    };
    ActionListener actionListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            createToolWindow(element, originalElement);
            final JBPopup hint = getDocInfoHint();
            if (hint != null && hint.isVisible())
                hint.cancel();
        }
    };
    List<Pair<ActionListener, KeyStroke>> actions = ContainerUtil.newSmartList();
    AnAction quickDocAction = ActionManager.getInstance().getAction(IdeActions.ACTION_QUICK_JAVADOC);
    for (Shortcut shortcut : quickDocAction.getShortcutSet().getShortcuts()) {
        if (!(shortcut instanceof KeyboardShortcut))
            continue;
        actions.add(Pair.create(actionListener, ((KeyboardShortcut) shortcut).getFirstKeyStroke()));
    }
    boolean hasLookup = LookupManager.getActiveLookup(myEditor) != null;
    final JBPopup hint = JBPopupFactory.getInstance().createComponentPopupBuilder(component, component).setProject(element.getProject()).addListener(updateProcessor).addUserData(updateProcessor).setKeyboardActions(actions).setDimensionServiceKey(myProject, JAVADOC_LOCATION_AND_SIZE, false).setResizable(true).setMovable(true).setRequestFocus(requestFocus).setCancelOnClickOutside(// otherwise selecting lookup items by mouse would close the doc
    !hasLookup).setTitle(getTitle(element, false)).setCouldPin(pinCallback).setModalContext(false).setCancelCallback(() -> {
        myCloseOnSneeze = false;
        if (closeCallback != null) {
            closeCallback.run();
        }
        if (fromQuickSearch()) {
            ((ChooseByNameBase.JPanelProvider) myPreviouslyFocused.getParent()).unregisterHint();
        }
        Disposer.dispose(component);
        myEditor = null;
        myPreviouslyFocused = null;
        return Boolean.TRUE;
    }).setKeyEventHandler(e -> {
        if (myCloseOnSneeze) {
            closeDocHint();
        }
        if (AbstractPopup.isCloseRequest(e) && getDocInfoHint() != null) {
            closeDocHint();
            return true;
        }
        return false;
    }).createPopup();
    component.setHint(hint);
    if (myEditor == null) {
        // subsequent invocation of javadoc popup from completion will have myEditor == null because of cancel invoked, 
        // so reevaluate the editor for proper popup placement
        Lookup lookup = LookupManager.getInstance(myProject).getActiveLookup();
        myEditor = lookup != null ? lookup.getEditor() : null;
    }
    fetchDocInfo(getDefaultCollector(element, originalElement), component);
    myDocInfoHintRef = new WeakReference<>(hint);
    if (fromQuickSearch() && myPreviouslyFocused != null) {
        ((ChooseByNameBase.JPanelProvider) myPreviouslyFocused.getParent()).registerHint(hint);
    }
}
Also used : Language(com.intellij.lang.Language) com.intellij.openapi.util(com.intellij.openapi.util) AccessibleContextUtil(com.intellij.util.ui.accessibility.AccessibleContextUtil) BaseNavigateToSourceAction(com.intellij.ide.actions.BaseNavigateToSourceAction) Lookup(com.intellij.codeInsight.lookup.Lookup) VirtualFile(com.intellij.openapi.vfs.VirtualFile) com.intellij.lang.documentation(com.intellij.lang.documentation) ModalityState(com.intellij.openapi.application.ModalityState) ProjectSettingsService(com.intellij.openapi.roots.ui.configuration.ProjectSettingsService) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) ShowQuickDocInfoAction(com.intellij.codeInsight.documentation.actions.ShowQuickDocInfoAction) Logger(com.intellij.openapi.diagnostic.Logger) Extensions(com.intellij.openapi.extensions.Extensions) PropertiesComponent(com.intellij.ide.util.PropertiesComponent) ScrollingUtil(com.intellij.ui.ScrollingUtil) ParameterInfoController(com.intellij.codeInsight.hint.ParameterInfoController) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) LookupManager(com.intellij.codeInsight.lookup.LookupManager) PreviewManager(com.intellij.openapi.preview.PreviewManager) OrderEntry(com.intellij.openapi.roots.OrderEntry) Content(com.intellij.ui.content.Content) AbstractPopup(com.intellij.ui.popup.AbstractPopup) LookupEx(com.intellij.codeInsight.lookup.LookupEx) BrowserUtil(com.intellij.ide.BrowserUtil) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ServiceManager(com.intellij.openapi.components.ServiceManager) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) IdeFocusManager.getGlobalInstance(com.intellij.openapi.wm.IdeFocusManager.getGlobalInstance) com.intellij.psi(com.intellij.psi) WindowManagerEx(com.intellij.openapi.wm.ex.WindowManagerEx) NotNull(org.jetbrains.annotations.NotNull) java.util(java.util) ActionListener(java.awt.event.ActionListener) NonNls(org.jetbrains.annotations.NonNls) ContainerUtil(com.intellij.util.containers.ContainerUtil) CodeInsightBundle(com.intellij.codeInsight.CodeInsightBundle) PopupUpdateProcessor(com.intellij.ui.popup.PopupUpdateProcessor) AnActionListener(com.intellij.openapi.actionSystem.ex.AnActionListener) ToolWindowId(com.intellij.openapi.wm.ToolWindowId) Project(com.intellij.openapi.project.Project) PopupPositionManager(com.intellij.ui.popup.PopupPositionManager) WeakReference(java.lang.ref.WeakReference) DataManager(com.intellij.ide.DataManager) LibraryUtil(com.intellij.openapi.roots.libraries.LibraryUtil) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ChooseByNameBase(com.intellij.ide.util.gotoByName.ChooseByNameBase) Editor(com.intellij.openapi.editor.Editor) ActionEvent(java.awt.event.ActionEvent) JBPopup(com.intellij.openapi.ui.popup.JBPopup) TestOnly(org.jetbrains.annotations.TestOnly) java.awt(java.awt) LanguageDocumentation(com.intellij.lang.LanguageDocumentation) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) CommandProcessor(com.intellij.openapi.command.CommandProcessor) SymbolPresentationUtil(com.intellij.psi.presentation.java.SymbolPresentationUtil) HintManagerImpl(com.intellij.codeInsight.hint.HintManagerImpl) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) Alarm(com.intellij.util.Alarm) javax.swing(javax.swing) AbstractPopup(com.intellij.ui.popup.AbstractPopup) ActionEvent(java.awt.event.ActionEvent) ActionListener(java.awt.event.ActionListener) AnActionListener(com.intellij.openapi.actionSystem.ex.AnActionListener) Lookup(com.intellij.codeInsight.lookup.Lookup) JBPopup(com.intellij.openapi.ui.popup.JBPopup)

Example 4 with PopupUpdateProcessor

use of com.intellij.ui.popup.PopupUpdateProcessor in project intellij-community by JetBrains.

the class DocumentationManager method showJavaDocInfo.

public void showJavaDocInfo(@NotNull final PsiElement element, final PsiElement original, final boolean requestFocus, @Nullable Runnable closeCallback) {
    if (!element.isValid()) {
        return;
    }
    PopupUpdateProcessor updateProcessor = new PopupUpdateProcessor(element.getProject()) {

        @Override
        public void updatePopup(Object lookupItemObject) {
            if (lookupItemObject instanceof PsiElement) {
                doShowJavaDocInfo((PsiElement) lookupItemObject, requestFocus, this, original, null);
            }
        }
    };
    doShowJavaDocInfo(element, requestFocus, updateProcessor, original, closeCallback);
}
Also used : PopupUpdateProcessor(com.intellij.ui.popup.PopupUpdateProcessor)

Example 5 with PopupUpdateProcessor

use of com.intellij.ui.popup.PopupUpdateProcessor in project intellij-community by JetBrains.

the class FileStructurePopup method show.

public void show() {
    //final long time = System.currentTimeMillis();
    JComponent panel = createCenterPanel();
    MnemonicHelper.init(panel);
    myPopup = JBPopupFactory.getInstance().createComponentPopupBuilder(panel, null).setTitle(myTitle).setResizable(true).setModalContext(false).setFocusable(true).setRequestFocus(true).setMovable(true).setBelongsToGlobalPopupStack(true).setCancelKeyEnabled(false).setDimensionServiceKey(null, getDimensionServiceKey(), true).setCancelCallback(() -> myCanClose).createPopup();
    myTree.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            if (myPopup.isVisible()) {
                final PopupUpdateProcessor updateProcessor = myPopup.getUserData(PopupUpdateProcessor.class);
                if (updateProcessor != null) {
                    final AbstractTreeNode node = getSelectedNode();
                    updateProcessor.updatePopup(node);
                }
            }
        }
    });
    Disposer.register(myPopup, this);
    Disposer.register(myPopup, new Disposable() {

        @Override
        public void dispose() {
            if (!myTreeHasBuilt.isDone()) {
                myTreeHasBuilt.setRejected();
            }
        }
    });
    myTree.getEmptyText().setText("Loading...");
    myPopup.showCenteredInCurrentWindow(myProject);
    ((AbstractPopup) myPopup).setShowHints(true);
    IdeFocusManager.getInstance(myProject).requestFocus(myTree, true);
    Window window = SwingUtilities.windowForComponent(myPopup.getContent());
    WindowFocusListener windowFocusListener = new WindowFocusListener() {

        @Override
        public void windowGainedFocus(WindowEvent e) {
        }

        @Override
        public void windowLostFocus(WindowEvent e) {
            myPopup.cancel();
        }
    };
    window.addWindowFocusListener(windowFocusListener);
    Disposer.register(myPopup, () -> window.removeWindowFocusListener(windowFocusListener));
    ApplicationManager.getApplication().executeOnPooledThread(() -> {
        ApplicationManager.getApplication().runReadAction(() -> myFilteringStructure.rebuild());
        //noinspection SSBasedInspection
        SwingUtilities.invokeLater(() -> {
            myAbstractTreeBuilder.queueUpdate().doWhenDone(() -> {
                myTreeHasBuilt.setDone();
                //noinspection SSBasedInspection
                SwingUtilities.invokeLater(() -> {
                    if (myAbstractTreeBuilder.isDisposed())
                        return;
                    if (selectPsiElement(myInitialPsiElement) == null) {
                        TreeUtil.ensureSelection(myAbstractTreeBuilder.getTree());
                        myAbstractTreeBuilder.revalidateTree();
                    }
                });
            });
            installUpdater();
        });
    });
}
Also used : Disposable(com.intellij.openapi.Disposable) AbstractPopup(com.intellij.ui.popup.AbstractPopup) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) TreeSelectionListener(javax.swing.event.TreeSelectionListener) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) PopupUpdateProcessor(com.intellij.ui.popup.PopupUpdateProcessor)

Aggregations

PopupUpdateProcessor (com.intellij.ui.popup.PopupUpdateProcessor)5 Editor (com.intellij.openapi.editor.Editor)3 Project (com.intellij.openapi.project.Project)3 AbstractPopup (com.intellij.ui.popup.AbstractPopup)3 CodeInsightBundle (com.intellij.codeInsight.CodeInsightBundle)2 TargetElementUtil (com.intellij.codeInsight.TargetElementUtil)2 LookupEx (com.intellij.codeInsight.lookup.LookupEx)2 LookupManager (com.intellij.codeInsight.lookup.LookupManager)2 DataManager (com.intellij.ide.DataManager)2 com.intellij.openapi.actionSystem (com.intellij.openapi.actionSystem)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 JBPopup (com.intellij.openapi.ui.popup.JBPopup)2 JBPopupFactory (com.intellij.openapi.ui.popup.JBPopupFactory)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 DocumentationManager (com.intellij.codeInsight.documentation.DocumentationManager)1 ShowQuickDocInfoAction (com.intellij.codeInsight.documentation.actions.ShowQuickDocInfoAction)1 HintManagerImpl (com.intellij.codeInsight.hint.HintManagerImpl)1 ImplementationViewComponent (com.intellij.codeInsight.hint.ImplementationViewComponent)1 ParameterInfoController (com.intellij.codeInsight.hint.ParameterInfoController)1