Search in sources :

Example 6 with PsiClassListCellRenderer

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

the class PullAsAbstractUpFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    final PsiMethod method = (PsiMethod) startElement;
    if (!FileModificationService.getInstance().prepareFileForWrite(method.getContainingFile()))
        return;
    final PsiClass containingClass = method.getContainingClass();
    LOG.assertTrue(containingClass != null);
    PsiManager manager = containingClass.getManager();
    if (containingClass instanceof PsiAnonymousClass) {
        final PsiClassType baseClassType = ((PsiAnonymousClass) containingClass).getBaseClassType();
        final PsiClass baseClass = baseClassType.resolve();
        if (baseClass != null && manager.isInProject(baseClass)) {
            pullUp(method, containingClass, baseClass);
        }
    } else {
        final LinkedHashSet<PsiClass> classesToPullUp = new LinkedHashSet<>();
        collectClassesToPullUp(manager, classesToPullUp, containingClass.getExtendsListTypes());
        collectClassesToPullUp(manager, classesToPullUp, containingClass.getImplementsListTypes());
        if (classesToPullUp.isEmpty()) {
            //check visibility
            new ExtractInterfaceHandler().invoke(project, new PsiElement[] { containingClass }, null);
        } else if (classesToPullUp.size() == 1) {
            pullUp(method, containingClass, classesToPullUp.iterator().next());
        } else if (editor != null) {
            NavigationUtil.getPsiElementPopup(classesToPullUp.toArray(new PsiClass[classesToPullUp.size()]), new PsiClassListCellRenderer(), "Choose super class", new PsiElementProcessor<PsiClass>() {

                @Override
                public boolean execute(@NotNull PsiClass aClass) {
                    pullUp(method, containingClass, aClass);
                    return false;
                }
            }, classesToPullUp.iterator().next()).showInBestPositionFor(editor);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ExtractInterfaceHandler(com.intellij.refactoring.extractInterface.ExtractInterfaceHandler) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor)

Example 7 with PsiClassListCellRenderer

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

the class IntroduceParameterHandler method introduceStrategy.

@VisibleForTesting
public boolean introduceStrategy(final Project project, final Editor editor, PsiFile file, final PsiElement[] elements) {
    if (elements.length > 0) {
        final AbstractInplaceIntroducer inplaceIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
        if (inplaceIntroducer instanceof InplaceIntroduceParameterPopup) {
            return false;
        }
        final PsiMethod containingMethod = Util.getContainingMethod(elements[0]);
        if (containingMethod == null) {
            return false;
        }
        final List<PsiMethod> enclosingMethods = getEnclosingMethods(containingMethod);
        if (enclosingMethods.isEmpty()) {
            return false;
        }
        final PsiElement[] elementsCopy;
        if (!elements[0].isPhysical()) {
            elementsCopy = elements;
        } else {
            final PsiFile copy = PsiFileFactory.getInstance(project).createFileFromText(file.getName(), file.getFileType(), file.getText(), file.getModificationStamp(), false);
            final TextRange range = new TextRange(elements[0].getTextRange().getStartOffset(), elements[elements.length - 1].getTextRange().getEndOffset());
            final PsiExpression exprInRange = CodeInsightUtil.findExpressionInRange(copy, range.getStartOffset(), range.getEndOffset());
            elementsCopy = exprInRange != null ? new PsiElement[] { exprInRange } : CodeInsightUtil.findStatementsInRange(copy, range.getStartOffset(), range.getEndOffset());
        }
        final PsiMethod containingMethodCopy = Util.getContainingMethod(elementsCopy[0]);
        LOG.assertTrue(containingMethodCopy != null);
        final List<PsiMethod> enclosingMethodsInCopy = getEnclosingMethods(containingMethodCopy);
        final MyExtractMethodProcessor processor = new MyExtractMethodProcessor(project, editor, elementsCopy, enclosingMethodsInCopy.get(enclosingMethodsInCopy.size() - 1));
        try {
            if (!processor.prepare())
                return false;
            processor.showDialog();
            //provide context for generated method to check exceptions compatibility
            final PsiMethod emptyMethod = JavaPsiFacade.getElementFactory(project).createMethodFromText(processor.generateEmptyMethod("name").getText(), elements[0]);
            final Collection<? extends PsiType> types = FunctionalInterfaceSuggester.suggestFunctionalInterfaces(emptyMethod);
            if (types.isEmpty()) {
                return false;
            }
            if (types.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
                final PsiType next = types.iterator().next();
                functionalInterfaceSelected(next, enclosingMethods, project, editor, processor, elements);
            } else {
                final Map<PsiClass, PsiType> classes = new LinkedHashMap<>();
                for (PsiType type : types) {
                    classes.put(PsiUtil.resolveClassInType(type), type);
                }
                final PsiClass[] psiClasses = classes.keySet().toArray(new PsiClass[classes.size()]);
                final String methodSignature = PsiFormatUtil.formatMethod(emptyMethod, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE);
                final PsiType returnType = emptyMethod.getReturnType();
                LOG.assertTrue(returnType != null);
                final String title = "Choose Applicable Functional Interface: " + methodSignature + " -> " + returnType.getPresentableText();
                NavigationUtil.getPsiElementPopup(psiClasses, new PsiClassListCellRenderer(), title, new PsiElementProcessor<PsiClass>() {

                    @Override
                    public boolean execute(@NotNull PsiClass psiClass) {
                        functionalInterfaceSelected(classes.get(psiClass), enclosingMethods, project, editor, processor, elements);
                        return true;
                    }
                }).showInBestPositionFor(editor);
                return true;
            }
            return true;
        } catch (IncorrectOperationException | PrepareFailedException ignore) {
        }
    }
    return false;
}
Also used : PrepareFailedException(com.intellij.refactoring.extractMethod.PrepareFailedException) AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer) IncorrectOperationException(com.intellij.util.IncorrectOperationException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with PsiClassListCellRenderer

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

the class CreateFromUsageBaseFix method chooseTargetClass.

private void chooseTargetClass(List<PsiClass> classes, final Editor editor) {
    final PsiClass firstClass = classes.get(0);
    final Project project = firstClass.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);
    final PsiClass preselection = AnonymousTargetClassPreselectionUtil.getPreselection(classes, firstClass);
    if (preselection != null) {
        list.setSelectedValue(preselection, true);
    }
    Runnable runnable = () -> {
        int index = list.getSelectedIndex();
        if (index < 0)
            return;
        final PsiClass aClass = (PsiClass) list.getSelectedValue();
        AnonymousTargetClassPreselectionUtil.rememberSelection(aClass, firstClass);
        CommandProcessor.getInstance().executeCommand(project, () -> doInvoke(project, aClass), getText(), null);
    };
    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)

Example 9 with PsiClassListCellRenderer

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

the class LocalToFieldHandler method convertLocalToField.

public boolean convertLocalToField(final PsiLocalVariable local, final Editor editor) {
    boolean tempIsStatic = myIsConstant;
    PsiElement parent = local.getParent();
    final List<PsiClass> classes = new ArrayList<>();
    while (parent != null && parent.getContainingFile() != null) {
        if (parent instanceof PsiClass && !(myIsConstant && parent instanceof PsiAnonymousClass)) {
            classes.add((PsiClass) parent);
        }
        if (parent instanceof PsiFile && FileTypeUtils.isInServerPageFile(parent)) {
            String message = RefactoringBundle.message("error.not.supported.for.jsp", REFACTORING_NAME);
            CommonRefactoringUtil.showErrorHint(myProject, editor, message, REFACTORING_NAME, HelpID.LOCAL_TO_FIELD);
            return false;
        }
        if (parent instanceof PsiModifierListOwner && ((PsiModifierListOwner) parent).hasModifierProperty(PsiModifier.STATIC)) {
            tempIsStatic = true;
        }
        parent = parent.getParent();
    }
    if (classes.isEmpty())
        return false;
    final AbstractInplaceIntroducer activeIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
    final boolean shouldSuggestDialog = activeIntroducer instanceof InplaceIntroduceConstantPopup && activeIntroducer.startsOnTheSameElement(null, local);
    if (classes.size() == 1 || ApplicationManager.getApplication().isUnitTestMode() || shouldSuggestDialog) {
        if (convertLocalToField(local, classes.get(getChosenClassIndex(classes)), editor, tempIsStatic))
            return false;
    } else {
        final boolean isStatic = tempIsStatic;
        final PsiClass firstClass = classes.get(0);
        final PsiClass preselection = AnonymousTargetClassPreselectionUtil.getPreselection(classes, firstClass);
        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, aClass);
                convertLocalToField(local, aClass, editor, isStatic);
                return false;
            }
        }, preselection).showInBestPositionFor(editor);
    }
    return true;
}
Also used : AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer)

Example 10 with PsiClassListCellRenderer

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

the class GrCreateFromUsageBaseFix method chooseClass.

private void chooseClass(List<PsiClass> classes, Editor editor) {
    final Project project = classes.get(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;
        final PsiClass aClass = (PsiClass) list.getSelectedValue();
        CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> invokeImpl(project, aClass)), getText(), null);
    };
    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