Search in sources :

Example 1 with CheckboxTree

use of com.intellij.ui.CheckboxTree in project android by JetBrains.

the class ModuleVariantsInfoDialog method createDependenciesTree.

@NotNull
private static JTree createDependenciesTree(@NotNull Module module, @NotNull AndroidModuleModel androidModel) {
    VariantCheckboxTreeCellRenderer renderer = new VariantCheckboxTreeCellRenderer() {

        @Override
        public void customizeRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            if (value instanceof DefaultMutableTreeNode) {
                Object data = ((DefaultMutableTreeNode) value).getUserObject();
                if (data instanceof String) {
                    appendVariant((String) data);
                } else if (data instanceof DependencyTreeElement) {
                    DependencyTreeElement dependency = (DependencyTreeElement) data;
                    appendModule(dependency.myModule, dependency.myVariant);
                }
            }
        }
    };
    //noinspection ConstantConditions
    CheckedTreeNode root = new CheckedTreeNode(null);
    AndroidProject androidProject = GradleUtil.getAndroidProject(module);
    assert androidProject != null;
    Multimap<String, DependencyTreeElement> dependenciesByVariant = HashMultimap.create();
    for (Variant variant : androidProject.getVariants()) {
        for (AndroidLibrary library : GradleUtil.getDirectLibraryDependencies(variant, androidModel)) {
            String gradlePath = library.getProject();
            if (gradlePath == null) {
                continue;
            }
            Module dependency = GradleUtil.findModuleByGradlePath(module.getProject(), gradlePath);
            if (dependency != null) {
                DependencyTreeElement element = new DependencyTreeElement(dependency, library.getProjectVariant());
                dependenciesByVariant.put(variant.getName(), element);
            }
        }
    }
    // Consolidate variants. This means if "debug" and "release" have the same dependencies, we show only one node as "debug, release".
    List<String> variantNames = Lists.newArrayList(dependenciesByVariant.keySet());
    Collections.sort(variantNames);
    List<String> consolidatedVariants = Lists.newArrayList();
    List<String> variantsToSkip = Lists.newArrayList();
    int variantCount = variantNames.size();
    for (int i = 0; i < variantCount; i++) {
        String variant1 = variantNames.get(i);
        if (variantsToSkip.contains(variant1)) {
            continue;
        }
        Collection<DependencyTreeElement> set1 = dependenciesByVariant.get(variant1);
        for (int j = i + 1; j < variantCount; j++) {
            String variant2 = variantNames.get(j);
            Collection<DependencyTreeElement> set2 = dependenciesByVariant.get(variant2);
            if (set1.equals(set2)) {
                variantsToSkip.add(variant2);
                if (!consolidatedVariants.contains(variant1)) {
                    consolidatedVariants.add(variant1);
                }
                consolidatedVariants.add(variant2);
            }
        }
        String variantName = variant1;
        if (!consolidatedVariants.isEmpty()) {
            variantName = Joiner.on(", ").join(consolidatedVariants);
        }
        DefaultMutableTreeNode variantNode = new DefaultMutableTreeNode(variantName);
        root.add(variantNode);
        List<DependencyTreeElement> dependencies = Lists.newArrayList(set1);
        Collections.sort(dependencies);
        for (DependencyTreeElement dependency : dependencies) {
            variantNode.add(new DefaultMutableTreeNode(dependency));
        }
        consolidatedVariants.clear();
    }
    CheckboxTree tree = new CheckboxTree(renderer, root);
    tree.setRootVisible(false);
    TreeUtil.expandAll(tree);
    return tree;
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) CheckboxTree(com.intellij.ui.CheckboxTree) CheckedTreeNode(com.intellij.ui.CheckedTreeNode) AndroidProject(com.android.builder.model.AndroidProject) Variant(com.android.builder.model.Variant) AndroidLibrary(com.android.builder.model.AndroidLibrary) Module(com.intellij.openapi.module.Module) VariantCheckboxTreeCellRenderer(com.android.tools.idea.gradle.variant.ui.VariantCheckboxTreeCellRenderer) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with CheckboxTree

use of com.intellij.ui.CheckboxTree in project android by JetBrains.

the class ModuleVariantsInfoDialog method createDependentsTree.

@NotNull
private static JTree createDependentsTree(@NotNull Module module, @NotNull AndroidModuleModel androidModel) {
    VariantCheckboxTreeCellRenderer renderer = new VariantCheckboxTreeCellRenderer() {

        @Override
        public void customizeRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            if (value instanceof DefaultMutableTreeNode) {
                Object data = ((DefaultMutableTreeNode) value).getUserObject();
                if (data instanceof String) {
                    appendVariant((String) data);
                } else if (data instanceof DependentTreeElement) {
                    DependentTreeElement dependent = (DependentTreeElement) data;
                    String variant = null;
                    if (!dependent.myVariants.isEmpty()) {
                        variant = Joiner.on(", ").join(dependent.myVariants);
                    }
                    appendModule(dependent.myModule, variant);
                }
            }
        }
    };
    //noinspection ConstantConditions
    CheckedTreeNode root = new CheckedTreeNode(null);
    AndroidProject androidProject = GradleUtil.getAndroidProject(module);
    assert androidProject != null;
    String gradlePath = GradleUtil.getGradlePath(module);
    assert gradlePath != null;
    Multimap<String, DependentTreeElement> dependentsByVariant = HashMultimap.create();
    ModuleManager moduleManager = ModuleManager.getInstance(module.getProject());
    for (Module dependent : moduleManager.getModuleDependentModules(module)) {
        AndroidProject dependentProject = GradleUtil.getAndroidProject(dependent);
        if (dependentProject == null) {
            continue;
        }
        DependentTreeElement element = new DependentTreeElement(dependent);
        for (Variant variant : dependentProject.getVariants()) {
            for (AndroidLibrary library : GradleUtil.getDirectLibraryDependencies(variant, androidModel)) {
                if (gradlePath.equals(library.getProject())) {
                    element.addVariant(variant.getName());
                    String projectVariant = library.getProjectVariant();
                    if (StringUtil.isNotEmpty(projectVariant)) {
                        dependentsByVariant.put(projectVariant, element);
                    }
                }
            }
        }
    }
    List<String> variantNames = Lists.newArrayList(dependentsByVariant.keySet());
    Collections.sort(variantNames);
    for (String variantName : variantNames) {
        Collection<DependentTreeElement> dependents = dependentsByVariant.get(variantName);
        if (!dependents.isEmpty()) {
            List<DependentTreeElement> sortedDependents = Lists.newArrayList(dependents);
            Collections.sort(sortedDependents);
            DefaultMutableTreeNode variantNode = new DefaultMutableTreeNode(variantName);
            for (DependentTreeElement dependent : dependents) {
                variantNode.add(new DefaultMutableTreeNode(dependent));
            }
            root.add(variantNode);
        }
    }
    CheckboxTree tree = new CheckboxTree(renderer, root);
    tree.setRootVisible(false);
    TreeUtil.expandAll(tree);
    return tree;
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) CheckboxTree(com.intellij.ui.CheckboxTree) CheckedTreeNode(com.intellij.ui.CheckedTreeNode) AndroidProject(com.android.builder.model.AndroidProject) ModuleManager(com.intellij.openapi.module.ModuleManager) Variant(com.android.builder.model.Variant) AndroidLibrary(com.android.builder.model.AndroidLibrary) Module(com.intellij.openapi.module.Module) VariantCheckboxTreeCellRenderer(com.android.tools.idea.gradle.variant.ui.VariantCheckboxTreeCellRenderer) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AndroidLibrary (com.android.builder.model.AndroidLibrary)2 AndroidProject (com.android.builder.model.AndroidProject)2 Variant (com.android.builder.model.Variant)2 VariantCheckboxTreeCellRenderer (com.android.tools.idea.gradle.variant.ui.VariantCheckboxTreeCellRenderer)2 Module (com.intellij.openapi.module.Module)2 CheckboxTree (com.intellij.ui.CheckboxTree)2 CheckedTreeNode (com.intellij.ui.CheckedTreeNode)2 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 NotNull (org.jetbrains.annotations.NotNull)2 ModuleManager (com.intellij.openapi.module.ModuleManager)1