mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-04 17:20:55 +07:00
root chooser: replace constructor with builder
This commit is contained in:
@@ -29,12 +29,15 @@ import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.wm.IdeFocusManager;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.refactoring.PackageWrapper;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.move.moveClassesOrPackages.DestinationFolderComboBox;
|
||||
import com.intellij.refactoring.ui.PackageNameReferenceEditorCombo;
|
||||
import com.intellij.refactoring.ui.RefactoringDialog;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.ui.*;
|
||||
import com.intellij.util.ui.Table;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -59,6 +62,8 @@ public class ReplaceConstructorWithBuilderDialog extends RefactoringDialog {
|
||||
private JTextField myNewClassName;
|
||||
private ReferenceEditorComboWithBrowseButton myPackageTextField;
|
||||
private ReferenceEditorComboWithBrowseButton myExistentClassTF;
|
||||
private ComboboxWithBrowseButton myDestinationCb;
|
||||
private JPanel myCreateNewPanel;
|
||||
|
||||
private static final Logger LOG = Logger.getInstance("#" + ReplaceConstructorWithBuilderDialog.class.getName());
|
||||
private final LinkedHashMap<String, ParameterData> myParametersMap;
|
||||
@@ -99,6 +104,7 @@ public class ReplaceConstructorWithBuilderDialog extends RefactoringDialog {
|
||||
if (builderClass != null && !CommonRefactoringUtil.checkReadOnlyStatus(myProject, builderClass)) return;
|
||||
}
|
||||
invokeRefactoring(new ReplaceConstructorWithBuilderProcessor(getProject(), myConstructors, myParametersMap, className, packageName,
|
||||
((DestinationFolderComboBox)myDestinationCb).selectDirectory(new PackageWrapper(myConstructors[0].getManager(), packageName), false),
|
||||
myCreateBuilderClassRadioButton.isSelected()));
|
||||
}
|
||||
|
||||
@@ -139,8 +145,7 @@ public class ReplaceConstructorWithBuilderDialog extends RefactoringDialog {
|
||||
}
|
||||
|
||||
private void setEnabled(final boolean createNew) {
|
||||
myNewClassName.setEnabled(createNew);
|
||||
myPackageTextField.setEnabled(createNew);
|
||||
UIUtil.setEnabled(myCreateNewPanel, createNew, true);
|
||||
myExistentClassTF.setEnabled(!createNew);
|
||||
}
|
||||
|
||||
@@ -222,6 +227,13 @@ public class ReplaceConstructorWithBuilderDialog extends RefactoringDialog {
|
||||
new PackageNameReferenceEditorCombo(((PsiJavaFile)myConstructors[0].getContainingFile()).getPackageName(), myProject, RECENT_KEYS, RefactoringBundle.message("choose.destination.package"));
|
||||
myPackageTextField.getChildComponent().getDocument().addDocumentListener(adapter);
|
||||
|
||||
myDestinationCb = new DestinationFolderComboBox() {
|
||||
@Override
|
||||
public String getTargetPackage() {
|
||||
return myPackageTextField.getText().trim();
|
||||
}
|
||||
};
|
||||
((DestinationFolderComboBox)myDestinationCb).setData(myProject, myConstructors[0].getContainingFile().getContainingDirectory(), myPackageTextField.getChildComponent());
|
||||
|
||||
myExistentClassTF = new ReferenceEditorComboWithBrowseButton(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.refactoring.replaceConstructorWithBuilder.ReplaceConstructorWithBuilderDialog">
|
||||
<grid id="27dc6" binding="myWholePanel" layout-manager="GridBagLayout">
|
||||
<constraints>
|
||||
<xy x="387" y="313" width="358" height="190"/>
|
||||
<xy x="387" y="313" width="358" height="210"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
@@ -15,7 +15,7 @@
|
||||
</hspacer>
|
||||
<vspacer id="57b6e">
|
||||
<constraints>
|
||||
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
<gridbag weightx="0.0" weighty="0.0"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
@@ -30,14 +30,14 @@
|
||||
</component>
|
||||
<component id="97fc9" class="javax.swing.JRadioButton" binding="myExistingBuilderClassRadioButton">
|
||||
<constraints>
|
||||
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
<gridbag top="5" left="0" bottom="0" right="0" weightx="0.0" weighty="0.0"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="&Use existing"/>
|
||||
</properties>
|
||||
</component>
|
||||
<grid id="c4e05" layout-manager="GridBagLayout">
|
||||
<grid id="c4e05" binding="myCreateNewPanel" layout-manager="GridBagLayout">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
<gridbag top="5" left="20" bottom="0" right="0" weightx="0.0" weighty="0.0"/>
|
||||
@@ -80,11 +80,27 @@
|
||||
<text value="&Package for new builder"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="bb743" class="javax.swing.JLabel">
|
||||
<constraints>
|
||||
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
<gridbag weightx="0.0" weighty="0.0"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Target"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="3e899" class="com.intellij.ui.ComboboxWithBrowseButton" binding="myDestinationCb" custom-create="true">
|
||||
<constraints>
|
||||
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<gridbag weightx="1.0" weighty="0.0"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
<grid id="414f6" layout-manager="GridBagLayout">
|
||||
<constraints>
|
||||
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
<gridbag top="5" left="20" bottom="0" right="0" weightx="1.0" weighty="0.0"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
|
||||
@@ -37,6 +37,7 @@ import com.intellij.psi.util.PropertyUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.refactoring.MoveDestination;
|
||||
import com.intellij.refactoring.replaceConstructorWithBuilder.usageInfo.ReplaceConstructorWithSettersChainInfo;
|
||||
import com.intellij.refactoring.util.FixableUsageInfo;
|
||||
import com.intellij.refactoring.util.FixableUsagesRefactoringProcessor;
|
||||
@@ -48,6 +49,7 @@ import com.intellij.util.containers.MultiMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -59,6 +61,7 @@ public class ReplaceConstructorWithBuilderProcessor extends FixableUsagesRefacto
|
||||
private final String myPackageName;
|
||||
private final boolean myCreateNewBuilderClass;
|
||||
private final PsiElementFactory myElementFactory;
|
||||
private MoveDestination myMoveDestination;
|
||||
|
||||
|
||||
public ReplaceConstructorWithBuilderProcessor(Project project,
|
||||
@@ -66,8 +69,9 @@ public class ReplaceConstructorWithBuilderProcessor extends FixableUsagesRefacto
|
||||
Map<String, ParameterData> parametersMap,
|
||||
String className,
|
||||
String packageName,
|
||||
boolean createNewBuilderClass) {
|
||||
MoveDestination moveDestination, boolean createNewBuilderClass) {
|
||||
super(project);
|
||||
myMoveDestination = moveDestination;
|
||||
myElementFactory = JavaPsiFacade.getInstance(myProject).getElementFactory();
|
||||
myConstructors = constructors;
|
||||
myParametersMap = parametersMap;
|
||||
@@ -108,9 +112,14 @@ public class ReplaceConstructorWithBuilderProcessor extends FixableUsagesRefacto
|
||||
|
||||
final PsiFile containingFile = myConstructors[0].getContainingFile();
|
||||
final PsiDirectory containingDirectory = containingFile.getContainingDirectory();
|
||||
final PsiDirectory directory;
|
||||
if (myMoveDestination != null) {
|
||||
directory = myMoveDestination.getTargetDirectory(containingDirectory);
|
||||
} else {
|
||||
final Module module = ModuleUtil.findModuleForPsiElement(containingFile);
|
||||
assert module != null;
|
||||
final PsiDirectory directory = PackageUtil.findOrCreateDirectoryForPackage(module, myPackageName, containingDirectory, true, true);
|
||||
directory = PackageUtil.findOrCreateDirectoryForPackage(module, myPackageName, containingDirectory, true, true);
|
||||
}
|
||||
|
||||
if (directory != null) {
|
||||
|
||||
@@ -281,6 +290,10 @@ public class ReplaceConstructorWithBuilderProcessor extends FixableUsagesRefacto
|
||||
conflicts.putValue(builderClass, "Class with chosen name already exist.");
|
||||
}
|
||||
|
||||
if (myMoveDestination != null && myCreateNewBuilderClass) {
|
||||
myMoveDestination.analyzeModuleConflicts(Collections.<PsiElement>emptyList(), conflicts, refUsages.get());
|
||||
}
|
||||
|
||||
final PsiMethod commonConstructor = getMostCommonConstructor();
|
||||
if (commonConstructor == null) {
|
||||
conflicts.putValue(null, "Found constructors are not reducible to simple chain");
|
||||
|
||||
@@ -253,8 +253,6 @@ public class RefactoringConflictsUtil {
|
||||
boolean isInTestSources = ModuleRootManager.getInstance(targetModule).getFileIndex().isInTestSourceContent(vFile);
|
||||
NextUsage:
|
||||
for (UsageInfo usage : usages) {
|
||||
if (usage instanceof MoveRenameUsageInfo) {
|
||||
final MoveRenameUsageInfo moveRenameUsageInfo = (MoveRenameUsageInfo)usage;
|
||||
final PsiElement element = usage.getElement();
|
||||
if (element != null && PsiTreeUtil.getParentOfType(element, PsiImportStatement.class, false) == null) {
|
||||
|
||||
@@ -278,8 +276,14 @@ public class RefactoringConflictsUtil {
|
||||
Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(usageVFile);
|
||||
if (module != null) {
|
||||
final String message;
|
||||
final PsiElement referencedElement = moveRenameUsageInfo.getReferencedElement();
|
||||
assert referencedElement != null : moveRenameUsageInfo;
|
||||
final PsiElement referencedElement;
|
||||
if (usage instanceof MoveRenameUsageInfo) {
|
||||
referencedElement = ((MoveRenameUsageInfo)usage).getReferencedElement();
|
||||
}
|
||||
else {
|
||||
referencedElement = usage.getElement();
|
||||
}
|
||||
assert referencedElement != null : usage;
|
||||
if (module == targetModule && isInTestSources) {
|
||||
message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.production.of.module.2",
|
||||
RefactoringUIUtil.getDescription(referencedElement, true),
|
||||
@@ -300,4 +304,3 @@ public class RefactoringConflictsUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ public class ReplaceConstructorWithBuilderTest extends MultiFileTestCase {
|
||||
}
|
||||
}
|
||||
try {
|
||||
new ReplaceConstructorWithBuilderProcessor(getProject(), constructors, map, "Builder", packageName, createNewBuilderClass).run();
|
||||
new ReplaceConstructorWithBuilderProcessor(getProject(), constructors, map, "Builder", packageName, null, createNewBuilderClass).run();
|
||||
if (conflicts != null) {
|
||||
fail("Conflicts were not detected:" + conflicts);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user