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

The following examples show how to use com.intellij.openapi.roots.libraries.Library. 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: BaseLibrariesConfigurable.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(final AnActionEvent e) {
  final Object o = getSelectedObject();
  if (o instanceof LibraryEx) {
    final LibraryEx selected = (LibraryEx)o;
    final String newName = Messages.showInputDialog("Enter library name:", "Copy Library", null, selected.getName() + "2", new NonEmptyInputValidator());
    if (newName == null) return;

    BaseLibrariesConfigurable configurable = BaseLibrariesConfigurable.this;
    final LibraryEx library = (LibraryEx)myContext.getLibrary(selected.getName(), myLevel);
    LOG.assertTrue(library != null);

    final LibrariesModifiableModel libsModel = configurable.getModelProvider().getModifiableModel();
    final Library lib = libsModel.createLibrary(newName, library.getKind());
    final LibraryEx.ModifiableModelEx model = (LibraryEx.ModifiableModelEx)libsModel.getLibraryEditor(lib).getModel();
    LibraryEditingUtil.copyLibrary(library, Collections.<String, String>emptyMap(), model);
  }
}
 
Example #2
Source File: LibraryUsageCollector.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public Set<UsageDescriptor> getProjectUsages(@Nonnull Project project) {
  final Set<LibraryKind> usedKinds = new HashSet<LibraryKind>();
  final Processor<Library> processor = new Processor<Library>() {
    @Override
    public boolean process(Library library) {
      usedKinds.addAll(LibraryPresentationManagerImpl.getLibraryKinds(library, null));
      return true;
    }
  };
  for (Module module : ModuleManager.getInstance(project).getModules()) {
    ModuleRootManager.getInstance(module).orderEntries().librariesOnly().forEachLibrary(processor);
  }

  final HashSet<UsageDescriptor> usageDescriptors = new HashSet<UsageDescriptor>();
  for (LibraryKind kind : usedKinds) {
    usageDescriptors.add(new UsageDescriptor(kind.getKindId(), 1));
  }
  return usageDescriptors;
}
 
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: ProjectLibraryTabContext.java    From consulo with Apache License 2.0 6 votes vote down vote up
public ProjectLibraryTabContext(final ClasspathPanel classpathPanel, StructureConfigurableContext context) {
  super(classpathPanel, context);

  StructureLibraryTableModifiableModelProvider projectLibrariesProvider = context.getProjectLibrariesProvider();
  Library[] libraries = projectLibrariesProvider.getModifiableModel().getLibraries();
  final Condition<Library> condition = LibraryEditingUtil.getNotAddedLibrariesCondition(myClasspathPanel.getRootModel());

  myItems = ContainerUtil.filter(libraries, condition);
  ContainerUtil.sort(myItems, new Comparator<Library>() {
    @Override
    public int compare(Library o1, Library o2) {
      return StringUtil.compare(o1.getName(), o2.getName(), false);
    }
  });

  myLibraryList = new JBList(myItems);
  myLibraryList.setCellRenderer(new ColoredListCellRendererWrapper<Library>() {
    @Override
    protected void doCustomize(JList list, Library value, int index, boolean selected, boolean hasFocus) {
      final CellAppearanceEx appearance = OrderEntryAppearanceService.getInstance().forLibrary(classpathPanel.getProject(), value, false);

      appearance.customize(this);
    }
  });
  new ListSpeedSearch(myLibraryList);
}
 
Example #5
Source File: IdeaUtils.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a URLClassLoader for a given library or libraries
 *
 * @param libraries the library or libraries
 * @return the classloader
 */
public @Nullable URLClassLoader newURLClassLoaderForLibrary(Library... libraries) throws MalformedURLException {
    List<URL> urls = new ArrayList<>();
    for (Library library : libraries) {
        if (library != null) {
            VirtualFile[] files = library.getFiles(OrderRootType.CLASSES);
            if (files.length == 1) {
                VirtualFile vf = files[0];
                if (vf.getName().toLowerCase().endsWith(".jar")) {
                    String path = vf.getPath();
                    if (path.endsWith("!/")) {
                        path = path.substring(0, path.length() - 2);
                    }
                    URL url = new URL("file:" + path);
                    urls.add(url);
                }
            }
        }
    }
    if (urls.isEmpty()) {
        return null;
    }

    URL[] array = urls.toArray(new URL[urls.size()]);
    return new URLClassLoader(array);
}
 
Example #6
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 #7
Source File: DescribeLibraryAction.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Override
protected void actionPerformedInBlazeProject(Project project, AnActionEvent e) {
  BlazeProjectData blazeProjectData =
      BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
  if (blazeProjectData == null) {
    return;
  }
  Library library = LibraryActionHelper.findLibraryForAction(e);
  if (library == null) {
    return;
  }
  BlazeJarLibrary blazeLibrary =
      LibraryActionHelper.findLibraryFromIntellijLibrary(project, blazeProjectData, library);
  if (blazeLibrary == null) {
    Messages.showErrorDialog(
        project, "Could not find this library in the project.", CommonBundle.getErrorTitle());
    return;
  }
  showLibraryDescription(project, blazeLibrary);
}
 
Example #8
Source File: ModuleStructureConfigurable.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void addLibraryOrderEntry(final Module module, final Library library) {
  Component parent = TargetAWT.to(WindowManager.getInstance().suggestParentWindow(module.getProject()));

  final ModuleEditor moduleEditor = myContext.myModulesConfigurator.getModuleEditor(module);
  LOG.assertTrue(moduleEditor != null, "Current module editor was not initialized");
  final ModifiableRootModel modelProxy = moduleEditor.getModifiableRootModelProxy();
  final OrderEntry[] entries = modelProxy.getOrderEntries();
  for (OrderEntry entry : entries) {
    if (entry instanceof LibraryOrderEntry && Comparing.strEqual(entry.getPresentableName(), library.getName())) {
      if (Messages.showYesNoDialog(parent,
                                   ProjectBundle.message("project.roots.replace.library.entry.message", entry.getPresentableName()),
                                   ProjectBundle.message("project.roots.replace.library.entry.title"),
                                   Messages.getInformationIcon()) == Messages.YES) {
        modelProxy.removeOrderEntry(entry);
        break;
      }
    }
  }
  modelProxy.addLibraryEntry(library);
  myContext.getDaemonAnalyzer().queueUpdate(new ModuleProjectStructureElement(myContext, module));
  myTree.repaint();
}
 
Example #9
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 #10
Source File: BlazeJavascriptLibrarySource.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Predicate<Library> getGcRetentionFilter() {
  if (JS_LIBRARY_KIND == null) {
    return null;
  }
  return BlazeJavascriptLibrarySource::isJavascriptLibrary;
}
 
Example #11
Source File: LibrariesModifiableModel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Library createLibrary(String name, @Nullable PersistentLibraryKind type) {
  final Library library = ((LibraryTableBase.ModifiableModelEx)getLibrariesModifiableModel()).createLibrary(name, type);
  //createLibraryEditor(library);                     \
  final BaseLibrariesConfigurable configurable = ProjectStructureConfigurable.getInstance(myProject).getConfigurableFor(library);
  configurable.createLibraryNode(library);
  return library;
}
 
Example #12
Source File: LibraryElementType.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public List<? extends LibraryPackagingElement> chooseAndCreate(@Nonnull ArtifactEditorContext context, @Nonnull Artifact artifact,
                                                               @Nonnull CompositePackagingElement<?> parent) {
  final List<Library> selected = context.chooseLibraries(ProjectBundle.message("dialog.title.packaging.choose.library"));
  final List<LibraryPackagingElement> elements = new ArrayList<LibraryPackagingElement>();
  for (Library library : selected) {
    elements.add(new LibraryPackagingElement(library.getTable().getTableLevel(), library.getName(), null));
  }
  return elements;
}
 
Example #13
Source File: LibraryConfigurable.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected LibraryConfigurable(final StructureLibraryTableModifiableModelProvider modelProvider,
                              final Library library,
                              final StructureConfigurableContext context,
                              final Runnable updateTree) {
  super(true, updateTree);
  myModel = modelProvider;
  myContext = context;
  myProject = context.getProject();
  myLibrary = library;
  myProjectStructureElement = new LibraryProjectStructureElement(context, myLibrary);
}
 
Example #14
Source File: AddLibraryTargetDirectoryToProjectViewAction.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
protected void actionPerformedInBlazeProject(Project project, AnActionEvent e) {
  Library library = LibraryActionHelper.findLibraryForAction(e);
  if (library != null) {
    addDirectoriesToProjectView(project, ImmutableList.of(library));
  }
}
 
Example #15
Source File: SourceJarGenerator.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public ActionCallback perform(List<LibraryOrderEntry> orderEntriesContainingFile) {
  Collection<Library> libraries = orderEntriesContainingFile.stream()
    .map(LibraryOrderEntry::getLibrary)
    .filter(Objects::nonNull)
    .collect(Collectors.toSet());

  if (libraries.isEmpty()) return ActionCallback.REJECTED;
  return perform(libraries);
}
 
Example #16
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 #17
Source File: HaxeLibrary.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
/**
 * Test whether this library is effectively the same as a Library appearing
 * in IDEA's library tables.
 *
 * @param lib - Library to test.
 * @return true if this library uses the same sources as the IDEA library; false otherwise.
 */
public boolean matchesIdeaLib(Library lib) {
  if (null == lib) {
    return false;
  }

  HaxeClasspath cp = getClasspathEntries();
  VirtualFile[] sources = lib.getFiles(OrderRootType.SOURCES);
  for (VirtualFile file : sources) {
    if (!cp.containsUrl(file.getUrl())) {
      return false;
    }
  }
  return cp.size() == sources.length;
}
 
Example #18
Source File: ChooseLibrariesDialogBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Object getParentElement(Object element) {
  if (element instanceof Application) return null;
  if (element instanceof Project) return ApplicationManager.getApplication();
  if (element instanceof Module) return ((Module)element).getProject();
  if (element instanceof LibraryTable) return myParentsMap.get(element);
  if (element instanceof Library) return myParentsMap.get(element);
  throw new AssertionError();
}
 
Example #19
Source File: LibraryOrderEntryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public Library getLibrary() {
  Library library = getRootModel().getConfigurationAccessor().getLibrary(myLibrary, myLibraryName, myLibraryLevel);
  if (library != null) { //library was not deleted
    return library;
  }
  if (myLibrary != null) {
    myLibraryName = myLibrary.getName();
    myLibraryLevel = myLibrary.getTable().getTableLevel();
  }
  myLibrary = null;
  return null;
}
 
Example #20
Source File: LibrariesContainerFactory.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static Library createLibraryInTable(final @Nonnull NewLibraryEditor editor, final LibraryTable table) {
  LibraryTableBase.ModifiableModelEx modifiableModel = (LibraryTableBase.ModifiableModelEx) table.getModifiableModel();
  final String name = StringUtil.isEmpty(editor.getName()) ? null : getUniqueLibraryName(editor.getName(), modifiableModel);
  final LibraryType<?> type = editor.getType();
  Library library = modifiableModel.createLibrary(name, type == null ? null : type.getKind());
  final LibraryEx.ModifiableModelEx model = (LibraryEx.ModifiableModelEx)library.getModifiableModel();
  editor.applyTo(model);
  model.commit();
  modifiableModel.commit();
  return library;
}
 
Example #21
Source File: LibrariesContainerFactory.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Library createLibrary(@Nonnull @NonNls String name,
                             @Nonnull LibraryLevel level,
                             @Nonnull VirtualFile[] classRoots,
                             @Nonnull VirtualFile[] sourceRoots) {
  NewLibraryEditor editor = new NewLibraryEditor();
  editor.setName(name);
  for (VirtualFile classRoot : classRoots) {
    editor.addRoot(classRoot, BinariesOrderRootType.getInstance());
  }
  for (VirtualFile sourceRoot : sourceRoots) {
    editor.addRoot(sourceRoot, SourcesOrderRootType.getInstance());
  }
  return createLibrary(editor, level);
}
 
Example #22
Source File: LibraryOrderEntryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public LibraryOrderEntryImpl(@Nonnull Library library, @Nonnull ModuleRootLayerImpl rootLayer) {
  super(LibraryOrderEntryType.getInstance(), rootLayer, ProjectRootManagerImpl.getInstanceImpl(rootLayer.getProject()));
  LOGGER.assertTrue(library.getTable() != null);
  myLibrary = library;
  addListeners();
  init();
}
 
Example #23
Source File: StructureConfigurableContext.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static Library findLibraryModel(final @Nonnull String libraryName, @Nonnull LibrariesModifiableModel model) {
  for (Library library : model.getLibraries()) {
    final Library libraryModel = findLibraryModel(library, model);
    if (libraryModel != null && libraryName.equals(libraryModel.getName())) {
      return libraryModel;
    }
  }
  return null;
}
 
Example #24
Source File: LibrariesContainerFactory.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public String suggestUniqueLibraryName(@Nonnull String baseName) {
  if (myNameGenerator == null) {
    myNameGenerator = new UniqueNameGenerator(Arrays.asList(getAllLibraries()), new Function<Library, String>() {
      @Override
      public String fun(Library o) {
        return o.getName();
      }
    });
  }
  return myNameGenerator.generateUniqueName(baseName, "", "", " (", ")");
}
 
Example #25
Source File: ModuleEditor.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(Object object, Method method, Object[] params) throws Throwable {
  final boolean needUpdate = myCheckedNames.contains(method.getName());
  try {
    final Object result = method.invoke(myDelegateTable, unwrapParams(params));
    if (result instanceof Library) {
      return Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{result instanceof LibraryEx ? LibraryEx.class : Library.class},
                                    new LibraryInvocationHandler((Library)result));
    }
    else if (result instanceof LibraryTable.ModifiableModel) {
      return Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{LibraryTableBase.ModifiableModelEx.class},
                                    new LibraryTableModelInvocationHandler((LibraryTable.ModifiableModel)result));
    }
    if (result instanceof Library[]) {
      Library[] libraries = (Library[])result;
      for (int idx = 0; idx < libraries.length; idx++) {
        Library library = libraries[idx];
        libraries[idx] =
        (Library)Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{library instanceof LibraryEx ? LibraryEx.class : Library.class},
                                        new LibraryInvocationHandler(library));
      }
    }
    return result;
  }
  catch (InvocationTargetException e) {
    throw e.getCause();
  }
  finally {
    if (needUpdate) {
      fireModuleStateChanged();
    }
  }
}
 
Example #26
Source File: DWFrameworkSupportConfigurable.java    From intellij-demandware with MIT License 5 votes vote down vote up
@Override
public void addSupport(@NotNull Module module, @NotNull ModifiableRootModel modifiableRootModel, @Nullable Library library) {
    final FacetManager facetManager = FacetManager.getInstance(module);
    ModifiableFacetModel facetModel = facetManager.createModifiableModel();
    DWSettingsProvider dwSettingsProvider = ModuleServiceManager.getService(module, DWSettingsProvider.class);
    dwSettingsProvider.setPasswordKey(UUID.randomUUID().toString());
    dwSettingsProvider.setHostname(dwFrameworkSupportConfigurablePanel.getHostname());
    dwSettingsProvider.setUsername(dwFrameworkSupportConfigurablePanel.getUsername());
    dwSettingsProvider.setPassword(dwFrameworkSupportConfigurablePanel.getPassword());
    dwSettingsProvider.setVersion(dwFrameworkSupportConfigurablePanel.getVersion());
    dwSettingsProvider.setAutoUploadEnabled(dwFrameworkSupportConfigurablePanel.getAutoUploadEnabled());
    Facet facet = FacetManager.getInstance(modifiableRootModel.getModule()).addFacet(DWFacetType.INSTANCE, "Demandware", null);
    facetModel.addFacet(facet);
    facetModel.commit();
}
 
Example #27
Source File: FlutterSdk.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns the Flutter SDK for a project that has a possibly broken "Dart SDK" project library.
 * <p>
 * (This can happen for a newly-cloned Flutter SDK where the Dart SDK is not cached yet.)
 */
@Nullable
public static FlutterSdk getIncomplete(@NotNull final Project project) {
  if (project.isDisposed()) {
    return null;
  }
  final Library lib = getDartSdkLibrary(project);
  if (lib == null) {
    return null;
  }
  return getFlutterFromDartSdkLibrary(lib);
}
 
Example #28
Source File: CreateNewLibraryAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
  Library library =
    createLibrary(myType, myLibrariesConfigurable.getTree(), myProject, myLibrariesConfigurable.getModelProvider().getModifiableModel());
  if (library == null) return;

  final BaseLibrariesConfigurable rootConfigurable = ProjectStructureConfigurable.getInstance(myProject).getConfigurableFor(library);
  final DefaultMutableTreeNode libraryNode =
    MasterDetailsComponent.findNodeByObject((TreeNode)rootConfigurable.getTree().getModel().getRoot(), library);
  rootConfigurable.selectNodeInTree(libraryNode);
  LibraryEditingUtil.showDialogAndAddLibraryToDependencies(library, myProject, true);
}
 
Example #29
Source File: LibrariesContainerFactory.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public ExistingLibraryEditor getLibraryEditor(@Nonnull Library library) {
  final LibraryTable table = library.getTable();
  if (table == null) return null;

  final LibraryTable.ModifiableModel model = myContext.getModifiableLibraryTable(table);
  if (model instanceof LibrariesModifiableModel) {
    return ((LibrariesModifiableModel)model).getLibraryEditor(library);
  }
  return null;
}
 
Example #30
Source File: LibraryTableBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Library createLibrary(String name, @javax.annotation.Nullable PersistentLibraryKind kind) {
  assertWritable();
  final LibraryImpl library = new LibraryImpl(name, kind, LibraryTableBase.this, null);
  myLibraries.add(library);
  return library;
}