Search in sources :

Example 1 with CreateClassDialog

use of com.intellij.codeInsight.intention.impl.CreateClassDialog in project intellij-community by JetBrains.

the class CreateFromUsageUtils method createClass.

@Nullable
public static PsiClass createClass(final PsiJavaCodeReferenceElement referenceElement, final CreateClassKind classKind, final String superClassName) {
    assert !ApplicationManager.getApplication().isWriteAccessAllowed() : "You must not run createClass() from under write action";
    final String name = referenceElement.getReferenceName();
    String qualifierName;
    final PsiElement qualifierElement;
    PsiElement qualifier = referenceElement.getQualifier();
    if (qualifier instanceof PsiJavaCodeReferenceElement) {
        qualifierName = ((PsiJavaCodeReferenceElement) qualifier).getQualifiedName();
        qualifierElement = ((PsiJavaCodeReferenceElement) qualifier).resolve();
        if (qualifierElement instanceof PsiClass) {
            if (!FileModificationService.getInstance().preparePsiElementForWrite(qualifierElement))
                return null;
            return WriteAction.compute(() -> createClassInQualifier((PsiClass) qualifierElement, classKind, name, referenceElement));
        }
    } else {
        qualifierName = null;
        qualifierElement = null;
    }
    final PsiManager manager = referenceElement.getManager();
    final PsiFile sourceFile = referenceElement.getContainingFile();
    final Module module = ModuleUtilCore.findModuleForPsiElement(sourceFile);
    if (qualifierName == null) {
        PsiPackage aPackage = findTargetPackage(qualifierElement, manager, sourceFile);
        if (aPackage == null)
            return null;
        qualifierName = aPackage.getQualifiedName();
    }
    final PsiDirectory targetDirectory;
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
        Project project = manager.getProject();
        String title = QuickFixBundle.message("create.class.title", StringUtil.capitalize(classKind.getDescription()));
        CreateClassDialog dialog = new CreateClassDialog(project, title, name, qualifierName, classKind, false, module) {

            @Override
            protected boolean reportBaseInSourceSelectionInTest() {
                return true;
            }
        };
        dialog.show();
        if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
            return null;
        targetDirectory = dialog.getTargetDirectory();
        if (targetDirectory == null)
            return null;
    } else {
        targetDirectory = null;
    }
    return createClass(classKind, targetDirectory, name, manager, referenceElement, sourceFile, superClassName);
}
Also used : Project(com.intellij.openapi.project.Project) CreateClassDialog(com.intellij.codeInsight.intention.impl.CreateClassDialog) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with CreateClassDialog

use of com.intellij.codeInsight.intention.impl.CreateClassDialog in project android by JetBrains.

the class HtmlLinkManager method handleNewClassUrl.

private static void handleNewClassUrl(@NotNull String url, @NotNull Module module) {
    assert url.startsWith(URL_CREATE_CLASS) : url;
    String s = url.substring(URL_CREATE_CLASS.length());
    final Project project = module.getProject();
    String title = "Create Custom View";
    final String className;
    final String packageName;
    int index = s.lastIndexOf('.');
    if (index == -1) {
        className = s;
        packageName = MergedManifest.get(module).getPackage();
        if (packageName == null) {
            return;
        }
    } else {
        packageName = s.substring(0, index);
        className = s.substring(index + 1);
    }
    CreateClassDialog dialog = new CreateClassDialog(project, title, className, packageName, CreateClassKind.CLASS, true, module) {

        @Override
        protected boolean reportBaseInSourceSelectionInTest() {
            return true;
        }
    };
    dialog.show();
    if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
        final PsiDirectory targetDirectory = dialog.getTargetDirectory();
        if (targetDirectory != null) {
            PsiClass newClass = new WriteCommandAction<PsiClass>(project, "Create Class") {

                @Override
                protected void run(@NotNull Result<PsiClass> result) throws Throwable {
                    PsiClass targetClass = JavaDirectoryService.getInstance().createClass(targetDirectory, className);
                    PsiManager manager = PsiManager.getInstance(project);
                    final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
                    final PsiElementFactory factory = facade.getElementFactory();
                    // Extend android.view.View
                    PsiJavaCodeReferenceElement superclassReference = factory.createReferenceElementByFQClassName(CLASS_VIEW, targetClass.getResolveScope());
                    PsiReferenceList extendsList = targetClass.getExtendsList();
                    if (extendsList != null) {
                        extendsList.add(superclassReference);
                    }
                    // Add constructor
                    GlobalSearchScope scope = GlobalSearchScope.allScope(project);
                    PsiJavaFile javaFile = (PsiJavaFile) targetClass.getContainingFile();
                    PsiImportList importList = javaFile.getImportList();
                    if (importList != null) {
                        PsiClass contextClass = JavaPsiFacade.getInstance(project).findClass(CLASS_CONTEXT, scope);
                        if (contextClass != null) {
                            importList.add(factory.createImportStatement(contextClass));
                        }
                        PsiClass attributeSetClass = JavaPsiFacade.getInstance(project).findClass(CLASS_ATTRIBUTE_SET, scope);
                        if (attributeSetClass != null) {
                            importList.add(factory.createImportStatement(attributeSetClass));
                        }
                    }
                    PsiMethod constructor = factory.createMethodFromText("public " + className + "(Context context, AttributeSet attrs, int defStyle) {\n" + "  super(context, attrs, defStyle);\n" + "}\n", targetClass);
                    targetClass.add(constructor);
                    // Format class
                    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
                    PsiFile containingFile = targetClass.getContainingFile();
                    if (containingFile != null) {
                        codeStyleManager.reformat(javaFile);
                    }
                    result.setResult(targetClass);
                }
            }.execute().getResultObject();
            if (newClass != null) {
                PsiFile file = newClass.getContainingFile();
                if (file != null) {
                    openEditor(project, file, newClass.getTextOffset());
                }
            }
        }
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) Project(com.intellij.openapi.project.Project) CreateClassDialog(com.intellij.codeInsight.intention.impl.CreateClassDialog) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 3 with CreateClassDialog

use of com.intellij.codeInsight.intention.impl.CreateClassDialog in project intellij-community by JetBrains.

the class JavaPushDownDelegate method preprocessNoInheritorsFound.

@Override
public NewSubClassData preprocessNoInheritorsFound(PsiElement sourceClass, String conflictDialogTitle) {
    final PsiClass aClass = (PsiClass) sourceClass;
    final PsiFile containingFile = aClass.getContainingFile();
    final boolean defaultPackage = StringUtil.isEmptyOrSpaces(containingFile instanceof PsiClassOwner ? ((PsiClassOwner) containingFile).getPackageName() : "");
    if (aClass.isEnum() || aClass.hasModifierProperty(PsiModifier.FINAL) || defaultPackage) {
        if (Messages.showOkCancelDialog((aClass.isEnum() ? "Enum " + aClass.getQualifiedName() + " doesn't have constants to inline to. " : (defaultPackage ? "Class " : "Final class ") + aClass.getQualifiedName() + "does not have inheritors. ") + "Pushing members down will result in them being deleted. " + "Would you like to proceed?", conflictDialogTitle, Messages.getWarningIcon()) != Messages.OK) {
            return NewSubClassData.ABORT_REFACTORING;
        }
    } else {
        String noInheritors = aClass.isInterface() ? RefactoringBundle.message("interface.0.does.not.have.inheritors", aClass.getQualifiedName()) : RefactoringBundle.message("class.0.does.not.have.inheritors", aClass.getQualifiedName());
        final String message = noInheritors + "\n" + RefactoringBundle.message("push.down.will.delete.members");
        final int answer = Messages.showYesNoCancelDialog(message, conflictDialogTitle, Messages.getWarningIcon());
        if (answer == Messages.YES) {
            final CreateClassDialog classDialog = CreateSubclassAction.chooseSubclassToCreate(aClass);
            if (classDialog != null) {
                return new NewSubClassData(classDialog.getTargetDirectory(), classDialog.getClassName());
            } else {
                return NewSubClassData.ABORT_REFACTORING;
            }
        } else if (answer != Messages.NO) {
            return NewSubClassData.ABORT_REFACTORING;
        }
    }
    return null;
}
Also used : CreateClassDialog(com.intellij.codeInsight.intention.impl.CreateClassDialog)

Example 4 with CreateClassDialog

use of com.intellij.codeInsight.intention.impl.CreateClassDialog in project intellij-community by JetBrains.

the class CreateClassActionBase method getTargetDirectory.

@Nullable
protected PsiDirectory getTargetDirectory(@NotNull Project project, @NotNull String qualifier, @NotNull String name, @Nullable Module module, @NotNull String title) {
    CreateClassDialog dialog = new CreateClassDialog(project, title, name, qualifier, getType(), false, module) {

        @Override
        protected boolean reportBaseInSourceSelectionInTest() {
            return true;
        }
    };
    dialog.show();
    if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
        return null;
    return dialog.getTargetDirectory();
}
Also used : CreateClassDialog(com.intellij.codeInsight.intention.impl.CreateClassDialog) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with CreateClassDialog

use of com.intellij.codeInsight.intention.impl.CreateClassDialog in project intellij-community by JetBrains.

the class MyPredicate method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull final Project project, Editor editor) throws IncorrectOperationException {
    final GrListOrMap map = (GrListOrMap) element;
    final GrNamedArgument[] namedArguments = map.getNamedArguments();
    LOG.assertTrue(map.getInitializers().length == 0);
    final PsiFile file = map.getContainingFile();
    final String packageName = file instanceof GroovyFileBase ? ((GroovyFileBase) file).getPackageName() : "";
    final CreateClassDialog dialog = new CreateClassDialog(project, GroovyBundle.message("create.class.family.name"), "", packageName, GrCreateClassKind.CLASS, true, ModuleUtilCore.findModuleForPsiElement(element));
    dialog.show();
    if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
        return;
    boolean replaceReturnType = checkForReturnFromMethod(map);
    boolean variableDeclaration = checkForVariableDeclaration(map);
    final GrParameter methodParameter = checkForMethodParameter(map);
    final String qualifiedClassName = dialog.getClassName();
    final String selectedPackageName = StringUtil.getPackageName(qualifiedClassName);
    final String shortName = StringUtil.getShortName(qualifiedClassName);
    final GrTypeDefinition typeDefinition = createClass(project, namedArguments, selectedPackageName, shortName);
    final PsiClass generatedClass = CreateClassActionBase.createClassByType(dialog.getTargetDirectory(), typeDefinition.getName(), PsiManager.getInstance(project), map, GroovyTemplates.GROOVY_CLASS, true);
    final PsiClass replaced = (PsiClass) generatedClass.replace(typeDefinition);
    replaceMapWithClass(project, map, replaced, replaceReturnType, variableDeclaration, methodParameter);
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) CreateClassDialog(com.intellij.codeInsight.intention.impl.CreateClassDialog) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Aggregations

CreateClassDialog (com.intellij.codeInsight.intention.impl.CreateClassDialog)5 Project (com.intellij.openapi.project.Project)2 Nullable (org.jetbrains.annotations.Nullable)2 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Module (com.intellij.openapi.module.Module)1 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 NotNull (org.jetbrains.annotations.NotNull)1 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)1 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)1 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)1 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)1 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)1