use of com.intellij.openapi.roots.libraries.LibraryRootType in project intellij-community by JetBrains.
the class RootDetectionUtil method detectRoots.
@NotNull
public static List<OrderRoot> detectRoots(@NotNull final Collection<VirtualFile> rootCandidates, @Nullable Component parentComponent, @Nullable Project project, @NotNull final LibraryRootsDetector detector, @NotNull OrderRootType[] rootTypesAllowedToBeSelectedByUserIfNothingIsDetected) {
final List<OrderRoot> result = new ArrayList<>();
final List<SuggestedChildRootInfo> suggestedRoots = new ArrayList<>();
new Task.Modal(project, "Scanning for Roots", true) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
try {
for (VirtualFile rootCandidate : rootCandidates) {
final Collection<DetectedLibraryRoot> roots = detector.detectRoots(rootCandidate, indicator);
if (!roots.isEmpty() && allRootsHaveOneTypeAndEqualToOrAreDirectParentOf(roots, rootCandidate)) {
for (DetectedLibraryRoot root : roots) {
final LibraryRootType libraryRootType = root.getTypes().get(0);
result.add(new OrderRoot(root.getFile(), libraryRootType.getType(), libraryRootType.isJarDirectory()));
}
} else {
for (DetectedLibraryRoot root : roots) {
final HashMap<LibraryRootType, String> names = new HashMap<>();
for (LibraryRootType type : root.getTypes()) {
final String typeName = detector.getRootTypeName(type);
LOG.assertTrue(typeName != null, "Unexpected root type " + type.getType().name() + (type.isJarDirectory() ? " (JAR directory)" : "") + ", detectors: " + detector);
names.put(type, typeName);
}
suggestedRoots.add(new SuggestedChildRootInfo(rootCandidate, root, names));
}
}
}
} catch (ProcessCanceledException ignored) {
}
}
}.queue();
if (!suggestedRoots.isEmpty()) {
final DetectedRootsChooserDialog dialog = parentComponent != null ? new DetectedRootsChooserDialog(parentComponent, suggestedRoots) : new DetectedRootsChooserDialog(project, suggestedRoots);
if (!dialog.showAndGet()) {
return Collections.emptyList();
}
for (SuggestedChildRootInfo rootInfo : dialog.getChosenRoots()) {
final LibraryRootType selectedRootType = rootInfo.getSelectedRootType();
result.add(new OrderRoot(rootInfo.getDetectedRoot().getFile(), selectedRootType.getType(), selectedRootType.isJarDirectory()));
}
}
if (result.isEmpty() && rootTypesAllowedToBeSelectedByUserIfNothingIsDetected.length > 0) {
Map<String, Pair<OrderRootType, Boolean>> types = new HashMap<>();
for (OrderRootType type : rootTypesAllowedToBeSelectedByUserIfNothingIsDetected) {
for (boolean isJarDirectory : new boolean[] { false, true }) {
final String typeName = detector.getRootTypeName(new LibraryRootType(type, isJarDirectory));
if (typeName != null) {
types.put(StringUtil.capitalizeWords(typeName, true), Pair.create(type, isJarDirectory));
}
}
}
LOG.assertTrue(!types.isEmpty(), "No allowed root types found for " + detector);
List<String> names = new ArrayList<>(types.keySet());
if (names.size() == 1) {
String title = "Attach Roots";
String typeName = names.get(0);
String message = ApplicationNamesInfo.getInstance().getProductName() + " cannot determine what kind of files the chosen items contain. " + "Do you want to attach them as '" + typeName + "'?";
int answer = parentComponent != null ? Messages.showYesNoDialog(parentComponent, message, title, null) : Messages.showYesNoDialog(project, message, title, null);
if (answer == Messages.YES) {
Pair<OrderRootType, Boolean> pair = types.get(typeName);
for (VirtualFile candidate : rootCandidates) {
result.add(new OrderRoot(candidate, pair.getFirst(), pair.getSecond()));
}
}
} else {
String title = "Choose Categories of Selected Files";
String description = XmlStringUtil.wrapInHtml(ApplicationNamesInfo.getInstance().getProductName() + " cannot determine what kind of files the chosen items contain.<br>" + "Choose the appropriate categories from the list.");
ChooseElementsDialog<String> dialog;
if (parentComponent != null) {
dialog = new ChooseRootTypeElementsDialog(parentComponent, names, title, description);
} else {
dialog = new ChooseRootTypeElementsDialog(project, names, title, description);
}
for (String rootType : dialog.showAndGetResult()) {
final Pair<OrderRootType, Boolean> pair = types.get(rootType);
for (VirtualFile candidate : rootCandidates) {
result.add(new OrderRoot(candidate, pair.getFirst(), pair.getSecond()));
}
}
}
}
return result;
}
use of com.intellij.openapi.roots.libraries.LibraryRootType in project intellij-plugins by JetBrains.
the class FlexLibraryRootsDetector method detectRoots.
@Override
public Collection<DetectedLibraryRoot> detectRoots(@NotNull final VirtualFile rootCandidate, @NotNull final ProgressIndicator progressIndicator) {
Collection<DetectedLibraryRoot> roots = super.detectRoots(rootCandidate, progressIndicator);
boolean swcsFoldersFound = ContainerUtil.find(roots, root -> {
LibraryRootType libraryRootType = root.getTypes().get(0);
return libraryRootType.getType() == OrderRootType.CLASSES && libraryRootType.isJarDirectory();
}) != null;
final List<LibraryRootType> types = Arrays.asList(new LibraryRootType(OrderRootType.CLASSES, false), new LibraryRootType(OrderRootType.SOURCES, false));
if (swcsFoldersFound) {
// if both sources and swcs were detected, assume that source files are src attachment, otherwise assume they are raw as libraries
Collections.reverse(types);
}
return ContainerUtil.map(roots, root -> {
if (root.getTypes().get(0).getType() == OrderRootType.SOURCES) {
return new DetectedLibraryRoot(root.getFile(), types);
}
return root;
});
}
Aggregations