'add library to dependencies' fix migrated to use new ProjectModelModificationService
authornik <Nikolay.Chashnikov@jetbrains.com>
Fri, 7 Aug 2015 15:46:19 +0000 (18:46 +0300)
committernik <Nikolay.Chashnikov@jetbrains.com>
Fri, 7 Aug 2015 15:47:18 +0000 (18:47 +0300)
java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/artifacts/sourceItems/LibrarySourceItem.java
java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddLibraryToDependenciesFix.java [new file with mode: 0644]
java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/OrderEntryFix.java
java/java-impl/src/com/intellij/openapi/roots/ProjectModelModificationService.java
java/java-impl/src/com/intellij/openapi/roots/ProjectModelModifier.java
java/java-impl/src/com/intellij/openapi/roots/impl/IdeaProjectModelModifier.java
java/java-impl/src/com/intellij/openapi/roots/impl/ProjectModelModificationServiceImpl.java
platform/lang-impl/src/com/intellij/openapi/roots/impl/OrderEntryUtil.java
platform/projectModel-api/src/com/intellij/openapi/roots/libraries/LibraryUtil.java

index c726c9f08e0bebbbfa756fae107fa085cdbdf5ed..292c06e11243eb3cfb3c1d65fdbafebef2bcd750 100644 (file)
@@ -20,6 +20,7 @@ import com.intellij.ide.projectView.PresentationData;
 import com.intellij.openapi.roots.OrderRootType;
 import com.intellij.openapi.roots.impl.libraries.LibraryEx;
 import com.intellij.openapi.roots.libraries.Library;
+import com.intellij.openapi.roots.libraries.LibraryUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.packaging.elements.PackagingElement;
 import com.intellij.packaging.elements.PackagingElementFactory;
@@ -97,12 +98,7 @@ public class LibrarySourceItem extends PackagingSourceItem {
 
     @Override
     public String getPresentableName() {
-      final String name = myLibrary.getName();
-      if (name != null) {
-        return name;
-      }
-      final VirtualFile[] files = myLibrary.getFiles(OrderRootType.CLASSES);
-      return files.length > 0 ? files[0].getName() : "Empty Library";
+      return LibraryUtil.getPresentableName(myLibrary);
     }
 
     @Override
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddLibraryToDependenciesFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddLibraryToDependenciesFix.java
new file mode 100644 (file)
index 0000000..e76071e
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2000-2015 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.codeInsight.daemon.impl.quickfix;
+
+import com.intellij.codeInsight.daemon.QuickFixBundle;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.module.Module;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.roots.DependencyScope;
+import com.intellij.openapi.roots.ProjectModelModificationService;
+import com.intellij.openapi.roots.impl.libraries.LibraryEx;
+import com.intellij.openapi.roots.libraries.Library;
+import com.intellij.openapi.roots.libraries.LibraryUtil;
+import com.intellij.psi.PsiFile;
+import com.intellij.psi.PsiReference;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * @author nik
+ */
+class AddLibraryToDependenciesFix extends OrderEntryFix {
+  private final Library myLibrary;
+  private final Module myCurrentModule;
+  private final PsiReference myReference;
+  private final String myQualifiedClassName;
+
+  public AddLibraryToDependenciesFix(@NotNull Module currentModule,
+                                     @NotNull Library library,
+                                     @NotNull PsiReference reference,
+                                     @Nullable String qualifiedClassName) {
+    myLibrary = library;
+    myCurrentModule = currentModule;
+    myReference = reference;
+    myQualifiedClassName = qualifiedClassName;
+  }
+
+  @Override
+  @NotNull
+  public String getText() {
+    return QuickFixBundle.message("orderEntry.fix.add.library.to.classpath", LibraryUtil.getPresentableName(myLibrary));
+  }
+
+  @Override
+  @NotNull
+  public String getFamilyName() {
+    return QuickFixBundle.message("orderEntry.fix.family.add.library.to.classpath");
+  }
+
+  @Override
+  public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
+    return !project.isDisposed() && !myCurrentModule.isDisposed() && !((LibraryEx)myLibrary).isDisposed();
+  }
+
+  @Override
+  public void invoke(@NotNull final Project project, @Nullable final Editor editor, PsiFile file) {
+    DependencyScope scope = suggestScopeByLocation(myCurrentModule, myReference.getElement());
+    ProjectModelModificationService.getInstance(project).addDependency(myCurrentModule, myLibrary, scope);
+    if (editor != null) {
+      importClass(myCurrentModule, editor, myReference, myQualifiedClassName);
+    }
+  }
+}
index 5d8fe9e4547c380d92ba0c24da0eb8f0fc6db3f5..93cc86cd5f0bbb77eca585c942deca2bd1d333e0 100644 (file)
@@ -16,7 +16,6 @@
 package com.intellij.codeInsight.daemon.impl.quickfix;
 
 import com.intellij.codeInsight.daemon.QuickFixActionRegistrar;
-import com.intellij.codeInsight.daemon.QuickFixBundle;
 import com.intellij.codeInsight.daemon.impl.actions.AddImportAction;
 import com.intellij.openapi.roots.ExternalLibraryDescriptor;
 import com.intellij.codeInsight.daemon.quickFix.ExternalLibraryResolver;
@@ -30,7 +29,6 @@ import com.intellij.openapi.module.Module;
 import com.intellij.openapi.project.DumbService;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.roots.*;
-import com.intellij.openapi.roots.impl.OrderEntryUtil;
 import com.intellij.openapi.roots.libraries.Library;
 import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.vfs.VfsUtil;
@@ -178,47 +176,9 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
                 !ModuleRootManager.getInstance(currentModule).getFileIndex().isInTestSourceContent(classVFile))) {
             continue;
           }
-          final OrderEntryFix platformFix = new OrderEntryFix() {
-            @Override
-            @NotNull
-            public String getText() {
-              return QuickFixBundle.message("orderEntry.fix.add.library.to.classpath", libraryEntry.getPresentableName());
-            }
-
-            @Override
-            @NotNull
-            public String getFamilyName() {
-              return QuickFixBundle.message("orderEntry.fix.family.add.library.to.classpath");
-            }
-
-            @Override
-            public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
-              return !project.isDisposed() && !currentModule.isDisposed() && libraryEntry.isValid();
-            }
-
-            @Override
-            public void invoke(@NotNull final Project project, @Nullable final Editor editor, PsiFile file) {
-              OrderEntryUtil.addLibraryToRoots(libraryEntry, currentModule);
-              if (editor != null) {
-                DumbService.getInstance(project).withAlternativeResolveEnabled(new Runnable() {
-                  @Override
-                  public void run() {
-                    new AddImportAction(project, reference, editor, aClass).execute();
-                  }
-                });
-              }
-            }
-          };
-
-          final OrderEntryFix providedFix = provideFix(new Function<MissingDependencyFixProvider, OrderEntryFix>() {
-            @Override
-            public OrderEntryFix fun(MissingDependencyFixProvider provider) {
-              return provider.getAddLibraryToClasspathFix(reference, platformFix, currentModule, libraryEntry, aClass);
-            }
-          });
-          final OrderEntryFix fix = ObjectUtils.notNull(providedFix, platformFix);
-          registrar.register(fix);
-          result.add(fix);
+          final OrderEntryFix platformFix = new AddLibraryToDependenciesFix(currentModule, library, reference, aClass.getQualifiedName());
+          registrar.register(platformFix);
+          result.add(platformFix);
         }
       }
     }
index 1a5e335f53333a8b43db846523a7d26edb1dcbde..e842bc6a225a941b370a0ef7c33ba2ad490a047d 100644 (file)
@@ -18,6 +18,7 @@ package com.intellij.openapi.roots;
 import com.intellij.openapi.components.ServiceManager;
 import com.intellij.openapi.module.Module;
 import com.intellij.openapi.project.Project;
+import com.intellij.openapi.roots.libraries.Library;
 import org.jetbrains.annotations.NotNull;
 
 import java.util.Collection;
@@ -47,4 +48,6 @@ public abstract class ProjectModelModificationService {
 
   public abstract void addDependency(@NotNull Collection<Module> from, @NotNull ExternalLibraryDescriptor libraryDescriptor,
                                      @NotNull DependencyScope scope);
+
+  public abstract void addDependency(@NotNull Module from, @NotNull Library library, @NotNull DependencyScope scope);
 }
index 9933040a91e7fe8018f540ef78ba48a33d599d90..398dfd0a6042eca82ae6aa226db0e0572c42b062 100644 (file)
@@ -17,6 +17,7 @@ package com.intellij.openapi.roots;
 
 import com.intellij.openapi.extensions.ExtensionPointName;
 import com.intellij.openapi.module.Module;
+import com.intellij.openapi.roots.libraries.Library;
 import org.jetbrains.annotations.NotNull;
 
 import java.util.Collection;
@@ -32,4 +33,6 @@ public abstract class ProjectModelModifier {
   public abstract boolean addExternalLibraryDependency(@NotNull Collection<Module> modules,
                                                        @NotNull ExternalLibraryDescriptor descriptor,
                                                        @NotNull DependencyScope scope);
+
+  public abstract boolean addLibraryDependency(@NotNull Module from, @NotNull Library library, @NotNull DependencyScope scope);
 }
index 7653cedd7dca8d0d4986423776f83be5df28992d..981cc47599d6918deeed593ac2d555852b0a7502 100644 (file)
@@ -84,4 +84,10 @@ public class IdeaProjectModelModifier extends ProjectModelModifier {
     }
     return true;
   }
+
+  @Override
+  public boolean addLibraryDependency(@NotNull Module from, @NotNull Library library, @NotNull DependencyScope scope) {
+    OrderEntryUtil.addLibraryToRoots(from, library);
+    return true;
+  }
 }
index 962d2bceb521b276be75307a583ec7e907f35120..5f6efd846384047bc2cbda74af22462e9a4ca7ca 100644 (file)
@@ -21,6 +21,7 @@ import com.intellij.openapi.roots.DependencyScope;
 import com.intellij.openapi.roots.ExternalLibraryDescriptor;
 import com.intellij.openapi.roots.ProjectModelModificationService;
 import com.intellij.openapi.roots.ProjectModelModifier;
+import com.intellij.openapi.roots.libraries.Library;
 import org.jetbrains.annotations.NotNull;
 
 import java.util.Collection;
@@ -53,6 +54,15 @@ public class ProjectModelModificationServiceImpl extends ProjectModelModificatio
     }
   }
 
+  @Override
+  public void addDependency(@NotNull Module from, @NotNull Library library, @NotNull DependencyScope scope) {
+    for (ProjectModelModifier modifier : getModelModifiers()) {
+      if (modifier.addLibraryDependency(from, library, scope)) {
+        return;
+      }
+    }
+  }
+
   @NotNull
   private ProjectModelModifier[] getModelModifiers() {
     return ProjectModelModifier.EP_NAME.getExtensions(myProject);
index 45f2d7c71a9a31799a4dbd4485aeaf27b23c52d7..c3099047ead04a95981835659a731d3b48912bbe 100644 (file)
@@ -136,10 +136,14 @@ public class OrderEntryUtil {
   public static void addLibraryToRoots(final LibraryOrderEntry libraryOrderEntry, final Module module) {
     Library library = libraryOrderEntry.getLibrary();
     if (library == null) return;
+    addLibraryToRoots(module, library);
+  }
+
+  public static void addLibraryToRoots(@NotNull Module module, @NotNull Library library) {
     final ModuleRootManager manager = ModuleRootManager.getInstance(module);
     final ModifiableRootModel rootModel = manager.getModifiableModel();
 
-    if (libraryOrderEntry.isModuleLevel()) {
+    if (library.getTable() == null) {
       final Library jarLibrary = rootModel.getModuleLibraryTable().createLibrary();
       final Library.ModifiableModel libraryModel = jarLibrary.getModifiableModel();
       for (OrderRootType orderRootType : OrderRootType.getAllTypes()) {
index dba8d85f4c08de94f1f0c0d176973d586295a5fa..2fd5f9346db8cc6d66ede2234959d52927f730d7 100644 (file)
@@ -162,4 +162,14 @@ public class LibraryUtil {
     }
     return null;
   }
+
+  @NotNull
+  public static String getPresentableName(@NotNull Library library) {
+    final String name = library.getName();
+    if (name != null) {
+      return name;
+    }
+    final VirtualFile[] files = library.getFiles(OrderRootType.CLASSES);
+    return files.length > 0 ? files[0].getName() : "Empty Library";
+  }
 }