@Override public boolean isMoveRedundant(PsiElement source, PsiElement target) { if (target instanceof PsiDirectory && source instanceof PsiClass) { try { JavaDirectoryServiceImpl.checkCreateClassOrInterface((PsiDirectory)target, ((PsiClass)source).getName()); } catch (IncorrectOperationException e) { return true; } } if (target instanceof PsiPackage && source instanceof PsiClass) { final GlobalSearchScope globalSearchScope = GlobalSearchScope.projectScope(source.getProject()); return ((PsiPackage)target).findClassByShortName(((PsiClass)source).getName(), globalSearchScope).length > 0; } if (target instanceof PsiDirectory && source instanceof PsiDirectory) { final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage((PsiDirectory)source); if (aPackage != null && !MoveClassesOrPackagesImpl.checkNesting(target.getProject(), aPackage, target, false)) return true; } return super.isMoveRedundant(source, target); }
@Override public boolean isMoveRedundant(PsiElement source, PsiElement target) { if (target instanceof PsiDirectory && source instanceof PsiClass) { try { JavaDirectoryServiceImpl.checkCreateClassOrInterface((PsiDirectory)target, ((PsiClass)source).getName()); } catch (IncorrectOperationException e) { return true; } } if (target instanceof PsiDirectory && source instanceof PsiDirectory) { final PsiPackage aPackage = JavaDirectoryServiceImpl.getInstance().getPackage((PsiDirectory)source); if (aPackage != null && !MoveClassesOrPackagesImpl.checkNesting(target.getProject(), aPackage, target, false)) return true; } return super.isMoveRedundant(source, target); }
@Override public boolean isMoveRedundant(PsiElement source, PsiElement target) { if(target instanceof PsiDirectory && source instanceof PsiClass) { try { JavaDirectoryServiceImpl.checkCreateClassOrInterface((PsiDirectory) target, ((PsiClass) source).getName()); } catch(IncorrectOperationException e) { return true; } } if(target instanceof PsiDirectory && source instanceof PsiDirectory) { final PsiJavaPackage aPackage = JavaDirectoryServiceImpl.getInstance().getPackage((PsiDirectory) source); if(aPackage != null && !MoveClassesOrPackagesImpl.checkNesting(target.getProject(), aPackage, target, false)) { return true; } } return super.isMoveRedundant(source, target); }
@Override public boolean isAvailable(@NotNull Project project, @Nullable Editor editor, @NotNull PsiFile file) { if (!myClass.isValid() || !myClass.getManager().isInProject(myClass)) return false; PsiDirectory dir = file.getContainingDirectory(); if (dir == null) return false; try { JavaDirectoryServiceImpl.checkCreateClassOrInterface(dir, myClass.getName()); } catch (IncorrectOperationException e) { return false; } return true; }
@Nullable private static PsiFile obtainContainingFile(@NotNull PsiElement element, PsiElement[] elements) { final PsiFile containingFile = element.getContainingFile(); final PsiClass[] classes = ((PsiClassOwner)containingFile).getClasses(); final Set<PsiClass> nonMovedClasses = new HashSet<PsiClass>(); for (PsiClass aClass : classes) { if (ArrayUtilRt.find(elements, aClass) < 0) { nonMovedClasses.add(aClass); } } if (nonMovedClasses.isEmpty()) { return containingFile; } else { final PsiDirectory containingDirectory = containingFile.getContainingDirectory(); if (containingDirectory != null) { try { JavaDirectoryServiceImpl.checkCreateClassOrInterface(containingDirectory, ((PsiClass)element).getName()); final PsiElement createdClass = containingDirectory.add(element); element.delete(); return createdClass.getContainingFile(); } catch (IncorrectOperationException e) { final Iterator<PsiClass> iterator = nonMovedClasses.iterator(); final PsiClass nonMovedClass = iterator.next(); final PsiElement createdFile = containingDirectory.add(nonMovedClass).getContainingFile(); nonMovedClass.delete(); while (iterator.hasNext()) { final PsiClass currentClass = iterator.next(); createdFile.add(currentClass); currentClass.delete(); } return containingFile; } } } return null; }
@Nullable private static PsiFile obtainContainingFile(@NotNull PsiElement element, PsiElement[] elements) { final PsiClass[] classes = ((PsiClassOwner)element.getParent()).getClasses(); final Set<PsiClass> nonMovedClasses = new HashSet<PsiClass>(); for (PsiClass aClass : classes) { if (ArrayUtilRt.find(elements, aClass) < 0) { nonMovedClasses.add(aClass); } } final PsiFile containingFile = element.getContainingFile(); if (nonMovedClasses.isEmpty()) { return containingFile; } else { final PsiDirectory containingDirectory = containingFile.getContainingDirectory(); if (containingDirectory != null) { try { JavaDirectoryServiceImpl.checkCreateClassOrInterface(containingDirectory, ((PsiClass)element).getName()); final PsiElement createdClass = containingDirectory.add(element); element.delete(); return createdClass.getContainingFile(); } catch (IncorrectOperationException e) { final Iterator<PsiClass> iterator = nonMovedClasses.iterator(); final PsiClass nonMovedClass = iterator.next(); final PsiElement createdFile = containingDirectory.add(nonMovedClass).getContainingFile(); nonMovedClass.delete(); while (iterator.hasNext()) { final PsiClass currentClass = iterator.next(); createdFile.add(currentClass); currentClass.delete(); } return containingFile; } } } return null; }
@Nullable private static PsiFile obtainContainingFile(PsiElement element, PsiElement[] elements) { final PsiClass[] classes = ((PsiClassOwner)element.getParent()).getClasses(); final Set<PsiClass> nonMovedClasses = new HashSet<PsiClass>(); for (PsiClass aClass : classes) { if (ArrayUtil.find(elements, aClass) < 0) { nonMovedClasses.add(aClass); } } final PsiFile containingFile = element.getContainingFile(); if (nonMovedClasses.isEmpty()) { return containingFile; } else { final PsiDirectory containingDirectory = containingFile.getContainingDirectory(); if (containingDirectory != null) { try { JavaDirectoryServiceImpl.checkCreateClassOrInterface(containingDirectory, ((PsiClass)element).getName()); final PsiElement createdClass = containingDirectory.add(element); element.delete(); return createdClass.getContainingFile(); } catch (IncorrectOperationException e) { final Iterator<PsiClass> iterator = nonMovedClasses.iterator(); final PsiClass nonMovedClass = iterator.next(); final PsiElement createdFile = containingDirectory.add(nonMovedClass).getContainingFile(); nonMovedClass.delete(); while (iterator.hasNext()) { final PsiClass currentClass = iterator.next(); createdFile.add(currentClass); currentClass.delete(); } return containingFile; } } } return null; }
public static PsiClass createClassOrInterface(Project project, PsiDirectory directory, String content, boolean reformat, String extension) throws IncorrectOperationException { if (extension == null) extension = StdFileTypes.JAVA.getDefaultExtension(); final String name = "myClass" + "." + extension; final PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText(name, StdFileTypes.JAVA, content); if (!(psiFile instanceof PsiJavaFile)){ throw new IncorrectOperationException("This template did not produce a Java class or an interface\n"+psiFile.getText()); } PsiJavaFile psiJavaFile = (PsiJavaFile)psiFile; final PsiClass[] classes = psiJavaFile.getClasses(); if (classes.length == 0) { throw new IncorrectOperationException("This template did not produce a Java class or an interface\n"+psiFile.getText()); } PsiClass createdClass = classes[0]; if(reformat){ CodeStyleManager.getInstance(project).reformat(psiJavaFile); } String className = createdClass.getName(); JavaDirectoryServiceImpl.checkCreateClassOrInterface(directory, className); final LanguageLevel ll = JavaDirectoryService.getInstance().getLanguageLevel(directory); if (ll.compareTo(LanguageLevel.JDK_1_5) < 0) { if (createdClass.isAnnotationType()) { throw new IncorrectOperationException("Annotations only supported at language level 1.5 and higher"); } if (createdClass.isEnum()) { throw new IncorrectOperationException("Enums only supported at language level 1.5 and higher"); } } psiJavaFile = (PsiJavaFile)psiJavaFile.setName(className + "." + extension); PsiElement addedElement = directory.add(psiJavaFile); if (addedElement instanceof PsiJavaFile) { psiJavaFile = (PsiJavaFile)addedElement; return psiJavaFile.getClasses()[0]; } else { PsiFile containingFile = addedElement.getContainingFile(); throw new IncorrectOperationException("Selected class file name '" + containingFile.getName() + "' mapped to not java file type '"+ containingFile.getFileType().getDescription() + "'"); } }
public static PsiClass createClassOrInterface(Project project, PsiDirectory directory, String content, boolean reformat, String extension) throws IncorrectOperationException { if (extension == null) extension = StdFileTypes.JAVA.getDefaultExtension(); final String name = "myClass" + "." + extension; final FileType type = FileTypeRegistry.getInstance().getFileTypeByFileName(name); final PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText(name, type, content); if (!(psiFile instanceof PsiJavaFile)){ throw new IncorrectOperationException("This template did not produce a Java class or an interface\n"+psiFile.getText()); } PsiJavaFile psiJavaFile = (PsiJavaFile)psiFile; final PsiClass[] classes = psiJavaFile.getClasses(); if (classes.length == 0) { throw new IncorrectOperationException("This template did not produce a Java class or an interface\n"+psiFile.getText()); } PsiClass createdClass = classes[0]; if(reformat){ CodeStyleManager.getInstance(project).reformat(psiJavaFile); } String className = createdClass.getName(); JavaDirectoryServiceImpl.checkCreateClassOrInterface(directory, className); final LanguageLevel ll = JavaDirectoryService.getInstance().getLanguageLevel(directory); if (ll.compareTo(LanguageLevel.JDK_1_5) < 0) { if (createdClass.isAnnotationType()) { throw new IncorrectOperationException("Annotations only supported at language level 1.5 and higher"); } if (createdClass.isEnum()) { throw new IncorrectOperationException("Enums only supported at language level 1.5 and higher"); } } psiJavaFile = (PsiJavaFile)psiJavaFile.setName(className + "." + extension); PsiElement addedElement = directory.add(psiJavaFile); if (addedElement instanceof PsiJavaFile) { psiJavaFile = (PsiJavaFile)addedElement; return psiJavaFile.getClasses()[0]; } else { PsiFile containingFile = addedElement.getContainingFile(); throw new IncorrectOperationException("Selected class file name '" + containingFile.getName() + "' mapped to not java file type '"+ containingFile.getFileType().getDescription() + "'"); } }