highlighting of libraries in project structure fixed
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / projectRoot / StructureConfigurableContext.java
blob2cf22d28a8787bf76daf82d7e12a8e4b13083497
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() {
64 public ModulesConfigurator getModulesConfigurator() {
65 return myModulesConfigurator;
68 public Module[] getModules() {
69 return myModulesConfigurator.getModules();
72 public String getRealName(final Module module) {
73 final ModifiableModuleModel moduleModel = myModulesConfigurator.getModuleModel();
74 String newName = moduleModel.getNewName(module);
75 return newName != null ? newName : module.getName();
78 public void resetLibraries() {
79 final LibraryTablesRegistrar tablesRegistrar = LibraryTablesRegistrar.getInstance();
81 myLevel2Providers.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 public Library getLibraryModel(@NotNull Library library) {
144 final LibraryTable libraryTable = library.getTable();
145 if (libraryTable != null) {
146 return findLibraryModel(library, myLevel2Providers.get(libraryTable.getTableLevel()));
148 return library;
151 @Nullable
152 private static Library findLibraryModel(final Library library, LibrariesModifiableModel tableModel) {
153 if (tableModel == null) return library;
154 if (tableModel.wasLibraryRemoved(library)) return null;
155 return tableModel.hasLibraryEditor(library) ? (Library)tableModel.getLibraryEditor(library).getModel() : library;
159 public void reset() {
160 resetLibraries();
161 myModulesConfigurator.resetModuleEditors();
162 myDaemonAnalyzer.reset(); // should be called after resetLibraries!
165 public void clear() {
166 myLevel2Providers.clear();