Search in sources :

Example 1 with LibraryRootType

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) LibraryRootType(com.intellij.openapi.roots.libraries.LibraryRootType) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) Pair(com.intellij.openapi.util.Pair) OrderRoot(com.intellij.openapi.roots.libraries.ui.OrderRoot) DetectedLibraryRoot(com.intellij.openapi.roots.libraries.ui.DetectedLibraryRoot) OrderRootType(com.intellij.openapi.roots.OrderRootType) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with LibraryRootType

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;
    });
}
Also used : Arrays(java.util.Arrays) OrderRootType(com.intellij.openapi.roots.OrderRootType) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Collection(java.util.Collection) DetectedLibraryRoot(com.intellij.openapi.roots.libraries.ui.DetectedLibraryRoot) ContainerUtil(com.intellij.util.containers.ContainerUtil) FlexBundle(com.intellij.lang.javascript.flex.FlexBundle) JavadocOrderRootType(com.intellij.openapi.roots.JavadocOrderRootType) LibraryRootsDetectorImpl(com.intellij.openapi.roots.libraries.ui.impl.LibraryRootsDetectorImpl) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) LibraryRootType(com.intellij.openapi.roots.libraries.LibraryRootType) Function(com.intellij.util.Function) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Condition(com.intellij.openapi.util.Condition) LibraryRootType(com.intellij.openapi.roots.libraries.LibraryRootType) DetectedLibraryRoot(com.intellij.openapi.roots.libraries.ui.DetectedLibraryRoot)

Aggregations

ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 OrderRootType (com.intellij.openapi.roots.OrderRootType)2 LibraryRootType (com.intellij.openapi.roots.libraries.LibraryRootType)2 DetectedLibraryRoot (com.intellij.openapi.roots.libraries.ui.DetectedLibraryRoot)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 NotNull (org.jetbrains.annotations.NotNull)2 FlexBundle (com.intellij.lang.javascript.flex.FlexBundle)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 Task (com.intellij.openapi.progress.Task)1 JavadocOrderRootType (com.intellij.openapi.roots.JavadocOrderRootType)1 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)1 LibraryRootsDetectorImpl (com.intellij.openapi.roots.libraries.ui.impl.LibraryRootsDetectorImpl)1 Condition (com.intellij.openapi.util.Condition)1 Pair (com.intellij.openapi.util.Pair)1 Function (com.intellij.util.Function)1 ContainerUtil (com.intellij.util.containers.ContainerUtil)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1