646bd391f4f59763ca7fe26c28b3fe4753f7646a
[idea/community.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / classpath / ProjectStructureChooseLibrariesDialog.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.ide.projectView.PresentationData;
19 import com.intellij.ide.util.treeView.NodeDescriptor;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.roots.impl.libraries.LibraryImpl;
22 import com.intellij.openapi.roots.libraries.Library;
23 import com.intellij.openapi.roots.libraries.LibraryTable;
24 import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesModifiableModel;
25 import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext;
26 import com.intellij.ui.SimpleTextAttributes;
27 import com.intellij.util.Icons;
28 import com.intellij.util.ui.classpath.ChooseLibrariesFromTablesDialog;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31
32 import javax.swing.*;
33 import java.util.Collection;
34
35 /**
36  * @author nik
37  */
38 public class ProjectStructureChooseLibrariesDialog extends ChooseLibrariesFromTablesDialog {
39   private StructureConfigurableContext myContext;
40   private Collection<Library> myAlreadyAddedLibraries;
41
42   public ProjectStructureChooseLibrariesDialog(JComponent parentComponent,
43                                                @Nullable Project project,
44                                                StructureConfigurableContext context,
45                                                Collection<Library> alreadyAddedLibraries) {
46     super(parentComponent, "Choose Libraries", project, true);
47     myContext = context;
48     myAlreadyAddedLibraries = alreadyAddedLibraries;
49     init();
50   }
51
52   @NotNull
53   @Override
54   protected Library[] getLibraries(@NotNull LibraryTable table) {
55     final LibrariesModifiableModel model = getLibrariesModifiableModel(table);
56     if (model == null) return Library.EMPTY_ARRAY;
57     return model.getLibraries();
58   }
59
60   private LibrariesModifiableModel getLibrariesModifiableModel(LibraryTable table) {
61     return myContext.myLevel2Providers.get(table.getTableLevel());
62   }
63
64   @Override
65   protected boolean acceptsElement(Object element) {
66     if (element instanceof Library) {
67       final Library library = (Library)element;
68       if (myAlreadyAddedLibraries.contains(library)) return false;
69       if (library instanceof LibraryImpl) {
70         final Library source = ((LibraryImpl)library).getSource();
71         if (source != null && myAlreadyAddedLibraries.contains(source)) return false;
72       }
73     }
74     return true;
75   }
76
77   @NotNull
78   private String getLibraryName(@NotNull Library library) {
79     final LibrariesModifiableModel model = getLibrariesModifiableModel(library.getTable());
80     if (model != null) {
81       if (model.hasLibraryEditor(library)) {
82         return model.getLibraryEditor(library).getName();
83       }
84     }
85     return library.getName();
86   }
87
88   @Override
89   protected LibrariesTreeNodeBase<Library> createLibraryDescriptor(NodeDescriptor parentDescriptor,
90                                                                    Library library) {
91     final String libraryName = getLibraryName(library);
92     return new LibraryEditorDescriptor(getProject(), parentDescriptor, library, libraryName);
93   }
94
95   private static class LibraryEditorDescriptor extends LibrariesTreeNodeBase<Library> {
96     protected LibraryEditorDescriptor(final Project project, final NodeDescriptor parentDescriptor, final Library element,
97                                       String libraryName) {
98       super(project, parentDescriptor, element);
99       final PresentationData templatePresentation = getTemplatePresentation();
100       templatePresentation.setIcons(Icons.LIBRARY_ICON);
101       templatePresentation.addText(libraryName, SimpleTextAttributes.REGULAR_ATTRIBUTES);
102     }
103   }
104
105 }