Search in sources :

Example 1 with SingleSourceRootMoveDestination

use of com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination in project intellij-community by JetBrains.

the class MoveToPackageFix method applyFix.

@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    PsiElement element = descriptor.getPsiElement();
    if (element == null)
        return;
    final PsiFile myFile = element.getContainingFile();
    if (!FileModificationService.getInstance().prepareFileForWrite(myFile))
        return;
    try {
        String error;
        PsiDirectory directory = null;
        try {
            directory = MoveClassesOrPackagesUtil.chooseDestinationPackage(project, myTargetPackage, myFile.getContainingDirectory());
            if (directory == null) {
                return;
            }
            error = RefactoringMessageUtil.checkCanCreateFile(directory, myFile.getName());
        } catch (IncorrectOperationException e) {
            error = e.getLocalizedMessage();
        }
        if (error != null) {
            Messages.showMessageDialog(project, error, CommonBundle.getErrorTitle(), Messages.getErrorIcon());
            return;
        }
        new MoveClassesOrPackagesProcessor(project, ((PsiJavaFile) myFile).getClasses(), new SingleSourceRootMoveDestination(PackageWrapper.create(JavaDirectoryService.getInstance().getPackage(directory)), directory), false, false, null).run();
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : SingleSourceRootMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination) IncorrectOperationException(com.intellij.util.IncorrectOperationException) MoveClassesOrPackagesProcessor(com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesProcessor)

Example 2 with SingleSourceRootMoveDestination

use of com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination in project intellij-community by JetBrains.

the class ConfigurationRefactoringsTest method move.

private void move(final PsiElement psiElement, String packageName) {
    VirtualFile pkgFile = mySource.createPackageDir(packageName);
    final PsiDirectory toDir = PsiManager.getInstance(myProject).findDirectory(pkgFile);
    assertNotNull(toDir);
    PackageWrapper wrapper = PackageWrapper.create(JavaDirectoryService.getInstance().getPackage(toDir));
    new MoveClassesOrPackagesProcessor(myProject, new PsiElement[] { psiElement }, new SingleSourceRootMoveDestination(wrapper, toDir), false, false, null).run();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SingleSourceRootMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination) PackageWrapper(com.intellij.refactoring.PackageWrapper) MoveClassesOrPackagesProcessor(com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesProcessor)

Example 3 with SingleSourceRootMoveDestination

use of com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination in project android by JetBrains.

the class AndroidRenameTest method doMovePackage.

private void doMovePackage(String packageName, String newPackageName) throws Exception {
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
    final PsiPackage aPackage = facade.findPackage(packageName);
    final PsiPackage newParentPackage = facade.findPackage(newPackageName);
    assertNotNull(newParentPackage);
    final PsiDirectory[] dirs = newParentPackage.getDirectories();
    assertEquals(dirs.length, 1);
    new MoveClassesOrPackagesProcessor(getProject(), new PsiElement[] { aPackage }, new SingleSourceRootMoveDestination(PackageWrapper.create(newParentPackage), dirs[0]), true, false, null).run();
    FileDocumentManager.getInstance().saveAllDocuments();
}
Also used : SingleSourceRootMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination) MoveClassesOrPackagesProcessor(com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesProcessor)

Example 4 with SingleSourceRootMoveDestination

use of com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination in project intellij-community by JetBrains.

the class MoveClassTest method performAction.

private void performAction(String[] classNames, String newPackageName) throws Exception {
    final PsiClass[] classes = new PsiClass[classNames.length];
    for (int i = 0; i < classes.length; i++) {
        String className = classNames[i];
        classes[i] = myJavaFacade.findClass(className, GlobalSearchScope.projectScope(getProject()));
        assertNotNull("Class " + className + " not found", classes[i]);
    }
    PsiPackage aPackage = JavaPsiFacade.getInstance(myPsiManager.getProject()).findPackage(newPackageName);
    assertNotNull("Package " + newPackageName + " not found", aPackage);
    final PsiDirectory[] dirs = aPackage.getDirectories();
    assertEquals(dirs.length, 1);
    new MoveClassesOrPackagesProcessor(myProject, classes, new SingleSourceRootMoveDestination(PackageWrapper.create(JavaDirectoryService.getInstance().getPackage(dirs[0])), dirs[0]), true, true, null).run();
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();
    FileDocumentManager.getInstance().saveAllDocuments();
}
Also used : SingleSourceRootMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination) MoveClassesOrPackagesProcessor(com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesProcessor)

Example 5 with SingleSourceRootMoveDestination

use of com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination in project intellij-community by JetBrains.

the class MovePackageTest method performAction.

private void performAction(String[] packageNames, String newPackageName) throws Exception {
    final PsiPackage[] packages = new PsiPackage[packageNames.length];
    for (int i = 0; i < packages.length; i++) {
        String packageName = packageNames[i];
        packages[i] = JavaPsiFacade.getInstance(myPsiManager.getProject()).findPackage(packageName);
        assertNotNull("Package " + packageName + " not found", packages[i]);
    }
    PsiPackage newParentPackage = JavaPsiFacade.getInstance(myPsiManager.getProject()).findPackage(newPackageName);
    assertNotNull(newParentPackage);
    final PsiDirectory[] dirs = newParentPackage.getDirectories();
    assertEquals(dirs.length, 1);
    new MoveClassesOrPackagesProcessor(myProject, packages, new SingleSourceRootMoveDestination(PackageWrapper.create(newParentPackage), dirs[0]), true, false, null).run();
    FileDocumentManager.getInstance().saveAllDocuments();
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) SingleSourceRootMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination) PsiPackage(com.intellij.psi.PsiPackage) MoveClassesOrPackagesProcessor(com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesProcessor)

Aggregations

MoveClassesOrPackagesProcessor (com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesProcessor)8 SingleSourceRootMoveDestination (com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiPackage (com.intellij.psi.PsiPackage)1 PackageWrapper (com.intellij.refactoring.PackageWrapper)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 ArrayList (java.util.ArrayList)1