use of com.intellij.refactoring.PackageWrapper in project intellij-community by JetBrains.
the class CreateClassDialog method doOKAction.
@Override
protected void doOKAction() {
RecentsManager.getInstance(myProject).registerRecentEntry(RECENTS_KEY, myPackageComponent.getText());
final String packageName = getPackageName();
final String[] errorString = new String[1];
CommandProcessor.getInstance().executeCommand(myProject, () -> {
try {
final PackageWrapper targetPackage = new PackageWrapper(PsiManager.getInstance(myProject), packageName);
final MoveDestination destination = myDestinationCB.selectDirectory(targetPackage, false);
if (destination == null)
return;
myTargetDirectory = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {
@Override
public PsiDirectory compute() {
PsiDirectory baseDir = getBaseDir(packageName);
if (baseDir == null && destination instanceof MultipleRootsMoveDestination) {
errorString[0] = "Destination not found for package '" + packageName + "'";
return null;
}
return destination.getTargetDirectory(baseDir);
}
});
if (myTargetDirectory == null) {
return;
}
errorString[0] = RefactoringMessageUtil.checkCanCreateClass(myTargetDirectory, getClassName());
} catch (IncorrectOperationException e) {
errorString[0] = e.getMessage();
}
}, CodeInsightBundle.message("create.directory.command"), null);
if (errorString[0] != null) {
if (errorString[0].length() > 0) {
Messages.showMessageDialog(myProject, errorString[0], CommonBundle.getErrorTitle(), Messages.getErrorIcon());
}
return;
}
super.doOKAction();
}
use of com.intellij.refactoring.PackageWrapper in project kotlin by JetBrains.
the class MoveKotlinNestedClassesToUpperLevelDialog method getTargetContainer.
@Nullable
private PsiElement getTargetContainer() {
if (targetContainer instanceof PsiDirectory) {
PsiDirectory psiDirectory = (PsiDirectory) targetContainer;
FqName oldPackageFqName = getTargetPackageFqName();
String targetName = packageNameField.getText();
if (!Comparing.equal(oldPackageFqName != null ? oldPackageFqName.asString() : null, targetName)) {
ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
List<VirtualFile> contentSourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(project);
final PackageWrapper newPackage = new PackageWrapper(PsiManager.getInstance(project), targetName);
final VirtualFile targetSourceRoot;
if (contentSourceRoots.size() > 1) {
PsiDirectory initialDir = null;
PsiPackage oldPackage = oldPackageFqName != null ? JavaPsiFacade.getInstance(project).findPackage(oldPackageFqName.asString()) : null;
if (oldPackage != null) {
PsiDirectory[] directories = oldPackage.getDirectories();
VirtualFile root = projectRootManager.getFileIndex().getContentRootForFile(psiDirectory.getVirtualFile());
for (PsiDirectory dir : directories) {
if (Comparing.equal(projectRootManager.getFileIndex().getContentRootForFile(dir.getVirtualFile()), root)) {
initialDir = dir;
}
}
}
VirtualFile sourceRoot = MoveClassesOrPackagesUtil.chooseSourceRoot(newPackage, contentSourceRoots, initialDir);
if (sourceRoot == null)
return null;
targetSourceRoot = sourceRoot;
} else {
targetSourceRoot = contentSourceRoots.get(0);
}
PsiDirectory dir = RefactoringUtil.findPackageDirectoryInSourceRoot(newPackage, targetSourceRoot);
if (dir == null) {
dir = ApplicationManager.getApplication().runWriteAction(new NullableComputable<PsiDirectory>() {
@Override
public PsiDirectory compute() {
try {
return RefactoringUtil.createPackageDirectoryInSourceRoot(newPackage, targetSourceRoot);
} catch (IncorrectOperationException e) {
return null;
}
}
});
}
return dir;
}
return targetContainer;
}
if (targetContainer instanceof KtFile || targetContainer instanceof KtClassOrObject)
return targetContainer;
return null;
}
use of com.intellij.refactoring.PackageWrapper in project kotlin by JetBrains.
the class MoveKotlinTopLevelDeclarationsDialog method selectPackageBasedTargetDirAndDestination.
@Nullable
private Pair<VirtualFile, ? extends MoveDestination> selectPackageBasedTargetDirAndDestination(boolean askIfDoesNotExist) {
String packageName = getTargetPackage();
RecentsManager.getInstance(myProject).registerRecentEntry(RECENTS_KEY, packageName);
PackageWrapper targetPackage = new PackageWrapper(PsiManager.getInstance(myProject), packageName);
if (!targetPackage.exists() && askIfDoesNotExist) {
int ret = Messages.showYesNoDialog(myProject, RefactoringBundle.message("package.does.not.exist", packageName), RefactoringBundle.message("move.title"), Messages.getQuestionIcon());
if (ret != Messages.YES)
return null;
}
DirectoryChooser.ItemWrapper selectedItem = (DirectoryChooser.ItemWrapper) destinationFolderCB.getComboBox().getSelectedItem();
PsiDirectory selectedPsiDirectory = selectedItem != null ? selectedItem.getDirectory() : null;
if (selectedPsiDirectory == null) {
if (initialTargetDirectory != null) {
selectedPsiDirectory = initialTargetDirectory;
} else {
return Pair.create(null, new MultipleRootsMoveDestination(targetPackage));
}
}
VirtualFile targetDirectory = selectedPsiDirectory.getVirtualFile();
return Pair.create(targetDirectory, new AutocreatingSingleSourceRootMoveDestination(targetPackage, targetDirectory));
}
Aggregations