library downloading setting extracted to a separate class

This commit is contained in:
nik
2010-10-06 10:10:06 +04:00
parent 5d1826c83a
commit b89623cb75
6 changed files with 164 additions and 97 deletions
@@ -25,6 +25,7 @@ import com.intellij.ui.CheckBoxList;
import com.intellij.ui.CollectionListModel;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
@@ -35,7 +36,6 @@ import java.util.List;
* @author Dmitry Avdeev
*/
public class DownloadingOptionsDialog extends DialogWrapper {
private JPanel myPanel;
private CheckBoxList myFilesList;
private TextFieldWithBrowseButton myDirectoryField;
@@ -44,16 +44,16 @@ public class DownloadingOptionsDialog extends DialogWrapper {
private JLabel myCopyDownloadedFilesToLabel;
private JCheckBox myDownloadJavadocsCheckBox;
private JPanel myNameWrappingPanel;
private final LibraryCompositionSettings mySettings;
private final LibraryDownloadSettings mySettings;
private final LibraryNameAndLevelPanel myNameAndLevelPanel;
private LibraryDownloadDescription myDownloadDescription;
protected DownloadingOptionsDialog(Component parent, LibraryCompositionSettings settings) {
protected DownloadingOptionsDialog(@NotNull Component parent, @NotNull LibraryDownloadSettings settings) {
super(parent, true);
setTitle("Downloading Options");
mySettings = settings;
myDownloadDescription = settings.getLibraryDescription().getDownloadDescription();
myDownloadDescription = settings.getDescription();
final List<LibraryDownloadInfo> downloads = myDownloadDescription.getDownloads();
myFilesList.setModel(new CollectionListModel(ContainerUtil.map2Array(downloads, JCheckBox.class, new Function<LibraryDownloadInfo, JCheckBox>() {
@Override
@@ -73,7 +73,7 @@ public class DownloadingOptionsDialog extends DialogWrapper {
myDownloadSourcesCheckBox.setSelected(settings.isDownloadSources());
myDownloadJavadocsCheckBox.setSelected(settings.isDownloadJavadocs());
myNameAndLevelPanel = new LibraryNameAndLevelPanel(settings.getDownloadedLibraryName(), settings.getLibraryLevel());
myNameAndLevelPanel = new LibraryNameAndLevelPanel(settings.getLibraryName(), settings.getLibraryLevel());
myNameWrappingPanel.add(myNameAndLevelPanel.getPanel());
init();
}
@@ -90,7 +90,7 @@ public class DownloadingOptionsDialog extends DialogWrapper {
@Override
protected void doOKAction() {
mySettings.setDownloadedLibraryName(myNameAndLevelPanel.getLibraryName());
mySettings.setLibraryName(myNameAndLevelPanel.getLibraryName());
mySettings.setLibraryLevel(myNameAndLevelPanel.getLibraryLevel());
mySettings.setDirectoryForDownloadedLibrariesPath(myDirectoryField.getText());
@@ -48,7 +48,7 @@ public class EditLibraryDialog extends DialogWrapper {
final boolean newLibrary = libraryEditor instanceof NewLibraryEditor;
setTitle((newLibrary ? "Create" : "Edit") + " Library");
myNameAndLevelPanel = new LibraryNameAndLevelPanel(libraryEditor.getName(), newLibrary ? settings.getLibraryLevel() : null);
myNameAndLevelPanel = new LibraryNameAndLevelPanel(libraryEditor.getName(), newLibrary ? settings.getNewLibraryLevel() : null);
init();
}
@@ -64,7 +64,7 @@ public class EditLibraryDialog extends DialogWrapper {
protected void doOKAction() {
myLibraryEditor.setName(myNameAndLevelPanel.getLibraryName());
if (myLibraryEditor instanceof NewLibraryEditor) {
mySettings.setLibraryLevel(myNameAndLevelPanel.getLibraryLevel());
mySettings.setNewLibraryLevel(myNameAndLevelPanel.getLibraryLevel());
}
super.doOKAction();
}
@@ -15,7 +15,6 @@
*/
package com.intellij.facet.impl.ui.libraries;
import com.intellij.facet.ui.libraries.LibraryDownloadInfo;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.OrderRootType;
@@ -28,7 +27,6 @@ import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContaine
import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainerFactory;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -41,28 +39,22 @@ import java.util.Map;
* @author nik
*/
public class LibraryCompositionSettings implements Disposable {
@NonNls private static final String DEFAULT_LIB_FOLDER = "lib";
private final CustomLibraryDescription myLibraryDescription;
private final String myBaseDirectoryForDownloadedFiles;
private String myDirectoryForDownloadedLibrariesPath;
private boolean myDownloadLibraries = true;
private LibrariesContainer.LibraryLevel myLibraryLevel = LibrariesContainer.LibraryLevel.PROJECT;
private String myDownloadedLibraryName;
private boolean myDownloadSources = true;
private boolean myDownloadJavadocs = true;
private List<LibraryDownloadInfo> mySelectedDownloads;
private final String myBaseDirectoryPath;
private LibrariesContainer.LibraryLevel myNewLibraryLevel = LibrariesContainer.LibraryLevel.PROJECT;
private NewLibraryEditor myNewLibraryEditor;
private Library mySelectedLibrary;
private boolean myDownloadLibraries;
private LibraryDownloadSettings myDownloadSettings;
private Map<Library, ExistingLibraryEditor> myExistingLibraryEditors = new HashMap<Library, ExistingLibraryEditor>();
public LibraryCompositionSettings(final @NotNull CustomLibraryDescription libraryDescription,
final @NotNull String baseDirectoryForDownloadedFiles) {
final @NotNull String baseDirectoryFiles) {
myLibraryDescription = libraryDescription;
myBaseDirectoryForDownloadedFiles = baseDirectoryForDownloadedFiles;
myBaseDirectoryPath = baseDirectoryFiles;
final LibraryDownloadDescription downloadDescription = myLibraryDescription.getDownloadDescription();
if (downloadDescription != null) {
myDownloadedLibraryName = downloadDescription.getDefaultLibraryName();
mySelectedDownloads = downloadDescription.getDownloads();
myDownloadSettings = new LibraryDownloadSettings(downloadDescription, baseDirectoryFiles);
}
}
@@ -81,13 +73,14 @@ public class LibraryCompositionSettings implements Disposable {
return myLibraryDescription;
}
@NotNull
public String getBaseDirectoryForDownloadedFiles() {
return myBaseDirectoryForDownloadedFiles;
@Nullable
public LibraryDownloadSettings getDownloadSettings() {
return myDownloadSettings;
}
public void setDirectoryForDownloadedLibrariesPath(final String directoryForDownloadedLibrariesPath) {
myDirectoryForDownloadedLibrariesPath = directoryForDownloadedLibrariesPath;
@NotNull
public String getBaseDirectoryPath() {
return myBaseDirectoryPath;
}
public void setDownloadLibraries(final boolean downloadLibraries) {
@@ -98,65 +91,36 @@ public class LibraryCompositionSettings implements Disposable {
mySelectedLibrary = library;
}
public void setLibraryLevel(final LibrariesContainer.LibraryLevel libraryLevel) {
myLibraryLevel = libraryLevel;
}
public void setDownloadedLibraryName(final String downloadedLibraryName) {
myDownloadedLibraryName = downloadedLibraryName;
}
public String getDirectoryForDownloadedLibrariesPath() {
if (myDirectoryForDownloadedLibrariesPath == null) {
myDirectoryForDownloadedLibrariesPath = myBaseDirectoryForDownloadedFiles + "/" + DEFAULT_LIB_FOLDER;
}
return myDirectoryForDownloadedLibrariesPath;
}
public List<LibraryDownloadInfo> getSelectedDownloads() {
return mySelectedDownloads;
}
public void setSelectedDownloads(List<LibraryDownloadInfo> selectedDownloads) {
mySelectedDownloads = selectedDownloads;
public void setNewLibraryLevel(final LibrariesContainer.LibraryLevel newLibraryLevel) {
myNewLibraryLevel = newLibraryLevel;
}
public boolean downloadFiles(final @NotNull JComponent parent) {
final LibraryDownloadDescription downloadDescription = myLibraryDescription.getDownloadDescription();
if (myDownloadLibraries && downloadDescription != null) {
LibraryDownloadInfo[] toDownload = mySelectedDownloads.toArray(new LibraryDownloadInfo[mySelectedDownloads.size()]);
LibraryDownloader downloader = new LibraryDownloader(toDownload, null, parent,
getDirectoryForDownloadedLibrariesPath(), myDownloadedLibraryName);
VirtualFile[] files = downloader.download();
if (files.length != toDownload.length) {
return false;
}
myNewLibraryEditor = new NewLibraryEditor();
myNewLibraryEditor.setName(myDownloadedLibraryName);
for (VirtualFile file : files) {
myNewLibraryEditor.addRoot(file, OrderRootType.CLASSES);
if (myDownloadLibraries && myDownloadSettings != null) {
final NewLibraryEditor libraryEditor = myDownloadSettings.download(parent);
if (libraryEditor != null) {
myNewLibraryEditor = libraryEditor;
}
}
return true;
}
@Nullable
private Library createLibrary(final ModifiableRootModel rootModel, @Nullable LibrariesContainer additionalContainer) {
if (myNewLibraryEditor != null) {
VirtualFile[] roots = myNewLibraryEditor.getFiles(OrderRootType.CLASSES);
return LibrariesContainerFactory.createLibrary(additionalContainer, LibrariesContainerFactory.createContainer(rootModel),
myNewLibraryEditor.getName(), myLibraryLevel, roots, VirtualFile.EMPTY_ARRAY);
myNewLibraryEditor.getName(), getLibraryLevel(), roots, VirtualFile.EMPTY_ARRAY);
}
return null;
}
public LibrariesContainer.LibraryLevel getLibraryLevel() {
return myLibraryLevel;
private LibrariesContainer.LibraryLevel getLibraryLevel() {
return myDownloadLibraries ? myDownloadSettings.getLibraryLevel() : myNewLibraryLevel;
}
public String getDownloadedLibraryName() {
return myDownloadedLibraryName;
public LibrariesContainer.LibraryLevel getNewLibraryLevel() {
return myNewLibraryLevel;
}
@Nullable
@@ -177,22 +141,6 @@ public class LibraryCompositionSettings implements Disposable {
return library;
}
public boolean isDownloadSources() {
return myDownloadSources;
}
public void setDownloadSources(boolean downloadSources) {
myDownloadSources = downloadSources;
}
public boolean isDownloadJavadocs() {
return myDownloadJavadocs;
}
public void setDownloadJavadocs(boolean downloadJavadocs) {
myDownloadJavadocs = downloadJavadocs;
}
public void setNewLibraryEditor(NewLibraryEditor libraryEditor) {
myNewLibraryEditor = libraryEditor;
}
@@ -0,0 +1,118 @@
/*
* Copyright 2000-2010 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.facet.impl.ui.libraries;
import com.intellij.facet.ui.libraries.LibraryDownloadInfo;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.ui.configuration.libraries.LibraryDownloadDescription;
import com.intellij.openapi.roots.ui.configuration.libraryEditor.NewLibraryEditor;
import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.List;
/**
* @author nik
*/
public class LibraryDownloadSettings {
@NonNls private static final String DEFAULT_LIB_FOLDER = "lib";
private final LibraryDownloadDescription myDescription;
private String myDirectoryForDownloadedLibrariesPath;
private String myLibraryName;
private boolean myDownloadSources = true;
private boolean myDownloadJavadocs = true;
private LibrariesContainer.LibraryLevel myLibraryLevel = LibrariesContainer.LibraryLevel.PROJECT;
private List<LibraryDownloadInfo> mySelectedDownloads;
public LibraryDownloadSettings(LibraryDownloadDescription description, String baseDirectoryForDownloadedFiles) {
myDescription = description;
myDirectoryForDownloadedLibrariesPath = baseDirectoryForDownloadedFiles + "/" + DEFAULT_LIB_FOLDER;
myLibraryName = description.getDefaultLibraryName();
mySelectedDownloads = description.getDownloads();
}
public LibraryDownloadDescription getDescription() {
return myDescription;
}
public boolean isDownloadJavadocs() {
return myDownloadJavadocs;
}
public void setDownloadJavadocs(boolean downloadJavadocs) {
myDownloadJavadocs = downloadJavadocs;
}
public boolean isDownloadSources() {
return myDownloadSources;
}
public void setDownloadSources(boolean downloadSources) {
myDownloadSources = downloadSources;
}
public String getLibraryName() {
return myLibraryName;
}
public void setLibraryName(String libraryName) {
myLibraryName = libraryName;
}
public String getDirectoryForDownloadedLibrariesPath() {
return myDirectoryForDownloadedLibrariesPath;
}
public void setDirectoryForDownloadedLibrariesPath(String directoryForDownloadedLibrariesPath) {
myDirectoryForDownloadedLibrariesPath = directoryForDownloadedLibrariesPath;
}
public List<LibraryDownloadInfo> getSelectedDownloads() {
return mySelectedDownloads;
}
public void setSelectedDownloads(List<LibraryDownloadInfo> selectedDownloads) {
mySelectedDownloads = selectedDownloads;
}
public LibrariesContainer.LibraryLevel getLibraryLevel() {
return myLibraryLevel;
}
public void setLibraryLevel(LibrariesContainer.LibraryLevel libraryLevel) {
myLibraryLevel = libraryLevel;
}
@Nullable
public NewLibraryEditor download(JComponent parent) {
LibraryDownloadInfo[] toDownload = mySelectedDownloads.toArray(new LibraryDownloadInfo[mySelectedDownloads.size()]);
LibraryDownloader downloader = new LibraryDownloader(toDownload, null, parent, myDirectoryForDownloadedLibrariesPath, myLibraryName);
VirtualFile[] files = downloader.download();
if (files.length != toDownload.length) {
return null;
}
final NewLibraryEditor libraryEditor = new NewLibraryEditor();
libraryEditor.setName(myLibraryName);
for (VirtualFile file : files) {
libraryEditor.addRoot(file, OrderRootType.CLASSES);
}
return libraryEditor;
}
}
@@ -20,7 +20,6 @@ import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.ui.configuration.ProjectStructureDialogCellAppearanceUtils;
import com.intellij.openapi.roots.ui.configuration.libraries.LibraryDownloadDescription;
import com.intellij.openapi.roots.ui.configuration.libraries.NewLibraryConfiguration;
import com.intellij.openapi.roots.ui.configuration.libraryEditor.ExistingLibraryEditor;
import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditor;
@@ -129,7 +128,7 @@ public class LibraryOptionsPanel {
});
myExistingLibraryComboBox.setRenderer(new LibraryListCellRenderer());
boolean canDownload = mySettings.getLibraryDescription().getDownloadDescription() != null;
boolean canDownload = mySettings.getDownloadSettings() != null;
myDownloadRadioButton.setVisible(canDownload);
myButtonEnumModel.setSelected(libraries.isEmpty() && canDownload ? Choice.DOWNLOAD : Choice.USE_LIBRARY);
@@ -155,7 +154,7 @@ public class LibraryOptionsPanel {
private void doConfigure() {
switch (myButtonEnumModel.getSelected()) {
case DOWNLOAD:
new DownloadingOptionsDialog(myPanel, mySettings).show();
new DownloadingOptionsDialog(myPanel, mySettings.getDownloadSettings()).show();
break;
case USE_LIBRARY:
final Object item = myExistingLibraryComboBox.getSelectedItem();
@@ -206,7 +205,7 @@ public class LibraryOptionsPanel {
@Nullable
private VirtualFile getBaseDirectory() {
String path = mySettings.getBaseDirectoryForDownloadedFiles();
String path = mySettings.getBaseDirectoryPath();
VirtualFile dir = LocalFileSystem.getInstance().findFileByPath(path);
if (dir == null) {
path = path.substring(0, path.lastIndexOf('/'));
@@ -233,7 +232,7 @@ public class LibraryOptionsPanel {
final LibraryEditor libraryEditor = (LibraryEditor)item;
message = MessageFormat.format("{0} level library <b>{1}</b>" +
" with {2} file(s) will be created",
mySettings.getLibraryLevel(),
mySettings.getNewLibraryLevel(),
libraryEditor.getName(),
libraryEditor.getFiles(OrderRootType.CLASSES).length);
}
@@ -245,7 +244,7 @@ public class LibraryOptionsPanel {
showConfigurePanel = false;
}
if (!showConfigurePanel && mySettings.getLibraryDescription().getDownloadDescription() != null) {
if (!showConfigurePanel && mySettings.getDownloadSettings() != null) {
//show the longest message on the hidden card to ensure that dialog won't jump if user selects another option
message = getDownloadFilesMessage();
}
@@ -254,8 +253,11 @@ public class LibraryOptionsPanel {
}
private String getDownloadFilesMessage() {
final String downloadPath = mySettings.getDirectoryForDownloadedLibrariesPath();
final String basePath = mySettings.getBaseDirectoryForDownloadedFiles();
final LibraryDownloadSettings downloadSettings = mySettings.getDownloadSettings();
if (downloadSettings == null) return "";
final String downloadPath = downloadSettings.getDirectoryForDownloadedLibrariesPath();
final String basePath = mySettings.getBaseDirectoryPath();
String path;
if (!StringUtil.isEmpty(basePath) && FileUtil.startsWith(downloadPath, basePath)) {
path = FileUtil.getRelativePath(basePath, downloadPath, File.separatorChar);
@@ -263,13 +265,12 @@ public class LibraryOptionsPanel {
else {
path = PathUtil.getFileName(downloadPath);
}
final LibraryDownloadDescription downloadDescription = mySettings.getLibraryDescription().getDownloadDescription();
return MessageFormat.format("{0} jar(s) will be downloaded into <b>{1}</b> directory <br>" +
"{2} library <b>{3}</b> will be created",
downloadDescription.getDownloads().size(),
downloadSettings.getSelectedDownloads().size(),
path,
mySettings.getLibraryLevel(),
mySettings.getDownloadedLibraryName());
downloadSettings.getLibraryLevel(),
downloadSettings.getLibraryName());
}
public LibraryCompositionSettings getSettings() {
@@ -117,7 +117,7 @@ public class FrameworkSupportNode extends CheckedTreeNode {
}
private boolean isObsolete(@NotNull LibraryCompositionSettings settings) {
return !settings.getBaseDirectoryForDownloadedFiles().equals(myBaseDirForLibrariesGetter.compute())
return !settings.getBaseDirectoryPath().equals(myBaseDirForLibrariesGetter.compute())
|| !Comparing.equal(settings.getLibraryDescription(), getOrCreateLibraryDescription());
}