2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com
.intellij
.facet
.impl
.ui
.libraries
;
18 import com
.intellij
.facet
.ui
.libraries
.LibraryDownloadInfo
;
19 import com
.intellij
.facet
.ui
.libraries
.LibraryInfo
;
20 import com
.intellij
.facet
.ui
.libraries
.RemoteRepositoryInfo
;
21 import com
.intellij
.ide
.IdeBundle
;
22 import com
.intellij
.openapi
.fileChooser
.FileChooser
;
23 import com
.intellij
.openapi
.fileChooser
.FileChooserDescriptor
;
24 import com
.intellij
.openapi
.fileChooser
.FileChooserDescriptorFactory
;
25 import com
.intellij
.openapi
.project
.ProjectBundle
;
26 import com
.intellij
.openapi
.roots
.OrderRootType
;
27 import com
.intellij
.openapi
.roots
.libraries
.Library
;
28 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.LibrariesContainer
;
29 import com
.intellij
.openapi
.ui
.TextFieldWithBrowseButton
;
30 import com
.intellij
.openapi
.ui
.VerticalFlowLayout
;
31 import com
.intellij
.openapi
.util
.MutualMap
;
32 import com
.intellij
.openapi
.util
.io
.FileUtil
;
33 import com
.intellij
.openapi
.vfs
.LocalFileSystem
;
34 import com
.intellij
.openapi
.vfs
.VfsUtil
;
35 import com
.intellij
.openapi
.vfs
.VirtualFile
;
36 import com
.intellij
.ui
.IdeBorderFactory
;
37 import com
.intellij
.util
.ui
.UIUtil
;
38 import org
.jetbrains
.annotations
.Nullable
;
39 import org
.jetbrains
.annotations
.NotNull
;
43 import java
.awt
.event
.ActionEvent
;
44 import java
.awt
.event
.ActionListener
;
46 import java
.util
.List
;
51 public class LibraryCompositionOptionsPanel
{
52 private final MutualMap
<LibrariesContainer
.LibraryLevel
, String
> myLibraryLevels
= new MutualMap
<LibrariesContainer
.LibraryLevel
, String
>(true);
53 private JPanel myMainPanel
;
54 private JButton myAddLibraryButton
;
55 private JButton myAddJarsButton
;
56 private JCheckBox myDownloadMissingJarsCheckBox
;
57 private TextFieldWithBrowseButton myDirectoryField
;
58 private JComboBox myLibraryLevelComboBox
;
59 private JTextField myLibraryNameField
;
60 private JPanel myMissingLibrariesPanel
;
61 private JPanel myNewLibraryPanel
;
62 private JPanel myLibraryPropertiesPanel
;
63 private JPanel myMirrorsPanel
;
64 private JLabel myMissingLibrariesLabel
;
65 private JLabel myHiddenLabel
;
66 private final List
<VirtualFile
> myAddedJars
= new ArrayList
<VirtualFile
>();
67 private final List
<Library
> myUsedLibraries
= new ArrayList
<Library
>();
68 private final LibrariesContainer myLibrariesContainer
;
69 private final LibraryCompositionSettings myLibraryCompositionSettings
;
70 private final List
<Library
> mySuitableLibraries
;
71 private final LibraryDownloadingMirrorsMap myMirrorsMap
;
72 private List
<RemoteRepositoryMirrorPanel
> myMirrorPanelsList
;
74 public LibraryCompositionOptionsPanel(final @NotNull LibrariesContainer librariesContainer
, final @NotNull LibraryCompositionSettings libraryCompositionSettings
,
75 final @NotNull LibraryDownloadingMirrorsMap mirrorsMap
) {
76 myLibrariesContainer
= librariesContainer
;
77 myLibraryCompositionSettings
= libraryCompositionSettings
;
78 myMirrorsMap
= mirrorsMap
;
81 myMainPanel
.setBorder(BorderFactory
.createCompoundBorder(IdeBorderFactory
.createTitledBorder(libraryCompositionSettings
.getTitle()), IdeBorderFactory
.createEmptyBorder(5,5,5,5)));
82 myDirectoryField
.addBrowseFolderListener(ProjectBundle
.message("file.chooser.directory.for.downloaded.libraries.title"),
83 ProjectBundle
.message("file.chooser.directory.for.downloaded.libraries.description"), null,
84 FileChooserDescriptorFactory
.createSingleFolderDescriptor());
85 myAddedJars
.addAll(myLibraryCompositionSettings
.getAddedJars());
86 myUsedLibraries
.addAll(myLibraryCompositionSettings
.getUsedLibraries());
87 myAddJarsButton
.addActionListener(new ActionListener() {
88 public void actionPerformed(final ActionEvent e
) {
92 myAddLibraryButton
.addActionListener(new ActionListener() {
93 public void actionPerformed(final ActionEvent e
) {
94 showLibrariesChooser();
97 myDownloadMissingJarsCheckBox
.setSelected(myLibraryCompositionSettings
.isDownloadLibraries());
98 myDownloadMissingJarsCheckBox
.addActionListener(new ActionListener() {
99 public void actionPerformed(final ActionEvent e
) {
104 mySuitableLibraries
= calculateSuitableLibraries();
105 myAddLibraryButton
.setEnabled(!mySuitableLibraries
.isEmpty());
107 myLibraryLevels
.put(LibrariesContainer
.LibraryLevel
.GLOBAL
, ProjectBundle
.message("combobox.item.global.library"));
108 myLibraryLevels
.put(LibrariesContainer
.LibraryLevel
.PROJECT
, ProjectBundle
.message("combobox.item.project.library"));
109 myLibraryLevels
.put(LibrariesContainer
.LibraryLevel
.MODULE
, ProjectBundle
.message("combobox.item.module.library"));
110 for (String level
: myLibraryLevels
.getValues()) {
111 myLibraryLevelComboBox
.addItem(level
);
113 myLibraryLevelComboBox
.setSelectedItem(myLibraryLevels
.getValue(myLibraryCompositionSettings
.getLibraryLevel()));
114 myLibraryNameField
.setText(myLibraryCompositionSettings
.getLibraryName());
116 myDirectoryField
.setText(FileUtil
.toSystemDependentName(myLibraryCompositionSettings
.getDirectoryForDownloadedLibrariesPath()));
117 String jars
= RequiredLibrariesInfo
.getLibrariesPresentableText(myLibraryCompositionSettings
.getLibraryInfos());
119 myHiddenLabel
.setText(UIUtil
.toHtml(ProjectBundle
.message("label.text.libraries.are.missing", jars
)));
122 myMissingLibrariesPanel
.getPreferredSize();
123 myMainPanel
.validate();
126 public LibraryCompositionSettings
getLibraryCompositionSettings() {
127 return myLibraryCompositionSettings
;
130 private void addMirrorsPanels() {
131 myMirrorsPanel
.setLayout(new VerticalFlowLayout());
132 myMirrorPanelsList
= new ArrayList
<RemoteRepositoryMirrorPanel
>();
133 Set
<String
> repositories
= new HashSet
<String
>();
134 LibraryInfo
[] libraryInfos
= myLibraryCompositionSettings
.getLibraryInfos();
135 for (LibraryInfo libraryInfo
: libraryInfos
) {
136 LibraryDownloadInfo downloadingInfo
= libraryInfo
.getDownloadingInfo();
137 if (downloadingInfo
!= null) {
138 RemoteRepositoryInfo repositoryInfo
= downloadingInfo
.getRemoteRepository();
139 if (repositoryInfo
!= null && repositories
.add(repositoryInfo
.getId())) {
140 RemoteRepositoryMirrorPanel mirrorPanel
= new RemoteRepositoryMirrorPanel(repositoryInfo
, myMirrorsMap
);
141 myMirrorPanelsList
.add(mirrorPanel
);
142 myMirrorsPanel
.add(mirrorPanel
.getPanel());
148 private List
<Library
> calculateSuitableLibraries() {
149 LibraryInfo
[] libraryInfos
= myLibraryCompositionSettings
.getLibraryInfos();
150 RequiredLibrariesInfo requiredLibraries
= new RequiredLibrariesInfo(libraryInfos
);
151 List
<Library
> suitableLibraries
= new ArrayList
<Library
>();
152 Library
[] libraries
= myLibrariesContainer
.getAllLibraries();
153 for (Library library
: libraries
) {
154 RequiredLibrariesInfo
.RequiredClassesNotFoundInfo info
=
155 requiredLibraries
.checkLibraries(myLibrariesContainer
.getLibraryFiles(library
, OrderRootType
.CLASSES
), false);
156 if (info
== null || info
.getLibraryInfos().length
< libraryInfos
.length
) {
157 suitableLibraries
.add(library
);
160 return suitableLibraries
;
163 private void showLibrariesChooser() {
164 ChooseLibrariesDialog dialog
= new ChooseLibrariesDialog(myMainPanel
, mySuitableLibraries
);
165 dialog
.markElements(myUsedLibraries
);
168 myUsedLibraries
.clear();
169 myUsedLibraries
.addAll(dialog
.getMarkedLibraries());
174 private void showFileChooser() {
175 final FileChooserDescriptor descriptor
= new FileChooserDescriptor(false, false, true, false, false, true);
176 descriptor
.setTitle(IdeBundle
.message("file.chooser.select.paths.title"));
177 descriptor
.setDescription(IdeBundle
.message("file.chooser.multiselect.description"));
178 final VirtualFile
[] files
= FileChooser
.chooseFiles(myAddJarsButton
, descriptor
, getBaseDirectory());
179 myAddedJars
.addAll(Arrays
.asList(files
));
184 private VirtualFile
getBaseDirectory() {
185 String path
= myLibraryCompositionSettings
.getBaseDirectoryForDownloadedFiles();
186 VirtualFile dir
= LocalFileSystem
.getInstance().findFileByPath(path
);
188 path
= path
.substring(0, path
.lastIndexOf('/'));
189 dir
= LocalFileSystem
.getInstance().findFileByPath(path
);
194 private void updateAll() {
195 String missingJarsText
;
196 List
<VirtualFile
> roots
= new ArrayList
<VirtualFile
>();
197 roots
.addAll(myAddedJars
);
198 for (Library library
: myUsedLibraries
) {
199 roots
.addAll(Arrays
.asList(myLibrariesContainer
.getLibraryFiles(library
, OrderRootType
.CLASSES
)));
201 RequiredLibrariesInfo
.RequiredClassesNotFoundInfo info
= new RequiredLibrariesInfo(myLibraryCompositionSettings
.getLibraryInfos()).checkLibraries(
202 VfsUtil
.toVirtualFileArray(roots
), false);
204 missingJarsText
= ProjectBundle
.message("label.text.libraries.are.missing", info
.getMissingJarsText());
207 missingJarsText
= ProjectBundle
.message("label.text.all.library.files.found");
209 myMissingLibrariesLabel
.setText(UIUtil
.toHtml(missingJarsText
));
210 ((CardLayout
)myMissingLibrariesPanel
.getLayout()).show(myMissingLibrariesPanel
, "shown");
212 myDownloadMissingJarsCheckBox
.setSelected(false);
216 myNewLibraryPanel
.setVisible(info
!= null || !myAddedJars
.isEmpty());
217 myLibraryPropertiesPanel
.setVisible(!myAddedJars
.isEmpty() || myDownloadMissingJarsCheckBox
.isSelected());
218 myDownloadMissingJarsCheckBox
.setEnabled(info
!= null);
219 myDirectoryField
.setEnabled(myDownloadMissingJarsCheckBox
.isSelected());
220 UIUtil
.setEnabled(myMirrorsPanel
, myDownloadMissingJarsCheckBox
.isSelected(), true);
223 public void updateRepositoriesMirrors(final LibraryDownloadingMirrorsMap mirrorsMap
) {
224 for (RemoteRepositoryMirrorPanel mirrorPanel
: myMirrorPanelsList
) {
225 mirrorPanel
.updateComboBox(mirrorsMap
);
229 public void saveSelectedRepositoriesMirrors(final LibraryDownloadingMirrorsMap mirrorsMap
) {
230 for (RemoteRepositoryMirrorPanel mirrorPanel
: myMirrorPanelsList
) {
231 mirrorsMap
.setMirror(mirrorPanel
.getRemoteRepository(), mirrorPanel
.getSelectedMirror());
235 public void apply() {
236 saveSelectedRepositoriesMirrors(myMirrorsMap
);
237 if (myDownloadMissingJarsCheckBox
.isSelected()) {
238 myLibraryCompositionSettings
.setDownloadLibraries(true);
239 myLibraryCompositionSettings
.setDirectoryForDownloadedLibrariesPath(FileUtil
.toSystemIndependentName(myDirectoryField
.getText()));
242 myLibraryCompositionSettings
.setDownloadLibraries(false);
244 myLibraryCompositionSettings
.setUsedLibraries(myUsedLibraries
);
245 myLibraryCompositionSettings
.setAddedJars(myAddedJars
);
246 myLibraryCompositionSettings
.setLibraryLevel(myLibraryLevels
.getKey((String
)myLibraryLevelComboBox
.getSelectedItem()));
247 myLibraryCompositionSettings
.setLibraryName(myLibraryNameField
.getText());
250 public JPanel
getMainPanel() {