dispose ModifiableRootModel
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / projectRoot / StructureConfigurableContext.java
blob84e2651f7f4055764ebce3d3c8958c9d814bce82
1 /*
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.Disposable;
19 import com.intellij.openapi.module.ModifiableModuleModel;
20 import com.intellij.openapi.module.Module;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.roots.libraries.Library;
23 import com.intellij.openapi.roots.libraries.LibraryTable;
24 import com.intellij.openapi.roots.libraries.LibraryTablePresentation;
25 import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar;
26 import com.intellij.openapi.roots.ui.configuration.LibraryTableModifiableModelProvider;
27 import com.intellij.openapi.roots.ui.configuration.ModulesConfigurator;
28 import com.intellij.openapi.roots.ui.configuration.projectRoot.daemon.ProjectStructureDaemonAnalyzer;
29 import com.intellij.openapi.util.Disposer;
30 import com.intellij.util.NotNullFunction;
31 import com.intellij.util.containers.ContainerUtil;
32 import gnu.trove.THashMap;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
36 import java.util.List;
37 import java.util.Map;
39 public class StructureConfigurableContext implements Disposable {
40 private final ProjectStructureDaemonAnalyzer myDaemonAnalyzer;
41 public final ModulesConfigurator myModulesConfigurator;
42 public final Map<String, LibrariesModifiableModel> myLevel2Providers = new THashMap<String, LibrariesModifiableModel>();
43 private final Project myProject;
46 public StructureConfigurableContext(Project project, final ModulesConfigurator modulesConfigurator) {
47 myProject = project;
48 myModulesConfigurator = modulesConfigurator;
49 Disposer.register(project, this);
50 myDaemonAnalyzer = new ProjectStructureDaemonAnalyzer(this);
53 public Project getProject() {
54 return myProject;
57 public ProjectStructureDaemonAnalyzer getDaemonAnalyzer() {
58 return myDaemonAnalyzer;
61 public void dispose() {
62 clear();
65 public ModulesConfigurator getModulesConfigurator() {
66 return myModulesConfigurator;
69 public Module[] getModules() {
70 return myModulesConfigurator.getModules();
73 public String getRealName(final Module module) {
74 final ModifiableModuleModel moduleModel = myModulesConfigurator.getModuleModel();
75 String newName = moduleModel.getNewName(module);
76 return newName != null ? newName : module.getName();
79 public void resetLibraries() {
80 final LibraryTablesRegistrar tablesRegistrar = LibraryTablesRegistrar.getInstance();
81 clear();
82 myLevel2Providers.put(LibraryTablesRegistrar.APPLICATION_LEVEL, new LibrariesModifiableModel(tablesRegistrar.getLibraryTable(), myProject));
83 myLevel2Providers.put(LibraryTablesRegistrar.PROJECT_LEVEL, new LibrariesModifiableModel(tablesRegistrar.getLibraryTable(myProject), myProject));
84 for (final LibraryTable table : tablesRegistrar.getCustomLibraryTables()) {
85 myLevel2Providers.put(table.getTableLevel(), new LibrariesModifiableModel(table, myProject));
89 public LibraryTableModifiableModelProvider getGlobalLibrariesProvider(final boolean tableEditable) {
90 return createModifiableModelProvider(LibraryTablesRegistrar.APPLICATION_LEVEL, tableEditable);
93 public LibraryTableModifiableModelProvider createModifiableModelProvider(final String level, final boolean isTableEditable) {
94 final LibraryTable table = LibraryTablesRegistrar.getInstance().getLibraryTableByLevel(level, myProject);
95 return new LibraryTableModifiableModelProvider() {
96 public LibraryTable.ModifiableModel getModifiableModel() {
97 return myLevel2Providers.get(level);
100 public String getTableLevel() {
101 return table.getTableLevel();
104 public LibraryTablePresentation getLibraryTablePresentation() {
105 return table.getPresentation();
108 public boolean isLibraryTableEditable() {
109 return isTableEditable && table.isEditable();
114 public LibraryTableModifiableModelProvider getProjectLibrariesProvider(final boolean tableEditable) {
115 return createModifiableModelProvider(LibraryTablesRegistrar.PROJECT_LEVEL, tableEditable);
119 public List<LibraryTableModifiableModelProvider> getCustomLibrariesProviders(final boolean tableEditable) {
120 return ContainerUtil.map2List(LibraryTablesRegistrar.getInstance().getCustomLibraryTables(), new NotNullFunction<LibraryTable, LibraryTableModifiableModelProvider>() {
121 @NotNull
122 public LibraryTableModifiableModelProvider fun(final LibraryTable libraryTable) {
123 return createModifiableModelProvider(libraryTable.getTableLevel(), tableEditable);
129 @Nullable
130 public Library getLibrary(final String libraryName, final String libraryLevel) {
131 /* the null check is added only to prevent NPE when called from getLibrary */
132 final LibrariesModifiableModel model = myLevel2Providers.get(libraryLevel);
133 return model == null ? null : findLibraryModel(libraryName, model);
136 @Nullable
137 private static Library findLibraryModel(final String libraryName, @NotNull LibrariesModifiableModel model) {
138 final Library library = model.getLibraryByName(libraryName);
139 return findLibraryModel(library, model);
142 @Nullable
143 private static Library findLibraryModel(final Library library, LibrariesModifiableModel tableModel) {
144 if (tableModel == null) return library;
145 if (tableModel.wasLibraryRemoved(library)) return null;
146 return tableModel.hasLibraryEditor(library) ? (Library)tableModel.getLibraryEditor(library).getModel() : library;
150 public void reset() {
151 resetLibraries();
152 myModulesConfigurator.resetModuleEditors();
153 myDaemonAnalyzer.reset(); // should be called after resetLibraries!
156 public void clear() {
157 for (LibrariesModifiableModel model : myLevel2Providers.values()) {
158 model.disposeUncommittedLibraries();
160 myLevel2Providers.clear();