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);
}
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());
}
}
}
}
}
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;
}
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();
}
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);
}
Aggregations