71efb179f92d2153dc60869ccb2c6e5df8da0e6a
[idea/community.git] / java / idea-ui / src / com / intellij / framework / library / impl / DownloadableLibraryPropertiesEditor.java
1 /*
2  * Copyright 2000-2011 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.framework.library.impl;
17
18 import com.intellij.facet.impl.ui.libraries.DownloadingOptionsDialog;
19 import com.intellij.facet.impl.ui.libraries.LibraryDownloadSettings;
20 import com.intellij.framework.library.DownloadableLibraryDescription;
21 import com.intellij.framework.library.DownloadableLibraryType;
22 import com.intellij.framework.library.FrameworkLibraryVersion;
23 import com.intellij.framework.library.LibraryVersionProperties;
24 import com.intellij.openapi.application.ApplicationManager;
25 import com.intellij.openapi.application.ModalityState;
26 import com.intellij.openapi.roots.libraries.ui.LibraryEditorComponent;
27 import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditorBase;
28 import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryPropertiesEditorBase;
29 import com.intellij.openapi.roots.ui.configuration.libraryEditor.NewLibraryEditor;
30 import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer;
31 import com.intellij.openapi.vfs.VirtualFile;
32 import com.intellij.util.download.DownloadableFileSetVersions;
33 import org.jetbrains.annotations.NotNull;
34
35 import java.util.List;
36
37 public class DownloadableLibraryPropertiesEditor extends LibraryPropertiesEditorBase<LibraryVersionProperties, DownloadableLibraryType> {
38   private final DownloadableLibraryDescription myDescription;
39   private final DownloadableLibraryType myLibraryType;
40   private String myCurrentVersionString;
41
42   public DownloadableLibraryPropertiesEditor(DownloadableLibraryDescription description,
43                                               LibraryEditorComponent<LibraryVersionProperties> editorComponent,
44                                               DownloadableLibraryType libraryType) {
45     super(editorComponent, libraryType, "Change &Version...");
46     myDescription = description;
47     myLibraryType = libraryType;
48     myCurrentVersionString = myEditorComponent.getProperties().getVersionString();
49   }
50
51   @Override
52   protected void edit() {
53     final ModalityState current = ModalityState.current();
54     myDescription.fetchVersions(new DownloadableFileSetVersions.FileSetVersionsCallback<FrameworkLibraryVersion>() {
55       @Override
56       public void onSuccess(@NotNull final List<? extends FrameworkLibraryVersion> versions) {
57         ApplicationManager.getApplication().invokeLater(() -> {
58           String pathForDownloaded = "";
59           final VirtualFile existingRootDirectory = myEditorComponent.getExistingRootDirectory();
60           if (existingRootDirectory != null) {
61             pathForDownloaded = existingRootDirectory.getPath();
62           }
63           else {
64             final VirtualFile baseDir = myEditorComponent.getBaseDirectory();
65             if (baseDir != null) {
66               pathForDownloaded = baseDir.getPath() + "/lib";
67             }
68           }
69           final LibraryDownloadSettings initialSettings = new LibraryDownloadSettings(getCurrentVersion(versions), myLibraryType,
70                                                                                       LibrariesContainer.LibraryLevel.PROJECT,
71                                                                                       pathForDownloaded);
72           final LibraryDownloadSettings settings = DownloadingOptionsDialog.showDialog(getMainPanel(), initialSettings, versions, false);
73           if (settings != null) {
74             final NewLibraryEditor editor = settings.download(getMainPanel(), null);
75             if (editor != null) {
76               final LibraryEditorBase target = (LibraryEditorBase)myEditorComponent.getLibraryEditor();
77               target.removeAllRoots();
78               myEditorComponent.renameLibrary(editor.getName());
79               target.setType(myLibraryType);
80               editor.applyTo(target);
81               myEditorComponent.updateRootsTree();
82               myCurrentVersionString = settings.getVersion().getVersionString();
83               setModified();
84             }
85           }
86         }, current);
87       }
88     });
89   }
90
91   private FrameworkLibraryVersion getCurrentVersion(List<? extends FrameworkLibraryVersion> versions) {
92     for (FrameworkLibraryVersion version : versions) {
93       if (version.getVersionString().equals(myCurrentVersionString)) {
94         return version;
95       }
96     }
97     return versions.get(0);
98   }
99
100   @Override
101   public void apply() {
102     myEditorComponent.getProperties().setVersionString(myCurrentVersionString);
103   }
104 }