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
.module
.impl
;
18 import com
.intellij
.ProjectTopics
;
19 import com
.intellij
.openapi
.Disposable
;
20 import com
.intellij
.openapi
.module
.Module
;
21 import com
.intellij
.openapi
.module
.ModulePointer
;
22 import com
.intellij
.openapi
.module
.ModulePointerManager
;
23 import com
.intellij
.openapi
.project
.ModuleAdapter
;
24 import com
.intellij
.openapi
.project
.Project
;
25 import com
.intellij
.openapi
.util
.Disposer
;
26 import org
.jetbrains
.annotations
.NotNull
;
28 import java
.util
.HashMap
;
29 import java
.util
.List
;
35 public class ModulePointerManagerImpl
extends ModulePointerManager
{
36 private final Map
<String
, ModulePointerImpl
> myUnresolved
= new HashMap
<String
, ModulePointerImpl
>();
37 private final Map
<Module
, ModulePointerImpl
> myPointers
= new HashMap
<Module
, ModulePointerImpl
>();
38 private final Project myProject
;
40 public ModulePointerManagerImpl(Project project
) {
42 project
.getMessageBus().connect().subscribe(ProjectTopics
.MODULES
, new ModuleAdapter() {
44 public void beforeModuleRemoved(Project project
, Module module
) {
45 unregisterPointer(module
);
49 public void moduleAdded(Project project
, Module module
) {
50 moduleAppears(module
);
54 public void modulesRenamed(Project project
, List
<Module
> modules
) {
55 for (Module module
: modules
) {
56 moduleAppears(module
);
62 private void moduleAppears(Module module
) {
63 ModulePointerImpl pointer
= myUnresolved
.remove(module
.getName());
64 if (pointer
!= null && pointer
.getModule() == null) {
65 pointer
.moduleAdded(module
);
66 registerPointer(module
, pointer
);
70 private void registerPointer(final Module module
, final ModulePointerImpl pointer
) {
71 myPointers
.put(module
, pointer
);
72 Disposer
.register(module
, new Disposable() {
73 public void dispose() {
74 unregisterPointer(module
);
79 private void unregisterPointer(Module module
) {
80 final ModulePointerImpl pointer
= myPointers
.remove(module
);
81 if (pointer
!= null) {
82 pointer
.moduleRemoved(module
);
83 myUnresolved
.put(pointer
.getModuleName(), pointer
);
89 public ModulePointer
create(@NotNull Module module
) {
90 ModulePointerImpl pointer
= myPointers
.get(module
);
91 if (pointer
== null) {
92 pointer
= myUnresolved
.get(module
.getName());
93 if (pointer
== null) {
94 pointer
= new ModulePointerImpl(module
);
97 pointer
.moduleAdded(module
);
99 registerPointer(module
, pointer
);
106 public ModulePointer
create(@NotNull String moduleName
) {
107 final Module module
= ModuleManagerImpl
.getInstance(myProject
).findModuleByName(moduleName
);
108 if (module
!= null) {
109 return create(module
);
112 ModulePointerImpl pointer
= myUnresolved
.get(moduleName
);
113 if (pointer
== null) {
114 pointer
= new ModulePointerImpl(moduleName
);
115 myUnresolved
.put(moduleName
, pointer
);