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
.openapi
.roots
.ui
.configuration
.artifacts
.sourceItems
;
18 import com
.intellij
.openapi
.module
.Module
;
19 import com
.intellij
.openapi
.module
.ModuleManager
;
20 import com
.intellij
.openapi
.roots
.LibraryOrderEntry
;
21 import com
.intellij
.openapi
.roots
.ModuleRootModel
;
22 import com
.intellij
.openapi
.roots
.OrderEntry
;
23 import com
.intellij
.openapi
.roots
.OrderRootType
;
24 import com
.intellij
.openapi
.roots
.libraries
.Library
;
25 import com
.intellij
.openapi
.util
.Comparing
;
26 import com
.intellij
.openapi
.vfs
.VirtualFile
;
27 import com
.intellij
.packaging
.artifacts
.Artifact
;
28 import com
.intellij
.packaging
.impl
.artifacts
.ArtifactUtil
;
29 import com
.intellij
.packaging
.impl
.elements
.FileCopyPackagingElement
;
30 import com
.intellij
.packaging
.impl
.elements
.ModuleOutputElementType
;
31 import com
.intellij
.packaging
.impl
.elements
.ModuleOutputPackagingElement
;
32 import com
.intellij
.packaging
.impl
.elements
.PackagingElementFactoryImpl
;
33 import com
.intellij
.packaging
.ui
.ArtifactEditorContext
;
34 import com
.intellij
.packaging
.ui
.PackagingSourceItem
;
35 import com
.intellij
.packaging
.ui
.PackagingSourceItemsProvider
;
36 import com
.intellij
.util
.ArrayUtil
;
37 import com
.intellij
.util
.Processor
;
38 import org
.jetbrains
.annotations
.NotNull
;
45 public class ModulesAndLibrariesSourceItemsProvider
extends PackagingSourceItemsProvider
{
48 public Collection
<?
extends PackagingSourceItem
> getSourceItems(@NotNull ArtifactEditorContext editorContext
, @NotNull Artifact artifact
,
49 PackagingSourceItem parent
) {
51 return createModuleItems(editorContext
, artifact
, ArrayUtil
.EMPTY_STRING_ARRAY
);
53 else if (parent
instanceof ModuleGroupItem
) {
54 return createModuleItems(editorContext
, artifact
, ((ModuleGroupItem
)parent
).getPath());
56 else if (parent
instanceof ModuleSourceItemGroup
) {
57 return createClasspathItems(editorContext
, artifact
, ((ModuleSourceItemGroup
)parent
).getModule());
59 return Collections
.emptyList();
62 private static Collection
<?
extends PackagingSourceItem
> createClasspathItems(ArtifactEditorContext editorContext
,
63 Artifact artifact
, @NotNull Module module
) {
64 final List
<PackagingSourceItem
> items
= new ArrayList
<PackagingSourceItem
>();
65 final ModuleRootModel rootModel
= editorContext
.getModulesProvider().getRootModel(module
);
66 List
<Library
> libraries
= new ArrayList
<Library
>();
67 for (OrderEntry orderEntry
: rootModel
.getOrderEntries()) {
68 if (orderEntry
instanceof LibraryOrderEntry
) {
69 final Library library
= ((LibraryOrderEntry
)orderEntry
).getLibrary();
70 if (library
!= null) {
71 libraries
.add(library
);
76 for (Module toAdd
: getNotAddedModules(editorContext
, artifact
, module
)) {
77 items
.add(new ModuleOutputSourceItem(toAdd
));
80 for (Library library
: getNotAddedLibraries(editorContext
, artifact
, libraries
)) {
81 items
.add(new LibrarySourceItem(library
));
86 private static Collection
<?
extends PackagingSourceItem
> createModuleItems(ArtifactEditorContext editorContext
, Artifact artifact
, @NotNull String
[] groupPath
) {
87 final Module
[] modules
= editorContext
.getModulesProvider().getModules();
88 final List
<PackagingSourceItem
> items
= new ArrayList
<PackagingSourceItem
>();
89 Set
<String
> groups
= new HashSet
<String
>();
90 for (Module module
: modules
) {
91 String
[] path
= ModuleManager
.getInstance(editorContext
.getProject()).getModuleGroupPath(module
);
93 path
= ArrayUtil
.EMPTY_STRING_ARRAY
;
96 if (Comparing
.equal(path
, groupPath
)) {
97 items
.add(new ModuleSourceItemGroup(module
));
99 else if (ArrayUtil
.startsWith(path
, groupPath
)) {
100 groups
.add(path
[groupPath
.length
]);
103 for (String group
: groups
) {
104 items
.add(0, new ModuleGroupItem(ArrayUtil
.append(groupPath
, group
)));
110 private static List
<?
extends Module
> getNotAddedModules(@NotNull final ArtifactEditorContext context
, @NotNull Artifact artifact
,
111 final Module
... allModules
) {
112 final Set
<Module
> modules
= new HashSet
<Module
>(Arrays
.asList(allModules
));
113 ArtifactUtil
.processPackagingElements(artifact
, ModuleOutputElementType
.MODULE_OUTPUT_ELEMENT_TYPE
, new Processor
<ModuleOutputPackagingElement
>() {
114 public boolean process(ModuleOutputPackagingElement moduleOutputPackagingElement
) {
115 modules
.remove(moduleOutputPackagingElement
.findModule(context
));
119 return new ArrayList
<Module
>(modules
);
122 private static List
<?
extends Library
> getNotAddedLibraries(@NotNull final ArtifactEditorContext context
, @NotNull Artifact artifact
,
123 List
<Library
> librariesList
) {
124 final Set
<VirtualFile
> roots
= new HashSet
<VirtualFile
>();
125 ArtifactUtil
.processPackagingElements(artifact
, PackagingElementFactoryImpl
.FILE_COPY_ELEMENT_TYPE
, new Processor
<FileCopyPackagingElement
>() {
126 public boolean process(FileCopyPackagingElement fileCopyPackagingElement
) {
127 final VirtualFile root
= fileCopyPackagingElement
.getLibraryRoot();
134 final List
<Library
> result
= new ArrayList
<Library
>();
135 for (Library library
: librariesList
) {
136 if (!roots
.containsAll(Arrays
.asList(library
.getFiles(OrderRootType
.CLASSES
)))) {