(no message)

This commit is contained in:
Eugene Vigdorchik
2005-02-12 17:23:02 +03:00
parent 99434abcbc
commit b341c59cb3
5 changed files with 49 additions and 128 deletions
@@ -17,11 +17,11 @@ import com.intellij.refactoring.replaceConstructorWithFactory.ReplaceConstructor
*/
public class ReplaceConstructorWithFactoryRefactoringImpl extends RefactoringImpl<ReplaceConstructorWithFactoryProcessor> 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() {
@@ -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<String> list = new ArrayList<String>();
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);
}
}
@@ -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();
}
}
@@ -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);
@@ -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();
}