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
.projectRoot
;
18 import com
.intellij
.openapi
.module
.Module
;
19 import com
.intellij
.openapi
.project
.Project
;
20 import com
.intellij
.openapi
.roots
.*;
21 import com
.intellij
.openapi
.roots
.libraries
.Library
;
22 import com
.intellij
.openapi
.roots
.libraries
.LibraryTable
;
23 import com
.intellij
.openapi
.roots
.libraries
.LibraryTablesRegistrar
;
24 import com
.intellij
.openapi
.roots
.ui
.configuration
.LibraryTableModifiableModelProvider
;
25 import com
.intellij
.openapi
.roots
.ui
.configuration
.libraryEditor
.LibraryEditor
;
26 import com
.intellij
.openapi
.vfs
.VirtualFile
;
27 import com
.intellij
.openapi
.diagnostic
.Logger
;
28 import com
.intellij
.util
.ArrayUtil
;
29 import org
.jetbrains
.annotations
.NonNls
;
30 import org
.jetbrains
.annotations
.NotNull
;
31 import org
.jetbrains
.annotations
.Nullable
;
33 import java
.util
.List
;
34 import java
.util
.ArrayList
;
39 public class LibrariesContainerFactory
{
40 private static final Logger LOG
= Logger
.getInstance("#com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainerFactory");
41 private static final Library
[] EMPTY_LIBRARIES_ARRAY
= new Library
[0];
43 private LibrariesContainerFactory() {
47 public static LibrariesContainer
createContainer(@Nullable Project project
) {
48 return new LibrariesContainerImpl(project
, null, null);
52 public static LibrariesContainer
createContainer(@NotNull Module module
) {
53 return new LibrariesContainerImpl(module
.getProject(), module
, null);
57 public static LibrariesContainer
createContainer(@NotNull ModifiableRootModel rootModel
) {
58 Module module
= rootModel
.getModule();
59 return new LibrariesContainerImpl(module
.getProject(), module
, rootModel
);
63 public static LibrariesContainer
createContainer() {
64 return new LibrariesContainerImpl(null, null, null);
67 public static LibrariesContainer
createContainer(@NotNull Project project
, StructureConfigurableContext context
) {
68 return new StructureConfigurableLibrariesContainer(project
, context
);
71 public static Library
createLibrary(@Nullable LibrariesContainer container1
, @NotNull LibrariesContainer container2
,
72 @NotNull @NonNls final String name
, @NotNull final LibrariesContainer
.LibraryLevel level
,
73 @NotNull final VirtualFile
[] classRoots
, @NotNull final VirtualFile
[] sourceRoots
) {
74 if (container1
!= null && container1
.canCreateLibrary(level
)) {
75 return container1
.createLibrary(name
, level
, classRoots
, sourceRoots
);
78 return container2
.createLibrary(name
, level
, classRoots
, sourceRoots
);
83 public static Library
createLibraryInTable(final @NonNls String name
, final VirtualFile
[] roots
, final VirtualFile
[] sources
, final LibraryTable table
) {
84 LibraryTable
.ModifiableModel modifiableModel
= table
.getModifiableModel();
85 Library library
= modifiableModel
.createLibrary(getUniqueLibraryName(name
, modifiableModel
));
86 final Library
.ModifiableModel model
= library
.getModifiableModel();
87 for (VirtualFile root
: roots
) {
88 model
.addRoot(root
, OrderRootType
.CLASSES
);
90 for (VirtualFile root
: sources
) {
91 model
.addRoot(root
, OrderRootType
.SOURCES
);
94 modifiableModel
.commit();
98 private static String
getUniqueLibraryName(final String baseName
, final LibraryTable
.ModifiableModel model
) {
99 String name
= baseName
;
101 while (model
.getLibraryByName(name
) != null) {
102 name
= baseName
+ " (" + count
++ + ")";
108 private abstract static class LibrariesContainerBase
implements LibrariesContainer
{
110 public Library
[] getAllLibraries() {
111 Library
[] libraries
= getLibraies(LibraryLevel
.GLOBAL
);
112 Library
[] projectLibraries
= getLibraies(LibraryLevel
.PROJECT
);
113 if (projectLibraries
.length
> 0) {
114 libraries
= ArrayUtil
.mergeArrays(libraries
, projectLibraries
, Library
.class);
116 Library
[] moduleLibraries
= getLibraies(LibraryLevel
.MODULE
);
117 if (moduleLibraries
.length
> 0) {
118 libraries
= ArrayUtil
.mergeArrays(libraries
, moduleLibraries
, Library
.class);
125 private static class LibrariesContainerImpl
extends LibrariesContainerBase
{
126 private @Nullable final Project myProject
;
127 @Nullable private final Module myModule
;
128 @Nullable private final ModifiableRootModel myRootModel
;
130 private LibrariesContainerImpl(final @Nullable Project project
, final @Nullable Module module
, final @Nullable ModifiableRootModel rootModel
) {
133 myRootModel
= rootModel
;
137 public Project
getProject() {
142 public Library
[] getLibraies(@NotNull final LibraryLevel libraryLevel
) {
143 if (libraryLevel
== LibraryLevel
.MODULE
&& myModule
!= null) {
144 return getModuleLibraries();
147 LibraryTablesRegistrar registrar
= LibraryTablesRegistrar
.getInstance();
148 if (libraryLevel
== LibraryLevel
.GLOBAL
) {
149 return registrar
.getLibraryTable().getLibraries();
152 if (libraryLevel
== LibraryLevel
.PROJECT
&& myProject
!= null) {
153 return registrar
.getLibraryTable(myProject
).getLibraries();
156 return EMPTY_LIBRARIES_ARRAY
;
159 private Library
[] getModuleLibraries() {
160 if (myRootModel
!= null) {
161 return myRootModel
.getModuleLibraryTable().getLibraries();
163 OrderEntry
[] orderEntries
= ModuleRootManager
.getInstance(myModule
).getOrderEntries();
164 List
<Library
> libraries
= new ArrayList
<Library
>();
165 for (OrderEntry orderEntry
: orderEntries
) {
166 if (orderEntry
instanceof LibraryOrderEntry
) {
167 final LibraryOrderEntry entry
= (LibraryOrderEntry
)orderEntry
;
168 if (entry
.isModuleLevel()) {
169 libraries
.add(entry
.getLibrary());
173 return libraries
.toArray(new Library
[libraries
.size()]);
177 public VirtualFile
[] getLibraryFiles(@NotNull final Library library
, @NotNull final OrderRootType rootType
) {
178 return library
.getFiles(rootType
);
181 public boolean canCreateLibrary(@NotNull final LibraryLevel level
) {
182 if (level
== LibraryLevel
.MODULE
) {
183 return myRootModel
!= null;
185 return level
== LibraryLevel
.GLOBAL
|| myProject
!= null;
188 public Library
createLibrary(@NotNull @NonNls final String name
, @NotNull final LibraryLevel level
,
189 @NotNull final VirtualFile
[] classRoots
, @NotNull final VirtualFile
[] sourceRoots
) {
190 if (level
== LibraryLevel
.MODULE
&& myRootModel
!= null) {
191 return createLibraryInTable(name
, classRoots
, sourceRoots
, myRootModel
.getModuleLibraryTable());
194 LibraryTablesRegistrar registrar
= LibraryTablesRegistrar
.getInstance();
196 if (level
== LibraryLevel
.GLOBAL
) {
197 table
= registrar
.getLibraryTable();
199 else if (level
== LibraryLevel
.PROJECT
&& myProject
!= null) {
200 table
= registrar
.getLibraryTable(myProject
);
205 return createLibraryInTable(name
, classRoots
, sourceRoots
, table
);
209 private static class StructureConfigurableLibrariesContainer
extends LibrariesContainerBase
{
210 private final Project myProject
;
211 private final StructureConfigurableContext myContext
;
213 public StructureConfigurableLibrariesContainer(final Project project
, final StructureConfigurableContext context
) {
218 public Library
createLibrary(@NotNull @NonNls final String name
, @NotNull final LibraryLevel level
,
219 @NotNull final VirtualFile
[] classRoots
, @NotNull final VirtualFile
[] sourceRoots
) {
220 LibraryTableModifiableModelProvider provider
= getProvider(level
);
221 if (provider
== null) {
222 LOG
.error("cannot create module library in this context");
225 LibraryTable
.ModifiableModel model
= provider
.getModifiableModel();
226 Library library
= model
.createLibrary(getUniqueLibraryName(name
, model
));
227 LibraryEditor libraryEditor
= ((LibrariesModifiableModel
)model
).getLibraryEditor(library
);
228 for (VirtualFile root
: classRoots
) {
229 libraryEditor
.addRoot(root
, OrderRootType
.CLASSES
);
231 for (VirtualFile source
: sourceRoots
) {
232 libraryEditor
.addRoot(source
, OrderRootType
.SOURCES
);
238 public Project
getProject() {
243 public Library
[] getLibraies(@NotNull final LibraryLevel libraryLevel
) {
244 LibraryTableModifiableModelProvider provider
= getProvider(libraryLevel
);
245 return provider
!= null ? provider
.getModifiableModel().getLibraries() : EMPTY_LIBRARIES_ARRAY
;
249 private LibraryTableModifiableModelProvider
getProvider(LibraryLevel libraryLevel
) {
250 if (libraryLevel
== LibraryLevel
.PROJECT
) {
251 return myContext
.getProjectLibrariesProvider(false);
253 else if (libraryLevel
== LibraryLevel
.GLOBAL
) {
254 return myContext
.getGlobalLibrariesProvider(false);
261 public boolean canCreateLibrary(@NotNull final LibraryLevel level
) {
262 return level
== LibraryLevel
.GLOBAL
|| level
== LibraryLevel
.PROJECT
;
266 public VirtualFile
[] getLibraryFiles(@NotNull final Library library
, @NotNull final OrderRootType rootType
) {
267 LibrariesModifiableModel projectLibrariesModel
= (LibrariesModifiableModel
)myContext
.getProjectLibrariesProvider(false).getModifiableModel();
268 if (projectLibrariesModel
.hasLibraryEditor(library
)) {
269 LibraryEditor libraryEditor
= projectLibrariesModel
.getLibraryEditor(library
);
270 return libraryEditor
.getFiles(rootType
);
272 LibrariesModifiableModel globalLibraries
= (LibrariesModifiableModel
)myContext
.getGlobalLibrariesProvider(false).getModifiableModel();
273 if (globalLibraries
.hasLibraryEditor(library
)) {
274 LibraryEditor libraryEditor
= globalLibraries
.getLibraryEditor(library
);
275 return libraryEditor
.getFiles(rootType
);
277 return library
.getFiles(rootType
);