diff --git a/platform/lang-api/src/com/intellij/openapi/module/ModulePointerManager.java b/platform/lang-api/src/com/intellij/openapi/module/ModulePointerManager.java index ff11d77ec254..6690ba696ee6 100644 --- a/platform/lang-api/src/com/intellij/openapi/module/ModulePointerManager.java +++ b/platform/lang-api/src/com/intellij/openapi/module/ModulePointerManager.java @@ -15,11 +15,12 @@ */ package com.intellij.openapi.module; +import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.project.Project; public abstract class ModulePointerManager { public static ModulePointerManager getInstance(Project project) { - return project.getComponent(ModulePointerManager.class); + return ServiceManager.getService(project, ModulePointerManager.class); } public abstract ModulePointer create(Module module); diff --git a/platform/lang-impl/lang-impl.iml b/platform/lang-impl/lang-impl.iml index ff79180fee81..05045cad5f6e 100644 --- a/platform/lang-impl/lang-impl.iml +++ b/platform/lang-impl/lang-impl.iml @@ -18,6 +18,7 @@ + diff --git a/java/java-impl/src/com/intellij/openapi/module/impl/ModulePointerImpl.java b/platform/lang-impl/src/com/intellij/openapi/module/impl/ModulePointerImpl.java similarity index 100% rename from java/java-impl/src/com/intellij/openapi/module/impl/ModulePointerImpl.java rename to platform/lang-impl/src/com/intellij/openapi/module/impl/ModulePointerImpl.java diff --git a/platform/lang-impl/src/com/intellij/openapi/module/impl/ModulePointerManagerImpl.java b/platform/lang-impl/src/com/intellij/openapi/module/impl/ModulePointerManagerImpl.java new file mode 100644 index 000000000000..eba14d81259d --- /dev/null +++ b/platform/lang-impl/src/com/intellij/openapi/module/impl/ModulePointerManagerImpl.java @@ -0,0 +1,83 @@ +/* + * Copyright 2000-2009 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.openapi.module.impl; + +import com.intellij.ProjectTopics; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModulePointer; +import com.intellij.openapi.module.ModulePointerManager; +import com.intellij.openapi.project.ModuleAdapter; +import com.intellij.openapi.project.Project; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author nik + */ +public class ModulePointerManagerImpl extends ModulePointerManager { + private Map myUnresolved = new HashMap(); + private Map myPointers = new HashMap(); + private final Project myProject; + + public ModulePointerManagerImpl(Project project) { + myProject = project; + project.getMessageBus().connect().subscribe(ProjectTopics.MODULES, new ModuleAdapter() { + @Override + public void beforeModuleRemoved(Project project, Module module) { + final ModulePointerImpl pointer = myPointers.remove(module); + if (pointer != null) { + pointer.moduleRemoved(module); + myUnresolved.put(pointer.getModuleName(), pointer); + } + } + + @Override + public void moduleAdded(Project project, Module module) { + final ModulePointerImpl pointer = myUnresolved.remove(module.getName()); + if (pointer != null) { + pointer.moduleAdded(module); + myPointers.put(module, pointer); + } + } + }); + } + + @Override + public ModulePointer create(Module module) { + ModulePointerImpl pointer = myPointers.get(module); + if (pointer == null) { + pointer = new ModulePointerImpl(module); + myPointers.put(module, pointer); + } + return pointer; + } + + @Override + public ModulePointer create(String moduleName) { + final Module module = ModuleManagerImpl.getInstance(myProject).findModuleByName(moduleName); + if (module != null) { + return create(module); + } + + ModulePointerImpl pointer = myUnresolved.get(moduleName); + if (pointer == null) { + pointer = new ModulePointerImpl(moduleName); + myUnresolved.put(moduleName, pointer); + } + return pointer; + } +} diff --git a/platform/lang-impl/testSrc/com/intellij/module/ModulePointerTest.java b/platform/lang-impl/testSrc/com/intellij/module/ModulePointerTest.java new file mode 100644 index 000000000000..d6e2449422cb --- /dev/null +++ b/platform/lang-impl/testSrc/com/intellij/module/ModulePointerTest.java @@ -0,0 +1,92 @@ +/* + * Copyright 2000-2009 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.module; + +import com.intellij.openapi.Disposable; +import com.intellij.openapi.application.Result; +import com.intellij.openapi.application.WriteAction; +import com.intellij.openapi.module.*; +import com.intellij.testFramework.PlatformTestCase; + +/** + * @author nik + */ +public class ModulePointerTest extends PlatformTestCase { + public void testCreateByName() throws Exception { + final ModulePointer pointer = getPointerManager().create("m"); + assertNull(pointer.getModule()); + assertEquals("m", pointer.getModuleName()); + + final Module module = addModule("m"); + + assertSame(module, pointer.getModule()); + assertEquals("m", pointer.getModuleName()); + } + + public void testCreateByModule() throws Exception { + final Module module = addModule("x"); + final ModulePointer pointer = getPointerManager().create(module); + assertSame(module, pointer.getModule()); + assertEquals("x", pointer.getModuleName()); + + ModifiableModuleModel model = getModuleManager().getModifiableModel(); + model.disposeModule(module); + commitModel(model); + + assertNull(pointer.getModule()); + assertEquals("x", pointer.getModuleName()); + } + + public void testRenameModule() throws Exception { + final ModulePointer pointer = getPointerManager().create("abc"); + final Module module = addModule("abc"); + ModifiableModuleModel model = getModuleManager().getModifiableModel(); + model.renameModule(module, "xyz"); + commitModel(model); + assertSame(module, pointer.getModule()); + assertEquals("xyz", pointer.getModuleName()); + } + + private ModuleManager getModuleManager() { + return ModuleManager.getInstance(myProject); + } + + private Module addModule(final String name) { + final ModifiableModuleModel model = getModuleManager().getModifiableModel(); + final Module module = model.newModule(myProject.getBaseDir().getPath() + "/" + name + ".iml", EmptyModuleType.getInstance()); + commitModel(model); + disposeOnTearDown(new Disposable() { + public void dispose() { + if (!module.isDisposed()) { + getModuleManager().disposeModule(module); + } + } + }); + return module; + } + + private static void commitModel(final ModifiableModuleModel model) { + new WriteAction() { + protected void run(final Result result) { + model.commit(); + } + }.execute(); + } + + private ModulePointerManager getPointerManager() { + return ModulePointerManager.getInstance(myProject); + } +} diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index a9489a80aa7c..3d3fffcaea6a 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -293,6 +293,9 @@ + +