@Nullable private String verifyInnerClassDestination() { PsiClass targetClass = findTargetClass(); if (targetClass == null) return null; for (PsiElement element : myElementsToMove) { if (PsiTreeUtil.isAncestor(element, targetClass, false)) { return RefactoringBundle.message("move.class.to.inner.move.to.self.error"); } final Language targetClassLanguage = targetClass.getLanguage(); if (!element.getLanguage().equals(targetClassLanguage)) { return RefactoringBundle.message( "move.to.different.language", UsageViewUtil.getType(element), ((PsiClass) element).getQualifiedName(), targetClass.getQualifiedName()); } if (element.getLanguage().equals(Language.findLanguageByID("Groovy"))) { return RefactoringBundle.message("dont.support.inner.classes", "Groovy"); } } while (targetClass != null) { if (targetClass.getContainingClass() != null && !targetClass.hasModifierProperty(PsiModifier.STATIC)) { return RefactoringBundle.message("move.class.to.inner.nonstatic.error"); } targetClass = targetClass.getContainingClass(); } return null; }
public MoveClassesOrPackagesDialog( Project project, boolean searchTextOccurences, PsiElement[] elementsToMove, final PsiElement initialTargetElement, MoveCallback moveCallback) { super(project, true); myElementsToMove = elementsToMove; myMoveCallback = moveCallback; myManager = PsiManager.getInstance(myProject); setTitle(MoveHandler.REFACTORING_NAME); mySearchTextOccurencesEnabled = searchTextOccurences; selectInitialCard(); init(); if (initialTargetElement instanceof PsiClass) { myMakeInnerClassOfRadioButton.setSelected(true); myInnerClassChooser.setText(((PsiClass) initialTargetElement).getQualifiedName()); ApplicationManager.getApplication() .invokeLater( () -> myInnerClassChooser.requestFocus(), ModalityState.stateForComponent(myMainPanel)); } else if (initialTargetElement instanceof PsiPackage) { myClassPackageChooser.setText(((PsiPackage) initialTargetElement).getQualifiedName()); } updateControlsEnabled(); myToPackageRadioButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { updateControlsEnabled(); myClassPackageChooser.requestFocus(); } }); myMakeInnerClassOfRadioButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { updateControlsEnabled(); myInnerClassChooser.requestFocus(); } }); for (PsiElement element : elementsToMove) { if (element.getContainingFile() != null) { myOpenInEditorPanel.add(initOpenInEditorCb(), BorderLayout.EAST); break; } } }
private void invokeMoveToPackage() { final MoveDestination destination = selectDestination(); if (destination == null) return; saveRefactoringSettings(); for (final PsiElement element : myElementsToMove) { String message = verifyDestinationForElement(element, destination); if (message != null) { String helpId = HelpID.getMoveHelpID(myElementsToMove[0]); CommonRefactoringUtil.showErrorMessage( RefactoringBundle.message("error.title"), message, helpId, getProject()); return; } } try { for (PsiElement element : myElementsToMove) { if (element instanceof PsiClass) { final PsiClass aClass = (PsiClass) element; LOG.assertTrue(aClass.isPhysical(), aClass); /*PsiElement toAdd; if (aClass.getContainingFile() instanceof PsiJavaFile && ((PsiJavaFile)aClass.getContainingFile()).getClasses().length > 1) { toAdd = aClass; } else { toAdd = aClass.getContainingFile(); }*/ final PsiDirectory targetDirectory = destination.getTargetIfExists(element.getContainingFile()); if (targetDirectory != null) { MoveFilesOrDirectoriesUtil.checkMove(aClass, targetDirectory); } } } MoveClassesOrPackagesProcessor processor = createMoveToPackageProcessor(destination, myElementsToMove, myMoveCallback); if (processor.verifyValidPackageName()) { processor.setOpenInEditor(isOpenInEditor()); invokeRefactoring(processor); } } catch (IncorrectOperationException e) { String helpId = HelpID.getMoveHelpID(myElementsToMove[0]); CommonRefactoringUtil.showErrorMessage( RefactoringBundle.message("error.title"), e.getMessage(), helpId, getProject()); } }
@Nullable private static String verifyDestinationForElement( final PsiElement element, final MoveDestination moveDestination) { final String message; if (element instanceof PsiDirectory) { message = moveDestination.verify((PsiDirectory) element); } else if (element instanceof PsiPackage) { message = moveDestination.verify((PsiPackage) element); } else { message = moveDestination.verify(element.getContainingFile()); } return message; }
public void setData( PsiElement[] psiElements, String targetPackageName, final PsiDirectory initialTargetDirectory, boolean isTargetDirectoryFixed, boolean searchInComments, boolean searchForTextOccurences, String helpID) { myInitialTargetDirectory = initialTargetDirectory; myTargetDirectoryFixed = isTargetDirectoryFixed; if (targetPackageName.length() != 0) { myWithBrowseButtonReference.prependItem(targetPackageName); myClassPackageChooser.prependItem(targetPackageName); } String nameFromCallback = myMoveCallback instanceof MoveClassesOrPackagesCallback ? ((MoveClassesOrPackagesCallback) myMoveCallback).getElementsToMoveName() : null; if (nameFromCallback != null) { myNameLabel.setText(nameFromCallback); } else if (psiElements.length == 1) { PsiElement firstElement = psiElements[0]; if (firstElement instanceof PsiClass) { LOG.assertTrue(!MoveClassesOrPackagesImpl.isClassInnerOrLocal((PsiClass) firstElement)); } else { PsiElement parent = firstElement.getParent(); LOG.assertTrue(parent != null); } myNameLabel.setText( RefactoringBundle.message( "move.single.class.or.package.name.label", UsageViewUtil.getType(firstElement), UsageViewUtil.getLongName(firstElement))); } else if (psiElements.length > 1) { myNameLabel.setText( psiElements[0] instanceof PsiClass ? RefactoringBundle.message("move.specified.classes") : RefactoringBundle.message("move.specified.packages")); } selectInitialCard(); myCbSearchInComments.setSelected(searchInComments); myCbSearchTextOccurences.setSelected(searchForTextOccurences); ((DestinationFolderComboBox) myDestinationFolderCB) .setData( myProject, myInitialTargetDirectory, new Pass<String>() { @Override public void pass(String s) { setErrorText(s); } }, myClassPackageChooser.getChildComponent()); UIUtil.setEnabled( myTargetPanel, getSourceRoots().length > 0 && isMoveToPackage() && !isTargetDirectoryFixed, true); validateButtons(); myHelpID = helpID; }
public void setData( PsiElement[] psiElements, String targetPackageName, PsiDirectory initialTargetDirectory, boolean isTargetDirectoryFixed, boolean suggestToMoveToAnotherRoot, boolean searchInComments, boolean searchForTextOccurences, String helpID) { myTargetDirectoryFixed = isTargetDirectoryFixed; mySuggestToMoveToAnotherRoot = suggestToMoveToAnotherRoot; if (targetPackageName.length() != 0) { myWithBrowseButtonReference.prependItem(targetPackageName); myClassPackageChooser.prependItem(targetPackageName); } String nameFromCallback = myMoveCallback instanceof MoveClassesOrPackagesCallback ? ((MoveClassesOrPackagesCallback) myMoveCallback).getElementsToMoveName() : null; if (nameFromCallback != null) { myNameLabel.setText(nameFromCallback); } else if (psiElements.length == 1) { PsiElement firstElement = psiElements[0]; if (firstElement instanceof PsiClass) { LOG.assertTrue(!MoveClassesOrPackagesImpl.isClassInnerOrLocal((PsiClass) firstElement)); } else { PsiElement parent = firstElement.getParent(); LOG.assertTrue(parent != null); } myNameLabel.setText( RefactoringBundle.message( "move.single.class.or.package.name.label", UsageViewUtil.getType(firstElement), UsageViewUtil.getLongName(firstElement))); } else if (psiElements.length > 1) { myNameLabel.setText( psiElements[0] instanceof PsiClass ? RefactoringBundle.message("move.specified.classes") : RefactoringBundle.message("move.specified.packages")); } selectInitialCard(); myCbSearchInComments.setSelected(searchInComments); myCbSearchTextOccurences.setSelected(searchForTextOccurences); if (initialTargetDirectory != null && JavaMoveClassesOrPackagesHandler.packageHasMultipleDirectoriesInModule( myProject, initialTargetDirectory)) { final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex(); final Set<VirtualFile> initialRoots = new HashSet<VirtualFile>(); collectSourceRoots(psiElements, fileIndex, initialRoots); if (initialRoots.size() > 1) { initialTargetDirectory = null; } } ((DestinationFolderComboBox) myDestinationFolderCB) .setData( myProject, initialTargetDirectory, new Pass<String>() { @Override public void pass(String s) { setErrorText(s); } }, myHavePackages ? myWithBrowseButtonReference.getChildComponent() : myClassPackageChooser.getChildComponent()); UIUtil.setEnabled( myTargetPanel, !getSourceRoots().isEmpty() && isMoveToPackage() && !isTargetDirectoryFixed, true); validateButtons(); myHelpID = helpID; }