Merge remote-tracking branch 'origin/master'
[idea/community.git] / plugins / gradle / src / org / jetbrains / plugins / gradle / diff / GradleModuleStructureChangesCalculator.java
1 /*
2  * Copyright 2000-2011 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.jetbrains.plugins.gradle.diff;
17
18 import com.intellij.openapi.module.Module;
19 import com.intellij.openapi.roots.*;
20 import org.jetbrains.annotations.NotNull;
21 import org.jetbrains.plugins.gradle.model.*;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Set;
26
27 /**
28  * Encapsulates functionality of calculating changes between Gradle and IntelliJ IDEA module hierarchies.
29  * <p/>
30  * Thread-safe.
31  *
32  * @author Denis Zhdanov
33  * @since 11/14/11 6:30 PM
34  */
35 public class GradleModuleStructureChangesCalculator implements GradleStructureChangesCalculator<GradleModule, Module> {
36   
37   private final GradleLibraryDependencyStructureChangesCalculator myLibraryDependencyCalculator;
38   private final GradleProjectStructureHelper myStructureHelper;
39
40   public GradleModuleStructureChangesCalculator(@NotNull GradleLibraryDependencyStructureChangesCalculator libraryDependencyCalculator,
41                                                 @NotNull GradleProjectStructureHelper structureHelper)
42   {
43     myLibraryDependencyCalculator = libraryDependencyCalculator;
44     myStructureHelper = structureHelper;
45   }
46
47   @Override
48   public void calculate(@NotNull GradleModule gradleEntity,
49                         @NotNull Module intellijEntity,
50                         @NotNull Set<GradleProjectStructureChange> knownChanges,
51                         @NotNull Set<GradleProjectStructureChange> currentChanges)
52   {
53     //TODO den process module-local settings
54     //TODO den process content roots
55     checkDependencies(gradleEntity, intellijEntity, knownChanges, currentChanges); 
56   }
57
58   @NotNull
59   @Override
60   public Object getIntellijKey(@NotNull Module entity) {
61     return entity.getName();
62   }
63
64   @NotNull
65   @Override
66   public Object getGradleKey(@NotNull GradleModule entity, @NotNull Set<GradleProjectStructureChange> knownChanges) {
67     // TODO den consider the known changes
68     return entity.getName();
69   }
70
71   private void checkDependencies(@NotNull GradleModule gradleModule,
72                                  @NotNull Module intellijModule,
73                                  @NotNull Set<GradleProjectStructureChange> knownChanges,
74                                  @NotNull Set<GradleProjectStructureChange> currentChanges)
75   {
76     // Prepare intellij part.
77     final List<ModuleOrderEntry> intellijModuleDependencies = new ArrayList<ModuleOrderEntry>();
78     final List<LibraryOrderEntry> intellijLibraryDependencies = new ArrayList<LibraryOrderEntry>();
79     RootPolicy<Object> policy = new RootPolicy<Object>() {
80       @Override
81       public Object visitModuleOrderEntry(ModuleOrderEntry moduleOrderEntry, Object value) {
82         intellijModuleDependencies.add(moduleOrderEntry);
83         return moduleOrderEntry;
84       }
85
86       @Override
87       public Object visitLibraryOrderEntry(LibraryOrderEntry libraryOrderEntry, Object value) {
88         intellijLibraryDependencies.add(libraryOrderEntry);
89         return libraryOrderEntry;
90       }
91     };
92     for (OrderEntry orderEntry : myStructureHelper.getOrderEntries(intellijModule)) {
93       orderEntry.accept(policy, null);
94     }
95
96     // Prepare gradle part.
97     final List<GradleModuleDependency> gradleModuleDependencies = new ArrayList<GradleModuleDependency>();
98     final List<GradleLibraryDependency> gradleLibraryDependencies = new ArrayList<GradleLibraryDependency>();
99     GradleEntityVisitor visitor = new GradleEntityVisitorAdapter() {
100       @Override
101       public void visit(@NotNull GradleModuleDependency dependency) {
102         gradleModuleDependencies.add(dependency);
103       }
104
105       @Override
106       public void visit(@NotNull GradleLibraryDependency dependency) {
107         gradleLibraryDependencies.add(dependency);
108       }
109     };
110     for (GradleDependency dependency : gradleModule.getDependencies()) {
111       dependency.invite(visitor);
112     }
113
114     // Calculate changes.
115     // TODO den process module dependencies here as well.
116     GradleDiffUtil.calculate(myLibraryDependencyCalculator, gradleLibraryDependencies, intellijLibraryDependencies,
117                              knownChanges, currentChanges);
118   }
119 }