[memory-agent] Use shortcut jetbrainsLibrary for memory agent library in licenses...
[idea/community.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / artifacts / sourceItems / LibrarySourceItem.java
1 /*
2  * Copyright 2000-2009 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.artifacts.sourceItems;
17
18 import com.intellij.ide.JavaUiBundle;
19 import com.intellij.ide.presentation.VirtualFilePresentation;
20 import com.intellij.ide.projectView.PresentationData;
21 import com.intellij.openapi.roots.OrderRootType;
22 import com.intellij.openapi.roots.impl.libraries.LibraryEx;
23 import com.intellij.openapi.roots.libraries.Library;
24 import com.intellij.openapi.roots.libraries.LibraryUtil;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import com.intellij.packaging.elements.PackagingElement;
27 import com.intellij.packaging.elements.PackagingElementFactory;
28 import com.intellij.packaging.elements.PackagingElementOutputKind;
29 import com.intellij.packaging.impl.elements.LibraryPackagingElement;
30 import com.intellij.packaging.impl.ui.LibraryElementPresentation;
31 import com.intellij.packaging.ui.ArtifactEditorContext;
32 import com.intellij.packaging.ui.PackagingSourceItem;
33 import com.intellij.packaging.ui.SourceItemPresentation;
34 import com.intellij.packaging.ui.SourceItemWeights;
35 import com.intellij.ui.SimpleTextAttributes;
36 import com.intellij.util.PlatformIcons;
37 import org.jetbrains.annotations.NotNull;
38
39 import java.util.List;
40
41 public class LibrarySourceItem extends PackagingSourceItem {
42   private final Library myLibrary;
43
44   public LibrarySourceItem(@NotNull Library library) {
45     myLibrary = library;
46   }
47
48   @NotNull
49   @Override
50   public SourceItemPresentation createPresentation(@NotNull ArtifactEditorContext context) {
51     return new LibrarySourceItemPresentation(myLibrary, context);
52   }
53
54   public boolean equals(Object obj) {
55     return obj instanceof LibrarySourceItem && myLibrary.equals(((LibrarySourceItem)obj).myLibrary);
56   }
57
58   public int hashCode() {
59     return myLibrary.hashCode();
60   }
61
62   @NotNull 
63   public Library getLibrary() {
64     return myLibrary;
65   }
66
67   @NotNull
68   @Override
69   public PackagingElementOutputKind getKindOfProducedElements() {
70     return LibraryPackagingElement.getKindForLibrary(myLibrary);
71   }
72
73   @Override
74   @NotNull
75   public List<? extends PackagingElement<?>> createElements(@NotNull ArtifactEditorContext context) {
76     return PackagingElementFactory.getInstance().createLibraryElements(myLibrary);
77   }
78
79   private static class LibrarySourceItemPresentation extends SourceItemPresentation {
80     private final Library myLibrary;
81     private final ArtifactEditorContext myContext;
82
83     LibrarySourceItemPresentation(Library library, ArtifactEditorContext context) {
84       myLibrary = library;
85       myContext = context;
86     }
87
88     @Override
89     public boolean canNavigateToSource() {
90       return myLibrary != null;
91     }
92
93     @Override
94     public void navigateToSource() {
95       myContext.selectLibrary(myLibrary);
96     }
97
98     @Override
99     public String getPresentableName() {
100       return LibraryUtil.getPresentableName(myLibrary);
101     }
102
103     @Override
104     public void render(@NotNull PresentationData presentationData, SimpleTextAttributes mainAttributes,
105                        SimpleTextAttributes commentAttributes) {
106       final String name = myLibrary.getName();
107       if (name != null) {
108         presentationData.setIcon(PlatformIcons.LIBRARY_ICON);
109         presentationData.addText(name, mainAttributes);
110         presentationData.addText(LibraryElementPresentation.getLibraryTableComment(myLibrary), commentAttributes);
111       }
112       else {
113         if (((LibraryEx)myLibrary).isDisposed()) {
114           //todo[nik] disposed library should not be shown in the tree
115           presentationData.addText(JavaUiBundle.message("library.source.item.label.invalid.library"), SimpleTextAttributes.ERROR_ATTRIBUTES);
116           return;
117         }
118         final VirtualFile[] files = myLibrary.getFiles(OrderRootType.CLASSES);
119         if (files.length > 0) {
120           final VirtualFile file = files[0];
121           presentationData.setIcon(VirtualFilePresentation.getIcon(file));
122           presentationData.addText(file.getName(), mainAttributes);
123         }
124         else {
125           presentationData.addText(JavaUiBundle.message("library.source.item.label.empty.library"), SimpleTextAttributes.ERROR_ATTRIBUTES);
126         }
127       }
128     }
129
130     @Override
131     public int getWeight() {
132       return SourceItemWeights.LIBRARY_WEIGHT;
133     }
134   }
135 }