Search in sources :

Example 16 with PackageWrapper

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();
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) MultipleRootsMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.MultipleRootsMoveDestination) MoveDestination(com.intellij.refactoring.MoveDestination) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PackageWrapper(com.intellij.refactoring.PackageWrapper) Computable(com.intellij.openapi.util.Computable) MultipleRootsMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.MultipleRootsMoveDestination)

Example 17 with PackageWrapper

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FqName(org.jetbrains.kotlin.name.FqName) NullableComputable(com.intellij.openapi.util.NullableComputable) PackageWrapper(com.intellij.refactoring.PackageWrapper) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with PackageWrapper

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));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AutocreatingSingleSourceRootMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.AutocreatingSingleSourceRootMoveDestination) PackageWrapper(com.intellij.refactoring.PackageWrapper) DirectoryChooser(com.intellij.ide.util.DirectoryChooser) MultipleRootsMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.MultipleRootsMoveDestination) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PackageWrapper (com.intellij.refactoring.PackageWrapper)18 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 Nullable (org.jetbrains.annotations.Nullable)5 DirectoryChooser (com.intellij.ide.util.DirectoryChooser)3 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)3 PsiDirectory (com.intellij.psi.PsiDirectory)3 Module (com.intellij.openapi.module.Module)2 ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)2 Computable (com.intellij.openapi.util.Computable)2 NullableComputable (com.intellij.openapi.util.NullableComputable)2 MoveDestination (com.intellij.refactoring.MoveDestination)2 AutocreatingSingleSourceRootMoveDestination (com.intellij.refactoring.move.moveClassesOrPackages.AutocreatingSingleSourceRootMoveDestination)2 DestinationFolderComboBox (com.intellij.refactoring.move.moveClassesOrPackages.DestinationFolderComboBox)2 MoveClassesOrPackagesProcessor (com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesProcessor)2 MultipleRootsMoveDestination (com.intellij.refactoring.move.moveClassesOrPackages.MultipleRootsMoveDestination)2 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 FileTemplateManager (com.intellij.ide.fileTemplates.FileTemplateManager)1 ReadAction (com.intellij.openapi.application.ReadAction)1 Result (com.intellij.openapi.application.Result)1