From b341c59cb3f28001d2d689c9fe02bd51c7d456c4 Mon Sep 17 00:00:00 2001 From: Eugene Vigdorchik Date: Sat, 12 Feb 2005 17:23:02 +0300 Subject: [PATCH] (no message) --- ...ConstructorWithFactoryRefactoringImpl.java | 4 +- .../ReplaceConstructorWithFactoryDialog.java | 64 +++++++++-------- .../ReplaceConstructorWithFactoryHandler.java | 72 +------------------ ...eplaceConstructorWithFactoryProcessor.java | 31 ++------ .../ReplaceConstructorWithFactoryTest.java | 6 +- 5 files changed, 49 insertions(+), 128 deletions(-) diff --git a/source/com/intellij/refactoring/openapi/impl/ReplaceConstructorWithFactoryRefactoringImpl.java b/source/com/intellij/refactoring/openapi/impl/ReplaceConstructorWithFactoryRefactoringImpl.java index 3576e1dd16d8..5d3026506d67 100644 --- a/source/com/intellij/refactoring/openapi/impl/ReplaceConstructorWithFactoryRefactoringImpl.java +++ b/source/com/intellij/refactoring/openapi/impl/ReplaceConstructorWithFactoryRefactoringImpl.java @@ -17,11 +17,11 @@ import com.intellij.refactoring.replaceConstructorWithFactory.ReplaceConstructor */ public class ReplaceConstructorWithFactoryRefactoringImpl extends RefactoringImpl implements ReplaceConstructorWithFactoryRefactoring { ReplaceConstructorWithFactoryRefactoringImpl(Project project, PsiMethod method, PsiClass targetClass, String factoryName) { - super(new ReplaceConstructorWithFactoryProcessor(project, method, targetClass, factoryName, BaseRefactoringProcessor.EMPTY_CALLBACK)); + super(new ReplaceConstructorWithFactoryProcessor(project, method, null, targetClass, factoryName)); } ReplaceConstructorWithFactoryRefactoringImpl(Project project, PsiClass originalClass, PsiClass targetClass, String factoryName) { - super(new ReplaceConstructorWithFactoryProcessor(project, originalClass, targetClass, factoryName, BaseRefactoringProcessor.EMPTY_CALLBACK)); + super(new ReplaceConstructorWithFactoryProcessor(project, null, originalClass, targetClass, factoryName)); } public PsiClass getOriginalClass() { diff --git a/source/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryDialog.java b/source/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryDialog.java index f2cf4e75b33b..13cdbc820646 100644 --- a/source/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryDialog.java +++ b/source/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryDialog.java @@ -1,18 +1,16 @@ package com.intellij.refactoring.replaceConstructorWithFactory; -import com.intellij.ide.util.TreeClassChooserDialog; import com.intellij.ide.util.TreeClassChooser; import com.intellij.ide.util.TreeClassChooserFactory; import com.intellij.openapi.help.HelpManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.TextFieldWithBrowseButton; -import com.intellij.psi.PsiClass; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiModifier; -import com.intellij.psi.PsiNameHelper; +import com.intellij.psi.*; +import com.intellij.psi.search.GlobalSearchScope; import com.intellij.refactoring.HelpID; import com.intellij.refactoring.RefactoringDialog; import com.intellij.refactoring.ui.NameSuggestionsField; +import com.intellij.refactoring.util.RefactoringMessageUtil; import javax.swing.*; import java.awt.*; @@ -24,27 +22,19 @@ import java.util.ArrayList; * @author dsl */ public class ReplaceConstructorWithFactoryDialog extends RefactoringDialog { - private final Callback myCallback; - private NameSuggestionsField myNameField; private final TextFieldWithBrowseButton myTfTargetClassName; private JComboBox myTargetClassNameCombo; - private Project myProject; private PsiClass myContainingClass; + private final PsiMethod myConstructor; private final boolean myIsInner; - public interface Callback { - void run(ReplaceConstructorWithFactoryDialog dialog); - } - - ReplaceConstructorWithFactoryDialog(Project project, PsiClass containingClass, Callback callback) { + ReplaceConstructorWithFactoryDialog(Project project, PsiMethod constructor, PsiClass containingClass) { super(project, true); - myProject = project; - - myCallback = callback; myContainingClass = containingClass; + myConstructor = constructor; myIsInner = myContainingClass.getContainingClass() != null - && !myContainingClass.hasModifierProperty(PsiModifier.STATIC); + && !myContainingClass.hasModifierProperty(PsiModifier.STATIC); setTitle(ReplaceConstructorWithFactoryHandler.REFACTORING_NAME); @@ -57,7 +47,7 @@ public class ReplaceConstructorWithFactoryDialog extends RefactoringDialog { return myNameField.getName(); } - protected boolean hasHelpAction () { return false; } + protected boolean hasHelpAction() { return false; } protected void doHelpAction() { HelpManager.getInstance().invokeHelp(HelpID.REPLACE_CONSTRUCTOR_WITH_FACTORY); @@ -70,8 +60,9 @@ public class ReplaceConstructorWithFactoryDialog extends RefactoringDialog { public String getTargetClassName() { if (!myIsInner) { return myTfTargetClassName.getText(); - } else { - return (String) myTargetClassNameCombo.getSelectedItem(); + } + else { + return (String)myTargetClassNameCombo.getSelectedItem(); } } @@ -92,9 +83,9 @@ public class ReplaceConstructorWithFactoryDialog extends RefactoringDialog { final String[] nameSuggestions = new String[]{ "create" + myContainingClass.getName(), "new" + myContainingClass.getName(), - "getInstance" - }; - myNameField = new NameSuggestionsField(nameSuggestions, myProject); + "getInstance" + }; + myNameField = new NameSuggestionsField(nameSuggestions, getProject()); myNameField.addDataChangedListener(new NameSuggestionsField.DataChanged() { public void dataChanged() { validateButtons(); @@ -122,11 +113,12 @@ public class ReplaceConstructorWithFactoryDialog extends RefactoringDialog { targetClassPanel.add(label, BorderLayout.NORTH); targetClassPanel.add(myTfTargetClassName, BorderLayout.CENTER); myTfTargetClassName.setText(myContainingClass.getQualifiedName()); - } else { + } + else { ArrayList list = new ArrayList(); PsiElement parent = myContainingClass; while (parent instanceof PsiClass) { - list.add(((PsiClass) parent).getQualifiedName()); + list.add(((PsiClass)parent).getQualifiedName()); parent = parent.getParent(); } @@ -145,7 +137,7 @@ public class ReplaceConstructorWithFactoryDialog extends RefactoringDialog { private class ChooseClassAction implements ActionListener { public void actionPerformed(ActionEvent e) { - TreeClassChooser chooser = TreeClassChooserFactory.getInstance(myProject).createProjectScopeChooser("Choose Destination Class"); + TreeClassChooser chooser = TreeClassChooserFactory.getInstance(getProject()).createProjectScopeChooser("Choose Destination Class"); chooser.selectDirectory(myContainingClass.getContainingFile().getContainingDirectory()); chooser.showDialog(); PsiClass aClass = chooser.getSelectedClass(); @@ -161,7 +153,24 @@ public class ReplaceConstructorWithFactoryDialog extends RefactoringDialog { } protected void doAction() { - myCallback.run(this); + final Project project = getProject(); + final PsiManager manager = PsiManager.getInstance(project); + final String targetClassName = getTargetClassName(); + final PsiClass targetClass = manager.findClass(targetClassName, GlobalSearchScope.allScope(project)); + if (targetClass == null) { + String message = + "Cannot perform the refactoring.\n" + + "Class " + targetClassName + " not found."; + RefactoringMessageUtil.showErrorMessage(ReplaceConstructorWithFactoryHandler.REFACTORING_NAME, + message, null, project); + return; + } + if (!targetClass.isWritable()) { + if (!RefactoringMessageUtil.checkReadOnlyStatus(project, targetClass)) return; + } + + invokeRefactoring(new ReplaceConstructorWithFactoryProcessor(project, myConstructor, myContainingClass, + targetClass, getName())); } protected boolean areButtonsValid() { @@ -169,5 +178,4 @@ public class ReplaceConstructorWithFactoryDialog extends RefactoringDialog { final PsiNameHelper nameHelper = myContainingClass.getManager().getNameHelper(); return nameHelper.isIdentifier(name); } - } \ No newline at end of file diff --git a/source/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryHandler.java b/source/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryHandler.java index f19d09ae1301..321c60dbcda8 100644 --- a/source/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryHandler.java +++ b/source/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryHandler.java @@ -4,10 +4,8 @@ import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.ScrollType; import com.intellij.openapi.project.Project; -import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.Messages; import com.intellij.psi.*; -import com.intellij.psi.search.GlobalSearchScope; import com.intellij.refactoring.HelpID; import com.intellij.refactoring.RefactoringActionHandler; import com.intellij.refactoring.util.RefactoringMessageUtil; @@ -81,9 +79,7 @@ public class ReplaceConstructorWithFactoryHandler if (!aClass.isWritable()) { if (!RefactoringMessageUtil.checkReadOnlyStatus(myProject, aClass)) return; } - final ReplaceConstructorWithFactoryDialog dialog = - new ReplaceConstructorWithFactoryDialog(myProject, aClass, new MyCallbackForClass(aClass)); - dialog.show(); + new ReplaceConstructorWithFactoryDialog(myProject, null, aClass).show(); } private void showJspOrLocalClassMessage() { @@ -122,70 +118,6 @@ public class ReplaceConstructorWithFactoryHandler if (!method.isWritable()) { if (!RefactoringMessageUtil.checkReadOnlyStatus(myProject, method)) return; } - final ReplaceConstructorWithFactoryDialog dialog = - new ReplaceConstructorWithFactoryDialog(myProject, - method.getContainingClass(), new MyCallbackForMethod(method)); - dialog.show(); - } - - - private class MyCallbackForMethod implements ReplaceConstructorWithFactoryDialog.Callback { - private PsiMethod myMethod; - - public MyCallbackForMethod(PsiMethod method) { - myMethod = method; - } - - public void run(final ReplaceConstructorWithFactoryDialog dialog) { - final PsiManager manager = myMethod.getManager(); - final String targetClassName = dialog.getTargetClassName(); - final PsiClass targetClass = manager.findClass(targetClassName, GlobalSearchScope.allScope(myProject)); - if (targetClass == null) { - String message = - "Cannot perform the refactoring.\n" + - "Class " + targetClassName + " not found."; - RefactoringMessageUtil.showErrorMessage(REFACTORING_NAME, message, null/*HelpID.REPLACE_CONSTRUCTOR_WITH_FACTORY*/, myProject); - return; - } - if (!targetClass.isWritable()) { - if (!RefactoringMessageUtil.checkReadOnlyStatus(myProject, targetClass)) return; - } - - new ReplaceConstructorWithFactoryProcessor(myProject, myMethod, - targetClass, dialog.getName(), new Runnable() { - public void run() { - dialog.close(DialogWrapper.CANCEL_EXIT_CODE); - } - } - ).run(null); - } - } - - private class MyCallbackForClass implements ReplaceConstructorWithFactoryDialog.Callback { - private PsiClass myClass; - - public MyCallbackForClass(PsiClass aClass) { - myClass = aClass; - } - - public void run(final ReplaceConstructorWithFactoryDialog dialog) { - final PsiManager manager = myClass.getManager(); - final String targetClassName = dialog.getTargetClassName(); - final PsiClass targetClass = manager.findClass(targetClassName, GlobalSearchScope.allScope(myProject)); - if (targetClass == null) { - String message = - "Cannot perform the refactoring.\n" + - "Class " + targetClassName + " not found."; - RefactoringMessageUtil.showErrorMessage(REFACTORING_NAME, message, null/*HelpID.REPLACE_CONSTRUCTOR_WITH_FACTORY*/, myProject); - return; - } - new ReplaceConstructorWithFactoryProcessor(myProject, myClass, - targetClass, dialog.getName(), new Runnable() { - public void run() { - dialog.close(DialogWrapper.CANCEL_EXIT_CODE); - } - } - ).run(null); - } + new ReplaceConstructorWithFactoryDialog(myProject, method, method.getContainingClass()).show(); } } diff --git a/source/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryProcessor.java b/source/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryProcessor.java index 90e9d4934a21..79934736105f 100644 --- a/source/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryProcessor.java +++ b/source/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryProcessor.java @@ -39,30 +39,13 @@ public class ReplaceConstructorWithFactoryProcessor extends BaseRefactoringProce private boolean myIsInner; public ReplaceConstructorWithFactoryProcessor(Project project, - PsiMethod constructor, - PsiClass targetClass, - String factoryName, - Runnable prepareSuccessfulCallback) { - super(project, prepareSuccessfulCallback); - myOriginalClass = null; - myConstructor = constructor; - myFactoryName = factoryName; - myTargetClass = targetClass; - LOG.assertTrue(myConstructor.isConstructor()); - myManager = PsiManager.getInstance(project); - myFactory = PsiManager.getInstance(project).getElementFactory(); - - myIsInner = isInner(myConstructor.getContainingClass()); - } - - public ReplaceConstructorWithFactoryProcessor(Project project, - PsiClass aClass, - PsiClass targetClass, - String factoryName, - Runnable prepareSuccessfulCallback) { - super(project, prepareSuccessfulCallback); - myOriginalClass = aClass; - myConstructor = null; + PsiMethod originalConstructor, + PsiClass originalClass, + PsiClass targetClass, + String factoryName) { + super(project); + myOriginalClass = originalClass; + myConstructor = originalConstructor; myTargetClass = targetClass; myFactoryName = factoryName; myManager = PsiManager.getInstance(project); diff --git a/testSource/com/intellij/refactoring/ReplaceConstructorWithFactoryTest.java b/testSource/com/intellij/refactoring/ReplaceConstructorWithFactoryTest.java index 272e776d3fc8..db57496e66fa 100644 --- a/testSource/com/intellij/refactoring/ReplaceConstructorWithFactoryTest.java +++ b/testSource/com/intellij/refactoring/ReplaceConstructorWithFactoryTest.java @@ -66,16 +66,14 @@ public class ReplaceConstructorWithFactoryTest extends CodeInsightTestCase { targetClass = constructor.getContainingClass(); } replaceConstructorWithFactoryProcessor = new ReplaceConstructorWithFactoryProcessor( - myProject, constructor, targetClass, "new" + constructor.getName(), null - ); + myProject, constructor, constructor.getContainingClass(), targetClass, "new" + constructor.getName()); } else { if (targetClass == null) { targetClass = aClass; } replaceConstructorWithFactoryProcessor = new ReplaceConstructorWithFactoryProcessor( - myProject, aClass, targetClass, "new" + aClass.getName(), null - ); + myProject, null, aClass, targetClass, "new" + aClass.getName()); } replaceConstructorWithFactoryProcessor.testRun(); }