Search in sources :

Example 1 with SourceFileContainerInfo

use of com.android.tools.idea.gradle.project.model.AndroidModuleModel.SourceFileContainerInfo in project android by JetBrains.

the class ProjectSubset method addResultAndPopulateProject.

/**
   * Adds the module in the given search results to the IDE. If the search result indicates the variant where the file is, this method
   * will select such variant in the Android model.
   *
   * @param result          the search result.
   * @param projectInfo     information about the project.
   * @param selectedModules all the modules to be included in the project.
   * @param file            the file to include in the project.
   */
private void addResultAndPopulateProject(@NotNull ModuleSearchResult result, @NotNull DataNode<ProjectData> projectInfo, @NotNull List<DataNode<ModuleData>> selectedModules, @NotNull File file) {
    DataNode<ModuleData> moduleNode = result.moduleNode;
    String moduleName = getNameOf(moduleNode);
    String text;
    if (result.selected) {
        String tmp = String.format("File '%1$s' is already in module '%2$s'", file.getName(), moduleName);
        SourceFileContainerInfo containerInfo = result.containerInfo;
        if (containerInfo != null) {
            containerInfo.updateSelectedVariantIn(moduleNode);
            Variant variant = containerInfo.variant;
            if (variant != null) {
                tmp += String.format(", variant '%1$s'", variant.getName());
            }
        }
        text = tmp;
    } else {
        text = String.format("Module '%1$s' was added to the project.", moduleName);
        SourceFileContainerInfo containerInfo = result.containerInfo;
        if (containerInfo != null) {
            containerInfo.updateSelectedVariantIn(moduleNode);
        }
        selectedModules.add(moduleNode);
        setSelection(selectedModules);
    }
    invokeLaterIfNeeded(() -> {
        AndroidGradleNotification notification = AndroidGradleNotification.getInstance(myProject);
        notification.showBalloon(MODULE_LOOKUP_MESSAGE_TITLE, text, INFORMATION);
    });
    populate(myProject, projectInfo, selectedModules, DEFAULT_REQUEST);
}
Also used : Variant(com.android.builder.model.Variant) SourceFileContainerInfo(com.android.tools.idea.gradle.project.model.AndroidModuleModel.SourceFileContainerInfo) ModuleData(com.intellij.openapi.externalSystem.model.project.ModuleData) AndroidGradleNotification(com.android.tools.idea.gradle.project.AndroidGradleNotification)

Example 2 with SourceFileContainerInfo

use of com.android.tools.idea.gradle.project.model.AndroidModuleModel.SourceFileContainerInfo in project android by JetBrains.

the class ProjectSubset method containsSourceFile.

/**
   * Checks in the Android and Java models to see if the module contains the given file.
   *
   * @param moduleInfos represents the module that is not included yet in the IDE.
   * @param file        the given file.
   * @param selected    indicates whether the module is included in the project.
   * @return the result of the search, or {@code null} if this module does not contain the given file.
   */
@Nullable
private static ModuleSearchResult containsSourceFile(@NotNull DataNode<ModuleData> moduleInfos, @NotNull File file, boolean selected) {
    DataNode<AndroidModuleModel> androidProjectNode = find(moduleInfos, ANDROID_MODEL);
    if (androidProjectNode != null) {
        AndroidModuleModel androidModel = androidProjectNode.getData();
        SourceFileContainerInfo result = androidModel.containsSourceFile(file);
        if (result != null) {
            return new ModuleSearchResult(moduleInfos, result, selected);
        }
    }
    DataNode<JavaModuleModel> javaProjectNode = find(moduleInfos, JAVA_MODULE_MODEL);
    if (javaProjectNode != null) {
        JavaModuleModel javaModuleModel = javaProjectNode.getData();
        if (javaModuleModel.containsSourceFile(file)) {
            return new ModuleSearchResult(moduleInfos, null, selected);
        }
    }
    return null;
}
Also used : SourceFileContainerInfo(com.android.tools.idea.gradle.project.model.AndroidModuleModel.SourceFileContainerInfo) JavaModuleModel(com.android.tools.idea.gradle.project.model.JavaModuleModel) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with SourceFileContainerInfo

use of com.android.tools.idea.gradle.project.model.AndroidModuleModel.SourceFileContainerInfo in project android by JetBrains.

the class ProjectSubset method showModuleSelectionDialog.

/**
   * Displays the "Select Modules" dialog. This method is invoked when the search for a module containing a file returns more than one
   * result. The user now needs to select the module(s) to include.
   *
   * @param searchResults includes the modules that might contain the given file.
   * @param selection     all the modules that need to be included in the project.
   * @param file          the file to include in the project.
   */
private void showModuleSelectionDialog(@NotNull List<ModuleSearchResult> searchResults, @NotNull DataNode<ProjectData> projectInfo, @NotNull List<DataNode<ModuleData>> selection, @NotNull File file) {
    List<DataNode<ModuleData>> finalSelection = Lists.newArrayList(selection);
    List<DataNode<ModuleData>> modulesToDisplayInDialog = Lists.newArrayList();
    Map<String, ModuleSearchResult> resultsByModuleName = Maps.newHashMap();
    for (ModuleSearchResult result : searchResults) {
        DataNode<ModuleData> module = result.moduleNode;
        modulesToDisplayInDialog.add(module);
        if (result.selected) {
            finalSelection.remove(module);
        }
        String moduleName = getNameOf(module);
        resultsByModuleName.put(moduleName, result);
    }
    invokeLaterIfNeeded(() -> {
        ModulesToImportDialog dialog = new ModulesToImportDialog(modulesToDisplayInDialog, myProject);
        String description = String.format("The file '%1$s' may be include in one of the following modules.", file.getName());
        dialog.setDescription(description);
        dialog.clearSelection();
        if (dialog.showAndGet()) {
            Collection<DataNode<ModuleData>> selectedModules = dialog.getSelectedModules();
            if (!selectedModules.isEmpty()) {
                for (DataNode<ModuleData> selectedModule : selectedModules) {
                    String name = getNameOf(selectedModule);
                    ModuleSearchResult result = resultsByModuleName.get(name);
                    if (result != null) {
                        SourceFileContainerInfo containerInfo = result.containerInfo;
                        if (containerInfo != null) {
                            containerInfo.updateSelectedVariantIn(selectedModule);
                        }
                    }
                }
                finalSelection.addAll(selectedModules);
                setSelection(finalSelection);
                populate(myProject, projectInfo, finalSelection, DEFAULT_REQUEST);
            }
        }
    });
}
Also used : SourceFileContainerInfo(com.android.tools.idea.gradle.project.model.AndroidModuleModel.SourceFileContainerInfo) DataNode(com.intellij.openapi.externalSystem.model.DataNode) ModuleData(com.intellij.openapi.externalSystem.model.project.ModuleData)

Aggregations

SourceFileContainerInfo (com.android.tools.idea.gradle.project.model.AndroidModuleModel.SourceFileContainerInfo)3 ModuleData (com.intellij.openapi.externalSystem.model.project.ModuleData)2 Variant (com.android.builder.model.Variant)1 AndroidGradleNotification (com.android.tools.idea.gradle.project.AndroidGradleNotification)1 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)1 JavaModuleModel (com.android.tools.idea.gradle.project.model.JavaModuleModel)1 DataNode (com.intellij.openapi.externalSystem.model.DataNode)1 Nullable (org.jetbrains.annotations.Nullable)1