Search in sources :

Example 1 with GenerateEqualsWizard

use of com.intellij.codeInsight.generation.ui.GenerateEqualsWizard in project intellij-community by JetBrains.

the class GenerateEqualsHandler method chooseOriginalMembers.

@Override
protected ClassMember[] chooseOriginalMembers(PsiClass aClass, Project project, Editor editor) {
    myEqualsFields = null;
    myHashCodeFields = null;
    myNonNullFields = PsiField.EMPTY_ARRAY;
    GlobalSearchScope scope = aClass.getResolveScope();
    final PsiMethod equalsMethod = GenerateEqualsHelper.findMethod(aClass, GenerateEqualsHelper.getEqualsSignature(project, scope));
    final PsiMethod hashCodeMethod = GenerateEqualsHelper.findMethod(aClass, GenerateEqualsHelper.getHashCodeSignature());
    boolean needEquals = needToGenerateMethod(equalsMethod);
    boolean needHashCode = needToGenerateMethod(hashCodeMethod);
    if (!needEquals && !needHashCode) {
        String text = aClass instanceof PsiAnonymousClass ? CodeInsightBundle.message("generate.equals.and.hashcode.already.defined.warning.anonymous") : CodeInsightBundle.message("generate.equals.and.hashcode.already.defined.warning", aClass.getQualifiedName());
        if (Messages.showYesNoDialog(project, text, CodeInsightBundle.message("generate.equals.and.hashcode.already.defined.title"), Messages.getQuestionIcon()) == Messages.YES) {
            if (!ApplicationManager.getApplication().runWriteAction(new Computable<Boolean>() {

                @Override
                public Boolean compute() {
                    try {
                        equalsMethod.delete();
                        hashCodeMethod.delete();
                        return Boolean.TRUE;
                    } catch (IncorrectOperationException e) {
                        LOG.error(e);
                        return Boolean.FALSE;
                    }
                }
            }).booleanValue()) {
                return null;
            } else {
                needEquals = needHashCode = true;
            }
        } else {
            return null;
        }
    }
    boolean hasNonStaticFields = hasNonStaticFields(aClass);
    if (!hasNonStaticFields) {
        HintManager.getInstance().showErrorHint(editor, "No fields to include in equals/hashCode have been found");
        return null;
    }
    GenerateEqualsWizard wizard = new GenerateEqualsWizard(project, aClass, needEquals, needHashCode);
    if (!wizard.showAndGet()) {
        return null;
    }
    myEqualsFields = wizard.getEqualsFields();
    myHashCodeFields = wizard.getHashCodeFields();
    myNonNullFields = wizard.getNonNullFields();
    return DUMMY_RESULT;
}
Also used : GenerateEqualsWizard(com.intellij.codeInsight.generation.ui.GenerateEqualsWizard) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 2 with GenerateEqualsWizard

use of com.intellij.codeInsight.generation.ui.GenerateEqualsWizard in project intellij-community by JetBrains.

the class GroovyGenerateEqualsHandler method chooseOriginalMembers.

@Override
@Nullable
protected ClassMember[] chooseOriginalMembers(PsiClass aClass, Project project) {
    myEqualsFields = null;
    myHashCodeFields = null;
    myNonNullFields = PsiField.EMPTY_ARRAY;
    GlobalSearchScope scope = aClass.getResolveScope();
    final PsiMethod equalsMethod = GroovyGenerateEqualsHelper.findMethod(aClass, GroovyGenerateEqualsHelper.getEqualsSignature(project, scope));
    final PsiMethod hashCodeMethod = GroovyGenerateEqualsHelper.findMethod(aClass, GroovyGenerateEqualsHelper.getHashCodeSignature());
    boolean needEquals = equalsMethod == null;
    boolean needHashCode = hashCodeMethod == null;
    if (!needEquals && !needHashCode) {
        String text = aClass instanceof PsiAnonymousClass ? GroovyCodeInsightBundle.message("generate.equals.and.hashcode.already.defined.warning.anonymous") : GroovyCodeInsightBundle.message("generate.equals.and.hashcode.already.defined.warning", aClass.getQualifiedName());
        if (Messages.showYesNoDialog(project, text, GroovyCodeInsightBundle.message("generate.equals.and.hashcode.already.defined.title"), Messages.getQuestionIcon()) == Messages.YES) {
            if (!ApplicationManager.getApplication().runWriteAction(new Computable<Boolean>() {

                @Override
                public Boolean compute() {
                    try {
                        equalsMethod.delete();
                        hashCodeMethod.delete();
                        return Boolean.TRUE;
                    } catch (IncorrectOperationException e) {
                        LOG.error(e);
                        return Boolean.FALSE;
                    }
                }
            }).booleanValue()) {
                return null;
            } else {
                needEquals = needHashCode = true;
            }
        } else {
            return null;
        }
    }
    GenerateEqualsWizard wizard = new GenerateEqualsWizard(project, aClass, needEquals, needHashCode);
    if (!wizard.showAndGet()) {
        return null;
    }
    myEqualsFields = wizard.getEqualsFields();
    myHashCodeFields = wizard.getHashCodeFields();
    myNonNullFields = wizard.getNonNullFields();
    return DUMMY_RESULT;
}
Also used : GenerateEqualsWizard(com.intellij.codeInsight.generation.ui.GenerateEqualsWizard) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiMethod(com.intellij.psi.PsiMethod) PsiAnonymousClass(com.intellij.psi.PsiAnonymousClass) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GenerateEqualsWizard (com.intellij.codeInsight.generation.ui.GenerateEqualsWizard)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 PsiAnonymousClass (com.intellij.psi.PsiAnonymousClass)1 PsiMethod (com.intellij.psi.PsiMethod)1 Nullable (org.jetbrains.annotations.Nullable)1