Example usage for com.intellij.openapi.ui.popup JBPopup isDisposed

List of usage examples for com.intellij.openapi.ui.popup JBPopup isDisposed

Introduction

In this page you can find the example usage for com.intellij.openapi.ui.popup JBPopup isDisposed.

Prototype

boolean isDisposed();

Source Link

Usage

From source file:com.antoine.ideaplugin.greenrobot.ShowUsagesAction.java

License:Apache License

private void showElementUsages(@NotNull final FindUsagesHandler handler, final Editor editor,
        @NotNull final RelativePoint popupPosition, 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();/*  ww  w  . ja va2 s.c o  m*/
    final UsageViewPresentation presentation = findUsagesManager.createPresentation(handler, options);
    presentation.setDetachedMode(true);
    final UsageViewImpl usageView = (UsageViewImpl) manager.createUsageView(UsageTarget.EMPTY_ARRAY,
            Usage.EMPTY_ARRAY, presentation, null);

    Disposer.register(usageView, new Disposable() {
        @Override
        public void dispose() {
            myUsageViewSettings.loadState(usageViewSettings);
            usageViewSettings.loadState(savedGlobalSettings);
        }
    });

    final List<Usage> usages = new ArrayList<Usage>();
    final Set<UsageNode> visibleNodes = new LinkedHashSet<UsageNode>();
    UsageInfoToUsageConverter.TargetElementsDescriptor descriptor = new UsageInfoToUsageConverter.TargetElementsDescriptor(
            handler.getPrimaryElements(), handler.getSecondaryElements());

    final MyTable table = new MyTable();
    final AsyncProcessIcon processIcon = new AsyncProcessIcon("xxx");
    boolean hadMoreSeparator = visibleNodes.remove(MORE_USAGES_SEPARATOR_NODE);
    if (hadMoreSeparator) {
        usages.add(MORE_USAGES_SEPARATOR);
        visibleNodes.add(MORE_USAGES_SEPARATOR_NODE);
    }

    addUsageNodes(usageView.getRoot(), usageView, new ArrayList<UsageNode>());

    TableScrollingUtil.installActions(table);

    final List<UsageNode> data = collectData(usages, visibleNodes, usageView, presentation);
    setTableModel(table, usageView, data);

    SpeedSearchBase<JTable> speedSearch = new MySpeedSearch(table);
    speedSearch.setComparator(new SpeedSearchComparator(false));

    final JBPopup popup = createUsagePopup(usages, descriptor, visibleNodes, handler, editor, popupPosition,
            maxUsages, usageView, options, table, presentation, processIcon, hadMoreSeparator);

    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(new Runnable() {
        @Override
        public void run() {
            showPopupIfNeedTo(popup, popupPosition);
        }
    }, 300);

    final PingEDT pingEDT = new PingEDT("Rebuild popup in EDT", new Condition<Object>() {
        @Override
        public boolean value(Object o) {
            return popup.isDisposed();
        }
    }, 100, new Runnable() {
        @Override
        public void run() {
            if (popup.isDisposed())
                return;

            final List<UsageNode> nodes = new ArrayList<UsageNode>();
            List<Usage> copy;
            synchronized (usages) {
                // open up popup as soon as several usages 've been found
                if (!popup.isVisible() && (usages.size() <= 1 || !showPopupIfNeedTo(popup, popupPosition))) {
                    return;
                }
                addUsageNodes(usageView.getRoot(), usageView, nodes);
                copy = new ArrayList<Usage>(usages);
            }

            rebuildPopup(usageView, copy, nodes, table, popup, presentation, popupPosition,
                    !processIcon.isDisposed());
        }
    });

    final MessageBusConnection messageBusConnection = project.getMessageBus().connect(usageView);
    messageBusConnection.subscribe(UsageFilteringRuleProvider.RULES_CHANGED, new Runnable() {
        @Override
        public void run() {
            pingEDT.ping();
        }
    });

    Processor<Usage> collect = new Processor<Usage>() {
        private final UsageTarget[] myUsageTarget = {
                new PsiElement2UsageTargetAdapter(handler.getPsiElement()) };

        @Override
        public boolean process(@NotNull Usage usage) {
            synchronized (usages) {
                if (visibleNodes.size() >= maxUsages)
                    return false;
                if (UsageViewManager.isSelfUsage(usage, myUsageTarget)) {
                    return true;
                }

                Usage usageToAdd = transform(usage);
                if (usageToAdd == null)
                    return true;

                UsageNode node = usageView.doAppendUsage(usageToAdd);
                usages.add(usageToAdd);
                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, new Runnable() {
                @Override
                public void run() {
                    ApplicationManager.getApplication().invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            Disposer.dispose(processIcon);
                            Container parent = processIcon.getParent();
                            parent.remove(processIcon);
                            parent.repaint();
                            pingEDT.ping(); // repaint title
                            synchronized (usages) {
                                if (visibleNodes.isEmpty()) {
                                    if (usages.isEmpty()) {
                                        String text = UsageViewBundle.message("no.usages.found.in",
                                                searchScopePresentableName(options, project));
                                        showHint(text, editor, popupPosition, handler, maxUsages, options);
                                        popup.cancel();
                                    } else {
                                        // all usages filtered out
                                    }
                                } else if (visibleNodes.size() == 1) {
                                    if (usages.size() == 1) {
                                        //the only usage
                                        Usage usage = visibleNodes.iterator().next().getUsage();
                                        usage.navigate(true);
                                        //String message = UsageViewBundle.message("show.usages.only.usage", searchScopePresentableName(options, project));
                                        //navigateAndHint(usage, message, handler, popupPosition, maxUsages, options);
                                        popup.cancel();
                                    } 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, project));
                                            navigateAndHint(visibleUsage, hint, handler, popupPosition,
                                                    maxUsages, options);
                                            popup.cancel();
                                        }
                                    }
                                } else {
                                    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());
                }
            });
    Disposer.register(popup, new Disposable() {
        @Override
        public void dispose() {
            indicator.cancel();
        }
    });
}

From source file:com.antoine.ideaplugin.greenrobot.ShowUsagesAction.java

License:Apache License

private static boolean showPopupIfNeedTo(@NotNull JBPopup popup, @NotNull RelativePoint popupPosition) {
    if (!popup.isDisposed() && !popup.isVisible()) {
        popup.show(popupPosition);/*from w w w .  jav a2  s  .c  o  m*/
        return true;
    } else {
        return false;
    }
}

From source file:com.intellij.codeInsight.daemon.impl.HectorComponent.java

License:Apache License

public void showComponent(RelativePoint point) {
    final JBPopup hector = JBPopupFactory.getInstance().createComponentPopupBuilder(this, this)
            .setRequestFocus(true).setMovable(true).setCancelCallback(new Computable<Boolean>() {
                @Override//  ww w .  ja v  a  2 s  .c  o  m
                public Boolean compute() {
                    for (HectorComponentPanel additionalPanel : myAdditionalPanels) {
                        if (!additionalPanel.canClose()) {
                            return Boolean.FALSE;
                        }
                    }
                    onClose();
                    return Boolean.TRUE;
                }
            }).createPopup();
    Disposer.register(myFile.getProject(), new Disposable() {
        @Override
        public void dispose() {
            final JBPopup oldHector = getOldHector();
            if (oldHector != null && !oldHector.isDisposed()) {
                Disposer.dispose(oldHector);
            }
            Disposer.dispose(hector);
        }
    });
    final JBPopup oldHector = getOldHector();
    if (oldHector != null) {
        oldHector.cancel();
    } else {
        myHectorRef = new WeakReference<JBPopup>(hector);
        hector.show(point);
    }
}

From source file:com.intellij.find.actions.ShowUsagesAction.java

License:Apache License

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();//from w  w w.j a  v a2  s  .c om
    final UsageViewPresentation presentation = findUsagesManager.createPresentation(handler, options);
    presentation.setDetachedMode(true);
    final UsageViewImpl usageView = (UsageViewImpl) manager.createUsageView(UsageTarget.EMPTY_ARRAY,
            Usage.EMPTY_ARRAY, presentation, null);

    Disposer.register(usageView, new Disposable() {
        @Override
        public void dispose() {
            myUsageViewSettings.loadState(usageViewSettings);
            usageViewSettings.loadState(savedGlobalSettings);
        }
    });
    final AtomicInteger outOfScopeUsages = new AtomicInteger();

    final List<Usage> usages = new ArrayList<Usage>();
    final Set<UsageNode> visibleNodes = new LinkedHashSet<UsageNode>();

    final MyTable table = new MyTable();
    final AsyncProcessIcon processIcon = new AsyncProcessIcon("xxx");

    addUsageNodes(usageView.getRoot(), usageView, new ArrayList<UsageNode>());

    TableScrollingUtil.installActions(table);

    final List<UsageNode> data = collectData(usages, visibleNodes, usageView, presentation);
    setTableModel(table, usageView, data, outOfScopeUsages, options.searchScope);

    boolean isPreviewMode = 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(new Runnable() {
            @Override
            public void run() {
                showPopupIfNeedTo(popup, popupPosition);
            }
        }, 300);
    }

    final PingEDT pingEDT = new PingEDT("Rebuild popup in EDT", new Condition<Object>() {
        @Override
        public boolean value(Object o) {
            return popup != null && popup.isDisposed();
        }
    }, 100, new Runnable() {
        @Override
        public void run() {
            if (popup != null && popup.isDisposed())
                return;

            final List<UsageNode> nodes = new ArrayList<UsageNode>();
            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<Usage>(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, new Runnable() {
        @Override
        public void run() {
            pingEDT.ping();
        }
    });

    final UsageTarget[] myUsageTarget = { new PsiElement2UsageTargetAdapter(handler.getPsiElement()) };
    Processor<Usage> collect = new Processor<Usage>() {
        @Override
        public boolean process(@NotNull final Usage 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 = ApplicationManager.getApplication().runReadAction(new Computable<UsageNode>() {
                    @Override
                    public UsageNode compute() {
                        return 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, new Runnable() {
                @Override
                public void run() {
                    ApplicationManager.getApplication().invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            Disposer.dispose(processIcon);
                            Container parent = processIcon.getParent();
                            if (parent != null) {
                                parent.remove(processIcon);
                                parent.repaint();
                            }
                            pingEDT.ping(); // repaint title
                            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, new Disposable() {
            @Override
            public void dispose() {
                indicator.cancel();
            }
        });
    }
}

From source file:com.kgmyshin.ideaplugin.eventbus.ShowUsagesAction.java

License:Apache License

private void showElementUsages(@NotNull final FindUsagesHandler handler, final Editor editor,
        @NotNull final RelativePoint popupPosition, 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();//w  w  w  .jav a  2s . c om
    final UsageViewPresentation presentation = findUsagesManager.createPresentation(handler, options);
    presentation.setDetachedMode(true);
    final UsageViewImpl usageView = (UsageViewImpl) manager.createUsageView(UsageTarget.EMPTY_ARRAY,
            Usage.EMPTY_ARRAY, presentation, null);

    Disposer.register(usageView, new Disposable() {
        @Override
        public void dispose() {
            myUsageViewSettings.loadState(usageViewSettings);
            usageViewSettings.loadState(savedGlobalSettings);
        }
    });

    final List<Usage> usages = new ArrayList<Usage>();
    final Set<UsageNode> visibleNodes = new LinkedHashSet<UsageNode>();
    UsageInfoToUsageConverter.TargetElementsDescriptor descriptor = new UsageInfoToUsageConverter.TargetElementsDescriptor(
            handler.getPrimaryElements(), handler.getSecondaryElements());

    final MyTable table = new MyTable();
    final AsyncProcessIcon processIcon = new AsyncProcessIcon("xxx");
    boolean hadMoreSeparator = visibleNodes.remove(MORE_USAGES_SEPARATOR_NODE);
    if (hadMoreSeparator) {
        usages.add(MORE_USAGES_SEPARATOR);
        visibleNodes.add(MORE_USAGES_SEPARATOR_NODE);
    }

    addUsageNodes(usageView.getRoot(), usageView, new ArrayList<UsageNode>());

    TableScrollingUtil.installActions(table);

    final List<UsageNode> data = collectData(usages, visibleNodes, usageView, presentation);
    setTableModel(table, usageView, data);

    SpeedSearchBase<JTable> speedSearch = new MySpeedSearch(table);
    speedSearch.setComparator(new SpeedSearchComparator(false));

    final JBPopup popup = createUsagePopup(usages, descriptor, visibleNodes, handler, editor, popupPosition,
            maxUsages, usageView, options, table, presentation, processIcon, hadMoreSeparator);

    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(new Runnable() {
        @Override
        public void run() {
            showPopupIfNeedTo(popup, popupPosition);
        }
    }, 300);

    final PingEDT pingEDT = new PingEDT("Rebuild popup in EDT", new Condition<Object>() {
        @Override
        public boolean value(Object o) {
            return popup.isDisposed();
        }
    }, 100, new Runnable() {
        @Override
        public void run() {
            if (popup.isDisposed())
                return;

            final List<UsageNode> nodes = new ArrayList<UsageNode>();
            List<Usage> copy;
            synchronized (usages) {
                // open up popup as soon as several usages 've been found
                if (!popup.isVisible() && (usages.size() <= 1 || !showPopupIfNeedTo(popup, popupPosition))) {
                    return;
                }
                addUsageNodes(usageView.getRoot(), usageView, nodes);
                copy = new ArrayList<Usage>(usages);
            }

            rebuildPopup(usageView, copy, nodes, table, popup, presentation, popupPosition,
                    !processIcon.isDisposed());
        }
    });

    final MessageBusConnection messageBusConnection = project.getMessageBus().connect(usageView);
    messageBusConnection.subscribe(UsageFilteringRuleProvider.RULES_CHANGED, new Runnable() {
        @Override
        public void run() {
            pingEDT.ping();
        }
    });

    Processor<Usage> collect = new Processor<Usage>() {
        private final UsageTarget[] myUsageTarget = {
                new PsiElement2UsageTargetAdapter(handler.getPsiElement()) };

        @Override
        public boolean process(@NotNull Usage usage) {
            synchronized (usages) {
                if (!filter.shouldShow(usage))
                    return true;
                if (visibleNodes.size() >= maxUsages)
                    return false;
                if (UsageViewManager.isSelfUsage(usage, myUsageTarget)) {
                    return true;
                }

                Usage usageToAdd = transform(usage);
                if (usageToAdd == null)
                    return true;

                UsageNode node = usageView.doAppendUsage(usageToAdd);
                usages.add(usageToAdd);
                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, new Runnable() {
                @Override
                public void run() {
                    ApplicationManager.getApplication().invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            Disposer.dispose(processIcon);
                            Container parent = processIcon.getParent();
                            parent.remove(processIcon);
                            parent.repaint();
                            pingEDT.ping(); // repaint title
                            synchronized (usages) {
                                if (visibleNodes.isEmpty()) {
                                    if (usages.isEmpty()) {
                                        String text = UsageViewBundle.message("no.usages.found.in",
                                                searchScopePresentableName(options, project));
                                        showHint(text, editor, popupPosition, handler, maxUsages, options);
                                        popup.cancel();
                                    } else {
                                        // all usages filtered out
                                    }
                                } else if (visibleNodes.size() == 1) {
                                    if (usages.size() == 1) {
                                        //the only usage
                                        Usage usage = visibleNodes.iterator().next().getUsage();
                                        usage.navigate(true);
                                        //String message = UsageViewBundle.message("show.usages.only.usage", searchScopePresentableName(options, project));
                                        //navigateAndHint(usage, message, handler, popupPosition, maxUsages, options);
                                        popup.cancel();
                                    } 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, project));
                                            navigateAndHint(visibleUsage, hint, handler, popupPosition,
                                                    maxUsages, options);
                                            popup.cancel();
                                        }
                                    }
                                } else {
                                    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());
                }
            });
    Disposer.register(popup, new Disposable() {
        @Override
        public void dispose() {
            indicator.cancel();
        }
    });
}

From source file:com.squareup.ideaplugin.dagger.ShowUsagesAction.java

License:Apache License

private void showElementUsages(@NotNull final FindUsagesHandler handler, final Editor editor,
        @NotNull final RelativePoint popupPosition, 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();/*from  w  ww.  jav a2 s.c  o m*/
    final UsageViewPresentation presentation = findUsagesManager.createPresentation(handler, options);
    presentation.setDetachedMode(true);
    final UsageViewImpl usageView = (UsageViewImpl) manager.createUsageView(UsageTarget.EMPTY_ARRAY,
            Usage.EMPTY_ARRAY, presentation, null);

    Disposer.register(usageView, new Disposable() {
        @Override
        public void dispose() {
            myUsageViewSettings.loadState(usageViewSettings);
            usageViewSettings.loadState(savedGlobalSettings);
        }
    });

    final List<Usage> usages = new ArrayList<Usage>();
    final Set<UsageNode> visibleNodes = new LinkedHashSet<UsageNode>();
    UsageInfoToUsageConverter.TargetElementsDescriptor descriptor = new UsageInfoToUsageConverter.TargetElementsDescriptor(
            handler.getPrimaryElements(), handler.getSecondaryElements());

    final MyTable table = new MyTable();
    final AsyncProcessIcon processIcon = new AsyncProcessIcon("xxx");
    boolean hadMoreSeparator = visibleNodes.remove(MORE_USAGES_SEPARATOR_NODE);
    if (hadMoreSeparator) {
        usages.add(MORE_USAGES_SEPARATOR);
        visibleNodes.add(MORE_USAGES_SEPARATOR_NODE);
    }

    addUsageNodes(usageView.getRoot(), usageView, new ArrayList<UsageNode>());

    TableScrollingUtil.installActions(table);

    final List<UsageNode> data = collectData(usages, visibleNodes, usageView, presentation);
    setTableModel(table, usageView, data);

    SpeedSearchBase<JTable> speedSearch = new MySpeedSearch(table);
    speedSearch.setComparator(new SpeedSearchComparator(false));

    final JBPopup popup = createUsagePopup(usages, descriptor, visibleNodes, handler, editor, popupPosition,
            maxUsages, usageView, options, table, presentation, processIcon, hadMoreSeparator);

    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(new Runnable() {
        @Override
        public void run() {
            showPopupIfNeedTo(popup, popupPosition);
        }
    }, 300);

    final PingEDT pingEDT = new PingEDT("Rebuild popup in EDT", new Condition<Object>() {
        @Override
        public boolean value(Object o) {
            return popup.isDisposed();
        }
    }, 100, new Runnable() {
        @Override
        public void run() {
            if (popup.isDisposed())
                return;

            final List<UsageNode> nodes = new ArrayList<UsageNode>();
            List<Usage> copy;
            synchronized (usages) {
                // open up popup as soon as several usages 've been found
                if (!popup.isVisible() && (usages.size() <= 1 || !showPopupIfNeedTo(popup, popupPosition))) {
                    return;
                }
                addUsageNodes(usageView.getRoot(), usageView, nodes);
                copy = new ArrayList<Usage>(usages);
            }

            rebuildPopup(usageView, copy, nodes, table, popup, presentation, popupPosition,
                    !processIcon.isDisposed());
        }
    });

    final MessageBusConnection messageBusConnection = project.getMessageBus().connect(usageView);
    messageBusConnection.subscribe(UsageFilteringRuleProvider.RULES_CHANGED, new Runnable() {
        @Override
        public void run() {
            pingEDT.ping();
        }
    });

    Processor<Usage> collect = new Processor<Usage>() {
        private UsageTarget myUsageTarget = new PsiElement2UsageTargetAdapter(handler.getPsiElement());

        @Override
        public boolean process(@NotNull Usage usage) {
            synchronized (usages) {
                if (visibleNodes.size() >= maxUsages)
                    return false;
                if (UsageViewManager.isSelfUsage(usage, new UsageTarget[] { myUsageTarget })) {
                    return true;
                }

                Usage usageToAdd = decider.shouldShow(myUsageTarget, usage) ? usage : null;
                if (usageToAdd == null)
                    return true;

                UsageNode node = usageView.doAppendUsage(usageToAdd);
                usages.add(usageToAdd);

                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, new Runnable() {
                @Override
                public void run() {
                    ApplicationManager.getApplication().invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            Disposer.dispose(processIcon);
                            Container parent = processIcon.getParent();
                            parent.remove(processIcon);
                            parent.repaint();
                            pingEDT.ping(); // repaint title
                            synchronized (usages) {
                                if (visibleNodes.isEmpty()) {
                                    if (usages.isEmpty()) {
                                        String text = UsageViewBundle.message("no.usages.found.in",
                                                searchScopePresentableName(options, project));
                                        showHint(text, editor, popupPosition, handler, maxUsages, options);
                                        popup.cancel();
                                    } else {
                                        // all usages filtered out
                                    }
                                } else if (visibleNodes.size() == 1) {
                                    if (usages.size() == 1) {
                                        //the only usage
                                        Usage usage = visibleNodes.iterator().next().getUsage();
                                        usage.navigate(true);
                                        //String message = UsageViewBundle.message("show.usages.only.usage", searchScopePresentableName(options, project));
                                        //navigateAndHint(usage, message, handler, popupPosition, maxUsages, options);
                                        popup.cancel();
                                    } 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, project));
                                            navigateAndHint(visibleUsage, hint, handler, popupPosition,
                                                    maxUsages, options);
                                            popup.cancel();
                                        }
                                    }
                                } else {
                                    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());
                }
            });
    Disposer.register(popup, new Disposable() {
        @Override
        public void dispose() {
            indicator.cancel();
        }
    });
}