47a8269f549f21dad37779a23d9c4ee7e9b2c864
[idea/community.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / classpath / ChooseExistingLibraryAction.java
1 /*
2  * Copyright 2000-2010 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.openapi.roots.ui.configuration.classpath;
17
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;
29
30 import java.util.*;
31
32 /**
33 * @author nik
34 */
35 class ChooseExistingLibraryAction extends AddItemPopupAction<Library> {
36   private StructureConfigurableContext myContext;
37
38   public ChooseExistingLibraryAction(ClasspathPanel classpathPanel, final int index, final String title,
39                                      final StructureConfigurableContext context) {
40     super(classpathPanel, index, title, Icons.LIBRARY_ICON);
41     myContext = context;
42   }
43
44   @Nullable
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"));
55             return null;
56           } else {
57             rootModel.removeOrderEntry(orderEntry);
58           }
59         }
60       }
61     }
62     return ClasspathTableItem.createLibItem(rootModel.addLibraryEntry(item));
63   }
64
65   protected ClasspathElementChooserDialog<Library> createChooserDialog() {
66     return new MyChooserDialog();
67   }
68
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());
77         }
78       }
79     }
80     return result;
81   }
82
83   class MyChooserDialog implements ClasspathElementChooserDialog<Library> {
84     private List<Library> mySelectedLibraries;
85
86     public List<Library> getChosenElements() {
87       return mySelectedLibraries;
88     }
89
90     public void doChoose() {
91       ProjectStructureChooseLibrariesDialog dialog = new ProjectStructureChooseLibrariesDialog(myClasspathPanel.getComponent(), myClasspathPanel.getProject(), myContext,
92                                                                                                getAlreadyAddedLibraries());
93       dialog.show();
94       mySelectedLibraries = dialog.getSelectedLibraries();
95     }
96
97     public boolean isOK() {
98       return mySelectedLibraries != null && !mySelectedLibraries.isEmpty();
99     }
100
101     public void dispose() {
102     }
103   }
104 }