Search in sources :

Example 11 with PackageWrapper

use of com.intellij.refactoring.PackageWrapper in project intellij-community by JetBrains.

the class ReplaceConstructorWithBuilderDialog method doAction.

protected void doAction() {
    TableUtil.stopEditing(myTable);
    final String className;
    final String packageName;
    if (myCreateBuilderClassRadioButton.isSelected()) {
        className = myNewClassName.getText().trim();
        packageName = myPackageTextField.getText().trim();
    } else {
        final String fqName = myExistentClassTF.getText().trim();
        className = StringUtil.getShortName(fqName);
        packageName = StringUtil.getPackageName(fqName);
        final PsiClass builderClass = JavaPsiFacade.getInstance(myProject).findClass(StringUtil.getQualifiedName(packageName, className), GlobalSearchScope.projectScope(myProject));
        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()));
}
Also used : DestinationFolderComboBox(com.intellij.refactoring.move.moveClassesOrPackages.DestinationFolderComboBox) PackageWrapper(com.intellij.refactoring.PackageWrapper)

Example 12 with PackageWrapper

use of com.intellij.refactoring.PackageWrapper in project intellij-community by JetBrains.

the class JavaExtractSuperBaseDialog method preparePackage.

@Override
protected void preparePackage() throws OperationFailedException {
    final String targetPackageName = getTargetPackageName();
    final PsiFile containingFile = mySourceClass.getContainingFile();
    final boolean fromDefaultPackage = containingFile instanceof PsiClassOwner && ((PsiClassOwner) containingFile).getPackageName().isEmpty();
    if (!(fromDefaultPackage && StringUtil.isEmpty(targetPackageName)) && !PsiNameHelper.getInstance(myProject).isQualifiedName(targetPackageName)) {
        throw new OperationFailedException("Invalid package name: " + targetPackageName);
    }
    final PsiPackage aPackage = JavaPsiFacade.getInstance(myProject).findPackage(targetPackageName);
    if (aPackage != null) {
        final PsiDirectory[] directories = aPackage.getDirectories(mySourceClass.getResolveScope());
        if (directories.length >= 1) {
            myTargetDirectory = getDirUnderSameSourceRoot(directories);
        }
    }
    final MoveDestination moveDestination = myDestinationFolderComboBox.selectDirectory(new PackageWrapper(PsiManager.getInstance(myProject), targetPackageName), false);
    if (moveDestination == null)
        return;
    myTargetDirectory = myTargetDirectory != null ? ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {

        @Override
        public PsiDirectory compute() {
            return moveDestination.getTargetDirectory(myTargetDirectory);
        }
    }) : null;
    if (myTargetDirectory == null) {
        // message already reported by PackageUtil
        throw new OperationFailedException("");
    }
    String error = RefactoringMessageUtil.checkCanCreateClass(myTargetDirectory, getExtractedSuperName());
    if (error != null) {
        throw new OperationFailedException(error);
    }
}
Also used : MoveDestination(com.intellij.refactoring.MoveDestination) PackageWrapper(com.intellij.refactoring.PackageWrapper)

Example 13 with PackageWrapper

use of com.intellij.refactoring.PackageWrapper in project intellij-community by JetBrains.

the class MoveClassesOrPackagesProcessor method detectPackageLocalsMoved.

private void detectPackageLocalsMoved(final UsageInfo[] usages, final MultiMap<PsiElement, String> conflicts) {
    //    final HashSet reportedPackageLocalUsed = new HashSet();
    final HashSet<PsiClass> movedClasses = new HashSet<>();
    final HashMap<PsiClass, HashSet<PsiElement>> reportedClassToContainers = new HashMap<>();
    final PackageWrapper aPackage = myTargetPackage;
    for (UsageInfo usage : usages) {
        PsiElement element = usage.getElement();
        if (element == null)
            continue;
        if (usage instanceof MoveRenameUsageInfo && !(usage instanceof NonCodeUsageInfo) && ((MoveRenameUsageInfo) usage).getReferencedElement() instanceof PsiClass) {
            PsiClass aClass = (PsiClass) ((MoveRenameUsageInfo) usage).getReferencedElement();
            if (!movedClasses.contains(aClass)) {
                movedClasses.add(aClass);
            }
            if (aClass != null && aClass.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
                if (PsiTreeUtil.getParentOfType(element, PsiImportStatement.class) != null)
                    continue;
                PsiElement container = ConflictsUtil.getContainer(element);
                HashSet<PsiElement> reported = reportedClassToContainers.get(aClass);
                if (reported == null) {
                    reported = new HashSet<>();
                    reportedClassToContainers.put(aClass, reported);
                }
                if (!reported.contains(container)) {
                    reported.add(container);
                    PsiFile containingFile = element.getContainingFile();
                    if (containingFile != null && !isInsideMoved(element)) {
                        PsiDirectory directory = containingFile.getContainingDirectory();
                        if (directory != null) {
                            PsiPackage usagePackage = JavaDirectoryService.getInstance().getPackage(directory);
                            if (aPackage != null && usagePackage != null && !aPackage.equalToPackage(usagePackage)) {
                                final String message = RefactoringBundle.message("a.package.local.class.0.will.no.longer.be.accessible.from.1", CommonRefactoringUtil.htmlEmphasize(aClass.getName()), RefactoringUIUtil.getDescription(container, true));
                                conflicts.putValue(aClass, message);
                            }
                        }
                    }
                }
            }
        }
    }
    final MyClassInstanceReferenceVisitor instanceReferenceVisitor = new MyClassInstanceReferenceVisitor(conflicts);
    for (final PsiClass aClass : movedClasses) {
        String visibility = VisibilityUtil.getVisibilityModifier(aClass.getModifierList());
        if (PsiModifier.PACKAGE_LOCAL.equals(visibility)) {
            findInstancesOfPackageLocal(aClass, usages, instanceReferenceVisitor);
        } else {
            // public classes
            findPublicClassConflicts(aClass, instanceReferenceVisitor);
        }
    }
}
Also used : HashMap(com.intellij.util.containers.HashMap) PackageWrapper(com.intellij.refactoring.PackageWrapper) UsageInfo(com.intellij.usageView.UsageInfo)

Example 14 with PackageWrapper

use of com.intellij.refactoring.PackageWrapper in project intellij-community by JetBrains.

the class DestinationFolderComboBox method setComboboxModel.

private void setComboboxModel(final JComboBox comboBox, final VirtualFile initialTargetDirectorySourceRoot, final VirtualFile oldSelection, final ProjectFileIndex fileIndex, final List<VirtualFile> sourceRoots, final Project project, final boolean forceIncludeAll, final Pass<String> updateErrorMessage) {
    final LinkedHashSet<PsiDirectory> targetDirectories = new LinkedHashSet<>();
    final HashMap<PsiDirectory, String> pathsToCreate = new HashMap<>();
    MoveClassesOrPackagesUtil.buildDirectoryList(new PackageWrapper(PsiManager.getInstance(project), getTargetPackage()), sourceRoots, targetDirectories, pathsToCreate);
    if (!forceIncludeAll && targetDirectories.size() > pathsToCreate.size()) {
        targetDirectories.removeAll(pathsToCreate.keySet());
    }
    final ArrayList<DirectoryChooser.ItemWrapper> items = new ArrayList<>();
    DirectoryChooser.ItemWrapper initial = null;
    DirectoryChooser.ItemWrapper oldOne = null;
    for (PsiDirectory targetDirectory : targetDirectories) {
        DirectoryChooser.ItemWrapper itemWrapper = new DirectoryChooser.ItemWrapper(targetDirectory, pathsToCreate.get(targetDirectory));
        items.add(itemWrapper);
        final VirtualFile sourceRootForFile = fileIndex.getSourceRootForFile(targetDirectory.getVirtualFile());
        if (Comparing.equal(sourceRootForFile, initialTargetDirectorySourceRoot)) {
            initial = itemWrapper;
        } else if (Comparing.equal(sourceRootForFile, oldSelection)) {
            oldOne = itemWrapper;
        }
    }
    if (oldSelection == null || !fileIndex.isInLibrarySource(oldSelection)) {
        items.add(NULL_WRAPPER);
    }
    final DirectoryChooser.ItemWrapper selection = chooseSelection(initialTargetDirectorySourceRoot, fileIndex, items, initial, oldOne);
    final ComboBoxModel model = comboBox.getModel();
    if (model instanceof CollectionComboBoxModel) {
        boolean sameModel = model.getSize() == items.size();
        if (sameModel) {
            for (int i = 0; i < items.size(); i++) {
                final DirectoryChooser.ItemWrapper oldItem = (DirectoryChooser.ItemWrapper) model.getElementAt(i);
                final DirectoryChooser.ItemWrapper itemWrapper = items.get(i);
                if (!areItemsEquivalent(oldItem, itemWrapper)) {
                    sameModel = false;
                    break;
                }
            }
        }
        if (sameModel) {
            if (areItemsEquivalent((DirectoryChooser.ItemWrapper) comboBox.getSelectedItem(), selection)) {
                return;
            }
        }
    }
    updateErrorMessage(updateErrorMessage, fileIndex, selection);
    Collections.sort(items, (o1, o2) -> {
        if (o1 == NULL_WRAPPER)
            return -1;
        if (o2 == NULL_WRAPPER)
            return 1;
        return o1.getRelativeToProjectPath().compareToIgnoreCase(o2.getRelativeToProjectPath());
    });
    comboBox.setModel(new CollectionComboBoxModel(items, selection));
    final Component root = SwingUtilities.getRoot(comboBox);
    if (root instanceof Window) {
        final Dimension preferredSize = root.getPreferredSize();
        if (preferredSize.getWidth() > root.getSize().getWidth()) {
            root.setSize(preferredSize);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PackageWrapper(com.intellij.refactoring.PackageWrapper) PsiDirectory(com.intellij.psi.PsiDirectory) DirectoryChooser(com.intellij.ide.util.DirectoryChooser)

Example 15 with PackageWrapper

use of com.intellij.refactoring.PackageWrapper in project intellij-community by JetBrains.

the class WrapReturnValueDialog method doAction.

protected void doAction() {
    final boolean useExistingClass = useExistingClassButton.isSelected();
    final boolean createInnerClass = myCreateInnerClassButton.isSelected();
    final String existingClassName = existingClassField.getText().trim();
    final String className;
    final String packageName;
    if (useExistingClass) {
        className = StringUtil.getShortName(existingClassName);
        packageName = StringUtil.getPackageName(existingClassName);
    } else if (createInnerClass) {
        className = getInnerClassName();
        packageName = "";
    } else {
        className = getClassName();
        packageName = getPackageName();
    }
    invokeRefactoring(new WrapReturnValueProcessor(className, packageName, ((DestinationFolderComboBox) myDestinationCb).selectDirectory(new PackageWrapper(sourceMethod.getManager(), packageName), false), sourceMethod, useExistingClass, createInnerClass, (PsiField) myFieldsCombo.getSelectedItem()));
}
Also used : DestinationFolderComboBox(com.intellij.refactoring.move.moveClassesOrPackages.DestinationFolderComboBox) PackageWrapper(com.intellij.refactoring.PackageWrapper)

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