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
.VirtualFile
;
35 import com
.intellij
.ui
.IdeBorderFactory
;
36 import com
.intellij
.util
.ui
.UIUtil
;
37 import org
.jetbrains
.annotations
.Nullable
;
38 import org
.jetbrains
.annotations
.NotNull
;
42 import java
.awt
.event
.ActionEvent
;
43 import java
.awt
.event
.ActionListener
;
45 import java
.util
.List
;
50 public class LibraryCompositionOptionsPanel
{
51 private final MutualMap
<LibrariesContainer
.LibraryLevel
, String
> myLibraryLevels
= new MutualMap
<LibrariesContainer
.LibraryLevel
, String
>(true);
52 private JPanel myMainPanel
;
53 private JButton myAddLibraryButton
;
54 private JButton myAddJarsButton
;
55 private JCheckBox myDownloadMissingJarsCheckBox
;
56 private TextFieldWithBrowseButton myDirectoryField
;
57 private JComboBox myLibraryLevelComboBox
;
58 private JTextField myLibraryNameField
;
59 private JPanel myMissingLibrariesPanel
;
60 private JPanel myNewLibraryPanel
;
61 private JPanel myLibraryPropertiesPanel
;
62 private JPanel myMirrorsPanel
;
63 private JLabel myMissingLibrariesLabel
;
64 private JLabel myHiddenLabel
;
65 private final List
<VirtualFile
> myAddedJars
= new ArrayList
<VirtualFile
>();
66 private final List
<Library
> myUsedLibraries
= new ArrayList
<Library
>();
67 private final LibrariesContainer myLibrariesContainer
;
68 private final LibraryCompositionSettings myLibraryCompositionSettings
;
69 private final List
<Library
> mySuitableLibraries
;
70 private final LibraryDownloadingMirrorsMap myMirrorsMap
;
71 private List
<RemoteRepositoryMirrorPanel
> myMirrorPanelsList
;
73 public LibraryCompositionOptionsPanel(final @NotNull LibrariesContainer librariesContainer
, final @NotNull LibraryCompositionSettings libraryCompositionSettings
,
74 final @NotNull LibraryDownloadingMirrorsMap mirrorsMap
) {
75 myLibrariesContainer
= librariesContainer
;
76 myLibraryCompositionSettings
= libraryCompositionSettings
;
77 myMirrorsMap
= mirrorsMap
;
80 myMainPanel
.setBorder(BorderFactory
.createCompoundBorder(IdeBorderFactory
.createTitledBorder(libraryCompositionSettings
.getTitle()), IdeBorderFactory
.createEmptyBorder(5,5,5,5)));
81 myDirectoryField
.addBrowseFolderListener(ProjectBundle
.message("file.chooser.directory.for.downloaded.libraries.title"),
82 ProjectBundle
.message("file.chooser.directory.for.downloaded.libraries.description"), null,
83 FileChooserDescriptorFactory
.createSingleFolderDescriptor());
84 myAddedJars
.addAll(myLibraryCompositionSettings
.getAddedJars());
85 myUsedLibraries
.addAll(myLibraryCompositionSettings
.getUsedLibraries());
86 myAddJarsButton
.addActionListener(new ActionListener() {
87 public void actionPerformed(final ActionEvent e
) {
91 myAddLibraryButton
.addActionListener(new ActionListener() {
92 public void actionPerformed(final ActionEvent e
) {
93 showLibrariesChooser();
96 myDownloadMissingJarsCheckBox
.setSelected(myLibraryCompositionSettings
.isDownloadLibraries());
97 myDownloadMissingJarsCheckBox
.addActionListener(new ActionListener() {
98 public void actionPerformed(final ActionEvent e
) {
103 mySuitableLibraries
= calculateSuitableLibraries();
104 myAddLibraryButton
.setEnabled(!mySuitableLibraries
.isEmpty());
106 myLibraryLevels
.put(LibrariesContainer
.LibraryLevel
.GLOBAL
, ProjectBundle
.message("combobox.item.global.library"));
107 myLibraryLevels
.put(LibrariesContainer
.LibraryLevel
.PROJECT
, ProjectBundle
.message("combobox.item.project.library"));
108 myLibraryLevels
.put(LibrariesContainer
.LibraryLevel
.MODULE
, ProjectBundle
.message("combobox.item.module.library"));
109 for (String level
: myLibraryLevels
.getValues()) {
110 myLibraryLevelComboBox
.addItem(level
);
112 myLibraryLevelComboBox
.setSelectedItem(myLibraryLevels
.getValue(myLibraryCompositionSettings
.getLibraryLevel()));
113 myLibraryNameField
.setText(myLibraryCompositionSettings
.getLibraryName());
115 myDirectoryField
.setText(FileUtil
.toSystemDependentName(myLibraryCompositionSettings
.getDirectoryForDownloadedLibrariesPath()));
116 String jars
= RequiredLibrariesInfo
.getLibrariesPresentableText(myLibraryCompositionSettings
.getLibraryInfos());
118 myHiddenLabel
.setText(UIUtil
.toHtml(ProjectBundle
.message("label.text.libraries.are.missing", jars
)));
121 myMissingLibrariesPanel
.getPreferredSize();
122 myMainPanel
.validate();
125 public LibraryCompositionSettings
getLibraryCompositionSettings() {
126 return myLibraryCompositionSettings
;
129 private void addMirrorsPanels() {
130 myMirrorsPanel
.setLayout(new VerticalFlowLayout());
131 myMirrorPanelsList
= new ArrayList
<RemoteRepositoryMirrorPanel
>();
132 Set
<String
> repositories
= new HashSet
<String
>();
133 LibraryInfo
[] libraryInfos
= myLibraryCompositionSettings
.getLibraryInfos();
134 for (LibraryInfo libraryInfo
: libraryInfos
) {
135 LibraryDownloadInfo downloadingInfo
= libraryInfo
.getDownloadingInfo();
136 if (downloadingInfo
!= null) {
137 RemoteRepositoryInfo repositoryInfo
= downloadingInfo
.getRemoteRepository();
138 if (repositoryInfo
!= null && repositories
.add(repositoryInfo
.getId())) {
139 RemoteRepositoryMirrorPanel mirrorPanel
= new RemoteRepositoryMirrorPanel(repositoryInfo
, myMirrorsMap
);
140 myMirrorPanelsList
.add(mirrorPanel
);
141 myMirrorsPanel
.add(mirrorPanel
.getPanel());
147 private List
<Library
> calculateSuitableLibraries() {
148 LibraryInfo
[] libraryInfos
= myLibraryCompositionSettings
.getLibraryInfos();
149 RequiredLibrariesInfo requiredLibraries
= new RequiredLibrariesInfo(libraryInfos
);
150 List
<Library
> suitableLibraries
= new ArrayList
<Library
>();
151 Library
[] libraries
= myLibrariesContainer
.getAllLibraries();
152 for (Library library
: libraries
) {
153 RequiredLibrariesInfo
.RequiredClassesNotFoundInfo info
=
154 requiredLibraries
.checkLibraries(myLibrariesContainer
.getLibraryFiles(library
, OrderRootType
.CLASSES
));
155 if (info
== null || info
.getLibraryInfos().length
< libraryInfos
.length
) {
156 suitableLibraries
.add(library
);
159 return suitableLibraries
;
162 private void showLibrariesChooser() {
163 ChooseLibrariesDialog dialog
= new ChooseLibrariesDialog(myMainPanel
, mySuitableLibraries
);
164 dialog
.markElements(myUsedLibraries
);
167 myUsedLibraries
.clear();
168 myUsedLibraries
.addAll(dialog
.getMarkedLibraries());
173 private void showFileChooser() {
174 final FileChooserDescriptor descriptor
= new FileChooserDescriptor(false, false, true, false, false, true);
175 descriptor
.setTitle(IdeBundle
.message("file.chooser.select.paths.title"));
176 descriptor
.setDescription(IdeBundle
.message("file.chooser.multiselect.description"));
177 final VirtualFile
[] files
= FileChooser
.chooseFiles(myAddJarsButton
, descriptor
, getBaseDirectory());
178 myAddedJars
.addAll(Arrays
.asList(files
));
183 private VirtualFile
getBaseDirectory() {
184 String path
= myLibraryCompositionSettings
.getBaseDirectoryForDownloadedFiles();
185 VirtualFile dir
= LocalFileSystem
.getInstance().findFileByPath(path
);
187 path
= path
.substring(0, path
.lastIndexOf('/'));
188 dir
= LocalFileSystem
.getInstance().findFileByPath(path
);
193 private void updateAll() {
194 String missingJarsText
= "";
195 List
<VirtualFile
> roots
= new ArrayList
<VirtualFile
>();
196 roots
.addAll(myAddedJars
);
197 for (Library library
: myUsedLibraries
) {
198 roots
.addAll(Arrays
.asList(myLibrariesContainer
.getLibraryFiles(library
, OrderRootType
.CLASSES
)));
200 RequiredLibrariesInfo
.RequiredClassesNotFoundInfo info
= new RequiredLibrariesInfo(myLibraryCompositionSettings
.getLibraryInfos()).checkLibraries(
201 roots
.toArray(new VirtualFile
[roots
.size()]));
203 missingJarsText
= ProjectBundle
.message("label.text.libraries.are.missing", info
.getMissingJarsText());
206 missingJarsText
= ProjectBundle
.message("label.text.all.library.files.found");
208 myMissingLibrariesLabel
.setText(UIUtil
.toHtml(missingJarsText
));
209 ((CardLayout
)myMissingLibrariesPanel
.getLayout()).show(myMissingLibrariesPanel
, "shown");
211 myDownloadMissingJarsCheckBox
.setSelected(false);
215 myNewLibraryPanel
.setVisible(info
!= null || !myAddedJars
.isEmpty());
216 myLibraryPropertiesPanel
.setVisible(!myAddedJars
.isEmpty() || myDownloadMissingJarsCheckBox
.isSelected());
217 myDownloadMissingJarsCheckBox
.setEnabled(info
!= null);
218 myDirectoryField
.setEnabled(myDownloadMissingJarsCheckBox
.isSelected());
219 UIUtil
.setEnabled(myMirrorsPanel
, myDownloadMissingJarsCheckBox
.isSelected(), true);
222 public void updateRepositoriesMirrors(final LibraryDownloadingMirrorsMap mirrorsMap
) {
223 for (RemoteRepositoryMirrorPanel mirrorPanel
: myMirrorPanelsList
) {
224 mirrorPanel
.updateComboBox(mirrorsMap
);
228 public void saveSelectedRepositoriesMirrors(final LibraryDownloadingMirrorsMap mirrorsMap
) {
229 for (RemoteRepositoryMirrorPanel mirrorPanel
: myMirrorPanelsList
) {
230 mirrorsMap
.setMirror(mirrorPanel
.getRemoteRepository(), mirrorPanel
.getSelectedMirror());
234 public void apply() {
235 saveSelectedRepositoriesMirrors(myMirrorsMap
);
236 if (myDownloadMissingJarsCheckBox
.isSelected()) {
237 myLibraryCompositionSettings
.setDownloadLibraries(true);
238 myLibraryCompositionSettings
.setDirectoryForDownloadedLibrariesPath(FileUtil
.toSystemIndependentName(myDirectoryField
.getText()));
241 myLibraryCompositionSettings
.setDownloadLibraries(false);
243 myLibraryCompositionSettings
.setUsedLibraries(myUsedLibraries
);
244 myLibraryCompositionSettings
.setAddedJars(myAddedJars
);
245 myLibraryCompositionSettings
.setLibraryLevel(myLibraryLevels
.getKey((String
)myLibraryLevelComboBox
.getSelectedItem()));
246 myLibraryCompositionSettings
.setLibraryName(myLibraryNameField
.getText());
249 public JPanel
getMainPanel() {