From 29427e71b448682d4cdbbb885edea215bc063243 Mon Sep 17 00:00:00 2001 From: nik Date: Wed, 6 Nov 2013 16:54:44 +0400 Subject: [PATCH] added service to quickly find modules by module type; hide DevKit and J2ME run configurations if there are no corresponding modules (IDEA-116046) --- .../openapi/module/TypedModuleService.java | 37 ++++++++++ .../module/impl/TypedModuleServiceImpl.java | 73 +++++++++++++++++++ .../src/META-INF/LangExtensions.xml | 2 + .../devkit/src/module/PluginModuleType.java | 12 --- .../src/run/PluginConfigurationType.java | 14 +++- 5 files changed, 122 insertions(+), 16 deletions(-) create mode 100644 platform/lang-api/src/com/intellij/openapi/module/TypedModuleService.java create mode 100644 platform/lang-impl/src/com/intellij/openapi/module/impl/TypedModuleServiceImpl.java diff --git a/platform/lang-api/src/com/intellij/openapi/module/TypedModuleService.java b/platform/lang-api/src/com/intellij/openapi/module/TypedModuleService.java new file mode 100644 index 000000000000..2db821ae54a9 --- /dev/null +++ b/platform/lang-api/src/com/intellij/openapi/module/TypedModuleService.java @@ -0,0 +1,37 @@ +/* + * Copyright 2000-2013 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; + +import com.intellij.openapi.components.ServiceManager; +import com.intellij.openapi.project.Project; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; + +/** + * @author nik + */ +public abstract class TypedModuleService { + @NotNull + public static TypedModuleService getInstance(@NotNull Project project) { + return ServiceManager.getService(project, TypedModuleService.class); + } + + @NotNull + public abstract Collection getModulesOfType(@NotNull ModuleType moduleType); + + public abstract boolean hasModulesOfType(@NotNull ModuleType module); +} diff --git a/platform/lang-impl/src/com/intellij/openapi/module/impl/TypedModuleServiceImpl.java b/platform/lang-impl/src/com/intellij/openapi/module/impl/TypedModuleServiceImpl.java new file mode 100644 index 000000000000..9490dcefbbbe --- /dev/null +++ b/platform/lang-impl/src/com/intellij/openapi/module/impl/TypedModuleServiceImpl.java @@ -0,0 +1,73 @@ +/* + * Copyright 2000-2013 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.ModuleManager; +import com.intellij.openapi.module.ModuleType; +import com.intellij.openapi.module.TypedModuleService; +import com.intellij.openapi.project.ModuleAdapter; +import com.intellij.openapi.project.Project; +import com.intellij.util.containers.MultiMap; +import com.intellij.util.messages.MessageBus; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; + +/** + * @author nik + */ +public class TypedModuleServiceImpl extends TypedModuleService { + private MultiMap, Module> myCachedModules; + private final ModuleManager myModuleManager; + + public TypedModuleServiceImpl(ModuleManager moduleManager, MessageBus messageBus) { + myModuleManager = moduleManager; + messageBus.connect().subscribe(ProjectTopics.MODULES, new ModuleAdapter() { + @Override + public void moduleAdded(Project project, Module module) { + myCachedModules = null; + } + + @Override + public void moduleRemoved(Project project, Module module) { + myCachedModules = null; + } + }); + } + + private MultiMap, Module> getCachedModules() { + if (myCachedModules == null) { + myCachedModules = new MultiMap, Module>(); + for (Module module : myModuleManager.getModules()) { + myCachedModules.putValue(ModuleType.get(module), module); + } + } + return myCachedModules; + } + + @NotNull + @Override + public Collection getModulesOfType(@NotNull ModuleType moduleType) { + return getCachedModules().get(moduleType); + } + + @Override + public boolean hasModulesOfType(@NotNull ModuleType module) { + return !getModulesOfType(module).isEmpty(); + } +} diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index e0082450dc10..c88d789eb012 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -248,6 +248,8 @@ serviceImplementation="com.intellij.facet.impl.invalid.InvalidFacetManagerImpl"/> + diff --git a/plugins/devkit/src/module/PluginModuleType.java b/plugins/devkit/src/module/PluginModuleType.java index 56bc22db96f6..b97ec1bac336 100644 --- a/plugins/devkit/src/module/PluginModuleType.java +++ b/plugins/devkit/src/module/PluginModuleType.java @@ -17,7 +17,6 @@ package org.jetbrains.idea.devkit.module; import com.intellij.icons.AllIcons; import com.intellij.openapi.module.*; -import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.util.IconLoader; @@ -123,15 +122,4 @@ public class PluginModuleType extends ModuleType { public boolean isValidSdk(@NotNull final Module module, final Sdk projectSdk) { return JavaModuleType.isValidJavaSdk(module); } - - public static Module[] getAllPluginModules(final Project project) { - List modules = new ArrayList(); - Module[] allModules = ModuleManager.getInstance(project).getModules(); - for (Module module : allModules) { - if (get(module) == getInstance()) { - modules.add(module); - } - } - return modules.toArray(new Module[modules.size()]); - } } diff --git a/plugins/devkit/src/run/PluginConfigurationType.java b/plugins/devkit/src/run/PluginConfigurationType.java index 8f7394aadeb0..ca4baeca9d4d 100644 --- a/plugins/devkit/src/run/PluginConfigurationType.java +++ b/plugins/devkit/src/run/PluginConfigurationType.java @@ -22,8 +22,10 @@ import com.intellij.execution.configurations.RunConfiguration; import com.intellij.icons.AllIcons; import com.intellij.openapi.application.PathManager; import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.TypedModuleService; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.io.FileUtil; +import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.idea.devkit.DevKitBundle; import org.jetbrains.idea.devkit.module.PluginModuleType; @@ -31,6 +33,7 @@ import org.jetbrains.idea.devkit.module.PluginModuleType; import javax.swing.*; import java.io.File; import java.io.IOException; +import java.util.Collection; public class PluginConfigurationType implements ConfigurationType { private final ConfigurationFactory myFactory; @@ -48,6 +51,11 @@ public class PluginConfigurationType implements ConfigurationType { return runConfiguration; } + @Override + public boolean isApplicable(@NotNull Project project) { + return TypedModuleService.getInstance(project).hasModulesOfType(PluginModuleType.getInstance()); + } + @Override public boolean isConfigurationSingletonByDefault() { return true; @@ -56,10 +64,8 @@ public class PluginConfigurationType implements ConfigurationType { public RunConfiguration createConfiguration(String name, RunConfiguration template) { final PluginRunConfiguration pluginRunConfiguration = (PluginRunConfiguration)template; if (pluginRunConfiguration.getModule() == null) { - final Module[] modules = PluginModuleType.getAllPluginModules(pluginRunConfiguration.getProject()); - if (modules.length > 0){ - pluginRunConfiguration.setModule(modules[0]); - } + final Collection modules = TypedModuleService.getInstance(pluginRunConfiguration.getProject()).getModulesOfType(PluginModuleType.getInstance()); + pluginRunConfiguration.setModule(ContainerUtil.getFirstItem(modules)); } return super.createConfiguration(name, pluginRunConfiguration); }