2 * Copyright 2000-2010 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.openapi.roots.ui.configuration.classpath;
18 import com.intellij.openapi.project.ProjectBundle;
19 import com.intellij.openapi.roots.LibraryOrderEntry;
20 import com.intellij.openapi.roots.ModifiableRootModel;
21 import com.intellij.openapi.roots.OrderEntry;
22 import com.intellij.openapi.roots.impl.libraries.LibraryImpl;
23 import com.intellij.openapi.roots.libraries.Library;
24 import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext;
25 import com.intellij.openapi.ui.Messages;
26 import com.intellij.util.Icons;
27 import com.intellij.util.containers.ContainerUtil;
28 import org.jetbrains.annotations.Nullable;
35 class ChooseExistingLibraryAction extends AddItemPopupAction<Library> {
36 private StructureConfigurableContext myContext;
38 public ChooseExistingLibraryAction(ClasspathPanel classpathPanel, final int index, final String title,
39 final StructureConfigurableContext context) {
40 super(classpathPanel, index, title, Icons.LIBRARY_ICON);
45 protected ClasspathTableItem createTableItem(final Library item) {
46 // clear invalid order entry corresponding to added library if any
47 final ModifiableRootModel rootModel = myClasspathPanel.getRootModel();
48 final OrderEntry[] orderEntries = rootModel.getOrderEntries();
49 for (OrderEntry orderEntry : orderEntries) {
50 if (orderEntry instanceof LibraryOrderEntry) {
51 if (item.getName().equals(((LibraryOrderEntry)orderEntry).getLibraryName())) {
52 if (orderEntry.isValid()) {
53 Messages.showErrorDialog(ProjectBundle.message("classpath.message.library.already.added",item.getName()),
54 ProjectBundle.message("classpath.title.adding.dependency"));
57 rootModel.removeOrderEntry(orderEntry);
62 return ClasspathTableItem.createLibItem(rootModel.addLibraryEntry(item));
65 protected ClasspathElementChooserDialog<Library> createChooserDialog() {
66 return new MyChooserDialog();
69 private Collection<Library> getAlreadyAddedLibraries() {
70 final OrderEntry[] orderEntries = myClasspathPanel.getRootModel().getOrderEntries();
71 final Set<Library> result = new HashSet<Library>(orderEntries.length);
72 for (OrderEntry orderEntry : orderEntries) {
73 if (orderEntry instanceof LibraryOrderEntry && orderEntry.isValid()) {
74 final LibraryImpl library = (LibraryImpl)((LibraryOrderEntry)orderEntry).getLibrary();
75 if (library != null) {
76 ContainerUtil.addIfNotNull(result, library.getSource());
83 class MyChooserDialog implements ClasspathElementChooserDialog<Library> {
84 private List<Library> mySelectedLibraries;
86 public List<Library> getChosenElements() {
87 return mySelectedLibraries;
90 public void doChoose() {
91 ProjectStructureChooseLibrariesDialog dialog = new ProjectStructureChooseLibrariesDialog(myClasspathPanel.getComponent(), myClasspathPanel.getProject(), myContext,
92 getAlreadyAddedLibraries());
94 mySelectedLibraries = dialog.getSelectedLibraries();
97 public boolean isOK() {
98 return mySelectedLibraries != null && !mySelectedLibraries.isEmpty();
101 public void dispose() {