com.intellij.openapi.roots.libraries.LibraryTable Java Examples

The following examples show how to use com.intellij.openapi.roots.libraries.LibraryTable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HaxeProjectConfigurationUpdater.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
private void setupLibraries(Project project, ModifiableModelsProvider modelsProvider, ModulesProvider modulesProvider, List<LibraryData> libraries) {
  LibraryTable.ModifiableModel librariesModel = modelsProvider.getLibraryTableModifiableModel(project);

  for(LibraryData lib:libraries) {
    VirtualFile root = LocalFileSystem.getInstance().findFileByPath(lib.getClasspath());
    if(root != null) {
      LibraryImpl library = (LibraryImpl)librariesModel.createLibrary(lib.getName());
      LibraryEx.ModifiableModelEx model = library.getModifiableModel();
      model.setKind(HaxeLibraryType.HAXE_LIBRARY);
      model.addRoot(root, OrderRootType.CLASSES);
      model.addRoot(root, OrderRootType.SOURCES);
      model.commit();

      for(Module module:modulesProvider.getModules()) {
        ModifiableRootModel moduleModel = modelsProvider.getModuleModifiableModel(module);
        moduleModel.addLibraryEntry(library);
        moduleModel.commit();
      }
    }
  }

  librariesModel.commit();
}
 
Example #2
Source File: ChooseLibrariesDialogBase.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected void collectChildren(Object element, final List<Object> result) {
  if (element instanceof Application) {
    Collections.addAll(result, ProjectManager.getInstance().getOpenProjects());
    final LibraryTablesRegistrar instance = LibraryTablesRegistrar.getInstance();
    result.add(instance.getLibraryTable()); //1
    result.addAll(instance.getCustomLibraryTables()); //2
  }
  else if (element instanceof Project) {
    Collections.addAll(result, ModuleManager.getInstance((Project)element).getModules());
    result.add(LibraryTablesRegistrar.getInstance().getLibraryTable((Project)element));
  }
  else if (element instanceof LibraryTable) {
    Collections.addAll(result, ((LibraryTable)element).getLibraries());
  }
  else if (element instanceof Module) {
    for (OrderEntry entry : ModuleRootManager.getInstance((Module)element).getOrderEntries()) {
      if (entry instanceof LibraryOrderEntry) {
        final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry)entry;
        if (LibraryTableImplUtil.MODULE_LEVEL.equals(libraryOrderEntry.getLibraryLevel())) {
          final Library library = libraryOrderEntry.getLibrary();
          result.add(library);
        }
      }
    }
  }
}
 
Example #3
Source File: AbstractLibraryManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void updateLibraryContent(@NotNull Set<String> contentUrls) {
  if (!FlutterModuleUtils.declaresFlutter(project)) {
    // If we have a Flutter library, remove it.
    final LibraryTable libraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(project);
    final Library existingLibrary = getLibraryByName(getLibraryName());
    if (existingLibrary != null) {
      WriteAction.compute(() -> {
        final LibraryTable.ModifiableModel libraryTableModel = libraryTable.getModifiableModel();
        libraryTableModel.removeLibrary(existingLibrary);
        libraryTableModel.commit();
        return null;
      });
    }
    return;
  }
  updateLibraryContent(getLibraryName(), contentUrls, null);
}
 
Example #4
Source File: AbstractLibraryManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void updateLibraryContent(@NotNull Set<String> contentUrls) {
  if (!FlutterModuleUtils.declaresFlutter(project)) {
    // If we have a Flutter library, remove it.
    final LibraryTable libraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(project);
    final Library existingLibrary = getLibraryByName(getLibraryName());
    if (existingLibrary != null) {
      WriteAction.compute(() -> {
        final LibraryTable.ModifiableModel libraryTableModel = libraryTable.getModifiableModel();
        libraryTableModel.removeLibrary(existingLibrary);
        libraryTableModel.commit();
        return null;
      });
    }
    return;
  }
  updateLibraryContent(getLibraryName(), contentUrls, null);
}
 
Example #5
Source File: OrderEntryUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static boolean equals(Library library1, Library library2) {
  if (library1 == library2) return true;
  if (library1 == null || library2 == null) return false;

  final LibraryTable table = library1.getTable();
  if (table != null) {
    if (library2.getTable() != table) return false;
    final String name = library1.getName();
    return name != null && name.equals(library2.getName());
  }

  if (library2.getTable() != null) return false;

  for (OrderRootType type : OrderRootType.getAllTypes()) {
    if (!Comparing.equal(library1.getUrls(type), library2.getUrls(type))) {
      return false;
    }
  }
  return true;
}
 
Example #6
Source File: FileOrDirectoryDependencyTabContext.java    From consulo with Apache License 2.0 6 votes vote down vote up
private Library createLibraryFromRoots(ModifiableModuleRootLayer layer, List<OrderRoot> roots, @Nullable final LibraryType libraryType) {
  final LibraryTable.ModifiableModel moduleLibraryModel = layer.getModuleLibraryTable().getModifiableModel();

  final PersistentLibraryKind kind = libraryType == null ? null : libraryType.getKind();
  final Library library = ((LibraryTableBase.ModifiableModelEx)moduleLibraryModel).createLibrary(null, kind);
  final LibraryEx.ModifiableModelEx libModel = (LibraryEx.ModifiableModelEx)library.getModifiableModel();

  for (OrderRoot root : roots) {
    if (root.isJarDirectory()) {
      libModel.addJarDirectory(root.getFile(), false, root.getType());
    }
    else {
      libModel.addRoot(root.getFile(), root.getType());
    }
  }
  libModel.commit();
  return library;
}
 
Example #7
Source File: CamelProjectComponentTestIT.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
public void testRemoveLibrary() throws IOException {
    CamelService service = ServiceManager.getService(myProject, CamelService.class);
    assertEquals(0, service.getLibraries().size());

    VirtualFile camelCoreVirtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(createTestArchive("camel-core-2.22.0.jar"));
    VirtualFile camelSpringVirtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(createTestArchive("camel-spring-2.22.0.jar"));

    final LibraryTable projectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(myProject);

    Library springLibrary = addLibraryToModule(camelSpringVirtualFile, projectLibraryTable, "Maven: org.apache.camel:camel-spring:2.22.0-snapshot");
    Library coreLibrary = addLibraryToModule(camelCoreVirtualFile, projectLibraryTable, "Maven: org.apache.camel:camel-core:2.22.0-snapshot");

    UIUtil.dispatchAllInvocationEvents();
    assertEquals(2, service.getLibraries().size());
    assertEquals(true, service.isCamelPresent());

    ApplicationManager.getApplication().runWriteAction(() -> projectLibraryTable.removeLibrary(springLibrary));

    UIUtil.dispatchAllInvocationEvents();
    assertEquals(1, service.getLibraries().size());
}
 
Example #8
Source File: CamelProjectComponentTestIT.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
public void testAddModule() throws IOException {
    CamelService service = ServiceManager.getService(myProject, CamelService.class);
    assertEquals(0, service.getLibraries().size());

    File camelJar = createTestArchive("camel-core-2.22.0.jar");
    VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(camelJar);

    final LibraryTable projectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(myProject);
    ApplicationManager.getApplication().runWriteAction(() -> {
        final Module moduleA = createModule("myNewModel.iml");
        Library library = projectLibraryTable.createLibrary("Maven: org.apache.camel:camel-core:2.22.0-snapshot");
        final Library.ModifiableModel libraryModifiableModel = library.getModifiableModel();
        libraryModifiableModel.addRoot(virtualFile, OrderRootType.CLASSES);
        libraryModifiableModel.commit();
        ModuleRootModificationUtil.addDependency(moduleA, library);
    });

    UIUtil.dispatchAllInvocationEvents();
    assertEquals(1, service.getLibraries().size());
}
 
Example #9
Source File: LibraryDataService.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void importLibrary(@Nonnull final String libraryName, @Nonnull final Map<OrderRootType, Collection<File>> libraryFiles, @Nonnull final Project project, boolean synchronous) {
  ExternalSystemApiUtil.executeProjectChangeAction(synchronous, new DisposeAwareProjectChange(project) {
    @RequiredUIAccess
    @Override
    public void execute() {
      // Is assumed to be called from the EDT.
      final LibraryTable libraryTable = ProjectLibraryTable.getInstance(project);
      final LibraryTable.ModifiableModel projectLibraryModel = libraryTable.getModifiableModel();
      final Library intellijLibrary;
      try {
        intellijLibrary = projectLibraryModel.createLibrary(libraryName);
      }
      finally {
        projectLibraryModel.commit();
      }
      final Library.ModifiableModel libraryModel = intellijLibrary.getModifiableModel();
      try {
        registerPaths(libraryFiles, libraryModel, libraryName);
      }
      finally {
        libraryModel.commit();
      }
    }
  });
}
 
Example #10
Source File: PackagingElementFactoryImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public List<? extends PackagingElement<?>> createLibraryElements(@Nonnull Library library) {
  final LibraryTable table = library.getTable();
  final String libraryName = library.getName();
  if (table != null) {
    return Collections.singletonList(createLibraryFiles(libraryName, table.getTableLevel(), null));
  }
  if (libraryName != null) {
    final Module module = ((LibraryImpl)library).getModule();
    if (module != null) {
      return Collections.singletonList(createLibraryFiles(libraryName, LibraryTableImplUtil.MODULE_LEVEL, module.getName()));
    }
  }
  final List<PackagingElement<?>> elements = new ArrayList<>();
  for (VirtualFile file : library.getFiles(BinariesOrderRootType.getInstance())) {
    final String path = FileUtil.toSystemIndependentName(PathUtil.getLocalPath(file));
    elements.add(file.isDirectory() && file.isInLocalFileSystem() ? new DirectoryCopyPackagingElement(path) : new FileCopyPackagingElement(path));
  }
  return elements;
}
 
Example #11
Source File: LibrariesContainerFactory.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public Library createLibrary(@Nonnull NewLibraryEditor libraryEditor,
                             @Nonnull LibraryLevel level) {
  if (level == LibraryLevel.MODULE && myRootModel != null) {
    return createLibraryInTable(libraryEditor, myRootModel.getModuleLibraryTable());
  }

  LibraryTablesRegistrar registrar = LibraryTablesRegistrar.getInstance();
  LibraryTable table;
  if (level == LibraryLevel.GLOBAL) {
    table = registrar.getLibraryTable();
  }
  else if (level == LibraryLevel.PROJECT && myProject != null) {
    table = registrar.getLibraryTable(myProject);
  }
  else {
    return null;
  }
  return createLibraryInTable(libraryEditor, table);
}
 
Example #12
Source File: LibraryProjectStructureElement.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void performFix() {
  final LibraryTable.ModifiableModel libraryTable = myContext.getModifiableLibraryTable(myLibrary.getTable());
  if (libraryTable instanceof LibrariesModifiableModel) {
    for (String invalidRoot : myInvalidUrls) {
      final ExistingLibraryEditor libraryEditor = ((LibrariesModifiableModel)libraryTable).getLibraryEditor(myLibrary);
      libraryEditor.removeRoot(invalidRoot, myType);
    }
    myContext.getDaemonAnalyzer().queueUpdate(LibraryProjectStructureElement.this);
    final ProjectStructureConfigurable structureConfigurable = ProjectStructureConfigurable.getInstance(myContext.getProject());
    navigate().doWhenDone(new Runnable() {
      @Override
      public void run() {
        final NamedConfigurable configurable = structureConfigurable.getConfigurableFor(myLibrary).getSelectedConfigurable();
        if (configurable instanceof LibraryConfigurable) {
          ((LibraryConfigurable)configurable).updateComponent();
        }
      }
    });
  }
}
 
Example #13
Source File: HaxelibClasspathUtils.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
/**
 * Loads a classpath from the given library table.  Workhorse for other APIs
 * in this class.
 *
 * @param libraryTable to load
 * @return the classpath
 */
@NotNull
private static HaxeClasspath loadClasspathFrom(LibraryTable libraryTable) {
  HaxeClasspath classpath = new HaxeClasspath();
  Library[] libraries = libraryTable.getLibraries();
  OrderRootType interestingRootTypes[] = {OrderRootType.SOURCES, OrderRootType.CLASSES};
  for (Library library : libraries) {
    for (OrderRootType rootType : interestingRootTypes) {
      for (String url : library.getUrls(rootType)) {
        if (!classpath.containsUrl(url)) {
          classpath.add(new HaxeClasspathEntry(library.getName(), url));
        }
      }
    }
  }
  return classpath;
}
 
Example #14
Source File: EditExistingLibraryDialog.java    From consulo with Apache License 2.0 6 votes vote down vote up
private EditExistingLibraryDialog(Component parent,
                                  LibraryTable.ModifiableModel tableModifiableModel,
                                  @Nullable Project project,
                                  ExistingLibraryEditor libraryEditor,
                                  boolean commitChanges,
                                  LibraryTablePresentation presentation, StructureConfigurableContext context) {
  super(parent, new LibraryRootsComponent(project, libraryEditor));
  setTitle("Configure " + presentation.getDisplayName(false));
  myTableModifiableModel = tableModifiableModel;
  myLibraryEditor = libraryEditor;
  myCommitChanges = commitChanges;
  if (commitChanges) {
    Disposer.register(getDisposable(), libraryEditor);
  }
  context.addLibraryEditorListener(new LibraryEditorListener() {
    @Override
    public void libraryRenamed(@Nonnull Library library, String oldName, String newName) {
      if (library.equals(myLibraryEditor.getLibrary())) {
        myNameField.setText(newName);
      }
    }
  }, getDisposable());
  init();
}
 
Example #15
Source File: CreateNewLibraryDialog.java    From consulo with Apache License 2.0 6 votes vote down vote up
public CreateNewLibraryDialog(@Nonnull JComponent parent, @Nonnull StructureConfigurableContext context, @Nonnull NewLibraryEditor libraryEditor,
                              @Nonnull List<LibraryTable> libraryTables, int selectedTable) {
  super(parent, new LibraryRootsComponent(context.getProject(), libraryEditor));
  myContext = context;
  myLibraryEditor = libraryEditor;
  final DefaultComboBoxModel model = new DefaultComboBoxModel();
  for (LibraryTable table : libraryTables) {
    model.addElement(table);
  }
  myLibraryLevelCombobox = new ComboBox(model);
  myLibraryLevelCombobox.setSelectedIndex(selectedTable);
  myLibraryLevelCombobox.setRenderer(new ListCellRendererWrapper() {
    @Override
    public void customize(JList list, Object value, int index, boolean selected, boolean hasFocus) {
      if (value instanceof LibraryTable) {
        setText(((LibraryTable)value).getPresentation().getDisplayName(false));
      }
    }
  });
  init();
}
 
Example #16
Source File: EditExistingLibraryDialog.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static EditExistingLibraryDialog createDialog(Component parent,
                                                     LibraryTableModifiableModelProvider modelProvider,
                                                     Library library,
                                                     @Nullable Project project,
                                                     LibraryTablePresentation presentation,
                                                     StructureConfigurableContext context) {
  LibraryTable.ModifiableModel modifiableModel = modelProvider.getModifiableModel();
  boolean commitChanges = false;
  ExistingLibraryEditor libraryEditor;
  if (modifiableModel instanceof LibrariesModifiableModel) {
    libraryEditor = ((LibrariesModifiableModel)modifiableModel).getLibraryEditor(library);
  }
  else {
    libraryEditor = new ExistingLibraryEditor(library, context);
    commitChanges = true;
  }
  return new EditExistingLibraryDialog(parent, modifiableModel, project, libraryEditor, commitChanges, presentation, context);
}
 
Example #17
Source File: HaxelibProjectUpdater.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
/**
 * Remove libraries from a library table.
 *
 * @param toRemove - The list of libraries to remove.
 * @param libraryTableModel - The (modifiable model of the) table to remove them from.
 * @param timeLog - Debugging time log.
 */
private void removeLibraries(@NotNull final HaxeLibraryList toRemove,
                             @NotNull final LibraryTable.ModifiableModel libraryTableModel,
                             @NotNull final HaxeDebugTimeLog timeLog) {
  timeLog.stamp("Removing libraries.");
  toRemove.iterate(new HaxeLibraryList.Lambda() {
    @Override
    public boolean processEntry(HaxeLibraryReference entry) {
      Library library = lookupModelLibrary(libraryTableModel, entry);
      if (null != library) {
        // Why use this?: ModuleHelper.removeDependency(rootManager, library);
        libraryTableModel.removeLibrary(library);
        timeLog.stamp("Removed library " + library.getName());
      }
      else {
        LOG.warn(
          "Internal inconsistency: library to remove was not found: " +
          entry.getName());
      }
      return true;
    }
  });
}
 
Example #18
Source File: ClasspathPanelImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
public LibraryTableModifiableModelProvider getModifiableModelProvider(@Nonnull String tableLevel) {
  if (LibraryTableImplUtil.MODULE_LEVEL.equals(tableLevel)) {
    final LibraryTable moduleLibraryTable = getRootModel().getModuleLibraryTable();
    return new LibraryTableModifiableModelProvider() {
      @Override
      public LibraryTable.ModifiableModel getModifiableModel() {
        return moduleLibraryTable.getModifiableModel();
      }
    };
  }
  else {
    return getStructureConfigurableContext().createModifiableModelProvider(tableLevel);
  }
}
 
Example #19
Source File: ArtifactEditorContextImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void selectLibrary(@Nonnull Library library) {
  final LibraryTable table = library.getTable();
  if (table != null) {
    ProjectStructureConfigurable.getInstance(getProject()).selectProjectOrGlobalLibrary(library, true);
  }
  else {
    final Module module = ((LibraryImpl)library).getModule();
    if (module != null) {
      final ModuleRootModel rootModel = myParent.getModulesProvider().getRootModel(module);
      final String libraryName = library.getName();
      for (OrderEntry entry : rootModel.getOrderEntries()) {
        if (entry instanceof ModuleLibraryOrderEntryImpl) {
          final ModuleLibraryOrderEntryImpl libraryEntry = (ModuleLibraryOrderEntryImpl)entry;
          if (libraryName != null && libraryName.equals(libraryEntry.getLibraryName())
             || libraryName == null && library.equals(libraryEntry.getLibrary())) {
            ProjectStructureConfigurable.getInstance(getProject()).selectOrderEntry(module, libraryEntry);
            return;
          }
        }
      }
    }
  }
}
 
Example #20
Source File: QuarkusModuleUtil.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Check if the Quarkus library needs to be recomputed and update it if required.
 *
 * @param module the module to check
 */
public static void ensureQuarkusLibrary(Module module) {
    LOGGER.info("Ensuring library to " + module.getName());
    long start = System.currentTimeMillis();
    ToolDelegate toolDelegate = ToolDelegate.getDelegate(module);
    if (toolDelegate != null) {
        LOGGER.info("Tool delegate found for " + module.getName());
        if (isQuarkusModule(module)) {
            LOGGER.info("isQuarkus module " + module.getName());
            Integer previousHash = module.getComponent(QuarkusModuleComponent.class).getHash();
            Integer actualHash = computeHash(module);
            if (actualHash != null && !actualHash.equals(previousHash)) {
                ModuleRootModificationUtil.updateModel(module, model -> {
                    LibraryTable table = model.getModuleLibraryTable();
                    Library library = table.getLibraryByName(QuarkusConstants.QUARKUS_DEPLOYMENT_LIBRARY_NAME);
                    while (library != null) {
                        table.removeLibrary(library);
                        library = table.getLibraryByName(QuarkusConstants.QUARKUS_DEPLOYMENT_LIBRARY_NAME);
                    }
                    List<VirtualFile>[] files = toolDelegate.getDeploymentFiles(module);
                    LOGGER.info("Adding library to " + module.getName() + " previousHash=" + previousHash + " newHash=" + actualHash);
                    addLibrary(model, files);
                });
                module.getComponent(QuarkusModuleComponent.class).setHash(actualHash);
            }
        }
    }
    LOGGER.info("ensureQuarkusLibrary ran in " + (System.currentTimeMillis() - start));
}
 
Example #21
Source File: LibraryOrderEntryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void searchForLibrary(@Nonnull String name, @Nonnull String level) {
  if (myLibrary != null) return;
  final LibraryTable libraryTable = LibraryTablesRegistrar.getInstance().getLibraryTableByLevel(level, getRootModel().getModule().getProject());
  final Library library = libraryTable != null ? libraryTable.getLibraryByName(name) : null;
  if (library == null) {
    myLibraryName = name;
    myLibraryLevel = level;
    myLibrary = null;
  }
  else {
    myLibraryName = null;
    myLibraryLevel = null;
    myLibrary = library;
  }
}
 
Example #22
Source File: StructureConfigurableContext.java    From consulo with Apache License 2.0 5 votes vote down vote up
public LibraryTable.ModifiableModel getModifiableLibraryTable(@Nonnull LibraryTable table) {
  final String tableLevel = table.getTableLevel();
  if (tableLevel.equals(LibraryTableImplUtil.MODULE_LEVEL)) {
    return table.getModifiableModel();
  }
  return myLevel2Providers.get(tableLevel);
}
 
Example #23
Source File: ChooseLibrariesDialogBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public NodeDescriptor createDescriptor(Object element, NodeDescriptor parentDescriptor) {
  if (element instanceof Application) return new RootDescriptor(myProject);
  if (element instanceof Project) return new ProjectDescriptor(myProject, (Project)element);
  if (element instanceof Module) return new ModuleDescriptor(myProject, parentDescriptor, (Module)element);
  if (element instanceof LibraryTable) {
    final LibraryTable libraryTable = (LibraryTable)element;
    return new LibraryTableDescriptor(myProject, parentDescriptor, libraryTable,
                                      getLibraryTableWeight(libraryTable),
                                      isAutoExpandLibraryTable(libraryTable));
  }
  if (element instanceof Library) return createLibraryDescriptor(parentDescriptor, (Library)element);
  throw new AssertionError();
}
 
Example #24
Source File: BaseStructureConfigurableNoDaemon.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean canObjectBeRemoved(Object editableObject) {
  if (editableObject instanceof Sdk || editableObject instanceof Module || editableObject instanceof Artifact) {
    return true;
  }
  if (editableObject instanceof Library) {
    final LibraryTable table = ((Library)editableObject).getTable();
    return table == null || table.isEditable();
  }
  return false;
}
 
Example #25
Source File: BaseLibrariesConfigurable.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void createLibraryNode(Library library) {
  final LibraryTable table = library.getTable();
  if (table != null) {
    final String level = table.getTableLevel();
    final LibraryConfigurable configurable =
      new LibraryConfigurable(myContext.createModifiableModelProvider(level), library, myContext, TREE_UPDATER);
    final MyNode node = new MyNode(configurable);
    addNode(node, myRoot);
    final ProjectStructureDaemonAnalyzer daemonAnalyzer = myContext.getDaemonAnalyzer();
    daemonAnalyzer.queueUpdate(new LibraryProjectStructureElement(myContext, library));
    daemonAnalyzer.queueUpdateForAllElementsWithErrors();
  }
}
 
Example #26
Source File: AbstractExternalModuleImportProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * The whole import sequence looks like below:
 * <p>
 * <pre>
 * <ol>
 *   <li>Get project view from the gradle tooling api without resolving dependencies (downloading libraries);</li>
 *   <li>Allow to adjust project settings before importing;</li>
 *   <li>Create IJ project and modules;</li>
 *   <li>Ask gradle tooling api to resolve library dependencies (download the if necessary);</li>
 *   <li>Configure libraries used by the gradle project at intellij;</li>
 *   <li>Configure library dependencies;</li>
 * </ol>
 * </pre>
 * <p>
 *
 * @param projectWithResolvedLibraries gradle project with resolved libraries (libraries have already been downloaded and
 *                                     are available at file system under gradle service directory)
 * @param project                      current intellij project which should be configured by libraries and module library
 *                                     dependencies information available at the given gradle project
 */
private void setupLibraries(@Nonnull final DataNode<ProjectData> projectWithResolvedLibraries, final Project project) {
  ExternalSystemApiUtil.executeProjectChangeAction(new DisposeAwareProjectChange(project) {
    @RequiredUIAccess
    @Override
    public void execute() {
      ProjectRootManagerEx.getInstanceEx(project).mergeRootsChangesDuring(new Runnable() {
        @Override
        public void run() {
          if (ExternalSystemApiUtil.isNewProjectConstruction()) {
            // Clean existing libraries (if any).
            LibraryTable projectLibraryTable = ProjectLibraryTable.getInstance(project);
            if (projectLibraryTable == null) {
              LOG.warn("Can't resolve external dependencies of the target gradle project (" + project + "). Reason: project " + "library table is undefined");
              return;
            }
            LibraryTable.ModifiableModel model = projectLibraryTable.getModifiableModel();
            try {
              for (Library library : model.getLibraries()) {
                model.removeLibrary(library);
              }
            }
            finally {
              model.commit();
            }
          }

          // Register libraries.
          myProjectDataManager.importData(Collections.<DataNode<?>>singletonList(projectWithResolvedLibraries), project, false);
        }
      });
    }
  });
}
 
Example #27
Source File: LibraryDependencyDataService.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void importMissingProjectLibraries(@Nonnull Module module, @Nonnull Collection<DataNode<LibraryDependencyData>> nodesToImport, boolean synchronous) {
  LibraryTable libraryTable = ProjectLibraryTable.getInstance(module.getProject());
  List<DataNode<LibraryData>> librariesToImport = ContainerUtilRt.newArrayList();
  for (DataNode<LibraryDependencyData> dataNode : nodesToImport) {
    final LibraryDependencyData dependencyData = dataNode.getData();
    if (dependencyData.getLevel() != LibraryLevel.PROJECT) {
      continue;
    }
    final Library library = libraryTable.getLibraryByName(dependencyData.getInternalName());
    if (library == null) {
      DataNode<ProjectData> projectNode = dataNode.getDataNode(ProjectKeys.PROJECT);
      if (projectNode != null) {
        DataNode<LibraryData> libraryNode = ExternalSystemApiUtil.find(projectNode, ProjectKeys.LIBRARY, new BooleanFunction<DataNode<LibraryData>>() {
          @Override
          public boolean fun(DataNode<LibraryData> node) {
            return node.getData().equals(dependencyData.getTarget());
          }
        });
        if (libraryNode != null) {
          librariesToImport.add(libraryNode);
        }
      }
    }
  }
  if (!librariesToImport.isEmpty()) {
    LibraryDataService.getInstance().importData(librariesToImport, module.getProject(), synchronous);
  }
}
 
Example #28
Source File: LibraryElementPresentation.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static String getLibraryTableComment(final Library library) {
  LibraryTable libraryTable = library.getTable();
  String displayName;
  if (libraryTable != null) {
    displayName = libraryTable.getPresentation().getDisplayName(false);
  }
  else {
    Module module = ((LibraryImpl)library).getModule();
    String tableName = getLibraryTableDisplayName(library);
    displayName = module != null ? "'" + module.getName() + "' " + tableName : tableName;
  }
  return " (" + displayName + ")";
}
 
Example #29
Source File: ProjectStructureHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static Library findIdeLibrary(@Nonnull final LibraryData libraryData, @Nonnull Project ideProject) {
  final LibraryTable libraryTable = ProjectLibraryTable.getInstance(ideProject);
  for (Library ideLibrary : libraryTable.getLibraries()) {
    if (ExternalSystemApiUtil.isRelated(ideLibrary, libraryData)) return ideLibrary;
  }
  return null;
}
 
Example #30
Source File: LibraryDataService.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void removeData(@Nonnull final Collection<? extends Library> libraries, @Nonnull final Project project, boolean synchronous) {
  if (libraries.isEmpty()) {
    return;
  }
  ExternalSystemApiUtil.executeProjectChangeAction(synchronous, new DisposeAwareProjectChange(project) {
    @RequiredUIAccess
    @Override
    public void execute() {
      final LibraryTable libraryTable = ProjectLibraryTable.getInstance(project);
      final LibraryTable.ModifiableModel model = libraryTable.getModifiableModel();
      try {
        for (Library library : libraries) {
          String libraryName = library.getName();
          if (libraryName != null) {
            Library libraryToRemove = model.getLibraryByName(libraryName);
            if (libraryToRemove != null) {
              model.removeLibrary(libraryToRemove);
            }
          }
        }
      }
      finally {
        model.commit();
      }
    }
  });
}