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 org
.jetbrains
.plugins
.groovy
.config
;
18 import com
.intellij
.facet
.impl
.ui
.ProjectConfigurableContext
;
19 import com
.intellij
.facet
.ui
.FacetEditorContext
;
20 import com
.intellij
.openapi
.extensions
.ExtensionPointName
;
21 import com
.intellij
.openapi
.fileChooser
.FileChooserDescriptor
;
22 import com
.intellij
.openapi
.fileChooser
.FileChooserFactory
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import com
.intellij
.openapi
.roots
.libraries
.Library
;
25 import com
.intellij
.openapi
.roots
.libraries
.LibraryTable
;
26 import com
.intellij
.openapi
.roots
.libraries
.LibraryTablesRegistrar
;
27 import com
.intellij
.openapi
.roots
.libraries
.LibraryUtil
;
28 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.LibrariesContainer
;
29 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.ProjectLibrariesConfigurable
;
30 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.GlobalLibrariesConfigurable
;
31 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.LibrariesModifiableModel
;
32 import com
.intellij
.openapi
.roots
.impl
.libraries
.ProjectLibraryTable
;
33 import com
.intellij
.openapi
.ui
.Messages
;
34 import com
.intellij
.openapi
.vfs
.VirtualFile
;
35 import com
.intellij
.openapi
.application
.ApplicationManager
;
36 import com
.intellij
.openapi
.util
.Computable
;
37 import com
.intellij
.util
.containers
.CollectionFactory
;
38 import com
.intellij
.util
.containers
.ContainerUtil
;
39 import org
.jetbrains
.annotations
.NotNull
;
40 import org
.jetbrains
.annotations
.Nullable
;
41 import org
.jetbrains
.plugins
.groovy
.config
.ui
.CreateLibraryDialog
;
44 import java
.util
.List
;
50 public abstract class AbstractGroovyLibraryManager
extends LibraryManager
{
51 public static final ExtensionPointName
<AbstractGroovyLibraryManager
> EP_NAME
= ExtensionPointName
.create("org.intellij.groovy.libraryManager");
54 private static String
generatePointerName(String version
, final String libPrefix
, final LibrariesContainer container
,
55 final Set
<String
> usedLibraryNames
) {
56 String originalName
= libPrefix
+ version
;
57 String newName
= originalName
;
59 while (usedLibraryNames
.contains(newName
)) {
60 newName
= originalName
+ " (" + index
+ ")";
67 public String
getAddActionText() {
68 return "Create new " + getLibraryCategoryName() + " library...";
71 public Icon
getDialogIcon() {
75 protected abstract void fillLibrary(final String path
, final Library
.ModifiableModel model
);
78 public final Library
createSDKLibrary(final String path
,
80 final Project project
,
81 final boolean inModuleSettings
,
82 final boolean inProject
) {
84 final Library
.ModifiableModel model
;
85 LibraryTable
.ModifiableModel globalModel
= null;
86 if (inModuleSettings
) {
87 globalModel
= project
!= null && inProject ?
88 ProjectLibrariesConfigurable
.getInstance(project
).getModelProvider(true).getModifiableModel() :
89 GlobalLibrariesConfigurable
.getInstance(project
).getModelProvider(true).getModifiableModel();
90 assert globalModel
!= null;
91 library
= globalModel
.createLibrary(name
);
92 model
= ((LibrariesModifiableModel
)globalModel
).getLibraryEditor(library
).getModel();
95 project
!= null && inProject ? ProjectLibraryTable
.getInstance(project
) : LibraryTablesRegistrar
.getInstance().getLibraryTable();
96 library
= LibraryUtil
.createLibrary(table
, name
);
97 model
= library
.getModifiableModel();
100 assert library
!= null;
103 fillLibrary(path
, model
);
106 if (!inModuleSettings
) {
110 globalModel
.commit();
117 public Library
createLibrary(@NotNull FacetEditorContext context
) {
118 final FileChooserDescriptor descriptor
= new FileChooserDescriptor(false, true, false, false, false, false) {
119 public boolean isFileSelectable(VirtualFile file
) {
120 return super.isFileSelectable(file
) && isSDKHome(file
);
123 final Project project
= context
.getModule().getProject();
124 final VirtualFile
[] files
= FileChooserFactory
.getInstance().createFileChooser(descriptor
, project
).choose(null, project
);
125 if (files
.length
== 1) {
126 return createLibrary(files
[0].getPath(), ((ProjectConfigurableContext
)context
).getContainer(), true);
132 public Library
createLibrary(@NotNull final String path
, final LibrariesContainer container
, final boolean inModuleSettings
) {
133 final List
<String
> versions
= CollectionFactory
.arrayList();
134 final Set
<String
> usedLibraryNames
= CollectionFactory
.newTroveSet();
135 for (Library library
: container
.getAllLibraries()) {
136 usedLibraryNames
.add(library
.getName());
137 if (managesLibrary(library
, container
)) {
138 ContainerUtil
.addIfNotNull(getLibraryVersion(library
, container
), versions
);
142 final String newVersion
= getSDKVersion(path
);
143 final String libraryKind
= getLibraryCategoryName();
145 boolean addVersion
= !versions
.contains(newVersion
) ||
146 Messages
.showOkCancelDialog("Add one more " + libraryKind
+ " library of version " + newVersion
+ "?",
147 "Duplicate library version", getDialogIcon()) == 0;
149 if (addVersion
&& !AbstractConfigUtils
.UNDEFINED_VERSION
.equals(newVersion
)) {
150 final Project project
= container
.getProject();
151 final String name
= generatePointerName(newVersion
, getLibraryPrefix(), container
, usedLibraryNames
);
152 final CreateLibraryDialog dialog
= new CreateLibraryDialog(project
, "Create " + libraryKind
+ " library",
153 "Create Project " + libraryKind
+ " library '" + name
+ "'",
154 "Create Global " + libraryKind
+ " library '" + name
+ "'");
157 return ApplicationManager
.getApplication().runWriteAction(new Computable
<Library
>() {
159 public Library
compute() {
160 return createSDKLibrary(path
, name
, project
, inModuleSettings
, dialog
.isInProject());