2 * Copyright 2000-2017 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.jetbrains.python.facet;
18 import com.intellij.facet.Facet;
19 import com.intellij.facet.FacetConfiguration;
20 import com.intellij.facet.FacetType;
21 import com.intellij.facet.ui.FacetEditorContext;
22 import com.intellij.facet.ui.FacetEditorTab;
23 import com.intellij.facet.ui.FacetValidatorsManager;
24 import com.intellij.openapi.application.ApplicationManager;
25 import com.intellij.openapi.module.Module;
26 import com.intellij.openapi.module.ModuleType;
27 import com.intellij.openapi.projectRoots.ProjectJdkTable;
28 import com.intellij.openapi.projectRoots.Sdk;
29 import com.intellij.openapi.util.InvalidDataException;
30 import com.intellij.openapi.util.WriteExternalException;
31 import com.intellij.openapi.util.text.StringUtil;
32 import com.jetbrains.python.sdk.PythonSdkType;
33 import icons.PythonIcons;
34 import org.jdom.Element;
35 import org.jetbrains.annotations.NonNls;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
40 import java.util.List;
42 import static com.jetbrains.python.PythonModuleTypeBase.PYTHON_MODULE;
47 public class PythonFacetType extends FacetType<PythonFacet, PythonFacetType.PythonFacetConfiguration> {
50 private static final String ID = "Python";
52 public static PythonFacetType getInstance() {
53 return findInstance(PythonFacetType.class);
56 public PythonFacetType() {
57 super(PythonFacet.ID, ID, "Python");
60 public PythonFacetConfiguration createDefaultConfiguration() {
61 PythonFacetConfiguration result = new PythonFacetConfiguration();
62 List<Sdk> sdks = ProjectJdkTable.getInstance().getSdksOfType(PythonSdkType.getInstance());
63 if (sdks.size() > 0) {
64 result.setSdk(sdks.get(0));
69 public PythonFacet createFacet(@NotNull Module module,
71 @NotNull PythonFacetConfiguration configuration,
72 @Nullable Facet underlyingFacet) {
73 return new PythonFacet(this, module, name, configuration, underlyingFacet);
76 public boolean isSuitableModuleType(ModuleType moduleType) {
77 return !(moduleType.getId().equals(PYTHON_MODULE));
81 public Icon getIcon() {
82 return PythonIcons.Python.Python;
85 public static class PythonFacetConfiguration extends PythonFacetSettings implements FacetConfiguration {
86 private static final String SDK_NAME = "sdkName";
88 public FacetEditorTab[] createEditorTabs(FacetEditorContext editorContext, FacetValidatorsManager validatorsManager) {
89 return new FacetEditorTab[]{};
92 public void readExternal(Element element) throws InvalidDataException {
93 String sdkName = element.getAttributeValue(SDK_NAME);
94 mySdk = StringUtil.isEmpty(sdkName) ? null : ProjectJdkTable.getInstance().findJdk(sdkName, PythonSdkType.getInstance().getName());
97 ApplicationManager.getApplication().getMessageBus().syncPublisher(ProjectJdkTable.JDK_TABLE_TOPIC).jdkAdded(mySdk);
101 public void writeExternal(Element element) throws WriteExternalException {
102 element.setAttribute(SDK_NAME, mySdk == null ? "" : mySdk.getName());