Search in sources :

Example 6 with PackageWrapper

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

the class MoveInnerDialog method getTargetContainer.

@Nullable
private PsiElement getTargetContainer() {
    if (myTargetContainer instanceof PsiDirectory) {
        final PsiDirectory psiDirectory = (PsiDirectory) myTargetContainer;
        PsiPackage oldPackage = getTargetPackage();
        String name = oldPackage == null ? "" : oldPackage.getQualifiedName();
        final String targetName = myPackageNameField.getText();
        if (!Comparing.equal(name, targetName)) {
            final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(myProject);
            final List<VirtualFile> contentSourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(myProject);
            final PackageWrapper newPackage = new PackageWrapper(PsiManager.getInstance(myProject), targetName);
            final VirtualFile targetSourceRoot;
            if (contentSourceRoots.size() > 1) {
                PsiPackage targetPackage = JavaPsiFacade.getInstance(myProject).findPackage(targetName);
                PsiDirectory initialDir = null;
                if (targetPackage != null) {
                    final PsiDirectory[] directories = targetPackage.getDirectories();
                    final VirtualFile root = projectRootManager.getFileIndex().getSourceRootForFile(psiDirectory.getVirtualFile());
                    for (PsiDirectory dir : directories) {
                        if (Comparing.equal(projectRootManager.getFileIndex().getSourceRootForFile(dir.getVirtualFile()), root)) {
                            initialDir = dir;
                            break;
                        }
                    }
                }
                final 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>() {

                    public PsiDirectory compute() {
                        try {
                            return RefactoringUtil.createPackageDirectoryInSourceRoot(newPackage, targetSourceRoot);
                        } catch (IncorrectOperationException e) {
                            return null;
                        }
                    }
                });
            }
            return dir;
        }
    }
    return myTargetContainer;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NullableComputable(com.intellij.openapi.util.NullableComputable) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) PackageWrapper(com.intellij.refactoring.PackageWrapper) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with PackageWrapper

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

the class DestinationFolderComboBox method setData.

public void setData(final Project project, final PsiDirectory initialTargetDirectory, final Pass<String> errorMessageUpdater, final EditorComboBox editorComboBox) {
    myInitialTargetDirectory = initialTargetDirectory;
    mySourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(project);
    new ComboboxSpeedSearch(getComboBox()) {

        @Override
        protected String getElementText(Object element) {
            if (element == NULL_WRAPPER)
                return LEAVE_IN_SAME_SOURCE_ROOT;
            if (element instanceof DirectoryChooser.ItemWrapper) {
                final VirtualFile virtualFile = ((DirectoryChooser.ItemWrapper) element).getDirectory().getVirtualFile();
                final Module module = ModuleUtil.findModuleForFile(virtualFile, project);
                if (module != null) {
                    return module.getName();
                }
            }
            return super.getElementText(element);
        }
    };
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    getComboBox().setRenderer(new ListCellRendererWrapper<DirectoryChooser.ItemWrapper>() {

        @Override
        public void customize(JList list, DirectoryChooser.ItemWrapper itemWrapper, int index, boolean selected, boolean hasFocus) {
            if (itemWrapper != NULL_WRAPPER && itemWrapper != null) {
                setIcon(itemWrapper.getIcon(fileIndex));
                setText(itemWrapper.getRelativeToProjectPath());
            } else {
                setText(LEAVE_IN_SAME_SOURCE_ROOT);
            }
        }
    });
    final VirtualFile initialSourceRoot = initialTargetDirectory != null ? fileIndex.getSourceRootForFile(initialTargetDirectory.getVirtualFile()) : null;
    final VirtualFile[] selection = new VirtualFile[] { initialSourceRoot };
    addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            VirtualFile root = MoveClassesOrPackagesUtil.chooseSourceRoot(new PackageWrapper(PsiManager.getInstance(project), getTargetPackage()), mySourceRoots, initialTargetDirectory);
            if (root == null)
                return;
            final ComboBoxModel model = getComboBox().getModel();
            for (int i = 0; i < model.getSize(); i++) {
                DirectoryChooser.ItemWrapper item = (DirectoryChooser.ItemWrapper) model.getElementAt(i);
                if (item != NULL_WRAPPER && Comparing.equal(fileIndex.getSourceRootForFile(item.getDirectory().getVirtualFile()), root)) {
                    getComboBox().setSelectedItem(item);
                    getComboBox().repaint();
                    return;
                }
            }
            setComboboxModel(getComboBox(), root, root, fileIndex, mySourceRoots, project, true, errorMessageUpdater);
        }
    });
    editorComboBox.addDocumentListener(new DocumentAdapter() {

        @Override
        public void documentChanged(DocumentEvent e) {
            JComboBox comboBox = getComboBox();
            DirectoryChooser.ItemWrapper selectedItem = (DirectoryChooser.ItemWrapper) comboBox.getSelectedItem();
            setComboboxModel(comboBox, selectedItem != null && selectedItem != NULL_WRAPPER ? fileIndex.getSourceRootForFile(selectedItem.getDirectory().getVirtualFile()) : initialSourceRoot, selection[0], fileIndex, mySourceRoots, project, false, errorMessageUpdater);
        }
    });
    setComboboxModel(getComboBox(), initialSourceRoot, selection[0], fileIndex, mySourceRoots, project, false, errorMessageUpdater);
    getComboBox().addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Object selectedItem = getComboBox().getSelectedItem();
            updateErrorMessage(errorMessageUpdater, fileIndex, selectedItem);
            if (selectedItem instanceof DirectoryChooser.ItemWrapper && selectedItem != NULL_WRAPPER) {
                PsiDirectory directory = ((DirectoryChooser.ItemWrapper) selectedItem).getDirectory();
                if (directory != null) {
                    selection[0] = fileIndex.getSourceRootForFile(directory.getVirtualFile());
                }
            }
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ActionEvent(java.awt.event.ActionEvent) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) PackageWrapper(com.intellij.refactoring.PackageWrapper) ActionListener(java.awt.event.ActionListener) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) PsiDirectory(com.intellij.psi.PsiDirectory) Module(com.intellij.openapi.module.Module) DirectoryChooser(com.intellij.ide.util.DirectoryChooser)

Example 8 with PackageWrapper

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

the class CreateValidationXmlIntention method createValidationXml.

private static void createValidationXml(final Project project, final PsiClass actionClass, @Nullable final String path) {
    final PsiManager manager = PsiManager.getInstance(project);
    final String actionClassQualifiedName = actionClass.getQualifiedName();
    assert actionClassQualifiedName != null;
    final PackageWrapper targetPackage = new PackageWrapper(manager, StringUtil.getPackageName(actionClassQualifiedName));
    final Module module = ModuleUtilCore.findModuleForPsiElement(actionClass);
    assert module != null;
    final List<VirtualFile> sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots(JavaModuleSourceRootTypes.PRODUCTION);
    final VirtualFile sourceRoot = sourceRoots.size() == 1 ? sourceRoots.get(0) : MoveClassesOrPackagesUtil.chooseSourceRoot(targetPackage, sourceRoots, manager.findDirectory(sourceRoots.get(0)));
    if (sourceRoot == null) {
        return;
    }
    final PsiDirectory directory = manager.findDirectory(sourceRoot);
    assert directory != null : sourceRoot.getPresentableUrl();
    final FileTemplateManager templateManager = FileTemplateManager.getInstance(project);
    final FileTemplate validationTemplate = templateManager.getJ2eeTemplate(StrutsFileTemplateGroupDescriptorFactory.VALIDATION_XML);
    final PsiDirectory packageDirectoryInSourceRoot = RefactoringUtil.createPackageDirectoryInSourceRoot(targetPackage, sourceRoot);
    try {
        final String filename = path == null ? actionClass.getName() + "-validation.xml" : actionClass.getName() + "-" + path + "-validation.xml";
        final PsiElement psiElement = FileTemplateUtil.createFromTemplate(validationTemplate, filename, null, packageDirectoryInSourceRoot);
        NavigationUtil.activateFileWithPsiElement(psiElement, true);
    } catch (Exception e) {
        throw new IncorrectOperationException("error creating validation.xml", (Throwable) e);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) PackageWrapper(com.intellij.refactoring.PackageWrapper) IncorrectOperationException(com.intellij.util.IncorrectOperationException) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module)

Example 9 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)

Example 10 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)

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