downloading libraries for frameworks: added options to download sources and javadocs, support for optional jars

This commit is contained in:
nik
2012-05-28 11:45:41 +04:00
parent 616c8af5c6
commit 39304ca707
14 changed files with 324 additions and 79 deletions
@@ -15,7 +15,7 @@
*/
package com.intellij.facet.impl.ui.libraries;
import com.intellij.util.download.DownloadableFileDescription;
import com.intellij.framework.library.DownloadableLibraryFileDescription;
import com.intellij.framework.library.DownloadableLibraryType;
import com.intellij.framework.library.FrameworkLibraryVersion;
import com.intellij.ide.ui.ListCellRendererWrapper;
@@ -29,6 +29,7 @@ import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.ui.CheckBoxList;
import com.intellij.ui.CheckBoxListListener;
import com.intellij.ui.CollectionListModel;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
@@ -47,6 +48,7 @@ import java.util.List;
* @author Dmitry Avdeev
*/
public class DownloadingOptionsDialog extends DialogWrapper {
private static enum AdditionalDownloadType {SOURCES, DOCUMENTATION}
private static final Logger LOG = Logger.getInstance("#com.intellij.facet.impl.ui.libraries.DownloadingOptionsDialog");
private JPanel myPanel;
private CheckBoxList myFilesList;
@@ -110,32 +112,79 @@ public class DownloadingOptionsDialog extends DialogWrapper {
myCopyDownloadedFilesToLabel.setLabelFor(myDirectoryField);
myDirectoryField.setText(FileUtil.toSystemDependentName(settings.getDirectoryForDownloadedLibrariesPath()));
//todo[nik] show when downloading sources/javadocs will be supported
myDownloadSourcesCheckBox.setVisible(false);
myDownloadJavadocsCheckBox.setVisible(false);
boolean sourcesCheckboxVisible = false;
boolean javadocCheckboxVisible = false;
for (FrameworkLibraryVersion version : versions) {
sourcesCheckboxVisible |= haveAdditionalDownloads(version.getFiles(), AdditionalDownloadType.SOURCES);
javadocCheckboxVisible |= haveAdditionalDownloads(version.getFiles(), AdditionalDownloadType.DOCUMENTATION);
}
myDownloadSourcesCheckBox.setVisible(sourcesCheckboxVisible);
myDownloadJavadocsCheckBox.setVisible(javadocCheckboxVisible);
myFilesList.setCheckBoxListListener(new CheckBoxListListener() {
@Override
public void checkBoxSelectionChanged(int index, boolean value) {
updateSourcesAndJavadocCheckboxes();
}
});
updateSourcesAndJavadocCheckboxes();
myDownloadSourcesCheckBox.setSelected(settings.isDownloadSources());
myDownloadJavadocsCheckBox.setSelected(settings.isDownloadJavaDocs());
init();
}
private void onVersionChanged(final @Nullable List<? extends DownloadableFileDescription> selected) {
private void updateSourcesAndJavadocCheckboxes() {
final FrameworkLibraryVersion version = getSelectedVersion();
boolean sourcesCheckboxEnabled;
boolean javadocCheckboxEnabled;
if (version == null) {
sourcesCheckboxEnabled = javadocCheckboxEnabled = false;
}
else {
final List<DownloadableLibraryFileDescription> descriptions = getSelectedDownloads(version);
sourcesCheckboxEnabled = haveAdditionalDownloads(descriptions, AdditionalDownloadType.SOURCES);
javadocCheckboxEnabled = haveAdditionalDownloads(descriptions, AdditionalDownloadType.DOCUMENTATION);
}
setEnabled(myDownloadSourcesCheckBox, sourcesCheckboxEnabled);
setEnabled(myDownloadJavadocsCheckBox, javadocCheckboxEnabled);
}
private static void setEnabled(final JCheckBox checkBox, final boolean enabled) {
if (!enabled) {
checkBox.setSelected(false);
}
checkBox.setEnabled(enabled);
}
private static boolean haveAdditionalDownloads(final List<? extends DownloadableLibraryFileDescription> descriptions, AdditionalDownloadType type) {
for (DownloadableLibraryFileDescription description : descriptions) {
if (type == AdditionalDownloadType.SOURCES && description.getSourcesDescription() != null
|| type == AdditionalDownloadType.DOCUMENTATION && description.getDocumentationDescription() != null) {
return true;
}
}
return false;
}
private void onVersionChanged(final @Nullable List<? extends DownloadableLibraryFileDescription> selectedFiles) {
final FrameworkLibraryVersion version = getSelectedVersion();
if (Comparing.equal(myLastSelectedVersion, version)) return;
if (version != null) {
final List<? extends DownloadableFileDescription> downloads = version.getFiles();
myFilesList.setModel(new CollectionListModel(
ContainerUtil.map2Array(downloads, JCheckBox.class, new Function<DownloadableFileDescription, JCheckBox>() {
final List<? extends DownloadableLibraryFileDescription> downloads = version.getFiles();
myFilesList.setModel(new CollectionListModel<JCheckBox>(
ContainerUtil.map2Array(downloads, JCheckBox.class, new Function<DownloadableLibraryFileDescription, JCheckBox>() {
@Override
public JCheckBox fun(DownloadableFileDescription description) {
return new JCheckBox(description.getPresentableFileName(), selected == null || selected.contains(description));
public JCheckBox fun(DownloadableLibraryFileDescription description) {
final boolean selected = selectedFiles != null ? selectedFiles.contains(description) : !description.isOptional();
return new JCheckBox(description.getPresentableFileName(), selected);
}
})));
if (myNameAndLevelPanel != null) {
myNameAndLevelPanel.setDefaultName(version.getDefaultLibraryName());
}
}
updateSourcesAndJavadocCheckboxes();
myLastSelectedVersion = version;
}
@@ -168,9 +217,9 @@ public class DownloadingOptionsDialog extends DialogWrapper {
return dialog.createSettings();
}
private List<DownloadableFileDescription> getSelectedDownloads(FrameworkLibraryVersion version) {
List<DownloadableFileDescription> selected = new ArrayList<DownloadableFileDescription>();
List<? extends DownloadableFileDescription> downloads = version.getFiles();
private List<DownloadableLibraryFileDescription> getSelectedDownloads(FrameworkLibraryVersion version) {
List<DownloadableLibraryFileDescription> selected = new ArrayList<DownloadableLibraryFileDescription>();
List<? extends DownloadableLibraryFileDescription> downloads = version.getFiles();
for (int i = 0; i < downloads.size(); i++) {
if (myFilesList.isItemSelected(i)) {
selected.add(downloads.get(i));
@@ -194,7 +243,7 @@ public class DownloadingOptionsDialog extends DialogWrapper {
}
final String path = FileUtil.toSystemIndependentName(myDirectoryField.getText());
List<DownloadableFileDescription> selected = getSelectedDownloads(version);
List<DownloadableLibraryFileDescription> selected = getSelectedDownloads(version);
return new LibraryDownloadSettings(version, myLibraryType, path, libraryName, libraryLevel, selected,
myDownloadSourcesCheckBox.isSelected(), myDownloadJavadocsCheckBox.isSelected());
@@ -15,20 +15,28 @@
*/
package com.intellij.facet.impl.ui.libraries;
import com.intellij.framework.library.DownloadableLibraryFileDescription;
import com.intellij.framework.library.DownloadableLibraryType;
import com.intellij.framework.library.FrameworkLibraryVersion;
import com.intellij.framework.library.LibraryVersionProperties;
import com.intellij.openapi.roots.JavadocOrderRootType;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.ui.configuration.libraryEditor.NewLibraryEditor;
import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.download.DownloadableFileDescription;
import com.intellij.util.download.DownloadableFileService;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author nik
@@ -41,19 +49,19 @@ public class LibraryDownloadSettings {
private final boolean myDownloadSources;
private final boolean myDownloadJavaDocs;
private final LibrariesContainer.LibraryLevel myLibraryLevel;
private final List<? extends DownloadableFileDescription> mySelectedDownloads;
private final List<? extends DownloadableLibraryFileDescription> mySelectedDownloads;
public LibraryDownloadSettings(@NotNull FrameworkLibraryVersion libraryVersion,
@Nullable DownloadableLibraryType libraryType,
final LibrariesContainer.LibraryLevel libraryLevel, final String downloadedLibrariesPath) {
this(libraryVersion, libraryType, downloadedLibrariesPath, libraryVersion.getDefaultLibraryName(), libraryLevel,
libraryVersion.getFiles(), true, true);
getRequiredFiles(libraryVersion.getFiles()), true, true);
}
public LibraryDownloadSettings(@NotNull FrameworkLibraryVersion libraryVersion, @Nullable DownloadableLibraryType libraryType,
@NotNull String directoryForDownloadedLibrariesPath, @NotNull String libraryName,
@NotNull LibrariesContainer.LibraryLevel libraryLevel,
@NotNull List<? extends DownloadableFileDescription> selectedDownloads,
@NotNull List<? extends DownloadableLibraryFileDescription> selectedDownloads,
boolean downloadSources, boolean downloadJavaDocs) {
myVersion = libraryVersion;
myLibraryType = libraryType;
@@ -65,6 +73,15 @@ public class LibraryDownloadSettings {
mySelectedDownloads = selectedDownloads;
}
private static List<? extends DownloadableLibraryFileDescription> getRequiredFiles(List<? extends DownloadableLibraryFileDescription> files) {
return ContainerUtil.filter(files, new Condition<DownloadableLibraryFileDescription>() {
@Override
public boolean value(DownloadableLibraryFileDescription description) {
return !description.isOptional();
}
});
}
@NotNull
public FrameworkLibraryVersion getVersion() {
return myVersion;
@@ -86,7 +103,7 @@ public class LibraryDownloadSettings {
return myDirectoryForDownloadedLibrariesPath;
}
public List<? extends DownloadableFileDescription> getSelectedDownloads() {
public List<? extends DownloadableLibraryFileDescription> getSelectedDownloads() {
return mySelectedDownloads;
}
@@ -109,10 +126,26 @@ public class LibraryDownloadSettings {
@Nullable
public NewLibraryEditor download(JComponent parent) {
VirtualFile[] files = DownloadableFileService.getInstance().createDownloader(mySelectedDownloads, null, parent, myLibraryName + " Library")
final List<DownloadableFileDescription> toDownload = new ArrayList<DownloadableFileDescription>(mySelectedDownloads);
Map<DownloadableFileDescription, OrderRootType> rootTypes = new HashMap<DownloadableFileDescription, OrderRootType>();
for (DownloadableLibraryFileDescription description : mySelectedDownloads) {
final DownloadableFileDescription sources = description.getSourcesDescription();
if (myDownloadSources && sources != null) {
toDownload.add(sources);
rootTypes.put(sources, OrderRootType.SOURCES);
}
final DownloadableFileDescription docs = description.getDocumentationDescription();
if (myDownloadJavaDocs && docs != null) {
toDownload.add(docs);
rootTypes.put(docs, JavadocOrderRootType.getInstance());
}
}
List<Pair<VirtualFile,DownloadableFileDescription>> downloaded =
DownloadableFileService.getInstance().createDownloader(toDownload, null, parent, myLibraryName + " Library")
.toDirectory(myDirectoryForDownloadedLibrariesPath)
.download();
if (files == null) {
.downloadAndReturnWithDescriptions();
if (downloaded == null) {
return null;
}
@@ -124,8 +157,9 @@ public class LibraryDownloadSettings {
libraryEditor = new NewLibraryEditor();
}
libraryEditor.setName(myLibraryName);
for (VirtualFile file : files) {
libraryEditor.addRoot(file, OrderRootType.CLASSES);
for (Pair<VirtualFile, DownloadableFileDescription> pair : downloaded) {
final OrderRootType rootType = rootTypes.containsKey(pair.getSecond()) ? rootTypes.get(pair.getSecond()) : OrderRootType.CLASSES;
libraryEditor.addRoot(pair.getFirst(), rootType);
}
return libraryEditor;
}
@@ -19,16 +19,16 @@ import com.intellij.facet.impl.ui.libraries.RequiredLibrariesInfo;
import com.intellij.facet.ui.libraries.LibraryDownloadInfo;
import com.intellij.facet.ui.libraries.LibraryInfo;
import com.intellij.framework.library.DownloadableLibraryDescription;
import com.intellij.framework.library.DownloadableLibraryFileDescription;
import com.intellij.framework.library.FrameworkLibraryVersion;
import com.intellij.framework.library.impl.DownloadableLibraryDescriptionImpl;
import com.intellij.framework.library.impl.DownloadableLibraryFileDescriptionImpl;
import com.intellij.framework.library.impl.FrameworkLibraryVersionImpl;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryKind;
import com.intellij.openapi.roots.ui.configuration.libraries.CustomLibraryDescription;
import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer;
import com.intellij.util.download.DownloadableFileDescription;
import com.intellij.util.download.impl.DownloadableFileDescriptionImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -53,13 +53,12 @@ public class OldCustomLibraryDescription extends CustomLibraryDescriptionBase {
myVersions = versions;
final List<FrameworkLibraryVersion> libraryVersions = new ArrayList<FrameworkLibraryVersion>();
for (FrameworkVersion version : versions) {
List<DownloadableFileDescription> downloads = new ArrayList<DownloadableFileDescription>();
List<DownloadableLibraryFileDescription> downloads = new ArrayList<DownloadableLibraryFileDescription>();
for (LibraryInfo info : version.getLibraries()) {
final LibraryDownloadInfo downloadingInfo = info.getDownloadingInfo();
if (downloadingInfo != null) {
final DownloadableFileDescription
element = new DownloadableFileDescriptionImpl(downloadingInfo.getDownloadUrl(), downloadingInfo.getFileNamePrefix(), downloadingInfo.getFileNameSuffix());
downloads.add(element);
downloads.add(new DownloadableLibraryFileDescriptionImpl(downloadingInfo.getDownloadUrl(), downloadingInfo.getFileNamePrefix(),
downloadingInfo.getFileNameSuffix(), null, null, false));
}
}
libraryVersions.add(new FrameworkLibraryVersionImpl(version.getVersionName(), downloads, version.getLibraryName()));