Search in sources :

Example 6 with AbstractPopup

use of com.intellij.ui.popup.AbstractPopup 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 7 with AbstractPopup

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

the class DocumentationManager method doFetchDocInfo.

private ActionCallback doFetchDocInfo(final DocumentationComponent component, final DocumentationCollector provider, final boolean cancelRequests, final boolean clearHistory) {
    final ActionCallback callback = new ActionCallback();
    myLastAction = callback;
    boolean wasEmpty = component.isEmpty();
    component.startWait();
    if (cancelRequests) {
        myUpdateDocAlarm.cancelAllRequests();
    }
    if (wasEmpty) {
        component.setText(CodeInsightBundle.message("javadoc.fetching.progress"), null, clearHistory);
        final AbstractPopup jbPopup = (AbstractPopup) getDocInfoHint();
        if (jbPopup != null) {
            jbPopup.setDimensionServiceKey(null);
        }
    }
    ModalityState modality = ModalityState.defaultModalityState();
    myUpdateDocAlarm.addRequest(() -> {
        if (myProject.isDisposed())
            return;
        LOG.debug("Started fetching documentation...");
        final Throwable[] ex = new Throwable[1];
        String text = null;
        try {
            text = provider.getDocumentation();
        } catch (Throwable e) {
            LOG.info(e);
            ex[0] = e;
        }
        if (ex[0] != null) {
            //noinspection SSBasedInspection
            SwingUtilities.invokeLater(() -> {
                String message = ex[0] instanceof IndexNotReadyException ? "Documentation is not available until indices are built." : CodeInsightBundle.message("javadoc.external.fetch.error.message");
                component.setText(message, null, true);
                callback.setDone();
            });
            return;
        }
        LOG.debug("Documentation fetched successfully:\n", text);
        final PsiElement element = ApplicationManager.getApplication().runReadAction(new Computable<PsiElement>() {

            @Override
            @Nullable
            public PsiElement compute() {
                return provider.getElement();
            }
        });
        if (element == null) {
            LOG.debug("Element for which documentation was requested is not available anymore");
            return;
        }
        final String documentationText = text;
        PsiDocumentManager.getInstance(myProject).performLaterWhenAllCommitted(() -> {
            if (!element.isValid()) {
                LOG.debug("Element for which documentation was requested is not valid");
                callback.setDone();
                return;
            }
            if (documentationText == null) {
                component.setText(CodeInsightBundle.message("no.documentation.found"), element, true, clearHistory);
            } else if (documentationText.isEmpty()) {
                component.setText(component.getText(), element, true, clearHistory);
            } else {
                component.setData(element, documentationText, clearHistory, provider.getEffectiveExternalUrl(), provider.getRef());
            }
            final AbstractPopup jbPopup = (AbstractPopup) getDocInfoHint();
            if (jbPopup == null) {
                callback.setDone();
                return;
            }
            jbPopup.setDimensionServiceKey(JAVADOC_LOCATION_AND_SIZE);
            jbPopup.setCaption(getTitle(element, false));
            // Set panel name so that it is announced by readers when it gets the focus
            AccessibleContextUtil.setName(component, getTitle(element, false));
            callback.setDone();
        }, modality);
    }, 10);
    return callback;
}
Also used : AbstractPopup(com.intellij.ui.popup.AbstractPopup) ModalityState(com.intellij.openapi.application.ModalityState) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with AbstractPopup

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

the class ShowUsagesAction method rebuildTable.

private void rebuildTable(@NotNull final UsageViewImpl usageView, @NotNull final List<Usage> usages, @NotNull List<UsageNode> nodes, @NotNull final JTable table, @Nullable final JBPopup popup, @NotNull final UsageViewPresentation presentation, @NotNull final RelativePoint popupPosition, boolean findUsagesInProgress, @NotNull AtomicInteger outOfScopeUsages, @NotNull SearchScope searchScope) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    boolean shouldShowMoreSeparator = usages.contains(MORE_USAGES_SEPARATOR);
    if (shouldShowMoreSeparator) {
        nodes.add(MORE_USAGES_SEPARATOR_NODE);
    }
    boolean hasOutsideScopeUsages = usages.contains(USAGES_OUTSIDE_SCOPE_SEPARATOR);
    if (hasOutsideScopeUsages && !shouldShowMoreSeparator) {
        nodes.add(USAGES_OUTSIDE_SCOPE_NODE);
    }
    String title = presentation.getTabText();
    String fullTitle = getFullTitle(usages, title, shouldShowMoreSeparator || hasOutsideScopeUsages, nodes.size() - (shouldShowMoreSeparator || hasOutsideScopeUsages ? 1 : 0), findUsagesInProgress);
    if (popup != null) {
        ((AbstractPopup) popup).setCaption(fullTitle);
    }
    List<UsageNode> data = collectData(usages, nodes, usageView, presentation);
    MyModel tableModel = setTableModel(table, usageView, data, outOfScopeUsages, searchScope);
    List<UsageNode> existingData = tableModel.getItems();
    int row = table.getSelectedRow();
    int newSelection = updateModel(tableModel, existingData, data, row == -1 ? 0 : row);
    if (newSelection < 0 || newSelection >= tableModel.getRowCount()) {
        ScrollingUtil.ensureSelectionExists(table);
        newSelection = table.getSelectedRow();
    } else {
        // do not pre-select the usage under caret by default
        if (newSelection == 0 && table.getModel().getRowCount() > 1) {
            Object valueInTopRow = table.getModel().getValueAt(0, 0);
            if (valueInTopRow instanceof UsageNode && usageView.isOriginUsage(((UsageNode) valueInTopRow).getUsage())) {
                newSelection++;
            }
        }
        table.getSelectionModel().setSelectionInterval(newSelection, newSelection);
    }
    ScrollingUtil.ensureIndexIsVisible(table, newSelection, 0);
    if (popup != null) {
        setSizeAndDimensions(table, popup, popupPosition, data);
    }
}
Also used : AbstractPopup(com.intellij.ui.popup.AbstractPopup) RelativePoint(com.intellij.ui.awt.RelativePoint)

Example 9 with AbstractPopup

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

the class QuickDocUtil method getActiveDocComponent.

@Nullable
public static DocumentationComponent getActiveDocComponent(@NotNull Project project) {
    DocumentationManager documentationManager = DocumentationManager.getInstance(project);
    DocumentationComponent component;
    JBPopup hint = documentationManager.getDocInfoHint();
    if (hint != null) {
        component = (DocumentationComponent) ((AbstractPopup) hint).getComponent();
    } else if (documentationManager.hasActiveDockedDocWindow()) {
        ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(ToolWindowId.DOCUMENTATION);
        Content selectedContent = toolWindow == null ? null : toolWindow.getContentManager().getSelectedContent();
        component = selectedContent == null ? null : (DocumentationComponent) selectedContent.getComponent();
    } else {
        component = null;
    }
    return component;
}
Also used : AbstractPopup(com.intellij.ui.popup.AbstractPopup) ToolWindow(com.intellij.openapi.wm.ToolWindow) Content(com.intellij.ui.content.Content) JBPopup(com.intellij.openapi.ui.popup.JBPopup) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with AbstractPopup

use of com.intellij.ui.popup.AbstractPopup 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

AbstractPopup (com.intellij.ui.popup.AbstractPopup)10 JBPopup (com.intellij.openapi.ui.popup.JBPopup)6 Nullable (org.jetbrains.annotations.Nullable)6 Editor (com.intellij.openapi.editor.Editor)4 Project (com.intellij.openapi.project.Project)4 TargetElementUtil (com.intellij.codeInsight.TargetElementUtil)3 DataManager (com.intellij.ide.DataManager)3 com.intellij.openapi.actionSystem (com.intellij.openapi.actionSystem)3 ApplicationManager (com.intellij.openapi.application.ApplicationManager)3 Logger (com.intellij.openapi.diagnostic.Logger)3 JBPopupFactory (com.intellij.openapi.ui.popup.JBPopupFactory)3 RelativePoint (com.intellij.ui.awt.RelativePoint)3 NotNull (org.jetbrains.annotations.NotNull)3 CodeInsightBundle (com.intellij.codeInsight.CodeInsightBundle)2 LookupManager (com.intellij.codeInsight.lookup.LookupManager)2 FeatureUsageTracker (com.intellij.featureStatistics.FeatureUsageTracker)2 ModalityState (com.intellij.openapi.application.ModalityState)2 ReadAction (com.intellij.openapi.application.ReadAction)2 FileEditor (com.intellij.openapi.fileEditor.FileEditor)2 TextEditor (com.intellij.openapi.fileEditor.TextEditor)2