Search in sources :

Example 1 with PsiClassListCellRenderer

use of com.intellij.ide.util.PsiClassListCellRenderer in project intellij-community by JetBrains.

the class GenerateMissedTestsAction method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    final PsiClass srcClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
    if (srcClass == null)
        return;
    final Collection<PsiElement> testClasses = TestFinderHelper.findTestsForClass(srcClass);
    if (testClasses.isEmpty()) {
        HintManager.getInstance().showErrorHint(editor, "No tests found.");
        return;
    }
    if (testClasses.size() == 1) {
        generateMissedTests((PsiClass) ContainerUtil.getFirstItem(testClasses), srcClass, editor);
        return;
    }
    final JBList list = new JBList(testClasses);
    list.setCellRenderer(new PsiClassListCellRenderer());
    JBPopupFactory.getInstance().createListPopupBuilder(list).setItemChoosenCallback(() -> generateMissedTests((PsiClass) list.getSelectedValue(), srcClass, editor)).setTitle("Choose Test").createPopup().showInBestPositionFor(editor);
}
Also used : PsiClass(com.intellij.psi.PsiClass) JBList(com.intellij.ui.components.JBList) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PsiClassListCellRenderer

use of com.intellij.ide.util.PsiClassListCellRenderer in project intellij-community by JetBrains.

the class InheritorChooser method runMethodInAbstractClass.

public boolean runMethodInAbstractClass(final ConfigurationContext context, final Runnable performRunnable, final PsiMethod psiMethod, final PsiClass containingClass, final Condition<PsiClass> acceptAbstractCondition) {
    if (containingClass != null && acceptAbstractCondition.value(containingClass)) {
        final Location location = context.getLocation();
        if (location instanceof MethodLocation) {
            final PsiClass aClass = ((MethodLocation) location).getContainingClass();
            if (aClass != null && !aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
                return false;
            }
        } else if (location instanceof PsiMemberParameterizedLocation) {
            return false;
        }
        final List<PsiClass> classes = new ArrayList<>();
        if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
            final boolean isJUnit5 = ReadAction.compute(() -> JUnitUtil.isJUnit5(containingClass));
            ClassInheritorsSearch.search(containingClass).forEach(aClass -> {
                if (isJUnit5 && JUnitUtil.isJUnit5TestClass(aClass, true) || PsiClassUtil.isRunnableClass(aClass, true, true)) {
                    classes.add(aClass);
                }
                return true;
            });
        }, "Search for " + containingClass.getQualifiedName() + " inheritors", true, containingClass.getProject())) {
            return true;
        }
        if (classes.size() == 1) {
            runForClass(classes.get(0), psiMethod, context, performRunnable);
            return true;
        }
        if (classes.isEmpty())
            return false;
        final FileEditor fileEditor = PlatformDataKeys.FILE_EDITOR.getData(context.getDataContext());
        if (fileEditor instanceof TextEditor) {
            final Document document = ((TextEditor) fileEditor).getEditor().getDocument();
            final PsiFile containingFile = PsiDocumentManager.getInstance(context.getProject()).getPsiFile(document);
            if (containingFile instanceof PsiClassOwner) {
                final List<PsiClass> psiClasses = new ArrayList<>(Arrays.asList(((PsiClassOwner) containingFile).getClasses()));
                psiClasses.retainAll(classes);
                if (psiClasses.size() == 1) {
                    runForClass(psiClasses.get(0), psiMethod, context, performRunnable);
                    return true;
                }
            }
        }
        final int numberOfInheritors = classes.size();
        final PsiClassListCellRenderer renderer = new PsiClassListCellRenderer() {

            @Override
            protected boolean customizeNonPsiElementLeftRenderer(ColoredListCellRenderer renderer, JList list, Object value, int index, boolean selected, boolean hasFocus) {
                if (value == null) {
                    renderer.append("All (" + numberOfInheritors + ")");
                    return true;
                }
                return super.customizeNonPsiElementLeftRenderer(renderer, list, value, index, selected, hasFocus);
            }
        };
        Collections.sort(classes, renderer.getComparator());
        //suggest to run all inherited tests 
        classes.add(0, null);
        final JBList list = new JBList(classes);
        list.setCellRenderer(renderer);
        JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle("Choose executable classes to run " + (psiMethod != null ? psiMethod.getName() : containingClass.getName())).setMovable(false).setResizable(false).setRequestFocus(true).setItemChoosenCallback(() -> {
            final Object[] values = list.getSelectedValues();
            if (values == null)
                return;
            chooseAndPerform(values, psiMethod, context, performRunnable, classes);
        }).createPopup().showInBestPositionFor(context.getDataContext());
        return true;
    }
    return false;
}
Also used : ProgressManager(com.intellij.openapi.progress.ProgressManager) JBList(com.intellij.ui.components.JBList) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) Arrays(java.util.Arrays) ArrayUtil(com.intellij.util.ArrayUtil) ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) Document(com.intellij.openapi.editor.Document) FileEditor(com.intellij.openapi.fileEditor.FileEditor) ReadAction(com.intellij.openapi.application.ReadAction) ArrayList(java.util.ArrayList) List(java.util.List) PlatformDataKeys(com.intellij.openapi.actionSystem.PlatformDataKeys) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer) PsiClassUtil(com.intellij.psi.util.PsiClassUtil) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) com.intellij.psi(com.intellij.psi) ClassInheritorsSearch(com.intellij.psi.search.searches.ClassInheritorsSearch) Location(com.intellij.execution.Location) Collections(java.util.Collections) TextEditor(com.intellij.openapi.fileEditor.TextEditor) Condition(com.intellij.openapi.util.Condition) javax.swing(javax.swing) FileEditor(com.intellij.openapi.fileEditor.FileEditor) ArrayList(java.util.ArrayList) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) Document(com.intellij.openapi.editor.Document) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) TextEditor(com.intellij.openapi.fileEditor.TextEditor) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer) JBList(com.intellij.ui.components.JBList) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Location(com.intellij.execution.Location)

Example 3 with PsiClassListCellRenderer

use of com.intellij.ide.util.PsiClassListCellRenderer in project intellij-community by JetBrains.

the class ShowSiblingsAction method performForContext.

@Override
public void performForContext(@NotNull DataContext dataContext, final boolean invokedByShortcut) {
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    final PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext);
    if (project == null)
        return;
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    final Editor editor = getEditor(dataContext);
    PsiElement element = getElement(project, file, editor, CommonDataKeys.PSI_ELEMENT.getData(dataContext));
    if (element == null && file == null)
        return;
    PsiFile containingFile = element != null ? element.getContainingFile() : file;
    if (containingFile == null || !containingFile.getViewProvider().isPhysical())
        return;
    if (editor != null) {
        PsiReference ref = TargetElementUtil.findReference(editor, editor.getCaretModel().getOffset());
        if (element == null && ref != null) {
            element = TargetElementUtil.getInstance().adjustReference(ref);
        }
    }
    final PsiElement[] superElements = findSuperElements(element);
    if (superElements.length == 0)
        return;
    final boolean isMethod = superElements[0] instanceof PsiMethod;
    NavigatablePsiElement[] navigatablePsiElements = ContainerUtil.findAllAsArray(superElements, NavigatablePsiElement.class);
    final JBPopup popup = PsiElementListNavigator.navigateOrCreatePopup(navigatablePsiElements, "Choose super " + (isMethod ? "method" : "class or interface"), "Super " + (isMethod ? "methods" : "classes/interfaces"), isMethod ? new MethodCellRenderer(false) : new PsiClassListCellRenderer(), null, objects -> showSiblings(invokedByShortcut, project, editor, file, editor != null, (PsiElement) objects[0]));
    if (popup != null) {
        if (editor != null) {
            popup.showInBestPositionFor(editor);
        } else {
            popup.showCenteredInCurrentWindow(project);
        }
    }
}
Also used : MethodCellRenderer(com.intellij.ide.util.MethodCellRenderer) Project(com.intellij.openapi.project.Project) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer) Editor(com.intellij.openapi.editor.Editor) JBPopup(com.intellij.openapi.ui.popup.JBPopup)

Example 4 with PsiClassListCellRenderer

use of com.intellij.ide.util.PsiClassListCellRenderer in project intellij-community by JetBrains.

the class BaseExpressionToFieldHandler method invokeImpl.

protected boolean invokeImpl(final Project project, @NotNull final PsiExpression selectedExpr, final Editor editor) {
    final PsiElement element = getPhysicalElement(selectedExpr);
    final PsiFile file = element.getContainingFile();
    LOG.assertTrue(file != null, "expr.getContainingFile() == null");
    if (LOG.isDebugEnabled()) {
        LOG.debug("expression:" + selectedExpr);
    }
    final PsiType tempType = getTypeByExpression(selectedExpr);
    if (tempType == null || LambdaUtil.notInferredType(tempType)) {
        String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("unknown.expression.type"));
        CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), getHelpID());
        return false;
    }
    if (PsiType.VOID.equals(tempType)) {
        String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("selected.expression.has.void.type"));
        CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), getHelpID());
        return false;
    }
    myParentClass = getParentClass(selectedExpr);
    final List<PsiClass> classes = new ArrayList<>();
    PsiClass aClass = myParentClass;
    while (aClass != null) {
        classes.add(aClass);
        final PsiField psiField = ConvertToFieldRunnable.checkForwardRefs(selectedExpr, aClass);
        if (psiField != null && psiField.getParent() == aClass)
            break;
        aClass = PsiTreeUtil.getParentOfType(aClass, PsiClass.class, true);
    }
    final AbstractInplaceIntroducer activeIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
    final boolean shouldSuggestDialog = activeIntroducer instanceof InplaceIntroduceConstantPopup && activeIntroducer.startsOnTheSameElement(selectedExpr, null);
    if (classes.size() == 1 || editor == null || ApplicationManager.getApplication().isUnitTestMode() || shouldSuggestDialog) {
        return !convertExpressionToField(selectedExpr, editor, file, project, tempType);
    } else if (!classes.isEmpty()) {
        PsiClass selection = AnonymousTargetClassPreselectionUtil.getPreselection(classes, myParentClass);
        NavigationUtil.getPsiElementPopup(classes.toArray(new PsiClass[classes.size()]), new PsiClassListCellRenderer(), "Choose class to introduce " + (myIsConstant ? "constant" : "field"), new PsiElementProcessor<PsiClass>() {

            @Override
            public boolean execute(@NotNull PsiClass aClass) {
                AnonymousTargetClassPreselectionUtil.rememberSelection(aClass, myParentClass);
                myParentClass = aClass;
                convertExpressionToField(selectedExpr, editor, file, project, tempType);
                return false;
            }
        }, selection).showInBestPositionFor(editor);
    }
    return true;
}
Also used : AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer) ArrayList(java.util.ArrayList) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer)

Example 5 with PsiClassListCellRenderer

use of com.intellij.ide.util.PsiClassListCellRenderer in project intellij-community by JetBrains.

the class CreateInnerClassFromUsageFix method chooseTargetClass.

private void chooseTargetClass(PsiClass[] classes, final Editor editor, final String superClassName) {
    final Project project = classes[0].getProject();
    final JList list = new JBList(classes);
    PsiElementListCellRenderer renderer = new PsiClassListCellRenderer();
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setCellRenderer(renderer);
    final PopupChooserBuilder builder = new PopupChooserBuilder(list);
    renderer.installSpeedSearch(builder);
    Runnable runnable = () -> {
        int index = list.getSelectedIndex();
        if (index < 0)
            return;
        doInvoke((PsiClass) list.getSelectedValue(), superClassName);
    };
    builder.setTitle(QuickFixBundle.message("target.class.chooser.title")).setItemChoosenCallback(runnable).createPopup().showInBestPositionFor(editor);
}
Also used : Project(com.intellij.openapi.project.Project) JBList(com.intellij.ui.components.JBList) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer)

Aggregations

PsiClassListCellRenderer (com.intellij.ide.util.PsiClassListCellRenderer)10 JBList (com.intellij.ui.components.JBList)5 Project (com.intellij.openapi.project.Project)4 PsiElementListCellRenderer (com.intellij.ide.util.PsiElementListCellRenderer)3 PopupChooserBuilder (com.intellij.openapi.ui.popup.PopupChooserBuilder)3 PsiElementProcessor (com.intellij.psi.search.PsiElementProcessor)3 AbstractInplaceIntroducer (com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer)3 ArrayList (java.util.ArrayList)3 NotNull (org.jetbrains.annotations.NotNull)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Location (com.intellij.execution.Location)1 ConfigurationContext (com.intellij.execution.actions.ConfigurationContext)1 PsiMemberParameterizedLocation (com.intellij.execution.junit2.PsiMemberParameterizedLocation)1 MethodLocation (com.intellij.execution.junit2.info.MethodLocation)1 MethodCellRenderer (com.intellij.ide.util.MethodCellRenderer)1 PlatformDataKeys (com.intellij.openapi.actionSystem.PlatformDataKeys)1 ReadAction (com.intellij.openapi.application.ReadAction)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 FileEditor (com.intellij.openapi.fileEditor.FileEditor)1