IDEA-248370 - Show an error when no runtime is configured
[idea/community.git] / platform / projectModel-api / src / com / intellij / openapi / project / ProjectTypeService.java
1 // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.openapi.project;
3
4 import com.intellij.openapi.components.PersistentStateComponent;
5 import com.intellij.openapi.components.ServiceManager;
6 import com.intellij.openapi.components.State;
7 import org.jetbrains.annotations.NotNull;
8 import org.jetbrains.annotations.Nullable;
9 import org.jetbrains.annotations.TestOnly;
10
11 /**
12  * @author Dmitry Avdeev
13  */
14 @State(name = "ProjectType")
15 public class ProjectTypeService implements PersistentStateComponent<ProjectType> {
16   private ProjectType myProjectType;
17
18   @Nullable
19   public static ProjectType getProjectType(@Nullable Project project) {
20     if (project != null) {
21       ProjectType projectType = getInstance(project).myProjectType;
22       if (projectType != null) return projectType;
23     }
24     return DefaultProjectTypeEP.getDefaultProjectType();
25   }
26
27   public static void setProjectType(@NotNull Project project, @NotNull ProjectType projectType) {
28     getInstance(project).loadState(projectType);
29   }
30
31   private static ProjectTypeService getInstance(@NotNull Project project) {
32     return ServiceManager.getService(project, ProjectTypeService.class);
33   }
34
35   @Nullable
36   @Override
37   public ProjectType getState() {
38     return myProjectType;
39   }
40
41   @Override
42   public void loadState(@NotNull ProjectType state) {
43     myProjectType = state;
44   }
45
46   @TestOnly
47   public static void clearFieldsForLightProjectInTests(@NotNull Project project) {
48     getInstance(project).myProjectType = null;
49   }
50 }