ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / projectRoot / StructureConfigurableContext.java
blobe9a9630ad1635974b78c9a446d8dfbee497014bf
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.libraryEditor.LibraryEditorListener;
29 import com.intellij.openapi.roots.ui.configuration.projectRoot.daemon.ProjectStructureDaemonAnalyzer;
30 import com.intellij.openapi.util.Disposer;
31 import com.intellij.util.NotNullFunction;
32 import com.intellij.util.containers.ContainerUtil;
33 import gnu.trove.THashMap;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Map;
41 public class StructureConfigurableContext implements Disposable, LibraryEditorListener {
42 private final ProjectStructureDaemonAnalyzer myDaemonAnalyzer;
43 public final ModulesConfigurator myModulesConfigurator;
44 public final Map<String, LibrariesModifiableModel> myLevel2Providers = new THashMap<String, LibrariesModifiableModel>();
45 private List<LibraryEditorListener> myLibraryEditorListeners = new ArrayList<LibraryEditorListener>();
46 private final Project myProject;
49 public StructureConfigurableContext(Project project, final ModulesConfigurator modulesConfigurator) {
50 myProject = project;
51 myModulesConfigurator = modulesConfigurator;
52 Disposer.register(project, this);
53 myDaemonAnalyzer = new ProjectStructureDaemonAnalyzer(this);
56 public Project getProject() {
57 return myProject;
60 public ProjectStructureDaemonAnalyzer getDaemonAnalyzer() {
61 return myDaemonAnalyzer;
64 public void dispose() {
67 public ModulesConfigurator getModulesConfigurator() {
68 return myModulesConfigurator;
71 public Module[] getModules() {
72 return myModulesConfigurator.getModules();
75 public String getRealName(final Module module) {
76 final ModifiableModuleModel moduleModel = myModulesConfigurator.getModuleModel();
77 String newName = moduleModel.getNewName(module);
78 return newName != null ? newName : module.getName();
81 public void resetLibraries() {
82 final LibraryTablesRegistrar tablesRegistrar = LibraryTablesRegistrar.getInstance();
84 myLevel2Providers.clear();
85 myLevel2Providers.put(LibraryTablesRegistrar.APPLICATION_LEVEL, new LibrariesModifiableModel(tablesRegistrar.getLibraryTable(), myProject, this));
86 myLevel2Providers.put(LibraryTablesRegistrar.PROJECT_LEVEL, new LibrariesModifiableModel(tablesRegistrar.getLibraryTable(myProject), myProject, this));
87 for (final LibraryTable table : tablesRegistrar.getCustomLibraryTables()) {
88 myLevel2Providers.put(table.getTableLevel(), new LibrariesModifiableModel(table, myProject, this));
92 public void addLibraryEditorListener(LibraryEditorListener listener) {
93 myLibraryEditorListeners.add(listener);
96 public void removeLibraryEditorListener(LibraryEditorListener listener) {
97 myLibraryEditorListeners.remove(listener);
100 public void libraryRenamed(@NotNull Library library, String oldName, String newName) {
101 for (LibraryEditorListener listener : myLibraryEditorListeners) {
102 listener.libraryRenamed(library, oldName, newName);
106 public LibraryTableModifiableModelProvider getGlobalLibrariesProvider(final boolean tableEditable) {
107 return createModifiableModelProvider(LibraryTablesRegistrar.APPLICATION_LEVEL, tableEditable);
110 public LibraryTableModifiableModelProvider createModifiableModelProvider(final String level, final boolean isTableEditable) {
111 final LibraryTable table = LibraryTablesRegistrar.getInstance().getLibraryTableByLevel(level, myProject);
112 return new LibraryTableModifiableModelProvider() {
113 public LibraryTable.ModifiableModel getModifiableModel() {
114 return myLevel2Providers.get(level);
117 public String getTableLevel() {
118 return table.getTableLevel();
121 public LibraryTablePresentation getLibraryTablePresentation() {
122 return table.getPresentation();
125 public boolean isLibraryTableEditable() {
126 return isTableEditable && table.isEditable();
131 public LibraryTableModifiableModelProvider getProjectLibrariesProvider(final boolean tableEditable) {
132 return createModifiableModelProvider(LibraryTablesRegistrar.PROJECT_LEVEL, tableEditable);
136 public List<LibraryTableModifiableModelProvider> getCustomLibrariesProviders(final boolean tableEditable) {
137 return ContainerUtil.map2List(LibraryTablesRegistrar.getInstance().getCustomLibraryTables(), new NotNullFunction<LibraryTable, LibraryTableModifiableModelProvider>() {
138 @NotNull
139 public LibraryTableModifiableModelProvider fun(final LibraryTable libraryTable) {
140 return createModifiableModelProvider(libraryTable.getTableLevel(), tableEditable);
146 @Nullable
147 public Library getLibrary(final String libraryName, final String libraryLevel) {
148 /* the null check is added only to prevent NPE when called from getLibrary */
149 final LibrariesModifiableModel model = myLevel2Providers.get(libraryLevel);
150 return model == null ? null : findLibraryModel(libraryName, model);
153 @Nullable
154 private static Library findLibraryModel(final @NotNull String libraryName, @NotNull LibrariesModifiableModel model) {
155 for (Library library : model.getLibraries()) {
156 final Library libraryModel = findLibraryModel(library, model);
157 if (libraryModel != null && libraryName.equals(libraryModel.getName())) {
158 return libraryModel;
161 return null;
164 @Nullable
165 public Library getLibraryModel(@NotNull Library library) {
166 final LibraryTable libraryTable = library.getTable();
167 if (libraryTable != null) {
168 return findLibraryModel(library, myLevel2Providers.get(libraryTable.getTableLevel()));
170 return library;
173 @Nullable
174 private static Library findLibraryModel(final Library library, LibrariesModifiableModel tableModel) {
175 if (tableModel == null) return library;
176 if (tableModel.wasLibraryRemoved(library)) return null;
177 return tableModel.hasLibraryEditor(library) ? (Library)tableModel.getLibraryEditor(library).getModel() : library;
181 public void reset() {
182 resetLibraries();
183 myModulesConfigurator.resetModuleEditors();
184 myDaemonAnalyzer.reset(); // should be called after resetLibraries!
187 public void clear() {
188 myLevel2Providers.clear();