use of com.intellij.openapi.roots.LibraryOrderEntry in project intellij-community by JetBrains.
the class MavenAttachSourcesProvider method getActions.
@Override
@NotNull
public Collection<AttachSourcesAction> getActions(final List<LibraryOrderEntry> orderEntries, final PsiFile psiFile) {
Collection<MavenProject> projects = getMavenProjects(psiFile);
if (projects.isEmpty())
return Collections.emptyList();
if (findArtifacts(projects, orderEntries).isEmpty())
return Collections.emptyList();
return Collections.singleton(new AttachSourcesAction() {
@Override
public String getName() {
return ProjectBundle.message("maven.action.download.sources");
}
@Override
public String getBusyText() {
return ProjectBundle.message("maven.action.download.sources.busy.text");
}
@Override
public ActionCallback perform(List<LibraryOrderEntry> orderEntries) {
// may have been changed by this time...
Collection<MavenProject> mavenProjects = getMavenProjects(psiFile);
if (mavenProjects.isEmpty()) {
return ActionCallback.REJECTED;
}
MavenProjectsManager manager = MavenProjectsManager.getInstance(psiFile.getProject());
Collection<MavenArtifact> artifacts = findArtifacts(mavenProjects, orderEntries);
if (artifacts.isEmpty())
return ActionCallback.REJECTED;
final AsyncResult<MavenArtifactDownloader.DownloadResult> result = new AsyncResult<>();
manager.scheduleArtifactsDownloading(mavenProjects, artifacts, true, false, result);
final ActionCallback resultWrapper = new ActionCallback();
result.doWhenDone(new Consumer<MavenArtifactDownloader.DownloadResult>() {
@Override
public void consume(MavenArtifactDownloader.DownloadResult downloadResult) {
if (!downloadResult.unresolvedSources.isEmpty()) {
final StringBuilder message = new StringBuilder();
message.append("<html>Sources not found for:");
int count = 0;
for (MavenId each : downloadResult.unresolvedSources) {
if (count++ > 5) {
message.append("<br>and more...");
break;
}
message.append("<br>").append(each.getDisplayString());
}
message.append("</html>");
Notifications.Bus.notify(new Notification(MavenUtil.MAVEN_NOTIFICATION_GROUP, "Cannot download sources", message.toString(), NotificationType.WARNING), psiFile.getProject());
}
if (downloadResult.resolvedSources.isEmpty()) {
resultWrapper.setRejected();
} else {
resultWrapper.setDone();
}
}
});
return resultWrapper;
}
});
}
use of com.intellij.openapi.roots.LibraryOrderEntry in project intellij-community by JetBrains.
the class GroovyFacetUtil method tryToSetUpGroovyFacetOnTheFly.
public static boolean tryToSetUpGroovyFacetOnTheFly(final Module module) {
final Project project = module.getProject();
GroovyConfigUtils utils = GroovyConfigUtils.getInstance();
final Library[] libraries = utils.getAllSDKLibraries(project);
if (libraries.length > 0) {
final Library library = libraries[0];
int result = Messages.showOkCancelDialog(GroovyBundle.message("groovy.like.library.found.text", module.getName(), library.getName(), utils.getSDKLibVersion(library)), GroovyBundle.message("groovy.like.library.found"), JetgroovyIcons.Groovy.Groovy_32x32);
if (result == Messages.OK) {
WriteAction.run(() -> {
ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
LibraryOrderEntry entry = model.addLibraryEntry(libraries[0]);
LibrariesUtil.placeEntryToCorrectPlace(model, entry);
model.commit();
});
return true;
}
}
return false;
}
use of com.intellij.openapi.roots.LibraryOrderEntry in project intellij-community by JetBrains.
the class ModuleLibraryTable method createLibrary.
@Override
public Library createLibrary(String name, @Nullable PersistentLibraryKind kind) {
LibraryOrderEntry orderEntry = new ModuleLibraryOrderEntryImpl(name, kind, myRootModel, myProjectRootManager);
myRootModel.addOrderEntry(orderEntry);
return orderEntry.getLibrary();
}
use of com.intellij.openapi.roots.LibraryOrderEntry in project intellij-community by JetBrains.
the class LibraryCompositionSettings method addLibraries.
@Nullable
public Library addLibraries(@NotNull final ModifiableRootModel rootModel, @NotNull final List<Library> addedLibraries, @Nullable final LibrariesContainer librariesContainer) {
Library newLibrary = createLibrary(rootModel, librariesContainer);
if (newLibrary != null) {
addedLibraries.add(newLibrary);
DependencyScope scope = LibraryDependencyScopeSuggester.getDefaultScope(newLibrary);
if (getLibraryLevel() != LibrariesContainer.LibraryLevel.MODULE) {
rootModel.addLibraryEntry(newLibrary).setScope(scope);
} else {
LibraryOrderEntry orderEntry = rootModel.findLibraryOrderEntry(newLibrary);
assert orderEntry != null;
orderEntry.setScope(scope);
}
}
if (mySelectedLibrary != null) {
addedLibraries.add(mySelectedLibrary);
rootModel.addLibraryEntry(mySelectedLibrary).setScope(LibraryDependencyScopeSuggester.getDefaultScope(mySelectedLibrary));
}
if (myLibraryProvider != null) {
Library library = myLibraryProvider.createLibrary(myLibraryDescription.getSuitableLibraryKinds());
addedLibraries.add(library);
rootModel.addLibraryEntry(library).setScope(LibraryDependencyScopeSuggester.getDefaultScope(library));
}
return newLibrary;
}
use of com.intellij.openapi.roots.LibraryOrderEntry in project intellij-community by JetBrains.
the class InternetAttachSourceProvider method getActions.
@NotNull
@Override
public Collection<AttachSourcesAction> getActions(List<LibraryOrderEntry> orderEntries, @Nullable PsiFile psiFile) {
final VirtualFile jar = getJarByPsiFile(psiFile);
if (jar == null)
return Collections.emptyList();
final String jarName = jar.getNameWithoutExtension();
int index = jarName.lastIndexOf('-');
if (index == -1)
return Collections.emptyList();
final String version = jarName.substring(index + 1);
final String artifactId = jarName.substring(0, index);
if (!ARTIFACT_IDENTIFIER.matcher(version).matches() || !ARTIFACT_IDENTIFIER.matcher(artifactId).matches()) {
return Collections.emptyList();
}
final Set<Library> libraries = new HashSet<>();
for (LibraryOrderEntry orderEntry : orderEntries) {
ContainerUtil.addIfNotNull(libraries, orderEntry.getLibrary());
}
if (libraries.isEmpty())
return Collections.emptyList();
final String sourceFileName = jarName + "-sources.jar";
for (Library library : libraries) {
for (VirtualFile file : library.getFiles(OrderRootType.SOURCES)) {
if (file.getPath().contains(sourceFileName)) {
if (isRootInExistingFile(file)) {
// Sources already attached, but source-jar doesn't contain current class.
return Collections.emptyList();
}
}
}
}
final File libSourceDir = getLibrarySourceDir();
final File sourceFile = new File(libSourceDir, sourceFileName);
if (sourceFile.exists()) {
return Collections.singleton(new LightAttachSourcesAction() {
@Override
public String getName() {
return "Attach downloaded source";
}
@Override
public String getBusyText() {
return getName();
}
@Override
public ActionCallback perform(List<LibraryOrderEntry> orderEntriesContainingFile) {
attachSourceJar(sourceFile, libraries);
return ActionCallback.DONE;
}
});
}
return Collections.singleton(new LightAttachSourcesAction() {
@Override
public String getName() {
return "Download...";
}
@Override
public String getBusyText() {
return "Searching...";
}
@Override
public ActionCallback perform(List<LibraryOrderEntry> orderEntriesContainingFile) {
final Task task = new Task.Modal(psiFile.getProject(), "Searching source...", true) {
@Override
public void run(@NotNull final ProgressIndicator indicator) {
String artifactUrl = null;
SourceSearcher[] searchers = { new MavenCentralSourceSearcher(), new SonatypeSourceSearcher() };
for (SourceSearcher searcher : searchers) {
try {
artifactUrl = searcher.findSourceJar(indicator, artifactId, version, jar);
} catch (SourceSearchException e) {
LOG.warn(e);
showMessage("Downloading failed", e.getMessage(), NotificationType.ERROR);
continue;
}
if (artifactUrl != null)
break;
}
if (artifactUrl == null) {
showMessage("Sources not found", "Sources for '" + jarName + ".jar' not found", NotificationType.WARNING);
return;
}
if (!(libSourceDir.isDirectory() || libSourceDir.mkdirs())) {
showMessage("Downloading failed", "Failed to create directory to store sources: " + libSourceDir, NotificationType.ERROR);
return;
}
try {
File tmpDownload = FileUtil.createTempFile(libSourceDir, "download.", ".tmp", false, false);
HttpRequests.request(artifactUrl).saveToFile(tmpDownload, indicator);
if (!sourceFile.exists() && !tmpDownload.renameTo(sourceFile)) {
LOG.warn("Failed to rename file " + tmpDownload + " to " + sourceFileName);
}
} catch (IOException e) {
LOG.warn(e);
showMessage("Downloading failed", "Connection problem. See log for more details.", NotificationType.ERROR);
}
}
@Override
public void onSuccess() {
attachSourceJar(sourceFile, libraries);
}
private void showMessage(String title, String message, NotificationType notificationType) {
new Notification("Source searcher", title, message, notificationType).notify(getProject());
}
};
task.queue();
return ActionCallback.DONE;
}
});
}
Aggregations