From 8eb784f0267ef0f8f045ca418f31dc066fc28955 Mon Sep 17 00:00:00 2001 From: nik Date: Fri, 8 Oct 2010 13:22:17 +0400 Subject: [PATCH] fixed adding source/javadoc roots to library created in the new project wizard --- .../libraries/LibraryCompositionSettings.java | 3 +- .../libraryEditor/NewLibraryEditor.java | 15 +++++ .../projectRoot/LibrariesContainer.java | 3 + .../LibrariesContainerFactory.java | 60 +++++++++++-------- 4 files changed, 53 insertions(+), 28 deletions(-) diff --git a/java/idea-ui/src/com/intellij/facet/impl/ui/libraries/LibraryCompositionSettings.java b/java/idea-ui/src/com/intellij/facet/impl/ui/libraries/LibraryCompositionSettings.java index f8c16e806d0e..1c950688d919 100644 --- a/java/idea-ui/src/com/intellij/facet/impl/ui/libraries/LibraryCompositionSettings.java +++ b/java/idea-ui/src/com/intellij/facet/impl/ui/libraries/LibraryCompositionSettings.java @@ -108,9 +108,8 @@ public class LibraryCompositionSettings implements Disposable { @Nullable private Library createLibrary(final ModifiableRootModel rootModel, @Nullable LibrariesContainer additionalContainer) { if (myNewLibraryEditor != null) { - VirtualFile[] roots = myNewLibraryEditor.getFiles(OrderRootType.CLASSES); return LibrariesContainerFactory.createLibrary(additionalContainer, LibrariesContainerFactory.createContainer(rootModel), - myNewLibraryEditor.getName(), getLibraryLevel(), roots, VirtualFile.EMPTY_ARRAY); + myNewLibraryEditor, getLibraryLevel()); } return null; } diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/NewLibraryEditor.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/NewLibraryEditor.java index 6f448fc6506d..d4a48d5b0ff0 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/NewLibraryEditor.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/libraryEditor/NewLibraryEditor.java @@ -126,6 +126,10 @@ public class NewLibraryEditor implements LibraryEditor { public void apply(@NotNull Library.ModifiableModel model) { model.setName(myLibraryName); + applyRoots(model); + } + + public void applyRoots(Library.ModifiableModel model) { for (OrderRootType type : myRoots.keySet()) { for (LightFilePointer pointer : myRoots.get(type)) { model.addRoot(pointer.getUrl(), type); @@ -135,4 +139,15 @@ public class NewLibraryEditor implements LibraryEditor { model.addJarDirectory(entry.getKey(), entry.getValue()); } } + + public void copyRoots(ExistingLibraryEditor editor) { + for (OrderRootType type : myRoots.keySet()) { + for (LightFilePointer pointer : myRoots.get(type)) { + editor.addRoot(pointer.getUrl(), type); + } + } + for (Map.Entry entry : myJarDirectories.entrySet()) { + editor.addJarDirectory(entry.getKey(), entry.getValue()); + } + } } diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesContainer.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesContainer.java index 39a98803bef9..3b0b23719f61 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesContainer.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesContainer.java @@ -19,6 +19,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.OrderRootType; import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.roots.ui.configuration.libraryEditor.ExistingLibraryEditor; +import com.intellij.openapi.roots.ui.configuration.libraryEditor.NewLibraryEditor; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NonNls; @@ -54,6 +55,8 @@ public interface LibrariesContainer { Library createLibrary(@NotNull @NonNls String name, @NotNull LibraryLevel level, @NotNull VirtualFile[] classRoots, @NotNull VirtualFile[] sourceRoots); + Library createLibrary(@NotNull NewLibraryEditor libraryEditor, @NotNull LibraryLevel level); + @NotNull String suggestUniqueLibraryName(@NotNull String baseName); diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesContainerFactory.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesContainerFactory.java index a14670142748..f8444ad44229 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesContainerFactory.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/LibrariesContainerFactory.java @@ -25,6 +25,7 @@ import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar; import com.intellij.openapi.roots.ui.configuration.LibraryTableModifiableModelProvider; import com.intellij.openapi.roots.ui.configuration.libraryEditor.ExistingLibraryEditor; import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditor; +import com.intellij.openapi.roots.ui.configuration.libraryEditor.NewLibraryEditor; import com.intellij.openapi.util.Condition; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.ArrayUtil; @@ -72,27 +73,21 @@ public class LibrariesContainerFactory { } public static Library createLibrary(@Nullable LibrariesContainer container1, @NotNull LibrariesContainer container2, - @NotNull @NonNls final String name, @NotNull final LibrariesContainer.LibraryLevel level, - @NotNull final VirtualFile[] classRoots, @NotNull final VirtualFile[] sourceRoots) { + @NotNull @NonNls final NewLibraryEditor editor, @NotNull final LibrariesContainer.LibraryLevel level) { if (container1 != null && container1.canCreateLibrary(level)) { - return container1.createLibrary(name, level, classRoots, sourceRoots); + return container1.createLibrary(editor, level); } else { - return container2.createLibrary(name, level, classRoots, sourceRoots); + return container2.createLibrary(editor, level); } } @NotNull - public static Library createLibraryInTable(final @NonNls String name, final VirtualFile[] roots, final VirtualFile[] sources, final LibraryTable table) { + private static Library createLibraryInTable(final @NotNull NewLibraryEditor editor, final LibraryTable table) { LibraryTable.ModifiableModel modifiableModel = table.getModifiableModel(); - Library library = modifiableModel.createLibrary(getUniqueLibraryName(name, modifiableModel)); + Library library = modifiableModel.createLibrary(getUniqueLibraryName(editor.getName(), modifiableModel)); final Library.ModifiableModel model = library.getModifiableModel(); - for (VirtualFile root : roots) { - model.addRoot(root, OrderRootType.CLASSES); - } - for (VirtualFile root : sources) { - model.addRoot(root, OrderRootType.SOURCES); - } + editor.applyRoots(model); model.commit(); modifiableModel.commit(); return library; @@ -111,6 +106,22 @@ public class LibrariesContainerFactory { private abstract static class LibrariesContainerBase implements LibrariesContainer { private UniqueNameGenerator myNameGenerator; + @Override + public Library createLibrary(@NotNull @NonNls String name, + @NotNull LibraryLevel level, + @NotNull VirtualFile[] classRoots, + @NotNull VirtualFile[] sourceRoots) { + NewLibraryEditor editor = new NewLibraryEditor(); + editor.setName(name); + for (VirtualFile classRoot : classRoots) { + editor.addRoot(classRoot, OrderRootType.CLASSES); + } + for (VirtualFile sourceRoot : sourceRoots) { + editor.addRoot(sourceRoot, OrderRootType.SOURCES); + } + return createLibrary(editor, level); + } + @NotNull public Library[] getAllLibraries() { Library[] libraries = getLibraries(LibraryLevel.GLOBAL); @@ -204,10 +215,11 @@ public class LibrariesContainerFactory { return level == LibraryLevel.GLOBAL || myProject != null; } - public Library createLibrary(@NotNull @NonNls final String name, @NotNull final LibraryLevel level, - @NotNull final VirtualFile[] classRoots, @NotNull final VirtualFile[] sourceRoots) { + @Override + public Library createLibrary(@NotNull NewLibraryEditor libraryEditor, + @NotNull LibraryLevel level) { if (level == LibraryLevel.MODULE && myRootModel != null) { - return createLibraryInTable(name, classRoots, sourceRoots, myRootModel.getModuleLibraryTable()); + return createLibraryInTable(libraryEditor, myRootModel.getModuleLibraryTable()); } LibraryTablesRegistrar registrar = LibraryTablesRegistrar.getInstance(); @@ -221,7 +233,7 @@ public class LibrariesContainerFactory { else { return null; } - return createLibraryInTable(name, classRoots, sourceRoots, table); + return createLibraryInTable(libraryEditor, table); } @Override @@ -239,22 +251,18 @@ public class LibrariesContainerFactory { myContext = context; } - public Library createLibrary(@NotNull @NonNls final String name, @NotNull final LibraryLevel level, - @NotNull final VirtualFile[] classRoots, @NotNull final VirtualFile[] sourceRoots) { + @Override + public Library createLibrary(@NotNull NewLibraryEditor libraryEditor, + @NotNull LibraryLevel level) { LibraryTableModifiableModelProvider provider = getProvider(level); if (provider == null) { LOG.error("cannot create module library in this context"); } LibraryTable.ModifiableModel model = provider.getModifiableModel(); - Library library = model.createLibrary(getUniqueLibraryName(name, model)); - LibraryEditor libraryEditor = ((LibrariesModifiableModel)model).getLibraryEditor(library); - for (VirtualFile root : classRoots) { - libraryEditor.addRoot(root, OrderRootType.CLASSES); - } - for (VirtualFile source : sourceRoots) { - libraryEditor.addRoot(source, OrderRootType.SOURCES); - } + Library library = model.createLibrary(getUniqueLibraryName(libraryEditor.getName(), model)); + ExistingLibraryEditor createdLibraryEditor = ((LibrariesModifiableModel)model).getLibraryEditor(library); + libraryEditor.copyRoots(createdLibraryEditor); return library; }