Search in sources :

Example 11 with RelativePoint

use of com.intellij.ui.awt.RelativePoint in project intellij-community by JetBrains.

the class IntentionHintComponent method showPopup.

private void showPopup(boolean mouseClick) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    if (myPopup == null || myPopup.isDisposed())
        return;
    if (mouseClick && myPanel.isShowing()) {
        final RelativePoint swCorner = RelativePoint.getSouthWestOf(myPanel);
        final int yOffset = canPlaceBulbOnTheSameLine(myEditor) ? 0 : myEditor.getLineHeight() - (myEditor.isOneLineMode() ? SMALL_BORDER_SIZE : NORMAL_BORDER_SIZE);
        myPopup.show(new RelativePoint(swCorner.getComponent(), new Point(swCorner.getPoint().x, swCorner.getPoint().y + yOffset)));
    } else {
        myPopup.showInBestPositionFor(myEditor);
    }
    myPopupShown = true;
}
Also used : RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) com.intellij.codeInsight.hint(com.intellij.codeInsight.hint) LightweightHint(com.intellij.ui.LightweightHint) RelativePoint(com.intellij.ui.awt.RelativePoint) HintHint(com.intellij.ui.HintHint)

Example 12 with RelativePoint

use of com.intellij.ui.awt.RelativePoint in project intellij-community by JetBrains.

the class ShowUsagesAction method showElementUsages.

private void showElementUsages(final Editor editor, @NotNull final RelativePoint popupPosition, @NotNull final FindUsagesHandler handler, final int maxUsages, @NotNull final FindUsagesOptions options) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    final UsageViewSettings usageViewSettings = UsageViewSettings.getInstance();
    final UsageViewSettings savedGlobalSettings = new UsageViewSettings();
    savedGlobalSettings.loadState(usageViewSettings);
    usageViewSettings.loadState(myUsageViewSettings);
    final Project project = handler.getProject();
    UsageViewManager manager = UsageViewManager.getInstance(project);
    FindUsagesManager findUsagesManager = ((FindManagerImpl) FindManager.getInstance(project)).getFindUsagesManager();
    final UsageViewPresentation presentation = findUsagesManager.createPresentation(handler, options);
    presentation.setDetachedMode(true);
    UsageViewImpl usageView = (UsageViewImpl) manager.createUsageView(UsageTarget.EMPTY_ARRAY, Usage.EMPTY_ARRAY, presentation, null);
    if (editor != null) {
        PsiReference reference = TargetElementUtil.findReference(editor);
        if (reference != null) {
            UsageInfo2UsageAdapter origin = new UsageInfo2UsageAdapter(new UsageInfo(reference));
            usageView.setOriginUsage(origin);
        }
    }
    Disposer.register(usageView, () -> {
        myUsageViewSettings.loadState(usageViewSettings);
        usageViewSettings.loadState(savedGlobalSettings);
    });
    final MyTable table = new MyTable();
    final AsyncProcessIcon processIcon = new AsyncProcessIcon("xxx");
    addUsageNodes(usageView.getRoot(), usageView, new ArrayList<>());
    final List<Usage> usages = new ArrayList<>();
    final Set<UsageNode> visibleNodes = new LinkedHashSet<>();
    final List<UsageNode> data = collectData(usages, visibleNodes, usageView, presentation);
    final AtomicInteger outOfScopeUsages = new AtomicInteger();
    setTableModel(table, usageView, data, outOfScopeUsages, options.searchScope);
    boolean isPreviewMode = Boolean.TRUE == PreviewManager.SERVICE.preview(handler.getProject(), UsagesPreviewPanelProvider.ID, Pair.create(usageView, table), false);
    Runnable itemChosenCallback = prepareTable(table, editor, popupPosition, handler, maxUsages, options, isPreviewMode);
    @Nullable final JBPopup popup = isPreviewMode ? null : createUsagePopup(usages, visibleNodes, handler, editor, popupPosition, maxUsages, usageView, options, table, itemChosenCallback, presentation, processIcon);
    if (popup != null) {
        Disposer.register(popup, usageView);
        // show popup only if find usages takes more than 300ms, otherwise it would flicker needlessly
        Alarm alarm = new Alarm(usageView);
        alarm.addRequest(() -> showPopupIfNeedTo(popup, popupPosition), 300);
    }
    final PingEDT pingEDT = new PingEDT("Rebuild popup in EDT", o -> popup != null && popup.isDisposed(), 100, () -> {
        if (popup != null && popup.isDisposed())
            return;
        final List<UsageNode> nodes = new ArrayList<>();
        List<Usage> copy;
        synchronized (usages) {
            // open up popup as soon as several usages 've been found
            if (popup != null && !popup.isVisible() && (usages.size() <= 1 || !showPopupIfNeedTo(popup, popupPosition))) {
                return;
            }
            addUsageNodes(usageView.getRoot(), usageView, nodes);
            copy = new ArrayList<>(usages);
        }
        rebuildTable(usageView, copy, nodes, table, popup, presentation, popupPosition, !processIcon.isDisposed(), outOfScopeUsages, options.searchScope);
    });
    final MessageBusConnection messageBusConnection = project.getMessageBus().connect(usageView);
    messageBusConnection.subscribe(UsageFilteringRuleProvider.RULES_CHANGED, pingEDT::ping);
    final UsageTarget[] myUsageTarget = { new PsiElement2UsageTargetAdapter(handler.getPsiElement()) };
    Processor<Usage> collect = usage -> {
        if (!UsageViewManagerImpl.isInScope(usage, options.searchScope)) {
            if (outOfScopeUsages.getAndIncrement() == 0) {
                visibleNodes.add(USAGES_OUTSIDE_SCOPE_NODE);
                usages.add(USAGES_OUTSIDE_SCOPE_SEPARATOR);
            }
            return true;
        }
        synchronized (usages) {
            if (visibleNodes.size() >= maxUsages)
                return false;
            if (UsageViewManager.isSelfUsage(usage, myUsageTarget))
                return true;
            UsageNode node = ReadAction.compute(() -> usageView.doAppendUsage(usage));
            usages.add(usage);
            if (node != null) {
                visibleNodes.add(node);
                boolean continueSearch = true;
                if (visibleNodes.size() == maxUsages) {
                    visibleNodes.add(MORE_USAGES_SEPARATOR_NODE);
                    usages.add(MORE_USAGES_SEPARATOR);
                    continueSearch = false;
                }
                pingEDT.ping();
                return continueSearch;
            }
        }
        return true;
    };
    final ProgressIndicator indicator = FindUsagesManager.startProcessUsages(handler, handler.getPrimaryElements(), handler.getSecondaryElements(), collect, options, () -> ApplicationManager.getApplication().invokeLater(() -> {
        Disposer.dispose(processIcon);
        Container parent = processIcon.getParent();
        if (parent != null) {
            parent.remove(processIcon);
            parent.repaint();
        }
        // repaint title
        pingEDT.ping();
        synchronized (usages) {
            if (visibleNodes.isEmpty()) {
                if (usages.isEmpty()) {
                    String text = UsageViewBundle.message("no.usages.found.in", searchScopePresentableName(options));
                    hint(editor, text, handler, popupPosition, maxUsages, options, false);
                    cancel(popup);
                }
            // else all usages filtered out
            } else if (visibleNodes.size() == 1) {
                if (usages.size() == 1) {
                    //the only usage
                    Usage usage = visibleNodes.iterator().next().getUsage();
                    if (usage == USAGES_OUTSIDE_SCOPE_SEPARATOR) {
                        hint(editor, UsageViewManagerImpl.outOfScopeMessage(outOfScopeUsages.get(), options.searchScope), handler, popupPosition, maxUsages, options, true);
                    } else {
                        String message = UsageViewBundle.message("show.usages.only.usage", searchScopePresentableName(options));
                        navigateAndHint(usage, message, handler, popupPosition, maxUsages, options);
                    }
                    cancel(popup);
                } else {
                    assert usages.size() > 1 : usages;
                    // usage view can filter usages down to one
                    Usage visibleUsage = visibleNodes.iterator().next().getUsage();
                    if (areAllUsagesInOneLine(visibleUsage, usages)) {
                        String hint = UsageViewBundle.message("all.usages.are.in.this.line", usages.size(), searchScopePresentableName(options));
                        navigateAndHint(visibleUsage, hint, handler, popupPosition, maxUsages, options);
                        cancel(popup);
                    }
                }
            } else {
                if (popup != null) {
                    String title = presentation.getTabText();
                    boolean shouldShowMoreSeparator = visibleNodes.contains(MORE_USAGES_SEPARATOR_NODE);
                    String fullTitle = getFullTitle(usages, title, shouldShowMoreSeparator, visibleNodes.size() - (shouldShowMoreSeparator ? 1 : 0), false);
                    ((AbstractPopup) popup).setCaption(fullTitle);
                }
            }
        }
    }, project.getDisposed()));
    if (popup != null) {
        Disposer.register(popup, indicator::cancel);
    }
}
Also used : UIUtil(com.intellij.util.ui.UIUtil) AllIcons(com.intellij.icons.AllIcons) MessageType(com.intellij.openapi.ui.MessageType) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ColumnInfo(com.intellij.util.ui.ColumnInfo) FileEditorLocation(com.intellij.openapi.fileEditor.FileEditorLocation) TableCellRenderer(javax.swing.table.TableCellRenderer) ReadAction(com.intellij.openapi.application.ReadAction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncProcessIcon(com.intellij.util.ui.AsyncProcessIcon) Disposer(com.intellij.openapi.util.Disposer) MouseAdapter(java.awt.event.MouseAdapter) FeatureUsageTracker(com.intellij.featureStatistics.FeatureUsageTracker) TextEditor(com.intellij.openapi.fileEditor.TextEditor) com.intellij.usages.impl(com.intellij.usages.impl) HintUtil(com.intellij.codeInsight.hint.HintUtil) FindManagerImpl(com.intellij.find.impl.FindManagerImpl) PreviewManager(com.intellij.openapi.preview.PreviewManager) PsiReference(com.intellij.psi.PsiReference) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) AbstractPopup(com.intellij.ui.popup.AbstractPopup) KeyEvent(java.awt.event.KeyEvent) FileEditor(com.intellij.openapi.fileEditor.FileEditor) KeymapUtil(com.intellij.openapi.keymap.KeymapUtil) com.intellij.ui(com.intellij.ui) HintUpdateSupply(com.intellij.ui.popup.HintUpdateSupply) FindManager(com.intellij.find.FindManager) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) XmlStringUtil(com.intellij.xml.util.XmlStringUtil) FindSettings(com.intellij.find.FindSettings) NotNull(org.jetbrains.annotations.NotNull) RelativePoint(com.intellij.ui.awt.RelativePoint) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) UsagesPreviewPanelProvider(com.intellij.find.UsagesPreviewPanelProvider) NonNls(org.jetbrains.annotations.NonNls) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UsageInfo(com.intellij.usageView.UsageInfo) SearchScope(com.intellij.psi.search.SearchScope) KeyAdapter(java.awt.event.KeyAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) Comparing(com.intellij.openapi.util.Comparing) com.intellij.find.findUsages(com.intellij.find.findUsages) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) ListTableModel(com.intellij.util.ui.ListTableModel) DataManager(com.intellij.ide.DataManager) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) PlatformIcons(com.intellij.util.PlatformIcons) UsageFilteringRuleProvider(com.intellij.usages.rules.UsageFilteringRuleProvider) StringUtil(com.intellij.openapi.util.text.StringUtil) TableColumn(javax.swing.table.TableColumn) com.intellij.usages(com.intellij.usages) AsyncEditorLoader(com.intellij.openapi.fileEditor.impl.text.AsyncEditorLoader) ModelDiff(com.intellij.ide.util.gotoByName.ModelDiff) Editor(com.intellij.openapi.editor.Editor) PopupUtil(com.intellij.openapi.ui.popup.util.PopupUtil) JBPopup(com.intellij.openapi.ui.popup.JBPopup) UsageViewUtil(com.intellij.usageView.UsageViewUtil) MouseEvent(java.awt.event.MouseEvent) java.awt(java.awt) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) JBTable(com.intellij.ui.table.JBTable) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) Pair(com.intellij.openapi.util.Pair) TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) UsageViewBundle(com.intellij.usageView.UsageViewBundle) Navigatable(com.intellij.pom.Navigatable) HintManager(com.intellij.codeInsight.hint.HintManager) TransactionGuard(com.intellij.openapi.application.TransactionGuard) Alarm(com.intellij.util.Alarm) javax.swing(javax.swing) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) JBPopup(com.intellij.openapi.ui.popup.JBPopup) UsageInfo(com.intellij.usageView.UsageInfo) AbstractPopup(com.intellij.ui.popup.AbstractPopup) AsyncProcessIcon(com.intellij.util.ui.AsyncProcessIcon) PsiReference(com.intellij.psi.PsiReference) FindManagerImpl(com.intellij.find.impl.FindManagerImpl) Project(com.intellij.openapi.project.Project) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Alarm(com.intellij.util.Alarm) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with RelativePoint

use of com.intellij.ui.awt.RelativePoint in project intellij-community by JetBrains.

the class ShowUsagesAction method setSizeAndDimensions.

private void setSizeAndDimensions(@NotNull JTable table, @NotNull JBPopup popup, @NotNull RelativePoint popupPosition, @NotNull List<UsageNode> data) {
    JComponent content = popup.getContent();
    Window window = SwingUtilities.windowForComponent(content);
    Dimension d = window.getSize();
    int width = calcMaxWidth(table);
    width = (int) Math.max(d.getWidth(), width);
    Dimension headerSize = ((AbstractPopup) popup).getHeaderPreferredSize();
    width = Math.max((int) headerSize.getWidth(), width);
    width = Math.max(myWidth, width);
    if (myWidth == -1)
        myWidth = width;
    int newWidth = Math.max(width, d.width + width - myWidth);
    myWidth = newWidth;
    int rowsToShow = Math.min(30, data.size());
    Dimension dimension = new Dimension(newWidth, table.getRowHeight() * rowsToShow);
    Rectangle rectangle = fitToScreen(dimension, popupPosition, table);
    if (!data.isEmpty()) {
        ScrollingUtil.ensureSelectionExists(table);
    }
    table.setSize(rectangle.getSize());
    //table.setPreferredSize(dimension);
    //table.setMaximumSize(dimension);
    //table.setPreferredScrollableViewportSize(dimension);
    Dimension footerSize = ((AbstractPopup) popup).getFooterPreferredSize();
    int footer = footerSize.height;
    int footerBorder = footer == 0 ? 0 : 1;
    Insets insets = ((AbstractPopup) popup).getPopupBorder().getBorderInsets(content);
    rectangle.height += headerSize.height + footer + footerBorder + insets.top + insets.bottom;
    ScreenUtil.fitToScreen(rectangle);
    Dimension newDim = rectangle.getSize();
    window.setBounds(rectangle);
    window.setMinimumSize(newDim);
    window.setMaximumSize(newDim);
    window.validate();
    window.repaint();
}
Also used : AbstractPopup(com.intellij.ui.popup.AbstractPopup) RelativePoint(com.intellij.ui.awt.RelativePoint)

Example 14 with RelativePoint

use of com.intellij.ui.awt.RelativePoint in project intellij-community by JetBrains.

the class ShowUsagesAction method fitToScreen.

private static Rectangle fitToScreen(@NotNull Dimension newDim, @NotNull RelativePoint popupPosition, JTable table) {
    Rectangle rectangle = new Rectangle(popupPosition.getScreenPoint(), newDim);
    ScreenUtil.fitToScreen(rectangle);
    if (rectangle.getHeight() != newDim.getHeight()) {
        int newHeight = (int) rectangle.getHeight();
        int roundedHeight = newHeight - newHeight % table.getRowHeight();
        rectangle.setSize((int) rectangle.getWidth(), Math.max(roundedHeight, table.getRowHeight()));
    }
    return rectangle;
}
Also used : RelativePoint(com.intellij.ui.awt.RelativePoint)

Example 15 with RelativePoint

use of com.intellij.ui.awt.RelativePoint in project intellij-community by JetBrains.

the class ShowUsagesAction method prepareTable.

@NotNull
private Runnable prepareTable(final MyTable table, final Editor editor, final RelativePoint popupPosition, final FindUsagesHandler handler, final int maxUsages, @NotNull final FindUsagesOptions options, final boolean previewMode) {
    SpeedSearchBase<JTable> speedSearch = new MySpeedSearch(table);
    speedSearch.setComparator(new SpeedSearchComparator(false));
    table.setRowHeight(PlatformIcons.CLASS_ICON.getIconHeight() + 2);
    table.setShowGrid(false);
    table.setShowVerticalLines(false);
    table.setShowHorizontalLines(false);
    table.setTableHeader(null);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
    table.setIntercellSpacing(new Dimension(0, 0));
    final AtomicReference<List<Object>> selectedUsages = new AtomicReference<>();
    final AtomicBoolean moreUsagesSelected = new AtomicBoolean();
    final AtomicBoolean outsideScopeUsagesSelected = new AtomicBoolean();
    table.getSelectionModel().addListSelectionListener(e -> {
        selectedUsages.set(null);
        outsideScopeUsagesSelected.set(false);
        moreUsagesSelected.set(false);
        List<Object> usages = null;
        for (int i : table.getSelectedRows()) {
            Object value = table.getValueAt(i, 0);
            if (value instanceof UsageNode) {
                Usage usage = ((UsageNode) value).getUsage();
                if (usage == USAGES_OUTSIDE_SCOPE_SEPARATOR) {
                    outsideScopeUsagesSelected.set(true);
                    usages = null;
                    break;
                } else if (usage == MORE_USAGES_SEPARATOR) {
                    moreUsagesSelected.set(true);
                    usages = null;
                    break;
                } else {
                    if (usages == null)
                        usages = new ArrayList<>();
                    usages.add(usage instanceof UsageInfo2UsageAdapter ? ((UsageInfo2UsageAdapter) usage).getUsageInfo().copy() : usage);
                }
            }
        }
        selectedUsages.set(usages);
    });
    final Runnable itemChosenCallback = () -> {
        if (moreUsagesSelected.get()) {
            appendMoreUsages(editor, popupPosition, handler, maxUsages, options);
            return;
        }
        if (outsideScopeUsagesSelected.get()) {
            options.searchScope = GlobalSearchScope.projectScope(handler.getProject());
            showElementUsages(editor, popupPosition, handler, maxUsages, options);
            return;
        }
        List<Object> usages = selectedUsages.get();
        if (usages != null) {
            for (Object usage : usages) {
                if (usage instanceof UsageInfo) {
                    UsageViewUtil.navigateTo((UsageInfo) usage, true);
                } else if (usage instanceof Navigatable) {
                    ((Navigatable) usage).navigate(true);
                }
            }
        }
    };
    if (previewMode) {
        table.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseReleased(MouseEvent e) {
                if (UIUtil.isActionClick(e, MouseEvent.MOUSE_RELEASED) && !UIUtil.isSelectionButtonDown(e) && !e.isConsumed()) {
                    itemChosenCallback.run();
                }
            }
        });
        table.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    itemChosenCallback.run();
                }
            }
        });
    }
    return itemChosenCallback;
}
Also used : MouseEvent(java.awt.event.MouseEvent) KeyAdapter(java.awt.event.KeyAdapter) MouseAdapter(java.awt.event.MouseAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) RelativePoint(com.intellij.ui.awt.RelativePoint) Navigatable(com.intellij.pom.Navigatable) KeyEvent(java.awt.event.KeyEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) List(java.util.List) UsageInfo(com.intellij.usageView.UsageInfo) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

RelativePoint (com.intellij.ui.awt.RelativePoint)127 Project (com.intellij.openapi.project.Project)16 Balloon (com.intellij.openapi.ui.popup.Balloon)15 JBPopup (com.intellij.openapi.ui.popup.JBPopup)15 NotNull (org.jetbrains.annotations.NotNull)15 MouseEvent (java.awt.event.MouseEvent)12 Nullable (org.jetbrains.annotations.Nullable)10 Editor (com.intellij.openapi.editor.Editor)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)9 PsiElement (com.intellij.psi.PsiElement)8 JBList (com.intellij.ui.components.JBList)8 Disposable (com.intellij.openapi.Disposable)7 BalloonBuilder (com.intellij.openapi.ui.popup.BalloonBuilder)7 BaseListPopupStep (com.intellij.openapi.ui.popup.util.BaseListPopupStep)7 ArrayList (java.util.ArrayList)7 JBPopupFactory (com.intellij.openapi.ui.popup.JBPopupFactory)6 ListPopup (com.intellij.openapi.ui.popup.ListPopup)6 PsiFile (com.intellij.psi.PsiFile)5 HintHint (com.intellij.ui.HintHint)5 DumbAwareAction (com.intellij.openapi.project.DumbAwareAction)4