simplify GTDUCollector a bit more
[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.ide.JavaUiBundle;
25 import com.intellij.openapi.application.ApplicationManager;
26 import com.intellij.openapi.application.ModalityState;
27 import com.intellij.openapi.roots.libraries.ui.LibraryEditorComponent;
28 import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditorBase;
29 import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryPropertiesEditorBase;
30 import com.intellij.openapi.roots.ui.configuration.libraryEditor.NewLibraryEditor;
31 import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer;
32 import com.intellij.openapi.vfs.VirtualFile;
33 import com.intellij.util.download.DownloadableFileSetVersions;
34 import org.jetbrains.annotations.NotNull;
35
36 import java.util.List;
37
38 public class DownloadableLibraryPropertiesEditor extends LibraryPropertiesEditorBase<LibraryVersionProperties, DownloadableLibraryType> {
39   private final DownloadableLibraryDescription myDescription;
40   private final DownloadableLibraryType myLibraryType;
41   private String myCurrentVersionString;
42
43   public DownloadableLibraryPropertiesEditor(DownloadableLibraryDescription description,
44                                               LibraryEditorComponent<LibraryVersionProperties> editorComponent,
45                                               DownloadableLibraryType libraryType) {
46     super(editorComponent, libraryType, JavaUiBundle.message("downloadable.library.properties.change.version.title"));
47     myDescription = description;
48     myLibraryType = libraryType;
49     myCurrentVersionString = myEditorComponent.getProperties().getVersionString();
50   }
51
52   @Override
53   protected void edit() {
54     final ModalityState current = ModalityState.current();
55     myDescription.fetchVersions(new DownloadableFileSetVersions.FileSetVersionsCallback<FrameworkLibraryVersion>() {
56       @Override
57       public void onSuccess(@NotNull final List<? extends FrameworkLibraryVersion> versions) {
58         ApplicationManager.getApplication().invokeLater(() -> {
59           String pathForDownloaded = "";
60           final VirtualFile existingRootDirectory = myEditorComponent.getExistingRootDirectory();
61           if (existingRootDirectory != null) {
62             pathForDownloaded = existingRootDirectory.getPath();
63           }
64           else {
65             final VirtualFile baseDir = myEditorComponent.getBaseDirectory();
66             if (baseDir != null) {
67               pathForDownloaded = baseDir.getPath() + "/lib";
68             }
69           }
70           final LibraryDownloadSettings initialSettings = new LibraryDownloadSettings(getCurrentVersion(versions), myLibraryType,
71                                                                                       LibrariesContainer.LibraryLevel.PROJECT,
72                                                                                       pathForDownloaded);
73           final LibraryDownloadSettings settings = DownloadingOptionsDialog.showDialog(getMainPanel(), initialSettings, versions, false);
74           if (settings != null) {
75             final NewLibraryEditor editor = settings.download(getMainPanel(), null);
76             if (editor != null) {
77               final LibraryEditorBase target = (LibraryEditorBase)myEditorComponent.getLibraryEditor();
78               target.removeAllRoots();
79               myEditorComponent.renameLibrary(editor.getName());
80               target.setType(myLibraryType);
81               editor.applyTo(target);
82               myEditorComponent.updateRootsTree();
83               myCurrentVersionString = settings.getVersion().getVersionString();
84               setModified();
85             }
86           }
87         }, current);
88       }
89     });
90   }
91
92   private FrameworkLibraryVersion getCurrentVersion(List<? extends FrameworkLibraryVersion> versions) {
93     for (FrameworkLibraryVersion version : versions) {
94       if (version.getVersionString().equals(myCurrentVersionString)) {
95         return version;
96       }
97     }
98     return versions.get(0);
99   }
100
101   @Override
102   public void apply() {
103     myEditorComponent.getProperties().setVersionString(myCurrentVersionString);
104   }
105 }