Search in sources :

Example 1 with PackageWrapper

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

the class CreateClassToBindFix method run.

public void run() {
    final Project project = myEditor.getProject();
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    final VirtualFile sourceRoot = fileIndex.getSourceRootForFile(myEditor.getFile());
    if (sourceRoot == null) {
        Messages.showErrorDialog(myEditor, UIDesignerBundle.message("error.cannot.create.class.not.in.source.root"), CommonBundle.getErrorTitle());
        return;
    }
    ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(project, () -> {
        // 1. Create all necessary packages
        final int indexOfLastDot = myClassName.lastIndexOf('.');
        final String packageName = myClassName.substring(0, indexOfLastDot != -1 ? indexOfLastDot : 0);
        final PsiDirectory psiDirectory;
        if (packageName.length() > 0) {
            final PackageWrapper packageWrapper = new PackageWrapper(PsiManager.getInstance(project), packageName);
            try {
                psiDirectory = RefactoringUtil.createPackageDirectoryInSourceRoot(packageWrapper, sourceRoot);
                LOG.assertTrue(psiDirectory != null);
            } catch (final IncorrectOperationException e) {
                ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(myEditor, UIDesignerBundle.message("error.cannot.create.package", packageName, e.getMessage()), CommonBundle.getErrorTitle()));
                return;
            }
        } else {
            psiDirectory = PsiManager.getInstance(project).findDirectory(sourceRoot);
            LOG.assertTrue(psiDirectory != null);
        }
        // 2. Create class in the package
        try {
            final String name = myClassName.substring(indexOfLastDot != -1 ? indexOfLastDot + 1 : 0);
            final PsiClass aClass = JavaDirectoryService.getInstance().createClass(psiDirectory, name);
            createBoundFields(aClass);
        } catch (final IncorrectOperationException e) {
            ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(myEditor, UIDesignerBundle.message("error.cannot.create.class", packageName, e.getMessage()), CommonBundle.getErrorTitle()));
        }
    }, getName(), null));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PackageWrapper(com.intellij.refactoring.PackageWrapper)

Example 2 with PackageWrapper

use of com.intellij.refactoring.PackageWrapper in project smali by JesusFreke.

the class ClassMoveTest method doMove.

private void doMove(String oldQualifiedName, final String newPackage) throws Exception {
    final PsiClass testClass = myJavaFacade.findClass(oldQualifiedName, GlobalSearchScope.allScope(getProject()));
    final List<VirtualFile> contentSourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(getProject());
    new MoveClassesOrPackagesProcessor(getProject(), new PsiClass[] { testClass }, new AutocreatingSingleSourceRootMoveDestination(new PackageWrapper(getPsiManager(), newPackage), contentSourceRoots.get(0)), false, false, null).run();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiClass(com.intellij.psi.PsiClass) AutocreatingSingleSourceRootMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.AutocreatingSingleSourceRootMoveDestination) MoveClassesOrPackagesProcessor(com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesProcessor) PackageWrapper(com.intellij.refactoring.PackageWrapper)

Example 3 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 4 with PackageWrapper

use of com.intellij.refactoring.PackageWrapper 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 5 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)

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