下面列出了怎么用com.intellij.openapi.roots.JavaProjectRootsUtil的API类实例代码及写法,或者点击链接到github查看源代码。
private static SourceFolder suitableTestSourceFolders(Project project, Module module) {
ContentEntry[] contentEntries = ModuleRootManager.getInstance(module).getContentEntries();
for (ContentEntry contentEntry : contentEntries) {
List<SourceFolder> testSourceFolders =
contentEntry.getSourceFolders(JavaSourceRootType.TEST_SOURCE);
for (SourceFolder testSourceFolder : testSourceFolders) {
if (testSourceFolder.getFile() != null) {
if (!JavaProjectRootsUtil.isInGeneratedCode(testSourceFolder.getFile(), project)) {
return testSourceFolder;
}
}
}
}
return null;
}
@Override
protected JPanel createDestinationRootPanel() {
final List<VirtualFile> sourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(myProject);
if (sourceRoots.size() <= 1) return super.createDestinationRootPanel();
final JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
final JBLabel label = new JBLabel(RefactoringBundle.message("target.destination.folder"));
panel.add(label, BorderLayout.NORTH);
label.setLabelFor(myDestinationFolderComboBox);
myDestinationFolderComboBox.setData(myProject, myTargetDirectory, new Pass<String>() {
@Override
public void pass(String s) {
}
}, ((PackageNameReferenceEditorCombo)myPackageNameField).getChildComponent());
panel.add(myDestinationFolderComboBox, BorderLayout.CENTER);
return panel;
}
/**
* allow user to select the generated code source root
*/
private DestinationChooser.Destination chooseDestination(ClassEntry classEntry, Project project, PsiElement psiElement) {
String packageName = classEntry.getPackageName();
final PackageWrapper targetPackage = new PackageWrapper(PsiManager.getInstance(project), packageName);
List<VirtualFile> suitableRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(project);
return DestinationChooser.chooseDestination(targetPackage, suitableRoots,
psiElement.getContainingFile().getContainingDirectory());
}