public static void addLibraryChildren( final LibraryOrSdkOrderEntry entry, final List<AbstractTreeNode> children, Project project, ProjectViewNode node) { final PsiManager psiManager = PsiManager.getInstance(project); VirtualFile[] files = entry instanceof LibraryOrderEntry ? getLibraryRoots((LibraryOrderEntry) entry) : entry.getRootFiles(OrderRootType.CLASSES); for (final VirtualFile file : files) { if (!file.isValid()) continue; if (file.isDirectory()) { final PsiDirectory psiDir = psiManager.findDirectory(file); if (psiDir == null) { continue; } children.add(new PsiDirectoryNode(project, psiDir, node.getSettings())); } else { final PsiFile psiFile = psiManager.findFile(file); if (psiFile == null) continue; children.add(new PsiFileNode(project, psiFile, node.getSettings())); } } }
private static PsiDirectory getWritableModuleDirectory( @NotNull Query<VirtualFile> vFiles, @NotNull Module module, PsiManager manager) { for (VirtualFile vFile : vFiles) { if (ModuleUtil.findModuleForFile(vFile, module.getProject()) != module) continue; PsiDirectory directory = manager.findDirectory(vFile); if (directory != null && directory.isValid() && directory.isWritable()) { return directory; } } return null; }
private Map<PsiJavaStubDataSource, SModel> getModelMap() { Map<PsiJavaStubDataSource, SModel> modelMap = MapSequence.fromMap(new HashMap<PsiJavaStubDataSource, SModel>()); final VirtualFile[] sourceRoots = ModuleRootManager.getInstance(myIdeaModule).getSourceRoots(false); PsiManager psiMgr = PsiManager.getInstance(myIdeaModule.getProject()); for (VirtualFile root : sourceRoots) { PsiDirectory dir = psiMgr.findDirectory(root); addModelsForDir(dir, dir, modelMap); } return modelMap; }
protected PsiDirectory[] getSelectedDirectoriesInAmbiguousCase( @NotNull final DefaultMutableTreeNode node) { final Object userObject = node.getUserObject(); if (userObject instanceof AbstractModuleNode) { final Module module = ((AbstractModuleNode) userObject).getValue(); if (module != null) { final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module); final VirtualFile[] sourceRoots = moduleRootManager.getSourceRoots(); List<PsiDirectory> dirs = new ArrayList<PsiDirectory>(sourceRoots.length); final PsiManager psiManager = PsiManager.getInstance(myProject); for (final VirtualFile sourceRoot : sourceRoots) { final PsiDirectory directory = psiManager.findDirectory(sourceRoot); if (directory != null) { dirs.add(directory); } } return dirs.toArray(new PsiDirectory[dirs.size()]); } } return PsiDirectory.EMPTY_ARRAY; }
public static List<PsiDirectory> getWritableDirectoryListDefault( @Nullable final PsiPackage context, final GlobalSearchScope scope, final PsiManager psiManager) { if (LOG.isDebugEnabled()) { LOG.debug( "Getting writable directory list for package '" + (context == null ? null : context.getQualifiedName()) + "', scope=" + scope); } final List<PsiDirectory> writableDirectoryList = new ArrayList<PsiDirectory>(); if (context != null) { for (PsiDirectory directory : context.getDirectories()) { if (LOG.isDebugEnabled()) { LOG.debug("Package directory: " + directory); } if (directory.isWritable() && scope.contains(directory.getVirtualFile())) { writableDirectoryList.add(directory); } } } else { for (VirtualFile root : ProjectRootManager.getInstance(psiManager.getProject()).getContentSourceRoots()) { PsiDirectory directory = psiManager.findDirectory(root); if (LOG.isDebugEnabled()) { LOG.debug("Root: " + root + ", directory: " + directory); } if (directory != null && directory.isWritable() && scope.contains(directory.getVirtualFile())) { writableDirectoryList.add(directory); } } } if (LOG.isDebugEnabled()) { LOG.debug("Result " + writableDirectoryList); } return writableDirectoryList; }
public static Collection<ErlangFile> findFileTestElements( Project project, DataContext dataContext) { VirtualFile[] selectedFiles = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext); if (selectedFiles == null) return Collections.emptyList(); List<ErlangFile> testFiles = new ArrayList<ErlangFile>(selectedFiles.length); PsiManager psiManager = PsiManager.getInstance(project); for (VirtualFile file : selectedFiles) { if (!file.isDirectory()) { ContainerUtil.addIfNotNull(testFiles, getFileTestElement(psiManager.findFile(file))); continue; } PsiDirectory directory = psiManager.findDirectory(file); PsiFile[] children = directory == null ? new PsiFile[0] : directory.getFiles(); for (PsiFile psiFile : children) { ContainerUtil.addIfNotNull(testFiles, getFileTestElement(psiFile)); } } return testFiles; }
@Override @NotNull public Collection<AbstractTreeNode> getChildren() { Module module = getValue(); if (module == null || module.isDisposed()) { // module has been disposed return Collections.emptyList(); } ModuleRootManager rootManager = ModuleRootManager.getInstance(module); ModuleFileIndex moduleFileIndex = rootManager.getFileIndex(); final VirtualFile[] contentRoots = rootManager.getContentRoots(); final List<AbstractTreeNode> children = new ArrayList<AbstractTreeNode>(contentRoots.length + 1); final PsiManager psiManager = PsiManager.getInstance(module.getProject()); for (final VirtualFile contentRoot : contentRoots) { if (!moduleFileIndex.isInContent(contentRoot)) continue; if (contentRoot.isDirectory()) { PsiDirectory directory = psiManager.findDirectory(contentRoot); if (directory != null) { children.add(new PsiDirectoryNode(getProject(), directory, getSettings())); } } else { PsiFile file = psiManager.findFile(contentRoot); if (file != null) { children.add(new PsiFileNode(getProject(), file, getSettings())); } } } /* if (getSettings().isShowLibraryContents()) { children.add(new LibraryGroupNode(getProject(), new LibraryGroupElement(getValue()), getSettings())); } */ return children; }
@NotNull private static PsiClass createBeanClass(final WizardData wizardData) throws MyException { final PsiManager psiManager = PsiManager.getInstance(wizardData.myProject); final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(wizardData.myProject); final ProjectFileIndex fileIndex = projectRootManager.getFileIndex(); final VirtualFile sourceRoot = fileIndex.getSourceRootForFile(wizardData.myFormFile); if (sourceRoot == null) { throw new MyException(UIDesignerBundle.message("error.form.file.is.not.in.source.root")); } final PsiDirectory rootDirectory = psiManager.findDirectory(sourceRoot); LOG.assertTrue(rootDirectory != null); final PsiPackage aPackage = JavaPsiFacade.getInstance(psiManager.getProject()).findPackage(wizardData.myPackageName); if (aPackage == null) { throw new MyException( UIDesignerBundle.message("error.package.does.not.exist", wizardData.myPackageName)); } PsiDirectory targetDir = null; final PsiDirectory[] directories = aPackage.getDirectories(); for (final PsiDirectory psiDirectory : directories) { if (PsiTreeUtil.isAncestor(rootDirectory, psiDirectory, false)) { targetDir = psiDirectory; break; } } if (targetDir == null) { // todo throw new MyException( UIDesignerBundle.message("error.cannot.find.package", wizardData.myPackageName)); } //noinspection HardCodedStringLiteral final String body = "public class " + wizardData.myShortClassName + "{\n" + "public " + wizardData.myShortClassName + "(){}\n" + "}"; try { PsiFile sourceFile = PsiFileFactory.getInstance(psiManager.getProject()) .createFileFromText(wizardData.myShortClassName + ".java", body); sourceFile = (PsiFile) targetDir.add(sourceFile); final PsiClass beanClass = ((PsiJavaFile) sourceFile).getClasses()[0]; final ArrayList<String> properties = new ArrayList<String>(); final HashMap<String, String> property2fqClassName = new HashMap<String, String>(); final FormProperty2BeanProperty[] bindings = wizardData.myBindings; for (final FormProperty2BeanProperty binding : bindings) { if (binding == null || binding.myBeanProperty == null) { continue; } properties.add(binding.myBeanProperty.myName); // todo: handle "casts" ? final String propertyClassName = binding.myFormProperty.getComponentPropertyClassName(); property2fqClassName.put(binding.myBeanProperty.myName, propertyClassName); } generateBean(beanClass, ArrayUtil.toStringArray(properties), property2fqClassName); return beanClass; } catch (IncorrectOperationException e) { throw new MyException(e.getMessage()); } }
protected AnalysisScope createAnalysisScope(VirtualFile sourceDir) { PsiManager psiManager = PsiManager.getInstance(myProject); return new AnalysisScope(psiManager.findDirectory(sourceDir)); }