Search in sources :

Example 1 with ImplementationViewComponent

use of com.intellij.codeInsight.hint.ImplementationViewComponent 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 ImplementationViewComponent

use of com.intellij.codeInsight.hint.ImplementationViewComponent in project intellij-community by JetBrains.

the class ImageDuplicateResultsDialog method createCenterPanel.

@Override
protected JComponent createCenterPanel() {
    final JPanel panel = new JPanel(new BorderLayout());
    DataManager.registerDataProvider(panel, new DataProvider() {

        @Override
        public Object getData(@NonNls String dataId) {
            final TreePath path = myTree.getSelectionPath();
            if (path != null) {
                Object component = path.getLastPathComponent();
                VirtualFile file = null;
                if (component instanceof MyFileNode) {
                    component = ((MyFileNode) component).getParent();
                }
                if (component instanceof MyDuplicatesNode) {
                    file = ((MyDuplicatesNode) component).getUserObject().iterator().next();
                }
                if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) {
                    return file;
                }
                if (CommonDataKeys.VIRTUAL_FILE_ARRAY.is(dataId) && file != null) {
                    return new VirtualFile[] { file };
                }
            }
            return null;
        }
    });
    final JBList list = new JBList(new ResourceModules().getModuleNames());
    final NotNullFunction<Object, JComponent> modulesRenderer = dom -> new JLabel(dom instanceof Module ? ((Module) dom).getName() : dom.toString(), PlatformIcons.SOURCE_FOLDERS_ICON, SwingConstants.LEFT);
    list.installCellRenderer(modulesRenderer);
    final JPanel modulesPanel = ToolbarDecorator.createDecorator(list).setAddAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            final Module[] all = ModuleManager.getInstance(myProject).getModules();
            Arrays.sort(all, (o1, o2) -> o1.getName().compareTo(o2.getName()));
            final JBList modules = new JBList(all);
            modules.installCellRenderer(modulesRenderer);
            JBPopupFactory.getInstance().createListPopupBuilder(modules).setTitle("Add Resource Module").setFilteringEnabled(o -> ((Module) o).getName()).setItemChoosenCallback(() -> {
                final Object value = modules.getSelectedValue();
                if (value instanceof Module && !myResourceModules.contains((Module) value)) {
                    myResourceModules.add((Module) value);
                    ((DefaultListModel) list.getModel()).addElement(((Module) value).getName());
                }
                ((DefaultTreeModel) myTree.getModel()).reload();
                TreeUtil.expandAll(myTree);
            }).createPopup().show(button.getPreferredPopupPoint());
        }
    }).setRemoveAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            final Object[] values = list.getSelectedValues();
            for (Object value : values) {
                myResourceModules.remove((String) value);
                ((DefaultListModel) list.getModel()).removeElement(value);
            }
            ((DefaultTreeModel) myTree.getModel()).reload();
            TreeUtil.expandAll(myTree);
        }
    }).disableDownAction().disableUpAction().createPanel();
    modulesPanel.setPreferredSize(new Dimension(-1, 60));
    final JPanel top = new JPanel(new BorderLayout());
    top.add(new JLabel("Image modules:"), BorderLayout.NORTH);
    top.add(modulesPanel, BorderLayout.CENTER);
    panel.add(top, BorderLayout.NORTH);
    panel.add(new JBScrollPane(myTree), BorderLayout.CENTER);
    new AnAction() {

        @Override
        public void actionPerformed(AnActionEvent e) {
            VirtualFile file = getFileFromSelection();
            if (file != null) {
                final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file);
                if (psiFile != null) {
                    final ImplementationViewComponent viewComponent = new ImplementationViewComponent(new PsiElement[] { psiFile }, 0);
                    final TreeSelectionListener listener = new TreeSelectionListener() {

                        @Override
                        public void valueChanged(TreeSelectionEvent e) {
                            final VirtualFile selection = getFileFromSelection();
                            if (selection != null) {
                                final PsiFile newElement = PsiManager.getInstance(myProject).findFile(selection);
                                if (newElement != null) {
                                    viewComponent.update(new PsiElement[] { newElement }, 0);
                                }
                            }
                        }
                    };
                    myTree.addTreeSelectionListener(listener);
                    final JBPopup popup = JBPopupFactory.getInstance().createComponentPopupBuilder(viewComponent, viewComponent.getPreferredFocusableComponent()).setProject(myProject).setDimensionServiceKey(myProject, ImageDuplicateResultsDialog.class.getName(), false).setResizable(true).setMovable(true).setRequestFocus(false).setCancelCallback(() -> {
                        myTree.removeTreeSelectionListener(listener);
                        return true;
                    }).setTitle("Image Preview").createPopup();
                    final Window window = ImageDuplicateResultsDialog.this.getWindow();
                    popup.show(new RelativePoint(window, new Point(window.getWidth(), 0)));
                    viewComponent.setHint(popup, "Image Preview");
                }
            }
        }
    }.registerCustomShortcutSet(CustomShortcutSet.fromString("ENTER"), panel);
    int total = myDuplicates.values().stream().mapToInt(Set::size).sum() - myDuplicates.size();
    final JLabel label = new JLabel("<html>Press <b>Enter</b> to preview image<br>Total images found: " + myImages.size() + ". Total duplicates found: " + total + "</html>");
    panel.add(label, BorderLayout.SOUTH);
    return panel;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) UIUtil(com.intellij.util.ui.UIUtil) java.util(java.util) ModuleManager(com.intellij.openapi.module.ModuleManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NonNls(org.jetbrains.annotations.NonNls) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) PsiManager(com.intellij.psi.PsiManager) ModuleUtil(com.intellij.openapi.module.ModuleUtil) TreeSelectionListener(javax.swing.event.TreeSelectionListener) PropertyName(com.intellij.ide.util.PropertyName) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Tree(com.intellij.ui.treeStructure.Tree) FileUtil(com.intellij.openapi.util.io.FileUtil) Module(com.intellij.openapi.module.Module) DataManager(com.intellij.ide.DataManager) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) PlatformIcons(com.intellij.util.PlatformIcons) JBList(com.intellij.ui.components.JBList) TreeUtil(com.intellij.util.ui.tree.TreeUtil) PropertiesComponent(com.intellij.ide.util.PropertiesComponent) TreePath(javax.swing.tree.TreePath) StringUtil(com.intellij.openapi.util.text.StringUtil) NotNullFunction(com.intellij.util.NotNullFunction) com.intellij.ui(com.intellij.ui) ActionEvent(java.awt.event.ActionEvent) Collectors(java.util.stream.Collectors) JBPopup(com.intellij.openapi.ui.popup.JBPopup) File(java.io.File) JBScrollPane(com.intellij.ui.components.JBScrollPane) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) java.awt(java.awt) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) NotNull(org.jetbrains.annotations.NotNull) ImplementationViewComponent(com.intellij.codeInsight.hint.ImplementationViewComponent) RelativePoint(com.intellij.ui.awt.RelativePoint) javax.swing(javax.swing) TreeSelectionListener(javax.swing.event.TreeSelectionListener) RelativePoint(com.intellij.ui.awt.RelativePoint) PsiFile(com.intellij.psi.PsiFile) JBPopup(com.intellij.openapi.ui.popup.JBPopup) PsiElement(com.intellij.psi.PsiElement) ImplementationViewComponent(com.intellij.codeInsight.hint.ImplementationViewComponent) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) TreePath(javax.swing.tree.TreePath) JBList(com.intellij.ui.components.JBList) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) Module(com.intellij.openapi.module.Module) JBScrollPane(com.intellij.ui.components.JBScrollPane)

Example 3 with ImplementationViewComponent

use of com.intellij.codeInsight.hint.ImplementationViewComponent in project intellij-community by JetBrains.

the class ImplementationsViewTest method testFunctionalInterface.

public void testFunctionalInterface() {
    myFixture.configureByText("a.java", "interface AF<caret>oo{\n" + "    boolean aaa();\n" + "}\n" + "class AFooImpl {\n" + "        {\n" + "             AFoo a = () -> {return false;};\n" + "        }\n" + "}");
    PsiClass psiClass = (PsiClass) TargetElementUtil.findTargetElement(myFixture.getEditor(), TargetElementUtil.getInstance().getAllAccepted());
    assert psiClass != null;
    final Collection<PsiElement> classes = getClassImplementations(psiClass);
    List<PsiElement> all = new ArrayList<>();
    all.add(psiClass);
    all.addAll(classes);
    final ImplementationViewComponent component = new ImplementationViewComponent(all.toArray(new PsiElement[all.size()]), 0);
    assertContent(component, new String[] { "a.java (AFoo)", "a.java" });
}
Also used : PsiClass(com.intellij.psi.PsiClass) ImplementationViewComponent(com.intellij.codeInsight.hint.ImplementationViewComponent) PsiElement(com.intellij.psi.PsiElement)

Example 4 with ImplementationViewComponent

use of com.intellij.codeInsight.hint.ImplementationViewComponent in project intellij-community by JetBrains.

the class ImplementationsViewTest method testInnerClasses.

public void testInnerClasses() {
    myFixture.configureByText("a.java", "abstract class AF<caret>oo{\n" + "    abstract boolean aaa();\n" + "    static class AFoo1 extends AFoo {\n" + "        @Override\n" + "        boolean aaa() {\n" + "            return false;\n" + "        }\n" + "    }\n" + "    static class AFoo3 extends AFoo {\n" + "        @Override\n" + "        boolean aaa() {\n" + "            return false;\n" + "        }\n" + "    }\n" + "    static class AFoo2 extends AFoo {\n" + "        @Override\n" + "        boolean aaa() {\n" + "            return false;\n" + "        }\n" + "    }\n" + "    \n" + "}");
    PsiClass psiClass = (PsiClass) TargetElementUtil.findTargetElement(myFixture.getEditor(), TargetElementUtil.getInstance().getAllAccepted());
    assert psiClass != null;
    final Collection<PsiElement> classes = getClassImplementations(psiClass);
    List<PsiElement> all = new ArrayList<>();
    all.add(psiClass);
    all.addAll(classes);
    final ImplementationViewComponent component = new ImplementationViewComponent(all.toArray(new PsiElement[all.size()]), 0);
    try {
        final String[] visibleFiles = component.getVisibleFiles();
        assertTrue(visibleFiles.length > 0);
        assertEquals(visibleFiles[0], "a.java (AFoo)");
        Arrays.sort(visibleFiles);
        Assert.assertArrayEquals(Arrays.toString(visibleFiles), new String[] { "a.java (AFoo)", "a.java (AFoo1 in AFoo)", "a.java (AFoo2 in AFoo)", "a.java (AFoo3 in AFoo)" }, visibleFiles);
    } finally {
        component.removeNotify();
    }
}
Also used : PsiClass(com.intellij.psi.PsiClass) ImplementationViewComponent(com.intellij.codeInsight.hint.ImplementationViewComponent) PsiElement(com.intellij.psi.PsiElement)

Example 5 with ImplementationViewComponent

use of com.intellij.codeInsight.hint.ImplementationViewComponent in project intellij-community by JetBrains.

the class ImplementationsViewTest method testInterfaceMethodOfFunctionalInterface.

public void testInterfaceMethodOfFunctionalInterface() {
    myFixture.configureByText("a.java", "interface AFoo{\n" + "    boolean a<caret>aa();\n" + "}\n" + "class AFooImpl {\n" + "        {\n" + "             AFoo a = () -> {return false;};\n" + "        }\n" + "}");
    PsiMethod psiMethod = (PsiMethod) TargetElementUtil.findTargetElement(myFixture.getEditor(), TargetElementUtil.getInstance().getAllAccepted());
    assert psiMethod != null;
    final Collection<PsiElement> methods = getMethodImplementations(psiMethod);
    List<PsiElement> all = new ArrayList<>();
    all.add(psiMethod);
    all.addAll(methods);
    final ImplementationViewComponent component = new ImplementationViewComponent(all.toArray(new PsiElement[all.size()]), 0);
    assertContent(component, new String[] { "a.java (AFoo)", "a.java" });
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) ImplementationViewComponent(com.intellij.codeInsight.hint.ImplementationViewComponent) PsiElement(com.intellij.psi.PsiElement)

Aggregations

ImplementationViewComponent (com.intellij.codeInsight.hint.ImplementationViewComponent)9 PsiElement (com.intellij.psi.PsiElement)8 com.intellij.openapi.actionSystem (com.intellij.openapi.actionSystem)3 Project (com.intellij.openapi.project.Project)3 JBPopup (com.intellij.openapi.ui.popup.JBPopup)3 JBPopupFactory (com.intellij.openapi.ui.popup.JBPopupFactory)3 PsiClass (com.intellij.psi.PsiClass)3 PsiMethod (com.intellij.psi.PsiMethod)3 java.util (java.util)3 Nullable (org.jetbrains.annotations.Nullable)3 DataManager (com.intellij.ide.DataManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Editor (com.intellij.openapi.editor.Editor)2 ProgressManager (com.intellij.openapi.progress.ProgressManager)2 FileUtil (com.intellij.openapi.util.io.FileUtil)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 NonNls (org.jetbrains.annotations.NonNls)2 NotNull (org.jetbrains.annotations.NotNull)2 CodeInsightBundle (com.intellij.codeInsight.CodeInsightBundle)1 TargetElementUtil (com.intellij.codeInsight.TargetElementUtil)1